• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.RoleManager;
18 import android.content.Context;
19 import android.content.Intent;
20 import android.os.UserManager;
21 import android.text.TextUtils;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.internal.util.CollectionUtils;
27 import com.android.settings.R;
28 import com.android.settings.core.BasePreferenceController;
29 
30 import java.util.concurrent.Executor;
31 
32 /*
33  * Abstract base controller for the default app shortcut preferences that launches the default app
34  * settings with the corresponding default app highlighted.
35  */
36 public abstract class DefaultAppShortcutPreferenceControllerBase extends BasePreferenceController {
37 
38     private final String mRoleName;
39 
40     protected final String mPackageName;
41 
42     private final RoleManager mRoleManager;
43 
44     private boolean mRoleVisible;
45 
46     private boolean mAppVisible;
47 
48     private PreferenceScreen mPreferenceScreen;
49 
DefaultAppShortcutPreferenceControllerBase(Context context, String preferenceKey, String roleName, String packageName)50     public DefaultAppShortcutPreferenceControllerBase(Context context, String preferenceKey,
51             String roleName, String packageName) {
52         super(context, preferenceKey);
53 
54         mRoleName = roleName;
55         mPackageName = packageName;
56 
57         mRoleManager = context.getSystemService(RoleManager.class);
58 
59         final Executor executor = mContext.getMainExecutor();
60         mRoleManager.isRoleVisible(mRoleName, executor, visible -> {
61             mRoleVisible = visible;
62             refreshAvailability();
63         });
64         mRoleManager.isApplicationVisibleForRole(mRoleName, mPackageName, executor,
65                 visible -> {
66                     mAppVisible = visible;
67                     refreshAvailability();
68                 });
69     }
70 
71     @Override
displayPreference(PreferenceScreen screen)72     public void displayPreference(PreferenceScreen screen) {
73         super.displayPreference(screen);
74 
75         mPreferenceScreen = screen;
76     }
77 
refreshAvailability()78     private void refreshAvailability() {
79         if (mPreferenceScreen != null) {
80             final Preference preference = mPreferenceScreen.findPreference(getPreferenceKey());
81             if (preference != null) {
82                 preference.setVisible(isAvailable());
83                 updateState(preference);
84             }
85         }
86     }
87 
88     @Override
getAvailabilityStatus()89     public int getAvailabilityStatus() {
90         if (mContext.getSystemService(UserManager.class).isManagedProfile()) {
91             return DISABLED_FOR_USER;
92         }
93         return mRoleVisible && mAppVisible ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
94     }
95 
96     @Override
getSummary()97     public CharSequence getSummary() {
98         final int summaryResId = isDefaultApp() ? R.string.yes : R.string.no;
99         return mContext.getText(summaryResId);
100     }
101 
102     @Override
handlePreferenceTreeClick(Preference preference)103     public boolean handlePreferenceTreeClick(Preference preference) {
104         if (!TextUtils.equals(mPreferenceKey, preference.getKey())) {
105             return false;
106         }
107         final Intent intent = new Intent(Intent.ACTION_MANAGE_DEFAULT_APP)
108                 .putExtra(Intent.EXTRA_ROLE_NAME, mRoleName);
109         mContext.startActivity(intent);
110         return true;
111     }
112 
113     /**
114      * Check whether the app is the default app
115      *
116      * @return true if the app is the default app
117      */
isDefaultApp()118     private boolean isDefaultApp() {
119         final String packageName = CollectionUtils.firstOrNull(mRoleManager.getRoleHolders(
120                 mRoleName));
121         return TextUtils.equals(mPackageName, packageName);
122     }
123 }
124