Example usage for android.content.pm PackageManager getApplicationLabel

List of usage examples for android.content.pm PackageManager getApplicationLabel

Introduction

In this page you can find the example usage for android.content.pm PackageManager getApplicationLabel.

Prototype

public abstract CharSequence getApplicationLabel(ApplicationInfo info);

Source Link

Document

Return the label to use for this application.

Usage

From source file:com.google.android.gms.common.GooglePlayServicesUtilLight.java

public static String zzap(Context context) {
    Object obj = context.getApplicationInfo().name;
    if (!TextUtils.isEmpty(obj)) {
        return obj;
    }/*from  w  w  w  .j  a  v a 2  s .  co m*/
    ApplicationInfo applicationInfo;
    String packageName = context.getPackageName();
    PackageManager packageManager = context.getApplicationContext().getPackageManager();
    try {
        applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
    } catch (NameNotFoundException e) {
        applicationInfo = null;
    }
    return applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo).toString()
            : packageName;
}

From source file:com.google.android.gms.common.zze.java

public static String zzao(Context context) {
    Object obj = context.getApplicationInfo().name;
    if (!TextUtils.isEmpty(obj)) {
        return obj;
    }/*from   ww w .ja  v  a2  s  .c o  m*/
    ApplicationInfo applicationInfo;
    String packageName = context.getPackageName();
    PackageManager packageManager = context.getApplicationContext().getPackageManager();
    try {
        applicationInfo = packageManager.getApplicationInfo(context.getPackageName(),
                GOOGLE_PLAY_SERVICES_VERSION_CODE);
    } catch (NameNotFoundException e) {
        applicationInfo = null;
    }
    return applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo).toString()
            : packageName;
}

From source file:com.google.android.gms.common.GooglePlayServicesUtil.java

private static String m119u(Context context) {
    Object obj = context.getApplicationInfo().name;
    if (!TextUtils.isEmpty(obj)) {
        return obj;
    }/*from w w  w.j  ava 2  s.  c  om*/
    ApplicationInfo applicationInfo;
    String packageName = context.getPackageName();
    PackageManager packageManager = context.getApplicationContext().getPackageManager();
    try {
        applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
    } catch (NameNotFoundException e) {
        applicationInfo = null;
    }
    return applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo).toString()
            : packageName;
}

From source file:com.amazonaws.mobileconnectors.util.ClientContext.java

/**
 * Gets the client info, including installation_id_id, app_title,
 * app_version_name, app_version_code, and app_package_name.
 *
 * @param context context of the app//  w  w  w  .  j  a  va  2 s.  c o  m
 * @return an JSONObject that has the client info
 * @throws JSONException
 */
static JSONObject getClientInfo(Context context) throws JSONException {
    final JSONObject client = new JSONObject();

    final PackageManager packageManager = context.getPackageManager();
    try {
        final PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        final ApplicationInfo applicationInfo = context.getApplicationInfo();

        client.put("installation_id", getInstallationId(context))
                .put("app_version_name", packageInfo.versionName)
                .put("app_version_code", String.valueOf(packageInfo.versionCode))
                .put("app_package_name", packageInfo.packageName);
        // If null is returned for some reason, fall back to "Unknown"
        final CharSequence title = packageManager.getApplicationLabel(applicationInfo);
        client.put("app_title", title == null ? "Unknown" : title.toString());
    } catch (final NameNotFoundException e) {
        // When device starts, PackageManager will gather package
        // information by scanning them. It will take a while to finish.
        // This exception may be thrown when scan doesn't finish.
        LOGGER.warn("Failed to load package info: " + context.getPackageName(), e);
    }

    return client;
}

From source file:com.jungle.base.utils.MiscUtils.java

public static String getAppName() {
    String pkgName = MiscUtils.getPackageName();
    PackageManager mgr = BaseApplication.getAppContext().getPackageManager();

    try {//from w  w  w  .  j a  v  a 2 s  .c o m
        ApplicationInfo info = mgr.getApplicationInfo(pkgName, 0);
        return String.valueOf(mgr.getApplicationLabel(info));
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    return pkgName;
}

From source file:com.sxnyodot.uefqvmio207964.Util.java

static String m979p(Context context) {
    try {// w  w w  .  jav a  2 s  . c om
        ApplicationInfo applicationInfo;
        PackageManager packageManager = context.getPackageManager();
        try {
            applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        } catch (NameNotFoundException e) {
            applicationInfo = null;
        }
        return (String) (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo)
                : "(unknown)");
    } catch (Exception e2) {
        e2.printStackTrace();
        return "";
    }
}

From source file:com.harshad.linconnectclient.NotificationUtilities.java

