summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/widget/BasicColorPicker.java
blob: 4f146820348f386751a8d8f0dcf72d778f0eb296 (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
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.widget;

import org.mozilla.gecko.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;

public class BasicColorPicker extends ListView {
    private final static String LOGTAG = "GeckoBasicColorPicker";
    private final static List<Integer> DEFAULT_COLORS = Arrays.asList(Color.rgb(215, 57, 32),
                                                                      Color.rgb(255, 134, 5),
                                                                      Color.rgb(255, 203, 19),
                                                                      Color.rgb(95, 173, 71),
                                                                      Color.rgb(84, 201, 168),
                                                                      Color.rgb(33, 161, 222),
                                                                      Color.rgb(16, 36, 87),
                                                                      Color.rgb(91, 32, 103),
                                                                      Color.rgb(212, 221, 228),
                                                                      Color.BLACK);

    private static Drawable mCheckDrawable;
    int mSelected;
    final ColorPickerListAdapter mAdapter;

    public BasicColorPicker(Context context) {
        this(context, null);
    }

    public BasicColorPicker(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BasicColorPicker(Context context, AttributeSet attrs, int style) {
        this(context, attrs, style, DEFAULT_COLORS);
    }

    public BasicColorPicker(Context context, AttributeSet attrs, int style, List<Integer> colors) {
        super(context, attrs, style);
        mAdapter = new ColorPickerListAdapter(context, new ArrayList<Integer>(colors));
        setAdapter(mAdapter);

        setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mSelected = position;
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    public int getColor() {
        return mAdapter.getItem(mSelected);
    }

    public void setColor(int color) {
        if (!DEFAULT_COLORS.contains(color)) {
            mSelected = mAdapter.getCount();
            mAdapter.add(color);
        } else {
            mSelected = DEFAULT_COLORS.indexOf(color);
        }

        setSelection(mSelected);
        mAdapter.notifyDataSetChanged();
    }

    Drawable getCheckDrawable() {
        if (mCheckDrawable == null) {
            Resources res = getContext().getResources();

            TypedValue typedValue = new TypedValue();
            getContext().getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, typedValue, true);
            DisplayMetrics metrics = new android.util.DisplayMetrics();
            ((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
            int height = (int) typedValue.getDimension(metrics);

            Drawable background = res.getDrawable(R.drawable.color_picker_row_bg);
            Rect r = new Rect();
            background.getPadding(r);
            height -= r.top + r.bottom;

            mCheckDrawable = res.getDrawable(R.drawable.color_picker_checkmark);
            mCheckDrawable.setBounds(0, 0, height, height);
        }

        return mCheckDrawable;
    }

   private class ColorPickerListAdapter extends ArrayAdapter<Integer> {
        private final List<Integer> mColors;

        public ColorPickerListAdapter(Context context, List<Integer> colors) {
            super(context, R.layout.color_picker_row, colors);
            mColors = colors;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);

            Drawable d = v.getBackground();
            d.setColorFilter(getItem(position), PorterDuff.Mode.MULTIPLY);
            v.setBackgroundDrawable(d);

            Drawable check = null;
            CheckedTextView checked = ((CheckedTextView) v);
            if (mSelected == position) {
                check = getCheckDrawable();
            }

            checked.setCompoundDrawables(check, null, null, null);
            checked.setText("");

            return v;
        }
    }
}