• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.settings.display;
15 
16 import android.content.Context;
17 import android.graphics.drawable.Drawable;
18 import android.support.annotation.VisibleForTesting;
19 import android.support.v7.preference.PreferenceScreen;
20 
21 import com.android.internal.app.ColorDisplayController;
22 import com.android.internal.logging.nano.MetricsProto;
23 
24 import com.android.settings.applications.LayoutPreference;
25 import com.android.settings.R;
26 import com.android.settings.widget.RadioButtonPickerFragment;
27 import com.android.settingslib.widget.CandidateInfo;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 @SuppressWarnings("WeakerAccess")
33 public class ColorModePreferenceFragment extends RadioButtonPickerFragment
34         implements ColorDisplayController.Callback {
35 
36     @VisibleForTesting
37     static final String KEY_COLOR_MODE_NATURAL = "color_mode_natural";
38     @VisibleForTesting
39     static final String KEY_COLOR_MODE_BOOSTED = "color_mode_boosted";
40     @VisibleForTesting
41     static final String KEY_COLOR_MODE_SATURATED = "color_mode_saturated";
42     @VisibleForTesting
43     static final String KEY_COLOR_MODE_AUTOMATIC = "color_mode_automatic";
44 
45     private ColorDisplayController mController;
46 
47     @Override
onAttach(Context context)48     public void onAttach(Context context) {
49         super.onAttach(context);
50         mController = new ColorDisplayController(context);
51         mController.setListener(this);
52     }
53 
54     @Override
onDetach()55     public void onDetach() {
56         super.onDetach();
57         if (mController != null) {
58             mController.setListener(null);
59             mController = null;
60         }
61     }
62 
63     @Override
getPreferenceScreenResId()64     protected int getPreferenceScreenResId() {
65         return R.xml.color_mode_settings;
66     }
67 
68     @VisibleForTesting
configureAndInstallPreview(LayoutPreference preview, PreferenceScreen screen)69     void configureAndInstallPreview(LayoutPreference preview, PreferenceScreen screen) {
70         preview.setSelectable(false);
71         screen.addPreference(preview);
72     }
73 
74     @Override
addStaticPreferences(PreferenceScreen screen)75     protected void addStaticPreferences(PreferenceScreen screen) {
76         final LayoutPreference preview = new LayoutPreference(screen.getContext(),
77                 R.layout.color_mode_preview);
78         configureAndInstallPreview(preview, screen);
79     }
80 
81     @Override
getCandidates()82     protected List<? extends CandidateInfo> getCandidates() {
83         final Context c = getContext();
84         final int[] availableColorModes = c.getResources().getIntArray(
85                 com.android.internal.R.array.config_availableColorModes);
86 
87         List<ColorModeCandidateInfo> candidates = new ArrayList<ColorModeCandidateInfo>();
88         if (availableColorModes != null) {
89             for (int colorMode : availableColorModes) {
90                 if (colorMode == ColorDisplayController.COLOR_MODE_NATURAL) {
91                     candidates.add(new ColorModeCandidateInfo(
92                                 c.getText(R.string.color_mode_option_natural),
93                                 KEY_COLOR_MODE_NATURAL, true /* enabled */));
94                 } else if (colorMode == ColorDisplayController.COLOR_MODE_BOOSTED) {
95                     candidates.add(new ColorModeCandidateInfo(
96                                 c.getText(R.string.color_mode_option_boosted),
97                                 KEY_COLOR_MODE_BOOSTED, true /* enabled */));
98                 } else if (colorMode == ColorDisplayController.COLOR_MODE_SATURATED) {
99                     candidates.add(new ColorModeCandidateInfo(
100                                 c.getText(R.string.color_mode_option_saturated),
101                                 KEY_COLOR_MODE_SATURATED, true /* enabled */));
102                 } else if (colorMode == ColorDisplayController.COLOR_MODE_AUTOMATIC) {
103                     candidates.add(new ColorModeCandidateInfo(
104                                 c.getText(R.string.color_mode_option_automatic),
105                                 KEY_COLOR_MODE_AUTOMATIC, true /* enabled */));
106                 }
107             }
108         }
109         return candidates;
110     }
111 
112     @Override
getDefaultKey()113     protected String getDefaultKey() {
114         final int colorMode = mController.getColorMode();
115         if (colorMode == ColorDisplayController.COLOR_MODE_AUTOMATIC) {
116             return KEY_COLOR_MODE_AUTOMATIC;
117         } else if (colorMode == ColorDisplayController.COLOR_MODE_SATURATED) {
118             return KEY_COLOR_MODE_SATURATED;
119         } else if (colorMode == ColorDisplayController.COLOR_MODE_BOOSTED) {
120             return KEY_COLOR_MODE_BOOSTED;
121         }
122         return KEY_COLOR_MODE_NATURAL;
123     }
124 
125     @Override
setDefaultKey(String key)126     protected boolean setDefaultKey(String key) {
127         switch (key) {
128             case KEY_COLOR_MODE_NATURAL:
129                 mController.setColorMode(ColorDisplayController.COLOR_MODE_NATURAL);
130                 break;
131             case KEY_COLOR_MODE_BOOSTED:
132                 mController.setColorMode(ColorDisplayController.COLOR_MODE_BOOSTED);
133                 break;
134             case KEY_COLOR_MODE_SATURATED:
135                 mController.setColorMode(ColorDisplayController.COLOR_MODE_SATURATED);
136                 break;
137             case KEY_COLOR_MODE_AUTOMATIC:
138                 mController.setColorMode(ColorDisplayController.COLOR_MODE_AUTOMATIC);
139                 break;
140         }
141         return true;
142     }
143 
144     @Override
getMetricsCategory()145     public int getMetricsCategory() {
146         return MetricsProto.MetricsEvent.COLOR_MODE_SETTINGS;
147     }
148 
149     @VisibleForTesting
150     static class ColorModeCandidateInfo extends CandidateInfo {
151         private final CharSequence mLabel;
152         private final String mKey;
153 
ColorModeCandidateInfo(CharSequence label, String key, boolean enabled)154         ColorModeCandidateInfo(CharSequence label, String key, boolean enabled) {
155             super(enabled);
156             mLabel = label;
157             mKey = key;
158         }
159 
160         @Override
loadLabel()161         public CharSequence loadLabel() {
162             return mLabel;
163         }
164 
165         @Override
loadIcon()166         public Drawable loadIcon() {
167             return null;
168         }
169 
170         @Override
getKey()171         public String getKey() {
172             return mKey;
173         }
174     }
175 
176     @Override
onAccessibilityTransformChanged(boolean state)177     public void onAccessibilityTransformChanged(boolean state) {
178         // Color modes are no not configurable when Accessibility transforms are enabled. Close
179         // this fragment in that case.
180         if (state) {
181             getActivity().onBackPressed();
182         }
183     }
184 }
185