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