1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.ui;
7 import android.content.Context;
8 import android.graphics.Color;
9 import android.graphics.drawable.GradientDrawable;
10 import android.graphics.drawable.LayerDrawable;
11 import android.text.TextUtils;
12 import android.view.View;
13 import android.view.ViewGroup;
14 import android.widget.AbsListView;
15 import android.widget.BaseAdapter;
16 import android.widget.LinearLayout;
18 import org.chromium.base.ApiCompatibilityUtils;
21 * The adapter used to populate ColorPickerSimple.
23 public class ColorSuggestionListAdapter extends BaseAdapter implements View.OnClickListener {
24 private Context mContext;
25 private ColorSuggestion[] mSuggestions;
26 private OnColorSuggestionClickListener mListener;
29 * The callback used to indicate the user has clicked on a suggestion.
31 public interface OnColorSuggestionClickListener {
34 * Called upon a click on a suggestion.
36 * @param suggestion The suggestion that was clicked.
38 void onColorSuggestionClick(ColorSuggestion suggestion);
41 private static final int COLORS_PER_ROW = 4;
43 ColorSuggestionListAdapter(Context context, ColorSuggestion[] suggestions) {
45 mSuggestions = suggestions;
49 * Sets the listener that will be notified upon a click on a suggestion.
51 public void setOnColorSuggestionClickListener(OnColorSuggestionClickListener listener) {
56 * Sets up the color button to represent a color suggestion.
58 * @param button The button view to set up.
59 * @param index The index of the suggestion in mSuggestions.
61 private void setUpColorButton(View button, int index) {
62 if (index >= mSuggestions.length) {
64 button.setContentDescription(null);
65 button.setVisibility(View.INVISIBLE);
68 button.setTag(mSuggestions[index]);
69 button.setVisibility(View.VISIBLE);
70 ColorSuggestion suggestion = mSuggestions[index];
71 LayerDrawable layers = (LayerDrawable) button.getBackground();
72 GradientDrawable swatch =
73 (GradientDrawable) layers.findDrawableByLayerId(R.id.color_button_swatch);
74 swatch.setColor(suggestion.mColor);
75 String description = suggestion.mLabel;
76 if (TextUtils.isEmpty(description)) {
77 description = String.format("#%06X", (0xFFFFFF & suggestion.mColor));
79 button.setContentDescription(description);
80 button.setOnClickListener(this);
84 public void onClick(View v) {
85 if (mListener == null) {
88 ColorSuggestion suggestion = (ColorSuggestion) v.getTag();
89 if (suggestion == null) {
92 mListener.onColorSuggestionClick(suggestion);
96 public View getView(int position, View convertView, ViewGroup parent) {
98 if (convertView != null && convertView instanceof LinearLayout) {
99 layout = (LinearLayout) convertView;
101 layout = new LinearLayout(mContext);
102 layout.setLayoutParams(new AbsListView.LayoutParams(
103 AbsListView.LayoutParams.MATCH_PARENT,
104 AbsListView.LayoutParams.WRAP_CONTENT));
105 layout.setOrientation(LinearLayout.HORIZONTAL);
106 layout.setBackgroundColor(Color.WHITE);
108 mContext.getResources().getDimensionPixelOffset(R.dimen.color_button_height);
109 for (int i = 0; i < COLORS_PER_ROW; ++i) {
110 View button = new View(mContext);
111 LinearLayout.LayoutParams layoutParams =
112 new LinearLayout.LayoutParams(0, buttonHeight, 1f);
113 ApiCompatibilityUtils.setMarginStart(layoutParams, -1);
114 if (i == COLORS_PER_ROW - 1) {
115 ApiCompatibilityUtils.setMarginEnd(layoutParams, -1);
117 button.setLayoutParams(layoutParams);
118 button.setBackgroundResource(R.drawable.color_button_background);
119 layout.addView(button);
122 for (int i = 0; i < COLORS_PER_ROW; ++i) {
123 setUpColorButton(layout.getChildAt(i), position * COLORS_PER_ROW + i);
129 public long getItemId(int position) {
134 public Object getItem(int position) {
139 public int getCount() {
140 return (mSuggestions.length + COLORS_PER_ROW - 1) / COLORS_PER_ROW;