Use CustomDialogPreferenceCompat for ZenDuration
Test: ZenModeDurationPreferenceControllerTest Bug: 112041657 Change-Id: I0158a703d26f73512c560889d6549d68230bf98e
This commit is contained in:
@@ -56,9 +56,10 @@
|
|||||||
android:key="zen_mode_settings_category_schedule"
|
android:key="zen_mode_settings_category_schedule"
|
||||||
android:title="@string/zen_category_schedule">
|
android:title="@string/zen_category_schedule">
|
||||||
<!-- DND duration settings -->
|
<!-- DND duration settings -->
|
||||||
<Preference
|
<com.android.settings.notification.ZenDurationDialogPreference
|
||||||
android:key="zen_mode_duration_settings"
|
android:key="zen_mode_duration_settings"
|
||||||
android:title="@string/zen_mode_duration_settings_title" />
|
android:title="@string/zen_mode_duration_settings_title"
|
||||||
|
android:widgetLayout="@null"/>
|
||||||
|
|
||||||
<!-- Automatic rules -->
|
<!-- Automatic rules -->
|
||||||
<Preference
|
<Preference
|
||||||
|
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 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;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
|
||||||
|
import com.android.settingslib.CustomDialogPreferenceCompat;
|
||||||
|
import com.android.settingslib.notification.ZenDurationDialog;
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AlertDialog;
|
||||||
|
|
||||||
|
public class ZenDurationDialogPreference extends CustomDialogPreferenceCompat {
|
||||||
|
|
||||||
|
public ZenDurationDialogPreference(Context context, AttributeSet attrs, int defStyleAttr,
|
||||||
|
int defStyleRes) {
|
||||||
|
super(context, attrs, defStyleAttr, defStyleRes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ZenDurationDialogPreference(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||||
|
super(context, attrs, defStyleAttr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ZenDurationDialogPreference(Context context, AttributeSet attrs) {
|
||||||
|
super(context, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPrepareDialogBuilder(AlertDialog.Builder builder,
|
||||||
|
DialogInterface.OnClickListener listener) {
|
||||||
|
super.onPrepareDialogBuilder(builder, listener);
|
||||||
|
|
||||||
|
ZenDurationDialog zenDialog = new ZenDurationDialog(getContext());
|
||||||
|
zenDialog.setupDialog(builder);
|
||||||
|
}
|
||||||
|
}
|
@@ -22,21 +22,13 @@ import com.android.settings.R;
|
|||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
|
|
||||||
import androidx.fragment.app.FragmentManager;
|
|
||||||
import androidx.preference.Preference;
|
|
||||||
import androidx.preference.PreferenceScreen;
|
|
||||||
|
|
||||||
public class ZenModeDurationPreferenceController extends AbstractZenModePreferenceController
|
public class ZenModeDurationPreferenceController extends AbstractZenModePreferenceController
|
||||||
implements PreferenceControllerMixin, Preference.OnPreferenceClickListener {
|
implements PreferenceControllerMixin {
|
||||||
|
|
||||||
private static final String TAG = "ZenModeDurationDialog";
|
|
||||||
protected static final String KEY = "zen_mode_duration_settings";
|
protected static final String KEY = "zen_mode_duration_settings";
|
||||||
private FragmentManager mFragment;
|
|
||||||
|
|
||||||
public ZenModeDurationPreferenceController(Context context, Lifecycle lifecycle, FragmentManager
|
public ZenModeDurationPreferenceController(Context context, Lifecycle lifecycle) {
|
||||||
fragment) {
|
|
||||||
super(context, KEY, lifecycle);
|
super(context, KEY, lifecycle);
|
||||||
mFragment = fragment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -50,16 +42,8 @@ public class ZenModeDurationPreferenceController extends AbstractZenModePreferen
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void displayPreference(PreferenceScreen screen) {
|
public CharSequence getSummary() {
|
||||||
super.displayPreference(screen);
|
String summary;
|
||||||
screen.findPreference(KEY).setOnPreferenceClickListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void updateState(Preference preference) {
|
|
||||||
super.updateState(preference);
|
|
||||||
|
|
||||||
String summary = "";
|
|
||||||
int zenDuration = getZenDuration();
|
int zenDuration = getZenDuration();
|
||||||
if (zenDuration < 0) {
|
if (zenDuration < 0) {
|
||||||
summary = mContext.getString(R.string.zen_mode_duration_summary_always_prompt);
|
summary = mContext.getString(R.string.zen_mode_duration_summary_always_prompt);
|
||||||
@@ -76,12 +60,6 @@ public class ZenModeDurationPreferenceController extends AbstractZenModePreferen
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
preference.setSummary(summary);
|
return summary;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onPreferenceClick(Preference preference) {
|
|
||||||
new SettingsZenDurationDialog().show(mFragment, TAG);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -86,8 +86,7 @@ public class ZenModeSettings extends ZenModeSettingsBase {
|
|||||||
controllers.add(new ZenModeBehaviorSoundPreferenceController(context, lifecycle));
|
controllers.add(new ZenModeBehaviorSoundPreferenceController(context, lifecycle));
|
||||||
controllers.add(new ZenModeBehaviorCallsPreferenceController(context, lifecycle));
|
controllers.add(new ZenModeBehaviorCallsPreferenceController(context, lifecycle));
|
||||||
controllers.add(new ZenModeBlockedEffectsPreferenceController(context, lifecycle));
|
controllers.add(new ZenModeBlockedEffectsPreferenceController(context, lifecycle));
|
||||||
controllers.add(new ZenModeDurationPreferenceController(context, lifecycle,
|
controllers.add(new ZenModeDurationPreferenceController(context, lifecycle));
|
||||||
fragmentManager));
|
|
||||||
controllers.add(new ZenModeAutomationPreferenceController(context));
|
controllers.add(new ZenModeAutomationPreferenceController(context));
|
||||||
controllers.add(new ZenModeButtonPreferenceController(context, lifecycle, fragmentManager));
|
controllers.add(new ZenModeButtonPreferenceController(context, lifecycle, fragmentManager));
|
||||||
controllers.add(new ZenModeSettingsFooterPreferenceController(context, lifecycle));
|
controllers.add(new ZenModeSettingsFooterPreferenceController(context, lifecycle));
|
||||||
|
@@ -16,8 +16,8 @@
|
|||||||
|
|
||||||
package com.android.settings.notification;
|
package com.android.settings.notification;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
@@ -38,10 +38,6 @@ import org.robolectric.RuntimeEnvironment;
|
|||||||
import org.robolectric.shadows.ShadowApplication;
|
import org.robolectric.shadows.ShadowApplication;
|
||||||
import org.robolectric.util.ReflectionHelpers;
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
|
||||||
import androidx.fragment.app.FragmentManager;
|
|
||||||
import androidx.preference.Preference;
|
|
||||||
import androidx.preference.PreferenceScreen;
|
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
public class ZenModeDurationPreferenceControllerTest {
|
public class ZenModeDurationPreferenceControllerTest {
|
||||||
private ZenModeDurationPreferenceController mController;
|
private ZenModeDurationPreferenceController mController;
|
||||||
@@ -51,11 +47,7 @@ public class ZenModeDurationPreferenceControllerTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private NotificationManager mNotificationManager;
|
private NotificationManager mNotificationManager;
|
||||||
@Mock
|
@Mock
|
||||||
private Preference mockPref;
|
|
||||||
@Mock
|
|
||||||
private NotificationManager.Policy mPolicy;
|
private NotificationManager.Policy mPolicy;
|
||||||
@Mock
|
|
||||||
private PreferenceScreen mPreferenceScreen;
|
|
||||||
private ContentResolver mContentResolver;
|
private ContentResolver mContentResolver;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
|
||||||
@@ -67,34 +59,27 @@ public class ZenModeDurationPreferenceControllerTest {
|
|||||||
|
|
||||||
mContext = shadowApplication.getApplicationContext();
|
mContext = shadowApplication.getApplicationContext();
|
||||||
mContentResolver = RuntimeEnvironment.application.getContentResolver();
|
mContentResolver = RuntimeEnvironment.application.getContentResolver();
|
||||||
mController = new ZenModeDurationPreferenceController(mContext, mock(Lifecycle.class),
|
mController = new ZenModeDurationPreferenceController(mContext, mock(Lifecycle.class));
|
||||||
mock(FragmentManager.class));
|
|
||||||
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
|
when(mNotificationManager.getNotificationPolicy()).thenReturn(mPolicy);
|
||||||
ReflectionHelpers.setField(mController, "mBackend", mBackend);
|
ReflectionHelpers.setField(mController, "mBackend", mBackend);
|
||||||
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
|
|
||||||
mockPref);
|
|
||||||
mController.displayPreference(mPreferenceScreen);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateState_DurationForever() {
|
public void updateState_DurationForever() {
|
||||||
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
|
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
|
||||||
Settings.Secure.ZEN_DURATION_FOREVER);
|
Settings.Secure.ZEN_DURATION_FOREVER);
|
||||||
final Preference mockPref = mock(Preference.class);
|
|
||||||
mController.updateState(mockPref);
|
|
||||||
|
|
||||||
verify(mockPref).setSummary(mContext.getString(R.string.zen_mode_duration_summary_forever));
|
assertEquals(mContext.getString(R.string.zen_mode_duration_summary_forever),
|
||||||
|
mController.getSummary());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateState_DurationPrompt() {
|
public void updateState_DurationPrompt() {
|
||||||
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
|
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
|
||||||
Settings.Secure.ZEN_DURATION_PROMPT);
|
Settings.Secure.ZEN_DURATION_PROMPT);
|
||||||
final Preference mockPref = mock(Preference.class);
|
|
||||||
mController.updateState(mockPref);
|
|
||||||
|
|
||||||
verify(mockPref).setSummary(mContext.getString(
|
assertEquals(mContext.getString(R.string.zen_mode_duration_summary_always_prompt),
|
||||||
R.string.zen_mode_duration_summary_always_prompt));
|
mController.getSummary());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -102,10 +87,8 @@ public class ZenModeDurationPreferenceControllerTest {
|
|||||||
int zenDuration = 45;
|
int zenDuration = 45;
|
||||||
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
|
Settings.Secure.putInt(mContentResolver, Settings.Secure.ZEN_DURATION,
|
||||||
zenDuration);
|
zenDuration);
|
||||||
final Preference mockPref = mock(Preference.class);
|
|
||||||
mController.updateState(mockPref);
|
|
||||||
|
|
||||||
verify(mockPref).setSummary(mContext.getResources().getString(
|
assertEquals(mContext.getString(R.string.zen_mode_duration_summary_time_minutes,
|
||||||
R.string.zen_mode_duration_summary_time_minutes, zenDuration));
|
zenDuration), mController.getSummary());
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user