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