Example usage for android.content.pm PackageManager getPackageInfo

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

Introduction

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

Prototype

public abstract PackageInfo getPackageInfo(VersionedPackage versionedPackage, @PackageInfoFlags int flags)
        throws NameNotFoundException;

Source Link

Document

Retrieve overall information about an application package that is installed on the system.

Usage

From source file:com.facebook.Settings.java

public static String getApplicationSignature(Context context) {
    if (context == null) {
        return null;
    }//from  w ww  .  j a  v  a  2s . c  o  m
    PackageManager packageManager = context.getPackageManager();
    if (packageManager == null) {
        return null;
    }

    String packageName = context.getPackageName();
    PackageInfo pInfo;
    try {
        pInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }

    Signature[] signatures = pInfo.signatures;
    if (signatures == null || signatures.length == 0) {
        return null;
    }

    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        return null;
    }

    md.update(pInfo.signatures[0].toByteArray());
    return Base64.encodeToString(md.digest(), Base64.URL_SAFE | Base64.NO_PADDING);
}

From source file:com.apptentive.android.sdk.util.Util.java

public static String getAppVersionName(Context appContext) {
    try {//  w w  w .jav  a2 s .  c  o m
        PackageManager packageManager = appContext.getPackageManager();
        PackageInfo packageInfo = packageManager.getPackageInfo(appContext.getPackageName(), 0);
        return packageInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        ApptentiveLog.e("Error getting app version name.", e);
    }
    return null;
}

From source file:com.apptentive.android.sdk.util.Util.java

public static int getAppVersionCode(Context appContext) {
    try {//from w  w  w.j  a v  a 2s  .  co m
        PackageManager packageManager = appContext.getPackageManager();
        PackageInfo packageInfo = packageManager.getPackageInfo(appContext.getPackageName(), 0);
        return packageInfo.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        ApptentiveLog.e("Error getting app version code.", e);
    }
    return -1;
}

From source file:com.facebook.FacebookSdk.java

/**
 * Internal call please don't use directly.
 * @param context The application context.
 * @return The application signature.// w ww.  ja  v a  2 s  . com
 */
public static String getApplicationSignature(Context context) {
    Validate.sdkInitialized();
    if (context == null) {
        return null;
    }
    PackageManager packageManager = context.getPackageManager();
    if (packageManager == null) {
        return null;
    }

    String packageName = context.getPackageName();
    PackageInfo pInfo;
    try {
        pInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }

    Signature[] signatures = pInfo.signatures;
    if (signatures == null || signatures.length == 0) {
        return null;
    }

    MessageDigest md;
    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        return null;
    }

    md.update(pInfo.signatures[0].toByteArray());
    return Base64.encodeToString(md.digest(), Base64.URL_SAFE | Base64.NO_PADDING);
}

From source file:com.google.android.gcm.GCMRegistrar.java

