Add schedule setting page for time-based modes.

Creates new layout for setting the start & end time, and days of the week, for a schedule-based mode.

This is close to the mocks specified in https://screenshot.googleplex.com/8zmb7PAjjt73VkN, but with the following differences:
- the end time is left-aligned with the center rather than on the right side of the screen. This is a side effect of using LinearLayout to evenly space start & end times, and could in theory be fixed by using a ConstraintLayout, but that option seems to cause times to overlap instead of wrap when display size is cranked up. Could be fixed later.
- no icons yet on either side of the time display
- no Done button. Instead, has the "exit at alarm" switch that exists today.

Have not yet checked how this interacts with TalkBack, etc.

Flag: android.app.modes_ui
Bug: 332730302
Test: ZenModeSetSchedulePreferenceControllerTest, ZenModeExitAtAlarmPreferenceControllerTest, ZenModeSetTriggerLinkPreferenceControllerTest
Test: manual: interacting with UI in normal size, with font & display at minimum and maximum, and in locales (fr) where the first day of the week is a different day
Change-Id: I0b76f55891d6c12fc27720657c9eea6fe42fbafe
This commit is contained in:
Yuri Lin
2024-05-28 17:28:13 -04:00
parent cfaedb0199
commit 4203f311dd
13 changed files with 1129 additions and 3 deletions

View File

