1 /*
2  * Copyright (C) 2018 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;
18 
19 import android.app.NotificationManager;
20 import android.content.Context;
21 import android.provider.Settings;
22 import android.text.TextUtils;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.ListPreference;
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.R;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 
32 public class ZenModePriorityMessagesPreferenceController extends AbstractZenModePreferenceController
33         implements Preference.OnPreferenceChangeListener {
34 
35     protected static final String KEY = "zen_mode_messages";
36     private final ZenModeBackend mBackend;
37     private ListPreference mPreference;
38     private final String[] mListValues;
39 
ZenModePriorityMessagesPreferenceController(Context context, Lifecycle lifecycle)40     public ZenModePriorityMessagesPreferenceController(Context context, Lifecycle lifecycle) {
41         super(context, KEY, lifecycle);
42         mBackend = ZenModeBackend.getInstance(context);
43         mListValues = context.getResources().getStringArray(R.array.zen_mode_contacts_values);
44     }
45 
46     @Override
getPreferenceKey()47     public String getPreferenceKey() {
48         return KEY;
49     }
50 
51     @Override
isAvailable()52     public boolean isAvailable() {
53         return true;
54     }
55 
56     @Override
displayPreference(PreferenceScreen screen)57     public void displayPreference(PreferenceScreen screen) {
58         super.displayPreference(screen);
59         mPreference = screen.findPreference(KEY);
60     }
61 
62     @Override
updateState(Preference preference)63     public void updateState(Preference preference) {
64         super.updateState(preference);
65         updateFromContactsValue(preference);
66     }
67 
68     @Override
onPreferenceChange(Preference preference, Object selectedContactsFrom)69     public boolean onPreferenceChange(Preference preference, Object selectedContactsFrom) {
70         mBackend.saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES,
71                 ZenModeBackend.getSettingFromPrefKey(selectedContactsFrom.toString()));
72         updateFromContactsValue(preference);
73         return true;
74     }
75 
updateFromContactsValue(Preference preference)76     private void updateFromContactsValue(Preference preference) {
77         mPreference = (ListPreference) preference;
78         switch (getZenMode()) {
79             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
80             case Settings.Global.ZEN_MODE_ALARMS:
81                 mPreference.setEnabled(false);
82                 mPreference.setValue(ZenModeBackend.ZEN_MODE_FROM_NONE);
83                 mPreference.setSummary(mBackend.getAlarmsTotalSilenceCallsMessagesSummary(
84                         NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES));
85                 break;
86             default:
87                 preference.setEnabled(true);
88                 preference.setSummary(mBackend.getContactsSummary(
89                         NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES));
90 
91                 final String currentVal = ZenModeBackend.getKeyFromSetting(
92                         mBackend.getPriorityMessageSenders());
93                 mPreference.setValue(mListValues[getIndexOfSendersValue(currentVal)]);
94         }
95     }
96 
97     @VisibleForTesting
getIndexOfSendersValue(String currentVal)98     protected int getIndexOfSendersValue(String currentVal) {
99         int index = 3; // defaults to "none" based on R.array.zen_mode_contacts_values
100         for (int i = 0; i < mListValues.length; i++) {
101             if (TextUtils.equals(currentVal, mListValues[i])) {
102                 return i;
103             }
104         }
105 
106         return index;
107     }
108 }
109