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