Java Code Examples for android.content.res.Resources#getDrawableForDensity()

The following examples show how to use android.content.res.Resources#getDrawableForDensity() . 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: LauncherActivityInfo.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the icon for this activity, without any badging for the profile.
 * @param density The preferred density of the icon, zero for default density. Use
 * density DPI values from {@link DisplayMetrics}.
 * @see #getBadgedIcon(int)
 * @see DisplayMetrics
 * @return The drawable associated with the activity.
 */
public Drawable getIcon(int density) {
    // TODO: Go through LauncherAppsService
    final int iconRes = mActivityInfo.getIconResource();
    Drawable icon = null;
    // Get the preferred density icon from the app's resources
    if (density != 0 && iconRes != 0) {
        try {
            final Resources resources
                    = mPm.getResourcesForApplication(mActivityInfo.applicationInfo);
            icon = resources.getDrawableForDensity(iconRes, density);
        } catch (NameNotFoundException | Resources.NotFoundException exc) {
        }
    }
    // Get the default density icon
    if (icon == null) {
        icon = mActivityInfo.loadIcon(mPm);
    }
    return icon;
}
 
Example 2
Source File: AppWidgetProviderInfo.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private Drawable loadDrawable(Context context, int density, int resourceId,
        boolean loadDefaultIcon) {
    try {
        Resources resources = context.getPackageManager().getResourcesForApplication(
                providerInfo.applicationInfo);
        if (ResourceId.isValid(resourceId)) {
            if (density < 0) {
                density = 0;
            }
            return resources.getDrawableForDensity(resourceId, density, null);
        }
    } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
        /* ignore */
    }
    return loadDefaultIcon ? providerInfo.loadIcon(context.getPackageManager()) : null;
}
 
Example 3
Source File: IconThemer.java    From LaunchEnr with GNU General Public License v3.0 6 votes vote down vote up
private Drawable getRoundIcon(Context context,String packageName, int iconDpi) {

        mPackageManager = context.getPackageManager();

        try {
            Resources resourcesForApplication = mPackageManager.getResourcesForApplication(packageName);
            AssetManager assets = resourcesForApplication.getAssets();
            XmlResourceParser parseXml = assets.openXmlResourceParser("AndroidManifest.xml");
            int eventType;
            while ((eventType = parseXml.nextToken()) != XmlPullParser.END_DOCUMENT)
                if (eventType == XmlPullParser.START_TAG && parseXml.getName().equals("application"))
                    for (int i = 0; i < parseXml.getAttributeCount(); i++)
                        if (parseXml.getAttributeName(i).equals("roundIcon"))
                            return resourcesForApplication.getDrawableForDensity(Integer.parseInt(parseXml.getAttributeValue(i).substring(1)), iconDpi, context.getTheme());
            parseXml.close();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
 
Example 4
Source File: LauncherActivityInfoCompatV16.java    From Trebuchet with GNU General Public License v3.0 6 votes vote down vote up
public Drawable getIcon(int density) {
    int iconRes = mResolveInfo.getIconResource();
    Resources resources = null;
    Drawable icon = null;
    // Get the preferred density icon from the app's resources
    if (density != 0 && iconRes != 0) {
        try {
            resources = mPm.getResourcesForApplication(mActivityInfo.applicationInfo);
            icon = resources.getDrawableForDensity(iconRes, density);
        } catch (NameNotFoundException | Resources.NotFoundException exc) {
        }
    }
    // Get the default density icon
    if (icon == null) {
        icon = mResolveInfo.loadIcon(mPm);
    }
    if (icon == null) {
        resources = Resources.getSystem();
        icon = resources.getDrawableForDensity(android.R.mipmap.sym_def_app_icon, density);
    }
    return icon;
}
 
Example 5
Source File: LauncherApps.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private Drawable loadDrawableResourceFromPackage(String packageName, int resId,
        UserHandle user, int density) {
    try {
        if (resId == 0) {
            return null; // Shouldn't happen but just in case.
        }
        final ApplicationInfo ai = getApplicationInfo(packageName, /* flags =*/ 0, user);
        final Resources res = mContext.getPackageManager().getResourcesForApplication(ai);
        return res.getDrawableForDensity(resId, density);
    } catch (NameNotFoundException | Resources.NotFoundException e) {
        return null;
    }
}
 
Example 6
Source File: IconCache.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
private Drawable getFullResIcon(Resources resources, int iconId) {
    Drawable d;
    try {
        d = resources.getDrawableForDensity(iconId, mIconDpi, mContext.getTheme());
    } catch (Resources.NotFoundException e) {
        d = null;
    }

    return (d != null) ? d : getFullResDefaultActivityIcon();
}
 
Example 7
Source File: ApiCompatibilityUtils.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @see android.content.res.Resources#getDrawableForDensity(int id, int density).
 */
@SuppressWarnings("deprecation")
public static Drawable getDrawableForDensity(Resources res, int id, int density) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return res.getDrawableForDensity(id, density, null);
    } else {
        return res.getDrawableForDensity(id, density);
    }
}
 
