Use public APIs to update DND policies.

Bug: 22977552
Change-Id: I42d3da4052b9212505920b30bb2e24c3e56bc603
This commit is contained in:
Julia Reynolds
2015-09-28 08:57:03 -04:00
parent 39090f1cba
commit f02aa0cef4
4 changed files with 100 additions and 50 deletions

View File

@@ -82,6 +82,7 @@
<uses-permission android:name="android.permission.USER_ACTIVITY" /> <uses-permission android:name="android.permission.USER_ACTIVITY" />
<uses-permission android:name="android.permission.CHANGE_APP_IDLE_STATE" /> <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.PEERS_MAC_ADDRESS"/>
<uses-permission android:name="android.permission.MANAGE_NOTIFICATIONS"/>
<application android:label="@string/settings_label" <application android:label="@string/settings_label"
android:icon="@mipmap/ic_launcher_settings" android:icon="@mipmap/ic_launcher_settings"

View File

@@ -6301,6 +6301,9 @@
<!-- [CHAR LIMIT=20] Zen mode settings: Messages option --> <!-- [CHAR LIMIT=20] Zen mode settings: Messages option -->
<string name="zen_mode_messages">Messages</string> <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 --> <!-- [CHAR LIMIT=50] Zen mode settings: Selected messages summary -->
<string name="zen_mode_selected_messages">Selected messages</string> <string name="zen_mode_selected_messages">Selected messages</string>
@@ -6325,6 +6328,9 @@
<!-- [CHAR LIMIT=50] Zen mode settings: Events option --> <!-- [CHAR LIMIT=50] Zen mode settings: Events option -->
<string name="zen_mode_events">Events</string> <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 --> <!-- [CHAR LIMIT=50] Zen mode settings: Selected callers summary -->
<string name="zen_mode_selected_callers">Selected callers</string> <string name="zen_mode_selected_callers">Selected callers</string>

View File

