Example usage for android.content.pm ApplicationInfo loadLabel

List of usage examples for android.content.pm ApplicationInfo loadLabel

Introduction

In this page you can find the example usage for android.content.pm ApplicationInfo loadLabel.

Prototype

public @NonNull CharSequence loadLabel(@NonNull PackageManager pm) 

Source Link

Document

Retrieve the current textual label associated with this item.

Usage

From source file:net.sf.fakenames.fddemo.PermissionActivity.java

private CharSequence toLabel(int uid, String[] packages) {
    final SpannableStringBuilder ssb = new SpannableStringBuilder();

    if (packages != null) {
        try {// w  w w .  ja v a 2 s . c  o m
            final PackageManager pm = getPackageManager();

            if (packages.length == 1) {
                final ApplicationInfo soleInfo = pm.getApplicationInfo(packages[0], 0);
                if (soleInfo != null) {
                    final CharSequence label = soleInfo.loadLabel(pm);

                    if (!TextUtils.isEmpty(label)) {
                        ssb.append(label);
                        setBold(ssb, 0, ssb.length());
                        return ssb;
                    }
                }

                ssb.append(packages[0]);
                setBold(ssb, 0, ssb.length());
                return ssb;
            } else {
                Arrays.sort(packages);

                int named = 0;

                ssb.append("UID ").append(String.valueOf(uid)).append(" (");

                final ApplicationInfo firstInfo = pm.getApplicationInfo(packages[0], 0);

                if (firstInfo != null) {
                    final CharSequence label = firstInfo.loadLabel(pm);

                    if (!TextUtils.isEmpty(label)) {
                        ++named;

                        int old = ssb.length();
                        ssb.append(label);
                        setBold(ssb, old, ssb.length());
                    }
                }

                final int maxSummary = Math.min(packages.length, 3);
                int i;
                for (i = 1; i < maxSummary; ++i) {
                    final ApplicationInfo appInfo = pm.getApplicationInfo(packages[i], 0);

                    if (appInfo != null) {
                        final CharSequence label = appInfo.loadLabel(pm);

                        if (!TextUtils.isEmpty(label)) {
                            ++named;

                            ssb.append(", ");
                            int old = ssb.length();
                            ssb.append(label);
                            setBold(ssb, old, ssb.length());
                        }
                    }
                }

                if (named == 0) {
                    ssb.append(String.valueOf(packages.length)).append(" packages)");
                    setBold(ssb, 0, ssb.length());
                } else {
                    if (packages.length <= 4) {
                        ssb.append(")");
                    } else {
                        ssb.append(" and ");
                        ssb.append(String.valueOf(packages.length - named));
                        ssb.append(" others)");
                    }
                }

                return ssb;
            }
        } catch (PackageManager.NameNotFoundException ignored) {
        }
    }

    ssb.append("UID ").append(String.valueOf(uid));
    setBold(ssb, 0, ssb.length());
    return ssb;
}

From source file:com.rp.podemu.SettingsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    sharedPref = this.getSharedPreferences("PODEMU_PREFS", Context.MODE_PRIVATE);

    //String[] controlledApp = new String[appsRunning.size()];
    //Drawable[] icons = new Drawable[appsRunning.size()];
    //pm.getApplicationInfo(r.baseActivity.getPackageName(), PackageManager.GET_META_DATA);
    //Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    //startActivity(intent);

    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_APP_MUSIC);

    PackageManager pm = getPackageManager();
    String text = "";
    List<ResolveInfo> packages = pm.queryIntentActivities(intent, 0);
    //get a list of installed apps.
    //List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    //using hashset so that there will be no duplicate packages,
    //if no duplicate packages then there will be no duplicate apps
    HashSet<String> packageNames = new HashSet<String>(0);
    appInfos = new ArrayList<ApplicationInfo>(0);

    //getting package names and adding them to the hashset
    for (ResolveInfo resolveInfo : packages) {
        packageNames.add(resolveInfo.activityInfo.packageName);
    }/*from w  w  w.  j a  v  a 2 s .  c om*/

    // used just for tests
    /*
    ApplicationInfo dummyApp = new ApplicationInfo();
    dummyApp.name="select application";
    dummyApp.processName="dummy";
    appInfos.add(dummyApp);
    */

    for (String packageName : PodEmuIntentFilter.getAppList()) {
        try {
            appInfos.add(pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
        } catch (PackageManager.NameNotFoundException e) {
            //Do Nothing
        }
    }
    //now we have unique packages in the hashset, so get their application infos
    //and add them to the arraylist
    for (String packageName : packageNames) {
        try {
            appInfos.add(pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA));

        } catch (PackageManager.NameNotFoundException e) {
            //Do Nothing
        }
    }

    text += "Apps count: " + appInfos.size() + "\n";
    for (ApplicationInfo appInfo : appInfos) {
        //if (packageInfo.)
        {
            appInfo.name = (String) appInfo.loadLabel(pm);
            text += appInfo.loadLabel(pm) + "\n";
            //text += packageInfo.
            //text += "\n";
        }
    }

    //TextView textView = (TextView) findViewById(R.id.ctrlAppTitle);
    //textView.setText(text);

    //LauncherApps launcherApps=new LauncherApps();
    //List<LauncherActivityInfo> activities=launcherApps.getActivityList(null, android.os.Process.myUserHandle());
    //List<LauncherActivityInfo> activities=LauncherApps().getActivityList(null, android.os.Process.myUserHandle());

    baudRateList.add(9600);
    baudRateList.add(14400);
    baudRateList.add(19200);
    baudRateList.add(28800);
    baudRateList.add(38400);
    baudRateList.add(56000);
    baudRateList.add(57600);
    baudRateList.add(115200);

    try {
        PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        String version = pInfo.versionName;
        TextView versionHint = (TextView) findViewById(R.id.versionHint);
        versionHint.setText(getResources().getString(R.string.version_hint) + version);
    } catch (PackageManager.NameNotFoundException e) {
        // do nothing
    }

}

