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

The following examples show how to use android.content.pm.ApplicationInfo#loadLabel() . 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: LoadPackagesAsyncTask.java    From android-apps with MIT License 6 votes vote down vote up
@Override
protected List<String[]> doInBackground(List<String[]>... objects) {
  List<String[]> labelsPackages = objects[0];
  PackageManager packageManager = activity.getPackageManager();
  List<ApplicationInfo> appInfos = packageManager.getInstalledApplications(0);
  for (ApplicationInfo appInfo : appInfos) {
    CharSequence label = appInfo.loadLabel(packageManager);
    if (label != null) {
      String packageName = appInfo.packageName;
      if (!isHidden(packageName)) {
        labelsPackages.add(new String[]{label.toString(), packageName});
      }
    }
  }
  Collections.sort(labelsPackages, new ByFirstStringComparator());
  return labelsPackages;
}
 
Example 2
Source File: IconCache.java    From LB-Launcher with Apache License 2.0 6 votes vote down vote up
/**
 * Gets an entry for the package, which can be used as a fallback entry for various components.
 * This method is not thread safe, it must be called from a synchronized method.
 */
private CacheEntry getEntryForPackage(String packageName, UserHandleCompat user) {
    ComponentName cn = new ComponentName(packageName, EMPTY_CLASS_NAME);;
    CacheKey cacheKey = new CacheKey(cn, user);
    CacheEntry entry = mCache.get(cacheKey);
    if (entry == null) {
        entry = new CacheEntry();
        entry.title = "";
        mCache.put(cacheKey, entry);

        try {
            ApplicationInfo info = mPackageManager.getApplicationInfo(packageName, 0);
            entry.title = info.loadLabel(mPackageManager);
            entry.icon = Utilities.createIconBitmap(info.loadIcon(mPackageManager), mContext);
        } catch (NameNotFoundException e) {
            if (DEBUG) Log.d(TAG, "Application not installed " + packageName);
        }

        if (entry.icon == null) {
            entry.icon = getPreloadedIcon(cn, user);
        }
    }
    return entry;
}
 
Example 3
Source File: ZenModeConfig.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the name of the app associated with owner
 */
public static String getOwnerCaption(Context context, String owner) {
    final PackageManager pm = context.getPackageManager();
    try {
        final ApplicationInfo info = pm.getApplicationInfo(owner, 0);
        if (info != null) {
            final CharSequence seq = info.loadLabel(pm);
            if (seq != null) {
                final String str = seq.toString().trim();
                if (str.length() > 0) {
                    return str;
                }
            }
        }
    } catch (Throwable e) {
        Slog.w(TAG, "Error loading owner caption", e);
    }
    return "";
}
 
Example 4
Source File: AppSecurityPermissions.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
private void setPermissions(List<MyPermissionInfo> permList) {
    if (permList != null) {
        // First pass to group permissions
        for (MyPermissionInfo pInfo : permList) {
            if (!isDisplayablePermission(pInfo, pInfo.existingReqFlags)) {
                continue;
            }
            MyPermissionGroupInfo group = permGroups.get(pInfo.group);
            if (group != null) {
                pInfo.label = pInfo.loadLabel(pm);
                addPermToList(group.allPermissions, pInfo);
                if (pInfo.newPerm) {
                    addPermToList(group.newPermissions, pInfo);
                }
            }
        }
    }

    for (MyPermissionGroupInfo pgrp : permGroups.values()) {
        if (pgrp.labelRes != 0 || pgrp.nonLocalizedLabel != null) {
            pgrp.label = pgrp.loadLabel(pm);
        } else {
            try {
                ApplicationInfo app = pm.getApplicationInfo(pgrp.packageName, 0);
                pgrp.label = app.loadLabel(pm);
            } catch (NameNotFoundException e) {
                pgrp.label = pgrp.loadLabel(pm);
            }
        }
        permGroupsList.add(pgrp);
    }
    Collections.sort(permGroupsList, permGroupComparator);
}
 
