Exemplo n.º 1
0
 private Drawable getSubtypeIcon(
     InputMethodInfoCompatWrapper imi, InputMethodSubtypeCompatWrapper subtype) {
   final PackageManager pm = mService.getPackageManager();
   if (imi != null) {
     final String imiPackageName = imi.getPackageName();
     if (DBG) {
       Log.d(
           TAG,
           "Update icons of IME: "
               + imiPackageName
               + ","
               + getSubtypeLocale(subtype)
               + ","
               + subtype.getMode());
     }
     if (subtype != null) {
       return pm.getDrawable(
           imiPackageName, subtype.getIconResId(), imi.getServiceInfo().applicationInfo);
     } else if (imi.getSubtypeCount() > 0 && imi.getSubtypeAt(0) != null) {
       return pm.getDrawable(
           imiPackageName,
           imi.getSubtypeAt(0).getIconResId(),
           imi.getServiceInfo().applicationInfo);
     } else {
       try {
         return pm.getApplicationInfo(imiPackageName, 0).loadIcon(pm);
       } catch (PackageManager.NameNotFoundException e) {
         Log.w(TAG, "IME can't be found: " + imiPackageName);
       }
     }
   }
   return null;
 }
Exemplo n.º 2
0
 private Drawable getSubtypeIcon(InputMethodInfo imi, InputMethodSubtype subtype) {
   if (imi != null) {
     if (DEBUG) {
       Log.d(TAG, "Update icons of IME: " + imi.getPackageName());
       if (subtype != null) {
         Log.d(TAG, "subtype =" + subtype.getLocale() + "," + subtype.getMode());
       }
     }
     if (subtype != null) {
       return mPackageManager.getDrawable(
           imi.getPackageName(), subtype.getIconResId(), imi.getServiceInfo().applicationInfo);
     } else if (imi.getSubtypeCount() > 0) {
       return mPackageManager.getDrawable(
           imi.getPackageName(),
           imi.getSubtypeAt(0).getIconResId(),
           imi.getServiceInfo().applicationInfo);
     } else {
       try {
         return mPackageManager
             .getApplicationInfo(imi.getPackageName(), 0)
             .loadIcon(mPackageManager);
       } catch (PackageManager.NameNotFoundException e) {
         Log.w(TAG, "IME can't be found: " + imi.getPackageName());
       }
     }
   }
   return null;
 }
Exemplo n.º 3
0
    /** {@inheritDoc} */
    public Drawable getFallbackIcon() {
      // Bail early if no valid resources
      final String resPackageName = mKind.resPackageName;
      if (resPackageName == null) return null;

      final PackageManager pm = mContext.getPackageManager();
      if (mAlternate && mKind.iconAltRes != -1) {
        return pm.getDrawable(resPackageName, mKind.iconAltRes, null);
      } else if (mKind.iconRes != -1) {
        return pm.getDrawable(resPackageName, mKind.iconRes, null);
      } else {
        return null;
      }
    }
Exemplo n.º 4
0
  public Drawable getIcon(Context context) {
    final PackageManager pm = context.getPackageManager();

    // Use local package for resources when none requested
    if (mResPackage == null) mResPackage = context.getPackageName();

    final boolean validIcon = mResPackage != null && mIconRes != -1;
    return validIcon ? pm.getDrawable(mResPackage, mIconRes, null) : null;
  }
  /** Load label and icon for this entry */
  public void init(Context context, Account account, AuthenticatorDescription description) {
    PackageManager pm = context.getPackageManager();
    label = description.packageName;
    try {
      label = pm.getResourcesForApplication(description.packageName).getString(description.labelId);
    } catch (NotFoundException | NameNotFoundException e) {
      Log.e(Constants.TAG, "Error retrieving label!", e);
    }

    icon = pm.getDrawable(description.packageName, description.iconId, null);
  }
Exemplo n.º 6
0
  @Override
  public Drawable getAlternateIcon() {
    if (mAlternateIconRes == 0) return null;

    final String resourcePackageName = mKind.resourcePackageName;
    if (resourcePackageName == null) {
      return mContext.getResources().getDrawable(mAlternateIconRes);
    }

    final PackageManager pm = mContext.getPackageManager();
    return pm.getDrawable(resourcePackageName, mAlternateIconRes, null);
  }
