Home | History | Annotate | Download | only in category
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.gallery3d.filtershow.category;
     18 
     19 import android.content.Context;
     20 import android.util.Log;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 import android.widget.ArrayAdapter;
     25 import android.widget.ListView;
     26 
     27 import com.android.gallery3d.R;
     28 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
     29 import com.android.gallery3d.filtershow.filters.ImageFilterTinyPlanet;
     30 import com.android.gallery3d.filtershow.imageshow.MasterImage;
     31 import com.android.gallery3d.filtershow.presets.ImagePreset;
     32 import com.android.gallery3d.filtershow.ui.FilterIconButton;
     33 
     34 public class CategoryAdapter extends ArrayAdapter<Action> {
     35 
     36     private static final String LOGTAG = "CategoryAdapter";
     37     private int mItemHeight;
     38     private View mContainer;
     39     private int mItemWidth = ListView.LayoutParams.MATCH_PARENT;
     40     private boolean mUseFilterIconButton = false;
     41     private int mSelectedPosition;
     42     int mCategory;
     43 
     44     public CategoryAdapter(Context context, int textViewResourceId) {
     45         super(context, textViewResourceId);
     46         mItemHeight = (int) (context.getResources().getDisplayMetrics().density * 100);
     47     }
     48 
     49     public CategoryAdapter(Context context) {
     50         this(context, 0);
     51     }
     52 
     53     public void setItemHeight(int height) {
     54         mItemHeight = height;
     55     }
     56 
     57     public void setItemWidth(int width) {
     58         mItemWidth = width;
     59     }
     60 
     61     @Override
     62     public void add(Action action) {
     63         super.add(action);
     64         action.setAdapter(this);
     65     }
     66 
     67     public void initializeSelection(int category) {
     68         mCategory = category;
     69         if (category == MainPanel.LOOKS || category == MainPanel.BORDERS) {
     70             ImagePreset preset = MasterImage.getImage().getPreset();
     71             if (preset != null) {
     72                 for (int i = 0; i < getCount(); i++) {
     73                     if (preset.historyName().equals(getItem(i).getRepresentation().getName())) {
     74                         mSelectedPosition = i;
     75                     }
     76                 }
     77             }
     78         } else {
     79             mSelectedPosition = -1;
     80         }
     81     }
     82 
     83     @Override
     84     public View getView(int position, View convertView, ViewGroup parent) {
     85         if (mUseFilterIconButton) {
     86             if (convertView == null) {
     87                 LayoutInflater inflater =
     88                         (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     89                 convertView = inflater.inflate(R.layout.filtericonbutton, parent, false);
     90             }
     91             FilterIconButton view = (FilterIconButton) convertView;
     92             Action action = getItem(position);
     93             view.setAction(action);
     94             view.setup(action.getName(), null, this);
     95             view.setLayoutParams(
     96                     new ListView.LayoutParams(mItemWidth, mItemHeight));
     97             view.setTag(position);
     98             if (mCategory == MainPanel.LOOKS || mCategory == MainPanel.BORDERS) {
     99                 view.setBackgroundResource(0);
    100             }
    101             return view;
    102         }
    103         if (convertView == null) {
    104             convertView = new CategoryView(getContext());
    105         }
    106         CategoryView view = (CategoryView) convertView;
    107         view.setAction(getItem(position), this);
    108         view.setLayoutParams(
    109                 new ListView.LayoutParams(mItemWidth, mItemHeight));
    110         view.setTag(position);
    111         return view;
    112     }
    113 
    114     public void setSelected(View v) {
    115         int old = mSelectedPosition;
    116         mSelectedPosition = (Integer) v.getTag();
    117         if (old != -1) {
    118             invalidateView(old);
    119         }
    120         invalidateView(mSelectedPosition);
    121     }
    122 
    123     public boolean isSelected(View v) {
    124         return (Integer) v.getTag() == mSelectedPosition;
    125     }
    126 
    127     private void invalidateView(int position) {
    128         View child = null;
    129         if (mContainer instanceof ListView) {
    130             ListView lv = (ListView) mContainer;
    131             child = lv.getChildAt(position - lv.getFirstVisiblePosition());
    132         } else {
    133             CategoryTrack ct = (CategoryTrack) mContainer;
    134             child = ct.getChildAt(position);
    135         }
    136         if (child != null) {
    137             child.invalidate();
    138         }
    139     }
    140 
    141     public void setContainer(View container) {
    142         mContainer = container;
    143     }
    144 
    145     public void imageLoaded() {
    146         notifyDataSetChanged();
    147     }
    148 
    149     public void setUseFilterIconButton(boolean useFilterIconButton) {
    150         mUseFilterIconButton = useFilterIconButton;
    151     }
    152 
    153     public boolean isUseFilterIconButton() {
    154         return mUseFilterIconButton;
    155     }
    156 
    157     public FilterRepresentation getTinyPlanet() {
    158         for (int i = 0; i < getCount(); i++) {
    159             Action action = getItem(i);
    160             if (action.getRepresentation() != null
    161                     && action.getRepresentation().getFilterClass()
    162                     == ImageFilterTinyPlanet.class) {
    163                 return action.getRepresentation();
    164             }
    165         }
    166         return null;
    167     }
    168 
    169     public void removeTinyPlanet() {
    170         for (int i = 0; i < getCount(); i++) {
    171             Action action = getItem(i);
    172             if (action.getRepresentation() != null
    173                     && action.getRepresentation().getFilterClass()
    174                     == ImageFilterTinyPlanet.class) {
    175                 remove(action);
    176                 return;
    177             }
    178         }
    179     }
    180 }
    181