/**
 * Checks that the application manifest is properly configured.
 * <p>/*from  w  w  w.  j  a  v  a2  s  .  com*/
 * A proper configuration means:
 * <ol>
 *    <li>It creates a custom permission called
 *      {@code PACKAGE_NAME.permission.C2D_MESSAGE}.
 *    <li>It defines at least one {@link BroadcastReceiver} with category
 *      {@code PACKAGE_NAME}.
 *    <li>The {@link BroadcastReceiver}(s) uses the
 *      {@value GCMConstants#PERMISSION_GCM_INTENTS} permission.
 *    <li>The {@link BroadcastReceiver}(s) handles the 3 GCM intents
 *      ({@value GCMConstants#INTENT_FROM_GCM_MESSAGE},
 *      {@value GCMConstants#INTENT_FROM_GCM_REGISTRATION_CALLBACK},
 *      and {@value GCMConstants#INTENT_FROM_GCM_LIBRARY_RETRY}).
 * </ol>
 * ...where {@code PACKAGE_NAME} is the application package.
 * <p>
 * This method should be used during development time to verify that the
 * manifest is properly set up, but it doesn't need to be called once the
 * application is deployed to the users' devices.
 *
 * @param context application context.
 * @throws IllegalStateException if any of the conditions above is not met.
 */
public static void checkManifest(Context context) {
    PackageManager packageManager = context.getPackageManager();
    String packageName = context.getPackageName();
    String permissionName = packageName + ".permission.C2D_MESSAGE";
    // check permission
    try {
        packageManager.getPermissionInfo(permissionName, PackageManager.GET_PERMISSIONS);
    } catch (NameNotFoundException e) {
        throw new IllegalStateException("Application does not define permission " + permissionName);
    }
    // check receivers
    PackageInfo receiversInfo;
    try {
        receiversInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_RECEIVERS);
    } catch (NameNotFoundException e) {
        throw new IllegalStateException("Could not get receivers for package " + packageName);
    }
    ActivityInfo[] receivers = receiversInfo.receivers;
    if (receivers == null || receivers.length == 0) {
        throw new IllegalStateException("No receiver for package " + packageName);
    }
    if (Log.isLoggable(TAG, Log.VERBOSE)) {
        Log.v(TAG, "number of receivers for " + packageName + ": " + receivers.length);
    }
    Set<String> allowedReceivers = new HashSet<String>();
    for (ActivityInfo receiver : receivers) {
        if (GCMConstants.PERMISSION_GCM_INTENTS.equals(receiver.permission)) {
            allowedReceivers.add(receiver.name);
        }
    }
    if (allowedReceivers.isEmpty()) {
        throw new IllegalStateException(
                "No receiver allowed to receive " + GCMConstants.PERMISSION_GCM_INTENTS);
    }
    checkReceiver(context, allowedReceivers, GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK);
    checkReceiver(context, allowedReceivers, GCMConstants.INTENT_FROM_GCM_MESSAGE);
}

From source file:android_network.hetnet.vpn_service.Util.java

public static boolean isSystem(String packageName, Context context) {
    try {//from  w w w. j a  va 2s . c o m
        PackageManager pm = context.getPackageManager();
        PackageInfo info = pm.getPackageInfo(packageName, 0);
        return ((info.applicationInfo.flags
                & (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0);
        /*
        PackageInfo pkg = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
        PackageInfo sys = pm.getPackageInfo("android", PackageManager.GET_SIGNATURES);
        return (pkg != null && pkg.signatures != null && pkg.signatures.length > 0 &&
            sys.signatures.length > 0 && sys.signatures[0].equals(pkg.signatures[0]));
        */
    } catch (PackageManager.NameNotFoundException ignore) {
        return false;
    }
}

From source file:android_network.hetnet.vpn_service.Util.java

public static String getFingerprint(Context context) {
    try {/*w  w  w  .  j av a  2  s . co  m*/
        PackageManager pm = context.getPackageManager();
        String pkg = context.getPackageName();
        PackageInfo info = pm.getPackageInfo(pkg, PackageManager.GET_SIGNATURES);
        byte[] cert = info.signatures[0].toByteArray();
        MessageDigest digest = MessageDigest.getInstance("SHA1");
        byte[] bytes = digest.digest(cert);
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes)
            sb.append(Integer.toString(b & 0xff, 16).toLowerCase());
        return sb.toString();
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        return null;
    }
}

From source file:it.evilsocket.dsploit.core.System.java

public static String getAppVersionName() {
    try {/*from  www. j  av  a  2 s .  c  o  m*/
        PackageManager manager = mContext.getPackageManager();
        PackageInfo info = manager.getPackageInfo(mContext.getPackageName(), 0);

        return info.versionName;
    } catch (NameNotFoundException e) {
        errorLogging(TAG, e);
    }

    return "?";
}

From source file:am.roadpolice.roadpolice.AboutUsDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    View dialogView = getActivity().getLayoutInflater().inflate(R.layout.dialog_about_us, null);
    TextView tvVersion = (TextView) dialogView.findViewById(R.id.tvAboutUsVersion);
    try {/*ww  w.  j  a v  a2  s. c om*/
        PackageManager manager = getActivity().getPackageManager();
        PackageInfo info = manager.getPackageInfo(getActivity().getPackageName(), 0);
        tvVersion.setText(tvVersion.getText() + " " + info.versionName);

    } catch (PackageManager.NameNotFoundException e) {
        tvVersion.setVisibility(View.GONE);
    }

    // Makes the link in the text view clickable.
    TextView t2 = (TextView) dialogView.findViewById(R.id.tvAboutUsDeveloper);
    t2.setMovementMethod(LinkMovementMethod.getInstance());

    // Creates About Us Alert dialog which extends from DialogFragment.
    return new AlertDialog.Builder(getActivity()).setTitle(R.string.txtAboutUs)
            .setNeutralButton(R.string.about_us_close, null).setView(dialogView).create();
}

From source file:com.apptentive.android.sdk.module.engagement.interaction.fragment.UpgradeMessageFragment.java

private Drawable getIconDrawableResourceId() {
    try {//  w  w w .  j  ava 2 s  .  c o  m
        Context context = getContext();
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
        return ContextCompat.getDrawable(getContext(), pi.applicationInfo.icon);
    } catch (Exception e) {
        ApptentiveLog.e("Error loading app icon.", e);
    }
    return null;
}