Use public APIs to update DND policies.
Bug: 22977552
Change-Id: I42d3da4052b9212505920b30bb2e24c3e56bc603
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 34898f5..3e69244 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -82,6 +82,7 @@
<uses-permission android:name="android.permission.USER_ACTIVITY" />
<uses-permission android:name="android.permission.CHANGE_APP_IDLE_STATE" />
<uses-permission android:name="android.permission.PEERS_MAC_ADDRESS"/>
+ <uses-permission android:name="android.permission.MANAGE_NOTIFICATIONS"/>
<application android:label="@string/settings_label"
android:icon="@mipmap/ic_launcher_settings"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 62f349b..e638de5 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -6301,6 +6301,9 @@
<!-- [CHAR LIMIT=20] Zen mode settings: Messages option -->
<string name="zen_mode_messages">Messages</string>
+ <!-- [CHAR LIMIT=50] Zen mode settings: All messages summary -->
+ <string name="zen_mode_all_messages">All messages</string>
+
<!-- [CHAR LIMIT=50] Zen mode settings: Selected messages summary -->
<string name="zen_mode_selected_messages">Selected messages</string>
@@ -6325,6 +6328,9 @@
<!-- [CHAR LIMIT=50] Zen mode settings: Events option -->
<string name="zen_mode_events">Events</string>
+ <!-- [CHAR LIMIT=50] Zen mode settings: All callers summary -->
+ <string name="zen_mode_all_callers">All callers</string>
+
<!-- [CHAR LIMIT=50] Zen mode settings: Selected callers summary -->
<string name="zen_mode_selected_callers">Selected callers</string>
diff --git a/src/com/android/settings/notification/ZenModePrioritySettings.java b/src/com/android/settings/notification/ZenModePrioritySettings.java
index 12faaca..be8e454 100644
--- a/src/com/android/settings/notification/ZenModePrioritySettings.java
+++ b/src/com/android/settings/notification/ZenModePrioritySettings.java
@@ -16,6 +16,8 @@
package com.android.settings.notification;
+import android.app.NotificationManager;
+import android.app.NotificationManager.Policy;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
@@ -45,12 +47,16 @@
private DropDownPreference mCalls;
private SwitchPreference mRepeatCallers;
+ private Policy mPolicy;
+
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.zen_mode_priority_settings);
final PreferenceScreen root = getPreferenceScreen();
+ mPolicy = NotificationManager.from(mContext).getNotificationPolicy();
+
mReminders = (SwitchPreference) root.findPreference(KEY_REMINDERS);
mReminders.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
@@ -58,11 +64,10 @@
if (mDisableListeners) return true;
final boolean val = (Boolean) newValue;
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_REMINDERS, val);
- if (val == mConfig.allowReminders) return true;
if (DEBUG) Log.d(TAG, "onPrefChange allowReminders=" + val);
- final ZenModeConfig newConfig = mConfig.copy();
- newConfig.allowReminders = val;
- return setZenModeConfig(newConfig);
+ savePolicy(getNewPriorityCategories(val, Policy.PRIORITY_CATEGORY_REMINDERS),
+ mPolicy.priorityCallSenders, mPolicy.priorityMessageSenders);
+ return true;
}
});
@@ -73,11 +78,10 @@
if (mDisableListeners) return true;
final boolean val = (Boolean) newValue;
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_EVENTS, val);
- if (val == mConfig.allowEvents) return true;
if (DEBUG) Log.d(TAG, "onPrefChange allowEvents=" + val);
- final ZenModeConfig newConfig = mConfig.copy();
- newConfig.allowEvents = val;
- return setZenModeConfig(newConfig);
+ savePolicy(getNewPriorityCategories(val, Policy.PRIORITY_CATEGORY_EVENTS),
+ mPolicy.priorityCallSenders, mPolicy.priorityMessageSenders);
+ return true;
}
});
@@ -88,19 +92,16 @@
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (mDisableListeners) return false;
final int val = Integer.parseInt((String) newValue);
- MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_MESSAGES, val);
final boolean allowMessages = val != SOURCE_NONE;
- final int allowMessagesFrom = val == SOURCE_NONE ? mConfig.allowMessagesFrom : val;
- if (allowMessages == mConfig.allowMessages
- && allowMessagesFrom == mConfig.allowMessagesFrom) {
- return false;
- }
+ final int allowMessagesFrom =
+ val == SOURCE_NONE ? mPolicy.priorityMessageSenders : val;
+ MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_MESSAGES, val);
if (DEBUG) Log.d(TAG, "onPrefChange allowMessages=" + allowMessages
+ " allowMessagesFrom=" + ZenModeConfig.sourceToString(allowMessagesFrom));
- final ZenModeConfig newConfig = mConfig.copy();
- newConfig.allowMessages = allowMessages;
- newConfig.allowMessagesFrom = allowMessagesFrom;
- return setZenModeConfig(newConfig);
+ savePolicy(
+ getNewPriorityCategories(allowMessages, Policy.PRIORITY_CATEGORY_MESSAGES),
+ mPolicy.priorityCallSenders, allowMessagesFrom);
+ return true;
}
});
@@ -111,19 +112,14 @@
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (mDisableListeners) return false;
final int val = Integer.parseInt((String) newValue);
- MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_CALLS, val);
final boolean allowCalls = val != SOURCE_NONE;
- final int allowCallsFrom = val == SOURCE_NONE ? mConfig.allowCallsFrom : val;
- if (allowCalls == mConfig.allowCalls
- && allowCallsFrom == mConfig.allowCallsFrom) {
- return false;
- }
+ final int allowCallsFrom = val == SOURCE_NONE ? mPolicy.priorityCallSenders : val;
+ MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_CALLS, val);
if (DEBUG) Log.d(TAG, "onPrefChange allowCalls=" + allowCalls
+ " allowCallsFrom=" + ZenModeConfig.sourceToString(allowCallsFrom));
- final ZenModeConfig newConfig = mConfig.copy();
- newConfig.allowCalls = allowCalls;
- newConfig.allowCallsFrom = allowCallsFrom;
- return !setZenModeConfig(newConfig);
+ savePolicy(getNewPriorityCategories(allowCalls, Policy.PRIORITY_CATEGORY_CALLS),
+ allowCallsFrom, mPolicy.priorityMessageSenders);
+ return true;
}
});
@@ -137,11 +133,12 @@
if (mDisableListeners) return true;
final boolean val = (Boolean) newValue;
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_REPEAT_CALLS, val);
- if (val == mConfig.allowRepeatCallers) return true;
if (DEBUG) Log.d(TAG, "onPrefChange allowRepeatCallers=" + val);
- final ZenModeConfig newConfig = mConfig.copy();
- newConfig.allowRepeatCallers = val;
- return setZenModeConfig(newConfig);
+ int priorityCategories = getNewPriorityCategories(val,
+ NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS);
+ savePolicy(priorityCategories, mPolicy.priorityCallSenders,
+ mPolicy.priorityMessageSenders);
+ return true;
}
});
@@ -155,22 +152,26 @@
@Override
protected void onZenModeConfigChanged() {
+ mPolicy = NotificationManager.from(mContext).getNotificationPolicy();
updateControls();
}
private void updateControls() {
mDisableListeners = true;
if (mCalls != null) {
- mCalls.setValue(Integer.toString(mConfig.allowCalls ? mConfig.allowCallsFrom
- : SOURCE_NONE));
+ mCalls.setValue(Integer.toString(
+ isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_CALLS)
+ ? mPolicy.priorityCallSenders : SOURCE_NONE));
}
- mMessages.setValue(Integer.toString(mConfig.allowMessages ? mConfig.allowMessagesFrom
- : SOURCE_NONE));
- mReminders.setChecked(mConfig.allowReminders);
- mEvents.setChecked(mConfig.allowEvents);
- mRepeatCallers.setChecked(mConfig.allowRepeatCallers);
- mRepeatCallers.setEnabled(!mConfig.allowCalls
- || mConfig.allowCallsFrom != ZenModeConfig.SOURCE_ANYONE);
+ mMessages.setValue(Integer.toString(
+ isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_MESSAGES)
+ ? mPolicy.priorityMessageSenders : SOURCE_NONE));
+ mReminders.setChecked(isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_REMINDERS));
+ mEvents.setChecked(isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_EVENTS));
+ mRepeatCallers.setChecked(
+ isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_REPEAT_CALLERS));
+ mRepeatCallers.setEnabled(!isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_CALLS)
+ || mPolicy.priorityCallSenders != Policy.PRIORITY_SENDERS_ANY);
mDisableListeners = false;
}
@@ -180,18 +181,38 @@
}
private static void addSources(DropDownPreference pref) {
- pref.setEntryValues(new CharSequence[] {
+ pref.setEntries(new CharSequence[]{
pref.getContext().getString(R.string.zen_mode_from_anyone),
pref.getContext().getString(R.string.zen_mode_from_contacts),
pref.getContext().getString(R.string.zen_mode_from_starred),
pref.getContext().getString(R.string.zen_mode_from_none),
});
pref.setEntryValues(new CharSequence[] {
- Integer.toString(ZenModeConfig.SOURCE_ANYONE),
- Integer.toString(ZenModeConfig.SOURCE_CONTACT),
- Integer.toString(ZenModeConfig.SOURCE_STAR),
+ Integer.toString(Policy.PRIORITY_SENDERS_ANY),
+ Integer.toString(Policy.PRIORITY_SENDERS_CONTACTS),
+ Integer.toString(Policy.PRIORITY_SENDERS_STARRED),
Integer.toString(SOURCE_NONE),
});
}
+ private boolean isPriorityCategoryEnabled(int categoryType) {
+ return (mPolicy.priorityCategories & categoryType) != 0;
+ }
+
+ private int getNewPriorityCategories(boolean allow, int categoryType) {
+ int priorityCategories = mPolicy.priorityCategories;
+ if (allow) {
+ priorityCategories |= categoryType;
+ } else {
+ priorityCategories &= ~categoryType;
+ }
+ return priorityCategories;
+ }
+
+ private void savePolicy(int priorityCategories, int priorityCallSenders,
+ int priorityMessageSenders) {
+ mPolicy = new Policy(priorityCategories, priorityCallSenders, priorityMessageSenders);
+ NotificationManager.from(mContext).setNotificationPolicy(mPolicy);
+ }
+
}
diff --git a/src/com/android/settings/notification/ZenModeSettings.java b/src/com/android/settings/notification/ZenModeSettings.java
index f76ee38..3f052b9 100644
--- a/src/com/android/settings/notification/ZenModeSettings.java
+++ b/src/com/android/settings/notification/ZenModeSettings.java
@@ -16,6 +16,8 @@
package com.android.settings.notification;
+import android.app.NotificationManager;
+import android.app.NotificationManager.Policy;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
@@ -77,15 +79,35 @@
}
private void updatePrioritySettingsSummary() {
- final boolean callers = mConfig.allowCalls || mConfig.allowRepeatCallers;
+ Policy policy = NotificationManager.from(mContext).getNotificationPolicy();
String s = getResources().getString(R.string.zen_mode_alarms);
- s = appendLowercase(s, mConfig.allowReminders, R.string.zen_mode_reminders);
- s = appendLowercase(s, mConfig.allowEvents, R.string.zen_mode_events);
- s = appendLowercase(s, callers, R.string.zen_mode_selected_callers);
- s = appendLowercase(s, mConfig.allowMessages, R.string.zen_mode_selected_messages);
+ s = appendLowercase(s, isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_REMINDERS),
+ R.string.zen_mode_reminders);
+ s = appendLowercase(s, isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_EVENTS),
+ R.string.zen_mode_events);
+ if (isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_MESSAGES)) {
+ if (policy.priorityMessageSenders == Policy.PRIORITY_SENDERS_ANY) {
+ s = appendLowercase(s, true, R.string.zen_mode_all_messages);
+ } else {
+ s = appendLowercase(s, true, R.string.zen_mode_selected_messages);
+ }
+ }
+ if (isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_CALLS)) {
+ if (policy.priorityCallSenders == Policy.PRIORITY_SENDERS_ANY) {
+ s = appendLowercase(s, true, R.string.zen_mode_all_callers);
+ } else {
+ s = appendLowercase(s, true, R.string.zen_mode_selected_callers);
+ }
+ } else if (isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_REPEAT_CALLERS)) {
+ s = appendLowercase(s, true, R.string.zen_mode_repeat_callers);
+ }
mPrioritySettings.setSummary(s);
}
+ private boolean isCategoryEnabled(Policy policy, int categoryType) {
+ return (policy.priorityCategories & categoryType) != 0;
+ }
+
private String appendLowercase(String s, boolean condition, int resId) {
if (condition) {
return getResources().getString(R.string.join_many_items_middle, s,