mobile/android/base/widget/BasicColorPicker.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial