Java Code Examples for android.content.pm.ApplicationInfo#loadIcon()

The following examples show how to use android.content.pm.ApplicationInfo#loadIcon() . 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: DrawableHelper.java    From candybar-library with Apache License 2.0 6 votes vote down vote up
@Nullable
public static Drawable getHighQualityIcon(@NonNull Context context, String packageName) {
    try {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo info = packageManager.getApplicationInfo(
                packageName, PackageManager.GET_META_DATA);

        Resources resources = packageManager.getResourcesForApplication(packageName);
        int density = DisplayMetrics.DENSITY_XXHIGH;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            density = DisplayMetrics.DENSITY_XXXHIGH;
        }

        Drawable drawable = ResourcesCompat.getDrawableForDensity(
                resources, info.icon, density, null);
        if (drawable != null) return drawable;
        return info.loadIcon(packageManager);
    } catch (Exception | OutOfMemoryError e) {
        LogUtil.e(Log.getStackTraceString(e));
    }
    return null;
}
 
Example 2
Source File: Util.java    From stynico with MIT License 6 votes vote down vote up
public static Drawable getApkIcon(Context context, String apkPath)
 {
PackageManager pm = context.getPackageManager();
PackageInfo info = pm.getPackageArchiveInfo(apkPath,
											PackageManager.GET_ACTIVITIES);
if (info != null)
{
  ApplicationInfo appInfo = info.applicationInfo;
  appInfo.sourceDir = apkPath;
  appInfo.publicSourceDir = apkPath;
  try
  {
	return appInfo.loadIcon(pm);
  }
  catch (OutOfMemoryError e)
  {
	Log.e(LOG_TAG, e.toString());
  }
}
return null;
 }
 
Example 3
Source File: FileUtil.java    From LiveGiftLayout with Apache License 2.0 6 votes vote down vote up
public static Drawable getApkIcon(Context context, String apkPath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageArchiveInfo(apkPath,
            PackageManager.GET_ACTIVITIES);
    if (info != null) {
        ApplicationInfo appInfo = info.applicationInfo;
        appInfo.sourceDir = apkPath;
        appInfo.publicSourceDir = apkPath;
        try {
            return appInfo.loadIcon(pm);
        } catch (OutOfMemoryError e) {
            Log.e(LOG_TAG, e.toString());
        }
    }
    return null;
}
 
Example 4
Source File: AppIconCacheUtils.java    From DocUIProxy-Android with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
public static Bitmap loadIconBitmap(@NonNull Context context, @NonNull ApplicationInfo info) {
    final Bitmap cachedBitmap = getBitmapFromCache(info.packageName);
    if (cachedBitmap != null) {
        return cachedBitmap;
    }
    final PackageManager pm = context.getPackageManager();
    Bitmap loadedResult = null;

    try {
        final int maxSize = context.getResources()
                .getDimensionPixelSize(R.dimen.expected_app_icon_max_size);
        final Drawable icon;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            icon = info.loadUnbadgedIcon(pm);
        } else {
            icon = info.loadIcon(pm);
        }
        loadedResult = DrawableUtils.toBitmap(icon, maxSize, maxSize, Bitmap.Config.ARGB_8888);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (loadedResult != null) {
        putBitmapToCache(info.packageName, loadedResult);
    }

    return loadedResult;
}
 
Example 5
Source File: RxAppTool.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
/**
 * 得到AppInfo的Bean
 *
 * @param pm 包的管理
 * @param pi 包的信息
 * @return AppInfo类
 */
private static AppInfo getBean(PackageManager pm, PackageInfo pi) {
    ApplicationInfo ai = pi.applicationInfo;
    String name = ai.loadLabel(pm).toString();
    Drawable icon = ai.loadIcon(pm);
    String packageName = pi.packageName;
    String packagePath = ai.sourceDir;
    String versionName = pi.versionName;
    int versionCode = pi.versionCode;
    boolean isSD = (ApplicationInfo.FLAG_SYSTEM & ai.flags) != ApplicationInfo.FLAG_SYSTEM;
    boolean isUser = (ApplicationInfo.FLAG_SYSTEM & ai.flags) != ApplicationInfo.FLAG_SYSTEM;
    return new AppInfo(name, icon, packageName, packagePath, versionName, versionCode, isSD, isUser);
}
 
Example 6
Source File: CrashUnit.java    From Cracker with Apache License 2.0 5 votes vote down vote up
public static Drawable getAppIcon(Context context, String packageName) {
    PackageManager manager = context.getPackageManager();

    try {
        ApplicationInfo info = manager.getApplicationInfo(packageName, 0);
        return info.loadIcon(manager);
    } catch (PackageManager.NameNotFoundException n) {
        return null;
    }
}
 
