1 /*
2  * Copyright (C) 2024 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.notification.modes;
18 
19 import static com.android.settings.notification.modes.ZenModeFragmentBase.MODE_ID;
20 
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.service.notification.ZenPolicy;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.annotation.VisibleForTesting;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 import androidx.preference.TwoStatePreference;
32 
33 import com.android.settings.core.SubSettingLauncher;
34 import com.android.settingslib.widget.SelectorWithWidgetPreference;
35 
36 public class ZenModeAppsPreferenceController extends
37         AbstractZenModePreferenceController implements Preference.OnPreferenceChangeListener {
38 
39     static final String KEY_PRIORITY = "zen_mode_apps_priority";
40     static final String KEY_NONE = "zen_mode_apps_none";
41     static final String KEY_ALL = "zen_mode_apps_all";
42 
43     String mModeId;
44 
45 
ZenModeAppsPreferenceController(@onNull Context context, @NonNull String key, @Nullable ZenModesBackend backend)46     public ZenModeAppsPreferenceController(@NonNull Context context,
47             @NonNull String key, @Nullable ZenModesBackend backend) {
48         super(context, key, backend);
49     }
50 
51     @Override
displayPreference(PreferenceScreen screen)52     public void displayPreference(PreferenceScreen screen) {
53         SelectorWithWidgetPreference pref = screen.findPreference(getPreferenceKey());
54         if (pref != null) {
55             pref.setOnClickListener(mSelectorClickListener);
56 
57             // Adds the widget to only the priority category.
58             if (getPreferenceKey().equals(KEY_PRIORITY)) {
59                 pref.setExtraWidgetOnClickListener(p -> {
60                     launchPrioritySettings();
61                 });
62             }
63         }
64         super.displayPreference(screen);
65     }
66 
67     @Override
updateState(Preference preference, @NonNull ZenMode zenMode)68     public void updateState(Preference preference, @NonNull ZenMode zenMode) {
69         mModeId = zenMode.getId();
70         TwoStatePreference pref = (TwoStatePreference) preference;
71         switch (getPreferenceKey()) {
72             case KEY_PRIORITY:
73                 boolean policy_priority = zenMode.getPolicy().getAllowedChannels()
74                         == ZenPolicy.CHANNEL_POLICY_PRIORITY;
75                 pref.setChecked(policy_priority);
76                 break;
77             case KEY_NONE:
78                 boolean policy_none = zenMode.getPolicy().getAllowedChannels()
79                         == ZenPolicy.CHANNEL_POLICY_NONE;
80                 pref.setChecked(policy_none);
81                 break;
82             case KEY_ALL:
83                 // A UI-only setting; the underlying policy never actually has this value,
84                 // but ZenMode acts as though it does for the sake of UI consistency.
85                 boolean policy_all = zenMode.getPolicy().getAllowedChannels()
86                         == ZenMode.CHANNEL_POLICY_ALL;
87                 pref.setChecked(policy_all);
88                 break;
89         }
90     }
91 
92     @Override
onPreferenceChange(@onNull Preference preference, Object newValue)93     public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {
94         switch (getPreferenceKey()) {
95             case KEY_PRIORITY:
96                 return savePolicy(p -> p.allowChannels(ZenPolicy.CHANNEL_POLICY_PRIORITY));
97             case KEY_NONE:
98                 return savePolicy(p -> p.allowChannels(ZenPolicy.CHANNEL_POLICY_NONE));
99             case KEY_ALL:
100                 return savePolicy(p -> p.allowChannels(ZenMode.CHANNEL_POLICY_ALL));
101         }
102         return true;
103     }
104 
105     @VisibleForTesting
106     SelectorWithWidgetPreference.OnClickListener mSelectorClickListener =
107             new SelectorWithWidgetPreference.OnClickListener() {
108                 @Override
109                 public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
110                     onPreferenceChange(preference, true);
111                 }
112             };
113 
launchPrioritySettings()114     private void launchPrioritySettings() {
115         Bundle bundle = new Bundle();
116         if (mModeId != null) {
117             bundle.putString(MODE_ID, mModeId);
118         }
119         // TODO(b/332937635): Update metrics category
120         new SubSettingLauncher(mContext)
121                 .setDestination(ZenModeSelectBypassingAppsFragment.class.getName())
122                 .setSourceMetricsCategory(SettingsEnums.SETTINGS_ZEN_NOTIFICATIONS)
123                 .setArguments(bundle)
124                 .launch();
125     }
126 }
127