summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/ColorModePreference.java
blob: 14ffe480a1097d3293b587416581ae1d0e4a982c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.res.Resources;
import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManager.DisplayListener;
import android.os.Handler;
import android.os.Looper;
import android.preference.DialogPreference;
import android.preference.SwitchPreference;
import android.util.AttributeSet;
import android.view.Display;
import android.view.Display.ColorTransform;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class ColorModePreference extends SwitchPreference implements DisplayListener {

    private DisplayManager mDisplayManager;
    private Display mDisplay;

    private int mCurrentIndex;
    private ArrayList<ColorTransformDescription> mDescriptions;

    public ColorModePreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        mDisplayManager = getContext().getSystemService(DisplayManager.class);
    }

    public int getTransformsCount() {
        return mDescriptions.size();
    }

    public void startListening() {
        mDisplayManager.registerDisplayListener(this, new Handler(Looper.getMainLooper()));
    }

    public void stopListening() {
        mDisplayManager.unregisterDisplayListener(this);
    }

    @Override
    public void onDisplayAdded(int displayId) {
        if (displayId == Display.DEFAULT_DISPLAY) {
            updateCurrentAndSupported();
        }
    }

    @Override
    public void onDisplayChanged(int displayId) {
        if (displayId == Display.DEFAULT_DISPLAY) {
            updateCurrentAndSupported();
        }
    }

    @Override
    public void onDisplayRemoved(int displayId) {
    }

    public void updateCurrentAndSupported() {
        mDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);

        mDescriptions = new ArrayList<>();

        Resources resources = getContext().getResources();
        int[] transforms = resources.getIntArray(
                com.android.internal.R.array.config_colorTransforms);
        String[] titles = resources.getStringArray(R.array.color_mode_names);
        String[] descriptions = resources.getStringArray(R.array.color_mode_descriptions);
        // Map the resource information describing color transforms.
        for (int i = 0; i < transforms.length; i++) {
            if (transforms[i] != -1 && i != 1 /* Skip Natural for now. */) {
                ColorTransformDescription desc = new ColorTransformDescription();
                desc.colorTransform = transforms[i];
                desc.title = titles[i];
                desc.summary = descriptions[i];
                mDescriptions.add(desc);
            }
        }
        // Match up a ColorTransform to every description.
        ColorTransform[] supportedColorTransforms = mDisplay.getSupportedColorTransforms();
        for (int i = 0; i < supportedColorTransforms.length; i++) {
            for (int j = 0; j < mDescriptions.size(); j++) {
                if (mDescriptions.get(j).colorTransform
                        == supportedColorTransforms[i].getColorTransform()
                        && mDescriptions.get(j).transform == null) {
                    mDescriptions.get(j).transform = supportedColorTransforms[i];
                    break;
                }
            }
        }
        // Remove any extras that don't have a transform for some reason.
        for (int i = 0; i < mDescriptions.size(); i++) {
            if (mDescriptions.get(i).transform == null) {
                mDescriptions.remove(i--);
            }
        }

        ColorTransform currentTransform = mDisplay.getColorTransform();
        mCurrentIndex = -1;
        for (int i = 0; i < mDescriptions.size(); i++) {
            if (mDescriptions.get(i).colorTransform == currentTransform.getColorTransform()) {
                mCurrentIndex = i;
                break;
            }
        }
        setChecked(mCurrentIndex == 1);
    }

    @Override
    protected boolean persistBoolean(boolean value) {
        // Right now this is a switch, so we only support two modes.
        if (mDescriptions.size() == 2) {
            ColorTransformDescription desc = mDescriptions.get(value ? 1 : 0);

            mDisplay.requestColorTransform(desc.transform);
            mCurrentIndex = mDescriptions.indexOf(desc);
        }

        return true;
    }

    private static class ColorTransformDescription {
        private int colorTransform;
        private String title;
        private String summary;
        private ColorTransform transform;
    }
}