1 /* 2 * Copyright (C) 2015 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.tv.settings.system.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.tv.settings.R; 30 31 import java.util.ArrayList; 32 33 public class ColorModePreference extends SwitchPreference implements DisplayListener { 34 35 private DisplayManager mDisplayManager; 36 private Display mDisplay; 37 38 private int mCurrentIndex; 39 private ArrayList<ColorModeDescription> mDescriptions; 40 ColorModePreference(Context context, AttributeSet attrs)41 public ColorModePreference(Context context, AttributeSet attrs) { 42 super(context, attrs); 43 mDisplayManager = getContext().getSystemService(DisplayManager.class); 44 } 45 getColorModeCount()46 public int getColorModeCount() { 47 return mDescriptions.size(); 48 } 49 startListening()50 public void startListening() { 51 mDisplayManager.registerDisplayListener(this, new Handler(Looper.getMainLooper())); 52 } 53 stopListening()54 public void stopListening() { 55 mDisplayManager.unregisterDisplayListener(this); 56 } 57 58 @Override onDisplayAdded(int displayId)59 public void onDisplayAdded(int displayId) { 60 if (displayId == Display.DEFAULT_DISPLAY) { 61 updateCurrentAndSupported(); 62 } 63 } 64 65 @Override onDisplayChanged(int displayId)66 public void onDisplayChanged(int displayId) { 67 if (displayId == Display.DEFAULT_DISPLAY) { 68 updateCurrentAndSupported(); 69 } 70 } 71 72 @Override onDisplayRemoved(int displayId)73 public void onDisplayRemoved(int displayId) { 74 } 75 updateCurrentAndSupported()76 public void updateCurrentAndSupported() { 77 mDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY); 78 79 mDescriptions = new ArrayList<>(); 80 81 Resources resources = getContext().getResources(); 82 int[] colorModes = resources.getIntArray(R.array.color_mode_ids); 83 String[] titles = resources.getStringArray(R.array.color_mode_names); 84 String[] descriptions = resources.getStringArray(R.array.color_mode_descriptions); 85 // Map the resource information describing color modes. 86 for (int i = 0; i < colorModes.length; i++) { 87 if (colorModes[i] != -1 && i != 1 /* Skip Natural for now. */) { 88 ColorModeDescription desc = new ColorModeDescription(); 89 desc.colorMode = colorModes[i]; 90 desc.title = titles[i]; 91 desc.summary = descriptions[i]; 92 mDescriptions.add(desc); 93 } 94 } 95 96 int currentColorMode = mDisplay.getColorMode(); 97 mCurrentIndex = -1; 98 for (int i = 0; i < mDescriptions.size(); i++) { 99 if (mDescriptions.get(i).colorMode == currentColorMode) { 100 mCurrentIndex = i; 101 break; 102 } 103 } 104 setChecked(mCurrentIndex == 1); 105 } 106 107 @Override persistBoolean(boolean value)108 protected boolean persistBoolean(boolean value) { 109 // Right now this is a switch, so we only support two modes. 110 if (mDescriptions.size() == 2) { 111 ColorModeDescription desc = mDescriptions.get(value ? 1 : 0); 112 113 mDisplay.requestColorMode(desc.colorMode); 114 mCurrentIndex = mDescriptions.indexOf(desc); 115 } 116 117 return true; 118 } 119 120 private static class ColorModeDescription { 121 private int colorMode; 122 private String title; 123 private String summary; 124 } 125 } 126