1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.settings.applications.appinfo; 16 17 import android.app.role.RoleControllerManager; 18 import android.app.role.RoleManager; 19 import android.app.settings.SettingsEnums; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.UserManager; 23 import android.text.TextUtils; 24 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceScreen; 27 28 import com.android.internal.util.CollectionUtils; 29 import com.android.settings.R; 30 import com.android.settings.core.BasePreferenceController; 31 32 import java.util.concurrent.Executor; 33 34 /* 35 * Abstract base controller for the default app shortcut preferences that launches the default app 36 * settings with the corresponding default app highlighted. 37 */ 38 public abstract class DefaultAppShortcutPreferenceControllerBase extends BasePreferenceController { 39 40 private final String mRoleName; 41 42 protected final String mPackageName; 43 44 private final RoleManager mRoleManager; 45 46 private boolean mRoleVisible; 47 48 private boolean mAppVisible; 49 50 private PreferenceScreen mPreferenceScreen; 51 DefaultAppShortcutPreferenceControllerBase(Context context, String preferenceKey, String roleName, String packageName)52 public DefaultAppShortcutPreferenceControllerBase(Context context, String preferenceKey, 53 String roleName, String packageName) { 54 super(context, preferenceKey); 55 56 mRoleName = roleName; 57 mPackageName = packageName; 58 59 mRoleManager = context.getSystemService(RoleManager.class); 60 61 final RoleControllerManager roleControllerManager = 62 mContext.getSystemService(RoleControllerManager.class); 63 final Executor executor = mContext.getMainExecutor(); 64 roleControllerManager.isRoleVisible(mRoleName, executor, visible -> { 65 mRoleVisible = visible; 66 refreshAvailability(); 67 }); 68 roleControllerManager.isApplicationVisibleForRole(mRoleName, mPackageName, executor, 69 visible -> { 70 mAppVisible = visible; 71 refreshAvailability(); 72 }); 73 } 74 75 @Override displayPreference(PreferenceScreen screen)76 public void displayPreference(PreferenceScreen screen) { 77 super.displayPreference(screen); 78 79 mPreferenceScreen = screen; 80 } 81 refreshAvailability()82 private void refreshAvailability() { 83 if (mPreferenceScreen != null) { 84 final Preference preference = mPreferenceScreen.findPreference(getPreferenceKey()); 85 if (preference != null) { 86 preference.setVisible(isAvailable()); 87 updateState(preference); 88 } 89 } 90 } 91 92 @Override getAvailabilityStatus()93 public int getAvailabilityStatus() { 94 if (mContext.getSystemService(UserManager.class).isManagedProfile()) { 95 return DISABLED_FOR_USER; 96 } 97 return mRoleVisible && mAppVisible ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 98 } 99 100 @Override getSummary()101 public CharSequence getSummary() { 102 final int summaryResId = isDefaultApp() ? R.string.yes : R.string.no; 103 return mContext.getText(summaryResId); 104 } 105 106 @Override handlePreferenceTreeClick(Preference preference)107 public boolean handlePreferenceTreeClick(Preference preference) { 108 if (!TextUtils.equals(mPreferenceKey, preference.getKey())) { 109 return false; 110 } 111 final Intent intent = new Intent(Intent.ACTION_MANAGE_DEFAULT_APP) 112 .putExtra(Intent.EXTRA_ROLE_NAME, mRoleName); 113 mContext.startActivity(intent); 114 return true; 115 } 116 117 /** 118 * Check whether the app is the default app 119 * 120 * @return true if the app is the default app 121 */ isDefaultApp()122 private boolean isDefaultApp() { 123 final String packageName = CollectionUtils.firstOrNull(mRoleManager.getRoleHolders( 124 mRoleName)); 125 return TextUtils.equals(mPackageName, packageName); 126 } 127 } 128