Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Unified Diff: ui/android/java/src/org/chromium/ui/ColorSuggestionListAdapter.java

Issue 23026006: Add support for color input datalist on Android (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..1ece6b1dd615f48f072e8229d94c09ef07ef2b8b
--- /dev/null
+++ b/ui/android/java/src/org/chromium/ui/ColorSuggestionListAdapter.java
@@ -0,0 +1,98 @@
+// 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.graphics.drawable.StateListDrawable;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.GridView;
+
+import java.util.ArrayList;
+
+public class ColorSuggestionListAdapter extends BaseAdapter implements View.OnClickListener {
newt (away) 2013/10/09 06:07:03 javadoc for all public classes and methods (except
keishi 2013/10/15 16:44:02 Done.
+ private Context mContext;
+ private ColorSuggestion[] mSuggestions;
+ private OnColorSuggestionClickListener mListener;
+
+ ColorSuggestionListAdapter(Context context, ColorSuggestion[] suggestions) {
+ 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.
+ mContext = context;
+ mSuggestions = suggestions;
+ }
+
+ public void setOnColorSuggestionClickListener(OnColorSuggestionClickListener listener) {
+ mListener = listener;
+ }
+
+ 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.
+ if (index >= mSuggestions.length) {
+ 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.
+ button.setEnabled(false);
+ button.setContentDescription(null);
+ 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.
+ return;
+ }
+ 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.
+ button.setEnabled(true);
+ ColorSuggestion suggestion = mSuggestions[index];
+ StateListDrawable background = (StateListDrawable) button.getBackground();
+ LayerDrawable layers = (LayerDrawable) background.getCurrent();
+ GradientDrawable swatch =
+ (GradientDrawable) layers.findDrawableByLayerId(R.id.color_button_swatch);
+ swatch.setColor(suggestion.mColor);
+ String description = suggestion.mLabel;
+ 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.
+ description = String.format("#%06X", (0xFFFFFF & suggestion.mColor));
+ }
+ button.setContentDescription(description);
+ button.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
+ button.setOnClickListener(this);
+ }
+
+ @Override
+ public void onClick(View v) {
+ 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.
+ return;
+ 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.
+ if (index < 0)
+ return;
+ mListener.onColorSuggestionClick(mSuggestions[index]);
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ View layout = convertView;
+ 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
+ LayoutInflater inflater =
newt (away) 2013/10/09 06:07:03 easier: LayoutInflater inflater = LayoutInflater.f
keishi 2013/10/15 16:44:02 Done.
+ (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ layout = inflater.inflate(R.layout.color_button_row, null);
+ }
+ setupColorButton(layout.findViewById(R.id.color_button_1), position * 4);
+ setupColorButton(layout.findViewById(R.id.color_button_2), position * 4 + 1);
+ setupColorButton(layout.findViewById(R.id.color_button_3), position * 4 + 2);
+ setupColorButton(layout.findViewById(R.id.color_button_4), position * 4 + 3);
+ return layout;
+ }
+
+ @Override
+ public long getItemId(int position) {
+ return position;
+ }
+
+ @Override
+ public Object getItem(int position) {
+ 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.
+ }
+
+ @Override
+ public int getCount() {
+ 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.
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698