Java Code Examples for android.content.pm.PackageManager#getActivityIcon()

The following examples show how to use android.content.pm.PackageManager#getActivityIcon() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * Pre-load an icon into the persistent cache.
 *
 * <P>Queries for a component that does not exist in the package manager
 * will be answered by the persistent cache.
 *
 * @param componentName the icon should be returned for this component
 * @param icon the icon to be persisted
 * @param dpi the native density of the icon
 */
public void preloadIcon(ComponentName componentName, Bitmap icon, int dpi, String label,
        long userSerial, InvariantDeviceProfile idp) {
    // TODO rescale to the correct native DPI
    try {
        PackageManager packageManager = mContext.getPackageManager();
        packageManager.getActivityIcon(componentName);
        // component is present on the system already, do nothing
        return;
    } catch (PackageManager.NameNotFoundException e) {
        // pass
    }

    ContentValues values = newContentValues(
            Bitmap.createScaledBitmap(icon, idp.iconBitmapSize, idp.iconBitmapSize, true),
            label, Color.TRANSPARENT);
    values.put(IconDB.COLUMN_COMPONENT, componentName.flattenToString());
    values.put(IconDB.COLUMN_USER, userSerial);
    mIconDb.getWritableDatabase().insertWithOnConflict(IconDB.TABLE_NAME, null, values,
            SQLiteDatabase.CONFLICT_REPLACE);
}
 
Example 2
public Drawable getCallingActivityIcon()
{
    if (e == null)
    {
        return null;
    }
    PackageManager packagemanager = b.getPackageManager();
    Drawable drawable;
    try
    {
        drawable = packagemanager.getActivityIcon(e);
    }
    catch (android.content.pm.ndException ndexception)
    {
        Log.e("IntentReader", "Could not retrieve icon for calling activity", ndexception);
        return null;
    }
    return drawable;
}
 
Example 3
/**
 * Loads an icon from the icon pack based on the received package name.
 *
 * @param activity       where LauncherApps service can be retrieved.
 * @param appPackageName Package name of the app whose icon is to be loaded.
 *
 * @return Drawable Will return null if there is no icon associated with the package name,
 * otherwise an associated icon from the icon pack will be returned.
 */
private static Drawable getIconDrawable(Activity activity, String appPackageName, long user) {
    PackageManager packageManager = activity.getPackageManager();
    String componentName = "ComponentInfo{" + appPackageName + "}";
    Resources iconRes = null;
    Drawable defaultIcon = null;

    try {
        if (Utils.atLeastLollipop()) {
            LauncherApps launcher = (LauncherApps) activity.getSystemService(
                    Context.LAUNCHER_APPS_SERVICE);
            UserManager userManager = (UserManager) activity.getSystemService(
                    Context.USER_SERVICE);

            if (userManager != null && launcher != null) {
                defaultIcon = launcher.getActivityList(AppUtils.getPackageName(appPackageName),
                        userManager.getUserForSerialNumber(user))
                                      .get(0).getBadgedIcon(0);
            }
        } else {
            defaultIcon = packageManager.getActivityIcon(
                    ComponentName.unflattenFromString(appPackageName));
        }

        if (!"default".equals(iconPackageName)) {
            iconRes = packageManager.getResourcesForApplication(iconPackageName);
        } else {
            return defaultIcon;
        }
    } catch (PackageManager.NameNotFoundException e) {
        Utils.sendLog(Utils.LogLevel.ERROR, e.toString());
    }

    String drawable = mPackagesDrawables.get(componentName);
    if (drawable != null && iconRes != null) {
        return loadDrawable(iconRes, drawable, iconPackageName);
    } else {
        return defaultIcon;
    }
}
 
Example 4
Source Project: AndroidUtilCode   File: ActivityUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Return the icon of activity.
 *
 * @param activityName The name of activity.
 * @return the icon of activity
 */
