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

Commit 2457f266 authored by Heemin Seog's avatar Heemin Seog
Browse files

All permissions settings screen in permission controller

Changes here are very close to the phone implementation, with very
little effort spent on improving the code, because permission controller
team has plans to make the business logic more shareable in R.

The only difference is the extraction of common logic into
AutoPermissionsUtils.

Remove the navigation to app details settings for now, since it messes
with the navigation flow between permission controller and car settings.
Will revisit after the remaining components are in place.

Bug: 122822231
Test: manual
Change-Id: I005670e156bde07aa057e7dc3b058504e96fd327
parent 1a028cdd
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.android.packageinstaller.DeviceUtils;
import com.android.packageinstaller.permission.ui.auto.AutoAllAppPermissionsFragment;
import com.android.packageinstaller.permission.ui.auto.AutoAppPermissionsFragment;
import com.android.packageinstaller.permission.ui.handheld.ManageStandardPermissionsFragment;
import com.android.packageinstaller.permission.ui.handheld.PermissionUsageFragment;
@@ -121,9 +122,8 @@ public final class ManagePermissionsActivity extends FragmentActivity {

                if (DeviceUtils.isAuto(this)) {
                    if (allPermissions) {
                        // TODO: Replace this with a car version.
                        androidXFragment = com.android.packageinstaller.permission.ui.handheld
                                .AllAppPermissionsFragment.newInstance(packageName, userHandle);
                        androidXFragment = AutoAllAppPermissionsFragment.newInstance(packageName,
                                userHandle);
                    } else {
                        androidXFragment = AutoAppPermissionsFragment.newInstance(packageName,
                                userHandle);
+386 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.packageinstaller.permission.ui.auto;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.content.pm.PermissionGroupInfo;
import android.content.pm.PermissionInfo;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Switch;

import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceViewHolder;
import androidx.preference.SwitchPreference;

import com.android.packageinstaller.auto.AutoSettingsFrameFragment;
import com.android.packageinstaller.permission.model.AppPermissionGroup;
import com.android.packageinstaller.permission.model.Permission;
import com.android.packageinstaller.permission.utils.ArrayUtils;
import com.android.packageinstaller.permission.utils.Utils;
import com.android.permissioncontroller.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/** Screen which shows all permissions for a particular app. */
public class AutoAllAppPermissionsFragment extends AutoSettingsFrameFragment {

    private static final String LOG_TAG = "AllAppPermsFrag";
    private static final String KEY_OTHER = "other_perms";

    private List<AppPermissionGroup> mGroups;

    /** Creates an {@link AutoAllAppPermissionsFragment} with no filter. */
    public static AutoAllAppPermissionsFragment newInstance(@NonNull String packageName,
            @NonNull UserHandle userHandle) {
        return newInstance(packageName, /* filterGroup= */ null, userHandle);
    }

    /** Creates an {@link AutoAllAppPermissionsFragment} with a specific filter group. */
    public static AutoAllAppPermissionsFragment newInstance(@NonNull String packageName,
            @NonNull String filterGroup, @NonNull UserHandle userHandle) {
        AutoAllAppPermissionsFragment instance = new AutoAllAppPermissionsFragment();
        Bundle arguments = new Bundle();
        arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName);
        arguments.putString(Intent.EXTRA_PERMISSION_GROUP_NAME, filterGroup);
        arguments.putParcelable(Intent.EXTRA_USER, userHandle);
        instance.setArguments(arguments);
        return instance;
    }

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getContext()));
    }

    @Override
    public void onStart() {
        super.onStart();

        // If we target a group make this look like app permissions.
        if (getArguments().getString(Intent.EXTRA_PERMISSION_GROUP_NAME) == null) {
            setHeaderLabel(getContext().getString(R.string.all_permissions));
        } else {
            setHeaderLabel(getContext().getString(R.string.app_permissions));
        }

        updateUi();
    }

    @Override
    public void onStop() {
        super.onStop();
        getPreferenceScreen().removeAll();
    }

    private void updateUi() {
        PreferenceGroup otherGroup = new PreferenceCategory(getContext());
        otherGroup.setKey(KEY_OTHER);
        otherGroup.setTitle(R.string.other_permissions);
        getPreferenceScreen().addPreference(otherGroup);
        ArrayList<Preference> prefs = new ArrayList<>(); // Used for sorting.
        prefs.add(otherGroup);
        String pkg = getArguments().getString(Intent.EXTRA_PACKAGE_NAME);
        String filterGroup = getArguments().getString(Intent.EXTRA_PERMISSION_GROUP_NAME);
        UserHandle userHandle = getArguments().getParcelable(Intent.EXTRA_USER);
        otherGroup.removeAll();
        PackageManager pm = getContext().getPackageManager();

        PackageInfo info = AutoPermissionsUtils.getPackageInfo(requireActivity(), pkg, userHandle);
        if (info == null) {
            return;
        }

        ApplicationInfo appInfo = info.applicationInfo;
        Preference header = AutoPermissionsUtils.createHeaderPreference(getContext(), appInfo);
        header.setOrder(0);
        getPreferenceScreen().addPreference(header);

        if (info.requestedPermissions != null) {
            for (int i = 0; i < info.requestedPermissions.length; i++) {
                PermissionInfo perm;
                try {
                    perm = pm.getPermissionInfo(info.requestedPermissions[i], /* flags= */ 0);
                } catch (PackageManager.NameNotFoundException e) {
                    Log.e(LOG_TAG,
                            "Can't get permission info for " + info.requestedPermissions[i], e);
                    continue;
                }

                if ((perm.flags & PermissionInfo.FLAG_INSTALLED) == 0
                        || (perm.flags & PermissionInfo.FLAG_REMOVED) != 0) {
                    continue;
                }

                if (appInfo.isInstantApp()
                        && (perm.protectionLevel & PermissionInfo.PROTECTION_FLAG_INSTANT)
                        == 0) {
                    continue;
                }
                if (appInfo.targetSdkVersion < Build.VERSION_CODES.M
                        && (perm.protectionLevel & PermissionInfo.PROTECTION_FLAG_RUNTIME_ONLY)
                        != 0) {
                    continue;
                }

                if ((perm.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE)
                        == PermissionInfo.PROTECTION_DANGEROUS) {
                    PackageItemInfo group = getGroup(Utils.getGroupOfPermission(perm), pm);
                    if (group == null) {
                        group = perm;
                    }
                    // If we show a targeted group, then ignore everything else.
                    if (filterGroup != null && !group.name.equals(filterGroup)) {
                        continue;
                    }
                    PreferenceGroup pref = findOrCreate(group, pm, prefs);
                    pref.addPreference(getPreference(info, perm, group, pm));
                } else if (filterGroup == null) {
                    if ((perm.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE)
                            == PermissionInfo.PROTECTION_NORMAL) {
                        PermissionGroupInfo group = getGroup(perm.group, pm);
                        otherGroup.addPreference(getPreference(info,
                                perm, group, pm));
                    }
                }

                // If we show a targeted group, then don't show 'other' permissions.
                if (filterGroup != null) {
                    getPreferenceScreen().removePreference(otherGroup);
                }
            }
        }

        // Sort an ArrayList of the groups and then set the order from the sorting.
        Collections.sort(prefs, (lhs, rhs) -> {
            String lKey = lhs.getKey();
            String rKey = rhs.getKey();
            if (lKey.equals(KEY_OTHER)) {
                return 1;
            } else if (rKey.equals(KEY_OTHER)) {
                return -1;
            } else if (Utils.isModernPermissionGroup(lKey)
                    != Utils.isModernPermissionGroup(rKey)) {
                return Utils.isModernPermissionGroup(lKey) ? -1 : 1;
            }
            return lhs.getTitle().toString().compareTo(rhs.getTitle().toString());
        });
        for (int i = 0; i < prefs.size(); i++) {
            prefs.get(i).setOrder(i + 1);
        }
    }

    private PermissionGroupInfo getGroup(String group, PackageManager pm) {
        try {
            return pm.getPermissionGroupInfo(group, /* flags= */ 0);
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
    }

    private PreferenceGroup findOrCreate(PackageItemInfo group, PackageManager pm,
            ArrayList<Preference> prefs) {
        PreferenceGroup pref = findPreference(group.name);
        if (pref == null) {
            pref = new PreferenceCategory(getPreferenceManager().getContext());
            pref.setKey(group.name);
            pref.setTitle(group.loadLabel(pm));
            prefs.add(pref);
            getPreferenceScreen().addPreference(pref);
        }
        return pref;
    }

    private Preference getPreference(PackageInfo packageInfo, PermissionInfo perm,
            PackageItemInfo group, PackageManager pm) {
        final Preference pref;
        Context context = getPreferenceManager().getContext();

        // We allow individual permission control for some permissions if review enabled
        final boolean mutable = Utils.isPermissionIndividuallyControlled(getContext(), perm.name);
        if (mutable) {
            pref = new MyMultiTargetSwitchPreference(context, perm.name,
                    getPermissionForegroundGroup(packageInfo, perm.name));
        } else {
            pref = new Preference(context);
        }

        Drawable icon;
        if (perm.icon != 0) {
            icon = perm.loadUnbadgedIcon(pm);
        } else if (group != null && group.icon != 0) {
            icon = group.loadUnbadgedIcon(pm);
        } else {
            icon = context.getDrawable(
                    com.android.permissioncontroller.R.drawable.ic_perm_device_info);
        }
        pref.setIcon(Utils.applyTint(context, icon, android.R.attr.colorControlNormal));
        pref.setTitle(
                perm.loadSafeLabel(pm, /* ellipsizeDip= */ 20000, TextUtils.SAFE_STRING_FLAG_TRIM));
        pref.setSingleLineTitle(false);
        final CharSequence desc = perm.loadDescription(pm);

        pref.setOnPreferenceClickListener((Preference preference) -> {
            new AlertDialog.Builder(getContext())
                    .setMessage(desc)
                    .setPositiveButton(android.R.string.ok, /* listener= */ null)
                    .show();
            return mutable;
        });

        return pref;
    }

    /**
     * Return the (foreground-) {@link AppPermissionGroup group} a permission belongs to.
     *
     * <p>For foreground or non background-foreground permissions this returns the group
     * {@link AppPermissionGroup} the permission is in. For background permisisons this returns
     * the group the matching foreground
     *
     * @param packageInfo Package information about the app
     * @param permission  The permission that belongs to a group
     * @return the group the permissions belongs to
     */
    private AppPermissionGroup getPermissionForegroundGroup(PackageInfo packageInfo,
            String permission) {
        AppPermissionGroup appPermissionGroup = null;
        if (mGroups != null) {
            final int groupCount = mGroups.size();
            for (int i = 0; i < groupCount; i++) {
                AppPermissionGroup currentPermissionGroup = mGroups.get(i);
                if (currentPermissionGroup.hasPermission(permission)) {
                    appPermissionGroup = currentPermissionGroup;
                    break;
                }
                if (currentPermissionGroup.getBackgroundPermissions() != null
                        && currentPermissionGroup.getBackgroundPermissions().hasPermission(
                        permission)) {
                    appPermissionGroup = currentPermissionGroup.getBackgroundPermissions();
                    break;
                }
            }
        }
        if (appPermissionGroup == null) {
            appPermissionGroup = AppPermissionGroup.create(
                    getContext(), packageInfo, permission, /* delayChanges= */ false);
            if (mGroups == null) {
                mGroups = new ArrayList<>();
            }
            mGroups.add(appPermissionGroup);
        }
        return appPermissionGroup;
    }


    private static final class MyMultiTargetSwitchPreference extends SwitchPreference {
        private View.OnClickListener mSwitchOnClickLister;

        MyMultiTargetSwitchPreference(Context context, String permission,
                AppPermissionGroup appPermissionGroup) {
            super(context);

            setChecked(appPermissionGroup.areRuntimePermissionsGranted(
                    new String[]{permission}));

            setSwitchOnClickListener(v -> {
                Switch switchView = (Switch) v;
                if (switchView.isChecked()) {
                    appPermissionGroup.grantRuntimePermissions(false,
                            new String[]{permission});
                    // We are granting a permission from a group but since this is an
                    // individual permission control other permissions in the group may
                    // be revoked, hence we need to mark them user fixed to prevent the
                    // app from requesting a non-granted permission and it being granted
                    // because another permission in the group is granted. This applies
                    // only to apps that support runtime permissions.
                    if (appPermissionGroup.doesSupportRuntimePermissions()) {
                        int grantedCount = 0;
                        String[] revokedPermissionsToFix = null;
                        final int permissionCount = appPermissionGroup.getPermissions().size();
                        for (int i = 0; i < permissionCount; i++) {
                            Permission current = appPermissionGroup.getPermissions().get(i);
                            if (!current.isGrantedIncludingAppOp()) {
                                if (!current.isUserFixed()) {
                                    revokedPermissionsToFix = ArrayUtils.appendString(
                                            revokedPermissionsToFix, current.getName());
                                }
                            } else {
                                grantedCount++;
                            }
                        }
                        if (revokedPermissionsToFix != null) {
                            // If some permissions were not granted then they should be fixed.
                            appPermissionGroup.revokeRuntimePermissions(/* fixedByTheUser= */ true,
                                    revokedPermissionsToFix);
                        } else if (appPermissionGroup.getPermissions().size() == grantedCount) {
                            // If all permissions are granted then they should not be fixed.
                            appPermissionGroup.grantRuntimePermissions(/* fixedByTheUser= */ false);
                        }
                    }
                } else {
                    appPermissionGroup.revokeRuntimePermissions(/* fixedByTheUser= */ true,
                            new String[]{permission});
                    // If we just revoked the last permission we need to clear
                    // the user fixed state as now the app should be able to
                    // request them at runtime if supported.
                    if (appPermissionGroup.doesSupportRuntimePermissions()
                            && !appPermissionGroup.areRuntimePermissionsGranted()) {
                        appPermissionGroup.revokeRuntimePermissions(/* fixedByTheUser= */ false);
                    }
                }
            });
        }

        @Override
        public void setChecked(boolean checked) {
            // If double target behavior is enabled do nothing
            if (mSwitchOnClickLister == null) {
                super.setChecked(checked);
            }
        }

        void setSwitchOnClickListener(View.OnClickListener listener) {
            mSwitchOnClickLister = listener;
        }

        @Override
        public void onBindViewHolder(PreferenceViewHolder holder) {
            super.onBindViewHolder(holder);
            Switch switchView = holder.itemView.findViewById(android.R.id.switch_widget);
            if (switchView != null) {
                switchView.setOnClickListener(mSwitchOnClickLister);
            }
        }
    }
}
+12 −68
Original line number Diff line number Diff line
@@ -19,16 +19,11 @@ package com.android.packageinstaller.permission.ui.auto;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

