summaryrefslogtreecommitdiffstats
path: root/src/com/cyngn/eleven/widgets/ColorSchemeDialog.java
blob: fa13fc6dd59f4d0a7213e4f0b4e8b6b3d6e67c7b (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (C) 2012 Andrew Neal 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.cyngn.eleven.widgets;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.cyngn.eleven.R;
import com.cyngn.eleven.utils.ApolloUtils;
import com.cyngn.eleven.utils.PreferenceUtils;
import com.cyngn.eleven.widgets.ColorPickerView.OnColorChangedListener;

import java.util.Locale;

/**
 * Shows the {@link ColorPanelView} in a new {@link AlertDialog}.
 * 
 * @author Andrew Neal (andrewdneal@gmail.com)
 */
public class ColorSchemeDialog extends AlertDialog implements
        ColorPickerView.OnColorChangedListener {

    private final int mCurrentColor;

    private final OnColorChangedListener mListener = this;;

    private LayoutInflater mInflater;

    private ColorPickerView mColorPicker;

    private Button mOldColor;

    private Button mNewColor;

    private View mRootView;

    private EditText mHexValue;

    /**
     * Constructor of <code>ColorSchemeDialog</code>
     * 
     * @param context The {@link Contxt} to use.
     */
    public ColorSchemeDialog(final Context context) {
        super(context);
        getWindow().setFormat(PixelFormat.RGBA_8888);
        mCurrentColor = PreferenceUtils.getInstance(context).getDefaultThemeColor(context);
        setUp(mCurrentColor);
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        ApolloUtils.removeHardwareAccelerationSupport(mColorPicker);
    }

    /*
     * (non-Javadoc)
     * @see com.cyngn.eleven.widgets.ColorPickerView.OnColorChangedListener#
     * onColorChanged(int)
     */
    @Override
    public void onColorChanged(final int color) {
        if (mHexValue != null) {
            mHexValue.setText(padLeft(Integer.toHexString(color).toUpperCase(Locale.getDefault()),
                    '0', 8));
        }
        mNewColor.setBackgroundColor(color);
    }

    private String padLeft(final String string, final char padChar, final int size) {
        if (string.length() >= size) {
            return string;
        }
        final StringBuilder result = new StringBuilder();
        for (int i = string.length(); i < size; i++) {
            result.append(padChar);
        }
        result.append(string);
        return result.toString();
    }

    /**
     * Initialzes the presets and color picker
     * 
     * @param color The color to use.
     */
    private void setUp(final int color) {
        mInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRootView = mInflater.inflate(R.layout.color_scheme_dialog, null);

        mColorPicker = (ColorPickerView)mRootView.findViewById(R.id.color_picker_view);
        mOldColor = (Button)mRootView.findViewById(R.id.color_scheme_dialog_old_color);
        mOldColor.setOnClickListener(mPresetListener);
        mNewColor = (Button)mRootView.findViewById(R.id.color_scheme_dialog_new_color);
        setUpPresets(R.id.color_scheme_dialog_preset_one);
        setUpPresets(R.id.color_scheme_dialog_preset_two);
        setUpPresets(R.id.color_scheme_dialog_preset_three);
        setUpPresets(R.id.color_scheme_dialog_preset_four);
        setUpPresets(R.id.color_scheme_dialog_preset_five);
        setUpPresets(R.id.color_scheme_dialog_preset_six);
        setUpPresets(R.id.color_scheme_dialog_preset_seven);
        setUpPresets(R.id.color_scheme_dialog_preset_eight);
        mHexValue = (EditText)mRootView.findViewById(R.id.color_scheme_dialog_hex_value);
        mHexValue.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(final CharSequence s, final int start, final int before,
                    final int count) {
                try {
                    mColorPicker.setColor(Color.parseColor("#"
                            + mHexValue.getText().toString().toUpperCase(Locale.getDefault())));
                    mNewColor.setBackgroundColor(Color.parseColor("#"
                            + mHexValue.getText().toString().toUpperCase(Locale.getDefault())));
                } catch (final Exception ignored) {
                }
            }

            @Override
            public void beforeTextChanged(final CharSequence s, final int start, final int count,
                    final int after) {
                /* Nothing to do */
            }

            @Override
            public void afterTextChanged(final Editable s) {
                /* Nothing to do */
            }
        });

        mColorPicker.setOnColorChangedListener(this);
        mOldColor.setBackgroundColor(color);
        mColorPicker.setColor(color, true);

        setTitle(R.string.color_picker_title);
        setView(mRootView);
    }

    /**
     * @param color The color resource.
     * @return A new color from Apollo's resources.
     */
    private int getColor(final int color) {
        return getContext().getResources().getColor(color);
    }

    /**
     * @return {@link ColorPickerView}'s current color
     */
    public int getColor() {
        return mColorPicker.getColor();
    }

    /**
     * @param which The Id of the preset color
     */
    private void setUpPresets(final int which) {
        final Button preset = (Button)mRootView.findViewById(which);
        if (preset != null) {
            preset.setOnClickListener(mPresetListener);
        }
    }

    /**
     * Sets up the preset buttons
     */
    private final View.OnClickListener mPresetListener = new View.OnClickListener() {

        @Override
        public void onClick(final View v) {
            switch (v.getId()) {
                case R.id.color_scheme_dialog_preset_one:
                    mColorPicker.setColor(getColor(R.color.holo_blue_light));
                    break;
                case R.id.color_scheme_dialog_preset_two:
                    mColorPicker.setColor(getColor(R.color.holo_green_light));
                    break;
                case R.id.color_scheme_dialog_preset_three:
                    mColorPicker.setColor(getColor(R.color.holo_orange_dark));
                    break;
                case R.id.color_scheme_dialog_preset_four:
                    mColorPicker.setColor(getColor(R.color.holo_orange_light));
                    break;
                case R.id.color_scheme_dialog_preset_five:
                    mColorPicker.setColor(getColor(R.color.holo_purple));
                    break;
                case R.id.color_scheme_dialog_preset_six:
                    mColorPicker.setColor(getColor(R.color.holo_red_light));
                    break;
                case R.id.color_scheme_dialog_preset_seven:
                    mColorPicker.setColor(getColor(R.color.white));
                    break;
                case R.id.color_scheme_dialog_preset_eight:
                    mColorPicker.setColor(getColor(R.color.black));
                    break;
                case R.id.color_scheme_dialog_old_color:
                    mColorPicker.setColor(mCurrentColor);
                    break;
                default:
                    break;
            }
            if (mListener != null) {
                mListener.onColorChanged(getColor());
            }
        }
    };

}