Merge "Hide ZenMode.getRule() - Settings" into main

This commit is contained in:
Matías Hernández
2025-02-19 05:43:17 -08:00
committed by Android (Google) Code Review
18 changed files with 52 additions and 45 deletions

View File

@@ -48,21 +48,22 @@ class ConfigurationActivityHelper {
Intent getConfigurationActivityIntentForMode(ZenMode zenMode,
Function<ComponentName, ComponentInfo> approvedServiceFinder) {
String owner = zenMode.getRule().getPackageName();
ZenMode.Owner owner = zenMode.getOwner();
ComponentName configActivity = null;
if (zenMode.getRule().getConfigurationActivity() != null) {
if (owner.configurationActivity() != null) {
// If a configuration activity is present, use that directly in the intent
configActivity = zenMode.getRule().getConfigurationActivity();
configActivity = owner.configurationActivity();
} else {
// Otherwise, look for a condition provider service for the rule's package
ComponentInfo ci = approvedServiceFinder.apply(zenMode.getRule().getOwner());
ComponentInfo ci = approvedServiceFinder.apply(owner.conditionProvider());
if (ci != null) {
configActivity = extractConfigurationActivityFromComponent(ci);
}
}
if (configActivity != null
&& (owner == null || isSameOwnerPackage(owner, configActivity))
&& (owner.packageName() == null
|| isSameOwnerPackage(owner.packageName(), configActivity))
&& isResolvableActivity(configActivity)) {
return new Intent()
.setComponent(configActivity)

View File

@@ -51,7 +51,7 @@ class ZenModeButtonPreferenceController extends AbstractZenModePreferenceControl
@Override
public boolean isAvailable(ZenMode zenMode) {
return zenMode.isEnabled()
&& (zenMode.isActive() || zenMode.getRule().isManualInvocationAllowed());
&& (zenMode.isActive() || zenMode.isManualInvocationAllowed());
}
@Override

View File

@@ -54,8 +54,8 @@ public class ZenModeEditNameIconFragment extends ZenModeEditNameIconFragmentBase
return;
}
modeToUpdate.getRule().setName(mode.getRule().getName());
modeToUpdate.getRule().setIconResId(mode.getRule().getIconResId());
modeToUpdate.setName(mode.getName());
modeToUpdate.setIconResId(mode.getIconResId());
requireBackend().updateMode(modeToUpdate);
finish();
}

View File

@@ -128,13 +128,13 @@ public abstract class ZenModeEditNameIconFragmentBase extends DashboardFragment
@VisibleForTesting
final void setModeName(String name) {
checkNotNull(mZenMode).getRule().setName(Strings.nullToEmpty(name));
checkNotNull(mZenMode).setName(Strings.nullToEmpty(name));
forceUpdatePreferences(); // Updates confirmation button.
}
@VisibleForTesting
final void setModeIcon(@DrawableRes int iconResId) {
checkNotNull(mZenMode).getRule().setIconResId(iconResId);
checkNotNull(mZenMode).setIconResId(iconResId);
forceUpdatePreferences(); // Updates icon at the top.
}

View File

@@ -16,6 +16,8 @@
package com.android.settings.notification.modes;
import static com.google.common.base.Preconditions.checkNotNull;
import android.content.Context;
import android.service.notification.ZenModeConfig;
@@ -24,6 +26,7 @@ import androidx.preference.Preference;
import androidx.preference.TwoStatePreference;
import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.notification.modes.ZenModeSchedules;
import com.android.settingslib.notification.modes.ZenModesBackend;
/**
@@ -40,7 +43,7 @@ class ZenModeExitAtAlarmPreferenceController extends
@Override
public void updateState(Preference preference, @NonNull ZenMode zenMode) {
mSchedule = ZenModeConfig.tryParseScheduleConditionId(zenMode.getRule().getConditionId());
mSchedule = checkNotNull(ZenModeSchedules.getTimeSchedule(zenMode));
((TwoStatePreference) preference).setChecked(mSchedule.exitAtAlarm);
}

View File

@@ -47,8 +47,7 @@ public class ZenModeNewCustomFragment extends ZenModeEditNameIconFragmentBase {
? requireContext().getString(R.string.zen_mode_new_custom_default_name)
: mode.getName();
ZenMode created = requireBackend().addCustomManualMode(modeName,
mode.getRule().getIconResId());
ZenMode created = requireBackend().addCustomManualMode(modeName, mode.getIconResId());
if (created != null) {
// Open the mode view fragment and close the "add mode" fragment, so exiting the mode
// view goes back to previous screen (which should be the modes list).

View File

@@ -32,6 +32,7 @@ import androidx.preference.PreferenceCategory;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.notification.modes.ZenModeSchedules;
import com.android.settingslib.notification.modes.ZenModesBackend;
import java.util.ArrayList;
@@ -85,7 +86,7 @@ class ZenModeSetCalendarPreferenceController extends AbstractZenModePreferenceCo
mReply.setOnPreferenceChangeListener(mReplyChangeListener);
// Parse the zen mode's condition to update our EventInfo object.
mEvent = ZenModeConfig.tryParseEventConditionId(zenMode.getRule().getConditionId());
mEvent = ZenModeSchedules.getCalendarSchedule(zenMode);
if (mEvent != null) {
reloadCalendar();
updatePrefValues();

View File

@@ -32,6 +32,7 @@ import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.notification.modes.ZenModeSchedules;
import com.android.settingslib.notification.modes.ZenModesBackend;
import com.android.settingslib.widget.LayoutPreference;
@@ -62,7 +63,7 @@ class ZenModeSetSchedulePreferenceController extends AbstractZenModePreferenceCo
@Override
public void updateState(Preference preference, @NonNull ZenMode zenMode) {
mSchedule = ZenModeConfig.tryParseScheduleConditionId(zenMode.getRule().getConditionId());
mSchedule = ZenModeSchedules.getTimeSchedule(zenMode);
LayoutPreference layoutPref = (LayoutPreference) preference;
TextView start = layoutPref.findViewById(R.id.start_time);

View File

@@ -20,7 +20,6 @@ import static android.app.AutomaticZenRule.TYPE_BEDTIME;
import static android.app.AutomaticZenRule.TYPE_DRIVING;
import static android.app.AutomaticZenRule.TYPE_SCHEDULE_CALENDAR;
import static android.app.AutomaticZenRule.TYPE_SCHEDULE_TIME;
import static android.service.notification.ZenModeConfig.tryParseScheduleConditionId;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
@@ -43,6 +42,7 @@ import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settingslib.PrimarySwitchPreference;
import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.notification.modes.ZenModeSchedules;
import com.android.settingslib.notification.modes.ZenModesBackend;
import com.google.common.base.Strings;
@@ -105,8 +105,7 @@ class ZenModeTriggerUpdatePreferenceController extends AbstractZenModePreference
// [Clock Icon] 9:00 - 17:00 / Sun-Mon
preference.setIcon(com.android.internal.R.drawable.ic_zen_mode_type_schedule_time);
ZenModeConfig.ScheduleInfo schedule =
tryParseScheduleConditionId(mode.getRule().getConditionId());
ZenModeConfig.ScheduleInfo schedule = ZenModeSchedules.getTimeSchedule(mode);
if (schedule != null) {
preference.setTitle(SystemZenRules.getTimeSummary(mContext, schedule));
String shortDaysSummary = SystemZenRules.getDaysOfWeekShort(mContext, schedule);
@@ -138,7 +137,7 @@ class ZenModeTriggerUpdatePreferenceController extends AbstractZenModePreference
@SuppressLint("SwitchIntDef")
private void setUpForAppTrigger(Preference preference, ZenMode mode) {
// App-owned mode may have triggerDescription, configurationActivity, or both/neither.
mServiceListing.loadApprovedComponents(mode.getRule().getPackageName());
mServiceListing.loadApprovedComponents(mode.getOwnerPackage());
Intent configurationIntent =
mConfigurationActivityHelper.getConfigurationActivityIntentForMode(
mode, mServiceListing::findService);
@@ -152,11 +151,11 @@ class ZenModeTriggerUpdatePreferenceController extends AbstractZenModePreference
String summary;
if (!Strings.isNullOrEmpty(mode.getTriggerDescription())) {
summary = mode.getTriggerDescription();
} else if (!Strings.isNullOrEmpty(mode.getRule().getPackageName())) {
} else if (!Strings.isNullOrEmpty(mode.getOwnerPackage())) {
String appName = null;
try {
ApplicationInfo appInfo = mPackageManager.getApplicationInfo(
mode.getRule().getPackageName(), 0);
mode.getOwnerPackage(), 0);
appName = appInfo.loadLabel(mPackageManager).toString();
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Couldn't resolve owner for mode: " + mode);
@@ -219,7 +218,7 @@ class ZenModeTriggerUpdatePreferenceController extends AbstractZenModePreference
private void setModeEnabled(boolean enabled) {
saveMode((zenMode) -> {
if (enabled != zenMode.isEnabled()) {
zenMode.getRule().setEnabled(enabled);
zenMode.setEnabled(enabled);
}
return zenMode;
});

View File

@@ -125,7 +125,7 @@ public class ZenModesListFragment extends ZenModesFragmentBase {
// If we find a new mode owned by the same package, presumably that's it. Open its page.
Optional<ZenMode> createdZenMode = mBackend.getModes().stream()
.filter(m -> !previousIds.contains(m.getId()))
.filter(m -> m.getRule().getPackageName().equals(activityInvoked.getPackageName()))
.filter(m -> activityInvoked.getPackageName().equals(m.getOwnerPackage()))
.findFirst();
createdZenMode.ifPresent(
mode ->

View File

@@ -130,7 +130,7 @@ public final class InterruptionFilterPreferenceControllerTest {
verify(mBackend).updateMode(captor.capture());
assertThat(captor.getValue().getPolicy().getPriorityCategoryAlarms())
.isEqualTo(STATE_DISALLOW);
assertThat(captor.getValue().getRule().getInterruptionFilter())
assertThat(captor.getValue().getInterruptionFilter())
.isEqualTo(INTERRUPTION_FILTER_PRIORITY);
}
@@ -162,7 +162,7 @@ public final class InterruptionFilterPreferenceControllerTest {
verify(mBackend).updateMode(captor.capture());
assertThat(captor.getValue().getPolicy().getPriorityCategoryAlarms())
.isEqualTo(STATE_DISALLOW);
assertThat(captor.getValue().getRule().getInterruptionFilter())
assertThat(captor.getValue().getInterruptionFilter())
.isEqualTo(INTERRUPTION_FILTER_ALL);
}
}

View File

@@ -183,7 +183,7 @@ public final class ZenModeAppsPreferenceControllerTest {
// NONE is not actually propagated to the backend as an interruption filter;
// the filter is set to priority, and sounds and visual effects are disallowed.
// See AbstractZenModePreferenceController.
assertThat(captor.getValue().getRule().getInterruptionFilter())
assertThat(captor.getValue().getInterruptionFilter())
.isEqualTo(INTERRUPTION_FILTER_PRIORITY);
// After screen is refreshed, NONE is now checked; others are unchecked.
@@ -217,7 +217,7 @@ public final class ZenModeAppsPreferenceControllerTest {
ArgumentCaptor<ZenMode> captor = ArgumentCaptor.forClass(ZenMode.class);
verify(mBackend).updateMode(captor.capture());
// Checks the policy value for PRIORITY is propagated to the backend.
assertThat(captor.getValue().getRule().getInterruptionFilter())
assertThat(captor.getValue().getInterruptionFilter())
.isEqualTo(INTERRUPTION_FILTER_PRIORITY);
// After screen is refreshed, PRIORITY is now checked; others are unchecked.

View File

@@ -124,7 +124,7 @@ public class ZenModeEditNameIconFragmentTest {
verify(mBackend).updateMode(captor.capture());
ZenMode savedMode = captor.getValue();
assertThat(savedMode.getName()).isEqualTo("A newer name");
assertThat(savedMode.getRule().getIconResId()).isEqualTo(
assertThat(savedMode.getIconKey().resId()).isEqualTo(
R.drawable.ic_zen_mode_type_theater);
assertThat(mActivity.isFinishing()).isTrue();

View File

@@ -34,6 +34,7 @@ import androidx.test.core.app.ApplicationProvider;
import com.android.settingslib.notification.modes.TestModeBuilder;
import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.notification.modes.ZenModeSchedules;
import com.android.settingslib.notification.modes.ZenModesBackend;
import org.junit.Before;
@@ -89,7 +90,7 @@ public class ZenModeExitAtAlarmPreferenceControllerTest {
// Now update state after changing exitAtAlarm
scheduleInfo.exitAtAlarm = true;
mode.getRule().setConditionId(ZenModeConfig.toScheduleConditionId(scheduleInfo));
mode.setCustomModeConditionId(mContext, ZenModeConfig.toScheduleConditionId(scheduleInfo));
// now can just call updateState
mPrefController.updateState(preference, mode);
@@ -117,8 +118,8 @@ public class ZenModeExitAtAlarmPreferenceControllerTest {
mPrefController.onPreferenceChange(preference, false);
ArgumentCaptor<ZenMode> captor = ArgumentCaptor.forClass(ZenMode.class);
verify(mBackend).updateMode(captor.capture());
ZenModeConfig.ScheduleInfo newSchedule = ZenModeConfig.tryParseScheduleConditionId(
captor.getValue().getRule().getConditionId());
ZenModeConfig.ScheduleInfo newSchedule = ZenModeSchedules.getTimeSchedule(
captor.getValue());
assertThat(newSchedule.exitAtAlarm).isFalse();
// other properties remain the same

View File

@@ -41,6 +41,7 @@ import androidx.test.core.app.ApplicationProvider;
import com.android.settingslib.notification.modes.TestModeBuilder;
import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.notification.modes.ZenModeSchedules;
import com.android.settingslib.notification.modes.ZenModesBackend;
import org.junit.Before;
@@ -102,10 +103,10 @@ public class ZenModeSetCalendarPreferenceControllerTest {
// apply event mode updater to existing mode
ZenMode out = mPrefController.updateEventMode(eventInfo).apply(mode);
assertThat(out.getRule().getOwner()).isEqualTo(ZenModeConfig.getEventConditionProvider());
assertThat(out.getRule().getConditionId()).isEqualTo(
ZenModeConfig.toEventConditionId(eventInfo));
assertThat(out.getRule().getTriggerDescription()).isEqualTo("My events");
assertThat(ZenModeSchedules.getCalendarSchedule(out)).isEqualTo(eventInfo);
assertThat(out.getOwner().conditionProvider()).isEqualTo(
ZenModeConfig.getEventConditionProvider());
assertThat(out.getTriggerDescription()).isEqualTo("My events");
}
@Test

View File

@@ -38,6 +38,7 @@ import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.notification.modes.TestModeBuilder;
import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.notification.modes.ZenModeSchedules;
import com.android.settingslib.notification.modes.ZenModesBackend;
import org.junit.Before;
@@ -92,11 +93,10 @@ public class ZenModeSetSchedulePreferenceControllerTest {
scheduleInfo.endHour = 2;
ZenMode out = mPrefController.updateScheduleMode(scheduleInfo).apply(mode);
assertThat(out.getRule().getConditionId())
.isEqualTo(ZenModeConfig.toScheduleConditionId(scheduleInfo));
assertThat(out.getRule().getTriggerDescription()).isNotEmpty();
assertThat(out.getRule().getOwner()).isEqualTo(
assertThat(ZenModeSchedules.getTimeSchedule(out)).isEqualTo(scheduleInfo);
assertThat(out.getOwner().conditionProvider()).isEqualTo(
ZenModeConfig.getScheduleConditionProvider());
assertThat(out.getTriggerDescription()).isNotEmpty();
}
@Test

View File

@@ -44,6 +44,7 @@ import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.notification.modes.TestModeBuilder;
import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.notification.modes.ZenModeSchedules;
import com.android.settingslib.notification.modes.ZenModesBackend;
import org.junit.Before;
@@ -168,9 +169,9 @@ public class ZenModeTriggerAddPreferenceControllerTest {
verify(mBackend).updateMode(captor.capture());
ZenMode updatedMode = captor.getValue();
assertThat(updatedMode.getType()).isEqualTo(TYPE_SCHEDULE_TIME);
assertThat(updatedMode.getRule().getConditionId()).isEqualTo(scheduleUri);
assertThat(updatedMode.getRule().getTriggerDescription()).isNotEmpty();
assertThat(updatedMode.getRule().getOwner()).isEqualTo(
assertThat(ZenModeSchedules.getTimeSchedule(updatedMode)).isEqualTo(scheduleInfo);
assertThat(updatedMode.getTriggerDescription()).isNotEmpty();
assertThat(updatedMode.getOwner().conditionProvider()).isEqualTo(
ZenModeConfig.getScheduleConditionProvider());
}
}

View File

@@ -161,7 +161,7 @@ public class ZenModeTriggerUpdatePreferenceControllerTest {
assertThat(mPreference.getCheckedState()).isFalse();
// Now with the rule enabled
zenMode.getRule().setEnabled(true);
zenMode.setEnabled(true);
mController.updateZenMode(mPreference, zenMode);
assertThat(mPreference.getCheckedState()).isTrue();
}
@@ -189,7 +189,7 @@ public class ZenModeTriggerUpdatePreferenceControllerTest {
// Verify the backend got asked to update the mode to be enabled
ArgumentCaptor<ZenMode> captor = ArgumentCaptor.forClass(ZenMode.class);
verify(mBackend).updateMode(captor.capture());
assertThat(captor.getValue().getRule().isEnabled()).isTrue();
assertThat(captor.getValue().isEnabled()).isTrue();
assertThat(ShadowAlertDialog.getLatestAlertDialog().isShowing()).isFalse();
}
@@ -216,7 +216,7 @@ public class ZenModeTriggerUpdatePreferenceControllerTest {
// Verify the backend got asked to update the mode to be disabled
ArgumentCaptor<ZenMode> captor = ArgumentCaptor.forClass(ZenMode.class);
verify(mBackend).updateMode(captor.capture());
assertThat(captor.getValue().getRule().isEnabled()).isFalse();
assertThat(captor.getValue().isEnabled()).isFalse();
assertThat(ShadowAlertDialog.getLatestAlertDialog().isShowing()).isFalse();
}