Example 5
Source File: AppsProvider.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
@Override
public String copyDocument(String sourceDocumentId, String targetParentDocumentId) throws FileNotFoundException {
	final String packageName = getPackageForDocId(sourceDocumentId);
	String fromFilePath = "";
	String fileName = "";
	try {
		PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
		ApplicationInfo appInfo = packageInfo.applicationInfo;
		fromFilePath = appInfo.sourceDir;
		fileName = (String) (appInfo.loadLabel(packageManager) != null ? appInfo.loadLabel(packageManager) : appInfo.packageName);
		fileName += getAppVersion(packageInfo.versionName);
	} catch (Exception e) {
	}

	final File fileFrom = new File(fromFilePath);
	final File fileTo = Utils.getAppsBackupFile(getContext());
	if(!fileTo.exists()){
		fileTo.mkdir();
	}
	if (!FileUtils.moveDocument(fileFrom, fileTo, fileName)) {
		throw new IllegalStateException("Failed to copy " + fileFrom);
	}
	else{
		FileUtils.updateMediaStore(getContext(), FileUtils.makeFilePath(fileTo.getPath(),
				fileName +"."+ FileUtils.getExtFromFilename(fileFrom.getPath())));
	}
	return fromFilePath;
}
 
Example 6
Source File: AppsProvider.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
@Override
public String copyDocument(String sourceDocumentId, String targetParentDocumentId) throws FileNotFoundException {
	final String packageName = getPackageForDocId(sourceDocumentId);
	String fromFilePath = "";
	String fileName = "";
	try {
		PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
		ApplicationInfo appInfo = packageInfo.applicationInfo;
		fromFilePath = appInfo.sourceDir;
		fileName = (String) (appInfo.loadLabel(packageManager) != null ? appInfo.loadLabel(packageManager) : appInfo.packageName);
		fileName += getAppVersion(packageInfo.versionName);
	} catch (Exception e) {
	}

	final File fileFrom = new File(fromFilePath);
	final File fileTo = Utils.getAppsBackupFile(getContext());
	if(!fileTo.exists()){
		fileTo.mkdir();
	}
	if (!FileUtils.moveDocument(fileFrom, fileTo, fileName)) {
		throw new IllegalStateException("Failed to copy " + fileFrom);
	}
	else{
		FileUtils.updateMediaStore(getContext(), FileUtils.makeFilePath(fileTo.getPath(),
				fileName +"."+ FileUtils.getExtFromFilename(fileFrom.getPath())));
	}
	return fromFilePath;
}
 
Example 7
Source File: AppInfoUtils.java    From sa-sdk-android with Apache License 2.0 5 votes vote down vote up
/**
 * 获取应用名称
 *
 * @param context Context
 * @return 应用名称
 */
public static CharSequence getAppName(Context context) {
    if (context == null) return "";
    try {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo appInfo = packageManager.getApplicationInfo(context.getPackageName(),
                PackageManager.GET_META_DATA);
        return appInfo.loadLabel(packageManager);
    } catch (Exception e) {
        SALog.printStackTrace(e);
    }
    return "";
}
 
Example 8
Source File: AppSecurityPermissions.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onClick(View v) {
    if (group != null && perm != null) {
        if (dialog != null) {
            dialog.dismiss();
        }
        PackageManager pm = getContext().getPackageManager();
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle(group.label);
        if (perm.descriptionRes != 0) {
            builder.setMessage(perm.loadDescription(pm));
        } else {
            CharSequence appName;
            try {
                ApplicationInfo app = pm.getApplicationInfo(perm.packageName, 0);
                appName = app.loadLabel(pm);
            } catch (NameNotFoundException e) {
                appName = perm.packageName;
            }
            builder.setMessage(getContext().getString(
                    R.string.perms_description_app, appName) + "\n\n" + perm.name);
        }
        builder.setCancelable(true);
        builder.setIcon(group.loadGroupIcon(getContext(), pm));
        dialog = builder.show();
        dialog.setCanceledOnTouchOutside(true);
    }
}
 
