1 /*
2  * Copyright (C) 2022 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.notificationaccess;
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.service.notification.NotificationListenerService;
24 
25 import androidx.preference.Preference;
26 
27 import com.android.settings.core.BasePreferenceController;
28 
29 import java.util.List;
30 
31 /**
32  * Controls link to reach more preference settings inside the app.
33  */
34 public class MoreSettingsPreferenceController extends BasePreferenceController {
35 
36     private static final String TAG = "MoreSettingsPrefContr";
37     private static final String KEY_MORE_SETTINGS = "more_settings";
38 
39     PackageManager mPm;
40     String mPackage;
41     Intent mIntent = new Intent(NotificationListenerService.ACTION_SETTINGS_HOME);
42 
MoreSettingsPreferenceController(Context context)43     public MoreSettingsPreferenceController(Context context) {
44         super(context, KEY_MORE_SETTINGS);
45     }
46 
47     @Override
getAvailabilityStatus()48     public int getAvailabilityStatus() {
49         final List<ResolveInfo> resolveInfos = mPm.queryIntentActivities(
50                 mIntent,
51                 PackageManager.ResolveInfoFlags.of(0));
52         if (resolveInfos == null || resolveInfos.isEmpty()) {
53             return CONDITIONALLY_UNAVAILABLE;
54         }
55         return AVAILABLE;
56     }
57 
58     @Override
getPreferenceKey()59     public String getPreferenceKey() {
60         return KEY_MORE_SETTINGS;
61     }
62 
setPackageManager(PackageManager pm)63     public MoreSettingsPreferenceController setPackageManager(PackageManager pm) {
64         mPm = pm;
65         return this;
66     }
67 
setPackage(String pkg)68     public MoreSettingsPreferenceController setPackage(String pkg) {
69         mPackage = pkg;
70         mIntent.setPackage(mPackage);
71         return this;
72     }
73 
updateState(Preference preference)74     public void updateState(Preference preference) {
75         preference.setIntent(mIntent);
76     }
77 }
78