mobile/android/base/widget/BasicColorPicker.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/widget/BasicColorPicker.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.widget;
    1.10 +
    1.11 +import org.mozilla.gecko.R;
    1.12 +
    1.13 +import java.util.ArrayList;
    1.14 +import java.util.Arrays;
    1.15 +import java.util.List;
    1.16 +
    1.17 +import android.content.Context;
    1.18 +import android.content.res.Resources;
    1.19 +import android.graphics.Color;
    1.20 +import android.graphics.PorterDuff;
    1.21 +import android.graphics.Rect;
    1.22 +import android.graphics.drawable.Drawable;
    1.23 +import android.view.View;
    1.24 +import android.view.ViewGroup;
    1.25 +import android.view.WindowManager;
    1.26 +import android.widget.ArrayAdapter;
    1.27 +import android.widget.AdapterView;
    1.28 +import android.widget.CheckedTextView;
    1.29 +import android.widget.ListView;
    1.30 +import android.util.AttributeSet;
    1.31 +import android.util.DisplayMetrics;
    1.32 +import android.util.TypedValue;
    1.33 +
    1.34 +public class BasicColorPicker extends ListView {
    1.35 +    private final static String LOGTAG = "GeckoBasicColorPicker";
    1.36 +    private final static List<Integer> DEFAULT_COLORS = Arrays.asList(Color.rgb(215,57,32),
    1.37 +                                                                      Color.rgb(255,134,5),
    1.38 +                                                                      Color.rgb(255,203,19),
    1.39 +                                                                      Color.rgb(95,173,71),
    1.40 +                                                                      Color.rgb(84,201,168),
    1.41 +                                                                      Color.rgb(33,161,222),
    1.42 +                                                                      Color.rgb(16,36,87),
    1.43 +                                                                      Color.rgb(91,32,103),
    1.44 +                                                                      Color.rgb(212,221,228),
    1.45 +                                                                      Color.BLACK);
    1.46 +
    1.47 +    private static Drawable mCheckDrawable = null;
    1.48 +    private int mSelected = 0;
    1.49 +    final private ColorPickerListAdapter mAdapter;
    1.50 +
    1.51 +    public BasicColorPicker(Context context) {
    1.52 +        this(context, null);
    1.53 +    }
    1.54 +
    1.55 +    public BasicColorPicker(Context context, AttributeSet attrs) {
    1.56 +        this(context, attrs, 0);
    1.57 +    }
    1.58 +
    1.59 +    public BasicColorPicker(Context context, AttributeSet attrs, int style) {
    1.60 +        this(context, attrs, style, DEFAULT_COLORS);
    1.61 +    }
    1.62 +
    1.63 +    public BasicColorPicker(Context context, AttributeSet attrs, int style, List<Integer> colors) {
    1.64 +        super(context, attrs, style);
    1.65 +        mAdapter = new ColorPickerListAdapter(context, new ArrayList<Integer>(colors));
    1.66 +        setAdapter(mAdapter);
    1.67 +
    1.68 +        setOnItemClickListener(new AdapterView.OnItemClickListener() {
    1.69 +            @Override
    1.70 +            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    1.71 +                mSelected = position;
    1.72 +                mAdapter.notifyDataSetChanged();
    1.73 +            }
    1.74 +        });
    1.75 +    }
    1.76 +
    1.77 +    public int getColor() {
    1.78 +        return mAdapter.getItem(mSelected);
    1.79 +    }
    1.80 +
    1.81 +    public void setColor(int color) {
    1.82 +        if (!DEFAULT_COLORS.contains(color)) {
    1.83 +            mSelected = mAdapter.getCount();
    1.84 +            mAdapter.add(color);
    1.85 +        } else {
    1.86 +            mSelected = DEFAULT_COLORS.indexOf(color);
    1.87 +        }
    1.88 +
    1.89 +        setSelection(mSelected);
    1.90 +        mAdapter.notifyDataSetChanged();
    1.91 +    }
    1.92 +
    1.93 +    private Drawable getCheckDrawable() {
    1.94 +        if (mCheckDrawable == null) {
    1.95 +            Resources res = getContext().getResources();
    1.96 +
    1.97 +            TypedValue typedValue = new TypedValue();
    1.98 +            getContext().getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, typedValue, true);
    1.99 +            DisplayMetrics metrics = new android.util.DisplayMetrics();
   1.100 +            ((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
   1.101 +            int height = (int) typedValue.getDimension(metrics);
   1.102 +
   1.103 +            Drawable background = res.getDrawable(R.drawable.color_picker_row_bg);
   1.104 +            Rect r = new Rect();
   1.105 +            background.getPadding(r);
   1.106 +            height -= r.top + r.bottom;
   1.107 +
   1.108 +            mCheckDrawable = res.getDrawable(R.drawable.color_picker_checkmark);
   1.109 +            mCheckDrawable.setBounds(0, 0, height, height);
   1.110 +        }
   1.111 +
   1.112 +        return mCheckDrawable;
   1.113 +    }
   1.114 +
   1.115 +   private class ColorPickerListAdapter extends ArrayAdapter<Integer> {
   1.116 +        private final List<Integer> mColors;
   1.117 +
   1.118 +        public ColorPickerListAdapter(Context context, List<Integer> colors) {
   1.119 +            super(context, R.layout.color_picker_row, colors);
   1.120 +            mColors = colors;
   1.121 +        }
   1.122 +
   1.123 +        public View getView(int position, View convertView, ViewGroup parent) {
   1.124 +            View v = super.getView(position, convertView, parent);
   1.125 +
   1.126 +            Drawable d = v.getBackground();
   1.127 +            d.setColorFilter(getItem(position), PorterDuff.Mode.MULTIPLY);
   1.128 +            v.setBackgroundDrawable(d);
   1.129 +
   1.130 +            Drawable check = null;
   1.131 +            CheckedTextView checked = ((CheckedTextView) v);
   1.132 +            if (mSelected == position) {
   1.133 +                check = getCheckDrawable();
   1.134 +            }
   1.135 +
   1.136 +            checked.setCompoundDrawables(check, null, null, null);
   1.137 +            checked.setText("");
   1.138 +
   1.139 +            return v;
   1.140 +        }
   1.141 +    }
   1.142 +}

mercurial