Tag Zen operations from Settings as coming from the user

Bug: 308670715
Test: atest ApprovalPreferenceControllerTest
Change-Id: Id118f867e84f3d742db6b12eab0f34df1357d178
This commit is contained in:
Matías Hernández
2023-12-11 18:06:28 +01:00
parent facd2f7197
commit 907b47105b
7 changed files with 76 additions and 20 deletions

View File

@@ -139,8 +139,12 @@ public class ApprovalPreferenceController extends BasePreferenceController {
AsyncTask.execute(() -> { AsyncTask.execute(() -> {
if (!mNm.isNotificationPolicyAccessGrantedForPackage( if (!mNm.isNotificationPolicyAccessGrantedForPackage(
cn.getPackageName())) { cn.getPackageName())) {
if (android.app.Flags.modesApi()) {
mNm.removeAutomaticZenRules(cn.getPackageName(), /* fromUser= */ true);
} else {
mNm.removeAutomaticZenRules(cn.getPackageName()); mNm.removeAutomaticZenRules(cn.getPackageName());
} }
}
}); });
} }

View File

@@ -102,8 +102,12 @@ public class ZenAccessController extends BasePreferenceController {
public static void deleteRules(final Context context, final String pkg) { public static void deleteRules(final Context context, final String pkg) {
final NotificationManager mgr = context.getSystemService(NotificationManager.class); final NotificationManager mgr = context.getSystemService(NotificationManager.class);
if (android.app.Flags.modesApi()) {
mgr.removeAutomaticZenRules(pkg, /* fromUser= */ true);
} else {
mgr.removeAutomaticZenRules(pkg); mgr.removeAutomaticZenRules(pkg);
} }
}
@VisibleForTesting @VisibleForTesting
static void logSpecialPermissionChange(boolean enable, String packageName, Context context) { static void logSpecialPermissionChange(boolean enable, String packageName, Context context) {

View File

@@ -16,6 +16,7 @@
package com.android.settings.homepage.contextualcards.conditional; package com.android.settings.homepage.contextualcards.conditional;
import android.app.Flags;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@@ -86,8 +87,13 @@ public class DndConditionCardController implements ConditionalCardController {
@Override @Override
public void onActionClick() { public void onActionClick() {
if (Flags.modesApi()) {
mNotificationManager.setZenMode(Settings.Global.ZEN_MODE_OFF, null, TAG,
/* fromUser= */ true);
} else {
mNotificationManager.setZenMode(Settings.Global.ZEN_MODE_OFF, null, TAG); mNotificationManager.setZenMode(Settings.Global.ZEN_MODE_OFF, null, TAG);
} }
}
@Override @Override
public ContextualCard buildContextualCard() { public ContextualCard buildContextualCard() {

View File

@@ -56,7 +56,6 @@ public class ZenModeBackend {
@VisibleForTesting @VisibleForTesting
protected static final String ZEN_MODE_FROM_NONE = "zen_mode_from_none"; protected static final String ZEN_MODE_FROM_NONE = "zen_mode_from_none";
protected static final int SOURCE_NONE = -1; protected static final int SOURCE_NONE = -1;
private static List<String> mDefaultRuleIds;
private static ZenModeBackend sInstance; private static ZenModeBackend sInstance;
@@ -65,7 +64,7 @@ public class ZenModeBackend {
protected NotificationManager.Policy mPolicy; protected NotificationManager.Policy mPolicy;
private final NotificationManager mNotificationManager; private final NotificationManager mNotificationManager;
private String TAG = "ZenModeSettingsBackend"; private static final String TAG = "ZenModeSettingsBackend";
private final Context mContext; private final Context mContext;
public static ZenModeBackend getInstance(Context context) { public static ZenModeBackend getInstance(Context context) {
@@ -95,19 +94,32 @@ public class ZenModeBackend {
} }
protected boolean updateZenRule(String id, AutomaticZenRule rule) { protected boolean updateZenRule(String id, AutomaticZenRule rule) {
if (android.app.Flags.modesApi()) {
return mNotificationManager.updateAutomaticZenRule(id, rule, /* fromUser= */ true);
} else {
return NotificationManager.from(mContext).updateAutomaticZenRule(id, rule); return NotificationManager.from(mContext).updateAutomaticZenRule(id, rule);
} }
}
protected void setZenMode(int zenMode) { protected void setZenMode(int zenMode) {
if (android.app.Flags.modesApi()) {
mNotificationManager.setZenMode(zenMode, null, TAG, /* fromUser= */ true);
} else {
NotificationManager.from(mContext).setZenMode(zenMode, null, TAG); NotificationManager.from(mContext).setZenMode(zenMode, null, TAG);
}
mZenMode = getZenMode(); mZenMode = getZenMode();
} }
protected void setZenModeForDuration(int minutes) { protected void setZenModeForDuration(int minutes) {
Uri conditionId = ZenModeConfig.toTimeCondition(mContext, minutes, Uri conditionId = ZenModeConfig.toTimeCondition(mContext, minutes,
ActivityManager.getCurrentUser(), true).id; ActivityManager.getCurrentUser(), true).id;
if (android.app.Flags.modesApi()) {
mNotificationManager.setZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS,
conditionId, TAG, /* fromUser= */ true);
} else {
mNotificationManager.setZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS, mNotificationManager.setZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS,
conditionId, TAG); conditionId, TAG);
}
mZenMode = getZenMode(); mZenMode = getZenMode();
} }
@@ -180,8 +192,12 @@ public class ZenModeBackend {
int priorityConversationSenders) { int priorityConversationSenders) {
mPolicy = new NotificationManager.Policy(priorityCategories, priorityCallSenders, mPolicy = new NotificationManager.Policy(priorityCategories, priorityCallSenders,
priorityMessageSenders, suppressedVisualEffects, priorityConversationSenders); priorityMessageSenders, suppressedVisualEffects, priorityConversationSenders);
if (android.app.Flags.modesApi()) {
mNotificationManager.setNotificationPolicy(mPolicy, /* fromUser= */ true);
} else {
mNotificationManager.setNotificationPolicy(mPolicy); mNotificationManager.setNotificationPolicy(mPolicy);
} }
}
private int getNewSuppressedEffects(boolean suppress, int effectType) { private int getNewSuppressedEffects(boolean suppress, int effectType) {
@@ -357,8 +373,12 @@ public class ZenModeBackend {
} }
public boolean removeZenRule(String ruleId) { public boolean removeZenRule(String ruleId) {
if (android.app.Flags.modesApi()) {
return mNotificationManager.removeAutomaticZenRule(ruleId, /* fromUser= */ true);
} else {
return NotificationManager.from(mContext).removeAutomaticZenRule(ruleId); return NotificationManager.from(mContext).removeAutomaticZenRule(ruleId);
} }
}
public NotificationManager.Policy getConsolidatedPolicy() { public NotificationManager.Policy getConsolidatedPolicy() {
return NotificationManager.from(mContext).getConsolidatedNotificationPolicy(); return NotificationManager.from(mContext).getConsolidatedNotificationPolicy();
@@ -366,7 +386,11 @@ public class ZenModeBackend {
protected String addZenRule(AutomaticZenRule rule) { protected String addZenRule(AutomaticZenRule rule) {
try { try {
if (android.app.Flags.modesApi()) {
return mNotificationManager.addAutomaticZenRule(rule, /* fromUser= */ true);
} else {
return NotificationManager.from(mContext).addAutomaticZenRule(rule); return NotificationManager.from(mContext).addAutomaticZenRule(rule);
}
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
@@ -429,10 +453,7 @@ public class ZenModeBackend {
} }
private static List<String> getDefaultRuleIds() { private static List<String> getDefaultRuleIds() {
if (mDefaultRuleIds == null) { return ZenModeConfig.DEFAULT_RULE_IDS;
mDefaultRuleIds = ZenModeConfig.DEFAULT_RULE_IDS;
}
return mDefaultRuleIds;
} }
NotificationManager.Policy toNotificationPolicy(ZenPolicy policy) { NotificationManager.Policy toNotificationPolicy(ZenPolicy policy) {

View File

@@ -19,6 +19,7 @@ package com.android.settings.notification.zen;
import static android.app.slice.Slice.EXTRA_TOGGLE_STATE; import static android.app.slice.Slice.EXTRA_TOGGLE_STATE;
import android.annotation.ColorInt; import android.annotation.ColorInt;
import android.app.Flags;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
@@ -116,7 +117,12 @@ public class ZenModeSliceBuilder {
} else { } else {
zenMode = Settings.Global.ZEN_MODE_OFF; zenMode = Settings.Global.ZEN_MODE_OFF;
} }
if (Flags.modesApi()) {
NotificationManager.from(context).setZenMode(zenMode, /* conditionId= */ null, TAG,
/* fromUser= */ true);
} else {
NotificationManager.from(context).setZenMode(zenMode, null /* conditionId */, TAG); NotificationManager.from(context).setZenMode(zenMode, null /* conditionId */, TAG);
}
// Do not notifyChange on Uri. The service takes longer to update the current value than it // Do not notifyChange on Uri. The service takes longer to update the current value than it
// does for the Slice to check the current value again. Let {@link SliceBroadcastRelay} // does for the Slice to check the current value again. Let {@link SliceBroadcastRelay}
// handle it. // handle it.

View File

@@ -17,6 +17,7 @@
package com.android.settings.notification.zen; package com.android.settings.notification.zen;
import android.app.Activity; import android.app.Activity;
import android.app.Flags;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.NotificationManager.Policy; import android.app.NotificationManager.Policy;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
@@ -129,7 +130,11 @@ public class ZenOnboardingActivity extends Activity {
Policy.PRIORITY_SENDERS_STARRED, Policy.PRIORITY_SENDERS_STARRED,
policy.priorityMessageSenders, policy.priorityMessageSenders,
NotificationManager.Policy.getAllSuppressedVisualEffects()); NotificationManager.Policy.getAllSuppressedVisualEffects());
if (Flags.modesApi()) {
mNm.setNotificationPolicy(newPolicy, /* fromUser= */ true);
} else {
mNm.setNotificationPolicy(newPolicy); mNm.setNotificationPolicy(newPolicy);
}
mMetrics.action(SettingsEnums.ACTION_ZEN_ONBOARDING_OK); mMetrics.action(SettingsEnums.ACTION_ZEN_ONBOARDING_OK);
} else { } else {
mMetrics.action(SettingsEnums.ACTION_ZEN_ONBOARDING_KEEP_CURRENT_SETTINGS); mMetrics.action(SettingsEnums.ACTION_ZEN_ONBOARDING_KEEP_CURRENT_SETTINGS);

View File

@@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
@@ -27,12 +28,15 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.app.AppOpsManager; import android.app.AppOpsManager;
import android.app.Flags;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -42,6 +46,7 @@ import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.RestrictedSwitchPreference; import com.android.settingslib.RestrictedSwitchPreference;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
@@ -50,6 +55,10 @@ import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class ApprovalPreferenceControllerTest { public class ApprovalPreferenceControllerTest {
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(
SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT);
private Context mContext; private Context mContext;
private FakeFeatureFactory mFeatureFactory; private FakeFeatureFactory mFeatureFactory;
@Mock @Mock
@@ -80,7 +89,6 @@ public class ApprovalPreferenceControllerTest {
mController.setNm(mNm); mController.setNm(mNm);
mController.setParent(mFragment); mController.setParent(mFragment);
mController.setPkgInfo(mPkgInfo); mController.setPkgInfo(mPkgInfo);
} }
@Test @Test
@@ -165,6 +173,7 @@ public class ApprovalPreferenceControllerTest {
} }
@Test @Test
@EnableFlags(Flags.FLAG_MODES_API)
public void disable() { public void disable() {
mController.disable(mCn); mController.disable(mCn);
verify(mFeatureFactory.metricsFeatureProvider).action( verify(mFeatureFactory.metricsFeatureProvider).action(
@@ -172,6 +181,7 @@ public class ApprovalPreferenceControllerTest {
MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_NOTIVIEW_ALLOW, MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_NOTIVIEW_ALLOW,
"a"); "a");
verify(mNm).removeAutomaticZenRules(eq(mCn.getPackageName()), eq(true));
verify(mNm).setNotificationListenerAccessGranted(mCn, false); verify(mNm).setNotificationListenerAccessGranted(mCn, false);
} }
} }