@@ -0,0 +1,115 @@
/*
* 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 com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import android.app.AutomaticZenRule;
import android.content.Context;
import android.service.notification.ZenModeConfig;
import androidx.preference.TwoStatePreference;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
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;
import java.util.Calendar;
@RunWith(RobolectricTestRunner.class)
public class ZenModeExitAtAlarmPreferenceControllerTest {
private Context mContext;
@Mock
private ZenModesBackend mBackend;
private ZenModeExitAtAlarmPreferenceController mPrefController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mPrefController = new ZenModeExitAtAlarmPreferenceController(mContext, "exit_at_alarm",
mBackend);
}
@Test
public void testUpdateState() {
TwoStatePreference preference = mock(TwoStatePreference.class);
// previously: don't exit at alarm
ZenModeConfig.ScheduleInfo scheduleInfo = new ZenModeConfig.ScheduleInfo();
scheduleInfo.days = new int[] { Calendar.MONDAY };
scheduleInfo.startHour = 1;
scheduleInfo.endHour = 2;
scheduleInfo.exitAtAlarm = false;
ZenMode mode = new ZenMode("id",
new AutomaticZenRule.Builder("name",
ZenModeConfig.toScheduleConditionId(scheduleInfo)).build(),
true); // is active
// need to call updateZenMode for the first call
mPrefController.updateZenMode(preference, mode);
verify(preference).setChecked(false);
// Now update state after changing exitAtAlarm
scheduleInfo.exitAtAlarm = true;
mode.getRule().setConditionId(ZenModeConfig.toScheduleConditionId(scheduleInfo));
// now can just call updateState
mPrefController.updateState(preference, mode);
verify(preference).setChecked(true);
}
@Test
public void testOnPreferenceChange() {
TwoStatePreference preference = mock(TwoStatePreference.class);
// previously: exit at alarm
ZenModeConfig.ScheduleInfo scheduleInfo = new ZenModeConfig.ScheduleInfo();
scheduleInfo.days = new int[] { Calendar.MONDAY };
scheduleInfo.startHour = 1;
scheduleInfo.endHour = 2;
scheduleInfo.exitAtAlarm = true;
ZenMode mode = new ZenMode("id",
new AutomaticZenRule.Builder("name",
ZenModeConfig.toScheduleConditionId(scheduleInfo)).build(),
true); // is active
mPrefController.updateZenMode(preference, mode);
// turn off exit at alarm
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());
assertThat(newSchedule.exitAtAlarm).isFalse();
// other properties remain the same
assertThat(newSchedule.startHour).isEqualTo(1);
assertThat(newSchedule.endHour).isEqualTo(2);
}
}

View File

@@ -0,0 +1,163 @@
/*
* 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 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.net.Uri;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.service.notification.ZenModeConfig;
import android.view.ViewGroup;
import android.widget.ToggleButton;
import androidx.fragment.app.Fragment;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
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.Calendar;
@RunWith(RobolectricTestRunner.class)
public class ZenModeSetSchedulePreferenceControllerTest {
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(DEVICE_DEFAULT);
@Mock
private ZenModesBackend mBackend;
private Context mContext;
@Mock
private Fragment mParent;
@Mock
private Calendar mCalendar;
@Mock
private ViewGroup mDaysContainer;
@Mock
private ToggleButton mDay0, mDay1, mDay2, mDay3, mDay4, mDay5, mDay6;
private ZenModeSetSchedulePreferenceController mPrefController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mPrefController = new ZenModeSetSchedulePreferenceController(mContext, mParent, "schedule",
mBackend);
setupMockDayContainer();
}
@Test
@EnableFlags({Flags.FLAG_MODES_API, Flags.FLAG_MODES_UI})
public void updateScheduleRule_updatesConditionAndTriggerDescription() {
ZenMode mode = new ZenMode("id",
new AutomaticZenRule.Builder("name", Uri.parse("condition")).build(),
true); // is active
ZenModeConfig.ScheduleInfo scheduleInfo = new ZenModeConfig.ScheduleInfo();
scheduleInfo.days = new int[] { Calendar.MONDAY };
scheduleInfo.startHour = 1;
scheduleInfo.endHour = 2;
ZenMode out = mPrefController.updateScheduleMode(scheduleInfo).apply(mode);
assertThat(out.getRule().getConditionId())
.isEqualTo(ZenModeConfig.toScheduleConditionId(scheduleInfo));
assertThat(out.getRule().getTriggerDescription()).isNotEmpty();
}
@Test
public void testUpdateScheduleDays() {
// Confirm that adding/subtracting/etc days works as expected
// starting from null: no days set
ZenModeConfig.ScheduleInfo schedule = new ZenModeConfig.ScheduleInfo();
// Unset a day that's already unset: nothing should change
assertThat(ZenModeSetSchedulePreferenceController.updateScheduleDays(schedule,
Calendar.TUESDAY, false)).isFalse();
// not explicitly checking whether schedule.days is still null here, as we don't necessarily
// want to require nullness as distinct from an empty list of days.
// set a few new days
assertThat(ZenModeSetSchedulePreferenceController.updateScheduleDays(schedule,
Calendar.MONDAY, true)).isTrue();
assertThat(ZenModeSetSchedulePreferenceController.updateScheduleDays(schedule,
Calendar.FRIDAY, true)).isTrue();
assertThat(schedule.days).hasLength(2);
assertThat(schedule.days).asList().containsExactly(Calendar.MONDAY, Calendar.FRIDAY);
// remove an existing day to make sure that works
assertThat(ZenModeSetSchedulePreferenceController.updateScheduleDays(schedule,
Calendar.MONDAY, false)).isTrue();
assertThat(schedule.days).hasLength(1);
assertThat(schedule.days).asList().containsExactly(Calendar.FRIDAY);
}
@Test
public void testSetupDayToggles_daysOfWeekOrder() {
// Confirm that days are correctly associated with the actual day of the week independent
// of when the first day of the week is for the given calendar.
ZenModeConfig.ScheduleInfo schedule = new ZenModeConfig.ScheduleInfo();
schedule.days = new int[] { Calendar.SUNDAY, Calendar.TUESDAY, Calendar.FRIDAY };
schedule.startHour = 1;
schedule.endHour = 5;
// Start mCalendar on Wednesday, arbitrarily
when(mCalendar.getFirstDayOfWeek()).thenReturn(Calendar.WEDNESDAY);
// Setup the day toggles
mPrefController.setupDayToggles(mDaysContainer, schedule, mCalendar);
// we should see toggle 0 associated with the first day of the week, etc.
// in this week order, schedule turns on friday (2), sunday (4), tuesday (6) so those
// should be checked while everything else should not be checked.
verify(mDay0).setChecked(false); // weds
verify(mDay1).setChecked(false); // thurs
verify(mDay2).setChecked(true); // fri
verify(mDay3).setChecked(false); // sat
verify(mDay4).setChecked(true); // sun
verify(mDay5).setChecked(false); // mon
verify(mDay6).setChecked(true); // tues
}
private void setupMockDayContainer() {
// associate each index (regardless of associated day of the week) with the appropriate
// res id in the days container
when(mDaysContainer.findViewById(R.id.day0)).thenReturn(mDay0);
when(mDaysContainer.findViewById(R.id.day1)).thenReturn(mDay1);
when(mDaysContainer.findViewById(R.id.day2)).thenReturn(mDay2);
when(mDaysContainer.findViewById(R.id.day3)).thenReturn(mDay3);
when(mDaysContainer.findViewById(R.id.day4)).thenReturn(mDay4);
when(mDaysContainer.findViewById(R.id.day5)).thenReturn(mDay5);
when(mDaysContainer.findViewById(R.id.day6)).thenReturn(mDay6);
}
}

View File

@@ -17,6 +17,7 @@
package com.android.settings.notification.modes;
import static android.app.AutomaticZenRule.TYPE_SCHEDULE_CALENDAR;
import static android.app.AutomaticZenRule.TYPE_SCHEDULE_TIME;
import static android.app.NotificationManager.INTERRUPTION_FILTER_PRIORITY;
import static android.platform.test.flag.junit.SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT;
@@ -53,6 +54,8 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import java.util.Calendar;
@RunWith(RobolectricTestRunner.class)
public class ZenModeSetTriggerLinkPreferenceControllerTest {
@Rule
@@ -167,4 +170,29 @@ public class ZenModeSetTriggerLinkPreferenceControllerTest {
captor.getValue().getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo(
ZenModeSetCalendarFragment.class.getName());
}
@Test
public void testRuleLink_schedule() {
ZenModeConfig.ScheduleInfo scheduleInfo = new ZenModeConfig.ScheduleInfo();
scheduleInfo.days = new int[] { Calendar.MONDAY, Calendar.TUESDAY, Calendar.THURSDAY };
scheduleInfo.startHour = 1;
scheduleInfo.endHour = 15;
ZenMode mode = new ZenMode("id", new AutomaticZenRule.Builder("name",
ZenModeConfig.toScheduleConditionId(scheduleInfo))
.setType(TYPE_SCHEDULE_TIME)
.setTriggerDescription("some schedule")
.build(),
true); // is active
mPrefController.updateZenMode(mPrefCategory, mode);
verify(mPreference).setTitle(R.string.zen_mode_set_schedule_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(
ZenModeSetScheduleFragment.class.getName());
}
}