Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PackageManager.getApplicationIcon() returning default icon?

Is there a way to tell if the Drawable I'm getting back from getApplicationIcon() is a default/built-in icon or not?

For example, I have several applications installed on my emulator. "Adobe Reader" has an icon provided by Adobe. "com.android.gesture.builder" and "Sample Soft Keyboard", on the other hand, have a generic Android icon. getApplicationIcon() for those two packages returned different BitmapDrawable objects, but running getBitmap() on those two objects returned the same Bitmap object (android.graphics.Bitmap@401a7df8).

The only idea I have so far is to do something like How to preview R.drawable.* images and grab all the android.R.drawable resources, create Drawables from them, and check to see if the Bitmap I get back from getApplicationIcon() matches any of them. That's pretty sub-optimal, though.

Thanks!

like image 482
craig65535 Avatar asked Jun 30 '12 01:06

craig65535


1 Answers

I just figured this out. There's a PackageManager.getDefaultActivityIcon() method that returns a Drawable. If that Drawable's Bitmap matches the application icon Drawable's Bitmap, then it's the default icon.

PackageManager pm = context.getPackageManager();
Drawable icon = pm.getApplicationIcon(apk.package_name);
Drawable default_icon = pm.getDefaultActivityIcon();
if (icon instanceof BitmapDrawable && default_icon instanceof BitmapDrawable) {
    BitmapDrawable icon_bd = (BitmapDrawable)icon;
    Bitmap icon_b = icon_bd.getBitmap();
    BitmapDrawable default_bd = (BitmapDrawable)pm.getDefaultActivityIcon();
    Bitmap default_b = default_bd.getBitmap();
    if (icon_b == default_b) {
        // It's the default icon
    }
}
like image 132
craig65535 Avatar answered Nov 15 '22 13:11

craig65535