public static Drawable getActivityIcon(@NonNull final ComponentName activityName) {
    PackageManager pm = Utils.getApp().getPackageManager();
    try {
        return pm.getActivityIcon(activityName);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 5
/**
 * Loads an icon from the icon pack based on the received package name.
 *
 * @param activity       where LauncherApps service can be retrieved.
 * @param appPackageName Package name of the app whose icon is to be loaded.
 *
 * @return Drawable Will return null if there is no icon associated with the package name,
 * otherwise an associated icon from the icon pack will be returned.
 */
private static Drawable getIconDrawable(Activity activity, String appPackageName, long user) {
    PackageManager packageManager = activity.getPackageManager();
    String componentName = "ComponentInfo{" + appPackageName + "}";
    Resources iconRes = null;
    Drawable defaultIcon = null;

    try {
        if (Utils.atLeastLollipop()) {
            LauncherApps launcher = (LauncherApps) activity.getSystemService(
                    Context.LAUNCHER_APPS_SERVICE);
            UserManager userManager = (UserManager) activity.getSystemService(
                    Context.USER_SERVICE);

            if (userManager != null && launcher != null) {
                defaultIcon = launcher.getActivityList(AppUtils.getPackageName(appPackageName),
                        userManager.getUserForSerialNumber(user))
                                      .get(0).getBadgedIcon(0);
            }
        } else {
            defaultIcon = packageManager.getActivityIcon(
                    ComponentName.unflattenFromString(appPackageName));
        }

        if (!"default".equals(iconPackageName)) {
            iconRes = packageManager.getResourcesForApplication(iconPackageName);
        } else {
            return defaultIcon;
        }
    } catch (PackageManager.NameNotFoundException e) {
        Utils.sendLog(Utils.LogLevel.ERROR, e.toString());
    }

    String drawable = mPackagesDrawables.get(componentName);
    if (drawable != null && iconRes != null) {
        return loadDrawable(iconRes, drawable, iconPackageName);
    } else {
        return defaultIcon;
    }
}
 
Example 6
/**
 * Get the icon of the calling activity as a Drawable if data about
 * the calling activity is available.
 *
 * <p><em>Note:</em> This data may have been provided voluntarily by the calling
 * application. As such it should not be trusted for accuracy in the context of
 * security or verification.</p>
 *
 * @return The calling Activity's icon or null if unknown
 */
public Drawable getCallingActivityIcon() {
    if (mCallingActivity == null) return null;

    PackageManager pm = mActivity.getPackageManager();
    try {
        return pm.getActivityIcon(mCallingActivity);
    } catch (NameNotFoundException e) {
        Log.e(TAG, "Could not retrieve icon for calling activity", e);
    }
    return null;
}
 
Example 7
/**
 * Get the icon of the calling activity as a Drawable if data about
 * the calling activity is available.
 *
 * <p><em>Note:</em> This data may have been provided voluntarily by the calling
 * application. As such it should not be trusted for accuracy in the context of
 * security or verification.</p>
 *
 * @return The calling Activity's icon or null if unknown
 */
public Drawable getCallingActivityIcon() {
    if (mCallingActivity == null) return null;

    PackageManager pm = mActivity.getPackageManager();
    try {
        return pm.getActivityIcon(mCallingActivity);
    } catch (NameNotFoundException e) {
        Log.e(TAG, "Could not retrieve icon for calling activity", e);
    }
    return null;
}
 
Example 8
Source Project: android-recipes-app   File: ShareCompat.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get the icon of the calling activity as a Drawable if data about
 * the calling activity is available.
 *
 * <p><em>Note:</em> This data may have been provided voluntarily by the calling
 * application. As such it should not be trusted for accuracy in the context of
 * security or verification.</p>
 *
 * @return The calling Activity's icon or null if unknown
 */
public Drawable getCallingActivityIcon() {
    if (mCallingActivity == null) return null;

    PackageManager pm = mActivity.getPackageManager();
    try {
        return pm.getActivityIcon(mCallingActivity);
    } catch (NameNotFoundException e) {
        Log.e(TAG, "Could not retrieve icon for calling activity", e);
    }
    return null;
}
 
Example 9
Source Project: V.FlyoutTest   File: ShareCompat.java    License: MIT License 5 votes vote down vote up
/**
 * Get the icon of the calling activity as a Drawable if data about
 * the calling activity is available.
 *
 * <p><em>Note:</em> This data may have been provided voluntarily by the calling
 * application. As such it should not be trusted for accuracy in the context of
 * security or verification.</p>
 *
 * @return The calling Activity's icon or null if unknown
 */
public Drawable getCallingActivityIcon() {
    if (mCallingActivity == null) return null;

    PackageManager pm = mActivity.getPackageManager();
    try {
        return pm.getActivityIcon(mCallingActivity);
    } catch (NameNotFoundException e) {
        Log.e(TAG, "Could not retrieve icon for calling activity", e);
    }
    return null;
}
 
Example 10
@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final AppInfo info = getItem(position);

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item, parent, false);
		}

		//info.icon = Utilities.createIconThumbnail(info.icon, getContext());

		final TextView textView = ViewHolder.get(convertView, R.id.item_text);
		textView.setText(info.title);
        
		Bitmap bitmap = mMemoryCache.get(info.title_py);
		if (bitmap == null) {
			final PackageManager manager = getContext().getPackageManager();
			Drawable icon = null;
			try {
				icon = manager.getActivityIcon(info.mComponentName);
			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			bitmap = Utilities.createIcontoBitmapThumbnail(icon, this.getContext());
			mMemoryCache.put(info.title_py, bitmap);
		}
		
		final ImageView imageView = ViewHolder.get(convertView, R.id.item_image);
		//imageView.setImageDrawable(info.icon);
		imageView.setImageBitmap(bitmap);
//		imageView.setImageResource(R.drawable.ic_launcher);
		return convertView;
	}
 
Example 11
Source Project: guideshow   File: ShareCompat.java    License: MIT License 5 votes vote down vote up
/**
 * Get the icon of the calling activity as a Drawable if data about
 * the calling activity is available.
 *
 * <p><em>Note:</em> This data may have been provided voluntarily by the calling
 * application. As such it should not be trusted for accuracy in the context of
 * security or verification.</p>
 *
 * @return The calling Activity's icon or null if unknown
 */
public Drawable getCallingActivityIcon() {
    if (mCallingActivity == null) return null;

    PackageManager pm = mActivity.getPackageManager();
    try {
        return pm.getActivityIcon(mCallingActivity);
    } catch (NameNotFoundException e) {
        Log.e(TAG, "Could not retrieve icon for calling activity", e);
    }
    return null;
}