1/*
2 * Copyright (C) 2015 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
17package com.android.settings.notification;
18
19import android.app.NotificationManager;
20import android.app.NotificationManager.Policy;
21import android.os.Bundle;
22import android.service.notification.ZenModeConfig;
23import android.support.v14.preference.SwitchPreference;
24import android.support.v7.preference.DropDownPreference;
25import android.support.v7.preference.Preference;
26import android.support.v7.preference.Preference.OnPreferenceChangeListener;
27import android.support.v7.preference.PreferenceScreen;
28import android.util.Log;
29
30import com.android.internal.logging.MetricsLogger;
31import com.android.internal.logging.MetricsProto.MetricsEvent;
32import com.android.settings.R;
33import com.android.settings.search.Indexable;
34
35public class ZenModePrioritySettings extends ZenModeSettingsBase implements Indexable {
36    private static final String KEY_REMINDERS = "reminders";
37    private static final String KEY_EVENTS = "events";
38    private static final String KEY_MESSAGES = "messages";
39    private static final String KEY_CALLS = "calls";
40    private static final String KEY_REPEAT_CALLERS = "repeat_callers";
41
42    private static final int SOURCE_NONE = -1;
43
44    private boolean mDisableListeners;
45    private SwitchPreference mReminders;
46    private SwitchPreference mEvents;
47    private DropDownPreference mMessages;
48    private DropDownPreference mCalls;
49    private SwitchPreference mRepeatCallers;
50
51    private Policy mPolicy;
52
53    @Override
54    public void onCreate(Bundle savedInstanceState) {
55        super.onCreate(savedInstanceState);
56        addPreferencesFromResource(R.xml.zen_mode_priority_settings);
57        final PreferenceScreen root = getPreferenceScreen();
58
59        mPolicy = NotificationManager.from(mContext).getNotificationPolicy();
60
61        mReminders = (SwitchPreference) root.findPreference(KEY_REMINDERS);
62        mReminders.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
63            @Override
64            public boolean onPreferenceChange(Preference preference, Object newValue) {
65                if (mDisableListeners) return true;
66                final boolean val = (Boolean) newValue;
67                MetricsLogger.action(mContext, MetricsEvent.ACTION_ZEN_ALLOW_REMINDERS, val);
68                if (DEBUG) Log.d(TAG, "onPrefChange allowReminders=" + val);
69                savePolicy(getNewPriorityCategories(val, Policy.PRIORITY_CATEGORY_REMINDERS),
70                        mPolicy.priorityCallSenders, mPolicy.priorityMessageSenders,
71                        mPolicy.suppressedVisualEffects);
72                return true;
73            }
74        });
75
76        mEvents = (SwitchPreference) root.findPreference(KEY_EVENTS);
77        mEvents.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
78            @Override
79            public boolean onPreferenceChange(Preference preference, Object newValue) {
80                if (mDisableListeners) return true;
81                final boolean val = (Boolean) newValue;
82                MetricsLogger.action(mContext, MetricsEvent.ACTION_ZEN_ALLOW_EVENTS, val);
83                if (DEBUG) Log.d(TAG, "onPrefChange allowEvents=" + val);
84                savePolicy(getNewPriorityCategories(val, Policy.PRIORITY_CATEGORY_EVENTS),
85                        mPolicy.priorityCallSenders, mPolicy.priorityMessageSenders,
86                        mPolicy.suppressedVisualEffects);
87                return true;
88            }
89        });
90
91        mMessages = (DropDownPreference) root.findPreference(KEY_MESSAGES);
92        addSources(mMessages);
93        mMessages.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
94            @Override
95            public boolean onPreferenceChange(Preference preference, Object newValue) {
96                if (mDisableListeners) return false;
97                final int val = Integer.parseInt((String) newValue);
98                final boolean allowMessages = val != SOURCE_NONE;
99                final int allowMessagesFrom =
100                        val == SOURCE_NONE ? mPolicy.priorityMessageSenders : val;
101                MetricsLogger.action(mContext, MetricsEvent.ACTION_ZEN_ALLOW_MESSAGES, val);
102                if (DEBUG) Log.d(TAG, "onPrefChange allowMessages=" + allowMessages
103                        + " allowMessagesFrom=" + ZenModeConfig.sourceToString(allowMessagesFrom));
104                savePolicy(
105                        getNewPriorityCategories(allowMessages, Policy.PRIORITY_CATEGORY_MESSAGES),
106                        mPolicy.priorityCallSenders, allowMessagesFrom,
107                        mPolicy.suppressedVisualEffects);
108                return true;
109            }
110        });
111
112        mCalls = (DropDownPreference) root.findPreference(KEY_CALLS);
113        addSources(mCalls);
114        mCalls.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
115            @Override
116            public boolean onPreferenceChange(Preference preference, Object newValue) {
117                if (mDisableListeners) return false;
118                final int val = Integer.parseInt((String) newValue);
119                final boolean allowCalls = val != SOURCE_NONE;
120                final int allowCallsFrom = val == SOURCE_NONE ? mPolicy.priorityCallSenders : val;
121                MetricsLogger.action(mContext, MetricsEvent.ACTION_ZEN_ALLOW_CALLS, val);
122                if (DEBUG) Log.d(TAG, "onPrefChange allowCalls=" + allowCalls
123                        + " allowCallsFrom=" + ZenModeConfig.sourceToString(allowCallsFrom));
124                savePolicy(getNewPriorityCategories(allowCalls, Policy.PRIORITY_CATEGORY_CALLS),
125                        allowCallsFrom, mPolicy.priorityMessageSenders,
126                        mPolicy.suppressedVisualEffects);
127                return true;
128            }
129        });
130
131        mRepeatCallers = (SwitchPreference) root.findPreference(KEY_REPEAT_CALLERS);
132        mRepeatCallers.setSummary(mContext.getString(R.string.zen_mode_repeat_callers_summary,
133                mContext.getResources().getInteger(com.android.internal.R.integer
134                        .config_zen_repeat_callers_threshold)));
135        mRepeatCallers.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
136            @Override
137            public boolean onPreferenceChange(Preference preference, Object newValue) {
138                if (mDisableListeners) return true;
139                final boolean val = (Boolean) newValue;
140                MetricsLogger.action(mContext, MetricsEvent.ACTION_ZEN_ALLOW_REPEAT_CALLS, val);
141                if (DEBUG) Log.d(TAG, "onPrefChange allowRepeatCallers=" + val);
142                int priorityCategories = getNewPriorityCategories(val,
143                        NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS);
144                savePolicy(priorityCategories, mPolicy.priorityCallSenders,
145                        mPolicy.priorityMessageSenders, mPolicy.suppressedVisualEffects);
146                return true;
147            }
148        });
149
150        updateControls();
151    }
152
153    @Override
154    protected void onZenModeChanged() {
155        // don't care
156    }
157
158    @Override
159    protected void onZenModeConfigChanged() {
160        mPolicy = NotificationManager.from(mContext).getNotificationPolicy();
161        updateControls();
162    }
163
164    private void updateControls() {
165        mDisableListeners = true;
166        if (mCalls != null) {
167            mCalls.setValue(Integer.toString(
168                    isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_CALLS)
169                            ? mPolicy.priorityCallSenders : SOURCE_NONE));
170        }
171        mMessages.setValue(Integer.toString(
172                isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_MESSAGES)
173                        ? mPolicy.priorityMessageSenders : SOURCE_NONE));
174        mReminders.setChecked(isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_REMINDERS));
175        mEvents.setChecked(isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_EVENTS));
176        mRepeatCallers.setChecked(
177                isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_REPEAT_CALLERS));
178        mRepeatCallers.setVisible(!isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_CALLS)
179                || mPolicy.priorityCallSenders != Policy.PRIORITY_SENDERS_ANY);
180        mDisableListeners = false;
181    }
182
183    @Override
184    protected int getMetricsCategory() {
185        return MetricsEvent.NOTIFICATION_ZEN_MODE_PRIORITY;
186    }
187
188    private static void addSources(DropDownPreference pref) {
189        pref.setEntries(new CharSequence[]{
190                pref.getContext().getString(R.string.zen_mode_from_anyone),
191                pref.getContext().getString(R.string.zen_mode_from_contacts),
192                pref.getContext().getString(R.string.zen_mode_from_starred),
193                pref.getContext().getString(R.string.zen_mode_from_none),
194        });
195        pref.setEntryValues(new CharSequence[] {
196                Integer.toString(Policy.PRIORITY_SENDERS_ANY),
197                Integer.toString(Policy.PRIORITY_SENDERS_CONTACTS),
198                Integer.toString(Policy.PRIORITY_SENDERS_STARRED),
199                Integer.toString(SOURCE_NONE),
200        });
201    }
202
203    private boolean isPriorityCategoryEnabled(int categoryType) {
204        return (mPolicy.priorityCategories & categoryType) != 0;
205    }
206
207    private int getNewPriorityCategories(boolean allow, int categoryType) {
208        int priorityCategories = mPolicy.priorityCategories;
209        if (allow) {
210            priorityCategories |= categoryType;
211        } else {
212            priorityCategories &= ~categoryType;
213        }
214        return priorityCategories;
215    }
216
217    private void savePolicy(int priorityCategories, int priorityCallSenders,
218            int priorityMessageSenders, int suppressedVisualEffects) {
219        mPolicy = new Policy(priorityCategories, priorityCallSenders, priorityMessageSenders,
220                suppressedVisualEffects);
221        NotificationManager.from(mContext).setNotificationPolicy(mPolicy);
222    }
223
224}
225