Example 7
Source File: AppInstalledActivity.java    From LeanbackTvSample with MIT License 5 votes vote down vote up
public List<AppInfo> getInstallApps(Context context) {
        Log.e(TAG, "getInstallApps0: ");
        PackageManager pm = context.getPackageManager();
        List<PackageInfo> installedPackages = pm.getInstalledPackages(0);  //获取所以已安装的包

        List<AppInfo> list = new ArrayList<>();
        for (PackageInfo packageInfo : installedPackages) {
            Intent intent = pm.getLaunchIntentForPackage(packageInfo.packageName);
            if (intent == null) {
                continue;
            }
            AppInfo info = new AppInfo();
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;  //应用信息
            info.name = applicationInfo.loadLabel(pm).toString();
            info.icon = applicationInfo.loadIcon(pm);        //状态机,通过01状态来表示是否具备某些属性和功能

            info.packageName = packageInfo.packageName;
            info.versionName = packageInfo.versionName;
            info.versionCode = packageInfo.versionCode;
            int flags = applicationInfo.flags;  //获取应用标记
            info.isRom = (flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != ApplicationInfo
                    .FLAG_EXTERNAL_STORAGE;
            info.isUser = (flags & ApplicationInfo.FLAG_SYSTEM) != ApplicationInfo
                    .FLAG_SYSTEM;
//            Log.e(TAG, "getInstallApps: " + info.toString());
            list.add(info);
        }
        Log.e(TAG, "getInstallApps1: ");
        return list;
    }
 
Example 8
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 9
Source File: BitmapUtil.java    From FileManager with Apache License 2.0 5 votes vote down vote up
public static Bitmap getApkIcon(Context context, String path) {

        Bitmap mBitmap = null;
        PackageManager pm = context.getApplicationContext().getPackageManager();
        PackageInfo packageInfo = pm.getPackageArchiveInfo(path,
                PackageManager.GET_ACTIVITIES);

        if (packageInfo != null) {
            final ApplicationInfo appInfo = packageInfo.applicationInfo;

            if (appInfo != null) {
                appInfo.sourceDir = path;
                appInfo.publicSourceDir = path;
                final Drawable icon = appInfo.loadIcon(pm);

                if (icon != null) {
                    mBitmap = ((BitmapDrawable) icon).getBitmap();
                }
            }
        } else {
            // load apk icon from /res/drawable/..
            mBitmap = BitmapFactory.decodeResource(context.getApplicationContext().getResources(),
                    R.drawable.type_apk);
        }

        return mBitmap;
    }
 
Example 10
Source File: AppUtils.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
private static AppInfo getBean(final PackageManager pm, final PackageInfo pi) {
    if (pi == null) return null;
    ApplicationInfo ai = pi.applicationInfo;
    String packageName = pi.packageName;
    String name = ai.loadLabel(pm).toString();
    Drawable icon = ai.loadIcon(pm);
    String packagePath = ai.sourceDir;
    String versionName = pi.versionName;
    int versionCode = pi.versionCode;
    boolean isSystem = (ApplicationInfo.FLAG_SYSTEM & ai.flags) != 0;
    return new AppInfo(packageName, name, icon, packagePath, versionName, versionCode, isSystem);
}
 
Example 11
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
@Override public Drawable getApplicationIcon(ApplicationInfo info) {
    return info.loadIcon(this);
}
 
Example 12
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
@Override public Drawable getApplicationIcon(ApplicationInfo info) {
    return info.loadIcon(this);
}
 
Example 13
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
@Override public Drawable getApplicationIcon(ApplicationInfo info) {
    return info.loadIcon(this);
}
 
Example 14
Source File: IconPreview.java    From SimpleExplorer with GNU General Public License v3.0 4 votes vote down vote up
private static Bitmap getPreview(File file) {
    final boolean isImage = MimeTypes.isPicture(file);
    final boolean isVideo = MimeTypes.isVideo(file);
    final boolean isApk = file.getName().endsWith(".apk");
    Bitmap mBitmap = null;
    String path = file.getAbsolutePath();

    if (isImage) {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(path, o);
        o.inJustDecodeBounds = false;

        if (o.outWidth != -1 && o.outHeight != -1) {
            final int originalSize = (o.outHeight > o.outWidth) ? o.outWidth
                    : o.outHeight;
            o.inSampleSize = originalSize / mWidth;
        }

        mBitmap = BitmapFactory.decodeFile(path, o);

        addBitmapToMemoryCache(path, mBitmap);
        return mBitmap;
    } else if (isVideo) {
        mBitmap = ThumbnailUtils.createVideoThumbnail(path,
                MediaStore.Video.Thumbnails.MICRO_KIND);

        addBitmapToMemoryCache(path, mBitmap);
        return mBitmap;
    } else if (isApk) {
        final PackageInfo packageInfo = pm.getPackageArchiveInfo(path,
                PackageManager.GET_ACTIVITIES);

        if (packageInfo != null) {
            final ApplicationInfo appInfo = packageInfo.applicationInfo;

            if (appInfo != null) {
                appInfo.sourceDir = path;
                appInfo.publicSourceDir = path;
                final Drawable icon = appInfo.loadIcon(pm);

                if (icon != null) {
                    mBitmap = ((BitmapDrawable) icon).getBitmap();
                }
            }
        } else {
            // load apk icon from /res/drawable/..
            mBitmap = BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.type_apk);
        }

        addBitmapToMemoryCache(path, mBitmap);
        return mBitmap;
    }
    return null;
}
 
