Skip to content

Commit ed29c2b

Browse files
Julia Reynoldsofficialputuid
authored andcommitted
Store DND rule owners
Test: uiservicestest, cts Bug: 189332346 Bug: 235823407 Change-Id: Ie546c15c25fcbd193b67cb997220f075691e9bab Merged-In: Ie546c15c25fcbd193b67cb997220f075691e9bab (cherry picked from commit 422cffb) Merged-In: Ie546c15c25fcbd193b67cb997220f075691e9bab
1 parent 309127e commit ed29c2b

9 files changed

Lines changed: 108 additions & 29 deletions

File tree

core/java/android/app/AutomaticZenRule.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public final class AutomaticZenRule implements Parcelable {
4545
private long creationTime;
4646
private ZenPolicy mZenPolicy;
4747
private boolean mModified = false;
48+
private String mPkg;
4849

4950
/**
5051
* Creates an automatic zen rule.
@@ -123,6 +124,7 @@ public AutomaticZenRule(Parcel source) {
123124
creationTime = source.readLong();
124125
mZenPolicy = source.readParcelable(null);
125126
mModified = source.readInt() == ENABLED;
127+
mPkg = source.readString();
126128
}
127129

128130
/**
@@ -244,6 +246,20 @@ public void setConfigurationActivity(@Nullable ComponentName componentName) {
244246
this.configurationActivity = componentName;
245247
}
246248

249+
/**
250+
* @hide
251+
*/
252+
public void setPackageName(String pkgName) {
253+
mPkg = pkgName;
254+
}
255+
256+
/**
257+
* @hide
258+
*/
259+
public String getPackageName() {
260+
return mPkg;
261+
}
262+
247263
@Override
248264
public int describeContents() {
249265
return 0;
@@ -265,6 +281,7 @@ public void writeToParcel(Parcel dest, int flags) {
265281
dest.writeLong(creationTime);
266282
dest.writeParcelable(mZenPolicy, 0);
267283
dest.writeInt(mModified ? ENABLED : DISABLED);
284+
dest.writeString(mPkg);
268285
}
269286

270287
@Override
@@ -273,6 +290,7 @@ public String toString() {
273290
.append("enabled=").append(enabled)
274291
.append(",name=").append(name)
275292
.append(",interruptionFilter=").append(interruptionFilter)
293+
.append(",pkg=").append(mPkg)
276294
.append(",conditionId=").append(conditionId)
277295
.append(",owner=").append(owner)
278296
.append(",configActivity=").append(configurationActivity)
@@ -294,13 +312,14 @@ public boolean equals(Object o) {
294312
&& Objects.equals(other.owner, owner)
295313
&& Objects.equals(other.mZenPolicy, mZenPolicy)
296314
&& Objects.equals(other.configurationActivity, configurationActivity)
315+
&& Objects.equals(other.mPkg, mPkg)
297316
&& other.creationTime == creationTime;
298317
}
299318

300319
@Override
301320
public int hashCode() {
302321
return Objects.hash(enabled, name, interruptionFilter, conditionId, owner,
303-
configurationActivity, mZenPolicy, mModified, creationTime);
322+
configurationActivity, mZenPolicy, mModified, creationTime, mPkg);
304323
}
305324

306325
public static final @android.annotation.NonNull Parcelable.Creator<AutomaticZenRule> CREATOR

core/java/android/app/INotificationManager.aidl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ interface INotificationManager
203203
void setNotificationPolicyAccessGrantedForUser(String pkg, int userId, boolean granted);
204204
AutomaticZenRule getAutomaticZenRule(String id);
205205
List<ZenModeConfig.ZenRule> getZenRules();
206-
String addAutomaticZenRule(in AutomaticZenRule automaticZenRule);
206+
String addAutomaticZenRule(in AutomaticZenRule automaticZenRule, String pkg);
207207
boolean updateAutomaticZenRule(String id, in AutomaticZenRule automaticZenRule);
208208
boolean removeAutomaticZenRule(String id);
209209
boolean removeAutomaticZenRules(String packageName);

core/java/android/app/NotificationManager.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,10 +1072,12 @@ public Map<String, AutomaticZenRule> getAutomaticZenRules() {
10721072
List<ZenModeConfig.ZenRule> rules = service.getZenRules();
10731073
Map<String, AutomaticZenRule> ruleMap = new HashMap<>();
10741074
for (ZenModeConfig.ZenRule rule : rules) {
1075-
ruleMap.put(rule.id, new AutomaticZenRule(rule.name, rule.component,
1075+
AutomaticZenRule azr = new AutomaticZenRule(rule.name, rule.component,
10761076
rule.configurationActivity, rule.conditionId, rule.zenPolicy,
10771077
zenModeToInterruptionFilter(rule.zenMode), rule.enabled,
1078-
rule.creationTime));
1078+
rule.creationTime);
1079+
azr.setPackageName(rule.pkg);
1080+
ruleMap.put(rule.id, azr);
10791081
}
10801082
return ruleMap;
10811083
} catch (RemoteException e) {
@@ -1116,7 +1118,7 @@ public AutomaticZenRule getAutomaticZenRule(String id) {
11161118
public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) {
11171119
INotificationManager service = getService();
11181120
try {
1119-
return service.addAutomaticZenRule(automaticZenRule);
1121+
return service.addAutomaticZenRule(automaticZenRule, mContext.getPackageName());
11201122
} catch (RemoteException e) {
11211123
throw e.rethrowFromSystemServer();
11221124
}

core/java/android/service/notification/ZenModeConfig.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import android.util.proto.ProtoOutputStream;
4747

4848
import com.android.internal.R;
49+
import com.android.internal.util.XmlUtils;
4950

5051
import org.xmlpull.v1.XmlPullParser;
5152
import org.xmlpull.v1.XmlPullParserException;
@@ -159,6 +160,7 @@ public class ZenModeConfig implements Parcelable {
159160
private static final String RULE_ATT_ENABLED = "enabled";
160161
private static final String RULE_ATT_SNOOZING = "snoozing";
161162
private static final String RULE_ATT_NAME = "name";
163+
private static final String RULE_ATT_PKG = "pkg";
162164
private static final String RULE_ATT_COMPONENT = "component";
163165
private static final String RULE_ATT_CONFIG_ACTIVITY = "configActivity";
164166
private static final String RULE_ATT_ZEN = "zen";
@@ -669,11 +671,11 @@ public static ZenRule readRuleXml(XmlPullParser parser) {
669671
rt.conditionId = safeUri(parser, RULE_ATT_CONDITION_ID);
670672
rt.component = safeComponentName(parser, RULE_ATT_COMPONENT);
671673
rt.configurationActivity = safeComponentName(parser, RULE_ATT_CONFIG_ACTIVITY);
672-
rt.pkg = (rt.component != null)
673-
? rt.component.getPackageName()
674-
: (rt.configurationActivity != null)
675-
? rt.configurationActivity.getPackageName()
676-
: null;
674+
rt.pkg = XmlUtils.readStringAttribute(parser, RULE_ATT_PKG);
675+
if (rt.pkg == null) {
676+
// backfill from component, if present. configActivity is not safe to backfill from
677+
rt.pkg = rt.component != null ? rt.component.getPackageName() : null;
678+
}
677679
rt.creationTime = safeLong(parser, RULE_ATT_CREATION_TIME, 0);
678680
rt.enabler = parser.getAttributeValue(null, RULE_ATT_ENABLER);
679681
rt.condition = readConditionXml(parser);
@@ -695,6 +697,9 @@ public static void writeRuleXml(ZenRule rule, XmlSerializer out) throws IOExcept
695697
out.attribute(null, RULE_ATT_NAME, rule.name);
696698
}
697699
out.attribute(null, RULE_ATT_ZEN, Integer.toString(rule.zenMode));
700+
if (rule.pkg != null) {
701+
out.attribute(null, RULE_ATT_PKG, rule.pkg);
702+
}
698703
if (rule.component != null) {
699704
out.attribute(null, RULE_ATT_COMPONENT, rule.component.flattenToString());
700705
}

services/core/java/com/android/server/notification/NotificationManagerService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4506,7 +4506,7 @@ public AutomaticZenRule getAutomaticZenRule(String id) throws RemoteException {
45064506
}
45074507

45084508
@Override
4509-
public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) {
4509+
public String addAutomaticZenRule(AutomaticZenRule automaticZenRule, String pkg) {
45104510
Objects.requireNonNull(automaticZenRule, "automaticZenRule is null");
45114511
Objects.requireNonNull(automaticZenRule.getName(), "Name is null");
45124512
if (automaticZenRule.getOwner() == null
@@ -4515,14 +4515,15 @@ public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) {
45154515
"Rule must have a conditionproviderservice and/or configuration activity");
45164516
}
45174517
Objects.requireNonNull(automaticZenRule.getConditionId(), "ConditionId is null");
4518+
checkCallerIsSameApp(pkg);
45184519
if (automaticZenRule.getZenPolicy() != null
45194520
&& automaticZenRule.getInterruptionFilter() != INTERRUPTION_FILTER_PRIORITY) {
45204521
throw new IllegalArgumentException("ZenPolicy is only applicable to "
45214522
+ "INTERRUPTION_FILTER_PRIORITY filters");
45224523
}
45234524
enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule");
45244525

4525-
return mZenModeHelper.addAutomaticZenRule(automaticZenRule,
4526+
return mZenModeHelper.addAutomaticZenRule(pkg, automaticZenRule,
45264527
"addAutomaticZenRule");
45274528
}
45284529

services/core/java/com/android/server/notification/ZenModeHelper.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ public AutomaticZenRule getAutomaticZenRule(String id) {
303303
return null;
304304
}
305305

306-
public String addAutomaticZenRule(AutomaticZenRule automaticZenRule, String reason) {
306+
public String addAutomaticZenRule(String pkg, AutomaticZenRule automaticZenRule,
307+
String reason) {
307308
if (!isSystemRule(automaticZenRule)) {
308309
PackageItemInfo component = getServiceInfo(automaticZenRule.getOwner());
309310
if (component == null) {
@@ -336,7 +337,7 @@ public String addAutomaticZenRule(AutomaticZenRule automaticZenRule, String reas
336337
}
337338
newConfig = mConfig.copy();
338339
ZenRule rule = new ZenRule();
339-
populateZenRule(automaticZenRule, rule, true);
340+
populateZenRule(pkg, automaticZenRule, rule, true);
340341
newConfig.automaticRules.put(rule.id, rule);
341342
if (setConfigLocked(newConfig, reason, rule.component, true)) {
342343
return rule.id;
@@ -372,7 +373,7 @@ public boolean updateAutomaticZenRule(String ruleId, AutomaticZenRule automaticZ
372373
? AUTOMATIC_RULE_STATUS_ENABLED : AUTOMATIC_RULE_STATUS_DISABLED);
373374
}
374375

375-
populateZenRule(automaticZenRule, rule, false);
376+
populateZenRule(rule.pkg, automaticZenRule, rule, false);
376377
return setConfigLocked(newConfig, reason, rule.component, true);
377378
}
378379
}
@@ -568,15 +569,14 @@ private ActivityInfo getActivityInfo(ComponentName configActivity) {
568569
return null;
569570
}
570571

571-
private void populateZenRule(AutomaticZenRule automaticZenRule, ZenRule rule, boolean isNew) {
572+
private void populateZenRule(String pkg, AutomaticZenRule automaticZenRule, ZenRule rule,
573+
boolean isNew) {
572574
if (isNew) {
573575
rule.id = ZenModeConfig.newRuleId();
574576
rule.creationTime = System.currentTimeMillis();
575577
rule.component = automaticZenRule.getOwner();
576578
rule.configurationActivity = automaticZenRule.getConfigurationActivity();
577-
rule.pkg = (rule.component != null)
578-
? rule.component.getPackageName()
579-
: rule.configurationActivity.getPackageName();
579+
rule.pkg = pkg;
580580
}
581581

582582
if (rule.enabled != automaticZenRule.isEnabled()) {
@@ -593,10 +593,13 @@ private void populateZenRule(AutomaticZenRule automaticZenRule, ZenRule rule, bo
593593
}
594594

595595
protected AutomaticZenRule createAutomaticZenRule(ZenRule rule) {
596-
return new AutomaticZenRule(rule.name, rule.component, rule.configurationActivity,
596+
AutomaticZenRule azr = new AutomaticZenRule(rule.name, rule.component,
597+
rule.configurationActivity,
597598
rule.conditionId, rule.zenPolicy,
598599
NotificationManager.zenModeToInterruptionFilter(rule.zenMode),
599600
rule.enabled, rule.creationTime);
601+
azr.setPackageName(rule.pkg);
602+
return azr;
600603
}
601604

602605
public void setManualZenMode(int zenMode, Uri conditionId, String caller, String reason) {

services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5921,19 +5921,19 @@ public void testAutomaticZenRuleValidation_policyFilterAgreement() throws Except
59215921
zenPolicy, NotificationManager.INTERRUPTION_FILTER_NONE, isEnabled);
59225922

59235923
try {
5924-
mBinderService.addAutomaticZenRule(rule);
5924+
mBinderService.addAutomaticZenRule(rule, mContext.getPackageName());
59255925
fail("Zen policy only applies to priority only mode");
59265926
} catch (IllegalArgumentException e) {
59275927
// yay
59285928
}
59295929

59305930
rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class),
59315931
zenPolicy, NotificationManager.INTERRUPTION_FILTER_PRIORITY, isEnabled);
5932-
mBinderService.addAutomaticZenRule(rule);
5932+
mBinderService.addAutomaticZenRule(rule, mContext.getPackageName());
59335933

59345934
rule = new AutomaticZenRule("test", owner, owner, mock(Uri.class),
59355935
null, NotificationManager.INTERRUPTION_FILTER_NONE, isEnabled);
5936-
mBinderService.addAutomaticZenRule(rule);
5936+
mBinderService.addAutomaticZenRule(rule, mContext.getPackageName());
59375937
}
59385938

59395939
@Test

services/tests/uiservicestests/src/com/android/server/notification/ZenModeConfigTest.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void testRuleXml() throws Exception {
188188

189189
ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
190190
rule.configurationActivity = new ComponentName("a", "a");
191-
rule.component = new ComponentName("a", "b");
191+
rule.component = new ComponentName("b", "b");
192192
rule.conditionId = new Uri.Builder().scheme("hello").build();
193193
rule.condition = new Condition(rule.conditionId, "", Condition.STATE_TRUE);
194194
rule.enabled = true;
@@ -198,6 +198,7 @@ public void testRuleXml() throws Exception {
198198
rule.modified = true;
199199
rule.name = "name";
200200
rule.snoozing = true;
201+
rule.pkg = "b";
201202

202203
XmlSerializer out = new FastXmlSerializer();
203204
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -213,8 +214,7 @@ public void testRuleXml() throws Exception {
213214
new ByteArrayInputStream(baos.toByteArray())), null);
214215
parser.nextTag();
215216
ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser);
216-
// read from backing component
217-
assertEquals("a", fromXml.pkg);
217+
assertEquals("b", fromXml.pkg);
218218
// always resets on reboot
219219
assertFalse(fromXml.snoozing);
220220
//should all match original
@@ -230,6 +230,55 @@ public void testRuleXml() throws Exception {
230230
assertEquals(rule.zenMode, fromXml.zenMode);
231231
}
232232

233+
@Test
234+
public void testRuleXml_pkg_component() throws Exception {
235+
String tag = "tag";
236+
237+
ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
238+
rule.configurationActivity = new ComponentName("a", "a");
239+
rule.component = new ComponentName("b", "b");
240+
241+
XmlSerializer out = new FastXmlSerializer();
242+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
243+
out.setOutput(new BufferedOutputStream(baos), "utf-8");
244+
out.startDocument(null, true);
245+
out.startTag(null, tag);
246+
ZenModeConfig.writeRuleXml(rule, out);
247+
out.endTag(null, tag);
248+
out.endDocument();
249+
250+
XmlPullParser parser = Xml.newPullParser();
251+
parser.setInput(new BufferedInputStream(
252+
new ByteArrayInputStream(baos.toByteArray())), null);
253+
parser.nextTag();
254+
ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser);
255+
assertEquals("b", fromXml.pkg);
256+
}
257+
258+
@Test
259+
public void testRuleXml_pkg_configActivity() throws Exception {
260+
String tag = "tag";
261+
262+
ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
263+
rule.configurationActivity = new ComponentName("a", "a");
264+
265+
XmlSerializer out = new FastXmlSerializer();
266+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
267+
out.setOutput(new BufferedOutputStream(baos), "utf-8");
268+
out.startDocument(null, true);
269+
out.startTag(null, tag);
270+
ZenModeConfig.writeRuleXml(rule, out);
271+
out.endTag(null, tag);
272+
out.endDocument();
273+
274+
XmlPullParser parser = Xml.newPullParser();
275+
parser.setInput(new BufferedInputStream(
276+
new ByteArrayInputStream(baos.toByteArray())), null);
277+
parser.nextTag();
278+
ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser);
279+
assertNull(fromXml.pkg);
280+
}
281+
233282
private ZenModeConfig getMutedRingerConfig() {
234283
ZenModeConfig config = new ZenModeConfig();
235284
// Allow alarms, media

services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ public void testAddAutomaticZenRule() {
15621562
new ComponentName("android", "ScheduleConditionProvider"),
15631563
ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
15641564
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
1565-
String id = mZenModeHelperSpy.addAutomaticZenRule(zenRule, "test");
1565+
String id = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule, "test");
15661566

15671567
assertTrue(id != null);
15681568
ZenModeConfig.ZenRule ruleInConfig = mZenModeHelperSpy.mConfig.automaticRules.get(id);
@@ -1586,7 +1586,7 @@ public void testAddAutomaticZenRule_beyondSystemLimit() {
15861586
ZenModeConfig.toScheduleConditionId(si),
15871587
new ZenPolicy.Builder().build(),
15881588
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
1589-
String id = mZenModeHelperSpy.addAutomaticZenRule(zenRule, "test");
1589+
String id = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule, "test");
15901590
assertNotNull(id);
15911591
}
15921592
try {
@@ -1596,7 +1596,7 @@ public void testAddAutomaticZenRule_beyondSystemLimit() {
15961596
ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
15971597
new ZenPolicy.Builder().build(),
15981598
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
1599-
String id = mZenModeHelperSpy.addAutomaticZenRule(zenRule, "test");
1599+
String id = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule, "test");
16001600
fail("allowed too many rules to be created");
16011601
} catch (IllegalArgumentException e) {
16021602
// yay

0 commit comments

Comments
 (0)