Example 9
Source File: ProcessFragment.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected List<ProcessInfo> doInBackground(Void... params) {
	final List<ProcessInfo> tempInfo = new LinkedList<>();

	display_process.clear();
	display_process.addAll(((ActivityManager)getContext().getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses());

	ApplicationInfo appinfo = null;
	String label;
	for (RunningAppProcessInfo r : display_process) {
		try {
			appinfo = pk.getApplicationInfo(r.processName, 0);
			label = appinfo.loadLabel(pk) + "";
		} catch (NameNotFoundException e1) {
			//e1.printStackTrace();
			label = r.processName.substring(r.processName.lastIndexOf(".") + 1);
		}
		if (appinfo != null) {
			tempInfo.add(new ProcessInfo(r, label, r.processName, r.importance, r.pid, new File(appinfo.publicSourceDir).length(), appinfo));
		} else {
			tempInfo.add(new ProcessInfo(r, label, r.processName, r.importance, r.pid, 0, null));
		}
	}
	Collections.sort(tempInfo, processSorter);

	return tempInfo;
}
 
Example 10
Source File: AppSecurityPermissions.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void onClick(View v) {
    if (mGroup != null && mPerm != null) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        PackageManager pm = getContext().getPackageManager();
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle(mGroup.mLabel);
        if (mPerm.descriptionRes != 0) {
            builder.setMessage(mPerm.loadDescription(pm));
        } else {
            CharSequence appName;
            try {
                ApplicationInfo app = pm.getApplicationInfo(mPerm.packageName, 0);
                appName = app.loadLabel(pm);
            } catch (NameNotFoundException e) {
                appName = mPerm.packageName;
            }
            StringBuilder sbuilder = new StringBuilder(128);
            sbuilder.append(getContext().getString(
                    R.string.perms_description_app, appName));
            sbuilder.append("\n\n");
            sbuilder.append(mPerm.name);
            builder.setMessage(sbuilder.toString());
        }
        builder.setCancelable(true);
        builder.setIcon(mGroup.loadGroupIcon(getContext(), pm));
        addRevokeUIIfNecessary(builder);
        mDialog = builder.show();
        mDialog.setCanceledOnTouchOutside(true);
    }
}
 
Example 11
Source File: LockedActivity.java    From MobileGuard with MIT License 5 votes vote down vote up
/**
 * 2
 */