@@ -16,6 +16,8 @@
package com.android.settings.notification; package com.android.settings.notification;
import android.app.NotificationManager;
import android.app.NotificationManager.Policy;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceChangeListener;
@@ -45,12 +47,16 @@ public class ZenModePrioritySettings extends ZenModeSettingsBase implements Inde
private DropDownPreference mCalls; private DropDownPreference mCalls;
private SwitchPreference mRepeatCallers; private SwitchPreference mRepeatCallers;
private Policy mPolicy;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.zen_mode_priority_settings); addPreferencesFromResource(R.xml.zen_mode_priority_settings);
final PreferenceScreen root = getPreferenceScreen(); final PreferenceScreen root = getPreferenceScreen();
mPolicy = NotificationManager.from(mContext).getNotificationPolicy();
mReminders = (SwitchPreference) root.findPreference(KEY_REMINDERS); mReminders = (SwitchPreference) root.findPreference(KEY_REMINDERS);
mReminders.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { mReminders.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override @Override
@@ -58,11 +64,10 @@ public class ZenModePrioritySettings extends ZenModeSettingsBase implements Inde
if (mDisableListeners) return true; if (mDisableListeners) return true;
final boolean val = (Boolean) newValue; final boolean val = (Boolean) newValue;
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_REMINDERS, val); MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_REMINDERS, val);
if (val == mConfig.allowReminders) return true;
if (DEBUG) Log.d(TAG, "onPrefChange allowReminders=" + val); if (DEBUG) Log.d(TAG, "onPrefChange allowReminders=" + val);
final ZenModeConfig newConfig = mConfig.copy(); savePolicy(getNewPriorityCategories(val, Policy.PRIORITY_CATEGORY_REMINDERS),
newConfig.allowReminders = val; mPolicy.priorityCallSenders, mPolicy.priorityMessageSenders);
return setZenModeConfig(newConfig); return true;
} }
}); });
@@ -73,11 +78,10 @@ public class ZenModePrioritySettings extends ZenModeSettingsBase implements Inde
if (mDisableListeners) return true; if (mDisableListeners) return true;
final boolean val = (Boolean) newValue; final boolean val = (Boolean) newValue;
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_EVENTS, val); MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_EVENTS, val);
if (val == mConfig.allowEvents) return true;
if (DEBUG) Log.d(TAG, "onPrefChange allowEvents=" + val); if (DEBUG) Log.d(TAG, "onPrefChange allowEvents=" + val);
final ZenModeConfig newConfig = mConfig.copy(); savePolicy(getNewPriorityCategories(val, Policy.PRIORITY_CATEGORY_EVENTS),
newConfig.allowEvents = val; mPolicy.priorityCallSenders, mPolicy.priorityMessageSenders);
return setZenModeConfig(newConfig); return true;
} }
}); });
@@ -88,19 +92,16 @@ public class ZenModePrioritySettings extends ZenModeSettingsBase implements Inde
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
if (mDisableListeners) return false; if (mDisableListeners) return false;
final int val = Integer.parseInt((String) newValue); final int val = Integer.parseInt((String) newValue);
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_MESSAGES, val);
final boolean allowMessages = val != SOURCE_NONE; final boolean allowMessages = val != SOURCE_NONE;
final int allowMessagesFrom = val == SOURCE_NONE ? mConfig.allowMessagesFrom : val; final int allowMessagesFrom =
if (allowMessages == mConfig.allowMessages val == SOURCE_NONE ? mPolicy.priorityMessageSenders : val;
&& allowMessagesFrom == mConfig.allowMessagesFrom) { MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_MESSAGES, val);
return false;
}
if (DEBUG) Log.d(TAG, "onPrefChange allowMessages=" + allowMessages if (DEBUG) Log.d(TAG, "onPrefChange allowMessages=" + allowMessages
+ " allowMessagesFrom=" + ZenModeConfig.sourceToString(allowMessagesFrom)); + " allowMessagesFrom=" + ZenModeConfig.sourceToString(allowMessagesFrom));
final ZenModeConfig newConfig = mConfig.copy(); savePolicy(
newConfig.allowMessages = allowMessages; getNewPriorityCategories(allowMessages, Policy.PRIORITY_CATEGORY_MESSAGES),
newConfig.allowMessagesFrom = allowMessagesFrom; mPolicy.priorityCallSenders, allowMessagesFrom);
return setZenModeConfig(newConfig); return true;
} }
}); });
@@ -111,19 +112,14 @@ public class ZenModePrioritySettings extends ZenModeSettingsBase implements Inde
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
if (mDisableListeners) return false; if (mDisableListeners) return false;
final int val = Integer.parseInt((String) newValue); final int val = Integer.parseInt((String) newValue);
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_CALLS, val);
final boolean allowCalls = val != SOURCE_NONE; final boolean allowCalls = val != SOURCE_NONE;
final int allowCallsFrom = val == SOURCE_NONE ? mConfig.allowCallsFrom : val; final int allowCallsFrom = val == SOURCE_NONE ? mPolicy.priorityCallSenders : val;
if (allowCalls == mConfig.allowCalls MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_CALLS, val);
&& allowCallsFrom == mConfig.allowCallsFrom) {
return false;
}
if (DEBUG) Log.d(TAG, "onPrefChange allowCalls=" + allowCalls if (DEBUG) Log.d(TAG, "onPrefChange allowCalls=" + allowCalls
+ " allowCallsFrom=" + ZenModeConfig.sourceToString(allowCallsFrom)); + " allowCallsFrom=" + ZenModeConfig.sourceToString(allowCallsFrom));
final ZenModeConfig newConfig = mConfig.copy(); savePolicy(getNewPriorityCategories(allowCalls, Policy.PRIORITY_CATEGORY_CALLS),
newConfig.allowCalls = allowCalls; allowCallsFrom, mPolicy.priorityMessageSenders);
newConfig.allowCallsFrom = allowCallsFrom; return true;
return !setZenModeConfig(newConfig);
} }
}); });
@@ -137,11 +133,12 @@ public class ZenModePrioritySettings extends ZenModeSettingsBase implements Inde
if (mDisableListeners) return true; if (mDisableListeners) return true;
final boolean val = (Boolean) newValue; final boolean val = (Boolean) newValue;
MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_REPEAT_CALLS, val); MetricsLogger.action(mContext, MetricsLogger.ACTION_ZEN_ALLOW_REPEAT_CALLS, val);
if (val == mConfig.allowRepeatCallers) return true;
if (DEBUG) Log.d(TAG, "onPrefChange allowRepeatCallers=" + val); if (DEBUG) Log.d(TAG, "onPrefChange allowRepeatCallers=" + val);
final ZenModeConfig newConfig = mConfig.copy(); int priorityCategories = getNewPriorityCategories(val,
newConfig.allowRepeatCallers = val; NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS);
return setZenModeConfig(newConfig); savePolicy(priorityCategories, mPolicy.priorityCallSenders,
mPolicy.priorityMessageSenders);
return true;
} }
}); });
@@ -155,22 +152,26 @@ public class ZenModePrioritySettings extends ZenModeSettingsBase implements Inde
@Override @Override
protected void onZenModeConfigChanged() { protected void onZenModeConfigChanged() {
mPolicy = NotificationManager.from(mContext).getNotificationPolicy();
updateControls(); updateControls();
} }
private void updateControls() { private void updateControls() {
mDisableListeners = true; mDisableListeners = true;
if (mCalls != null) { if (mCalls != null) {
mCalls.setValue(Integer.toString(mConfig.allowCalls ? mConfig.allowCallsFrom mCalls.setValue(Integer.toString(
: SOURCE_NONE)); isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_CALLS)
? mPolicy.priorityCallSenders : SOURCE_NONE));
} }
mMessages.setValue(Integer.toString(mConfig.allowMessages ? mConfig.allowMessagesFrom mMessages.setValue(Integer.toString(
: SOURCE_NONE)); isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_MESSAGES)
mReminders.setChecked(mConfig.allowReminders); ? mPolicy.priorityMessageSenders : SOURCE_NONE));
mEvents.setChecked(mConfig.allowEvents); mReminders.setChecked(isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_REMINDERS));
mRepeatCallers.setChecked(mConfig.allowRepeatCallers); mEvents.setChecked(isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_EVENTS));
mRepeatCallers.setEnabled(!mConfig.allowCalls mRepeatCallers.setChecked(
|| mConfig.allowCallsFrom != ZenModeConfig.SOURCE_ANYONE); isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_REPEAT_CALLERS));
mRepeatCallers.setEnabled(!isPriorityCategoryEnabled(Policy.PRIORITY_CATEGORY_CALLS)
|| mPolicy.priorityCallSenders != Policy.PRIORITY_SENDERS_ANY);
mDisableListeners = false; mDisableListeners = false;
} }
@@ -180,18 +181,38 @@ public class ZenModePrioritySettings extends ZenModeSettingsBase implements Inde
} }
private static void addSources(DropDownPreference pref) { 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_anyone),
pref.getContext().getString(R.string.zen_mode_from_contacts), 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_starred),
pref.getContext().getString(R.string.zen_mode_from_none), pref.getContext().getString(R.string.zen_mode_from_none),
}); });
pref.setEntryValues(new CharSequence[] { pref.setEntryValues(new CharSequence[] {
Integer.toString(ZenModeConfig.SOURCE_ANYONE), Integer.toString(Policy.PRIORITY_SENDERS_ANY),
Integer.toString(ZenModeConfig.SOURCE_CONTACT), Integer.toString(Policy.PRIORITY_SENDERS_CONTACTS),
Integer.toString(ZenModeConfig.SOURCE_STAR), Integer.toString(Policy.PRIORITY_SENDERS_STARRED),
Integer.toString(SOURCE_NONE), 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);
}
} }

