0

I want use my app to drop a launcher icon of a 3rd party app.

I have the INSTALL_SHORTCUT permission:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

I am able to drop the shortcut, however, the icon appears pixelated.

What is the way to get an icon in the size appropriate to the home screen? (like the one Google Play drop after installation)

*I use the following code to get the icon Bitmap for the shortcut:

public static Drawable getInstalledPackageIcon(Context context, String packageName) {

    PackageManager packageManager = context.getPackageManager();

    try {

        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, 0);

        if (applicationInfo != null) {
            Drawable iconDrawable = applicationInfo.loadIcon(packageManager);
            return iconDrawable;
        }

    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}
dors
  • 5,802
  • 8
  • 45
  • 71
  • Why are you using this approach as google play does this by its own.This will generate icon on home screen at every launches of your app.And multiple time it will occur on your home screen. – Jay Shah May 03 '15 at 15:15
  • The way I use this functionality is not relevant to the question. What I do need is to understand how to get an appropriate sized app icon – dors May 03 '15 at 15:31
  • You need to put icons of specific resolutions in each drawable folder as per android guidline. LDPI should be 36 x 36. MDPI should be 48 x 48. TVDPI should be 64 x 64. HDPI should be 72 x 72. XHDPI should be 96 x 96. XXHDPI should be 144 x 144. XXXHDPI should be 192 x 192. – Jay Shah May 03 '15 at 15:32
  • I'm talking about 3rd party app icons, not my app's icon – dors May 03 '15 at 15:36
  • 1
    Please refer this link.It might help you. http://stackoverflow.com/questions/18386040/retrieving-xhdpi-app-icons-from-packagemanager – Jay Shah May 03 '15 at 15:41
  • Thanks! that does seem to work. However, in that code snippet he uses "config.densityDpi = DisplayMetrics.DENSITY_XHIGH". I need to understand what is the appropriate density for the home screen launcher icon. any ideas? – dors May 03 '15 at 16:17
  • Yes XHDPI is standard resolution that will work for all.If you want to show for higher then that or lower then that.If you want to make 9 patch images then also you need to make for xhdpi resolution image so it will work with correct behavior. – Jay Shah May 03 '15 at 16:19
  • Where does it say that XHDPI is the proper resolution for launcher icons? – dors May 03 '15 at 16:24
  • It is conclusion.As I have mentioned above various launcher icons sizes in which XHDPI stands 96*96 which is more than lower and some sort less then Extra High resolution.So it stands between all resolutions. – Jay Shah May 03 '15 at 16:26

1 Answers1

0

I don't think the solution with the use of getDrawableForDensity is the most correct. You should let the launcher decide, passing the resource name as you can see in this example:

private void createAppShortcut(String packageName) {

    PackageManager packageManager = getPackageManager();

    ApplicationInfo appInfo = null;
    Resources resources = null;

    try {
        appInfo = packageManager.getApplicationInfo(packageName, 0);
        resources = packageManager.getResourcesForApplication(packageName);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return;
    }

    Intent shortcutIntent = packageManager.getLaunchIntentForPackage(packageName);
    CharSequence shortcutName = appInfo.loadLabel(packageManager);

    Intent.ShortcutIconResource shortcutIconResource = new Intent.ShortcutIconResource();
    shortcutIconResource.packageName = packageName;
    shortcutIconResource.resourceName = resources.getResourceName(appInfo.icon);

    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
    shortcut.putExtra("duplicate", false);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIconResource);
    sendBroadcast(shortcut);
}
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94