public static boolean sendData(Context c, Notification n, String packageName) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

    ConnectivityManager connManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    // Check Wifi state, whether notifications are enabled globally, and
    // whether notifications are enabled for specific application
    if (prefs.getBoolean("pref_toggle", true) && prefs.getBoolean(packageName, true) && mWifi.isConnected()) {
        String ip = prefs.getString("pref_ip", "0.0.0.0:9090");

        // Magically extract text from notification
        ArrayList<String> notificationData = NotificationUtilities.getNotificationText(n);

        // Use PackageManager to get application name and icon
        final PackageManager pm = c.getPackageManager();
        ApplicationInfo ai;//from   www  .  j a va 2 s  .  c o  m
        try {
            ai = pm.getApplicationInfo(packageName, 0);
        } catch (final NameNotFoundException e) {
            ai = null;
        }

        String notificationBody = "";
        String notificationHeader = "";
        // Create header and body of notification
        if (notificationData.size() > 0) {
            notificationHeader = notificationData.get(0);
            if (notificationData.size() > 1) {
                notificationBody = notificationData.get(1);
            }
        } else {
            return false;
        }

        for (int i = 2; i < notificationData.size(); i++) {
            notificationBody += "\n" + notificationData.get(i);
        }

        // Append application name to body
        if (pm.getApplicationLabel(ai) != null) {
            if (notificationBody.isEmpty()) {
                notificationBody = "via " + pm.getApplicationLabel(ai);
            } else {
                notificationBody += " (via " + pm.getApplicationLabel(ai) + ")";
            }
        }

        // Setup HTTP request
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        // If the notification contains an icon, use it
        if (n.largeIcon != null) {
            entity.addPart("notificon",
                    new InputStreamBody(ImageUtilities.bitmapToInputStream(n.largeIcon), "drawable.png"));
        }
        // Otherwise, use the application's icon
        else {
            entity.addPart("notificon",
                    new InputStreamBody(
                            ImageUtilities.bitmapToInputStream(
                                    ImageUtilities.drawableToBitmap(pm.getApplicationIcon(ai))),
                            "drawable.png"));
        }

        HttpPost post = new HttpPost("http://" + ip + "/notif");
        post.setEntity(entity);

        try {
            post.addHeader("notifheader", Base64.encodeToString(notificationHeader.getBytes("UTF-8"),
                    Base64.URL_SAFE | Base64.NO_WRAP));
            post.addHeader("notifdescription", Base64.encodeToString(notificationBody.getBytes("UTF-8"),
                    Base64.URL_SAFE | Base64.NO_WRAP));
        } catch (UnsupportedEncodingException e) {
            post.addHeader("notifheader",
                    Base64.encodeToString(notificationHeader.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP));
            post.addHeader("notifdescription",
                    Base64.encodeToString(notificationBody.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP));
        }

        // Send HTTP request
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        try {
            response = client.execute(post);
            String html = EntityUtils.toString(response.getEntity());
            if (html.contains("true")) {
                return true;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return false;
}

From source file:com.noshufou.android.su.util.Util.java

public static String getAppName(Context c, int uid, boolean withUid) {
    if (sSystemUids.get(uid) != null) {
        return sSystemUids.get(uid);
    }//from  w w  w .j a v a  2s  .  c o m

    PackageManager pm = c.getPackageManager();
    String appName = "Unknown";
    String[] packages = pm.getPackagesForUid(uid);

    if (packages != null) {
        try {
            if (packages.length == 1) {
                appName = pm.getApplicationLabel(pm.getApplicationInfo(packages[0], 0)).toString();
            } else if (packages.length > 1) {
                appName = "";
                for (int i = 0; i < packages.length; i++) {
                    appName += packages[i];
                    if (i < packages.length - 1) {
                        appName += ", ";
                    }
                }
            }
        } catch (NameNotFoundException e) {
            Log.e(TAG, "Package name not found", e);
        }
    } else {
        Log.e(TAG, "Package not found for uid " + uid);
    }

    if (withUid) {
        appName += " (" + uid + ")";
    }

    return appName;
}

From source file:org.whispersystems.libpastelog.SubmitLogFragment.java

private static String buildDescription(Context context) {
    final PackageManager pm = context.getPackageManager();
    final StringBuilder builder = new StringBuilder();

    builder.append("Device  : ").append(Build.MANUFACTURER).append(" ").append(Build.MODEL).append(" (")
            .append(Build.PRODUCT).append(")\n");
    builder.append("Android : ").append(VERSION.RELEASE).append(" (").append(VERSION.INCREMENTAL).append(", ")
            .append(Build.DISPLAY).append(")\n");
    builder.append("Memory  : ").append(getMemoryUsage(context)).append("\n");
    builder.append("Memclass: ").append(getMemoryClass(context)).append("\n");
    builder.append("OS Host : ").append(Build.HOST).append("\n");
    builder.append("App     : ");
    try {//from  ww w .j  a  v a 2 s. c o m
        builder.append(pm.getApplicationLabel(pm.getApplicationInfo(context.getPackageName(), 0))).append(" ")
                .append(pm.getPackageInfo(context.getPackageName(), 0).versionName).append("\n");
    } catch (PackageManager.NameNotFoundException nnfe) {
        builder.append("Unknown\n");
    }

    return builder.toString();
}

From source file:com.github.pennyfive.sqlitestudio.ui.PackagesFragment.java

private String getApplicationName(String packageName) {
    try {/*from   ww w  .  java2s.c om*/
        PackageManager pm = getActivity().getPackageManager();
        return (String) pm.getApplicationLabel(pm.getApplicationInfo(packageName, 0));
    } catch (NameNotFoundException e) {
        return "";
    }
}