• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.settings.notification;
2 
3 import android.app.NotificationManager;
4 import android.content.Context;
5 import android.provider.Settings;
6 import android.support.v7.preference.ListPreference;
7 import android.support.v7.preference.Preference;
8 import android.support.v7.preference.PreferenceScreen;
9 import android.text.TextUtils;
10 
11 import com.android.internal.annotations.VisibleForTesting;
12 import com.android.settings.R;
13 import com.android.settingslib.core.lifecycle.Lifecycle;
14 
15 public class ZenModeMessagesPreferenceController extends AbstractZenModePreferenceController
16         implements Preference.OnPreferenceChangeListener {
17 
18     protected static final String KEY = "zen_mode_messages";
19 
20     private final ZenModeBackend mBackend;
21     private ListPreference mPreference;
22     private final String[] mListValues;
23 
ZenModeMessagesPreferenceController(Context context, Lifecycle lifecycle)24     public ZenModeMessagesPreferenceController(Context context, Lifecycle lifecycle) {
25         super(context, KEY, lifecycle);
26         mBackend = ZenModeBackend.getInstance(context);
27         mListValues = context.getResources().getStringArray(R.array.zen_mode_contacts_values);
28     }
29 
30     @Override
getPreferenceKey()31     public String getPreferenceKey() {
32         return KEY;
33     }
34 
35     @Override
isAvailable()36     public boolean isAvailable() {
37         return true;
38     }
39 
40     @Override
displayPreference(PreferenceScreen screen)41     public void displayPreference(PreferenceScreen screen) {
42         super.displayPreference(screen);
43         mPreference = (ListPreference) screen.findPreference(KEY);
44     }
45 
46     @Override
updateState(Preference preference)47     public void updateState(Preference preference) {
48         super.updateState(preference);
49         updateFromContactsValue(preference);
50     }
51 
52     @Override
onPreferenceChange(Preference preference, Object selectedContactsFrom)53     public boolean onPreferenceChange(Preference preference, Object selectedContactsFrom) {
54         mBackend.saveSenders(NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES,
55                 ZenModeBackend.getSettingFromPrefKey(selectedContactsFrom.toString()));
56         updateFromContactsValue(preference);
57         return true;
58     }
59 
updateFromContactsValue(Preference preference)60     private void updateFromContactsValue(Preference preference) {
61         mPreference = (ListPreference) preference;
62         switch (getZenMode()) {
63             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
64             case Settings.Global.ZEN_MODE_ALARMS:
65                 mPreference.setEnabled(false);
66                 mPreference.setValue(ZenModeBackend.ZEN_MODE_FROM_NONE);
67                 mPreference.setSummary(mBackend.getContactsSummary(ZenModeBackend.SOURCE_NONE));
68                 break;
69             default:
70                 preference.setEnabled(true);
71                 preference.setSummary(mBackend.getContactsSummary(
72                         NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES));
73 
74                 final String currentVal = ZenModeBackend.getKeyFromSetting(
75                         mBackend.getPriorityMessageSenders());
76                 mPreference.setValue(mListValues[getIndexOfSendersValue(currentVal)]);
77         }
78     }
79 
80     @VisibleForTesting
getIndexOfSendersValue(String currentVal)81     protected int getIndexOfSendersValue(String currentVal) {
82         int index = 3; // defaults to "none" based on R.array.zen_mode_contacts_values
83         for (int i = 0; i < mListValues.length; i++) {
84             if (TextUtils.equals(currentVal, mListValues[i])) {
85                 return i;
86             }
87         }
88 
89         return index;
90     }
91 }
92