Migrate "duration for quick settings" to new modes page.

This mostly continues to use the existing dialogs in settingslib (ZenDurationDialog, SettingsEnableZenModeDialog) and creates a controller to read from the settings value.

Also updates the "turn on / turn off" button to respect the preferred duration.

Flag: android.app.modes_ui
Bug: 343444249
Test: ZenModeButtonPreferenceControllerTest, ManualDurationPreferenceControllerTest
Change-Id: I2fd49a79d9a5807fefdd7ec310a6cc60d70f9bb1
This commit is contained in:
Yuri Lin
2024-06-20 17:26:23 -04:00
parent 5653acd533
commit c1a4abbc51
8 changed files with 473 additions and 6 deletions

View File

@@ -0,0 +1,77 @@
/*
* 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 android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class ManualDurationHelperTest {
private Context mContext;
private ContentResolver mContentResolver;
private ManualDurationHelper mHelper;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
mHelper = new ManualDurationHelper(mContext);
}
@Test
public void getDurationSummary_durationForever() {
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
Settings.Secure.ZEN_DURATION_FOREVER);
assertThat(mHelper.getSummary()).isEqualTo(
mContext.getString(R.string.zen_mode_duration_summary_forever));
}
@Test
public void getDurationSummary_durationPrompt() {
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
Settings.Secure.ZEN_DURATION_PROMPT);
assertThat(mHelper.getSummary()).isEqualTo(
mContext.getString(R.string.zen_mode_duration_summary_always_prompt));
}
@Test
public void getDurationSummary_durationCustom() {
// minutes
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION, 45);
assertThat(mHelper.getSummary()).isEqualTo("45 minutes");
// hours
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION, 300);
assertThat(mHelper.getSummary()).isEqualTo("5 hours");
}
}

View File

@@ -0,0 +1,91 @@
/*
* 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 android.app.AutomaticZenRule;
import android.app.Flags;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.notification.modes.ZenModesBackend;
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 org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@EnableFlags(Flags.FLAG_MODES_UI)
public class ManualDurationPreferenceControllerTest {
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private Context mContext;
private ContentResolver mContentResolver;
@Mock
private ZenModesBackend mBackend;
@Mock
private Fragment mParent;
private ManualDurationPreferenceController mPrefController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
mPrefController = new ManualDurationPreferenceController(mContext, "key", mParent,
mBackend);
}
@Test
public void testIsAvailable_onlyForManualDnd() {
assertThat(mPrefController.isAvailable(TestModeBuilder.EXAMPLE)).isFalse();
ZenMode manualDnd = ZenMode.manualDndMode(
new AutomaticZenRule.Builder("id", Uri.EMPTY).build(), false);
assertThat(mPrefController.isAvailable(manualDnd)).isTrue();
}
@Test
public void testUpdateState_durationSummary() {
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
45 /* minutes */);
Preference pref = new Preference(mContext);
mPrefController.updateState(pref, TestModeBuilder.EXAMPLE);
assertThat(pref.getSummary().toString()).contains("45");
}
}

View File

@@ -23,12 +23,18 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.AutomaticZenRule;
import android.app.Flags;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.widget.Button;
import androidx.fragment.app.Fragment;
import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.notification.modes.ZenModesBackend;
import com.android.settingslib.widget.LayoutPreference;
@@ -42,6 +48,8 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.time.Duration;
@EnableFlags(Flags.FLAG_MODES_UI)
@RunWith(RobolectricTestRunner.class)
public final class ZenModeButtonPreferenceControllerTest {
@@ -51,19 +59,24 @@ public final class ZenModeButtonPreferenceControllerTest {
@Rule
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
private Context mContext;
private ContentResolver mContentResolver;
@Mock
private ZenModesBackend mBackend;
@Mock
private Fragment mParent;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mContentResolver = RuntimeEnvironment.application.getContentResolver();
mController = new ZenModeButtonPreferenceController(
mContext, "something", mBackend);
mContext, "something", mParent, mBackend);
}
@Test
@@ -162,4 +175,34 @@ public final class ZenModeButtonPreferenceControllerTest {
button.callOnClick();
verify(mBackend).activateMode(zenMode, null);
}
@Test
public void updateStateThenClick_withDuration() {
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
45 /* minutes */);
Button button = new Button(mContext);
LayoutPreference pref = mock(LayoutPreference.class);
when(pref.findViewById(anyInt())).thenReturn(button);
ZenMode zenMode = ZenMode.manualDndMode(
new AutomaticZenRule.Builder("manual", Uri.EMPTY).build(), false);
mController.updateZenMode(pref, zenMode);
button.callOnClick();
verify(mBackend).activateMode(zenMode, Duration.ofMinutes(45));
}
@Test
public void updateStateThenClick_durationForever() {
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
Settings.Secure.ZEN_DURATION_FOREVER);
Button button = new Button(mContext);
LayoutPreference pref = mock(LayoutPreference.class);
when(pref.findViewById(anyInt())).thenReturn(button);
ZenMode zenMode = ZenMode.manualDndMode(
new AutomaticZenRule.Builder("manual", Uri.EMPTY).build(), false);
mController.updateZenMode(pref, zenMode);
button.callOnClick();
verify(mBackend).activateMode(zenMode, null);
}
}