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

The following examples show how to use android.content.pm.PackageManager#getDrawable() . 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 check out the related API usage on the sidebar.
Example 1
Source File: SuggestionsAdapter.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: SuggestionsAdapter.java    From CSipSimple with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: SuggestionsAdapter.java    From zen4android with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: SuggestionsAdapter.java    From zhangshangwuda with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: SuggestionsAdapter.java    From Libraries-for-Android-Developers with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: PrinterInfo.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Get the icon to be used for this printer. If no per printer icon is available, the printer's
 * service's icon is returned. If the printer has a custom icon this icon might get requested
 * asynchronously. Once the icon is loaded the discovery sessions will be notified that the
 * printer changed.
 *
 * @param context The context that will be using the icons
 * @return The icon to be used for the printer or null if no icon could be found.
 * @hide
 */
@TestApi
public @Nullable Drawable loadIcon(@NonNull Context context) {
    Drawable drawable = null;
    PackageManager packageManager = context.getPackageManager();

    if (mHasCustomPrinterIcon) {
        PrintManager printManager = (PrintManager) context
                .getSystemService(Context.PRINT_SERVICE);

        Icon icon = printManager.getCustomPrinterIcon(mId);

        if (icon != null) {
            drawable = icon.loadDrawable(context);
        }
    }

    if (drawable == null) {
        try {
            String packageName = mId.getServiceName().getPackageName();
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
            ApplicationInfo appInfo = packageInfo.applicationInfo;

            // If no custom icon is available, try the icon from the resources
            if (mIconResourceId != 0) {
                drawable = packageManager.getDrawable(packageName, mIconResourceId, appInfo);
            }

            // Fall back to the printer's service's icon if no per printer icon could be found
            if (drawable == null) {
                drawable = appInfo.loadIcon(packageManager);
            }
        } catch (NameNotFoundException e) {
        }
    }

    return drawable;
}
 
Example 7
Source File: WallpaperInfo.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Load the thumbnail image for this wallpaper.
 * 
 * @param pm Supply a PackageManager used to load the wallpaper's
 * resources.
 */
public Drawable loadThumbnail(PackageManager pm) {
    if (mThumbnailResource < 0) return null;

    return pm.getDrawable(mService.serviceInfo.packageName,
                          mThumbnailResource,
                          mService.serviceInfo.applicationInfo);
}
 
Example 8
Source File: IconUtils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
public static Drawable loadPackageIcon(Context context, String authority, int icon) {
    if (icon != 0) {
        if (authority != null) {
            final PackageManager pm = context.getPackageManager();
            final ProviderInfo info = pm.resolveContentProvider(authority, 0);
            if (info != null) {
                return pm.getDrawable(info.packageName, icon, info.applicationInfo);
            }
        } else {
            return ContextCompat.getDrawable(context, icon);
        }
    }
    return null;
}
 
Example 9
Source File: IconUtils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
public static Drawable loadPackageIcon(Context context, String authority, int icon) {
    if (icon != 0) {
        if (authority != null) {
            final PackageManager pm = context.getPackageManager();
            final ProviderInfo info = pm.resolveContentProvider(authority, 0);
            if (info != null) {
                return pm.getDrawable(info.packageName, icon, info.applicationInfo);
            }
        } else {
            return ContextCompat.getDrawable(context, icon);
        }
    }
    return null;
}
 
Example 10
Source File: IconUtils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
public static Drawable loadPackageIcon(Context context, String authority, int icon) {
    if (icon != 0) {
        if (authority != null) {
            final PackageManager pm = context.getPackageManager();
            final ProviderInfo info = pm.resolveContentProvider(authority, 0);
            if (info != null) {
                return pm.getDrawable(info.packageName, icon, info.applicationInfo);
            }
        } else {
            return ContextCompat.getDrawable(context, icon);
        }
    }
    return null;
}
 
Example 11
Source File: AppInfo.java    From island with Apache License 2.0 5 votes vote down vote up
private Drawable loadUnbadgedIconCompat(final PackageManager pm) {
	if (SDK_INT >= LOLLIPOP_MR1) try {
		return loadUnbadgedIcon(pm);
	} catch (final SecurityException e) {		// Appears on some Samsung devices (e.g. Galaxy S7, Note 8) with Android 8.0
		Analytics.$().logAndReport(TAG, "Error loading unbadged icon for " + this, e);
	}
	Drawable dr = null;
	if (packageName != null) dr = pm.getDrawable(packageName, icon, this);
	if (dr == null) dr = pm.getDefaultActivityIcon();
	return dr;
}
 
Example 12
Source File: ContactAdderFragment.java    From ETSMobile-Android2 with Apache License 2.0 5 votes vote down vote up
/**
 * @param name
 *            The name of the account. This is usually the user's email
 *            address or username.
 * @param description
 *            The description for this account. This will be dictated by
 *            the type of account returned, and can be obtained from the
 *            system AccountManager.
 */
public AccountData(String name, AuthenticatorDescription description) {
	mName = name;
	if (description != null) {
		mType = description.type;

		// The type string is stored in a resource, so we need to
		// convert it into something
		// human readable.
		final String packageName = description.packageName;
		final PackageManager pm = getActivity().getPackageManager();

		if (description.labelId != 0) {
			mTypeLabel = pm.getText(packageName, description.labelId,
					null);
			if (mTypeLabel == null) {
				throw new IllegalArgumentException(
						"LabelID provided, but label not found");
			}
		} else {
			mTypeLabel = "";
		}

		if (description.iconId != 0) {
			mIcon = pm.getDrawable(packageName, description.iconId,
					null);
			if (mIcon == null) {
				throw new IllegalArgumentException(
						"IconID provided, but drawable not " + "found");
			}
		} else {
			mIcon = getResources().getDrawable(
					android.R.drawable.sym_def_app_icon);
		}
	}
}