Example usage for android.content.pm PackageManager getPermissionInfo

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

Introduction

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

Prototype

public abstract PermissionInfo getPermissionInfo(String name, @PermissionInfoFlags int flags)
        throws NameNotFoundException;

Source Link

Document

Retrieve all of the information we know about a particular permission.

Usage

From source file:Main.java

private static String getPermissionLabel(Context context, String permission, String defValue) {
    try {/*from   w w w  .j a va 2  s  . c om*/
        final PackageManager pm = context.getPackageManager();
        final PermissionInfo pi = pm.getPermissionInfo(permission, 0);

        final CharSequence label = pi.loadLabel(pm);
        if (label != null && !label.toString().equals(permission))
            return label.toString();
    } catch (NameNotFoundException e) {
        // om nom nom
    }

    return defValue;
}

From source file:com.kaliturin.blacklist.utils.Permissions.java

/**
 * Returns information string about permission
 **//*from w  w w .ja v  a 2 s. c  o  m*/
@Nullable
private static String getPermissionInfo(@NonNull Context context, @NonNull String permission) {
    context = context.getApplicationContext();
    PackageManager pm = context.getPackageManager();
    PermissionInfo info = null;
    try {
        info = pm.getPermissionInfo(permission, PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException ex) {
        Log.w(TAG, ex);
    }

    if (info != null) {
        CharSequence label = info.loadLabel(pm);
        if (label == null) {
            label = info.nonLocalizedLabel;
        }
        return label.toString();
    }

    return null;
}

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

/**
 * Checks that the application manifest is properly configured.
 * <p>/*from   w  w w. j  a va 2s  . c o  m*/
 * 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:org.microg.tools.selfcheck.PermissionCheckGroup.java

private void doPermissionCheck(Context context, ResultCollector collector, final String permission) {
    PackageManager pm = context.getPackageManager();
    try {// w  ww. j a v a  2 s .c o  m
        PermissionInfo info = pm.getPermissionInfo(permission, 0);
        PermissionGroupInfo groupInfo = info.group != null ? pm.getPermissionGroupInfo(info.group, 0) : null;
        CharSequence permLabel = info.loadLabel(pm);
        CharSequence groupLabel = groupInfo != null ? groupInfo.loadLabel(pm) : permLabel;
        collector.addResult(context.getString(R.string.self_check_name_permission, permLabel),
                context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED ? Positive
                        : Negative,
                context.getString(R.string.self_check_resolution_permission, groupLabel),
                new SelfCheckGroup.CheckResolver() {

                    @Override
                    public void tryResolve(Fragment fragment) {
                        fragment.requestPermissions(new String[] { permission }, 0);
                    }
                });
    } catch (PackageManager.NameNotFoundException e) {
        Log.w(TAG, e);
    }
}

From source file:com.vuze.android.remote.AppCompatActivityM.java

public void requestPermissions(String[] permissions, Runnable runnableOnGrant, Runnable runnableOnDeny) {

    // requestPermissions supposedly does checkSelfPermission for us, but
    // I get prompted anyway, and clicking Revoke (on an already granted perm):
    // I/ActivityManager: Killing xxxx:com.vuze.android.remote/u0a24 (adj 1): permissions revoked
    // Also, requestPermissions assumes PERMISSION_REVOKED on unknown
    // permission strings (ex READ_EXTERNAL_STORAGE on API 7)
    boolean allGranted = true;
    if (permissions.length > 0) {
        PackageManager packageManager = getPackageManager();
        for (int i = 0; i < permissions.length; i++) {
            try {
                packageManager.getPermissionInfo(permissions[i], 0);
            } catch (PackageManager.NameNotFoundException e) {
                Log.d("Perms", "requestPermissions: Permission " + permissions[i]
                        + " doesn't exist.  Assuming granted.");
                continue;
            }//w w  w . j  a va  2s  . c  om
            if (ActivityCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                allGranted = false;
                break;
            }
        }
    }

    if (allGranted) {
        if (AndroidUtils.DEBUG) {
            Log.d("Perms", "requestPermissions: allGranted, running " + runnableOnGrant);
        }
        if (runnableOnGrant != null) {
            runnableOnGrant.run();
        }
        return;
    }

    if (AndroidUtils.DEBUG) {
        Log.d("Perms",
                "requestPermissions: requesting " + Arrays.toString(permissions) + " for " + runnableOnGrant);
    }
    requestPermissionRunnables.put(requestPermissionID, new Runnable[] { runnableOnGrant, runnableOnDeny });
    ActivityCompat.requestPermissions(this, permissions, requestPermissionID);
    requestPermissionID++;
}

From source file:com.vuze.android.remote.FragmentM.java

public void requestPermissions(String[] permissions, Runnable runnableOnGrant, Runnable runnableOnDeny) {
    // requestPermissions supposedly does checkSelfPermission for us, but
    // I get prompted anyway, and clicking Revoke (on an already granted perm):
    // I/ActivityManager: Killing xxxx:com.vuze.android.remote/u0a24 (adj 1):
    // permissions revoked

    boolean allGranted = true;
    if (permissions.length > 0) {
        PackageManager packageManager = getContext().getPackageManager();
        for (int i = 0; i < permissions.length; i++) {
            try {
                packageManager.getPermissionInfo(permissions[i], 0);
            } catch (PackageManager.NameNotFoundException e) {
                Log.d("Perms", "requestPermissions: Permission " + permissions[i]
                        + " doesn't exist.  Assuming granted.");
                continue;
            }/* ww  w. j a va  2  s  .  co m*/
            if (ActivityCompat.checkSelfPermission(getContext(),
                    permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                allGranted = false;
                break;
            }
        }
    }

    if (allGranted) {
        if (AndroidUtils.DEBUG) {
            Log.d("Perms", "requestPermissions: allGranted, running " + runnableOnGrant);
        }
        if (runnableOnGrant != null) {
            runnableOnGrant.run();
        }
        return;
    }
    /**/

    if (AndroidUtils.DEBUG) {
        Log.d("Perms",
                "requestPermissions: requesting " + Arrays.toString(permissions) + " for " + runnableOnGrant);
    }
    requestPermissionRunnables.put(requestPermissionID, new Runnable[] { runnableOnGrant, runnableOnDeny });
    requestPermissions(permissions, requestPermissionID);
    requestPermissionID++;
}

From source file:com.afwsamples.testdpc.provision.PostProvisioningTask.java

private boolean isRuntimePermission(PackageManager packageManager, String permission) {
    try {/*from w w  w .j a v  a  2 s. c  om*/
        PermissionInfo pInfo = packageManager.getPermissionInfo(permission, 0);
        if (pInfo != null) {
            if ((pInfo.protectionLevel
                    & PermissionInfo.PROTECTION_MASK_BASE) == PermissionInfo.PROTECTION_DANGEROUS) {
                return true;
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.i(TAG, "Could not retrieve info about the permission: " + permission);
    }
    return false;
}

From source file:com.vuze.android.remote.AndroidUtils.java

public static boolean hasPermisssion(@NonNull Context context, @NonNull String permission) {
    PackageManager packageManager = context.getPackageManager();
    try {/*from www .  j av  a  2  s  .c  o  m*/
        packageManager.getPermissionInfo(permission, 0);
    } catch (PackageManager.NameNotFoundException e) {
        Log.d("Perms", "requestPermissions: Permission " + permission + " doesn't exist.  Assuming granted.");
        return true;
    }
    return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
}

From source file:com.github.michalbednarski.intentslab.browser.ComponentFetcher.java

private boolean checkPermissionFilter(PackageManager pm, ComponentInfo cmp) {
    // Not exported?
    if (!cmp.exported) {
        return (protection & PROTECTION_UNEXPORTED) != 0;
    }/*from  ww  w .j  av  a2s  .  c  om*/

    // Get checked permission
    String permission = cmp instanceof ServiceInfo ? ((ServiceInfo) cmp).permission
            : cmp instanceof ActivityInfo ? ((ActivityInfo) cmp).permission
                    : cmp instanceof ProviderInfo
                            ? (testWritePermissionForProviders ? ((ProviderInfo) cmp).writePermission
                                    : ((ProviderInfo) cmp).readPermission)
                            : null;

    // World accessible
    if (permission == null) {
        return (protection & PROTECTION_WORLD_ACCESSIBLE) != 0;
    }

    // Skip checking protectionLevel if it doesn't matter
    if ((protection & PROTECTION_ANY_PERMISSION) == PROTECTION_ANY_PERMISSION) {
        return true;
    }
    if ((protection & PROTECTION_ANY_PERMISSION) == 0) {
        return false;
    }

    // Obtain PermissionInfo
    PermissionInfo permissionInfo;
    try {
        permissionInfo = pm.getPermissionInfo(permission, 0);
    } catch (PackageManager.NameNotFoundException e) {
        Log.v("PermissionFilter", "Unknown permission " + permission + " for " + cmp.name, e);
        return (protection & PROTECTION_UNKNOWN) != 0;
    }

    return checkProtectionLevel(permissionInfo, protection);
}