1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.permissioncontroller; 18 19 import android.app.Application; 20 import android.content.ComponentName; 21 import android.content.pm.PackageItemInfo; 22 import android.content.pm.PackageManager; 23 import android.os.Build; 24 import android.os.Process; 25 import android.util.ArrayMap; 26 import android.view.accessibility.AccessibilityManager; 27 28 import androidx.annotation.RequiresApi; 29 30 import com.android.modules.utils.build.SdkLevel; 31 import com.android.permissioncontroller.permission.utils.KotlinUtils; 32 import com.android.permissioncontroller.permission.utils.Utils; 33 import com.android.permissioncontroller.privacysources.SafetyCenterAccessibilityListener; 34 import com.android.permissioncontroller.role.model.RoleParserInitializer; 35 import com.android.permissioncontroller.role.ui.SpecialAppAccessListActivity; 36 import com.android.role.controller.model.Role; 37 import com.android.role.controller.model.Roles; 38 39 public final class PermissionControllerApplication extends Application { 40 41 private static PermissionControllerApplication sInstance; 42 43 @Override onCreate()44 public void onCreate() { 45 super.onCreate(); 46 47 sInstance = this; 48 49 PackageItemInfo.forceSafeLabels(); 50 RoleParserInitializer.initialize(); 51 updateSpecialAppAccessListActivityEnabledState(); 52 if (SdkLevel.isAtLeastT()) { 53 addAccessibilityListener(); 54 } 55 if (Utils.isHealthPermissionUiEnabled()) { 56 KotlinUtils.INSTANCE.addHealthPermissions(this); 57 } 58 } 59 60 /** 61 * Statically gets the {@link PermissionControllerApplication} instance 62 */ get()63 public static PermissionControllerApplication get() { 64 return sInstance; 65 } 66 updateSpecialAppAccessListActivityEnabledState()67 private void updateSpecialAppAccessListActivityEnabledState() { 68 ArrayMap<String, Role> roles = Roles.get(this); 69 boolean hasVisibleSpecialAppAccess = false; 70 int rolesSize = roles.size(); 71 for (int i = 0; i < rolesSize; i++) { 72 Role role = roles.valueAt(i); 73 74 if (!role.isAvailableAsUser(Process.myUserHandle(), this) 75 || !role.isVisibleAsUser(Process.myUserHandle(), this)) { 76 continue; 77 } 78 if (!role.isExclusive()) { 79 hasVisibleSpecialAppAccess = true; 80 break; 81 } 82 } 83 84 PackageManager packageManager = getPackageManager(); 85 ComponentName componentName = new ComponentName(this, SpecialAppAccessListActivity.class); 86 int enabledState = hasVisibleSpecialAppAccess 87 ? PackageManager.COMPONENT_ENABLED_STATE_DEFAULT 88 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED; 89 packageManager.setComponentEnabledSetting(componentName, enabledState, 90 PackageManager.DONT_KILL_APP); 91 } 92 93 @RequiresApi(Build.VERSION_CODES.TIRAMISU) addAccessibilityListener()94 private void addAccessibilityListener() { 95 AccessibilityManager a11yManager = Utils.getSystemServiceSafe( 96 this, AccessibilityManager.class); 97 a11yManager.addAccessibilityServicesStateChangeListener( 98 new SafetyCenterAccessibilityListener(this)); 99 } 100 101 } 102