Example 15
Source File: DelegateApplicationPackageManager.java    From AndroidDownload with Apache License 2.0 4 votes vote down vote up
@Override public Drawable getApplicationIcon(ApplicationInfo info) {
    return info.loadIcon(this);
}
 
Example 16
Source File: IconPreview.java    From FileManager with Apache License 2.0 4 votes vote down vote up
private static Bitmap getPreview(File file) {
    final boolean isImage = MimeTypes.isPicture(file);
    final boolean isVideo = MimeTypes.isVideo(file);
    final boolean isApk = file.getName().endsWith(".apk");

    Bitmap mBitmap = null;
    String path = file.getAbsolutePath();

    if (isImage) {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        BitmapFactory.decodeFile(path, o);
        o.inJustDecodeBounds = false;

        if (o.outWidth != -1 && o.outHeight != -1) {
            final int originalSize = (o.outHeight > o.outWidth) ? o.outWidth
                    : o.outHeight;
            o.inSampleSize = originalSize / mWidth;
        }
 
        mBitmap = BitmapFactory.decodeFile(path, o);

        addBitmapToMemoryCache(path, mBitmap);
        return mBitmap;
    } else if (isVideo) {
        mBitmap = ThumbnailUtils.createVideoThumbnail(path,
                MediaStore.Video.Thumbnails.MICRO_KIND);

        addBitmapToMemoryCache(path, mBitmap);
        return mBitmap;
    } else if (isApk) {
        //Log.w("ruijie", path);
        final PackageInfo packageInfo = pm.getPackageArchiveInfo(path,
                PackageManager.GET_ACTIVITIES);

        if (packageInfo != null) {
            final ApplicationInfo appInfo = packageInfo.applicationInfo;

            if (appInfo != null) {
                appInfo.sourceDir = path;
                appInfo.publicSourceDir = path;
                final Drawable icon = appInfo.loadIcon(pm);

                if (icon != null) {
                    mBitmap = ((BitmapDrawable) icon).getBitmap();
                }
            }
        } else {
            // load apk icon from /res/drawable/..
            mBitmap = BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.type_apk);
        }

        addBitmapToMemoryCache(path, mBitmap);
        return mBitmap;
    }
    return null;
}
 
Example 17
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
@Override public Drawable getApplicationIcon(ApplicationInfo info) {
    return info.loadIcon(this);
}
 
Example 18
Source File: BruteAppMetaExtractor.java    From SAI with GNU General Public License v3.0 4 votes vote down vote up
private AppMeta extractAppMeta(ApkSourceFile apkSourceFile, ApkSourceFile.Entry baseApkEntry) throws Exception {
    File apkFile = null;
    try {
        apkFile = Utils.createTempFileInCache(mContext, "BruteAppMetaExtractor", "apk");
        if (apkFile == null)
            throw new IOException("Unable to create temp file");

        MessageDigest messageDigest = MessageDigest.getInstance(HASH_ALGORITHM);
        try (DigestInputStream in = new DigestInputStream(apkSourceFile.openEntryInputStream(baseApkEntry), messageDigest); OutputStream out = new FileOutputStream(apkFile)) {
            IOUtils.copyStream(in, out);
        }
        String apkHash = Utils.bytesToHex(messageDigest.digest());

        PackageManager pm = mContext.getPackageManager();
        PackageInfo packageInfo = Objects.requireNonNull(pm.getPackageArchiveInfo(apkFile.getAbsolutePath(), 0));

        ApplicationInfo applicationInfo = packageInfo.applicationInfo;
        applicationInfo.sourceDir = apkFile.getAbsolutePath();
        applicationInfo.publicSourceDir = apkFile.getAbsolutePath();

        String label = applicationInfo.loadLabel(pm).toString();

        Uri iconUri = null;
        synchronized (getLockForHash(apkHash)) {
            File iconFile = getIconFileForHash(apkHash);
            if (!iconFile.exists()) {
                try {
                    Drawable iconDrawable = applicationInfo.loadIcon(pm);
                    Utils.saveDrawableAsPng(iconDrawable, iconFile);
                    iconUri = Uri.fromFile(iconFile);
                } catch (Exception e) {
                    Log.w(TAG, "Unable to save icon to a file", e);
                    iconFile.delete();
                }
            }
        }

        AppMeta appMeta = new AppMeta.Builder()
                .setPackageName(packageInfo.packageName)
                .setVersionCode(packageInfo.versionCode)
                .setVersionName(packageInfo.versionName)
                .setAppName(label)
                .setIconUri(iconUri)
                .build();

        cacheAppMeta(apkHash, appMeta);

        Log.i(TAG, String.format("Extracted app meta for file %s, apk hash is %s", apkSourceFile.getName(), apkHash));

        return appMeta;

    } finally {
        if (apkFile != null)
            apkFile.delete();
    }
}
 