From source file:com.ayogo.cordova.notification.ScheduledNotificationManager.java

public void showNotification(ScheduledNotification scheduledNotification) {
    LOG.v(NotificationPlugin.TAG, "showNotification: " + scheduledNotification.toString());

    NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    // Build the notification options
    builder.setDefaults(Notification.DEFAULT_ALL).setTicker(scheduledNotification.body)
            .setPriority(Notification.PRIORITY_HIGH).setAutoCancel(true);

    // TODO: add sound support
    // if (scheduledNotification.sound != null) {
    //     builder.setSound(sound);
    // }//from  ww w. j a  v a2  s .c o  m

    if (scheduledNotification.body != null) {
        builder.setContentTitle(scheduledNotification.title);
        builder.setContentText(scheduledNotification.body);
        builder.setStyle(new NotificationCompat.BigTextStyle().bigText(scheduledNotification.body));
    } else {
        //Default the title to the app name
        try {
            PackageManager pm = context.getPackageManager();
            ApplicationInfo applicationInfo = pm.getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA);

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

            builder.setContentTitle(appName);
            builder.setContentText(scheduledNotification.title);
            builder.setStyle(new NotificationCompat.BigTextStyle().bigText(scheduledNotification.title));
        } catch (NameNotFoundException e) {
            LOG.v(NotificationPlugin.TAG, "Failed to set title for notification!");
            return;
        }
    }

    if (scheduledNotification.badge != null) {
        LOG.v(NotificationPlugin.TAG, "showNotification: has a badge!");
        builder.setSmallIcon(getResIdForDrawable(scheduledNotification.badge));
    } else {
        LOG.v(NotificationPlugin.TAG, "showNotification: has no badge, use app icon!");
        try {
            PackageManager pm = context.getPackageManager();
            ApplicationInfo applicationInfo = pm.getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA);
            Resources resources = pm.getResourcesForApplication(applicationInfo);
            builder.setSmallIcon(applicationInfo.icon);
        } catch (NameNotFoundException e) {
            LOG.v(NotificationPlugin.TAG, "Failed to set badge for notification!");
            return;
        }
    }

    if (scheduledNotification.icon != null) {
        LOG.v(NotificationPlugin.TAG, "showNotification: has an icon!");
        builder.setLargeIcon(getIconFromUri(scheduledNotification.icon));
    } else {
        LOG.v(NotificationPlugin.TAG, "showNotification: has no icon, use app icon!");
        try {
            PackageManager pm = context.getPackageManager();
            ApplicationInfo applicationInfo = pm.getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA);
            Resources resources = pm.getResourcesForApplication(applicationInfo);
            Bitmap appIconBitmap = BitmapFactory.decodeResource(resources, applicationInfo.icon);
            builder.setLargeIcon(appIconBitmap);
        } catch (NameNotFoundException e) {
            LOG.v(NotificationPlugin.TAG, "Failed to set icon for notification!");
            return;
        }
    }

    Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    launchIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    launchIntent.setAction("notification");

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launchIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    Notification notification = builder.build();

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    LOG.v(NotificationPlugin.TAG, "notify!");
    notificationManager.notify(scheduledNotification.tag.hashCode(), notification);
}

From source file:org.fdroid.fdroid.privileged.views.AppSecurityPermissions.java

