Java Code Examples for android.content.pm.PackageManager#getApplicationIcon()

The following examples show how to use android.content.pm.PackageManager#getApplicationIcon() . 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: DConnectUtil.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * プラグインのアイコン画像を読み込みます.
 *
 * @param context     コンテキスト
 * @param packageName プラグインのパッケージ名
 * @param iconId      アイコンのリソースID
 * @return プラグインのアイコン画像
 */
public static Drawable loadPluginIcon(final Context context, final String packageName, final Integer iconId) {
    PackageManager pkgMgr = context.getPackageManager();
    Drawable icon;
    if (iconId != null) {
        icon = ResourcesCompat.getDrawable(context.getResources(), iconId, null);
    } else {
        try {
            ApplicationInfo info = pkgMgr.getApplicationInfo(packageName, 0);
            icon = pkgMgr.getApplicationIcon(info.packageName);
        } catch (PackageManager.NameNotFoundException e) {
            icon = null;
            if (BuildConfig.DEBUG) {
                Log.d("Manager", "Icon is not found.");
            }
        }
    }
    return icon;
}
 
Example 2
Source File: DeviceAppsPlugin.java    From flutter_plugin_device_apps with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> getAppData(PackageManager packageManager, PackageInfo pInfo, boolean includeAppIcon) {
    Map<String, Object> map = new HashMap<>();
    map.put("app_name", pInfo.applicationInfo.loadLabel(packageManager).toString());
    map.put("apk_file_path", pInfo.applicationInfo.sourceDir);
    map.put("package_name", pInfo.packageName);
    map.put("version_code", pInfo.versionCode);
    map.put("version_name", pInfo.versionName);
    map.put("data_dir", pInfo.applicationInfo.dataDir);
    map.put("system_app", isSystemApp(pInfo));
    map.put("install_time", pInfo.firstInstallTime);
    map.put("update_time", pInfo.lastUpdateTime);
    map.put("category", pInfo.applicationInfo.category);

    if (includeAppIcon) {
        try {
            Drawable icon = packageManager.getApplicationIcon(pInfo.packageName);
            String encodedImage = encodeToBase64(getBitmapFromDrawable(icon), Bitmap.CompressFormat.PNG, 100);
            map.put("app_icon", encodedImage);
        } catch (PackageManager.NameNotFoundException ignored) {
        }
    }

    return map;
}
 
Example 3
Source File: BrowserSelectionAdapter.java    From AppAuth-Android with Apache License 2.0 6 votes vote down vote up
@Override
public List<BrowserInfo> loadInBackground() {
    List<BrowserDescriptor> descriptors = BrowserSelector.getAllBrowsers(getContext());
    ArrayList<BrowserInfo> infos = new ArrayList<>(descriptors.size());

    PackageManager pm = getContext().getPackageManager();
    for (BrowserDescriptor descriptor : descriptors) {
        try {
            ApplicationInfo info = pm.getApplicationInfo(descriptor.packageName, 0);
            CharSequence label = pm.getApplicationLabel(info);
            Drawable icon = pm.getApplicationIcon(descriptor.packageName);
            infos.add(new BrowserInfo(descriptor, label, icon));
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            infos.add(new BrowserInfo(descriptor, descriptor.packageName, null));
        }
    }

    return infos;
}
 
Example 4
Source File: IconUtils.java    From FireFiles with Apache License 2.0 6 votes vote down vote up
public static Drawable loadPackagePathIcon(Context context, String path, String mimeType){
 	int icon =  sMimeIcons.get(mimeType);
     if (path != null) {
         final PackageManager pm = context.getPackageManager();
try {
	final PackageInfo packageInfo = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
	if (packageInfo != null) {
		packageInfo.applicationInfo.sourceDir = packageInfo.applicationInfo.publicSourceDir = path;
		// know issue with nine patch image instead of drawable
		return pm.getApplicationIcon(packageInfo.applicationInfo);
	}
} catch (Exception e) {
	return ContextCompat.getDrawable(context, icon);
}
     } else {
         return ContextCompat.getDrawable(context, icon);
     }
     return null;
 }
 
