diff --git a/res/xml/configure_notification_settings.xml b/res/xml/configure_notification_settings.xml index 612f5e7623b..3bdc3f0e290 100644 --- a/res/xml/configure_notification_settings.xml +++ b/res/xml/configure_notification_settings.xml @@ -33,7 +33,8 @@ + android:title="@string/notification_pulse_title" + settings:controller="com.android.settings.notification.PulseNotificationPreferenceController" /> controllers = new ArrayList<>(); final BadgingNotificationPreferenceController badgeController = new BadgingNotificationPreferenceController(context); - final PulseNotificationPreferenceController pulseController = - new PulseNotificationPreferenceController(context); final LockScreenNotificationPreferenceController lockScreenNotificationController = new LockScreenNotificationPreferenceController(context, KEY_LOCKSCREEN, KEY_LOCKSCREEN_WORK_PROFILE_HEADER, KEY_LOCKSCREEN_WORK_PROFILE); if (lifecycle != null) { - lifecycle.addObserver(pulseController); lifecycle.addObserver(lockScreenNotificationController); } controllers.add(new RecentNotifyingAppsPreferenceController( context, new NotificationBackend(), app, host)); controllers.add(badgeController); - controllers.add(pulseController); controllers.add(lockScreenNotificationController); controllers.add(new NotificationRingtonePreferenceController(context) { @Override diff --git a/src/com/android/settings/notification/PulseNotificationPreferenceController.java b/src/com/android/settings/notification/PulseNotificationPreferenceController.java index 4a8b8204f47..c39f482fd25 100644 --- a/src/com/android/settings/notification/PulseNotificationPreferenceController.java +++ b/src/com/android/settings/notification/PulseNotificationPreferenceController.java @@ -24,33 +24,28 @@ import android.os.Handler; import android.provider.Settings; import android.support.v7.preference.Preference; 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.settingslib.core.AbstractPreferenceController; -import com.android.settingslib.core.lifecycle.LifecycleObserver; +import com.android.settings.core.TogglePreferenceController; import com.android.settingslib.core.lifecycle.events.OnPause; import com.android.settingslib.core.lifecycle.events.OnResume; import static android.provider.Settings.System.NOTIFICATION_LIGHT_PULSE; -public class PulseNotificationPreferenceController extends AbstractPreferenceController - implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener, - LifecycleObserver, OnResume, OnPause { +public class PulseNotificationPreferenceController extends TogglePreferenceController + implements OnResume, OnPause { - private static final String TAG = "PulseNotifPrefContr"; - private static final String KEY_NOTIFICATION_PULSE = "notification_pulse"; + private static final int ON = 1; + private static final int OFF = 0; private SettingObserver mSettingObserver; - public PulseNotificationPreferenceController(Context context) { - super(context); + public PulseNotificationPreferenceController(Context context, String key) { + super(context, key); } @Override public void displayPreference(PreferenceScreen screen) { super.displayPreference(screen); - Preference preference = screen.findPreference(KEY_NOTIFICATION_PULSE); + Preference preference = screen.findPreference(getPreferenceKey()); if (preference != null) { mSettingObserver = new SettingObserver(preference); } @@ -71,32 +66,22 @@ public class PulseNotificationPreferenceController extends AbstractPreferenceCon } @Override - public String getPreferenceKey() { - return KEY_NOTIFICATION_PULSE; + public int getAvailabilityStatus() { + return mContext.getResources().getBoolean( + com.android.internal.R.bool.config_intrusiveNotificationLed) ? AVAILABLE + : DISABLED_UNSUPPORTED; } @Override - public boolean isAvailable() { - return mContext.getResources() - .getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed); + public boolean isChecked() { + return Settings.System.getInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, OFF) + == ON; } @Override - public void updateState(Preference preference) { - try { - final boolean checked = Settings.System.getInt(mContext.getContentResolver(), - 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); + public boolean setChecked(boolean isChecked) { + return Settings.System.putInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, + isChecked ? ON : OFF); } class SettingObserver extends ContentObserver { diff --git a/tests/robotests/src/com/android/settings/notification/PulseNotificationPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/PulseNotificationPreferenceControllerTest.java index 0d4e6c46b12..905cb6f64eb 100644 --- a/tests/robotests/src/com/android/settings/notification/PulseNotificationPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/PulseNotificationPreferenceControllerTest.java @@ -19,15 +19,19 @@ package com.android.settings.notification; import static android.provider.Settings.System.NOTIFICATION_LIGHT_PULSE; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.content.Context; +import android.content.res.Resources; import android.provider.Settings; import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.TwoStatePreference; +import com.android.settings.core.BasePreferenceController; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -40,8 +44,9 @@ import org.robolectric.RuntimeEnvironment; @RunWith(RobolectricTestRunner.class) public class PulseNotificationPreferenceControllerTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Context mContext; + @Mock + private Resources mResources; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private PreferenceScreen mScreen; @@ -51,8 +56,11 @@ public class PulseNotificationPreferenceControllerTest { @Before public void setUp() { MockitoAnnotations.initMocks(this); - mController = new PulseNotificationPreferenceController(mContext); - mPreference = new Preference(RuntimeEnvironment.application); + mContext = spy(RuntimeEnvironment.application); + when(mContext.getResources()).thenReturn(mResources); + + mController = new PulseNotificationPreferenceController(mContext, "testkey"); + mPreference = new Preference(mContext); mPreference.setKey(mController.getPreferenceKey()); when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); } @@ -62,6 +70,7 @@ public class PulseNotificationPreferenceControllerTest { when(mContext.getResources(). getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)) .thenReturn(true); + mController.displayPreference(mScreen); assertThat(mPreference.isVisible()).isTrue(); @@ -84,7 +93,7 @@ public class PulseNotificationPreferenceControllerTest { final Context context = RuntimeEnvironment.application; Settings.System.putInt(context.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 1); - mController = new PulseNotificationPreferenceController(context); + mController = new PulseNotificationPreferenceController(context, "testkey"); mController.updateState(preference); verify(preference).setChecked(true); @@ -96,9 +105,65 @@ public class PulseNotificationPreferenceControllerTest { final Context context = RuntimeEnvironment.application; Settings.System.putInt(context.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 0); - mController = new PulseNotificationPreferenceController(context); + mController = new PulseNotificationPreferenceController(context, "testkey"); mController.updateState(preference); 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); + } }