diff options
author | Joe Castro <joecastro@google.com> | 2022-12-08 01:04:41 +0000 |
---|---|---|
committer | Joe Castro <joecastro@google.com> | 2023-01-11 01:28:27 +0000 |
commit | 8b8cfb8b9113b80d239e6de6bfe235a03e57c602 (patch) | |
tree | 2454286badb6d71df72cd718a09cf0c608af1d2c /PermissionController/src/com/android/permissioncontroller/role | |
parent | 2c13bedef00978d3e691d902e0762fa53dfd7567 (diff) | |
download | Permission-8b8cfb8b9113b80d239e6de6bfe235a03e57c602.tar.gz |
Isolating Role logic into a static lib for PermissionController
Bug: 249850342
Test: atest
Change-Id: I432cc695f35b147c5c7658bade8f936be9f49a4c
Diffstat (limited to 'PermissionController/src/com/android/permissioncontroller/role')
10 files changed, 200 insertions, 179 deletions
diff --git a/PermissionController/src/com/android/permissioncontroller/role/model/RoleParserInitializer.java b/PermissionController/src/com/android/permissioncontroller/role/model/RoleParserInitializer.java new file mode 100644 index 000000000..b1c9e3e1d --- /dev/null +++ b/PermissionController/src/com/android/permissioncontroller/role/model/RoleParserInitializer.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2022 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.permissioncontroller.role.model; + +import com.android.permissioncontroller.R; +import com.android.role.controller.model.RoleParser; + +/** + * Initialize the function to retrieve the roles.xml resource from a context within + * PermissionController APK + */ +public class RoleParserInitializer { + + /** + * Initialize the function to retrieve the roles.xml resource from a context within + * PermissionController APK + */ + public static void initialize() { + RoleParser.sGetRolesXml = context -> context.getResources().getXml(R.xml.roles); + } +} diff --git a/PermissionController/src/com/android/permissioncontroller/role/model/UserDeniedManager.java b/PermissionController/src/com/android/permissioncontroller/role/model/UserDeniedManager.java new file mode 100644 index 000000000..afe35f670 --- /dev/null +++ b/PermissionController/src/com/android/permissioncontroller/role/model/UserDeniedManager.java @@ -0,0 +1,160 @@ +/* + * 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.permissioncontroller.role.model; + +import android.content.Context; +import android.content.SharedPreferences; +import android.util.ArraySet; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.android.permissioncontroller.Constants; + +import java.util.Collections; +import java.util.Set; + +/** + * Manages user denied status for requesting roles. + */ +public class UserDeniedManager { + + @Nullable + private static UserDeniedManager sInstance; + + private final SharedPreferences mPreferences; + + /** + * Get a singleton instance of this class + * + * @param context the context for retrieving shared preferences. + * + * @return the singleton instance of this class + */ + @NonNull + public static UserDeniedManager getInstance(@NonNull Context context) { + if (sInstance == null) { + sInstance = new UserDeniedManager(context); + } + return sInstance; + } + + private UserDeniedManager(@NonNull Context context) { + context = context.getApplicationContext(); + mPreferences = context.getSharedPreferences(Constants.REQUEST_ROLE_USER_DENIED_FILE, + Context.MODE_PRIVATE); + } + + /** + * Check whether an application has been denied for a role once. + * + * @param roleName the name of the role + * @param packageName the package name of the application + * + * @return whether the application has been denied for the role once + */ + public boolean isDeniedOnce(@NonNull String roleName, @NonNull String packageName) { + return isDenied(roleName, packageName, false); + } + + /** + * Remember that an application has been denied for a role once. + * + * @param roleName the name of the role + * @param packageName the package name of the application + */ + public void setDeniedOnce(@NonNull String roleName, @NonNull String packageName) { + setDenied(roleName, packageName, false, true); + } + + /** + * Check whether an application is always denied for a role. + * + * @param roleName the name of the role + * @param packageName the package name of the application + * + * @return whether the application is always denied for the role + */ + public boolean isDeniedAlways(@NonNull String roleName, @NonNull String packageName) { + return isDenied(roleName, packageName, true); + } + + /** + * Remember that an application is always denied for a role. + * + * @param roleName the name of the role + * @param packageName the package name of the application + */ + public void setDeniedAlways(@NonNull String roleName, @NonNull String packageName) { + setDenied(roleName, packageName, true, true); + } + + /** + * Forget about whether an application is denied for a role, once or always. + * + * @param roleName the name of the role + * @param packageName the package name of the application + */ + public void clearDenied(@NonNull String roleName, @NonNull String packageName) { + setDenied(roleName, packageName, false, false); + setDenied(roleName, packageName, true, false); + } + + /** + * Forget about whether an application is denied for any of the roles, once or always. + * + * @param packageName the package name of the application + */ + public void clearPackageDenied(@NonNull String packageName) { + mPreferences.edit() + .remove(getKey(packageName, false)) + .remove(getKey(packageName, true)) + .apply(); + } + + @NonNull + private static String getKey(@NonNull String packageName, boolean always) { + return (always ? Constants.REQUEST_ROLE_USER_DENIED_ALWAYS_KEY_PREFIX + : Constants.REQUEST_ROLE_USER_DENIED_ONCE_KEY_PREFIX) + packageName; + } + + private boolean isDenied(@NonNull String roleName, @NonNull String packageName, + boolean always) { + String key = getKey(packageName, always); + return mPreferences.getStringSet(key, Collections.emptySet()).contains(roleName); + } + + private void setDenied(@NonNull String roleName, @NonNull String packageName, boolean always, + boolean denied) { + String key = getKey(packageName, always); + Set<String> roleNames = mPreferences.getStringSet(key, Collections.emptySet()); + if (roleNames.contains(roleName) == denied) { + return; + } + roleNames = new ArraySet<>(roleNames); + if (denied) { + roleNames.add(roleName); + } else { + roleNames.remove(roleName); + } + if (roleName.isEmpty()) { + mPreferences.edit().remove(key).apply(); + } else { + mPreferences.edit().putStringSet(key, roleNames).apply(); + } + } +} diff --git a/PermissionController/src/com/android/permissioncontroller/role/service/ClearUserDeniedReceiver.java b/PermissionController/src/com/android/permissioncontroller/role/service/ClearUserDeniedReceiver.java index ae54154c6..862cbba2d 100644 --- a/PermissionController/src/com/android/permissioncontroller/role/service/ClearUserDeniedReceiver.java +++ b/PermissionController/src/com/android/permissioncontroller/role/service/ClearUserDeniedReceiver.java @@ -22,7 +22,7 @@ import android.content.Intent; import androidx.annotation.NonNull; -import com.android.role.controller.model.UserDeniedManager; +import com.android.permissioncontroller.role.model.UserDeniedManager; import java.util.Objects; diff --git a/PermissionController/src/com/android/permissioncontroller/role/service/RoleSearchIndexablesProvider.java b/PermissionController/src/com/android/permissioncontroller/role/service/RoleSearchIndexablesProvider.java index db909a47e..1c845ee3a 100644 --- a/PermissionController/src/com/android/permissioncontroller/role/service/RoleSearchIndexablesProvider.java +++ b/PermissionController/src/com/android/permissioncontroller/role/service/RoleSearchIndexablesProvider.java @@ -27,9 +27,9 @@ import androidx.annotation.Nullable; import com.android.permissioncontroller.R; import com.android.permissioncontroller.permission.service.BaseSearchIndexablesProvider; +import com.android.permissioncontroller.role.model.RoleParserInitializer; import com.android.permissioncontroller.role.utils.RoleUiBehaviorUtils; import com.android.role.controller.model.Role; -import com.android.role.controller.model.RoleParserInitializer; import com.android.role.controller.model.Roles; /** diff --git a/PermissionController/src/com/android/permissioncontroller/role/ui/RequestRoleActivity.java b/PermissionController/src/com/android/permissioncontroller/role/ui/RequestRoleActivity.java index 5167cfba8..3a4312c00 100644 --- a/PermissionController/src/com/android/permissioncontroller/role/ui/RequestRoleActivity.java +++ b/PermissionController/src/com/android/permissioncontroller/role/ui/RequestRoleActivity.java @@ -34,11 +34,11 @@ import androidx.fragment.app.FragmentActivity; import com.android.permissioncontroller.PermissionControllerStatsLog; import com.android.permissioncontroller.permission.utils.CollectionUtils; +import com.android.permissioncontroller.role.model.UserDeniedManager; import com.android.permissioncontroller.role.utils.PackageUtils; import com.android.permissioncontroller.role.utils.RoleUiBehaviorUtils; import com.android.role.controller.model.Role; import com.android.role.controller.model.Roles; -import com.android.role.controller.model.UserDeniedManager; import java.util.List; import java.util.Objects; diff --git a/PermissionController/src/com/android/permissioncontroller/role/ui/RequestRoleFragment.java b/PermissionController/src/com/android/permissioncontroller/role/ui/RequestRoleFragment.java index 686703a7f..a5bd90dc2 100644 --- a/PermissionController/src/com/android/permissioncontroller/role/ui/RequestRoleFragment.java +++ b/PermissionController/src/com/android/permissioncontroller/role/ui/RequestRoleFragment.java @@ -52,10 +52,10 @@ import com.android.permissioncontroller.PermissionControllerStatsLog; import com.android.permissioncontroller.R; import com.android.permissioncontroller.permission.utils.PackageRemovalMonitor; import com.android.permissioncontroller.permission.utils.Utils; +import com.android.permissioncontroller.role.model.UserDeniedManager; import com.android.permissioncontroller.role.utils.PackageUtils; import com.android.role.controller.model.Role; import com.android.role.controller.model.Roles; -import com.android.role.controller.model.UserDeniedManager; import java.util.ArrayList; import java.util.List; diff --git a/PermissionController/src/com/android/permissioncontroller/role/ui/behavior/HomeRoleUiBehavior.java b/PermissionController/src/com/android/permissioncontroller/role/ui/behavior/HomeRoleUiBehavior.java index ac8538b9e..72c241b30 100644 --- a/PermissionController/src/com/android/permissioncontroller/role/ui/behavior/HomeRoleUiBehavior.java +++ b/PermissionController/src/com/android/permissioncontroller/role/ui/behavior/HomeRoleUiBehavior.java @@ -37,7 +37,7 @@ import com.android.permissioncontroller.permission.utils.Utils; import com.android.permissioncontroller.role.model.VisibilityMixin; import com.android.permissioncontroller.role.ui.TwoTargetPreference; import com.android.permissioncontroller.role.utils.UserUtils; -import com.android.role.controller.model.HomeRoleBehavior; +import com.android.role.controller.behavior.HomeRoleBehavior; import com.android.role.controller.model.Role; /*** diff --git a/PermissionController/src/com/android/permissioncontroller/role/utils/NotificationUtils.java b/PermissionController/src/com/android/permissioncontroller/role/utils/NotificationUtils.java deleted file mode 100644 index 08607594c..000000000 --- a/PermissionController/src/com/android/permissioncontroller/role/utils/NotificationUtils.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2021 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.permissioncontroller.role.utils; - -import android.app.NotificationManager; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; -import android.content.pm.ServiceInfo; -import android.service.notification.NotificationListenerService; -import android.util.Log; - -import androidx.annotation.NonNull; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -/** - * Utility methods about Notification permissions. - */ -public final class NotificationUtils { - - public static final String LOG_TAG = NotificationUtils.class.getSimpleName(); - - private NotificationUtils() {} - - /** - * Grants the NotificationListener access. - * - * @param context the {@code Context} to retrieve system services - * @param packageName the package name implements the NotificationListener - */ - public static void grantNotificationAccessForPackage(@NonNull Context context, - @NonNull String packageName) { - setNotificationGrantStateForPackage(context, packageName, true); - } - - /** - * Revokes the NotificationListener access. - * - * @param context the {@code Context} to retrieve system services - * @param packageName the package name implements the NotificationListener - */ - public static void revokeNotificationAccessForPackage(@NonNull Context context, - @NonNull String packageName) { - setNotificationGrantStateForPackage(context, packageName, false); - } - - - private static void setNotificationGrantStateForPackage(@NonNull Context context, - @NonNull String packageName, boolean granted) { - List<ComponentName> notificationListenersForPackage = - getNotificationListenersForPackage(packageName, context); - NotificationManager notificationManager = - context.getSystemService(NotificationManager.class); - for (ComponentName componentName : notificationListenersForPackage) { - notificationManager.setNotificationListenerAccessGranted( - componentName, granted, false); - } - } - - private static List<ComponentName> getNotificationListenersForPackage( - @NonNull String packageName, @NonNull Context context) { - List<ResolveInfo> allListeners = context.getPackageManager().queryIntentServices( - new Intent(NotificationListenerService.SERVICE_INTERFACE).setPackage(packageName), - PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); - ArrayList<ComponentName> pkgListeners = new ArrayList<>(); - for (ResolveInfo service : allListeners) { - ServiceInfo serviceInfo = service.serviceInfo; - if (Objects.equals(serviceInfo.permission, - android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE) - && packageName.equals(serviceInfo.packageName)) { - pkgListeners.add(new ComponentName(serviceInfo.packageName, serviceInfo.name)); - } - } - Log.d(LOG_TAG, "getNotificationListenersForPackage(" + packageName + "): " + pkgListeners); - return pkgListeners; - } - -} diff --git a/PermissionController/src/com/android/permissioncontroller/role/utils/PackageUtils.java b/PermissionController/src/com/android/permissioncontroller/role/utils/PackageUtils.java index 67d9ece44..9aaa07ee0 100644 --- a/PermissionController/src/com/android/permissioncontroller/role/utils/PackageUtils.java +++ b/PermissionController/src/com/android/permissioncontroller/role/utils/PackageUtils.java @@ -18,7 +18,6 @@ package com.android.permissioncontroller.role.utils; import android.content.Context; import android.content.pm.ApplicationInfo; -import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.UserHandle; @@ -33,40 +32,6 @@ public final class PackageUtils { private PackageUtils() {} /** - * Retrieve the {@link PackageInfo} of an application. - * - * @param packageName the package name of the application - * @param extraFlags the extra flags to pass to {@link PackageManager#getPackageInfo(String, - * int)} - * @param context the {@code Context} to retrieve system services - * - * @return the {@link PackageInfo} of the application, or {@code null} if not found - */ - @Nullable - public static PackageInfo getPackageInfo(@NonNull String packageName, int extraFlags, - @NonNull Context context) { - PackageManager packageManager = context.getPackageManager(); - try { - return packageManager.getPackageInfo(packageName, PackageManager.MATCH_DIRECT_BOOT_AWARE - | PackageManager.MATCH_DIRECT_BOOT_UNAWARE | extraFlags); - } catch (PackageManager.NameNotFoundException e) { - return null; - } - } - - /** - * Retrieve if a package is a system package. - * - * @param packageName the name of the package - * @param context the {@code Context} to retrieve system services - * - * @return whether the package is a system package - */ - public static boolean isSystemPackage(@NonNull String packageName, @NonNull Context context) { - return getPackageInfo(packageName, PackageManager.MATCH_SYSTEM_ONLY, context) != null; - } - - /** * Retrieve the {@link ApplicationInfo} of an application. * * @param packageName the package name of the application diff --git a/PermissionController/src/com/android/permissioncontroller/role/utils/RoleManagerCompat.java b/PermissionController/src/com/android/permissioncontroller/role/utils/RoleManagerCompat.java deleted file mode 100644 index 47f96adfb..000000000 --- a/PermissionController/src/com/android/permissioncontroller/role/utils/RoleManagerCompat.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2021 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.permissioncontroller.role.utils; - -import android.app.role.RoleManager; - -import androidx.annotation.NonNull; - -import com.android.modules.utils.build.SdkLevel; - -/** - * Helper for accessing features in {@link RoleManager}. - */ -public class RoleManagerCompat { - - private RoleManagerCompat() {} - - /** - * @see RoleManager#isBypassingRoleQualification() - */ - public static boolean isBypassingRoleQualification(@NonNull RoleManager roleManager) { - if (SdkLevel.isAtLeastS()) { - return roleManager.isBypassingRoleQualification(); - } else { - return false; - } - } -} |