Example 5
Source File: IconUtils.java    From FireFiles with Apache License 2.0 6 votes vote down vote up
public static Drawable loadPackagePathIcon(Context context, String path, String mimeType){
 	int icon =  sMimeIcons.get(mimeType);
     if (path != null) {
         final PackageManager pm = context.getPackageManager();
try {
	final PackageInfo packageInfo = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
	if (packageInfo != null) {
		packageInfo.applicationInfo.sourceDir = packageInfo.applicationInfo.publicSourceDir = path;
		// know issue with nine patch image instead of drawable
		return pm.getApplicationIcon(packageInfo.applicationInfo);
	}
} catch (Exception e) {
	return ContextCompat.getDrawable(context, icon);
}
     } else {
         return ContextCompat.getDrawable(context, icon);
     }
     return null;
 }
 
Example 6
Source File: UberVolumePanel.java    From Noyze with Apache License 2.0 5 votes vote down vote up
protected Drawable getAppIcon(String packageName) {
    PackageManager mPM = getContext().getPackageManager();
    try {
        return mPM.getApplicationIcon(packageName);
    } catch (PackageManager.NameNotFoundException nfe) {
        LOGE(TAG, "Couldn't get app icon for `" + packageName + "`", nfe);
        return null;
    }
}
 
Example 7
Source File: BlackberryVolumePanel.java    From Noyze with Apache License 2.0 5 votes vote down vote up
protected Drawable getAppIcon(String packageName) {
    PackageManager mPM = getContext().getPackageManager();
    try {
        return mPM.getApplicationIcon(packageName);
    } catch (PackageManager.NameNotFoundException nfe) {
        LOGE(TAG, "Couldn't get app icon for `" + packageName + "`", nfe);
        return null;
    }
}
 
Example 8
Source File: ApkItem.java    From DroidPlugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
ApkItem(PackageManager pm, PackageInfo info, String path) {
    try {
        icon = pm.getApplicationIcon(info.applicationInfo);
    } catch (Exception e) {
        icon = pm.getDefaultActivityIcon();
    }
    title = pm.getApplicationLabel(info.applicationInfo);
    versionName = info.versionName;
    versionCode = info.versionCode;
    apkfile = path;
    packageInfo = info;
}
 
Example 9
Source File: UpgradeActivity.java    From android_packages_apps_GmsCore with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.games_info);

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    getWindow().setAttributes(lp);

    String packageName = getIntent().getStringExtra(EXTRA_GAME_PACACKE_NAME);

    // receive package info
    PackageManager packageManager = getPackageManager();
    ApplicationInfo applicationInfo;
    try {
        applicationInfo = packageManager.getApplicationInfo(packageName, 0);
    } catch (PackageManager.NameNotFoundException e) {
        Log.w(TAG, e);
        finish();
        return;
    }
    CharSequence appLabel = packageManager.getApplicationLabel(applicationInfo);
    Drawable appIcon = packageManager.getApplicationIcon(applicationInfo);

    ((ImageView) findViewById(R.id.app_icon)).setImageDrawable(appIcon);
    ((TextView) findViewById(R.id.title)).setText(getString(R.string.games_info_title, appLabel));
    findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}
 
Example 10
Source File: AccessTokenListFragment.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * パッケージアイコンを返す.
 * @param context コンテキスト
 * @param packageName パッケージ名
 * @return not null: パッケージアイコン / null: パッケージが見つからない
 */
private Drawable getPackageIcon(final Context context, final String packageName) {
    PackageManager pm = context.getPackageManager();
    try {
        return pm.getApplicationIcon(packageName);
    } catch (NameNotFoundException e) {
        return null;
    }
}
 
Example 11
Source File: ShareCompat.java    From adt-leanback-support with Apache License 2.0 5 votes vote down vote up
/**
 * Get the icon of the calling application as a Drawable if data
 * about the calling package is available.
 *
 * <p><em>Note:</em> This data may have been provided voluntarily by the calling
 * application. As such it should not be trusted for accuracy in the context of
 * security or verification.</p>
 *
 * @return The calling application's icon or null if unknown
 */
public Drawable getCallingApplicationIcon() {
    if (mCallingPackage == null) return null;

    PackageManager pm = mActivity.getPackageManager();
    try {
        return pm.getApplicationIcon(mCallingPackage);
    } catch (NameNotFoundException e) {
        Log.e(TAG, "Could not retrieve icon for calling application", e);
    }
    return null;
}
 
Example 12
Source File: AppInfoUtils.java    From Bailan with Apache License 2.0 5 votes vote down vote up
public static Drawable getAppIcon(Context context, String apkFilepath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo pkgInfo = getPackageInfo(context, apkFilepath);
    if (pkgInfo == null) {
        return null;
    }

    ApplicationInfo appInfo = pkgInfo.applicationInfo;
    if (Build.VERSION.SDK_INT >= 8) {
        appInfo.sourceDir = apkFilepath;
        appInfo.publicSourceDir = apkFilepath;
    }
    return pm.getApplicationIcon(appInfo);
}
 
