Add page for choosing calendar & reply for event modes.

Also adds the link to set trigger behavior to the main mode page (only for calendar so far), and the switch preference to enable/disable automatic rules (for all but manual DND mode).

Removes the "escape hatch" to allow manual mode to also use the new modes pages.

Ported from ZenModeEventRuleSettings.

Flag: android.app.modes_ui
Bug: 332730302
Test: ZenModeSetCalendarPreferenceControllerTest, ZenModeSetTriggerLinkPreferenceControllerTest, manual
Change-Id: Ia7a716c66663a21494a6c05711250a5bda87ca8c
This commit is contained in:
Yuri Lin
2024-05-22 11:08:13 -04:00
parent e968a66489
commit f29f44296a
11 changed files with 808 additions and 17 deletions

View File

@@ -0,0 +1,151 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.modes;
import static android.platform.test.flag.junit.SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT;
import static android.service.notification.ZenModeConfig.EventInfo.REPLY_YES;
import static com.android.settings.notification.modes.ZenModeSetCalendarPreferenceController.CALENDAR_NAME;
import static com.android.settings.notification.modes.ZenModeSetCalendarPreferenceController.KEY_CALENDAR;
import static com.android.settings.notification.modes.ZenModeSetCalendarPreferenceController.KEY_REPLY;
import static com.android.settings.notification.modes.ZenModeSetCalendarPreferenceController.addCalendar;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.app.AutomaticZenRule;
import android.app.Flags;
import android.content.Context;
import android.net.Uri;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.service.notification.ZenModeConfig;
import androidx.preference.DropDownPreference;
import androidx.preference.PreferenceCategory;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class ZenModeSetCalendarPreferenceControllerTest {
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(DEVICE_DEFAULT);
@Mock
private ZenModesBackend mBackend;
private Context mContext;
@Mock
private PreferenceCategory mPrefCategory;
private DropDownPreference mCalendar, mReply;
private ZenModeSetCalendarPreferenceController mPrefController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mCalendar = new DropDownPreference(mContext);
mReply = new DropDownPreference(mContext);
when(mPrefCategory.findPreference(KEY_CALENDAR)).thenReturn(mCalendar);
when(mPrefCategory.findPreference(KEY_REPLY)).thenReturn(mReply);
mPrefController = new ZenModeSetCalendarPreferenceController(mContext,
"zen_mode_event_category", mBackend);
}
@Test
@EnableFlags({Flags.FLAG_MODES_API, Flags.FLAG_MODES_UI})
public void updateEventMode_updatesConditionAndTriggerDescription() {
ZenMode mode = new ZenMode("id",
new AutomaticZenRule.Builder("name", Uri.parse("condition")).build(),
true); // is active
// Explicitly update preference controller with mode info first, which will also call
// updateState()
mPrefController.updateZenMode(mPrefCategory, mode);
ZenModeConfig.EventInfo eventInfo = new ZenModeConfig.EventInfo();
eventInfo.calendarId = 1L;
eventInfo.calName = "My events";
// apply event mode updater to existing mode
ZenMode out = mPrefController.updateEventMode(eventInfo).apply(mode);
assertThat(out.getRule().getConditionId()).isEqualTo(
ZenModeConfig.toEventConditionId(eventInfo));
assertThat(out.getRule().getTriggerDescription()).isEqualTo("My events");
}
@Test
public void updateState_setsPreferenceValues() {
ZenModeConfig.EventInfo eventInfo = new ZenModeConfig.EventInfo();
eventInfo.calendarId = 1L;
eventInfo.calName = "Definitely A Calendar";
eventInfo.reply = REPLY_YES;
ZenMode mode = new ZenMode("id",
new AutomaticZenRule.Builder("name",
ZenModeConfig.toEventConditionId(eventInfo)).build(),
true); // is active
mPrefController.updateZenMode(mPrefCategory, mode);
// We should see mCalendar, mReply have their values set
assertThat(mCalendar.getValue()).isEqualTo(
ZenModeSetCalendarPreferenceController.key(eventInfo.userId, eventInfo.calendarId,
eventInfo.calName));
assertThat(mReply.getValue()).isEqualTo(Integer.toString(eventInfo.reply));
}
@Test
public void testNoDuplicateCalendars() {
List<ZenModeSetCalendarPreferenceController.CalendarInfo> calendarsList = new ArrayList<>();
addCalendar(1234, "calName", 1, calendarsList);
addCalendar(1234, "calName", 2, calendarsList);
addCalendar(1234, "calName", 3, calendarsList);
assertThat(calendarsList).hasSize(1);
}
@Test
public void testCalendarInfoSortByName() {
List<ZenModeSetCalendarPreferenceController.CalendarInfo> calendarsList = new ArrayList<>();
addCalendar(123, "zyx", 1, calendarsList);
addCalendar(456, "wvu", 2, calendarsList);
addCalendar(789, "abc", 3, calendarsList);
Collections.sort(calendarsList, CALENDAR_NAME);
List<ZenModeSetCalendarPreferenceController.CalendarInfo> sortedList = new ArrayList<>();
addCalendar(789, "abc", 3, sortedList);
addCalendar(456, "wvu", 2, sortedList);
addCalendar(123, "zyx", 1, sortedList);
assertThat(calendarsList).containsExactlyElementsIn(sortedList).inOrder();
}
}