Example 8
Source File: ResolverActivity.java    From container with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
Drawable getIcon(Resources res, int resId) {
    Drawable result;
    try {
        result = res.getDrawableForDensity(resId, mIconDpi);
    } catch (Resources.NotFoundException e) {
        result = null;
    }

    return result;
}
 
Example 9
Source File: AbstractAppLaunchShortcut.java    From island with Apache License 2.0 5 votes vote down vote up
private static Bitmap makeShortcutIconBitmap(final Context context, final ApplicationInfo app, final UserHandle user, final int icon) {
	final PackageManager pm = context.getPackageManager();
	Drawable drawable = null;
	if (icon != 0) try {
		final Resources resources = pm.getResourcesForApplication(app);
		drawable = resources.getDrawableForDensity(icon, getDpiForLargeIcon(resources.getDisplayMetrics().densityDpi), null);
	} catch (final NameNotFoundException | Resources.NotFoundException ignored) {}
	if (drawable == null) drawable = app.loadIcon(pm);		// Fallback to default density icon
	if (SDK_INT < O && ! Users.isOwner(user))	// Without badge icon on Android O+, since launcher will use the icon of Island as badge.
		drawable = pm.getUserBadgedIcon(drawable, user);
	return ShortcutIcons.createLargeIconBitmap(context, drawable, app.packageName);
}
 
Example 10
Source File: IconCache.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
private Drawable getFullResIcon(Resources resources, int iconId) {
    Drawable d;
    try {
        d = resources.getDrawableForDensity(iconId, mIconDpi);
    } catch (Resources.NotFoundException e) {
        d = null;
    }

    return (d != null) ? d : getFullResDefaultActivityIcon();
}
 
Example 11
Source File: IconCache.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
public Drawable getFullResIcon(Resources resources, int iconId) {
	Drawable d;
	try {
		d = resources.getDrawableForDensity(iconId, mIconDpi);
	} catch (Resources.NotFoundException e) {
		d = null;
	}

	return (d != null) ? d : getFullResDefaultActivityIcon();
}
 
Example 12
Source File: ApiCompatibilityUtils.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * @see android.content.res.Resources#getDrawableForDensity(int id, int density).
 */
@SuppressWarnings("deprecation")
public static Drawable getDrawableForDensity(Resources res, int id, int density) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return res.getDrawableForDensity(id, density, null);
    } else {
        return res.getDrawableForDensity(id, density);
    }
}
 
Example 13
Source File: AppIconRequestHandler.java    From AndroidProcesses with Apache License 2.0 5 votes vote down vote up
private Bitmap getFullResIcon(Resources resources, int iconId) {
  final Drawable drawable;
  try {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
      drawable = resources.getDrawableForDensity(iconId, dpi, null);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
      drawable = resources.getDrawableForDensity(iconId, dpi);
    } else {
      drawable = resources.getDrawable(iconId);
    }
  } catch (Resources.NotFoundException e) {
    return getFullResDefaultActivityIcon();
  }
  return drawableToBitmap(drawable);
}
 
Example 14
Source File: IconCache.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
private Drawable getFullResIcon(Resources resources, int iconId) {
    Drawable d;
    try {
        d = resources.getDrawableForDensity(iconId, mIconDpi);
    } catch (Resources.NotFoundException e) {
        d = null;
    }

    return (d != null) ? d : getFullResDefaultActivityIcon();
}
 
Example 15
Source File: IconCache.java    From KAM with GNU General Public License v3.0 5 votes vote down vote up
public Drawable getFullResIcon(Resources resources, int iconId) {
    Drawable d;
    try {
        d = resources.getDrawableForDensity(iconId, mIconDpi);
    } catch (Resources.NotFoundException e) {
        d = null;
    }

    return (d != null) ? d : getFullResDefaultActivityIcon();
}
 
Example 16
Source File: IconCacheHelper.java    From Hangar with GNU General Public License v3.0 5 votes vote down vote up
public Drawable getFullResIcon(Resources resources, int iconId) {
    Drawable d;
    try {
        d = resources.getDrawableForDensity(iconId, mIconDpi);
    } catch (Resources.NotFoundException e) {
        d = null;
    }
    return (d != null) ? d : getFullResDefaultActivityIcon();
}