1 /*
2  * Copyright (C) 2019 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.settings.applications.specialaccess;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.content.pm.ResolveInfo;
23 import android.text.TextUtils;
24 
25 import androidx.preference.Preference;
26 
27 import com.android.settings.core.BasePreferenceController;
28 
29 public class MoreSpecialAccessPreferenceController extends BasePreferenceController {
30 
31     private final Intent mIntent;
32 
MoreSpecialAccessPreferenceController(Context context, String preferenceKey)33     public MoreSpecialAccessPreferenceController(Context context, String preferenceKey) {
34         super(context, preferenceKey);
35 
36         final PackageManager packageManager = context.getPackageManager();
37         final String packageName = packageManager.getPermissionControllerPackageName();
38         if (packageName != null) {
39             Intent intent = new Intent(Intent.ACTION_MANAGE_SPECIAL_APP_ACCESSES)
40                     .setPackage(packageName);
41             ResolveInfo resolveInfo = packageManager.resolveActivity(intent,
42                     PackageManager.MATCH_DEFAULT_ONLY);
43             mIntent = resolveInfo != null ? intent : null;
44         } else {
45             mIntent = null;
46         }
47     }
48 
49     @Override
getAvailabilityStatus()50     public int getAvailabilityStatus() {
51         return mIntent != null ? AVAILABLE_UNSEARCHABLE : UNSUPPORTED_ON_DEVICE;
52     }
53 
54     @Override
handlePreferenceTreeClick(Preference preference)55     public boolean handlePreferenceTreeClick(Preference preference) {
56         if (TextUtils.equals(preference.getKey(), mPreferenceKey)) {
57             if (mIntent != null) {
58                 mContext.startActivity(mIntent);
59             }
60             return true;
61         }
62         return false;
63     }
64 }
65