Change zen defaults for upgrade and new users

am: 1f58057118

Change-Id: Iae6ec3f9535200a13702b4868224cda9d2ba8145
This commit is contained in:
Julia Reynolds
2018-05-01 10:39:01 -07:00
committed by android-build-merger
6 changed files with 157 additions and 70 deletions

View File

@@ -16,6 +16,10 @@
package android.service.notification;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.NotificationManager;
@@ -68,6 +72,7 @@ public class ZenModeConfig implements Parcelable {
public static final int SOURCE_STAR = 2;
public static final int MAX_SOURCE = SOURCE_STAR;
private static final int DEFAULT_SOURCE = SOURCE_CONTACT;
private static final int DEFAULT_CALLS_SOURCE = SOURCE_STAR;
public static final String EVENTS_DEFAULT_RULE_ID = "EVENTS_DEFAULT_RULE";
public static final String EVERY_NIGHT_DEFAULT_RULE_ID = "EVERY_NIGHT_DEFAULT_RULE";
@@ -93,13 +98,10 @@ public class ZenModeConfig implements Parcelable {
private static final boolean DEFAULT_ALLOW_REMINDERS = false;
private static final boolean DEFAULT_ALLOW_EVENTS = false;
private static final boolean DEFAULT_ALLOW_REPEAT_CALLERS = false;
private static final boolean DEFAULT_ALLOW_SCREEN_OFF = false;
private static final boolean DEFAULT_ALLOW_SCREEN_ON = false;
private static final boolean DEFAULT_CHANNELS_BYPASSING_DND = false;
private static final int DEFAULT_SUPPRESSED_VISUAL_EFFECTS =
Policy.getAllSuppressedVisualEffects();
private static final int DEFAULT_SUPPRESSED_VISUAL_EFFECTS = 0;
public static final int XML_VERSION = 7;
public static final int XML_VERSION = 8;
public static final String ZEN_TAG = "zen";
private static final String ZEN_ATT_VERSION = "version";
private static final String ZEN_ATT_USER = "user";
@@ -151,12 +153,10 @@ public class ZenModeConfig implements Parcelable {
public boolean allowMessages = DEFAULT_ALLOW_MESSAGES;
public boolean allowReminders = DEFAULT_ALLOW_REMINDERS;
public boolean allowEvents = DEFAULT_ALLOW_EVENTS;
public int allowCallsFrom = DEFAULT_SOURCE;
public int allowCallsFrom = DEFAULT_CALLS_SOURCE;
public int allowMessagesFrom = DEFAULT_SOURCE;
public int user = UserHandle.USER_SYSTEM;
public int suppressedVisualEffects = DEFAULT_SUPPRESSED_VISUAL_EFFECTS;
public boolean allowWhenScreenOff = DEFAULT_ALLOW_SCREEN_OFF;
public boolean allowWhenScreenOn = DEFAULT_ALLOW_SCREEN_ON;
public boolean areChannelsBypassingDnd = DEFAULT_CHANNELS_BYPASSING_DND;
public int version;
@@ -185,8 +185,6 @@ public class ZenModeConfig implements Parcelable {
automaticRules.put(ids[i], rules[i]);
}
}
allowWhenScreenOff = source.readInt() == 1;
allowWhenScreenOn = source.readInt() == 1;
allowAlarms = source.readInt() == 1;
allowMedia = source.readInt() == 1;
allowSystem = source.readInt() == 1;
@@ -219,8 +217,6 @@ public class ZenModeConfig implements Parcelable {
} else {
dest.writeInt(0);
}
dest.writeInt(allowWhenScreenOff ? 1 : 0);
dest.writeInt(allowWhenScreenOn ? 1 : 0);
dest.writeInt(allowAlarms ? 1 : 0);
dest.writeInt(allowMedia ? 1 : 0);
dest.writeInt(allowSystem ? 1 : 0);
@@ -242,8 +238,6 @@ public class ZenModeConfig implements Parcelable {
.append(",allowMessages=").append(allowMessages)
.append(",allowCallsFrom=").append(sourceToString(allowCallsFrom))
.append(",allowMessagesFrom=").append(sourceToString(allowMessagesFrom))
.append(",allowWhenScreenOff=").append(allowWhenScreenOff)
.append(",allowWhenScreenOn=").append(allowWhenScreenOn)
.append(",suppressedVisualEffects=").append(suppressedVisualEffects)
.append(",areChannelsBypassingDnd=").append(areChannelsBypassingDnd)
.append(",automaticRules=").append(automaticRules)
@@ -289,12 +283,6 @@ public class ZenModeConfig implements Parcelable {
if (allowMessagesFrom != to.allowMessagesFrom) {
d.addLine("allowMessagesFrom", allowMessagesFrom, to.allowMessagesFrom);
}
if (allowWhenScreenOff != to.allowWhenScreenOff) {
d.addLine("allowWhenScreenOff", allowWhenScreenOff, to.allowWhenScreenOff);
}
if (allowWhenScreenOn != to.allowWhenScreenOn) {
d.addLine("allowWhenScreenOn", allowWhenScreenOn, to.allowWhenScreenOn);
}
if (suppressedVisualEffects != to.suppressedVisualEffects) {
d.addLine("suppressedVisualEffects", suppressedVisualEffects,
to.suppressedVisualEffects);
@@ -404,8 +392,6 @@ public class ZenModeConfig implements Parcelable {
&& other.allowMessagesFrom == allowMessagesFrom
&& other.allowReminders == allowReminders
&& other.allowEvents == allowEvents
&& other.allowWhenScreenOff == allowWhenScreenOff
&& other.allowWhenScreenOn == allowWhenScreenOn
&& other.user == user
&& Objects.equals(other.automaticRules, automaticRules)
&& Objects.equals(other.manualRule, manualRule)
@@ -418,7 +404,7 @@ public class ZenModeConfig implements Parcelable {
return Objects.hash(allowAlarms, allowMedia, allowSystem, allowCalls,
allowRepeatCallers, allowMessages,
allowCallsFrom, allowMessagesFrom, allowReminders, allowEvents,
allowWhenScreenOff, allowWhenScreenOn, user, automaticRules, manualRule,
user, automaticRules, manualRule,
suppressedVisualEffects, areChannelsBypassingDnd);
}
@@ -472,6 +458,7 @@ public class ZenModeConfig implements Parcelable {
final ZenModeConfig rt = new ZenModeConfig();
rt.version = safeInt(parser, ZEN_ATT_VERSION, XML_VERSION);
rt.user = safeInt(parser, ZEN_ATT_USER, rt.user);
boolean readSuppressedEffects = false;
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
tag = parser.getName();
if (type == XmlPullParser.END_TAG && ZEN_TAG.equals(tag)) {
@@ -502,17 +489,33 @@ public class ZenModeConfig implements Parcelable {
rt.allowCallsFrom = DEFAULT_SOURCE;
rt.allowMessagesFrom = DEFAULT_SOURCE;
}
// continue to read even though we now have suppressedVisualEffects, in case
// we need to revert to users' previous settings
rt.allowWhenScreenOff =
safeBoolean(parser, ALLOW_ATT_SCREEN_OFF, DEFAULT_ALLOW_SCREEN_OFF);
rt.allowWhenScreenOn =
safeBoolean(parser, ALLOW_ATT_SCREEN_ON, DEFAULT_ALLOW_SCREEN_ON);
rt.allowAlarms = safeBoolean(parser, ALLOW_ATT_ALARMS, DEFAULT_ALLOW_ALARMS);
rt.allowMedia = safeBoolean(parser, ALLOW_ATT_MEDIA,
DEFAULT_ALLOW_MEDIA);
rt.allowSystem = safeBoolean(parser, ALLOW_ATT_SYSTEM, DEFAULT_ALLOW_SYSTEM);
} else if (DISALLOW_TAG.equals(tag)) {
// migrate old suppressed visual effects fields, if they still exist in the xml
Boolean allowWhenScreenOff = unsafeBoolean(parser, ALLOW_ATT_SCREEN_OFF);
if (allowWhenScreenOff != null) {
readSuppressedEffects = true;
if (allowWhenScreenOff) {
rt.suppressedVisualEffects |= SUPPRESSED_EFFECT_LIGHTS
| SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
}
}
Boolean allowWhenScreenOn = unsafeBoolean(parser, ALLOW_ATT_SCREEN_ON);
if (allowWhenScreenOn != null) {
readSuppressedEffects = true;
if (allowWhenScreenOn) {
rt.suppressedVisualEffects |= SUPPRESSED_EFFECT_PEEK;
}
}
if (readSuppressedEffects) {
Slog.d(TAG, "Migrated visual effects to " + rt.suppressedVisualEffects);
}
} else if (DISALLOW_TAG.equals(tag) && !readSuppressedEffects) {
// only read from suppressed visual effects field if we haven't just migrated
// the values from allowOn/allowOff, lest we wipe out those settings
rt.suppressedVisualEffects = safeInt(parser, DISALLOW_ATT_VISUAL_EFFECTS,
DEFAULT_SUPPRESSED_VISUAL_EFFECTS);
} else if (MANUAL_TAG.equals(tag)) {
@@ -552,8 +555,6 @@ public class ZenModeConfig implements Parcelable {
out.attribute(null, ALLOW_ATT_EVENTS, Boolean.toString(allowEvents));
out.attribute(null, ALLOW_ATT_CALLS_FROM, Integer.toString(allowCallsFrom));
out.attribute(null, ALLOW_ATT_MESSAGES_FROM, Integer.toString(allowMessagesFrom));
out.attribute(null, ALLOW_ATT_SCREEN_OFF, Boolean.toString(allowWhenScreenOff));
out.attribute(null, ALLOW_ATT_SCREEN_ON, Boolean.toString(allowWhenScreenOn));
out.attribute(null, ALLOW_ATT_ALARMS, Boolean.toString(allowAlarms));
out.attribute(null, ALLOW_ATT_MEDIA, Boolean.toString(allowMedia));
out.attribute(null, ALLOW_ATT_SYSTEM, Boolean.toString(allowSystem));
@@ -673,6 +674,12 @@ public class ZenModeConfig implements Parcelable {
return source >= SOURCE_ANYONE && source <= MAX_SOURCE;
}
private static Boolean unsafeBoolean(XmlPullParser parser, String att) {
final String val = parser.getAttributeValue(null, att);
if (TextUtils.isEmpty(val)) return null;
return Boolean.parseBoolean(val);
}
private static boolean safeBoolean(XmlPullParser parser, String att, boolean defValue) {
final String val = parser.getAttributeValue(null, att);
return safeBoolean(val, defValue);

View File

@@ -19,7 +19,7 @@
<!-- Default configuration for zen mode. See android.service.notification.ZenModeConfig. -->
<zen version="7">
<allow alarms="true" media="true" system="false" calls="false" messages="false"
<allow alarms="true" media="true" system="false" calls="false" callsFrom="2" messages="false"
reminders="false" events="false" />
<!-- all visual effects that exist as of P -->

View File

@@ -3878,7 +3878,9 @@ public class NotificationManagerService extends SystemService {
for (int j = 0; j < listenerSize; j++) {
if (i > 0) pw.print(',');
final ManagedServiceInfo listener = listeners.valueAt(i);
pw.print(listener.component);
if (listener != null) {
pw.print(listener.component);
}
}
}
pw.println(')');

View File

@@ -173,18 +173,6 @@ public class ZenModeHelper {
}
}
public boolean shouldSuppressWhenScreenOff() {
synchronized (mConfig) {
return !mConfig.allowWhenScreenOff;
}
}
public boolean shouldSuppressWhenScreenOn() {
synchronized (mConfig) {
return !mConfig.allowWhenScreenOn;
}
}
public void addCallback(Callback callback) {
mCallbacks.add(callback);
}
@@ -592,14 +580,12 @@ public class ZenModeHelper {
return;
}
pw.printf("allow(alarms=%b,media=%b,system=%b,calls=%b,callsFrom=%s,repeatCallers=%b,"
+ "messages=%b,messagesFrom=%s,"
+ "events=%b,reminders=%b,whenScreenOff=%b,whenScreenOn=%b)\n",
+ "messages=%b,messagesFrom=%s,events=%b,reminders=%b)\n",
config.allowAlarms, config.allowMedia, config.allowSystem,
config.allowCalls, ZenModeConfig.sourceToString(config.allowCallsFrom),
config.allowRepeatCallers, config.allowMessages,
ZenModeConfig.sourceToString(config.allowMessagesFrom),
config.allowEvents, config.allowReminders, config.allowWhenScreenOff,
config.allowWhenScreenOn);
config.allowEvents, config.allowReminders);
pw.printf(" disallow(visualEffects=%s)\n", config.suppressedVisualEffects);
pw.print(prefix); pw.print(" manualRule="); pw.println(config.manualRule);
if (config.automaticRules.isEmpty()) return;

View File

@@ -2188,7 +2188,6 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
@Test
public void testReadPolicyXml_readApprovedServicesFromXml() throws Exception {
final String upgradeXml = "<notification-policy version=\"1\">"
+ "<zen></zen>"
+ "<ranking></ranking>"
+ "<enabled_listeners>"
+ "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />"
@@ -2216,7 +2215,6 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
@Test
public void testReadPolicyXml_readApprovedServicesFromSettings() throws Exception {
final String preupgradeXml = "<notification-policy version=\"1\">"
+ "<zen></zen>"
+ "<ranking></ranking>"
+ "</notification-policy>";
mService.readPolicyXml(

View File

@@ -17,6 +17,9 @@
package com.android.server.notification;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertEquals;
@@ -569,8 +572,6 @@ public class ZenModeHelperTest extends UiServiceTestCase {
mZenModeHelperSpy.mConfig.allowMessages = true;
mZenModeHelperSpy.mConfig.allowEvents = true;
mZenModeHelperSpy.mConfig.allowRepeatCallers= true;
mZenModeHelperSpy.mConfig.allowWhenScreenOff = true;
mZenModeHelperSpy.mConfig.allowWhenScreenOn = true;
mZenModeHelperSpy.mConfig.suppressedVisualEffects = SUPPRESSED_EFFECT_BADGE;
mZenModeHelperSpy.mConfig.manualRule = new ZenModeConfig.ZenRule();
mZenModeHelperSpy.mConfig.manualRule.component = new ComponentName("a", "a");
@@ -593,8 +594,6 @@ public class ZenModeHelperTest extends UiServiceTestCase {
mZenModeHelperSpy.mConfig.allowMessages = true;
mZenModeHelperSpy.mConfig.allowEvents = true;
mZenModeHelperSpy.mConfig.allowRepeatCallers= true;
mZenModeHelperSpy.mConfig.allowWhenScreenOff = true;
mZenModeHelperSpy.mConfig.allowWhenScreenOn = true;
mZenModeHelperSpy.mConfig.suppressedVisualEffects = SUPPRESSED_EFFECT_BADGE;
mZenModeHelperSpy.mConfig.manualRule = new ZenModeConfig.ZenRule();
mZenModeHelperSpy.mConfig.manualRule.zenMode =
@@ -644,6 +643,115 @@ public class ZenModeHelperTest extends UiServiceTestCase {
setupZenConfigMaintained();
}
@Test
public void testMigrateSuppressedVisualEffects_oneExistsButOff() throws Exception {
String xml = "<zen version=\"6\" user=\"0\">\n"
+ "<allow calls=\"false\" repeatCallers=\"false\" messages=\"true\" "
+ "reminders=\"false\" events=\"false\" callsFrom=\"1\" messagesFrom=\"2\" "
+ "visualScreenOff=\"false\" alarms=\"true\" "
+ "media=\"true\" system=\"false\" />\n"
+ "<disallow visualEffects=\"511\" />"
+ "</zen>";
XmlPullParser parser = Xml.newPullParser();
parser.setInput(new BufferedInputStream(
new ByteArrayInputStream(xml.getBytes())), null);
parser.nextTag();
mZenModeHelperSpy.readXml(parser, false);
assertEquals(0, mZenModeHelperSpy.mConfig.suppressedVisualEffects);
xml = "<zen version=\"6\" user=\"0\">\n"
+ "<allow calls=\"false\" repeatCallers=\"false\" messages=\"true\" "
+ "reminders=\"false\" events=\"false\" callsFrom=\"1\" messagesFrom=\"2\" "
+ "visualScreenOn=\"false\" alarms=\"true\" "
+ "media=\"true\" system=\"false\" />\n"
+ "<disallow visualEffects=\"511\" />"
+ "</zen>";
parser = Xml.newPullParser();
parser.setInput(new BufferedInputStream(
new ByteArrayInputStream(xml.getBytes())), null);
parser.nextTag();
mZenModeHelperSpy.readXml(parser, false);
assertEquals(0, mZenModeHelperSpy.mConfig.suppressedVisualEffects);
}
@Test
public void testMigrateSuppressedVisualEffects_bothExistButOff() throws Exception {
String xml = "<zen version=\"6\" user=\"0\">\n"
+ "<allow calls=\"false\" repeatCallers=\"false\" messages=\"true\" "
+ "reminders=\"false\" events=\"false\" callsFrom=\"1\" messagesFrom=\"2\" "
+ "visualScreenOff=\"false\" visualScreenOn=\"false\" alarms=\"true\" "
+ "media=\"true\" system=\"false\" />\n"
+ "<disallow visualEffects=\"511\" />"
+ "</zen>";
XmlPullParser parser = Xml.newPullParser();
parser.setInput(new BufferedInputStream(
new ByteArrayInputStream(xml.getBytes())), null);
parser.nextTag();
mZenModeHelperSpy.readXml(parser, false);
assertEquals(0, mZenModeHelperSpy.mConfig.suppressedVisualEffects);
}
@Test
public void testMigrateSuppressedVisualEffects_bothExistButOn() throws Exception {
String xml = "<zen version=\"6\" user=\"0\">\n"
+ "<allow calls=\"false\" repeatCallers=\"false\" messages=\"true\" "
+ "reminders=\"false\" events=\"false\" callsFrom=\"1\" messagesFrom=\"2\" "
+ "visualScreenOff=\"true\" visualScreenOn=\"true\" alarms=\"true\" "
+ "media=\"true\" system=\"false\" />\n"
+ "<disallow visualEffects=\"511\" />"
+ "</zen>";
XmlPullParser parser = Xml.newPullParser();
parser.setInput(new BufferedInputStream(
new ByteArrayInputStream(xml.getBytes())), null);
parser.nextTag();
mZenModeHelperSpy.readXml(parser, false);
assertEquals(SUPPRESSED_EFFECT_FULL_SCREEN_INTENT
| SUPPRESSED_EFFECT_LIGHTS
| SUPPRESSED_EFFECT_PEEK,
mZenModeHelperSpy.mConfig.suppressedVisualEffects);
xml = "<zen version=\"6\" user=\"0\">\n"
+ "<allow calls=\"false\" repeatCallers=\"false\" messages=\"true\" "
+ "reminders=\"false\" events=\"false\" callsFrom=\"1\" messagesFrom=\"2\" "
+ "visualScreenOff=\"false\" visualScreenOn=\"true\" alarms=\"true\" "
+ "media=\"true\" system=\"false\" />\n"
+ "<disallow visualEffects=\"511\" />"
+ "</zen>";
parser = Xml.newPullParser();
parser.setInput(new BufferedInputStream(
new ByteArrayInputStream(xml.getBytes())), null);
parser.nextTag();
mZenModeHelperSpy.readXml(parser, false);
assertEquals(SUPPRESSED_EFFECT_PEEK, mZenModeHelperSpy.mConfig.suppressedVisualEffects);
xml = "<zen version=\"6\" user=\"0\">\n"
+ "<allow calls=\"false\" repeatCallers=\"false\" messages=\"true\" "
+ "reminders=\"false\" events=\"false\" callsFrom=\"1\" messagesFrom=\"2\" "
+ "visualScreenOff=\"true\" visualScreenOn=\"false\" alarms=\"true\" "
+ "media=\"true\" system=\"false\" />\n"
+ "<disallow visualEffects=\"511\" />"
+ "</zen>";
parser = Xml.newPullParser();
parser.setInput(new BufferedInputStream(
new ByteArrayInputStream(xml.getBytes())), null);
parser.nextTag();
mZenModeHelperSpy.readXml(parser, false);
assertEquals(SUPPRESSED_EFFECT_FULL_SCREEN_INTENT | SUPPRESSED_EFFECT_LIGHTS,
mZenModeHelperSpy.mConfig.suppressedVisualEffects);
}
@Test
public void testReadXmlResetDefaultRules() throws Exception {
setupZenConfig();
@@ -705,16 +813,6 @@ public class ZenModeHelperTest extends UiServiceTestCase {
setupZenConfigMaintained();
}
@Test
public void testPolicyReadsSuppressedEffects() {
mZenModeHelperSpy.mConfig.allowWhenScreenOff = true;
mZenModeHelperSpy.mConfig.allowWhenScreenOn = true;
mZenModeHelperSpy.mConfig.suppressedVisualEffects = SUPPRESSED_EFFECT_BADGE;
NotificationManager.Policy policy = mZenModeHelperSpy.getNotificationPolicy();
assertEquals(SUPPRESSED_EFFECT_BADGE, policy.suppressedVisualEffects);
}
private void setupZenConfig() {
mZenModeHelperSpy.mZenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
mZenModeHelperSpy.mConfig.allowAlarms = false;
@@ -725,8 +823,6 @@ public class ZenModeHelperTest extends UiServiceTestCase {
mZenModeHelperSpy.mConfig.allowMessages = true;
mZenModeHelperSpy.mConfig.allowEvents = true;
mZenModeHelperSpy.mConfig.allowRepeatCallers= true;
mZenModeHelperSpy.mConfig.allowWhenScreenOff = true;
mZenModeHelperSpy.mConfig.allowWhenScreenOn = true;
mZenModeHelperSpy.mConfig.suppressedVisualEffects = SUPPRESSED_EFFECT_BADGE;
mZenModeHelperSpy.mConfig.manualRule = new ZenModeConfig.ZenRule();
mZenModeHelperSpy.mConfig.manualRule.zenMode =
@@ -746,8 +842,6 @@ public class ZenModeHelperTest extends UiServiceTestCase {
assertTrue(mZenModeHelperSpy.mConfig.allowMessages);
assertTrue(mZenModeHelperSpy.mConfig.allowEvents);
assertTrue(mZenModeHelperSpy.mConfig.allowRepeatCallers);
assertTrue(mZenModeHelperSpy.mConfig.allowWhenScreenOff);
assertTrue(mZenModeHelperSpy.mConfig.allowWhenScreenOn);
assertEquals(SUPPRESSED_EFFECT_BADGE, mZenModeHelperSpy.mConfig.suppressedVisualEffects);
}
}