Home | History | Annotate | Download | only in ui
      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.View;
     13 import android.view.ViewGroup;
     14 import android.widget.AbsListView;
     15 import android.widget.BaseAdapter;
     16 import android.widget.LinearLayout;
     17 
     18 import org.chromium.base.ApiCompatibilityUtils;
     19 
     20 /**
     21  * The adapter used to populate ColorPickerSimple.
     22  */
     23 public class ColorSuggestionListAdapter extends BaseAdapter implements View.OnClickListener {
     24     private Context mContext;
     25     private ColorSuggestion[] mSuggestions;
     26     private OnColorSuggestionClickListener mListener;
     27 
     28     /**
     29      * The callback used to indicate the user has clicked on a suggestion.
     30      */
     31     public interface OnColorSuggestionClickListener {
     32 
     33         /**
     34          * Called upon a click on a suggestion.
     35          *
     36          * @param suggestion The suggestion that was clicked.
     37          */
     38         void onColorSuggestionClick(ColorSuggestion suggestion);
     39     }
     40 
     41     private static final int COLORS_PER_ROW = 4;
     42 
     43     ColorSuggestionListAdapter(Context context, ColorSuggestion[] suggestions) {
     44         mContext = context;
     45         mSuggestions = suggestions;
     46     }
     47 
     48     /**
     49      * Sets the listener that will be notified upon a click on a suggestion.
     50      */
     51     public void setOnColorSuggestionClickListener(OnColorSuggestionClickListener listener) {
     52         mListener = listener;
     53     }
     54 
     55     /**
     56      * Sets up the color button to represent a color suggestion.
     57      *
     58      * @param button The button view to set up.
     59      * @param index The index of the suggestion in mSuggestions.
     60      */
     61     private void setUpColorButton(View button, int index) {
     62         if (index >= mSuggestions.length) {
     63             button.setTag(null);
     64             button.setContentDescription(null);
     65             button.setVisibility(View.INVISIBLE);
     66             return;
     67         }
     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));
     78         }
     79         button.setContentDescription(description);
     80         button.setOnClickListener(this);
     81     }
     82 
     83     @Override
     84     public void onClick(View v) {
     85         if (mListener == null) {
     86             return;
     87         }
     88         ColorSuggestion suggestion = (ColorSuggestion) v.getTag();
     89         if (suggestion == null) {
     90             return;
     91         }
     92         mListener.onColorSuggestionClick(suggestion);
     93     }
     94 
     95     @Override
     96     public View getView(int position, View convertView, ViewGroup parent) {
     97         LinearLayout layout;
     98         if (convertView != null && convertView instanceof LinearLayout) {
     99             layout = (LinearLayout) convertView;
    100         } else {
    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);
    107             int buttonHeight =
    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);
    116                 }
    117                 button.setLayoutParams(layoutParams);
    118                 button.setBackgroundResource(R.drawable.color_button_background);
    119                 layout.addView(button);
    120             }
    121         }
    122         for (int i = 0; i < COLORS_PER_ROW; ++i) {
    123             setUpColorButton(layout.getChildAt(i), position * COLORS_PER_ROW + i);
    124         }
    125         return layout;
    126     }
    127 
    128     @Override
    129     public long getItemId(int position) {
    130         return position;
    131     }
    132 
    133     @Override
    134     public Object getItem(int position) {
    135         return null;
    136     }
    137 
    138     @Override
    139     public int getCount() {
    140         return (mSuggestions.length + COLORS_PER_ROW - 1) / COLORS_PER_ROW;
    141     }
    142 }
    143