private void setPermissions(List<MyPermissionInfo> permList) {
    if (permList != null) {
        // First pass to group permissions
        for (MyPermissionInfo pInfo : permList) {
            if (!isDisplayablePermission(pInfo, pInfo.existingReqFlags)) {
                continue;
            }//w ww . ja  va2  s  . c om
            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.mLabel = pgrp.loadLabel(pm);
        } else {
            try {
                ApplicationInfo app = pm.getApplicationInfo(pgrp.packageName, 0);
                pgrp.mLabel = app.loadLabel(pm);
            } catch (NameNotFoundException e) {
                pgrp.mLabel = pgrp.loadLabel(pm);
            }
        }
        permGroupsList.add(pgrp);
    }
    Collections.sort(permGroupsList, permGroupComparator);
}

From source file:com.rp.podemu.SettingsActivity.java

private void setCtrlApplicationInfo() {
    String processName = sharedPref.getString("ControlledAppProcessName", "unknown application");

    PackageManager pm = getPackageManager();
    ApplicationInfo appInfo;
    TextView textView = (TextView) findViewById(R.id.ctrlAppName);

    try {/*from   w  w w  .j  a  v a 2  s .c om*/
        appInfo = pm.getApplicationInfo(processName, PackageManager.GET_META_DATA);

        textView.setText(appInfo.loadLabel(pm));

        ImageView imageView = (ImageView) findViewById(R.id.ctrlAppIcon);
        imageView.setImageDrawable(appInfo.loadIcon(pm));

    } catch (PackageManager.NameNotFoundException e) {
        textView.setText("Cannot load application ");
    }

}

From source file:org.qeo.android.service.ApplicationSecurityStandalone.java

/**
 * Initialize a new instance.//from ww w . j av a 2  s  . co  m
 *
 * @param service Reference to the Qeo service
 * @param uid     the uid belonging to this content
 * @param pid     the pid belonging to this content
 */
ApplicationSecurityStandalone(QeoService service, int uid, int pid) {
    LOG.fine("Create new application security for uid: " + uid + ", pid: " + pid);
    mService = service;
    mUid = uid;
    mPid = pid;
    mRegisteredReadersWriters = new HashSet<Long>();
    try {
        PackageManager pm = mService.getPackageManager();
        // Packages with sharedUserId set in manifest will have their uid concatenated in getNameForUid call
        // What will happen if multiple packages with same sharedUserId make user of Qeo??
        mPkgName = pm.getNameForUid(mUid).split(":")[0];
        if (mPkgName.equals("org.qeo.android.service")) {
            // special case, will only be triggered by unit tests
            mAppVersion = 1;
            mAppLabel = "junit";
        } else {
            mAppVersion = pm.getPackageInfo(mPkgName, 0).versionCode;
            ApplicationInfo appInfo = pm.getPackageInfo(mPkgName, 0).applicationInfo;
            if (appInfo == null) {
                throw new SecurityException("Can't get application name for uid " + uid);
            }
            mAppLabel = appInfo.loadLabel(pm).toString();
            if (mAppLabel.isEmpty()) {
                throw new SecurityException("Can't get application name for uid " + uid);
            }
        }
        LOG.fine("Application security created: " + mPkgName + " -- " + mAppVersion + " -- " + mAppLabel);
    } catch (NameNotFoundException e) {
        throw new SecurityException("Can't get application version for uid " + uid, e);
    }
    mManifestDialogCallback = null;
}

From source file:org.namelessrom.devicecontrol.modules.appmanager.AppDetailsActivity.java

private void setupPermissionsView() {
    final LinearLayout permissionsView = (LinearLayout) findViewById(R.id.permissions_section);
    final boolean supported = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1;
    if (!supported) {
        permissionsView.setVisibility(View.GONE);
        return;/*from   w ww  .j  a v a 2  s. c  om*/
    }

    AppSecurityPermissions asp = new AppSecurityPermissions(this, mAppItem.getPackageName());
    // Make the security sections header visible
    LinearLayout securityList = (LinearLayout) findViewById(R.id.security_settings_list);
    securityList.removeAllViews();
    securityList.addView(asp.getPermissionsView());
    // If this app is running under a shared user ID with other apps,
    // update the description to explain this.
    String[] packages = mPm.getPackagesForUid(mAppItem.getApplicationInfo().uid);
    if (packages != null && packages.length > 1) {
        final ArrayList<CharSequence> pnames = new ArrayList<>();
        for (final String pkg : packages) {
            if (mAppItem.getPackageName().equals(pkg)) {
                continue;
            }
            try {
                ApplicationInfo ainfo = mPm.getApplicationInfo(pkg, 0);
                pnames.add(ainfo.loadLabel(mPm));
            } catch (PackageManager.NameNotFoundException ignored) {
            }
        }
        final int N = pnames.size();
        if (N > 0) {
            final Resources res = getResources();
            String appListStr;
            if (N == 1) {
                appListStr = pnames.get(0).toString();
            } else if (N == 2) {
                appListStr = res.getString(R.string.join_two_items, pnames.get(0), pnames.get(1));
            } else {
                appListStr = pnames.get(N - 2).toString();
                for (int i = N - 3; i >= 0; i--) {
                    appListStr = res.getString(
                            i == 0 ? R.string.join_many_items_first : R.string.join_many_items_middle,
                            pnames.get(i), appListStr);
                }
                appListStr = res.getString(R.string.join_many_items_last, appListStr, pnames.get(N - 1));
            }
            final TextView descr = (TextView) findViewById(R.id.security_settings_desc);
            descr.setText(res.getString(R.string.security_settings_desc_multi,
                    mAppItem.getApplicationInfo().loadLabel(mPm), appListStr));
        }
    }

}

