Merge "Convert Blink light to TogglePreferenceController"

This commit is contained in:
TreeHugger Robot
2018-04-18 17:49:52 +00:00
committed by Android (Google) Code Review
4 changed files with 90 additions and 45 deletions

View File

@@ -33,7 +33,8 @@
<!-- Pulse notification light --> <!-- Pulse notification light -->
<SwitchPreference <SwitchPreference
android:key="notification_pulse" android:key="notification_pulse"
android:title="@string/notification_pulse_title"/> android:title="@string/notification_pulse_title"
settings:controller="com.android.settings.notification.PulseNotificationPreferenceController" />
<!-- Default notification ringtone --> <!-- Default notification ringtone -->
<com.android.settings.DefaultRingtonePreference <com.android.settings.DefaultRingtonePreference

View File

@@ -26,14 +26,12 @@ import android.os.UserHandle;
import android.provider.SearchIndexableResource; import android.provider.SearchIndexableResource;
import android.support.annotation.VisibleForTesting; import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.text.TextUtils;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.RingtonePreference; import com.android.settings.RingtonePreference;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.dashboard.SummaryLoader; import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.gestures.SwipeToNotificationPreferenceController;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable; import com.android.settings.search.Indexable;
import com.android.settingslib.core.AbstractPreferenceController; import com.android.settingslib.core.AbstractPreferenceController;
@@ -97,21 +95,17 @@ public class ConfigureNotificationSettings extends DashboardFragment {
final List<AbstractPreferenceController> controllers = new ArrayList<>(); final List<AbstractPreferenceController> controllers = new ArrayList<>();
final BadgingNotificationPreferenceController badgeController = final BadgingNotificationPreferenceController badgeController =
new BadgingNotificationPreferenceController(context); new BadgingNotificationPreferenceController(context);
final PulseNotificationPreferenceController pulseController =
new PulseNotificationPreferenceController(context);
final LockScreenNotificationPreferenceController lockScreenNotificationController = final LockScreenNotificationPreferenceController lockScreenNotificationController =
new LockScreenNotificationPreferenceController(context, new LockScreenNotificationPreferenceController(context,
KEY_LOCKSCREEN, KEY_LOCKSCREEN,
KEY_LOCKSCREEN_WORK_PROFILE_HEADER, KEY_LOCKSCREEN_WORK_PROFILE_HEADER,
KEY_LOCKSCREEN_WORK_PROFILE); KEY_LOCKSCREEN_WORK_PROFILE);
if (lifecycle != null) { if (lifecycle != null) {
lifecycle.addObserver(pulseController);
lifecycle.addObserver(lockScreenNotificationController); lifecycle.addObserver(lockScreenNotificationController);
} }
controllers.add(new RecentNotifyingAppsPreferenceController( controllers.add(new RecentNotifyingAppsPreferenceController(
context, new NotificationBackend(), app, host)); context, new NotificationBackend(), app, host));
controllers.add(badgeController); controllers.add(badgeController);
controllers.add(pulseController);
controllers.add(lockScreenNotificationController); controllers.add(lockScreenNotificationController);
controllers.add(new NotificationRingtonePreferenceController(context) { controllers.add(new NotificationRingtonePreferenceController(context) {
@Override @Override

View File

@@ -24,33 +24,28 @@ import android.os.Handler;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference;
import android.util.Log;
import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause; import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume; import com.android.settingslib.core.lifecycle.events.OnResume;
import static android.provider.Settings.System.NOTIFICATION_LIGHT_PULSE; import static android.provider.Settings.System.NOTIFICATION_LIGHT_PULSE;
public class PulseNotificationPreferenceController extends AbstractPreferenceController public class PulseNotificationPreferenceController extends TogglePreferenceController
implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener, implements OnResume, OnPause {
LifecycleObserver, OnResume, OnPause {
private static final String TAG = "PulseNotifPrefContr"; private static final int ON = 1;
private static final String KEY_NOTIFICATION_PULSE = "notification_pulse"; private static final int OFF = 0;
private SettingObserver mSettingObserver; private SettingObserver mSettingObserver;
public PulseNotificationPreferenceController(Context context) { public PulseNotificationPreferenceController(Context context, String key) {
super(context); super(context, key);
} }
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
Preference preference = screen.findPreference(KEY_NOTIFICATION_PULSE); Preference preference = screen.findPreference(getPreferenceKey());
if (preference != null) { if (preference != null) {
mSettingObserver = new SettingObserver(preference); mSettingObserver = new SettingObserver(preference);
} }
@@ -71,32 +66,22 @@ public class PulseNotificationPreferenceController extends AbstractPreferenceCon
} }
@Override @Override
public String getPreferenceKey() { public int getAvailabilityStatus() {
return KEY_NOTIFICATION_PULSE; return mContext.getResources().getBoolean(
com.android.internal.R.bool.config_intrusiveNotificationLed) ? AVAILABLE
: DISABLED_UNSUPPORTED;
} }
@Override @Override
public boolean isAvailable() { public boolean isChecked() {
return mContext.getResources() return Settings.System.getInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, OFF)
.getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed); == ON;
} }
@Override @Override
public void updateState(Preference preference) { public boolean setChecked(boolean isChecked) {
try { return Settings.System.putInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE,
final boolean checked = Settings.System.getInt(mContext.getContentResolver(), isChecked ? ON : OFF);
NOTIFICATION_LIGHT_PULSE) == 1;
((TwoStatePreference) preference).setChecked(checked);
} catch (Settings.SettingNotFoundException snfe) {
Log.e(TAG, NOTIFICATION_LIGHT_PULSE + " not found");
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean val = (Boolean) newValue;
return Settings.System.putInt(mContext.getContentResolver(),
NOTIFICATION_LIGHT_PULSE, val ? 1 : 0);
} }
class SettingObserver extends ContentObserver { class SettingObserver extends ContentObserver {

View File

@@ -19,15 +19,19 @@ package com.android.settings.notification;
import static android.provider.Settings.System.NOTIFICATION_LIGHT_PULSE; import static android.provider.Settings.System.NOTIFICATION_LIGHT_PULSE;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.provider.Settings; import android.provider.Settings;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import android.support.v7.preference.TwoStatePreference; import android.support.v7.preference.TwoStatePreference;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -40,8 +44,9 @@ import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class PulseNotificationPreferenceControllerTest { public class PulseNotificationPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext; private Context mContext;
@Mock
private Resources mResources;
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen; private PreferenceScreen mScreen;
@@ -51,8 +56,11 @@ public class PulseNotificationPreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mController = new PulseNotificationPreferenceController(mContext); mContext = spy(RuntimeEnvironment.application);
mPreference = new Preference(RuntimeEnvironment.application); when(mContext.getResources()).thenReturn(mResources);
mController = new PulseNotificationPreferenceController(mContext, "testkey");
mPreference = new Preference(mContext);
mPreference.setKey(mController.getPreferenceKey()); mPreference.setKey(mController.getPreferenceKey());
when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
} }
@@ -62,6 +70,7 @@ public class PulseNotificationPreferenceControllerTest {
when(mContext.getResources(). when(mContext.getResources().
getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)) getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed))
.thenReturn(true); .thenReturn(true);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
assertThat(mPreference.isVisible()).isTrue(); assertThat(mPreference.isVisible()).isTrue();
@@ -84,7 +93,7 @@ public class PulseNotificationPreferenceControllerTest {
final Context context = RuntimeEnvironment.application; final Context context = RuntimeEnvironment.application;
Settings.System.putInt(context.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 1); Settings.System.putInt(context.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 1);
mController = new PulseNotificationPreferenceController(context); mController = new PulseNotificationPreferenceController(context, "testkey");
mController.updateState(preference); mController.updateState(preference);
verify(preference).setChecked(true); verify(preference).setChecked(true);
@@ -96,9 +105,65 @@ public class PulseNotificationPreferenceControllerTest {
final Context context = RuntimeEnvironment.application; final Context context = RuntimeEnvironment.application;
Settings.System.putInt(context.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 0); Settings.System.putInt(context.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 0);
mController = new PulseNotificationPreferenceController(context); mController = new PulseNotificationPreferenceController(context, "testkey");
mController.updateState(preference); mController.updateState(preference);
verify(preference).setChecked(false); verify(preference).setChecked(false);
} }
@Test
public void isAvailable_configTrue_shouldReturnTrue() {
when(mContext.getResources().
getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)).thenReturn(
true);
assertThat(mController.isAvailable()).isTrue();
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE);
}
@Test
public void isAvailable_configFalse_shouldReturnFalse() {
when(mContext.getResources().
getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)).thenReturn(
false);
assertThat(mController.isAvailable()).isFalse();
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.DISABLED_UNSUPPORTED);
}
@Test
public void isChecked_configOn_shouldReturnTrue() {
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 1);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void isChecked_configOff_shouldReturnFalse() {
Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 0);
assertThat(mController.isChecked()).isFalse();
}
@Test
public void testSetChecked_configIsSet_shouldReturnTrue() {
mController.setChecked(true);
assertThat(mController.isChecked()).isTrue();
assertThat(
Settings.Secure.getInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 0))
.isEqualTo(1);
}
@Test
public void testSetChecked_configIsNotSet_shouldReturnFalse() {
mController.setChecked(false);
assertThat(mController.isChecked()).isFalse();
assertThat(
Settings.Secure.getInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 1))
.isEqualTo(0);
}
} }