1 /*
2  * Copyright (C) 2021 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 package com.android.settings.applications.specialaccess.notificationaccess;
15 
16 import android.content.ComponentName;
17 import android.content.Context;
18 import android.os.Build;
19 import android.service.notification.NotificationListenerFilter;
20 
21 import androidx.preference.Preference;
22 
23 import com.android.settings.core.BasePreferenceController;
24 import com.android.settings.notification.NotificationBackend;
25 
26 public class BridgedAppsLinkPreferenceController extends BasePreferenceController {
27 
28     private ComponentName mCn;
29     private int mUserId;
30     private NotificationBackend mNm;
31     private NotificationListenerFilter mNlf;
32     private int mTargetSdk;
33 
BridgedAppsLinkPreferenceController(Context context, String key)34     public BridgedAppsLinkPreferenceController(Context context, String key) {
35         super(context, key);
36     }
37 
38 
setCn(ComponentName cn)39     public BridgedAppsLinkPreferenceController setCn(ComponentName cn) {
40         mCn = cn;
41         return this;
42     }
43 
setUserId(int userId)44     public BridgedAppsLinkPreferenceController setUserId(int userId) {
45         mUserId = userId;
46         return this;
47     }
48 
setNm(NotificationBackend nm)49     public BridgedAppsLinkPreferenceController setNm(NotificationBackend nm) {
50         mNm = nm;
51         return this;
52     }
53 
setTargetSdk(int targetSdk)54     public BridgedAppsLinkPreferenceController setTargetSdk(int targetSdk) {
55         mTargetSdk = targetSdk;
56         return this;
57     }
58 
59     @Override
getAvailabilityStatus()60     public int getAvailabilityStatus() {
61         if (mNm.isNotificationListenerAccessGranted(mCn)) {
62             if (mTargetSdk > Build.VERSION_CODES.S) {
63                 return AVAILABLE;
64             }
65             mNlf = mNm.getListenerFilter(mCn, mUserId);
66             if (!mNlf.areAllTypesAllowed() || !mNlf.getDisallowedPackages().isEmpty()) {
67                 return AVAILABLE;
68             }
69         }
70         return DISABLED_DEPENDENT_SETTING;
71     }
72 
73     @Override
updateState(Preference pref)74     public void updateState(Preference pref) {
75         pref.setEnabled(getAvailabilityStatus() == AVAILABLE);
76         super.updateState(pref);
77     }
78 }
79