View File

@@ -0,0 +1,170 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.notification.modes;
import static android.app.AutomaticZenRule.TYPE_SCHEDULE_CALENDAR;
import static android.app.NotificationManager.INTERRUPTION_FILTER_PRIORITY;
import static android.platform.test.flag.junit.SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT;
import static com.android.settings.notification.modes.ZenModeSetTriggerLinkPreferenceController.AUTOMATIC_TRIGGER_PREF_KEY;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.AutomaticZenRule;
import android.app.Flags;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.service.notification.ZenModeConfig;
import android.service.notification.ZenPolicy;
import androidx.preference.PreferenceCategory;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settingslib.PrimarySwitchPreference;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class ZenModeSetTriggerLinkPreferenceControllerTest {
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(DEVICE_DEFAULT);
@Mock
private ZenModesBackend mBackend;
private Context mContext;
@Mock
private PreferenceCategory mPrefCategory;
@Mock
private PrimarySwitchPreference mPreference;
private ZenModeSetTriggerLinkPreferenceController mPrefController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mPrefController = new ZenModeSetTriggerLinkPreferenceController(mContext,
"zen_automatic_trigger_category", mBackend);
when(mPrefCategory.findPreference(AUTOMATIC_TRIGGER_PREF_KEY)).thenReturn(mPreference);
}
@Test
@EnableFlags(Flags.FLAG_MODES_UI)
public void testIsAvailable() {
// should not be available for manual DND
ZenMode manualMode = ZenMode.manualDndMode(new AutomaticZenRule.Builder("Do Not Disturb",
Uri.parse("manual"))
.setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY)
.build(), true);
mPrefController.updateZenMode(mPrefCategory, manualMode);
assertThat(mPrefController.isAvailable()).isFalse();
// should be available for other modes
ZenMode zenMode = new ZenMode("id",
new AutomaticZenRule.Builder("Driving", Uri.parse("drive"))
.setType(AutomaticZenRule.TYPE_DRIVING)
.setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY)
.setZenPolicy(new ZenPolicy.Builder().allowAlarms(true).build())
.setEnabled(false)
.build(), false);
mPrefController.updateZenMode(mPrefCategory, zenMode);
assertThat(mPrefController.isAvailable()).isTrue();
}
@Test
public void testUpdateState() {
ZenMode zenMode = new ZenMode("id",
new AutomaticZenRule.Builder("Driving", Uri.parse("drive"))
.setType(AutomaticZenRule.TYPE_DRIVING)
.setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY)
.setZenPolicy(new ZenPolicy.Builder().allowAlarms(true).build())
.setEnabled(false)
.build(), false);
// Update preference controller with a zen mode that is not enabled
mPrefController.updateZenMode(mPrefCategory, zenMode);
verify(mPreference).setChecked(false);
// Now with the rule enabled
zenMode.getRule().setEnabled(true);
mPrefController.updateZenMode(mPrefCategory, zenMode);
verify(mPreference).setChecked(true);
}
@Test
public void testOnPreferenceChange() {
ZenMode zenMode = new ZenMode("id",
new AutomaticZenRule.Builder("Driving", Uri.parse("drive"))
.setType(AutomaticZenRule.TYPE_DRIVING)
.setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY)
.setZenPolicy(new ZenPolicy.Builder().allowAlarms(true).build())
.setEnabled(false)
.build(), false);
// start with disabled rule
mPrefController.updateZenMode(mPrefCategory, zenMode);
// then update the preference to be checked
mPrefController.mSwitchChangeListener.onPreferenceChange(mPreference, true);
// 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();
}
@Test
public void testRuleLink_calendar() {
ZenModeConfig.EventInfo eventInfo = new ZenModeConfig.EventInfo();
eventInfo.calendarId = 1L;
eventInfo.calName = "My events";
ZenMode mode = new ZenMode("id", new AutomaticZenRule.Builder("name",
ZenModeConfig.toEventConditionId(eventInfo))
.setType(TYPE_SCHEDULE_CALENDAR)
.setTriggerDescription("My events")
.build(),
true); // is active
mPrefController.updateZenMode(mPrefCategory, mode);
verify(mPreference).setTitle(R.string.zen_mode_set_calendar_link);
verify(mPreference).setSummary(mode.getRule().getTriggerDescription());
ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
verify(mPreference).setIntent(captor.capture());
// Destination as written into the intent by SubSettingLauncher
assertThat(
captor.getValue().getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo(
ZenModeSetCalendarFragment.class.getName());
}
}