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

Commit 553796e6 authored by Hai Zhang's avatar Hai Zhang
Browse files

Make system gallery role fall back to its default.

In work profile, the system gallery might be disabled upon profile
creation and installed later. Since it is still the system gallery, it
should still get the system gallery role, so we can just make the
system gallery role fall back to its default holder.

There are already several roles in need of this, and to avoid creating
a specialized system gallery role behavior just for this, a
fallBackToDefaultHolder attribute is added to role. The duplicated
logic in existing behaviors is also simplified to use the new
attribute.

Fixes: 157691735
Test: Set up a work profile with TestDPC on a build without this fix,
      run adb shell cmd package install-existing --user 10 <GALLERY>
      to enable the system gallery for work profile, and confirm that
      gallery cannot delete photos without asking for permission.
Test: Set up a work profile with TestDPC on a build with this fix, run
      adb shell cmd package install-existing --user 10 <GALLERY> to
      enable the system gallery for work profile, and confirm that
      gallery can delete photos.
Change-Id: I30e937bcd005ff0d6c27b225e535dad358a0af10
parent 981b8d6a
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -79,6 +79,7 @@
        defaultHolders="config_defaultAssistant"
        description="@string/role_assistant_description"
        exclusive="true"
        fallBackToDefaultHolder="true"
        showNone="true"
        label="@string/role_assistant_label"
        requestable="false"
@@ -167,6 +168,7 @@
        defaultHolders="config_defaultDialer"
        description="@string/role_dialer_description"
        exclusive="true"
        fallBackToDefaultHolder="true"
        label="@string/role_dialer_label"
        requestDescription="@string/role_dialer_request_description"
        requestTitle="@string/role_dialer_request_title"
@@ -426,6 +428,7 @@
        name="android.app.role.SYSTEM_GALLERY"
        defaultHolders="config_systemGallery"
        exclusive="true"
        fallBackToDefaultHolder="true"
        systemOnly="true"
        visible="false">
        <permissions>
+0 −7
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.permissioncontroller.R;
import com.android.permissioncontroller.permission.utils.CollectionUtils;
import com.android.permissioncontroller.role.utils.UserUtils;

