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

Commit 9aff8550 authored by Sally Yuen's avatar Sally Yuen Committed by Android (Google) Code Review
Browse files

Merge "[Reduce Bright Colors] Use a switch preference" into sc-dev

parents 6ca32df6 166c8989
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@
            settings:searchable="true"/>

        <!--TODO(b/170973645): Get icon-->
        <Preference
        <com.android.settings.widget.PrimarySwitchPreference
            android:fragment="com.android.settings.accessibility.ToggleReduceBrightColorsPreferenceFragment"
            android:key="reduce_bright_colors_preference"
            android:persistent="false"
+69 −3
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 * Copyright (C) 2021 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.
@@ -17,17 +17,60 @@
package com.android.settings.accessibility;

import android.content.Context;
import android.database.ContentObserver;
import android.hardware.display.ColorDisplayManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.widget.PrimarySwitchPreference;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;

/** PreferenceController that shows the Reduce Bright Colors summary */
public class ReduceBrightColorsPreferenceController extends BasePreferenceController {
public class ReduceBrightColorsPreferenceController extends TogglePreferenceController
        implements LifecycleObserver, OnStart, OnStop {
    private ContentObserver mSettingsContentObserver;
    private PrimarySwitchPreference mPreference;
    private final Context mContext;

    public ReduceBrightColorsPreferenceController(Context context,
            String preferenceKey) {
        super(context, preferenceKey);
        mContext = context;
        mSettingsContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())){
            @Override
            public void onChange(boolean selfChange, Uri uri) {
                final String path = uri == null ? null : uri.getLastPathSegment();
                if (TextUtils.equals(path, Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED)) {
                    updateState(mPreference);
                }
            }
        };
    }

    @Override
    public boolean isChecked() {
        return Settings.Secure.getIntForUser(mContext.getContentResolver(),
                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED,
                0,
                UserHandle.USER_CURRENT) == 1;
    }

    @Override
    public boolean setChecked(boolean isChecked) {
        return Settings.Secure.putIntForUser(mContext.getContentResolver(),
                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, isChecked ? 1 : 0,
                UserHandle.USER_CURRENT);
    }

    @Override
@@ -36,9 +79,32 @@ public class ReduceBrightColorsPreferenceController extends BasePreferenceContro
                R.string.reduce_bright_colors_preference_summary);
    }

    @Override
    public void updateState(Preference preference) {
        super.updateState(preference);
        refreshSummary(preference);
    }

    @Override
    public int getAvailabilityStatus() {
        return ColorDisplayManager.isColorTransformAccelerated(mContext) ? AVAILABLE
                : UNSUPPORTED_ON_DEVICE;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreference = screen.findPreference(getPreferenceKey());
    }

    @Override
    public void onStart() {
        mContext.getContentResolver().registerContentObserver(Settings.Secure.getUriFor(
                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED),
                false, mSettingsContentObserver, UserHandle.USER_CURRENT);
    }
    @Override
    public void onStop() {
        mContext.getContentResolver().unregisterContentObserver(mSettingsContentObserver);
    }
}
+15 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.provider.Settings;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -31,7 +32,6 @@ import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class ReduceBrightColorsPreferenceControllerTest {
    private static final String PREF_KEY = "rbc_preference";

    private final Context mContext = ApplicationProvider.getApplicationContext();
    private final ReduceBrightColorsPreferenceController mController =
            new ReduceBrightColorsPreferenceController(mContext, PREF_KEY);
@@ -41,4 +41,18 @@ public class ReduceBrightColorsPreferenceControllerTest {
        assertThat(mController.getSummary().toString().contains(
                mContext.getText(R.string.reduce_bright_colors_preference_summary))).isTrue();
    }

    @Test
    public void isChecked_reduceBrightColorsIsActivated_returnTrue() {
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1);
        assertThat(mController.isChecked()).isTrue();
    }

    @Test
    public void isChecked_reduceBrightColorsIsNotActivated_returnFalse() {
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 0);
        assertThat(mController.isChecked()).isFalse();
    }
}