Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 765f7dce authored by Makoto Onuki's avatar Makoto Onuki Committed by Android (Google) Code Review
Browse files

Merge "ACTION_GET_PERMISSIONS_COUNT now returns list of granted..." into mnc-dev

parents c7745215 bf3e5768
Loading
Loading
Loading
Loading
+42 −8
Original line number Diff line number Diff line
@@ -26,27 +26,44 @@ import android.util.SparseArray;
import com.android.packageinstaller.permission.model.PermissionApps.PermissionApp;
import com.android.packageinstaller.permission.utils.Utils;

public class PermissionStatusReceiver extends BroadcastReceiver {
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;

public class PermissionStatusReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int[] result = new int[2];
        int[] counts = new int[3];
        ArrayList<CharSequence> grantedGroups = new ArrayList<>();
        boolean succeeded = false;

        boolean isForPackage = intent.hasExtra(Intent.EXTRA_PACKAGE_NAME);

        Intent responseIntent = new Intent(intent.getStringExtra(
                Intent.EXTRA_GET_PERMISSIONS_RESPONSE_INTENT));
        responseIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        if (intent.hasExtra(Intent.EXTRA_PACKAGE_NAME)) {


        if (isForPackage) {
            String pkg = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME);
            succeeded = getPermissionsCount(context, pkg, result);
            succeeded = getPermissionsCount(context, pkg, counts, grantedGroups);
        } else {
            succeeded = getAppsWithPermissionsCount(context, result);
            succeeded = getAppsWithPermissionsCount(context, counts);
        }
        if (succeeded) {
            responseIntent.putExtra(Intent.EXTRA_GET_PERMISSIONS_COUNT_RESULT, counts);

            if (isForPackage) {
                responseIntent.putExtra(Intent.EXTRA_GET_PERMISSIONS_GROUP_LIST_RESULT,
                        grantedGroups.toArray(new CharSequence[grantedGroups.size()]));
            }
        }
        responseIntent.putExtra(Intent.EXTRA_GET_PERMISSIONS_COUNT_RESULT,
                succeeded ? result : null);

        context.sendBroadcast(responseIntent);
    }

    public boolean getPermissionsCount(Context context, String pkg, int[] counts) {
    public boolean getPermissionsCount(Context context, String pkg, int[] counts,
            ArrayList<CharSequence> grantedGroups) {
        try {
            PackageInfo packageInfo =
                    context.getPackageManager().getPackageInfo(pkg, PackageManager.GET_PERMISSIONS);
@@ -54,16 +71,33 @@ public class PermissionStatusReceiver extends BroadcastReceiver {
                    new AppPermissions(context, packageInfo, null, false, null);
            int grantedCount = 0;
            int totalCount = 0;
            int additionalCount = 0;

            for (AppPermissionGroup group : appPermissions.getPermissionGroups()) {
                if (Utils.shouldShowPermission(group, false)) {
                    totalCount++;
                    if (group.areRuntimePermissionsGranted()) {
                        grantedCount++;

                        if (Utils.OS_PKG.equals(group.getDeclaringPackage())) {
                            grantedGroups.add(group.getLabel());
                        } else {
                            additionalCount++;
                        }
                    }
                }
            }

            // Sort
            Collator coll = Collator.getInstance();
            coll.setStrength(Collator.PRIMARY);
            Collections.sort(grantedGroups, coll);

            // Set results
            counts[0] = grantedCount;
            counts[1] = totalCount;
            counts[2] = additionalCount;

            return true;
        } catch (NameNotFoundException e) {
            return false;