Java Code Examples for android.content.pm.PackageManager#getPermissionInfo()

The following examples show how to use android.content.pm.PackageManager#getPermissionInfo() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: BlackList   File: Permissions.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns information string about permission
 **/
@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;
}
 
Example 2
private void doPermissionCheck(Context context, ResultCollector collector, final String permission) {
    PackageManager pm = context.getPackageManager();
    try {
        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);
    }
}
 
Example 3
private static String getPermissionLabel(Context context, String permission, String defValue)
{
	try
	{
		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;
}
 
Example 4
Source Project: Collection-Android   File: GetPermissionsUtils.java    License: MIT License 5 votes vote down vote up
public static String  getAllPermissons(Context context){

        StringBuffer stringBuffer=new StringBuffer();
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            //得到自己的包名
            String pkgName = pi.packageName;

            PermissionGroupInfo pgi;
            PackageInfo pkgInfo = pm.getPackageInfo(pkgName, PackageManager.GET_PERMISSIONS);
            String sharedPkgList[] = pkgInfo.requestedPermissions;
            LogUtils.error("权限",sharedPkgList.length+"");

            for(int i=0;i<sharedPkgList.length;i++){
                String permName = sharedPkgList[i];

                PermissionInfo tmpPermInfo = pm.getPermissionInfo(permName, 0);

                stringBuffer.append("========"+permName+"========"+"\n");
                stringBuffer.append(tmpPermInfo.loadLabel(pm).toString()+"\n");
            }


        } catch (PackageManager.NameNotFoundException e) {
            LogUtils.error("权限","报错:"+e);
        }

        return stringBuffer.toString();
    }
 
Example 5
Source Project: AppOpsX   File: Helper.java    License: MIT License 5 votes vote down vote up
private static OpEntryInfo opEntry2Info(OpEntry op, Context context, PackageManager pm) {
  OpEntryInfo opEntryInfo = new OpEntryInfo(op);
  if (OtherOp.isOtherOp(op.getOp())) {
    opEntryInfo.opName = OtherOp.getOpName(op.getOp());
    opEntryInfo.opPermsName = OtherOp.getOpPermName(op.getOp());
  }
  if (opEntryInfo.opName != null) {
    try {
      if (!OtherOp.isOtherOp(op.getOp())) {
        PermissionInfo permissionInfo = pm.getPermissionInfo(opEntryInfo.opPermsName, 0);
        opEntryInfo.opPermsLab = String.valueOf(permissionInfo.loadLabel(pm));
        opEntryInfo.opPermsDesc = String.valueOf(permissionInfo.loadDescription(pm));
      }
    } catch (PackageManager.NameNotFoundException e) {
      //ignore
    }

    if (opEntryInfo.opPermsLab == null) {
      Integer resId = sPermI18N.get(opEntryInfo.opName);
      if (resId != null) {
        opEntryInfo.opPermsLab = context.getString(resId);
        opEntryInfo.opPermsDesc = opEntryInfo.opName;
      }
    }

    return opEntryInfo;
  }
  return null;
}
 
Example 6
Source Project: AppOpsX   File: Helper.java    License: MIT License 5 votes vote down vote up
public static List<OpEntryInfo> getLocalOpEntryInfos(Context context) {
  if (sOpEntryInfoList.isEmpty()) {
    int[] sOpToSwitch = FixCompat.sOpToSwitch();
    String[] sOpNames = FixCompat.sOpNames();
    String[] sOpPerms = FixCompat.sOpPerms();
    int len = sOpPerms.length;
    PackageManager pm = context.getPackageManager();
    for (int i = 0; i < len; i++) {
      OpEntry entry = new OpEntry(sOpToSwitch[i], AppOpsManager.MODE_ALLOWED, 0, 0, 0, 0, null);
      OpEntryInfo opEntryInfo = new OpEntryInfo(entry);
      opEntryInfo.opName = sOpNames[i];
      try {
        PermissionInfo permissionInfo = pm.getPermissionInfo(sOpPerms[i], 0);
        opEntryInfo.opPermsLab = String.valueOf(permissionInfo.loadLabel(pm));
        opEntryInfo.opPermsDesc = String.valueOf(permissionInfo.loadDescription(pm));
      } catch (PackageManager.NameNotFoundException e) {
        //ignore
        Integer resId = sPermI18N.get(opEntryInfo.opName);
        if (resId != null) {
          opEntryInfo.opPermsLab = context.getString(resId);
          opEntryInfo.opPermsDesc = opEntryInfo.opName;
        } else {
          opEntryInfo.opPermsLab = opEntryInfo.opName;
        }
      }
      sOpEntryInfo.put(entry.getOp(), opEntryInfo);
      sAllOps.put(entry.getOp(), entry.getOp());
      sOpEntryInfoList.add(opEntryInfo);
    }
  }
  return new ArrayList<OpEntryInfo>(sOpEntryInfoList);
}
 
Example 7
/**
 * 获取权限名称
 * <p>在需要显示某个具体权限的名称时使用</p>
 */
