OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 package org.chromium.ui; |
| 6 |
| 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.LayoutInflater; |
| 13 import android.view.View; |
| 14 import android.view.ViewGroup; |
| 15 import android.widget.AbsListView; |
| 16 import android.widget.BaseAdapter; |
| 17 import android.widget.GridView; |
| 18 import android.widget.LinearLayout; |
| 19 |
| 20 import org.chromium.base.ApiCompatibilityUtils; |
| 21 |
| 22 import java.util.ArrayList; |
| 23 |
| 24 /** |
| 25 * The adapter used to populate ColorPickerSimple. |
| 26 */ |
| 27 public class ColorSuggestionListAdapter extends BaseAdapter implements View.OnCl
ickListener { |
| 28 private Context mContext; |
| 29 private ColorSuggestion[] mSuggestions; |
| 30 private OnColorSuggestionClickListener mListener; |
| 31 |
| 32 /** |
| 33 * The callback used to indicate the user has clicked on a suggestion. |
| 34 */ |
| 35 public interface OnColorSuggestionClickListener { |
| 36 |
| 37 /** |
| 38 * Called upon a click on a suggestion. |
| 39 * |
| 40 * @param suggestion The suggestion that was clicked. |
| 41 */ |
| 42 void onColorSuggestionClick(ColorSuggestion suggestion); |
| 43 } |
| 44 |
| 45 private final static int COLORS_PER_ROW = 4; |
| 46 |
| 47 ColorSuggestionListAdapter(Context context, ColorSuggestion[] suggestions) { |
| 48 mContext = context; |
| 49 mSuggestions = suggestions; |
| 50 } |
| 51 |
| 52 /** |
| 53 * Sets the listener that will be notified upon a click on a suggestion. |
| 54 */ |
| 55 public void setOnColorSuggestionClickListener(OnColorSuggestionClickListener
listener) { |
| 56 mListener = listener; |
| 57 } |
| 58 |
| 59 /** |
| 60 * Sets up the color button to represent a color suggestion. |
| 61 * |
| 62 * @param button The button view to set up. |
| 63 * @param index The index of the suggestion in mSuggestions. |
| 64 */ |
| 65 private void setUpColorButton(View button, int index) { |
| 66 if (index >= mSuggestions.length) { |
| 67 button.setTag(null); |
| 68 button.setContentDescription(null); |
| 69 button.setVisibility(View.INVISIBLE); |
| 70 return; |
| 71 } |
| 72 button.setTag(mSuggestions[index]); |
| 73 button.setVisibility(View.VISIBLE); |
| 74 ColorSuggestion suggestion = mSuggestions[index]; |
| 75 LayerDrawable layers = (LayerDrawable) button.getBackground(); |
| 76 GradientDrawable swatch = |
| 77 (GradientDrawable) layers.findDrawableByLayerId(R.id.color_butto
n_swatch); |
| 78 swatch.setColor(suggestion.mColor); |
| 79 String description = suggestion.mLabel; |
| 80 if (TextUtils.isEmpty(description)) { |
| 81 description = String.format("#%06X", (0xFFFFFF & suggestion.mColor))
; |
| 82 } |
| 83 button.setContentDescription(description); |
| 84 button.setOnClickListener(this); |
| 85 } |
| 86 |
| 87 @Override |
| 88 public void onClick(View v) { |
| 89 if (mListener == null) { |
| 90 return; |
| 91 } |
| 92 ColorSuggestion suggestion = (ColorSuggestion) v.getTag(); |
| 93 if (suggestion == null) { |
| 94 return; |
| 95 } |
| 96 mListener.onColorSuggestionClick(suggestion); |
| 97 } |
| 98 |
| 99 @Override |
| 100 public View getView(int position, View convertView, ViewGroup parent) { |
| 101 LinearLayout layout; |
| 102 if (convertView != null && convertView instanceof LinearLayout) { |
| 103 layout = (LinearLayout) convertView; |
| 104 } else { |
| 105 layout = new LinearLayout(mContext); |
| 106 layout.setLayoutParams(new AbsListView.LayoutParams( |
| 107 AbsListView.LayoutParams.MATCH_PARENT, |
| 108 AbsListView.LayoutParams.WRAP_CONTENT)); |
| 109 layout.setOrientation(LinearLayout.HORIZONTAL); |
| 110 layout.setBackgroundColor(Color.WHITE); |
| 111 int buttonHeight = |
| 112 mContext.getResources().getDimensionPixelOffset(R.dimen.color_bu
tton_height); |
| 113 for (int i = 0; i < COLORS_PER_ROW; ++i) { |
| 114 View button = new View(mContext); |
| 115 LinearLayout.LayoutParams layoutParams = |
| 116 new LinearLayout.LayoutParams(0, buttonHeight, 1f); |
| 117 ApiCompatibilityUtils.setMarginStart(layoutParams, -1); |
| 118 if (i == COLORS_PER_ROW - 1) { |
| 119 ApiCompatibilityUtils.setMarginEnd(layoutParams, -1); |
| 120 } |
| 121 button.setLayoutParams(layoutParams); |
| 122 button.setBackgroundResource(R.drawable.color_button_background)
; |
| 123 layout.addView(button); |
| 124 } |
| 125 } |
| 126 for (int i = 0; i < COLORS_PER_ROW; ++i) { |
| 127 setUpColorButton(layout.getChildAt(i), position * COLORS_PER_ROW + i
); |
| 128 } |
| 129 return layout; |
| 130 } |
| 131 |
| 132 @Override |
| 133 public long getItemId(int position) { |
| 134 return position; |
| 135 } |
| 136 |
| 137 @Override |
| 138 public Object getItem(int position) { |
| 139 return null; |
| 140 } |
| 141 |
| 142 @Override |
| 143 public int getCount() { |
| 144 return (mSuggestions.length + COLORS_PER_ROW - 1) / COLORS_PER_ROW; |
| 145 } |
| 146 } |
OLD | NEW |