1 /*
2  * Copyright 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.car.settings.applications.specialaccess;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 
25 import androidx.annotation.Nullable;
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.Preference;
28 
29 import com.android.car.settings.common.FragmentController;
30 import com.android.car.settings.common.PreferenceController;
31 
32 /**
33  * Launches into additional special access settings if they are supported by the permission
34  * controller package.
35  */
36 public class MoreSpecialAccessPreferenceController extends PreferenceController<Preference> {
37 
38     @Nullable
39     private final Intent mIntent;
40 
MoreSpecialAccessPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)41     public MoreSpecialAccessPreferenceController(Context context, String preferenceKey,
42             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
43         this(context, preferenceKey, fragmentController, uxRestrictions,
44                 context.getPackageManager());
45     }
46 
47     @VisibleForTesting
MoreSpecialAccessPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions, PackageManager packageManager)48     MoreSpecialAccessPreferenceController(Context context, String preferenceKey,
49             FragmentController fragmentController, CarUxRestrictions uxRestrictions,
50             PackageManager packageManager) {
51         super(context, preferenceKey, fragmentController, uxRestrictions);
52         String packageName = packageManager.getPermissionControllerPackageName();
53         if (packageName != null) {
54             Intent intent = new Intent(Intent.ACTION_MANAGE_SPECIAL_APP_ACCESSES).setPackage(
55                     packageName);
56             ResolveInfo resolveInfo = packageManager.resolveActivity(intent,
57                     PackageManager.MATCH_DEFAULT_ONLY);
58             mIntent = (resolveInfo != null) ? intent : null;
59         } else {
60             mIntent = null;
61         }
62     }
63 
64     @Override
getPreferenceType()65     protected Class<Preference> getPreferenceType() {
66         return Preference.class;
67     }
68 
69     @Override
getAvailabilityStatus()70     protected int getAvailabilityStatus() {
71         return (mIntent != null) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
72     }
73 
74     @Override
handlePreferenceClicked(Preference preference)75     protected boolean handlePreferenceClicked(Preference preference) {
76         if (mIntent != null) {
77             getContext().startActivity(mIntent);
78             return true;
79         }
80         return false;
81     }
82 }
83