public static String getPermissionName(Activity context, String permission) {
    PackageManager packageManager = context.getPackageManager();
    try {
        PermissionInfo permissionInfo = packageManager.getPermissionInfo(permission, 0);
        return permissionInfo.loadLabel(packageManager).toString();
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return permission;
}
 
Example 8
/**
 * 获取权限所属权限组的名称
 * <p>在需要显示某个权限所属的一组权限的名称时使用</p>
 */
public static String getPermissionGroupName(Context context, String permission) {
    PackageManager packageManager = context.getPackageManager();
    try {
        PermissionInfo permissionInfo = packageManager.getPermissionInfo(permission, 0);
        PermissionGroupInfo permissionGroupInfo = packageManager.getPermissionGroupInfo(permissionInfo.group, 0);
        return permissionGroupInfo.loadDescription(packageManager).toString();
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return permission;
}
 
Example 9
private void askForPermission() {
    if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        // Should we show an explanation?
        if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            String name;
            try {
                PackageManager pm = getPackageManager();
                PermissionInfo permissionInfo = pm.getPermissionInfo(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.GET_META_DATA);
                name = (String) permissionInfo.loadLabel(pm);
            } catch (PackageManager.NameNotFoundException e) {
                logger.error("Failed to obtain name for permission", e);
                name = "read external storage";
            }
            new AlertDialog.Builder(this)
                    .setMessage(getString(R.string.msgWriteExternalStorageRationale, name))
                    .setPositiveButton(R.string.ok, (dialog, which) -> requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE))
                    .setNegativeButton(R.string.cancel, (dialog, which) -> finish())
                    .create()
                    .show();
        } else {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
        }

    } else {
        mDataMoveFragment.startDataMove();
    }
}
 
Example 10
private void askForPermission(Runnable task) {
    mTask = task;
    if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        // Should we show an explanation?
        if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
            String name;
            try {
                PackageManager pm = getPackageManager();
                PermissionInfo permissionInfo = pm.getPermissionInfo(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.GET_META_DATA);
                name = (String) permissionInfo.loadLabel(pm);
            } catch (PackageManager.NameNotFoundException e) {
                logger.error("Failed to obtain name for permission", e);
                name = "read external storage";
            }
            new AlertDialog.Builder(this)
                    .setMessage(getString(R.string.msgReadExternalStorageRationale, name))
                    .setPositiveButton(R.string.ok, (dialog, which) -> requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE))
                    .setNegativeButton(R.string.cancel, (dialog, which) -> finish())
                    .create()
                    .show();
        } else {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
        }

    } else {
        mDataImportFragment.startImport(mTask);
    }
}
 
Example 11
private boolean isRuntimePermission(PackageManager packageManager, String permission) {
    try {
        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;
}
 
Example 12
Source Project: DevUtils   File: AppInfoUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 打印 APP 注册的权限
 * @param packageName 应用包名
 */
public static void printAppPermission(final String packageName) {
    try {
        StringBuilder builder = new StringBuilder();
        // =
        PackageManager packageManager = AppUtils.getPackageManager();
        PackageInfo packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
        String[] usesPermissionsArray = packageInfo.requestedPermissions;
        for (int i = 0; i < usesPermissionsArray.length; i++) {
            // 获取每个权限的名字, 如: android.permission.INTERNET
            String usesPermissionName = usesPermissionsArray[i];
            // 拼接日志
            builder.append("usesPermissionName = " + usesPermissionName);
            builder.append(NEW_LINE_STR);

            // 通过 usesPermissionName 获取该权限的详细信息
            PermissionInfo permissionInfo = packageManager.getPermissionInfo(usesPermissionName, 0);

            // 获取该权限属于哪个权限组, 如: 网络通信
            PermissionGroupInfo permissionGroupInfo = packageManager.getPermissionGroupInfo(permissionInfo.group, 0);
            // 拼接日志
            builder.append("permissionGroup = " + permissionGroupInfo.loadLabel(packageManager).toString());
            builder.append(NEW_LINE_STR);

            // 获取该权限的标签信息, 比如: 完全的网络访问权限
            String permissionLabel = permissionInfo.loadLabel(packageManager).toString();
            // 拼接日志
            builder.append("permissionLabel = " + permissionLabel);
            builder.append(NEW_LINE_STR);

            // 获取该权限的详细描述信息, 比如: 允许该应用创建网络套接字和使用自定义网络协议
            // 浏览器和其他某些应用提供了向互联网发送数据的途径, 因此应用无需该权限即可向互联网发送数据
            String permissionDescription = permissionInfo.loadDescription(packageManager).toString();
            // 拼接日志
            builder.append("permissionDescription = " + permissionDescription);
            builder.append(NEW_LINE_STR);
        }
        // 打印日志
        LogPrintUtils.dTag(TAG, builder.toString());
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "printAppPermission");
    }
}
 
