Chromium Code Reviews| Index: ui/android/java/src/org/chromium/ui/ColorSuggestionListAdapter.java |
| diff --git a/ui/android/java/src/org/chromium/ui/ColorSuggestionListAdapter.java b/ui/android/java/src/org/chromium/ui/ColorSuggestionListAdapter.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..19a5c72d162756ff9f21dca9dc512d724c3da899 |
| --- /dev/null |
| +++ b/ui/android/java/src/org/chromium/ui/ColorSuggestionListAdapter.java |
| @@ -0,0 +1,132 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.ui; |
| + |
| +import android.content.Context; |
| +import android.graphics.drawable.GradientDrawable; |
| +import android.graphics.drawable.LayerDrawable; |
| +import android.text.TextUtils; |
| +import android.view.LayoutInflater; |
| +import android.view.View; |
| +import android.view.ViewGroup; |
| +import android.widget.AbsListView.LayoutParams; |
| +import android.widget.BaseAdapter; |
| +import android.widget.GridView; |
| +import android.widget.LinearLayout; |
| + |
| +import java.util.ArrayList; |
| + |
| +public class ColorSuggestionListAdapter extends BaseAdapter implements View.OnClickListener { |
|
newt (away)
2013/10/15 18:00:15
still need javadoc for the class
keishi
2013/10/16 04:47:23
Done.
|
| + private Context mContext; |
| + private ColorSuggestion[] mSuggestions; |
| + private OnColorSuggestionClickListener mListener; |
| + |
| + /** |
| + * The callback used to indicate the user has clicked on a suggestion. |
| + */ |
| + public interface OnColorSuggestionClickListener { |
| + |
| + /** |
| + * Called upon a click on a suggestion. |
| + * |
| + * @param suggestion The suggestion that was clicked. |
| + */ |
| + void onColorSuggestionClick(ColorSuggestion suggestion); |
| + } |
| + |
| + private final static int COLORS_PER_ROW = 4; |
| + |
| + ColorSuggestionListAdapter(Context context, ColorSuggestion[] suggestions) { |
| + mContext = context; |
| + mSuggestions = suggestions; |
| + } |
| + |
| + /** |
| + * Sets the listener that will be notified upon a click on a suggestion. |
| + */ |
| + public void setOnColorSuggestionClickListener(OnColorSuggestionClickListener listener) { |
| + mListener = listener; |
| + } |
| + |
| + /** |
| + * Sets up the color button to represent a color suggestion. |
| + * |
| + * @param button The button view to set up. |
| + * @param index The index of the suggestion in mSuggestions. |
| + */ |
| + private void setUpColorButton(View button, int index) { |
| + if (index >= mSuggestions.length) { |
| + button.setTag(null); |
| + button.setContentDescription(null); |
| + button.setVisibility(View.INVISIBLE); |
| + return; |
| + } |
| + button.setTag(mSuggestions[index]); |
| + button.setVisibility(View.VISIBLE); |
| + ColorSuggestion suggestion = mSuggestions[index]; |
| + LayerDrawable layers = (LayerDrawable) button.getBackground(); |
| + GradientDrawable swatch = |
| + (GradientDrawable) layers.findDrawableByLayerId(R.id.color_button_swatch); |
| + swatch.setColor(suggestion.mColor); |
| + String description = suggestion.mLabel; |
| + if (TextUtils.isEmpty(description)) { |
| + description = String.format("#%06X", (0xFFFFFF & suggestion.mColor)); |
| + } |
| + button.setContentDescription(description); |
| + button.setOnClickListener(this); |
| + } |
| + |
| + @Override |
| + public void onClick(View v) { |
| + if (mListener == null) { |
| + return; |
| + } |
| + ColorSuggestion suggestion = (ColorSuggestion) v.getTag(); |
| + if (suggestion == null) { |
| + return; |
| + } |
| + mListener.onColorSuggestionClick(suggestion); |
| + } |
| + |
| + @Override |
| + public View getView(int position, View convertView, ViewGroup parent) { |
| + LinearLayout layout = (LinearLayout) convertView; |
| + if (convertView == null) { |
|
newt (away)
2013/10/15 18:00:15
nit: convertView -> layout
keishi
2013/10/16 04:47:23
Done.
|
| + layout = new LinearLayout(mContext); |
| + layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, |
| + LayoutParams.WRAP_CONTENT)); |
| + layout.setOrientation(LinearLayout.HORIZONTAL); |
| + LayoutInflater inflater = LayoutInflater.from(mContext); |
|
newt (away)
2013/10/15 18:00:15
inflater is unused
keishi
2013/10/16 04:47:23
Done.
|
| + for (int i = 0; i < COLORS_PER_ROW; ++i) { |
| + View button = new View(mContext); |
| + button.setLayoutParams(new android.widget.LinearLayout.LayoutParams( |
|
newt (away)
2013/10/15 18:00:15
to make the different types of layout params clear
keishi
2013/10/16 04:47:23
Done.
|
| + 0, |
| + (int) mContext.getResources().getDimension(R.dimen.color_button_height), |
|
newt (away)
2013/10/15 18:00:15
nit: get this value once outside of the loop
keishi
2013/10/16 04:47:23
Done.
|
| + 1f)); |
| + button.setBackgroundResource(R.drawable.color_button_background); |
| + layout.addView(button); |
| + } |
| + } |
| + for (int i = 0; i < COLORS_PER_ROW; ++i) { |
| + setUpColorButton(layout.getChildAt(i), position * COLORS_PER_ROW + i); |
| + } |
| + return layout; |
| + } |
| + |
| + @Override |
| + public long getItemId(int position) { |
| + return position; |
| + } |
| + |
| + @Override |
| + public Object getItem(int position) { |
| + return null; |
| + } |
| + |
| + @Override |
| + public int getCount() { |
| + return (mSuggestions.length + COLORS_PER_ROW - 1) / COLORS_PER_ROW; |
| + } |
| +} |