Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c65b1b3c authored by HJ ChangLiao's avatar HJ ChangLiao
Browse files

Convert Blink light to TogglePreferenceController

Convert Blink light (notification) controller:
PulseNotificationPreferenceController
to TogglePreferenceController for slices

Change-Id: I4c49d2d52a5909b45f1a74518aa925abb14e1336
Fixes: 74923755
Test: make RunSettingsRoboTests
parent e2d07ea4
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -33,7 +33,8 @@
    <!-- Pulse notification light -->
    <SwitchPreference
        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 -->
    <com.android.settings.DefaultRingtonePreference
+0 −6
Original line number Diff line number Diff line
@@ -26,14 +26,12 @@ import android.os.UserHandle;
import android.provider.SearchIndexableResource;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.text.TextUtils;

import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.RingtonePreference;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.gestures.SwipeToNotificationPreferenceController;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settingslib.core.AbstractPreferenceController;
@@ -97,21 +95,17 @@ public class ConfigureNotificationSettings extends DashboardFragment {
        final List<AbstractPreferenceController> 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
+18 −33
Original line number Diff line number Diff line
@@ -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 {
+70 −5
Original line number Diff line number Diff line
@@ -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);
    }
}