From source file:com.android.leanlauncher.IconCache.java

/**
 * 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.
 */// w  ww .  j  av  a2 s  .  c  o m
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.icon == null) {
        entry = new CacheEntry();

        try {
            ApplicationInfo info = mPackageManager.getApplicationInfo(packageName, 0);
            entry.title = info.loadLabel(mPackageManager);
            entry.contentDescription = info.loadDescription(mPackageManager);
            Drawable defaultDrawable = info.loadIcon(mPackageManager);
            if (!TextUtils.isEmpty(mCurrentIconTheme)) {
                entry.icon = createNewIconBitmap(null, packageName, null, defaultDrawable);
            }

            if (entry.icon == null) {
                // pick default icon
                Log.d(TAG, packageName + " icon NOT FOUND in theme = " + mCurrentIconTheme);
                entry.icon = Utilities.createIconBitmap(defaultDrawable, mContext);
            }
            mCache.put(cacheKey, entry);
        } catch (NameNotFoundException e) {
            if (DEBUG)
                Log.d(TAG, "Application not installed " + packageName);
        }
    }
    return entry;
}

From source file:jackpal.androidterm.TermPreferences.java

private void setAppPickerList() {
    PackageManager pm = this.getApplicationContext().getPackageManager();
    final List<ApplicationInfo> installedAppList = pm.getInstalledApplications(0);
    final TreeMap<String, String> items = new TreeMap<>();
    for (ApplicationInfo app : installedAppList) {
        Intent intent = pm.getLaunchIntentForPackage(app.packageName);
        if (intent != null)
            items.put(app.loadLabel(pm).toString(), app.packageName);
    }/*www. j a  va2s  .co  m*/
    List<String> list = new ArrayList<>(items.keySet());
    final String labels[] = list.toArray(new String[list.size()]);
    list = new ArrayList<>(items.values());
    final String packageNames[] = list.toArray(new String[list.size()]);

    String id = "external_app_package_name";
    ListPreference packageName = (ListPreference) getPreferenceScreen().findPreference(id);
    packageName.setEntries(labels);
    packageName.setEntryValues(packageNames);
}

From source file:org.qeo.android.service.ApplicationSecurity.java

/**
 * Initialize a new instance.//  w ww.  j  a v a  2s  .  c o m
 *
 * @param service Reference to the Qeo service
 * @param uid     the uid belonging to this content
 * @param pid     the pid belonging to this content
 */
public ApplicationSecurity(QeoService service, int uid, int pid) {
    LOG.fine("Create new application security for uid: " + uid + ", pid: " + pid);
    mService = service;
    mUid = uid;
    mPid = pid;
    mRegisteredReadersWriters = new HashSet<Long>();
    try {
        PackageManager pm = mService.getPackageManager();
        // Packages with sharedUserId set in manifest will have their uid concatenated in getNameForUid call
        // What will happen if multiple packages with same sharedUserId make user of Qeo??
        mPkgName = pm.getNameForUid(mUid).split(":")[0];
        if (mPkgName.equals("org.qeo.android.service")) {
            // special case, will only be triggered by unit tests
            mAppVersion = 1;
            mAppLabel = "junit";
        } else {
            mAppVersion = pm.getPackageInfo(mPkgName, 0).versionCode;
            ApplicationInfo appInfo = pm.getPackageInfo(mPkgName, 0).applicationInfo;
            if (appInfo == null) {
                throw new SecurityException("Can't get application name for uid " + uid);
            }
            mAppLabel = appInfo.loadLabel(pm).toString();
            if (mAppLabel == null || mAppLabel.isEmpty()) {
                throw new SecurityException("Can't get application name for uid " + uid);
            }
        }
        LOG.fine("Application security created: " + mPkgName + " -- " + mAppVersion + " -- " + mAppLabel);
    } catch (NameNotFoundException e) {
        throw new SecurityException("Can't get application version for uid " + uid, e);
    }
    mManifestDialogCallback = null;
}