Example 13
/**
 * Converts the array of permissions to the array list of {@link DynamicPermissions}.
 *
 * @param permissions The permissions array to be converted.
 * 
 * @return The array list containing {@link DynamicPermissions}.
 */
public @NonNull List<DynamicPermission> getPermissionItemArrayList(
        @NonNull String[] permissions) {
    List<DynamicPermission> permissionsList = new ArrayList<>();
    PackageManager packageManager = mContext.getPackageManager();

    for (String permission: permissions) {
        try {
            DynamicPermission dynamicPermission;
            PermissionInfo permInfo = packageManager.getPermissionInfo(
                    permission, PackageManager.GET_META_DATA);

            if (permission.equals(Manifest.permission.WRITE_SETTINGS)
                    || permission.equals(Manifest.permission.PACKAGE_USAGE_STATS)
                    || permission.equals(Manifest.permission.SYSTEM_ALERT_WINDOW)) {
                dynamicPermission = new DynamicPermission(permission,
                        DynamicResourceUtils.getDrawable(mContext,
                        DynamicPermissionUtils.getPermissionIcon(permission)),
                        mContext.getString(DynamicPermissionUtils
                                .getPermissionTitle(permission)),
                        mContext.getString(DynamicPermissionUtils
                                .getPermissionSubtitle(permission)));

                if (permission.equals(Manifest.permission.WRITE_SETTINGS)) {
                    dynamicPermission.setAllowed(canWriteSystemSettings());
                }

                if (permission.equals(Manifest.permission.PACKAGE_USAGE_STATS)) {
                    dynamicPermission.setAllowed(hasUsageAccess());
                }

                if (permission.equals(Manifest.permission.SYSTEM_ALERT_WINDOW)) {
                    dynamicPermission.setAllowed(canDrawOverlays());
                }
            } else {
                dynamicPermission = new DynamicPermission(
                        permission, DynamicResourceUtils.getDrawable(mContext,
                        DynamicPermissionUtils.getPermissionIcon(permission)),
                        permInfo.loadLabel(packageManager).toString());

                dynamicPermission.setDangerous(true);
                dynamicPermission.setAllowed(ContextCompat.checkSelfPermission(mContext,
                        permission) == PackageManager.PERMISSION_GRANTED);
            }

            if (!permInfo.loadLabel(packageManager).equals(permission)) {
                dynamicPermission.setTitle(permInfo.loadLabel(packageManager).toString());
            }

            if (permInfo.loadDescription(packageManager) != null) {
                dynamicPermission.setSubtitle(
                        permInfo.loadDescription(packageManager).toString());
            }

            if (!permissionsList.contains(dynamicPermission)) {
                permissionsList.add(dynamicPermission);
            }
        } catch (Exception ignored) {
        }
    }

    return permissionsList;
}
 
Example 14
public static ReportDisplay buildReportDisplay(Context context, ApplicationViewModel model, PackageManager manager, PackageInfo info) {
    ReportDisplay reportDisplay = new ReportDisplay();
    reportDisplay.packageName = model.packageName;
    reportDisplay.versionName = model.versionName;
    reportDisplay.versionCode = model.versionCode;
    reportDisplay.displayName = model.label.toString();

    reportDisplay.report = model.report;

    reportDisplay.trackers = model.trackers;

    if (reportDisplay.report != null)
        reportDisplay.creator =  DatabaseManager.getInstance(context).getCreator(reportDisplay.report.appId);

    List<Permission> requestedPermissions= new ArrayList<>();
    if (info.requestedPermissions != null) {
        for(int i = 0; i < info.requestedPermissions.length; i++) {
            Permission permission = new Permission();
            permission.fullName = info.requestedPermissions[i];
            try {
                PermissionInfo permissionInfo = manager.getPermissionInfo(permission.fullName,PackageManager.GET_META_DATA);
                if(permissionInfo.loadDescription(manager) != null)
                    permission.description = permissionInfo.loadDescription(manager).toString();
                if(permissionInfo.loadLabel(manager) != null)
                    permission.name = permissionInfo.loadLabel(manager).toString();
                permission.dangerous = permissionInfo.protectionLevel == PermissionInfo.PROTECTION_DANGEROUS;
                if(permission.fullName.equals(Manifest.permission.WRITE_SETTINGS) || permission.fullName.equals(Manifest.permission.SYSTEM_ALERT_WINDOW)) //Special permissions
                    permission.dangerous = true;

                if (permissionInfo.group != null) {
                    PermissionGroupInfo permissionGroupInfo = manager.getPermissionGroupInfo(permissionInfo.group, PackageManager.GET_META_DATA);
                    if(permissionGroupInfo.loadIcon(manager) != null)
                        permission.icon = permissionGroupInfo.loadIcon(manager);
                }

            } catch (PackageManager.NameNotFoundException e) {
                e.getLocalizedMessage();
            }
            requestedPermissions.add(permission);
        }
    }
    reportDisplay.permissions = requestedPermissions;

    reportDisplay.logo = model.icon;

    return reportDisplay;
}