Example 13
Source File: LabelManagerPackageActivity.java    From talkback with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.label_manager_labels);

  final Intent intent = getIntent();
  if (!intent.hasExtra(EXTRA_PACKAGE_NAME)) {
    throw new IllegalArgumentException("Intent missing package name extra.");
  }

  packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME);

  final PackageManager packageManager = getPackageManager();
  CharSequence applicationLabel;
  Drawable packageIcon;
  try {
    packageIcon = packageManager.getApplicationIcon(packageName);
    final PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
    applicationLabel = packageManager.getApplicationLabel(packageInfo.applicationInfo);
  } catch (NameNotFoundException e) {
    LogUtils.i(TAG, "Could not load package info for package %s.", packageName);

    packageIcon = packageManager.getDefaultActivityIcon();
    applicationLabel = packageName;
  }

  setTitle(getString(R.string.label_manager_package_title, applicationLabel));

  final ActionBar actionBar = getSupportActionBar();
  actionBar.setIcon(packageIcon);
  actionBar.setDisplayHomeAsUpEnabled(true);

  labelList = (ListView) findViewById(R.id.label_list);
  labelProviderClient = new LabelProviderClient(this, LabelProvider.AUTHORITY);
}
 
Example 14
Source File: ShareCompat.java    From android-recipes-app with Apache License 2.0 5 votes vote down vote up
/**
 * Get the icon of the calling application as a Drawable if data
 * about the calling package is available.
 *
 * <p><em>Note:</em> This data may have been provided voluntarily by the calling
 * application. As such it should not be trusted for accuracy in the context of
 * security or verification.</p>
 *
 * @return The calling application's icon or null if unknown
 */
public Drawable getCallingApplicationIcon() {
    if (mCallingPackage == null) return null;

    PackageManager pm = mActivity.getPackageManager();
    try {
        return pm.getApplicationIcon(mCallingPackage);
    } catch (NameNotFoundException e) {
        Log.e(TAG, "Could not retrieve icon for calling application", e);
    }
    return null;
}
 
Example 15
Source File: ResourceUtil.java    From Android-Plugin-Framework with MIT License 5 votes vote down vote up
public static Drawable getIcon(PluginDescriptor pluginDescriptor) {
    if (Build.VERSION.SDK_INT >= 9) {
        PackageManager pm = FairyGlobal.getHostApplication().getPackageManager();
        PackageInfo info = pluginDescriptor.getPackageInfo(PackageManager.GET_ACTIVITIES);
        if (info != null) {
            Drawable logo = pm.getApplicationIcon(info.applicationInfo);
            return logo;
        }
    }
    return null;
}
 
Example 16
Source File: Util.java    From android-notification-log with MIT License 5 votes vote down vote up
public static Drawable getAppIconFromPackage(Context context, String packageName) {
	PackageManager pm = context.getApplicationContext().getPackageManager();
	Drawable drawable = null;
	try {
		ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
		if(ai != null) {
			drawable = pm.getApplicationIcon(ai);
		}
	} catch (Exception e) {
		if(Const.DEBUG) e.printStackTrace();
	}
	return drawable;
}
 
Example 17
Source File: ComputeAppListTask.java    From exodus-android-app with GNU General Public License v3.0 5 votes vote down vote up
private ApplicationViewModel buildViewModelFromPackageInfo(PackageInfo pi,
                                                           DatabaseManager databaseManager,
                                                           PackageManager packageManager) {
    ApplicationViewModel vm = new ApplicationViewModel();

    vm.versionName = pi.versionName;
    vm.packageName = pi.packageName;
    vm.versionCode = pi.versionCode;
    vm.requestedPermissions = pi.requestedPermissions;

    if (vm.versionName != null)
        vm.report = databaseManager.getReportFor(vm.packageName, vm.versionName);
    else {
        vm.report = databaseManager.getReportFor(vm.packageName, vm.versionCode);
    }

    if (vm.report != null) {
        vm.trackers = databaseManager.getTrackers(vm.report.trackers);
    }

    try {
        vm.icon = packageManager.getApplicationIcon(vm.packageName);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    vm.label = packageManager.getApplicationLabel(pi.applicationInfo);
    vm.installerPackageName = packageManager.getInstallerPackageName(vm.packageName);
    vm.isVisible = true;

    return vm;
}
 
Example 18
Source File: AppInfoBean.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化 AppInfoBean
 * @param packageInfo    {@link PackageInfo}
 * @param packageManager {@link PackageManager}
 */
protected AppInfoBean(final PackageInfo packageInfo, final PackageManager packageManager) {
    // APP 包名
    appPackName = packageInfo.applicationInfo.packageName;
    // APP 应用名
    appName = packageManager.getApplicationLabel(packageInfo.applicationInfo).toString();
    // APP 图标
    appIcon = packageManager.getApplicationIcon(packageInfo.applicationInfo);
    // APP 类型
    appType = AppInfoBean.getAppType(packageInfo);
    // APP 版本号
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        versionCode = packageInfo.getLongVersionCode();
    } else {
        versionCode = packageInfo.versionCode;
    }
    // APP 版本名
    versionName = packageInfo.versionName;
    // APP 首次安装时间
    firstInstallTime = packageInfo.firstInstallTime;
    // APP 最后一次更新时间
    lastUpdateTime = packageInfo.lastUpdateTime;
    // APP 地址
    sourceDir = packageInfo.applicationInfo.sourceDir;
    // APK 大小
    apkSize = FileUtils.getFileLength(sourceDir);
}
 
Example 19
Source File: ApplicationItem.java    From trafficstats-example with MIT License 4 votes vote down vote up
public Drawable getIcon(PackageManager _packageManager) {
    return _packageManager.getApplicationIcon(app);
}
 
Example 20
Source File: UnlockTorAppsFragment.java    From InviZible with GNU General Public License v3.0 4 votes vote down vote up
private void getDeviceApps(final Context context, final RecyclerView.Adapter adapter, final ArrayList<String> unlockAppsArrListSaved) {

        pbTorApp.setIndeterminate(true);
        pbTorApp.setVisibility(View.VISIBLE);

        final PackageManager pMgr = context.getPackageManager();

        List<ApplicationInfo> lAppInfo = pMgr.getInstalledApplications(0);

        final Iterator<ApplicationInfo> itAppInfo = lAppInfo.iterator();

        futureTask = new FutureTask<>(() -> {
            while (itAppInfo.hasNext()) {
                ApplicationInfo aInfo = itAppInfo.next();
                boolean appUseInternet = false;
                boolean system = false;
                boolean active = false;

                try {
                    PackageInfo pInfo = pMgr.getPackageInfo(aInfo.packageName, PackageManager.GET_PERMISSIONS);

                    if (pInfo != null && pInfo.requestedPermissions != null) {
                        for (String permInfo : pInfo.requestedPermissions) {
                            if (permInfo.equals("android.permission.INTERNET")) {
                                appUseInternet = true;
                                break;
                            }
                        }

                    }

                } catch (Exception e) {
                    Log.e(LOG_TAG, "UnlockTorAppsFragment getDeviceApps exception " + e.getMessage() + " " + e.getCause());
                }

                if ((aInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
                    //System app
                    appUseInternet = true;
                    system = true;
                }


                if (appUseInternet) {
                    String name;
                    try {
                        name = pMgr.getApplicationLabel(aInfo).toString();
                    } catch (Exception e) {
                        name = aInfo.packageName;
                    }
                    String pack = aInfo.packageName;
                    String uid = String.valueOf(aInfo.uid);
                    Drawable icon = pMgr.getApplicationIcon(aInfo);


                    if (unlockAppsArrListSaved.contains(uid)) {
                        active = true;
                    }

                    final AppUnlock app = new AppUnlock(name, pack, uid, icon, system, active);

                    if (getActivity() == null)
                        return null;

                    getActivity().runOnUiThread(() -> {
                        appsUnlock.add(app);
                        Collections.sort(appsUnlock, (appUnlock, t1) -> appUnlock.name.toLowerCase().compareTo(t1.name.toLowerCase()));
                        adapter.notifyDataSetChanged();
                    });

                }
            }

            if (getActivity() == null) {
                return null;
            }

            getActivity().runOnUiThread(() -> {
                pbTorApp.setIndeterminate(false);
                pbTorApp.setVisibility(View.GONE);
            });

            System.gc();

            return null;
        });

        CachedExecutor.INSTANCE.getExecutorService().submit(futureTask);

    }