• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 package com.android.settings.notification.app;
17 
18 import android.app.Flags;
19 import android.content.Context;
20 import android.service.notification.Adjustment;
21 
22 import androidx.annotation.NonNull;
23 import androidx.preference.Preference;
24 
25 import com.android.settings.notification.NotificationBackend;
26 import com.android.settingslib.RestrictedSwitchPreference;
27 
28 /**
29  * Used for the app-level preference screen to opt the app in or out of a provided Adjustment key.
30  * E.g. to say an app can or cannot be classified by the NotificationAssistantService.
31  */
32 public class AdjustmentKeyPreferenceController extends
33         NotificationPreferenceController implements Preference.OnPreferenceChangeListener {
34     private String mKey;
35 
AdjustmentKeyPreferenceController(@onNull Context context, @NonNull NotificationBackend backend, String key)36     public AdjustmentKeyPreferenceController(@NonNull Context context,
37             @NonNull NotificationBackend backend, String key) {
38         super(context, backend);
39         mKey = key;
40     }
41 
42     @Override
43     @NonNull
getPreferenceKey()44     public String getPreferenceKey() {
45         return mKey;
46     }
47 
48     @Override
isAvailable()49     public boolean isAvailable() {
50         if (!(Flags.notificationClassificationUi() || Flags.nmSummarizationUi()
51                 || Flags.nmSummarization())) {
52             return false;
53         }
54         boolean isBundlePref = Adjustment.KEY_TYPE.equals(mKey);
55         boolean isSummarizePref = Adjustment.KEY_SUMMARIZATION.equals(mKey);
56         if (!Flags.notificationClassificationUi() && isBundlePref) {
57             return false;
58         }
59         if (!(Flags.nmSummarizationUi() || Flags.nmSummarization()) && isSummarizePref) {
60             return false;
61         }
62         if (!isSummarizePref && !isBundlePref) {
63             return false;
64         }
65         if (isSummarizePref && !(mBackend.hasSentValidMsg(mAppRow.pkg, mAppRow.uid)
66                 || mBackend.isInInvalidMsgState(mAppRow.pkg, mAppRow.uid))) {
67             return false;
68         }
69         return super.isAvailable();
70     }
71 
72     @Override
isIncludedInFilter()73     boolean isIncludedInFilter() {
74         // not a channel-specific preference; only at the app level
75         return false;
76     }
77 
updateState(@onNull Preference preference)78     public void updateState(@NonNull Preference preference) {
79         RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference;
80         if (pref.getParent() != null) {
81             pref.getParent().setVisible(true);
82         }
83 
84         if (pref != null && mAppRow != null) {
85             pref.setDisabledByAdmin(mAdmin);
86             pref.setEnabled(!pref.isDisabledByAdmin());
87             pref.setChecked(mBackend.getAllowedAssistantAdjustments(mAppRow.pkg).contains(mKey));
88             pref.setOnPreferenceChangeListener(this);
89         }
90     }
91 
92     @Override
onPreferenceChange(@onNull Preference preference, @NonNull Object newValue)93     public boolean onPreferenceChange(@NonNull Preference preference, @NonNull Object newValue) {
94         final boolean allowedForPkg = (Boolean) newValue;
95         mBackend.setAdjustmentSupportedForPackage(mKey, mAppRow.pkg, allowedForPkg);
96         return true;
97     }
98 }
99