@@ -44,7 +39,6 @@ import com.android.packageinstaller.permission.model.AppPermissionGroup;
import com.android.packageinstaller.permission.model.AppPermissions;
import com.android.packageinstaller.permission.model.PermissionUsages;
import com.android.packageinstaller.permission.ui.AppPermissionActivity;
import com.android.packageinstaller.permission.ui.handheld.AllAppPermissionsFragment;
import com.android.packageinstaller.permission.utils.Utils;
import com.android.permissioncontroller.R;

@@ -53,11 +47,7 @@ import java.util.ArrayList;

/** Screen to show the permissions for a specific application. */
public class AutoAppPermissionsFragment extends AutoSettingsFrameFragment {
    private static final String LOG_TAG = "ManagePermsFragment";

    static final String EXTRA_HIDE_INFO_BUTTON = "hideInfoButton";
    private static final String KEY_APP_INFO_INTENT = "key_app_info_intent";
    private static final String KEY_USER_HANDLE = "key_user_handle";
    private static final String KEY_ALLOWED_PERMISSIONS_GROUP = "allowed_permissions_group";
    private static final String KEY_DENIED_PERMISSIONS_GROUP = "denied_permissions_group";

@@ -91,8 +81,9 @@ public class AutoAppPermissionsFragment extends AutoSettingsFrameFragment {

        String packageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME);
        UserHandle userHandle = getArguments().getParcelable(Intent.EXTRA_USER);
        Activity activity = getActivity();
        PackageInfo packageInfo = getPackageInfo(activity, packageName, userHandle);
        Activity activity = requireActivity();
        PackageInfo packageInfo = AutoPermissionsUtils.getPackageInfo(activity, packageName,
                userHandle);
        if (packageInfo == null) {
            Toast.makeText(getContext(), R.string.app_not_found_dlg_title,
                    Toast.LENGTH_LONG).show();
@@ -135,7 +126,7 @@ public class AutoAppPermissionsFragment extends AutoSettingsFrameFragment {
    }

    private void showAllPermissions() {
        Fragment frag = AllAppPermissionsFragment.newInstance(
        Fragment frag = AutoAllAppPermissionsFragment.newInstance(
                getArguments().getString(Intent.EXTRA_PACKAGE_NAME),
                getArguments().getParcelable(Intent.EXTRA_USER));
        getFragmentManager().beginTransaction()
@@ -145,7 +136,9 @@ public class AutoAppPermissionsFragment extends AutoSettingsFrameFragment {
    }

    protected void bindUi(PackageInfo packageInfo) {
        addAppHeaderPreference(requireActivity(), getPreferenceScreen(), packageInfo);
        getPreferenceScreen().addPreference(
                AutoPermissionsUtils.createHeaderPreference(getContext(),
                        packageInfo.applicationInfo));

        PreferenceGroup allowed = new PreferenceCategory(getContext());
        allowed.setKey(KEY_ALLOWED_PERMISSIONS_GROUP);
@@ -172,8 +165,8 @@ public class AutoAppPermissionsFragment extends AutoSettingsFrameFragment {

        if (mExtraScreen != null) {
            mExtraScreen.removeAll();
            addAppHeaderPreference(requireActivity(), mExtraScreen,
                    mAppPermissions.getPackageInfo());
            mExtraScreen.addPreference(AutoPermissionsUtils.createHeaderPreference(getContext(),
                    mAppPermissions.getPackageInfo().applicationInfo));
        }

        Preference extraPerms = new Preference(context);
@@ -203,8 +196,9 @@ public class AutoAppPermissionsFragment extends AutoSettingsFrameFragment {
            } else {
                if (mExtraScreen == null) {
                    mExtraScreen = getPreferenceManager().createPreferenceScreen(context);
                    addAppHeaderPreference(requireActivity(), mExtraScreen,
                            mAppPermissions.getPackageInfo());
                    mExtraScreen.addPreference(
                            AutoPermissionsUtils.createHeaderPreference(getContext(),
                                    mAppPermissions.getPackageInfo().applicationInfo));
                }
                mExtraScreen.addPreference(preference);
                if (group.areRuntimePermissionsGranted()) {
@@ -252,56 +246,6 @@ public class AutoAppPermissionsFragment extends AutoSettingsFrameFragment {
        setLoading(false);
    }

    private PackageInfo getPackageInfo(Activity activity, @NonNull String packageName,
            @NonNull UserHandle userHandle) {
        try {
            return activity.createPackageContextAsUser(packageName, 0,
                    userHandle).getPackageManager().getPackageInfo(packageName,
                    PackageManager.GET_PERMISSIONS);
        } catch (PackageManager.NameNotFoundException e) {
            Log.i(LOG_TAG, "No package:" + activity.getCallingPackage(), e);
            return null;
        }
    }

    private void addAppHeaderPreference(Activity activity, PreferenceScreen screen,
            PackageInfo packageInfo) {
        // Only add the app header if it is the first preference to be added.
        if (screen.getPreferenceCount() != 0) {
            Log.e(LOG_TAG, "cannot add app header, since screen is already populated");
            return;
        }

        Intent infoIntent = null;
        if (!activity.getIntent().getBooleanExtra(EXTRA_HIDE_INFO_BUTTON, false)) {
            infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                    .setData(Uri.fromParts("package", packageInfo.packageName, null));
        }

        Preference preference = createHeaderPreference(activity, infoIntent,
                packageInfo.applicationInfo);
        screen.addPreference(preference);
    }

    private Preference createHeaderPreference(Context context, Intent infoIntent,
            ApplicationInfo appInfo) {
        Drawable icon = Utils.getBadgedIcon(context, appInfo);
        Preference preference = new Preference(context);
        preference.setIcon(icon);
        preference.setKey(appInfo.packageName);
        preference.setTitle(Utils.getFullAppLabel(appInfo, context));
        preference.getExtras().putParcelable(KEY_APP_INFO_INTENT, infoIntent);
        preference.getExtras().putParcelable(KEY_USER_HANDLE,
                UserHandle.getUserHandleForUid(appInfo.uid));
        preference.setOnPreferenceClickListener(pref -> {
            Intent intent = pref.getExtras().getParcelable(KEY_APP_INFO_INTENT);
            UserHandle user = pref.getExtras().getParcelable(KEY_USER_HANDLE);
            context.startActivityAsUser(intent, user);
            return true;
        });
        return preference;
    }

    private Preference createPermissionPreference(Context context, AppPermissionGroup group) {
        Preference preference = new Preference(context);
        Drawable icon = Utils.loadDrawable(context.getPackageManager(),
+62 −0

File added.

Preview size limit exceeded, changes collapsed.