Example 19
Source File: NetworkUsageStatsFragment.java    From android-testdpc with Apache License 2.0 4 votes vote down vote up
private void bindView(View appView, List<NetworkStats.Bucket> item) {
    final ViewHolder viewHolder = (ViewHolder) appView.getTag();
    final TextView title = viewHolder.title;
    final TextView summary = viewHolder.summary;
    final TextView state = viewHolder.state;
    final ImageView imageView = viewHolder.image;
    final Resources res = getResources();
    Drawable icon = res.getDrawable(android.R.drawable.ic_dialog_info, null);
    NetworkStats.Bucket bucket = item.get(0);
    final int uid = bucket.getUid();
    switch (uid) {
        case NetworkStats.Bucket.UID_REMOVED: {
            title.setText(R.string.network_stats_uid_removed);
        } break;
        case NetworkStats.Bucket.UID_TETHERING: {
            title.setText(R.string.network_stats_uid_tethering);
        } break;
        case android.os.Process.SYSTEM_UID: {
            title.setText(R.string.network_stats_uid_system);
        } break;
        default: {
            title.setText(getString(R.string.network_stats_uid, uid));
            icon = mPackageManager.getDefaultActivityIcon();
            final String[] packageNames = mPackageManager.getPackagesForUid(uid);
            final int length = packageNames != null ? packageNames.length : 0;
            try {
                if (length == 1) {
                    final String pkgName = packageNames[0];
                    final ApplicationInfo info = mPackageManager.getApplicationInfo(pkgName,
                            0 /* no flags */);
                    if (info != null) {
                        title.setText(info.loadLabel(mPackageManager));
                        icon = info.loadIcon(mPackageManager);
                    }
                } else {
                    for (int i = 0; i < length; i++) {
                        final String packageName = packageNames[i];
                        final PackageInfo packageInfo = mPackageManager.getPackageInfo(
                                packageName, 0 /* no flags */);
                        final ApplicationInfo appInfo = mPackageManager.getApplicationInfo(
                                packageName, 0 /* no flags */);

                        if (appInfo != null && packageInfo != null) {
                            if (packageInfo.sharedUserLabel != 0) {
                                title.setText(mPackageManager.getText(packageName,
                                        packageInfo.sharedUserLabel,
                                        packageInfo.applicationInfo));
                                icon = appInfo.loadIcon(mPackageManager);
                            }
                        }
                    }
                }
            } catch (PackageManager.NameNotFoundException e) {
                // keep the default activity icon
            }
        }
    }
    imageView.setImageDrawable(icon);
    final int bucketsCount = item.size();
    if (bucketsCount == 1) {
        summary.setText(formatSize(bucket.getRxBytes() + bucket.getTxBytes()));
        switch (bucket.getState()) {
            case NetworkStats.Bucket.STATE_FOREGROUND: {
                state.setText(R.string.network_stats_foreground_state);
            } break;
            case NetworkStats.Bucket.STATE_DEFAULT: {
                state.setText(R.string.network_stats_default_state);
            } break;
            case NetworkStats.Bucket.STATE_ALL: {
                state.setText(R.string.network_stats_combined_state);
            } break;
        }
    } else {
        summary.setText(getString(R.string.network_stats_items, bucketsCount));
        state.setText(R.string.network_stats_combined_state);
    }
}
 
Example 20
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
@Override public Drawable getApplicationIcon(ApplicationInfo info) {
    return info.loadIcon(this);
}