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

Commit 6162fa5e authored by jeffreyhuang's avatar jeffreyhuang
Browse files

Introduce PictureColorModePreferenceController

 - Create new PictureColorModePreferenceController
 - Create controller inside the DashboardFragment
 - Refactor ColorModePreference so isAvailable() no longer depends on
 ColorModePreference
 - Port logic from DevelopmentSettings into the controller

Bug: 34203528
Test: make RunSettingsRoboTests -j40
Change-Id: I8dff5b0d5ad1d7f043fc7ead540b2c0c960933e3
parent 31b04f52
Loading
Loading
Loading
Loading
+24 −17
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.view.Display;
import com.android.settings.R;

import java.util.ArrayList;
import java.util.List;

public class ColorModePreference extends SwitchPreference implements DisplayListener {

@@ -35,7 +36,28 @@ public class ColorModePreference extends SwitchPreference implements DisplayList
    private Display mDisplay;

    private int mCurrentIndex;
    private ArrayList<ColorModeDescription> mDescriptions;
    private List<ColorModeDescription> mDescriptions;

    public static List<ColorModeDescription> getColorModeDescriptions(Context context) {

        List<ColorModeDescription> colorModeDescriptions = new ArrayList<>();
        Resources resources = context.getResources();
        int[] colorModes = resources.getIntArray(R.array.color_mode_ids);
        String[] titles = resources.getStringArray(R.array.color_mode_names);
        String[] descriptions = resources.getStringArray(R.array.color_mode_descriptions);
        // Map the resource information describing color modes.
        for (int i = 0; i < colorModes.length; i++) {
            if (colorModes[i] != -1 && i != 1 /* Skip Natural for now. */) {
                ColorModeDescription desc = new ColorModeDescription();
                desc.colorMode = colorModes[i];
                desc.title = titles[i];
                desc.summary = descriptions[i];
                colorModeDescriptions.add(desc);
            }
        }

        return colorModeDescriptions;
    }

