Merge "Ensure non null package" into sc-dev am: 77f42843ac

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15273600

Change-Id: I816dbf832349c5b11a100d0c1bff511effe4c7e2
This commit is contained in:
Julia Reynolds
2021-07-14 13:56:16 +00:00
committed by Automerger Merge Worker
4 changed files with 173 additions and 23 deletions

View File

@@ -1969,7 +1969,17 @@ public class ZenModeConfig implements Parcelable {
} }
public boolean isAutomaticActive() { public boolean isAutomaticActive() {
return enabled && !snoozing && pkg != null && isTrueOrUnknown(); return enabled && !snoozing && getPkg() != null && isTrueOrUnknown();
}
public String getPkg() {
return !TextUtils.isEmpty(pkg)
? pkg
: (component != null)
? component.getPackageName()
: (configurationActivity != null)
? configurationActivity.getPackageName()
: null;
} }
public boolean isTrueOrUnknown() { public boolean isTrueOrUnknown() {

View File

@@ -372,7 +372,7 @@ public class ZenModeHelper {
} }
} }
if (rule.enabled != automaticZenRule.isEnabled()) { if (rule.enabled != automaticZenRule.isEnabled()) {
dispatchOnAutomaticRuleStatusChanged(mConfig.user, rule.pkg, ruleId, dispatchOnAutomaticRuleStatusChanged(mConfig.user, rule.getPkg(), ruleId,
automaticZenRule.isEnabled() automaticZenRule.isEnabled()
? AUTOMATIC_RULE_STATUS_ENABLED : AUTOMATIC_RULE_STATUS_DISABLED); ? AUTOMATIC_RULE_STATUS_ENABLED : AUTOMATIC_RULE_STATUS_DISABLED);
} }
@@ -391,13 +391,14 @@ public class ZenModeHelper {
if (ruleToRemove == null) return false; if (ruleToRemove == null) return false;
if (canManageAutomaticZenRule(ruleToRemove)) { if (canManageAutomaticZenRule(ruleToRemove)) {
newConfig.automaticRules.remove(id); newConfig.automaticRules.remove(id);
if (ruleToRemove.pkg != null && !"android".equals(ruleToRemove.pkg)) { if (ruleToRemove.getPkg() != null && !"android".equals(ruleToRemove.getPkg())) {
for (ZenRule currRule : newConfig.automaticRules.values()) { for (ZenRule currRule : newConfig.automaticRules.values()) {
if (currRule.pkg != null && currRule.pkg.equals(ruleToRemove.pkg)) { if (currRule.getPkg() != null
&& currRule.getPkg().equals(ruleToRemove.getPkg())) {
break; // no need to remove from cache break; // no need to remove from cache
} }
} }
mRulesUidCache.remove(getPackageUserKey(ruleToRemove.pkg, newConfig.user)); mRulesUidCache.remove(getPackageUserKey(ruleToRemove.getPkg(), newConfig.user));
} }
if (DEBUG) Log.d(TAG, "removeZenRule zenRule=" + id + " reason=" + reason); if (DEBUG) Log.d(TAG, "removeZenRule zenRule=" + id + " reason=" + reason);
} else { } else {
@@ -405,7 +406,7 @@ public class ZenModeHelper {
"Cannot delete rules not owned by your condition provider"); "Cannot delete rules not owned by your condition provider");
} }
dispatchOnAutomaticRuleStatusChanged( dispatchOnAutomaticRuleStatusChanged(
mConfig.user, ruleToRemove.pkg, id, AUTOMATIC_RULE_STATUS_REMOVED); mConfig.user, ruleToRemove.getPkg(), id, AUTOMATIC_RULE_STATUS_REMOVED);
return setConfigLocked(newConfig, reason, null, true); return setConfigLocked(newConfig, reason, null, true);
} }
} }
@@ -417,14 +418,7 @@ public class ZenModeHelper {
newConfig = mConfig.copy(); newConfig = mConfig.copy();
for (int i = newConfig.automaticRules.size() - 1; i >= 0; i--) { for (int i = newConfig.automaticRules.size() - 1; i >= 0; i--) {
ZenRule rule = newConfig.automaticRules.get(newConfig.automaticRules.keyAt(i)); ZenRule rule = newConfig.automaticRules.get(newConfig.automaticRules.keyAt(i));
String pkg = rule.pkg != null if (Objects.equals(rule.getPkg(), packageName) && canManageAutomaticZenRule(rule)) {
? rule.pkg
: (rule.component != null)
? rule.component.getPackageName()
: (rule.configurationActivity != null)
? rule.configurationActivity.getPackageName()
: null;
if (Objects.equals(pkg, packageName) && canManageAutomaticZenRule(rule)) {
newConfig.automaticRules.removeAt(i); newConfig.automaticRules.removeAt(i);
} }
} }
@@ -524,7 +518,7 @@ public class ZenModeHelper {
if (packages != null) { if (packages != null) {
final int packageCount = packages.length; final int packageCount = packages.length;
for (int i = 0; i < packageCount; i++) { for (int i = 0; i < packageCount; i++) {
if (packages[i].equals(rule.pkg)) { if (packages[i].equals(rule.getPkg())) {
return true; return true;
} }
} }
@@ -834,8 +828,8 @@ public class ZenModeHelper {
ZenRule rule = newConfig.automaticRules.get(newConfig.automaticRules.keyAt(i)); ZenRule rule = newConfig.automaticRules.get(newConfig.automaticRules.keyAt(i));
if (RULE_INSTANCE_GRACE_PERIOD < (currentTime - rule.creationTime)) { if (RULE_INSTANCE_GRACE_PERIOD < (currentTime - rule.creationTime)) {
try { try {
if (rule.pkg != null) { if (rule.getPkg() != null) {
mPm.getPackageInfo(rule.pkg, PackageManager.MATCH_ANY_USER); mPm.getPackageInfo(rule.getPkg(), PackageManager.MATCH_ANY_USER);
} }
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {
newConfig.automaticRules.removeAt(i); newConfig.automaticRules.removeAt(i);
@@ -1246,7 +1240,7 @@ public class ZenModeHelper {
} }
// Look for packages and enablers, enablers get priority. // Look for packages and enablers, enablers get priority.
String pkg = rule.pkg == null ? "" : rule.pkg; String pkg = rule.getPkg() == null ? "" : rule.getPkg();
if (rule.enabler != null) { if (rule.enabler != null) {
pkg = rule.enabler; pkg = rule.enabler;
id = ZenModeConfig.MANUAL_RULE_ID; id = ZenModeConfig.MANUAL_RULE_ID;

View File

@@ -281,6 +281,34 @@ public class ZenModeConfigTest extends UiServiceTestCase {
assertNull(fromXml.pkg); assertNull(fromXml.pkg);
} }
@Test
public void testRuleXml_getPkg_nullPkg() throws Exception {
String tag = "tag";
ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule();
rule.enabled = true;
rule.configurationActivity = new ComponentName("a", "a");
TypedXmlSerializer out = Xml.newFastSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
out.setOutput(new BufferedOutputStream(baos), "utf-8");
out.startDocument(null, true);
out.startTag(null, tag);
ZenModeConfig.writeRuleXml(rule, out);
out.endTag(null, tag);
out.endDocument();
TypedXmlPullParser parser = Xml.newFastPullParser();
parser.setInput(new BufferedInputStream(
new ByteArrayInputStream(baos.toByteArray())), null);
parser.nextTag();
ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser);
assertEquals("a", fromXml.getPkg());
fromXml.condition = new Condition(Uri.EMPTY, "", Condition.STATE_TRUE);
assertTrue(fromXml.isAutomaticActive());
}
private ZenModeConfig getMutedRingerConfig() { private ZenModeConfig getMutedRingerConfig() {
ZenModeConfig config = new ZenModeConfig(); ZenModeConfig config = new ZenModeConfig();
// Allow alarms, media // Allow alarms, media

View File

@@ -33,6 +33,7 @@ import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCRE
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS; import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK; import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; import static android.provider.Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
import static android.service.notification.Condition.STATE_TRUE;
import static android.util.StatsLog.ANNOTATION_ID_IS_UID; import static android.util.StatsLog.ANNOTATION_ID_IS_UID;
import static com.android.internal.util.FrameworkStatsLog.DND_MODE_RULE; import static com.android.internal.util.FrameworkStatsLog.DND_MODE_RULE;
@@ -49,6 +50,7 @@ import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail; import static junit.framework.TestCase.fail;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
@@ -64,6 +66,8 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.AppGlobals; import android.app.AppGlobals;
import android.app.AppOpsManager; import android.app.AppOpsManager;
import android.app.AutomaticZenRule; import android.app.AutomaticZenRule;
@@ -72,7 +76,9 @@ import android.app.NotificationManager.Policy;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources; import android.content.res.Resources;
import android.content.res.XmlResourceParser; import android.content.res.XmlResourceParser;
import android.media.AudioAttributes; import android.media.AudioAttributes;
@@ -81,6 +87,7 @@ import android.media.AudioManagerInternal;
import android.media.AudioSystem; import android.media.AudioSystem;
import android.media.VolumePolicy; import android.media.VolumePolicy;
import android.net.Uri; import android.net.Uri;
import android.os.Binder;
import android.os.Process; import android.os.Process;
import android.os.UserHandle; import android.os.UserHandle;
import android.provider.Settings; import android.provider.Settings;
@@ -105,6 +112,8 @@ import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
import com.android.server.UiServiceTestCase; import com.android.server.UiServiceTestCase;
import com.android.server.notification.ManagedServices.UserProfiles; import com.android.server.notification.ManagedServices.UserProfiles;
import com.google.common.collect.ImmutableList;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -172,8 +181,14 @@ public class ZenModeHelperTest extends UiServiceTestCase {
mZenModeHelperSpy = spy(new ZenModeHelper(mContext, mTestableLooper.getLooper(), mZenModeHelperSpy = spy(new ZenModeHelper(mContext, mTestableLooper.getLooper(),
mConditionProviders, mStatsEventBuilderFactory)); mConditionProviders, mStatsEventBuilderFactory));
ResolveInfo ri = new ResolveInfo();
ri.activityInfo = new ActivityInfo();
when(mPackageManager.queryIntentActivitiesAsUser(any(), anyInt(), anyInt())).thenReturn(
ImmutableList.of(ri));
when(mPackageManager.getPackageUidAsUser(eq(CUSTOM_PKG_NAME), anyInt())) when(mPackageManager.getPackageUidAsUser(eq(CUSTOM_PKG_NAME), anyInt()))
.thenReturn(CUSTOM_PKG_UID); .thenReturn(CUSTOM_PKG_UID);
when(mPackageManager.getPackagesForUid(anyInt())).thenReturn(
new String[] {getContext().getPackageName()});
mZenModeHelperSpy.mPm = mPackageManager; mZenModeHelperSpy.mPm = mPackageManager;
} }
@@ -1486,7 +1501,7 @@ public class ZenModeHelperTest extends UiServiceTestCase {
mZenModeHelperSpy.mConfig.manualRule.component = new ComponentName("android", mZenModeHelperSpy.mConfig.manualRule.component = new ComponentName("android",
CountdownConditionProvider.class.getName()); CountdownConditionProvider.class.getName());
mZenModeHelperSpy.mConfig.manualRule.condition = new Condition(conditionId, "", "", "", 0, mZenModeHelperSpy.mConfig.manualRule.condition = new Condition(conditionId, "", "", "", 0,
Condition.STATE_TRUE, Condition.FLAG_RELEVANT_NOW); STATE_TRUE, Condition.FLAG_RELEVANT_NOW);
mZenModeHelperSpy.mConfig.manualRule.enabled = true; mZenModeHelperSpy.mConfig.manualRule.enabled = true;
ZenModeConfig originalConfig = mZenModeHelperSpy.mConfig.copy(); ZenModeConfig originalConfig = mZenModeHelperSpy.mConfig.copy();
@@ -1592,7 +1607,29 @@ public class ZenModeHelperTest extends UiServiceTestCase {
} }
@Test @Test
public void testAddAutomaticZenRule() { public void testAddAutomaticZenRule_CA() {
AutomaticZenRule zenRule = new AutomaticZenRule("name",
null,
new ComponentName("android", "ScheduleConditionProvider"),
ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
new ZenPolicy.Builder().build(),
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
String id = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule, "test");
assertTrue(id != null);
ZenModeConfig.ZenRule ruleInConfig = mZenModeHelperSpy.mConfig.automaticRules.get(id);
assertTrue(ruleInConfig != null);
assertEquals(zenRule.isEnabled(), ruleInConfig.enabled);
assertEquals(zenRule.isModified(), ruleInConfig.modified);
assertEquals(zenRule.getConditionId(), ruleInConfig.conditionId);
assertEquals(NotificationManager.zenModeFromInterruptionFilter(
zenRule.getInterruptionFilter(), -1), ruleInConfig.zenMode);
assertEquals(zenRule.getName(), ruleInConfig.name);
assertEquals("android", ruleInConfig.pkg);
}
@Test
public void testAddAutomaticZenRule_CPS() {
AutomaticZenRule zenRule = new AutomaticZenRule("name", AutomaticZenRule zenRule = new AutomaticZenRule("name",
new ComponentName("android", "ScheduleConditionProvider"), new ComponentName("android", "ScheduleConditionProvider"),
ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
@@ -1608,6 +1645,87 @@ public class ZenModeHelperTest extends UiServiceTestCase {
assertEquals(NotificationManager.zenModeFromInterruptionFilter( assertEquals(NotificationManager.zenModeFromInterruptionFilter(
zenRule.getInterruptionFilter(), -1), ruleInConfig.zenMode); zenRule.getInterruptionFilter(), -1), ruleInConfig.zenMode);
assertEquals(zenRule.getName(), ruleInConfig.name); assertEquals(zenRule.getName(), ruleInConfig.name);
assertEquals("android", ruleInConfig.pkg);
}
@Test
public void testSetAutomaticZenRuleState_nullPkg() {
AutomaticZenRule zenRule = new AutomaticZenRule("name",
null,
new ComponentName(mContext.getPackageName(), "ScheduleConditionProvider"),
ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
new ZenPolicy.Builder().build(),
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
String id = mZenModeHelperSpy.addAutomaticZenRule(null, zenRule, "test");
mZenModeHelperSpy.setAutomaticZenRuleState(zenRule.getConditionId(),
new Condition(zenRule.getConditionId(), "", STATE_TRUE));
ZenModeConfig.ZenRule ruleInConfig = mZenModeHelperSpy.mConfig.automaticRules.get(id);
assertEquals(STATE_TRUE, ruleInConfig.condition.state);
}
@Test
public void testUpdateAutomaticZenRule_nullPkg() {
AutomaticZenRule zenRule = new AutomaticZenRule("name",
null,
new ComponentName(mContext.getPackageName(), "ScheduleConditionProvider"),
ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
new ZenPolicy.Builder().build(),
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
String id = mZenModeHelperSpy.addAutomaticZenRule(null, zenRule, "test");
AutomaticZenRule zenRule2 = new AutomaticZenRule("NEW",
null,
new ComponentName(mContext.getPackageName(), "ScheduleConditionProvider"),
ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
new ZenPolicy.Builder().build(),
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
mZenModeHelperSpy.updateAutomaticZenRule(id, zenRule2, "");
ZenModeConfig.ZenRule ruleInConfig = mZenModeHelperSpy.mConfig.automaticRules.get(id);
assertEquals("NEW", ruleInConfig.name);
}
@Test
public void testRemoveAutomaticZenRule_nullPkg() {
AutomaticZenRule zenRule = new AutomaticZenRule("name",
null,
new ComponentName(mContext.getPackageName(), "ScheduleConditionProvider"),
ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
new ZenPolicy.Builder().build(),
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
String id = mZenModeHelperSpy.addAutomaticZenRule(null, zenRule, "test");
assertTrue(id != null);
ZenModeConfig.ZenRule ruleInConfig = mZenModeHelperSpy.mConfig.automaticRules.get(id);
assertTrue(ruleInConfig != null);
assertEquals(zenRule.getName(), ruleInConfig.name);
mZenModeHelperSpy.removeAutomaticZenRule(id, "test");
assertNull(mZenModeHelperSpy.mConfig.automaticRules.get(id));
}
@Test
public void testRemoveAutomaticZenRules_nullPkg() {
AutomaticZenRule zenRule = new AutomaticZenRule("name",
null,
new ComponentName(mContext.getPackageName(), "ScheduleConditionProvider"),
ZenModeConfig.toScheduleConditionId(new ScheduleInfo()),
new ZenPolicy.Builder().build(),
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
String id = mZenModeHelperSpy.addAutomaticZenRule(null, zenRule, "test");
assertTrue(id != null);
ZenModeConfig.ZenRule ruleInConfig = mZenModeHelperSpy.mConfig.automaticRules.get(id);
assertTrue(ruleInConfig != null);
assertEquals(zenRule.getName(), ruleInConfig.name);
mZenModeHelperSpy.removeAutomaticZenRules(mContext.getPackageName(), "test");
assertNull(mZenModeHelperSpy.mConfig.automaticRules.get(id));
} }
@Test @Test
@@ -1624,17 +1742,17 @@ public class ZenModeHelperTest extends UiServiceTestCase {
NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); NotificationManager.INTERRUPTION_FILTER_PRIORITY, true);
String id2 = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule2, "test"); String id2 = mZenModeHelperSpy.addAutomaticZenRule("android", zenRule2, "test");
Condition condition = new Condition(sharedUri, "", Condition.STATE_TRUE); Condition condition = new Condition(sharedUri, "", STATE_TRUE);
mZenModeHelperSpy.setAutomaticZenRuleState(sharedUri, condition); mZenModeHelperSpy.setAutomaticZenRuleState(sharedUri, condition);
for (ZenModeConfig.ZenRule rule : mZenModeHelperSpy.mConfig.automaticRules.values()) { for (ZenModeConfig.ZenRule rule : mZenModeHelperSpy.mConfig.automaticRules.values()) {
if (rule.id.equals(id)) { if (rule.id.equals(id)) {
assertNotNull(rule.condition); assertNotNull(rule.condition);
assertTrue(rule.condition.state == Condition.STATE_TRUE); assertTrue(rule.condition.state == STATE_TRUE);
} }
if (rule.id.equals(id2)) { if (rule.id.equals(id2)) {
assertNotNull(rule.condition); assertNotNull(rule.condition);
assertTrue(rule.condition.state == Condition.STATE_TRUE); assertTrue(rule.condition.state == STATE_TRUE);
} }
} }