summaryrefslogtreecommitdiff
path: root/PermissionController/role-controller
diff options
context:
space:
mode:
authorJay Thomas Sullivan <jaysullivan@google.com>2023-11-03 13:56:00 -0700
committerJay Thomas Sullivan <jaysullivan@google.com>2023-11-10 15:13:40 -0800
commit2d711bc5ea158418410a91566508f13d55dd6b83 (patch)
tree773d87817a7a25c1849f23ab6b459fbb1029942f /PermissionController/role-controller
parent38ee78b60fb369c69f3f0731e5398ce669c5b176 (diff)
downloadPermission-2d711bc5ea158418410a91566508f13d55dd6b83.tar.gz
[Role Logic Move] Migrate isVisible behavior
The goal of this change is to move any remaining PermissionController-specific dependencies out of RoleControllerServiceImpl. This includes the following two methods calls: - RoleUiBehaviorUtils::isVisibleAsUser - RoleUiBehaviorUtils::isApplicationVisibleAsUser ...which in turn call RoleUiBehavior::isVisibleAsUser and RoleUiBehavior::isApplicationVisibleAsUser, which are implemented in various RoleUiBehavior implementations. Move all of this logic into the role-controller module. After this change, RoleControllerServiceImpl now calls: - Role::isVisibleAsUser - Role::isApplicationVisibleAsUser ...which in turn call RoleBehavior::isVisibleAsUser and RoleBehavior::isApplicationVisibleAsUser, which are implemented in various RoleBehavior implementations. (All of these classes are in role-controller.) Also, our RoleUiBehavior implementations fetch app resources. But, resources are app-specific, some of our behaviors need PermissionController-specific resources, and those behaviors currently assume that PermissionController is the "current" app, and therefore this will break if we move this code to SystemServer. So, change the behaviors to explicitly fetch the resource from SystemServer or PermissionController as needed (now encapsulated within VisibilityMixin), without assuming the current app. Bug: 309139048 Test: atest CtsRoleTestCases Change-Id: I949969aa59f18c26ce3e11c59c8d8f1e72f013a4
Diffstat (limited to 'PermissionController/role-controller')
-rw-r--r--PermissionController/role-controller/java/com/android/role/controller/behavior/AssistantRoleBehavior.java7
-rw-r--r--PermissionController/role-controller/java/com/android/role/controller/behavior/BrowserRoleBehavior.java7
-rw-r--r--PermissionController/role-controller/java/com/android/role/controller/behavior/DialerRoleBehavior.java7
-rw-r--r--PermissionController/role-controller/java/com/android/role/controller/behavior/EmergencyRoleBehavior.java7
-rw-r--r--PermissionController/role-controller/java/com/android/role/controller/behavior/HomeRoleBehavior.java16
-rw-r--r--PermissionController/role-controller/java/com/android/role/controller/behavior/SmsRoleBehavior.java7
-rw-r--r--PermissionController/role-controller/java/com/android/role/controller/model/Role.java34
-rw-r--r--PermissionController/role-controller/java/com/android/role/controller/model/RoleBehavior.java31
-rw-r--r--PermissionController/role-controller/java/com/android/role/controller/model/VisibilityMixin.java70
-rw-r--r--PermissionController/role-controller/java/com/android/role/controller/util/ContextUtils.java57
10 files changed, 242 insertions, 1 deletions
diff --git a/PermissionController/role-controller/java/com/android/role/controller/behavior/AssistantRoleBehavior.java b/PermissionController/role-controller/java/com/android/role/controller/behavior/AssistantRoleBehavior.java
index 7ab0b9bd2..c20734cde 100644
--- a/PermissionController/role-controller/java/com/android/role/controller/behavior/AssistantRoleBehavior.java
+++ b/PermissionController/role-controller/java/com/android/role/controller/behavior/AssistantRoleBehavior.java
@@ -38,6 +38,7 @@ import androidx.annotation.Nullable;
import com.android.role.controller.model.Role;
import com.android.role.controller.model.RoleBehavior;
+import com.android.role.controller.model.VisibilityMixin;
import com.android.role.controller.util.UserUtils;
import org.xmlpull.v1.XmlPullParserException;
@@ -193,4 +194,10 @@ public class AssistantRoleBehavior implements RoleBehavior {
return true;
}
+
+ @Override
+ public boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user,
+ @NonNull Context context) {
+ return VisibilityMixin.isVisible("config_showDefaultAssistant", false, user, context);
+ }
}
diff --git a/PermissionController/role-controller/java/com/android/role/controller/behavior/BrowserRoleBehavior.java b/PermissionController/role-controller/java/com/android/role/controller/behavior/BrowserRoleBehavior.java
index f64c3bcf0..0261e1eee 100644
--- a/PermissionController/role-controller/java/com/android/role/controller/behavior/BrowserRoleBehavior.java
+++ b/PermissionController/role-controller/java/com/android/role/controller/behavior/BrowserRoleBehavior.java
@@ -31,6 +31,7 @@ import com.android.modules.utils.build.SdkLevel;
import com.android.role.controller.model.Permissions;
import com.android.role.controller.model.Role;
import com.android.role.controller.model.RoleBehavior;
+import com.android.role.controller.model.VisibilityMixin;
import com.android.role.controller.util.CollectionUtils;
import com.android.role.controller.util.PackageUtils;
import com.android.role.controller.util.UserUtils;
@@ -154,4 +155,10 @@ public class BrowserRoleBehavior implements RoleBehavior {
}
}
}
+
+ @Override
+ public boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user,
+ @NonNull Context context) {
+ return VisibilityMixin.isVisible("config_showBrowserRole", true, user, context);
+ }
}
diff --git a/PermissionController/role-controller/java/com/android/role/controller/behavior/DialerRoleBehavior.java b/PermissionController/role-controller/java/com/android/role/controller/behavior/DialerRoleBehavior.java
index d0ee2d5bf..57b5412dc 100644
--- a/PermissionController/role-controller/java/com/android/role/controller/behavior/DialerRoleBehavior.java
+++ b/PermissionController/role-controller/java/com/android/role/controller/behavior/DialerRoleBehavior.java
@@ -26,6 +26,7 @@ import com.android.modules.utils.build.SdkLevel;
import com.android.role.controller.model.Permissions;
import com.android.role.controller.model.Role;
import com.android.role.controller.model.RoleBehavior;
+import com.android.role.controller.model.VisibilityMixin;
import com.android.role.controller.util.PackageUtils;
import java.util.Arrays;
@@ -73,4 +74,10 @@ public class DialerRoleBehavior implements RoleBehavior {
user, context);
}
}
+
+ @Override
+ public boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user,
+ @NonNull Context context) {
+ return VisibilityMixin.isVisible("config_showDialerRole", true, user, context);
+ }
}
diff --git a/PermissionController/role-controller/java/com/android/role/controller/behavior/EmergencyRoleBehavior.java b/PermissionController/role-controller/java/com/android/role/controller/behavior/EmergencyRoleBehavior.java
index 5c23a996e..f19c86596 100644
--- a/PermissionController/role-controller/java/com/android/role/controller/behavior/EmergencyRoleBehavior.java
+++ b/PermissionController/role-controller/java/com/android/role/controller/behavior/EmergencyRoleBehavior.java
@@ -26,6 +26,7 @@ import androidx.annotation.Nullable;
import com.android.role.controller.model.Role;
import com.android.role.controller.model.RoleBehavior;
+import com.android.role.controller.model.VisibilityMixin;
import com.android.role.controller.util.PackageUtils;
import java.util.List;
@@ -68,4 +69,10 @@ public class EmergencyRoleBehavior implements RoleBehavior {
}
return fallbackPackageInfo != null ? fallbackPackageInfo.packageName : null;
}
+
+ @Override
+ public boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user,
+ @NonNull Context context) {
+ return VisibilityMixin.isVisible("config_showDefaultEmergency", false, user, context);
+ }
}
diff --git a/PermissionController/role-controller/java/com/android/role/controller/behavior/HomeRoleBehavior.java b/PermissionController/role-controller/java/com/android/role/controller/behavior/HomeRoleBehavior.java
index 5bdd5f682..4bf5a6294 100644
--- a/PermissionController/role-controller/java/com/android/role/controller/behavior/HomeRoleBehavior.java
+++ b/PermissionController/role-controller/java/com/android/role/controller/behavior/HomeRoleBehavior.java
@@ -35,6 +35,7 @@ import com.android.role.controller.model.AppOpPermissions;
import com.android.role.controller.model.Permissions;
import com.android.role.controller.model.Role;
import com.android.role.controller.model.RoleBehavior;
+import com.android.role.controller.model.VisibilityMixin;
import com.android.role.controller.util.UserUtils;
import java.util.Arrays;
@@ -113,7 +114,7 @@ public class HomeRoleBehavior implements RoleBehavior {
/**
* Check if the application is a settings application
*/
- public static boolean isSettingsApplicationAsUser(@NonNull ApplicationInfo applicationInfo,
+ private static boolean isSettingsApplicationAsUser(@NonNull ApplicationInfo applicationInfo,
@NonNull UserHandle user, @NonNull Context context) {
Context userContext = UserUtils.getUserContext(context, user);
PackageManager userPackageManager = userContext.getPackageManager();
@@ -213,4 +214,17 @@ public class HomeRoleBehavior implements RoleBehavior {
final int flags = permissionInfo.getProtectionFlags();
return (flags & PermissionInfo.PROTECTION_FLAG_ROLE) == PermissionInfo.PROTECTION_FLAG_ROLE;
}
+
+ @Override
+ public boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user,
+ @NonNull Context context) {
+ return VisibilityMixin.isVisible("config_showDefaultHome", false, user, context);
+ }
+
+ @Override
+ public boolean isApplicationVisibleAsUser(@NonNull Role role,
+ @NonNull ApplicationInfo applicationInfo, @NonNull UserHandle user,
+ @NonNull Context context) {
+ return !isSettingsApplicationAsUser(applicationInfo, user, context);
+ }
}
diff --git a/PermissionController/role-controller/java/com/android/role/controller/behavior/SmsRoleBehavior.java b/PermissionController/role-controller/java/com/android/role/controller/behavior/SmsRoleBehavior.java
index 6e5c5c920..b614594c5 100644
--- a/PermissionController/role-controller/java/com/android/role/controller/behavior/SmsRoleBehavior.java
+++ b/PermissionController/role-controller/java/com/android/role/controller/behavior/SmsRoleBehavior.java
@@ -30,6 +30,7 @@ import com.android.modules.utils.build.SdkLevel;
import com.android.role.controller.model.Permissions;
import com.android.role.controller.model.Role;
import com.android.role.controller.model.RoleBehavior;
+import com.android.role.controller.model.VisibilityMixin;
import com.android.role.controller.util.CollectionUtils;
import com.android.role.controller.util.PackageUtils;
import com.android.role.controller.util.UserUtils;
@@ -129,4 +130,10 @@ public class SmsRoleBehavior implements RoleBehavior {
user, context);
}
}
+
+ @Override
+ public boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user,
+ @NonNull Context context) {
+ return VisibilityMixin.isVisible("config_showSmsRole", true, user, context);
+ }
}
diff --git a/PermissionController/role-controller/java/com/android/role/controller/model/Role.java b/PermissionController/role-controller/java/com/android/role/controller/model/Role.java
index e80ea1194..6c7eb7d9e 100644
--- a/PermissionController/role-controller/java/com/android/role/controller/model/Role.java
+++ b/PermissionController/role-controller/java/com/android/role/controller/model/Role.java
@@ -949,6 +949,40 @@ public class Role {
RoleManagerCompat.setRoleFallbackEnabledAsUser(this, false, user, context);
}
+ /**
+ * Check whether this role should be visible to user.
+ *
+ * @param user the user to check for
+ * @param context the `Context` to retrieve system services
+ *
+ * @return whether this role should be visible to user
+ */
+ public boolean isVisibleAsUser(@NonNull UserHandle user, @NonNull Context context) {
+ RoleBehavior behavior = getBehavior();
+ if (behavior == null) {
+ return isVisible();
+ }
+ return isVisible() && behavior.isVisibleAsUser(this, user, context);
+ }
+
+ /**
+ * Check whether a qualifying application should be visible to user.
+ *
+ * @param applicationInfo the {@link ApplicationInfo} for the application
+ * @param user the user for the application
+ * @param context the {@code Context} to retrieve system services
+ *
+ * @return whether the qualifying application should be visible to user
+ */
+ public boolean isApplicationVisibleAsUser(@NonNull ApplicationInfo applicationInfo,
+ @NonNull UserHandle user, @NonNull Context context) {
+ RoleBehavior behavior = getBehavior();
+ if (behavior == null) {
+ return true;
+ }
+ return behavior.isApplicationVisibleAsUser(this, applicationInfo, user, context);
+ }
+
@Override
public String toString() {
return "Role{"
diff --git a/PermissionController/role-controller/java/com/android/role/controller/model/RoleBehavior.java b/PermissionController/role-controller/java/com/android/role/controller/model/RoleBehavior.java
index 34d2282a7..4bc1873d5 100644
--- a/PermissionController/role-controller/java/com/android/role/controller/model/RoleBehavior.java
+++ b/PermissionController/role-controller/java/com/android/role/controller/model/RoleBehavior.java
@@ -17,6 +17,7 @@
package com.android.role.controller.model;
import android.content.Context;
+import android.content.pm.ApplicationInfo;
import android.os.UserHandle;
import androidx.annotation.NonNull;
@@ -112,4 +113,34 @@ public interface RoleBehavior {
*/
default void onHolderChangedAsUser(@NonNull Role role, @NonNull UserHandle user,
@NonNull Context context) {}
+
+ /**
+ * Check whether this role should be visible to user.
+ *
+ * @param role the role to check for
+ * @param user the user to check for
+ * @param context the `Context` to retrieve system services
+ *
+ * @return whether this role should be visible to user
+ */
+ default boolean isVisibleAsUser(@NonNull Role role, @NonNull UserHandle user,
+ @NonNull Context context) {
+ return true;
+ }
+
+ /**
+ * Check whether a qualifying application should be visible to user.
+ *
+ * @param role the role to check for
+ * @param applicationInfo the {@link ApplicationInfo} for the application
+ * @param user the user for the application
+ * @param context the {@code Context} to retrieve system services
+ *
+ * @return whether the qualifying application should be visible to user
+ */
+ default boolean isApplicationVisibleAsUser(@NonNull Role role,
+ @NonNull ApplicationInfo applicationInfo, @NonNull UserHandle user,
+ @NonNull Context context) {
+ return true;
+ }
}
diff --git a/PermissionController/role-controller/java/com/android/role/controller/model/VisibilityMixin.java b/PermissionController/role-controller/java/com/android/role/controller/model/VisibilityMixin.java
new file mode 100644
index 000000000..d4f56fe37
--- /dev/null
+++ b/PermissionController/role-controller/java/com/android/role/controller/model/VisibilityMixin.java
@@ -0,0 +1,70 @@
+/*
+ * 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.role.controller.model;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.os.UserHandle;
+import android.util.Log;
+
+import androidx.annotation.NonNull;
+
+import com.android.role.controller.util.ContextUtils;
+
+/**
+ * Mixin for {@link RoleBehavior#isVisibleAsUser(Role, UserHandle, Context)} that returns whether
+ * the role should be visible from a corresponding boolean resource.
+ */
+public class VisibilityMixin {
+
+ private static final String LOG_TAG = VisibilityMixin.class.getSimpleName();
+
+ private VisibilityMixin() {}
+
+ /**
+ * Get the boolean resource value that represents whether a role is visible to the user.
+ *
+ * @param resourceName the name of the resource
+ * @param isPermissionControllerResource if {@code true}, and if the current SDK level is at
+ * least V, get the resource from a PermissionController context for the given user.
+ * Otherwise, get the resource the provided context.
+ * @param user the user to get the PermissionController context for
+ * @param context the `Context` to retrieve the resource (and system services)
+ *
+ * @return whether this role should be visible to user
+ */
+ public static boolean isVisible(@NonNull String resourceName,
+ boolean isPermissionControllerResource, @NonNull UserHandle user,
+ @NonNull Context context) {
+ Context packageContext = isPermissionControllerResource
+ ? ContextUtils.getPermissionControllerContext(user, context) : context;
+ Resources resources = packageContext.getResources();
+ String packageName = isPermissionControllerResource ? packageContext.getPackageName() :
+ "android";
+ int resourceId = resources.getIdentifier(resourceName, "bool", packageName);
+ if (resourceId == 0) {
+ Log.w(LOG_TAG, "Cannot find resource for visibility: " + resourceName);
+ return true;
+ }
+ try {
+ return resources.getBoolean(resourceId);
+ } catch (Resources.NotFoundException e) {
+ Log.w(LOG_TAG, "Cannot get resource for visibility: " + resourceName, e);
+ return true;
+ }
+ }
+}
diff --git a/PermissionController/role-controller/java/com/android/role/controller/util/ContextUtils.java b/PermissionController/role-controller/java/com/android/role/controller/util/ContextUtils.java
new file mode 100644
index 000000000..73ac266dc
--- /dev/null
+++ b/PermissionController/role-controller/java/com/android/role/controller/util/ContextUtils.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2023 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.role.controller.util;
+
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.os.UserHandle;
+
+import androidx.annotation.NonNull;
+
+import com.android.modules.utils.build.SdkLevel;
+
+/**
+ * Utility methods for dealing with {@link Context}s.
+ */
+public final class ContextUtils {
+
+ private ContextUtils() {}
+
+ /**
+ * Create a Context for the PermissionController app for the given user.
+ *
+ * @param user the user of the application
+ * @param context the context to clone
+ *
+ * @return The PermissionController context for the given user
+ */
+ @NonNull
+ public static Context getPermissionControllerContext(@NonNull UserHandle user,
+ @NonNull Context context) {
+ if (!SdkLevel.isAtLeastV()) {
+ // We don't have the getPermissionControllerPackageName() API below V,
+ // but role controller always runs in PermissionController and in its own user below V.
+ return context;
+ }
+ String packageName = context.getPackageManager().getPermissionControllerPackageName();
+ try {
+ return context.createPackageContextAsUser(packageName, 0, user);
+ } catch (PackageManager.NameNotFoundException e) {
+ throw new RuntimeException("Cannot create PermissionController context", e);
+ }
+ }
+}