View File

@@ -16,6 +16,8 @@
package com.android.settings.notification; package com.android.settings.notification;
import android.app.NotificationManager;
import android.app.NotificationManager.Policy;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
@@ -77,15 +79,35 @@ public class ZenModeSettings extends ZenModeSettingsBase implements Indexable {
} }
private void updatePrioritySettingsSummary() { 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); String s = getResources().getString(R.string.zen_mode_alarms);
s = appendLowercase(s, mConfig.allowReminders, R.string.zen_mode_reminders); s = appendLowercase(s, isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_REMINDERS),
s = appendLowercase(s, mConfig.allowEvents, R.string.zen_mode_events); R.string.zen_mode_reminders);
s = appendLowercase(s, callers, R.string.zen_mode_selected_callers); s = appendLowercase(s, isCategoryEnabled(policy, Policy.PRIORITY_CATEGORY_EVENTS),
s = appendLowercase(s, mConfig.allowMessages, R.string.zen_mode_selected_messages); 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); mPrioritySettings.setSummary(s);
} }
private boolean isCategoryEnabled(Policy policy, int categoryType) {
return (policy.priorityCategories & categoryType) != 0;
}
private String appendLowercase(String s, boolean condition, int resId) { private String appendLowercase(String s, boolean condition, int resId) {
if (condition) { if (condition) {
return getResources().getString(R.string.join_many_items_middle, s, return getResources().getString(R.string.join_many_items_middle, s,