import org.xmlpull.v1.XmlPullParserException;
@@ -79,12 +78,6 @@ public class AssistantRoleBehavior implements RoleBehavior {
        return !UserUtils.isWorkProfile(user, context);
    }

    @Nullable
    @Override
    public String getFallbackHolder(@NonNull Role role, @NonNull Context context) {
        return CollectionUtils.firstOrNull(role.getDefaultHolders(context));
    }

    @Override
    public boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user,
            @NonNull Context context) {
+0 −7
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ import androidx.annotation.Nullable;
import androidx.preference.Preference;

import com.android.permissioncontroller.R;
import com.android.permissioncontroller.permission.utils.CollectionUtils;

import java.util.Objects;

@@ -68,12 +67,6 @@ public class DialerRoleBehavior implements RoleBehavior {
                context);
    }

    @Nullable
    @Override
    public String getFallbackHolder(@NonNull Role role, @NonNull Context context) {
        return CollectionUtils.firstOrNull(role.getDefaultHolders(context));
    }

    @Override
    public boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user,
            @NonNull Context context) {
+16 −2
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import androidx.annotation.StringRes;
import androidx.preference.Preference;

import com.android.permissioncontroller.Constants;
import com.android.permissioncontroller.permission.utils.CollectionUtils;
import com.android.permissioncontroller.permission.utils.Utils;
import com.android.permissioncontroller.role.ui.TwoTargetPreference;
import com.android.permissioncontroller.role.utils.PackageUtils;
@@ -98,6 +99,11 @@ public class Role {
     */
    private final boolean mExclusive;

    /**
     * Whether this role should fall back to the default holder.
     */
    private final boolean mFallBackToDefaultHolder;

    /**
     * The string resource for the label of this role.
     */
@@ -186,7 +192,7 @@ public class Role {

    public Role(@NonNull String name, @Nullable RoleBehavior behavior,
            @Nullable String defaultHoldersResourceName, @StringRes int descriptionResource,
            boolean exclusive, @StringRes int labelResource,
            boolean exclusive, boolean fallBackToDefaultHolder, @StringRes int labelResource,
            @StringRes int requestDescriptionResource, @StringRes int requestTitleResource,
            boolean requestable, @StringRes int searchKeywordsResource,
            @StringRes int shortLabelResource, boolean showNone, boolean systemOnly,
@@ -198,6 +204,7 @@ public class Role {
        mDefaultHoldersResourceName = defaultHoldersResourceName;
        mDescriptionResource = descriptionResource;
        mExclusive = exclusive;
        mFallBackToDefaultHolder = fallBackToDefaultHolder;
        mLabelResource = labelResource;
        mRequestDescriptionResource = requestDescriptionResource;
        mRequestTitleResource = requestTitleResource;
@@ -416,7 +423,13 @@ public class Role {
     */
    @Nullable
    public String getFallbackHolder(@NonNull Context context) {
        if (mBehavior != null && !isNoneHolderSelected(context)) {
        if (isNoneHolderSelected(context)) {
            return null;
        }
        if (mFallBackToDefaultHolder) {
            return CollectionUtils.firstOrNull(getDefaultHolders(context));
        }
        if (mBehavior != null) {
            return mBehavior.getFallbackHolder(this, context);
        }
        return null;
@@ -860,6 +873,7 @@ public class Role {
                + ", mDefaultHoldersResourceName=" + mDefaultHoldersResourceName
                + ", mDescriptionResource=" + mDescriptionResource
                + ", mExclusive=" + mExclusive
                + ", mFallBackToDefaultHolder=" + mFallBackToDefaultHolder
                + ", mLabelResource=" + mLabelResource
                + ", mRequestDescriptionResource=" + mRequestDescriptionResource
                + ", mRequestTitleResource=" + mRequestTitleResource
+8 −3
Original line number Diff line number Diff line
@@ -75,6 +75,7 @@ public class RoleParser {
    private static final String ATTRIBUTE_DEFAULT_HOLDERS = "defaultHolders";
    private static final String ATTRIBUTE_DESCRIPTION = "description";
    private static final String ATTRIBUTE_EXCLUSIVE = "exclusive";
    private static final String ATTRIBUTE_FALL_BACK_TO_DEFAULT_HOLDER = "fallBackToDefaultHolder";
    private static final String ATTRIBUTE_LABEL = "label";
    private static final String ATTRIBUTE_REQUEST_TITLE = "requestTitle";
    private static final String ATTRIBUTE_REQUEST_DESCRIPTION = "requestDescription";
@@ -323,6 +324,9 @@ public class RoleParser {
            return null;
        }

        boolean fallBackToDefaultHolder = getAttributeBooleanValue(parser,
                ATTRIBUTE_FALL_BACK_TO_DEFAULT_HOLDER, false);

        boolean requestable = getAttributeBooleanValue(parser, ATTRIBUTE_REQUESTABLE, visible);
        Integer requestDescriptionResource;
        Integer requestTitleResource;
@@ -436,9 +440,10 @@ public class RoleParser {
            preferredActivities = Collections.emptyList();
        }
        return new Role(name, behavior, defaultHoldersResourceName, descriptionResource, exclusive,
                labelResource, requestDescriptionResource, requestTitleResource, requestable,
                searchKeywordsResource, shortLabelResource, showNone, systemOnly, visible,
                requiredComponents, permissions, appOpPermissions, appOps, preferredActivities);
                fallBackToDefaultHolder, labelResource, requestDescriptionResource,
                requestTitleResource, requestable, searchKeywordsResource, shortLabelResource,
                showNone, systemOnly, visible, requiredComponents, permissions, appOpPermissions,
                appOps, preferredActivities);
    }

    @NonNull