Example usage for android.content.pm PackageManager getResourcesForActivity

List of usage examples for android.content.pm PackageManager getResourcesForActivity

Introduction

In this page you can find the example usage for android.content.pm PackageManager getResourcesForActivity.

Prototype

public abstract Resources getResourcesForActivity(ComponentName activityName) throws NameNotFoundException;

Source Link

Document

Retrieve the resources associated with an activity.

Usage

From source file:de.baumann.thema.RequestActivity.java

private Drawable getHighResIcon(PackageManager pm, ResolveInfo resolveInfo) {

    Resources resources;/*from www.j  a  v a  2 s .c om*/
    Drawable icon;

    try {
        ComponentName componentName = new ComponentName(resolveInfo.activityInfo.packageName,
                resolveInfo.activityInfo.name);

        resources = pm.getResourcesForActivity(componentName);//Get resources for the activity

        int iconId = resolveInfo.getIconResource();//Get the resource Id for the activity icon

        if (iconId != 0) {
            icon = resources.getDrawableForDensity(iconId, 640, null); //Loads the icon at xxhdpi resolution or lower.
            return icon;
        }
        return resolveInfo.loadIcon(pm);

    } catch (PackageManager.NameNotFoundException e) {
        return resolveInfo.loadIcon(pm);//If it fails return the normal icon
    } catch (Resources.NotFoundException e) {
        return resolveInfo.loadIcon(pm);
    }
}

From source file:com.android.incallui.widget.multiwaveview.GlowPadView.java

/**
 * Searches the given package for a resource to use to replace the Drawable on the
 * target with the given resource id//from  w  w w  .j  a  va 2s . c  om
 * @param component of the .apk that contains the resource
 * @param name of the metadata in the .apk
 * @param existingResId the resource id of the target to search for
 * @return true if found in the given package and replaced at least one target Drawables
 */
public boolean replaceTargetDrawablesIfPresent(ComponentName component, String name, int existingResId) {
    if (existingResId == 0)
        return false;

    boolean replaced = false;
    if (component != null) {
        try {
            PackageManager packageManager = getContext().getPackageManager();
            // Look for the search icon specified in the activity meta-data
            Bundle metaData = packageManager.getActivityInfo(component, PackageManager.GET_META_DATA).metaData;
            if (metaData != null) {
                int iconResId = metaData.getInt(name);
                if (iconResId != 0) {
                    Resources res = packageManager.getResourcesForActivity(component);
                    replaced = replaceTargetDrawables(res, existingResId, iconResId);
                }
            }
        } catch (NameNotFoundException e) {
            Log.w(TAG, "Failed to swap drawable; " + component.flattenToShortString() + " not found", e);
        } catch (Resources.NotFoundException nfe) {
            Log.w(TAG, "Failed to swap drawable from " + component.flattenToShortString(), nfe);
        }
    }
    if (!replaced) {
        // Restore the original drawable
        replaceTargetDrawables(getContext().getResources(), existingResId, existingResId);
    }
    return replaced;
}

From source file:com.android.launcher2.Launcher.java

private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
    try {//  w  w w  .ja v  a2  s .  co  m
        PackageManager packageManager = getPackageManager();
        // Look for the toolbar icon specified in the activity meta-data
        Bundle metaData = packageManager.getActivityInfo(activityName, PackageManager.GET_META_DATA).metaData;
        if (metaData != null) {
            int iconResId = metaData.getInt(resourceName);
            if (iconResId != 0) {
                Resources res = packageManager.getResourcesForActivity(activityName);
                return res.getDrawable(iconResId);
            }
        }
    } catch (NameNotFoundException e) {
        // This can happen if the activity defines an invalid drawable
        Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() + " not found", e);
    } catch (Resources.NotFoundException nfe) {
        // This can happen if the activity defines an invalid drawable
        Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(), nfe);
    }
    return null;
}