    public ColorModePreference(Context context, AttributeSet attrs) {
        super(context, attrs);
@@ -75,22 +97,7 @@ public class ColorModePreference extends SwitchPreference implements DisplayList
    public void updateCurrentAndSupported() {
        mDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);

        mDescriptions = new ArrayList<>();

        Resources resources = getContext().getResources();
        int[] colorModes = resources.getIntArray(R.array.color_mode_ids);
        String[] titles = resources.getStringArray(R.array.color_mode_names);
        String[] descriptions = resources.getStringArray(R.array.color_mode_descriptions);
        // Map the resource information describing color modes.
        for (int i = 0; i < colorModes.length; i++) {
            if (colorModes[i] != -1 && i != 1 /* Skip Natural for now. */) {
                ColorModeDescription desc = new ColorModeDescription();
                desc.colorMode = colorModes[i];
                desc.title = titles[i];
                desc.summary = descriptions[i];
                mDescriptions.add(desc);
            }
        }
        mDescriptions = getColorModeDescriptions(getContext());

        int currentColorMode = mDisplay.getColorMode();
        mCurrentIndex = -1;
+1 −1
Original line number Diff line number Diff line
@@ -181,7 +181,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
        controllers.add(new OemUnlockPreferenceController(context, activity, fragment));
        // running services
        // convert to file encryption
        // picture color mode
        controllers.add(new PictureColorModePreferenceController(context, lifecycle));
        // webview implementation
        // cool color temperature
        // automatic system updates
+99 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.development;

import android.content.Context;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.PreferenceScreen;

import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;

public class PictureColorModePreferenceController extends
        DeveloperOptionsPreferenceController implements
        LifecycleObserver, OnResume, OnPause {

    private static final String KEY_COLOR_MODE = "picture_color_mode";

    private ColorModePreference mPreference;

    public PictureColorModePreferenceController(Context context, Lifecycle lifecycle) {
        super(context);

        if (lifecycle != null) {
            lifecycle.addObserver(this);
        }
    }

    @Override
    public boolean isAvailable() {
        return getColorModeDescriptionsSize() > 1 && !isWideColorGamut();
    }

    @Override
    public String getPreferenceKey() {
        return KEY_COLOR_MODE;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreference = (ColorModePreference) screen.findPreference(getPreferenceKey());
        if (mPreference != null) {
            mPreference.updateCurrentAndSupported();
        }
    }

    @Override
    public void onResume() {
        if (mPreference == null) {
            return;
        }
        mPreference.startListening();
        mPreference.updateCurrentAndSupported();
    }

    @Override
    public void onPause() {
        if (mPreference == null) {
            return;
        }
        mPreference.stopListening();
    }
    
    @Override
    protected void onDeveloperOptionsSwitchEnabled() {
        mPreference.setEnabled(true);
    }

    @Override
    protected void onDeveloperOptionsSwitchDisabled() {
        mPreference.setEnabled(false);
    }

    @VisibleForTesting
    boolean isWideColorGamut() {
        return mContext.getDisplay().isWideColorGamut();
    }

    @VisibleForTesting
    int getColorModeDescriptionsSize() {
        return ColorModePreference.getColorModeDescriptions(mContext).size();
    }
}
+12 −0
Original line number Diff line number Diff line
@@ -112,6 +112,9 @@ public class DevelopmentSettingsDashboardFragmentTest {
    }

    @Test
    @Config(shadows = {
            ShadowPictureColorModePreferenceController.class
    })
    public void searchIndex_pageEnabled_shouldNotAddKeysToNonIndexable() {
        final Context appContext = RuntimeEnvironment.application;
        DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(appContext, true);
@@ -198,4 +201,13 @@ public class DevelopmentSettingsDashboardFragmentTest {
            mShown = true;
        }
    }

    @Implements(PictureColorModePreferenceController.class)
    public static class ShadowPictureColorModePreferenceController {

        @Implementation
        public boolean isAvailable() {
            return true;
        }
    }
}
+137 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.development;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.doReturn;
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.support.v7.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.core.lifecycle.Lifecycle;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;

@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class PictureColorModePreferenceControllerTest {

    @Mock
    private ColorModePreference mPreference;
    @Mock
    private Context mContext;
    @Mock
    private PreferenceScreen mPreferenceScreen;
    @Mock
    private Resources mResources;

    private Lifecycle mLifecycle;
    private PictureColorModePreferenceController mController;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mLifecycle = new Lifecycle();
        mController = new PictureColorModePreferenceController(mContext, mLifecycle);
        when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
                mPreference);
        when(mContext.getResources()).thenReturn(mResources);
        when(mResources.getIntArray(R.array.color_mode_ids)).thenReturn(new int[0]);
        mController.displayPreference(mPreferenceScreen);
    }

    @Test
    public void isAvailable_shouldReturnFalseWhenWideColorGambit() {
        mController = spy(mController);
        doReturn(2).when(mController).getColorModeDescriptionsSize();
        doReturn(true).when(mController).isWideColorGamut();

        assertThat(mController.isAvailable()).isFalse();
    }

    @Test
    public void isAvailable_shouldReturnTrueWhenNotWideColorGambit() {
        mController = spy(mController);
        doReturn(2).when(mController).getColorModeDescriptionsSize();
        doReturn(false).when(mController).isWideColorGamut();

        assertThat(mController.isAvailable()).isTrue();
    }

    @Test
    public void isAvailable_shouldReturnFalseWhenColorCountIsOne() {
        mController = spy(mController);
        doReturn(1).when(mController).getColorModeDescriptionsSize();
        doReturn(true).when(mController).isWideColorGamut();

        assertThat(mController.isAvailable()).isFalse();
    }

    @Test
    public void isAvailable_shouldReturnTrueWhenColorCountIsTwo() {
        mController = spy(mController);
        doReturn(2).when(mController).getColorModeDescriptionsSize();
        doReturn(false).when(mController).isWideColorGamut();

        assertThat(mController.isAvailable()).isTrue();
    }

    @Test
    public void onDeveloperOptionEnabled_shouldEnablePreference() {
        mController = spy(mController);
        doReturn(true).when(mController).isAvailable();
        mController.onDeveloperOptionsEnabled();

        verify(mPreference).setEnabled(true);
    }

    @Test
    public void onDeveloperOptionDisabled_shouldDisablePreference() {
        mController = spy(mController);
        doReturn(true).when(mController).isAvailable();
        mController.onDeveloperOptionsDisabled();

        verify(mPreference).setEnabled(false);
    }

    @Test
    public void onResume_shouldStartListening() {
        mLifecycle.onResume();

        verify(mPreference).startListening();
    }

    @Test
    public void onPause_shouldStopListening() {
        mLifecycle.onPause();

        verify(mPreference).stopListening();
    }
}