michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.widget; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import java.util.ArrayList; michael@0: import java.util.Arrays; michael@0: import java.util.List; michael@0: michael@0: import android.content.Context; michael@0: import android.content.res.Resources; michael@0: import android.graphics.Color; michael@0: import android.graphics.PorterDuff; michael@0: import android.graphics.Rect; michael@0: import android.graphics.drawable.Drawable; michael@0: import android.view.View; michael@0: import android.view.ViewGroup; michael@0: import android.view.WindowManager; michael@0: import android.widget.ArrayAdapter; michael@0: import android.widget.AdapterView; michael@0: import android.widget.CheckedTextView; michael@0: import android.widget.ListView; michael@0: import android.util.AttributeSet; michael@0: import android.util.DisplayMetrics; michael@0: import android.util.TypedValue; michael@0: michael@0: public class BasicColorPicker extends ListView { michael@0: private final static String LOGTAG = "GeckoBasicColorPicker"; michael@0: private final static List DEFAULT_COLORS = Arrays.asList(Color.rgb(215,57,32), michael@0: Color.rgb(255,134,5), michael@0: Color.rgb(255,203,19), michael@0: Color.rgb(95,173,71), michael@0: Color.rgb(84,201,168), michael@0: Color.rgb(33,161,222), michael@0: Color.rgb(16,36,87), michael@0: Color.rgb(91,32,103), michael@0: Color.rgb(212,221,228), michael@0: Color.BLACK); michael@0: michael@0: private static Drawable mCheckDrawable = null; michael@0: private int mSelected = 0; michael@0: final private ColorPickerListAdapter mAdapter; michael@0: michael@0: public BasicColorPicker(Context context) { michael@0: this(context, null); michael@0: } michael@0: michael@0: public BasicColorPicker(Context context, AttributeSet attrs) { michael@0: this(context, attrs, 0); michael@0: } michael@0: michael@0: public BasicColorPicker(Context context, AttributeSet attrs, int style) { michael@0: this(context, attrs, style, DEFAULT_COLORS); michael@0: } michael@0: michael@0: public BasicColorPicker(Context context, AttributeSet attrs, int style, List colors) { michael@0: super(context, attrs, style); michael@0: mAdapter = new ColorPickerListAdapter(context, new ArrayList(colors)); michael@0: setAdapter(mAdapter); michael@0: michael@0: setOnItemClickListener(new AdapterView.OnItemClickListener() { michael@0: @Override michael@0: public void onItemClick(AdapterView parent, View view, int position, long id) { michael@0: mSelected = position; michael@0: mAdapter.notifyDataSetChanged(); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: public int getColor() { michael@0: return mAdapter.getItem(mSelected); michael@0: } michael@0: michael@0: public void setColor(int color) { michael@0: if (!DEFAULT_COLORS.contains(color)) { michael@0: mSelected = mAdapter.getCount(); michael@0: mAdapter.add(color); michael@0: } else { michael@0: mSelected = DEFAULT_COLORS.indexOf(color); michael@0: } michael@0: michael@0: setSelection(mSelected); michael@0: mAdapter.notifyDataSetChanged(); michael@0: } michael@0: michael@0: private Drawable getCheckDrawable() { michael@0: if (mCheckDrawable == null) { michael@0: Resources res = getContext().getResources(); michael@0: michael@0: TypedValue typedValue = new TypedValue(); michael@0: getContext().getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, typedValue, true); michael@0: DisplayMetrics metrics = new android.util.DisplayMetrics(); michael@0: ((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics); michael@0: int height = (int) typedValue.getDimension(metrics); michael@0: michael@0: Drawable background = res.getDrawable(R.drawable.color_picker_row_bg); michael@0: Rect r = new Rect(); michael@0: background.getPadding(r); michael@0: height -= r.top + r.bottom; michael@0: michael@0: mCheckDrawable = res.getDrawable(R.drawable.color_picker_checkmark); michael@0: mCheckDrawable.setBounds(0, 0, height, height); michael@0: } michael@0: michael@0: return mCheckDrawable; michael@0: } michael@0: michael@0: private class ColorPickerListAdapter extends ArrayAdapter { michael@0: private final List mColors; michael@0: michael@0: public ColorPickerListAdapter(Context context, List colors) { michael@0: super(context, R.layout.color_picker_row, colors); michael@0: mColors = colors; michael@0: } michael@0: michael@0: public View getView(int position, View convertView, ViewGroup parent) { michael@0: View v = super.getView(position, convertView, parent); michael@0: michael@0: Drawable d = v.getBackground(); michael@0: d.setColorFilter(getItem(position), PorterDuff.Mode.MULTIPLY); michael@0: v.setBackgroundDrawable(d); michael@0: michael@0: Drawable check = null; michael@0: CheckedTextView checked = ((CheckedTextView) v); michael@0: if (mSelected == position) { michael@0: check = getCheckDrawable(); michael@0: } michael@0: michael@0: checked.setCompoundDrawables(check, null, null, null); michael@0: checked.setText(""); michael@0: michael@0: return v; michael@0: } michael@0: } michael@0: }