@Override
protected void initData() {
    // get extra
    Bundle extras = getIntent().getExtras();
    if(null == extras) {
        return;
    }
    // get package name
    packageName = extras.getString(Constant.EXTRA_LOCKED_APP_PACKAGE_NAME);

    // get package info
    PackageManager pm = getPackageManager();
    try {
        ApplicationInfo info = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
        // get and set icon
        Drawable icon = info.loadIcon(pm);
        ivIcon.setImageDrawable(icon);
        // get app name
        CharSequence name = info.loadLabel(pm);
        tvName.setText(name);

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

    trueMd5Password = ConfigUtils.getString(this, Constant.KEY_APP_LOCK_PASSWORD, "");
}
 
Example 12
Source File: ApplicationPackageManager.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public CharSequence getApplicationLabel(ApplicationInfo info) {
    return info.loadLabel(this);
}
 
Example 13
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
@Override
public CharSequence getApplicationLabel(ApplicationInfo info) {
    return info.loadLabel(this);
}
 
Example 14
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
@Override
public CharSequence getApplicationLabel(ApplicationInfo info) {
    return info.loadLabel(this);
}
 
Example 15
Source File: VirtualCore.java    From container with GNU General Public License v3.0 4 votes vote down vote up
public boolean removeShortcut(int userId, String packageName, Intent splash, OnEmitShortcutListener listener) {
    AppSetting setting = findApp(packageName);
    if (setting == null) {
        return false;
    }
    ApplicationInfo appInfo = setting.getApplicationInfo(userId);
    PackageManager pm = context.getPackageManager();
    String name;
    try {
        CharSequence sequence = appInfo.loadLabel(pm);
        name = sequence.toString();
    } catch (Throwable e) {
        return false;
    }
    if (listener != null) {
        String newName = listener.getName(name);
        if (newName != null) {
            name = newName;
        }
    }
    Intent targetIntent = getLaunchIntent(packageName, userId);
    if (targetIntent == null) {
        return false;
    }
    Intent shortcutIntent = new Intent();
    shortcutIntent.setClassName(getHostPkg(), Constants.SHORTCUT_PROXY_ACTIVITY_NAME);
    shortcutIntent.addCategory(Intent.CATEGORY_DEFAULT);
    if (splash != null) {
        shortcutIntent.putExtra("_VA_|_splash_", splash.toUri(0));
    }
    shortcutIntent.putExtra("_VA_|_intent_", targetIntent);
    shortcutIntent.putExtra("_VA_|_uri_", targetIntent.toUri(0));
    shortcutIntent.putExtra("_VA_|_user_id_", VUserHandle.myUserId());

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
    context.sendBroadcast(addIntent);
    return true;
}
 
Example 16
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
@Override
public CharSequence getApplicationLabel(ApplicationInfo info) {
    return info.loadLabel(this);
}
 
Example 17
Source File: App.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
private void setFromPackageInfo(PackageManager pm, PackageInfo packageInfo) {

        this.packageName = packageInfo.packageName;
        final String installerPackageName = pm.getInstallerPackageName(packageName);
        CharSequence installerPackageLabel = null;
        if (!TextUtils.isEmpty(installerPackageName)) {
            try {
                ApplicationInfo installerAppInfo = pm.getApplicationInfo(installerPackageName,
                        PackageManager.GET_META_DATA);
                installerPackageLabel = installerAppInfo.loadLabel(pm);
            } catch (PackageManager.NameNotFoundException e) {
                Log.w(TAG, "Could not get app info: " + installerPackageName, e);
            }
        }
        if (TextUtils.isEmpty(installerPackageLabel)) {
            installerPackageLabel = installerPackageName;
        }

        ApplicationInfo appInfo = packageInfo.applicationInfo;
        final CharSequence appDescription = appInfo.loadDescription(pm);
        if (TextUtils.isEmpty(appDescription)) {
            this.summary = "(installed by " + installerPackageLabel + ")";
        } else if (appDescription.length() > 40) {
            this.summary = (String) appDescription.subSequence(0, 40);
        } else {
            this.summary = (String) appDescription;
        }
        this.added = new Date(packageInfo.firstInstallTime);
        this.lastUpdated = new Date(packageInfo.lastUpdateTime);
        this.description = "<p>";
        if (!TextUtils.isEmpty(appDescription)) {
            this.description += appDescription + "\n";
        }
        this.description += "(installed by " + installerPackageLabel
                + ", first installed on " + this.added
                + ", last updated on " + this.lastUpdated + ")</p>";

        this.name = (String) appInfo.loadLabel(pm);
        this.iconFromApk = getIconName(packageName, packageInfo.versionCode);
        this.installedVersionName = packageInfo.versionName;
        this.installedVersionCode = packageInfo.versionCode;
        this.compatible = true;
    }
 
Example 18
Source File: DataTrackerFrag.java    From PowerFileExplorer with GNU General Public License v3.0 4 votes vote down vote up
private void updateAppList() {

		final List<ApplicationInfo> applications = pk.getInstalledApplications(PackageManager.GET_META_DATA);
		AppStats appStats;

		int i = 0;

		long rx = 0;
		long tx = 0;
//		rxRate = 0;
//		txRate = 0;
		totalRxSincePrev = 0;
		totalTxSincePrev = 0;
		long elapsedRealtimeNanos = 0;
		final LinkedList<AppStats> tempAppStatsList = new LinkedList<>();
		for (ApplicationInfo app : applications) {
            try {
                //appName = app.loadLabel(pk);
                appStats = new AppStats(app.loadLabel(pk), app.packageName, app.uid);

//				if (networkType == 1) {
//					rx = TrafficStats.getUidRxBytes(app.uid);
//					tx = TrafficStats.getUidTxBytes(app.uid);
//				} else if (networkType == 2) {
//					rx = TrafficStats.getUidRxBytes(app.uid);
//					tx = TrafficStats.getUidTxBytes(app.uid);
//				} else if (networkType == 0) {
				rx = TrafficStats.getUidRxBytes(app.uid);
				tx = TrafficStats.getUidTxBytes(app.uid);
				elapsedRealtimeNanos = SystemClock.elapsedRealtimeNanos();
				//}
				//Log.d(TAG, SystemClock.elapsedRealtimeNanos() + ", " + rx + ", " + tx);
				if ((i = tempOriDataSourceL1.indexOf(appStats)) >= 0) {
					appStats = (AppStats) tempOriDataSourceL1.get(i);

					appStats.elapsedTimeSincePrev = elapsedRealtimeNanos - appStats.elapsedTimeSinceStart;
					appStats.elapsedTimeSinceStart = elapsedRealtimeNanos;

					appStats.bytesRxSincePrev = rx - appStats.bytesRxSinceStart;
					appStats.bytesTxSincePrev = tx - appStats.bytesTxSinceStart;

					appStats.idle = appStats.bytesRxSincePrev == 0 && appStats.bytesTxSincePrev == 0;
					if (!appStats.idle) {
						totalRxSincePrev += appStats.bytesRxSincePrev;
						totalTxSincePrev += appStats.bytesTxSincePrev;
//						rxRate += appStats.bytesRxSincePrev * 1000000 / (appStats.elapsedTimeSincePrev / 1000);
//						txRate += appStats.bytesTxSincePrev * 1000000 / (appStats.elapsedTimeSincePrev / 1000);
						if (statusType != 2) {
							tempAppStatsList.add(appStats);
						}
					} else if (statusType != 1 && (appStats.bytesRxSinceStart > 0 || appStats.bytesTxSinceStart > 0)) {
						tempAppStatsList.add(appStats);
					}
					appStats.bytesRxSinceStart = rx;
					appStats.bytesTxSinceStart = tx;

				} else {
					if (rx > 0 || tx > 0) {
						appStats.idle = false;
						appStats.bytesRxSinceStart = rx;
						appStats.bytesTxSinceStart = tx;

						appStats.bytesRxSincePrev = rx;
						appStats.bytesTxSincePrev = tx;

						totalRxSincePrev += rx;
						totalTxSincePrev += tx;

						if (statusType != 2) {
							tempAppStatsList.add(appStats);
						}
					} 
					tempOriDataSourceL1.add(appStats);

					appStats.elapsedTimeSincePrev = elapsedRealtimeNanos;
					appStats.elapsedTimeSinceStart = elapsedRealtimeNanos;
				}
            } catch (Resources.NotFoundException e) {
            }
        }

		prevElapsedTime = elapsedRealtimeNanos - startTime;
		startTime = elapsedRealtimeNanos;
        Collections.sort(tempAppStatsList, appStatsComparator);
		appStatAdapter.clear();
		appStatAdapter.addAll(tempAppStatsList);

		appStatAdapter.notifyDataSetChanged();
		update_labels();
	}
 
Example 19
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
@Override
public CharSequence getApplicationLabel(ApplicationInfo info) {
    return info.loadLabel(this);
}
 
Example 20
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
@Override
public CharSequence getApplicationLabel(ApplicationInfo info) {
    return info.loadLabel(this);
}