Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.widget; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.R; |
michael@0 | 9 | |
michael@0 | 10 | import java.util.ArrayList; |
michael@0 | 11 | import java.util.Arrays; |
michael@0 | 12 | import java.util.List; |
michael@0 | 13 | |
michael@0 | 14 | import android.content.Context; |
michael@0 | 15 | import android.content.res.Resources; |
michael@0 | 16 | import android.graphics.Color; |
michael@0 | 17 | import android.graphics.PorterDuff; |
michael@0 | 18 | import android.graphics.Rect; |
michael@0 | 19 | import android.graphics.drawable.Drawable; |
michael@0 | 20 | import android.view.View; |
michael@0 | 21 | import android.view.ViewGroup; |
michael@0 | 22 | import android.view.WindowManager; |
michael@0 | 23 | import android.widget.ArrayAdapter; |
michael@0 | 24 | import android.widget.AdapterView; |
michael@0 | 25 | import android.widget.CheckedTextView; |
michael@0 | 26 | import android.widget.ListView; |
michael@0 | 27 | import android.util.AttributeSet; |
michael@0 | 28 | import android.util.DisplayMetrics; |
michael@0 | 29 | import android.util.TypedValue; |
michael@0 | 30 | |
michael@0 | 31 | public class BasicColorPicker extends ListView { |
michael@0 | 32 | private final static String LOGTAG = "GeckoBasicColorPicker"; |
michael@0 | 33 | private final static List<Integer> DEFAULT_COLORS = Arrays.asList(Color.rgb(215,57,32), |
michael@0 | 34 | Color.rgb(255,134,5), |
michael@0 | 35 | Color.rgb(255,203,19), |
michael@0 | 36 | Color.rgb(95,173,71), |
michael@0 | 37 | Color.rgb(84,201,168), |
michael@0 | 38 | Color.rgb(33,161,222), |
michael@0 | 39 | Color.rgb(16,36,87), |
michael@0 | 40 | Color.rgb(91,32,103), |
michael@0 | 41 | Color.rgb(212,221,228), |
michael@0 | 42 | Color.BLACK); |
michael@0 | 43 | |
michael@0 | 44 | private static Drawable mCheckDrawable = null; |
michael@0 | 45 | private int mSelected = 0; |
michael@0 | 46 | final private ColorPickerListAdapter mAdapter; |
michael@0 | 47 | |
michael@0 | 48 | public BasicColorPicker(Context context) { |
michael@0 | 49 | this(context, null); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | public BasicColorPicker(Context context, AttributeSet attrs) { |
michael@0 | 53 | this(context, attrs, 0); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | public BasicColorPicker(Context context, AttributeSet attrs, int style) { |
michael@0 | 57 | this(context, attrs, style, DEFAULT_COLORS); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | public BasicColorPicker(Context context, AttributeSet attrs, int style, List<Integer> colors) { |
michael@0 | 61 | super(context, attrs, style); |
michael@0 | 62 | mAdapter = new ColorPickerListAdapter(context, new ArrayList<Integer>(colors)); |
michael@0 | 63 | setAdapter(mAdapter); |
michael@0 | 64 | |
michael@0 | 65 | setOnItemClickListener(new AdapterView.OnItemClickListener() { |
michael@0 | 66 | @Override |
michael@0 | 67 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
michael@0 | 68 | mSelected = position; |
michael@0 | 69 | mAdapter.notifyDataSetChanged(); |
michael@0 | 70 | } |
michael@0 | 71 | }); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | public int getColor() { |
michael@0 | 75 | return mAdapter.getItem(mSelected); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | public void setColor(int color) { |
michael@0 | 79 | if (!DEFAULT_COLORS.contains(color)) { |
michael@0 | 80 | mSelected = mAdapter.getCount(); |
michael@0 | 81 | mAdapter.add(color); |
michael@0 | 82 | } else { |
michael@0 | 83 | mSelected = DEFAULT_COLORS.indexOf(color); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | setSelection(mSelected); |
michael@0 | 87 | mAdapter.notifyDataSetChanged(); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | private Drawable getCheckDrawable() { |
michael@0 | 91 | if (mCheckDrawable == null) { |
michael@0 | 92 | Resources res = getContext().getResources(); |
michael@0 | 93 | |
michael@0 | 94 | TypedValue typedValue = new TypedValue(); |
michael@0 | 95 | getContext().getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, typedValue, true); |
michael@0 | 96 | DisplayMetrics metrics = new android.util.DisplayMetrics(); |
michael@0 | 97 | ((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics); |
michael@0 | 98 | int height = (int) typedValue.getDimension(metrics); |
michael@0 | 99 | |
michael@0 | 100 | Drawable background = res.getDrawable(R.drawable.color_picker_row_bg); |
michael@0 | 101 | Rect r = new Rect(); |
michael@0 | 102 | background.getPadding(r); |
michael@0 | 103 | height -= r.top + r.bottom; |
michael@0 | 104 | |
michael@0 | 105 | mCheckDrawable = res.getDrawable(R.drawable.color_picker_checkmark); |
michael@0 | 106 | mCheckDrawable.setBounds(0, 0, height, height); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | return mCheckDrawable; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | private class ColorPickerListAdapter extends ArrayAdapter<Integer> { |
michael@0 | 113 | private final List<Integer> mColors; |
michael@0 | 114 | |
michael@0 | 115 | public ColorPickerListAdapter(Context context, List<Integer> colors) { |
michael@0 | 116 | super(context, R.layout.color_picker_row, colors); |
michael@0 | 117 | mColors = colors; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | public View getView(int position, View convertView, ViewGroup parent) { |
michael@0 | 121 | View v = super.getView(position, convertView, parent); |
michael@0 | 122 | |
michael@0 | 123 | Drawable d = v.getBackground(); |
michael@0 | 124 | d.setColorFilter(getItem(position), PorterDuff.Mode.MULTIPLY); |
michael@0 | 125 | v.setBackgroundDrawable(d); |
michael@0 | 126 | |
michael@0 | 127 | Drawable check = null; |
michael@0 | 128 | CheckedTextView checked = ((CheckedTextView) v); |
michael@0 | 129 | if (mSelected == position) { |
michael@0 | 130 | check = getCheckDrawable(); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | checked.setCompoundDrawables(check, null, null, null); |
michael@0 | 134 | checked.setText(""); |
michael@0 | 135 | |
michael@0 | 136 | return v; |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | } |