1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.car.developeroptions.development; 17 18 import android.content.Context; 19 import android.content.res.Resources; 20 import android.hardware.display.DisplayManager; 21 import android.hardware.display.DisplayManager.DisplayListener; 22 import android.os.Handler; 23 import android.os.Looper; 24 import android.util.AttributeSet; 25 import android.view.Display; 26 27 import androidx.preference.SwitchPreference; 28 29 import com.android.car.developeroptions.R; 30 31 import java.util.ArrayList; 32 import java.util.List; 33 34 public class ColorModePreference extends SwitchPreference implements DisplayListener { 35 36 private DisplayManager mDisplayManager; 37 private Display mDisplay; 38 39 private int mCurrentIndex; 40 private List<ColorModeDescription> mDescriptions; 41 getColorModeDescriptions(Context context)42 public static List<ColorModeDescription> getColorModeDescriptions(Context context) { 43 44 List<ColorModeDescription> colorModeDescriptions = new ArrayList<>(); 45 Resources resources = context.getResources(); 46 int[] colorModes = resources.getIntArray(R.array.color_mode_ids); 47 String[] titles = resources.getStringArray(R.array.color_mode_names); 48 String[] descriptions = resources.getStringArray(R.array.color_mode_descriptions); 49 // Map the resource information describing color modes. 50 for (int i = 0; i < colorModes.length; i++) { 51 if (colorModes[i] != -1 && i != 1 /* Skip Natural for now. */) { 52 ColorModeDescription desc = new ColorModeDescription(); 53 desc.colorMode = colorModes[i]; 54 desc.title = titles[i]; 55 desc.summary = descriptions[i]; 56 colorModeDescriptions.add(desc); 57 } 58 } 59 60 return colorModeDescriptions; 61 } 62 ColorModePreference(Context context, AttributeSet attrs)63 public ColorModePreference(Context context, AttributeSet attrs) { 64 super(context, attrs); 65 mDisplayManager = getContext().getSystemService(DisplayManager.class); 66 } 67 getColorModeCount()68 public int getColorModeCount() { 69 return mDescriptions.size(); 70 } 71 startListening()72 public void startListening() { 73 mDisplayManager.registerDisplayListener(this, new Handler(Looper.getMainLooper())); 74 } 75 stopListening()76 public void stopListening() { 77 mDisplayManager.unregisterDisplayListener(this); 78 } 79 80 @Override onDisplayAdded(int displayId)81 public void onDisplayAdded(int displayId) { 82 if (displayId == Display.DEFAULT_DISPLAY) { 83 updateCurrentAndSupported(); 84 } 85 } 86 87 @Override onDisplayChanged(int displayId)88 public void onDisplayChanged(int displayId) { 89 if (displayId == Display.DEFAULT_DISPLAY) { 90 updateCurrentAndSupported(); 91 } 92 } 93 94 @Override onDisplayRemoved(int displayId)95 public void onDisplayRemoved(int displayId) { 96 } 97 updateCurrentAndSupported()98 public void updateCurrentAndSupported() { 99 mDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY); 100 101 mDescriptions = getColorModeDescriptions(getContext()); 102 103 int currentColorMode = mDisplay.getColorMode(); 104 mCurrentIndex = -1; 105 for (int i = 0; i < mDescriptions.size(); i++) { 106 if (mDescriptions.get(i).colorMode == currentColorMode) { 107 mCurrentIndex = i; 108 break; 109 } 110 } 111 setChecked(mCurrentIndex == 1); 112 } 113 114 @Override persistBoolean(boolean value)115 protected boolean persistBoolean(boolean value) { 116 // Right now this is a switch, so we only support two modes. 117 if (mDescriptions.size() == 2) { 118 ColorModeDescription desc = mDescriptions.get(value ? 1 : 0); 119 120 mDisplay.requestColorMode(desc.colorMode); 121 mCurrentIndex = mDescriptions.indexOf(desc); 122 } 123 124 return true; 125 } 126 127 private static class ColorModeDescription { 128 private int colorMode; 129 private String title; 130 private String summary; 131 } 132 } 133