• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.zen;
18 
19 import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CALLS;
20 import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES;
21 
22 import static com.android.settings.notification.zen.ZenPrioritySendersHelper.UNKNOWN;
23 
24 import android.content.Context;
25 import android.os.AsyncTask;
26 
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceCategory;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settings.notification.NotificationBackend;
33 import com.android.settingslib.core.lifecycle.Lifecycle;
34 import com.android.settingslib.widget.SelectorWithWidgetPreference;
35 
36 /**
37  * Common preference controller functionality for zen mode priority senders preferences for both
38  * messages and calls.
39  *
40  * These controllers handle the settings regarding which priority senders that are allowed to
41  * bypass DND for calls or messages, which may be one the following values: starred contacts, all
42  * contacts, priority conversations (for messages only), anyone, or no one.
43  *
44  * Most of the functionality is handled by ZenPrioritySendersHelper, so that it can also be shared
45  * with settings controllers for custom rules. This class handles the parts of the behavior where
46  * settings must be written to the relevant backends, as that's where this class diverges from
47  * custom rules.
48  */
49 public class ZenModePrioritySendersPreferenceController
50         extends AbstractZenModePreferenceController {
51     private final boolean mIsMessages; // if this is false, then this preference is for calls
52 
53     private PreferenceCategory mPreferenceCategory;
54     private ZenPrioritySendersHelper mHelper;
55 
ZenModePrioritySendersPreferenceController(Context context, String key, Lifecycle lifecycle, boolean isMessages, NotificationBackend notificationBackend)56     public ZenModePrioritySendersPreferenceController(Context context, String key,
57             Lifecycle lifecycle, boolean isMessages, NotificationBackend notificationBackend) {
58         super(context, key, lifecycle);
59         mIsMessages = isMessages;
60 
61         mHelper = new ZenPrioritySendersHelper(
62                 context, isMessages, mBackend, notificationBackend, mSelectorClickListener);
63     }
64 
65     @Override
displayPreference(PreferenceScreen screen)66     public void displayPreference(PreferenceScreen screen) {
67         mPreferenceCategory = screen.findPreference(getPreferenceKey());
68         mHelper.displayPreference(mPreferenceCategory);
69         super.displayPreference(screen);
70     }
71 
72     @Override
isAvailable()73     public boolean isAvailable() {
74         return true;
75     }
76 
77     @Override
getPreferenceKey()78     public String getPreferenceKey() {
79         return KEY;
80     }
81 
82     @Override
updateState(Preference preference)83     public void updateState(Preference preference) {
84         final int currContactsSetting = getPrioritySenders();
85         final int currConversationsSetting = getPriorityConversationSenders();
86         mHelper.updateState(currContactsSetting, currConversationsSetting);
87     }
88 
89     @Override
onResume()90     public void onResume() {
91         super.onResume();
92         if (mIsMessages) {
93             updateChannelCounts();
94         }
95         mHelper.updateSummaries();
96     }
97 
updateChannelCounts()98     private void updateChannelCounts() {
99         // Load conversations
100         new AsyncTask<Void, Void, Void>() {
101             @Override
102             protected Void doInBackground(Void... unused) {
103                 mHelper.updateChannelCounts();
104                 return null;
105             }
106 
107             @Override
108             protected void onPostExecute(Void unused) {
109                 if (mContext == null) {
110                     return;
111                 }
112                 updateState(mPreferenceCategory);
113             }
114         }.execute();
115     }
116 
getPrioritySenders()117     private int getPrioritySenders() {
118         if (mIsMessages) {
119             return mBackend.getPriorityMessageSenders();
120         } else {
121             return mBackend.getPriorityCallSenders();
122         }
123     }
124 
getPriorityConversationSenders()125     private int getPriorityConversationSenders() {
126         if (mIsMessages) {
127             return mBackend.getPriorityConversationSenders();
128         }
129         return UNKNOWN;
130     }
131 
132     @VisibleForTesting
133     SelectorWithWidgetPreference.OnClickListener mSelectorClickListener =
134             new SelectorWithWidgetPreference.OnClickListener() {
135         @Override
136         public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
137             // The settingsToSaveOnClick function takes whether or not the preference is a
138             // checkbox into account to determine whether this selection is checked or unchecked.
139             final int[] settingsToSave = mHelper.settingsToSaveOnClick(preference,
140                     getPrioritySenders(), getPriorityConversationSenders());
141             final int prioritySendersSetting = settingsToSave[0];
142             final int priorityConvosSetting = settingsToSave[1];
143 
144             if (prioritySendersSetting != UNKNOWN) {
145                 mBackend.saveSenders(
146                         mIsMessages ? PRIORITY_CATEGORY_MESSAGES : PRIORITY_CATEGORY_CALLS,
147                         prioritySendersSetting);
148             }
149 
150             if (mIsMessages && priorityConvosSetting != UNKNOWN) {
151                 mBackend.saveConversationSenders(priorityConvosSetting);
152             }
153         }
154     };
155 }
156