Exemplo n.º 7
0
  private static void fillIcons(Context c) {
    AccountManager am = AccountManager.get(c);
    AuthenticatorDescription[] auths = am.getAuthenticatorTypes();
    PackageManager pm = c.getPackageManager();
    for (AuthenticatorDescription auth : auths) {
      accountIcons.put(auth.type, pm.getDrawable(auth.packageName, auth.iconId, null));
      /*	Log.i("Account:", auth.type);
         Log.i("pkg:", auth.packageName);
      Log.i("icon:", String.valueOf(auth.smallIconId));*/

    }
  }
 @Nullable
 private static Drawable loadDrawable(
     @NonNull final PackageManager packageManager,
     @NonNull final String packageName,
     @DrawableRes final int resId,
     @NonNull final ApplicationInfo applicationInfo) {
   if (resId == 0) {
     return null;
   }
   try {
     return packageManager.getDrawable(packageName, resId, applicationInfo);
   } catch (Exception e) {
     return null;
   }
 }
 /**
  * Gets the activity or application icon for an activity.
  *
  * @param component Name of an activity.
  * @return A drawable, or {@code null} if neither the acitivy or the application have an icon set.
  */
 private Drawable getActivityIcon(ComponentName component) {
   PackageManager pm = mContext.getPackageManager();
   final ActivityInfo activityInfo;
   try {
     activityInfo = pm.getActivityInfo(component, PackageManager.GET_META_DATA);
   } catch (NameNotFoundException ex) {
     Log.w(LOG_TAG, ex.toString());
     return null;
   }
   int iconId = activityInfo.getIconResource();
   if (iconId == 0) return null;
   String pkg = component.getPackageName();
   Drawable drawable = pm.getDrawable(pkg, iconId, activityInfo.applicationInfo);
   if (drawable == null) {
     Log.w(
         LOG_TAG, "Invalid icon resource " + iconId + " for " + component.flattenToShortString());
     return null;
   }
   return drawable;
 }
  public Bitmap generateWidgetPreview(
      ComponentName provider,
      int previewImage,
      int iconId,
      int cellHSpan,
      int cellVSpan,
      int maxPreviewWidth,
      int maxPreviewHeight,
      Bitmap preview,
      int[] preScaledWidthOut) {
    // Load the preview image if possible
    String packageName = provider.getPackageName();
    if (maxPreviewWidth < 0) maxPreviewWidth = Integer.MAX_VALUE;
    if (maxPreviewHeight < 0) maxPreviewHeight = Integer.MAX_VALUE;

    Drawable drawable = null;
    if (previewImage != 0) {
      drawable = mPackageManager.getDrawable(packageName, previewImage, null);
      if (drawable == null) {
        Log.w(
            TAG,
            "Can't load widget preview drawable 0x"
                + Integer.toHexString(previewImage)
                + " for provider: "
                + provider);
      }
    }

    int previewWidth;
    int previewHeight;
    Bitmap defaultPreview = null;
    boolean widgetPreviewExists = (drawable != null);
    if (widgetPreviewExists) {
      previewWidth = drawable.getIntrinsicWidth();
      previewHeight = drawable.getIntrinsicHeight();
    } else {
      // Generate a preview image if we couldn't load one
      if (cellHSpan < 1) cellHSpan = 1;
      if (cellVSpan < 1) cellVSpan = 1;

      BitmapDrawable previewDrawable =
          (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.widget_tile);
      final int previewDrawableWidth = previewDrawable.getIntrinsicWidth();
      final int previewDrawableHeight = previewDrawable.getIntrinsicHeight();
      previewWidth = previewDrawableWidth * cellHSpan;
      previewHeight = previewDrawableHeight * cellVSpan;

      defaultPreview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
      final Canvas c = mCachedAppWidgetPreviewCanvas.get();
      c.setBitmap(defaultPreview);
      previewDrawable.setBounds(0, 0, previewWidth, previewHeight);
      previewDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
      previewDrawable.draw(c);
      c.setBitmap(null);

      // Draw the icon in the top left corner
      int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
      int smallestSide = Math.min(previewWidth, previewHeight);
      float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);

      try {
        Drawable icon = null;
        int hoffset = (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
        int yoffset = (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
        if (iconId > 0) icon = mIconCache.getFullResIcon(packageName, iconId);
        if (icon != null) {
          renderDrawableToBitmap(
              icon,
              defaultPreview,
              hoffset,
              yoffset,
              (int) (mAppIconSize * iconScale),
              (int) (mAppIconSize * iconScale));
        }
      } catch (Resources.NotFoundException e) {
      }
    }

    // Scale to fit width only - let the widget preview be clipped in the
    // vertical dimension
    float scale = 1f;
    if (preScaledWidthOut != null) {
      preScaledWidthOut[0] = previewWidth;
    }
    if (previewWidth > maxPreviewWidth) {
      scale = maxPreviewWidth / (float) previewWidth;
    }
    if (scale != 1f) {
      previewWidth = (int) (scale * previewWidth);
      previewHeight = (int) (scale * previewHeight);
    }

    // If a bitmap is passed in, we use it; otherwise, we create a bitmap of the right size
    if (preview == null) {
      preview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
    }

    // Draw the scaled preview into the final bitmap
    int x = (preview.getWidth() - previewWidth) / 2;
    if (widgetPreviewExists) {
      renderDrawableToBitmap(drawable, preview, x, 0, previewWidth, previewHeight);
    } else {
      final Canvas c = mCachedAppWidgetPreviewCanvas.get();
      final Rect src = mCachedAppWidgetPreviewSrcRect.get();
      final Rect dest = mCachedAppWidgetPreviewDestRect.get();
      c.setBitmap(preview);
      src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
      dest.set(x, 0, x + previewWidth, previewHeight);

      Paint p = mCachedAppWidgetPreviewPaint.get();
      if (p == null) {
        p = new Paint();
        p.setFilterBitmap(true);
        mCachedAppWidgetPreviewPaint.set(p);
      }
      c.drawBitmap(defaultPreview, src, dest, p);
      c.setBitmap(null);
    }
    return preview;
  }