Chromium Code Reviews| 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.drawable.GradientDrawable; | |
| 9 import android.graphics.drawable.LayerDrawable; | |
| 10 import android.graphics.drawable.StateListDrawable; | |
| 11 import android.view.LayoutInflater; | |
| 12 import android.view.View; | |
| 13 import android.view.ViewGroup; | |
| 14 import android.widget.BaseAdapter; | |
| 15 import android.widget.GridView; | |
| 16 | |
| 17 import java.util.ArrayList; | |
| 18 | |
| 19 public class ColorSuggestionListAdapter extends BaseAdapter implements View.OnCl ickListener { | |
|
newt (away)
2013/10/09 06:07:03
javadoc for all public classes and methods (except
keishi
2013/10/15 16:44:02
Done.
| |
| 20 private Context mContext; | |
| 21 private ColorSuggestion[] mSuggestions; | |
| 22 private OnColorSuggestionClickListener mListener; | |
| 23 | |
| 24 ColorSuggestionListAdapter(Context context, ColorSuggestion[] suggestions) { | |
| 25 super(); | |
|
newt (away)
2013/10/09 06:07:03
BaseAdapter doesn't define a constructor, so this
keishi
2013/10/15 16:44:02
Done.
| |
| 26 mContext = context; | |
| 27 mSuggestions = suggestions; | |
| 28 } | |
| 29 | |
| 30 public void setOnColorSuggestionClickListener(OnColorSuggestionClickListener listener) { | |
| 31 mListener = listener; | |
| 32 } | |
| 33 | |
| 34 private void setupColorButton(View button, int index) { | |
|
newt (away)
2013/10/09 06:07:03
"setup" -> "setUp"
keishi
2013/10/15 16:44:02
Done.
| |
| 35 if (index >= mSuggestions.length) { | |
| 36 button.setTag(new Integer(-1)); | |
|
newt (away)
2013/10/09 06:07:03
I'd just set the tag to null
keishi
2013/10/15 16:44:02
Done.
| |
| 37 button.setEnabled(false); | |
| 38 button.setContentDescription(null); | |
| 39 button.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY _NO); | |
|
newt (away)
2013/10/09 06:07:03
View.IMPORTANT_FOR_ACCESSIBILITY_NO was added in A
keishi
2013/10/15 16:44:02
Done.
| |
| 40 return; | |
| 41 } | |
| 42 button.setTag(new Integer(index)); | |
|
newt (away)
2013/10/09 06:07:03
why not just set the tag to the ColorSuggestion?
keishi
2013/10/15 16:44:02
Done.
| |
| 43 button.setEnabled(true); | |
| 44 ColorSuggestion suggestion = mSuggestions[index]; | |
| 45 StateListDrawable background = (StateListDrawable) button.getBackground( ); | |
| 46 LayerDrawable layers = (LayerDrawable) background.getCurrent(); | |
| 47 GradientDrawable swatch = | |
| 48 (GradientDrawable) layers.findDrawableByLayerId(R.id.color_butto n_swatch); | |
| 49 swatch.setColor(suggestion.mColor); | |
| 50 String description = suggestion.mLabel; | |
| 51 if (description.isEmpty()) { | |
|
newt (away)
2013/10/09 06:07:03
can you be sure that description is never null? if
keishi
2013/10/15 16:44:02
Done.
| |
| 52 description = String.format("#%06X", (0xFFFFFF & suggestion.mColor)) ; | |
| 53 } | |
| 54 button.setContentDescription(description); | |
| 55 button.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES ); | |
| 56 button.setOnClickListener(this); | |
| 57 } | |
| 58 | |
| 59 @Override | |
| 60 public void onClick(View v) { | |
| 61 if (mListener == null) | |
|
newt (away)
2013/10/09 06:07:03
need curly braces for if-else in Java, unless it's
keishi
2013/10/15 16:44:02
Done.
| |
| 62 return; | |
| 63 Integer index = (Integer) v.getTag(); | |
|
newt (away)
2013/10/09 06:07:03
it's probably good to safeguard against null tags
keishi
2013/10/15 16:44:02
Done.
| |
| 64 if (index < 0) | |
| 65 return; | |
| 66 mListener.onColorSuggestionClick(mSuggestions[index]); | |
| 67 } | |
| 68 | |
| 69 @Override | |
| 70 public View getView(int position, View convertView, ViewGroup parent) { | |
| 71 View layout = convertView; | |
| 72 if (convertView == null) { | |
|
newt (away)
2013/10/09 06:07:03
Ideally, we'd be able to configure the number of c
keishi
2013/10/15 16:44:02
I tried color_buttom.xml but the attributes like l
| |
| 73 LayoutInflater inflater = | |
|
newt (away)
2013/10/09 06:07:03
easier: LayoutInflater inflater = LayoutInflater.f
keishi
2013/10/15 16:44:02
Done.
| |
| 74 (LayoutInflater) mContext.getSystemService(Context.LAYOUT_IN FLATER_SERVICE); | |
| 75 layout = inflater.inflate(R.layout.color_button_row, null); | |
| 76 } | |
| 77 setupColorButton(layout.findViewById(R.id.color_button_1), position * 4) ; | |
| 78 setupColorButton(layout.findViewById(R.id.color_button_2), position * 4 + 1); | |
| 79 setupColorButton(layout.findViewById(R.id.color_button_3), position * 4 + 2); | |
| 80 setupColorButton(layout.findViewById(R.id.color_button_4), position * 4 + 3); | |
| 81 return layout; | |
| 82 } | |
| 83 | |
| 84 @Override | |
| 85 public long getItemId(int position) { | |
| 86 return position; | |
| 87 } | |
| 88 | |
| 89 @Override | |
| 90 public Object getItem(int position) { | |
| 91 return mSuggestions[position * 4]; | |
|
newt (away)
2013/10/09 06:07:03
I'd prefer returning null here -- that makes it cl
keishi
2013/10/15 16:44:02
Done.
| |
| 92 } | |
| 93 | |
| 94 @Override | |
| 95 public int getCount() { | |
| 96 return (int) Math.ceil(mSuggestions.length / 4d); | |
|
newt (away)
2013/10/09 06:07:03
return (mSuggestions.length + 3) / 4;
keishi
2013/10/15 16:44:02
Done.
| |
| 97 } | |
| 98 } | |
| OLD | NEW |