CategoryAdapter.java revision acc00f8fed8960b079448db8f05c3fbac8642ca4
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 17package com.android.gallery3d.filtershow.category; 18 19import android.content.Context; 20import android.util.Log; 21import android.view.LayoutInflater; 22import android.view.View; 23import android.view.ViewGroup; 24import android.widget.ArrayAdapter; 25import android.widget.ListView; 26 27import com.android.gallery3d.R; 28import com.android.gallery3d.filtershow.filters.FilterRepresentation; 29import com.android.gallery3d.filtershow.filters.FilterTinyPlanetRepresentation; 30import com.android.gallery3d.filtershow.filters.ImageFilterTinyPlanet; 31import com.android.gallery3d.filtershow.imageshow.MasterImage; 32import com.android.gallery3d.filtershow.presets.ImagePreset; 33import com.android.gallery3d.filtershow.ui.FilterIconButton; 34 35public class CategoryAdapter extends ArrayAdapter<Action> { 36 37 private static final String LOGTAG = "CategoryAdapter"; 38 private int mItemHeight; 39 private View mContainer; 40 private int mItemWidth = ListView.LayoutParams.MATCH_PARENT; 41 private boolean mUseFilterIconButton = false; 42 private int mSelectedPosition; 43 int mCategory; 44 private int mOrientation; 45 46 public CategoryAdapter(Context context, int textViewResourceId) { 47 super(context, textViewResourceId); 48 mItemHeight = (int) (context.getResources().getDisplayMetrics().density * 100); 49 } 50 51 public CategoryAdapter(Context context) { 52 this(context, 0); 53 } 54 55 public void setItemHeight(int height) { 56 mItemHeight = height; 57 } 58 59 public void setItemWidth(int width) { 60 mItemWidth = width; 61 } 62 63 @Override 64 public void add(Action action) { 65 super.add(action); 66 action.setAdapter(this); 67 } 68 69 public void initializeSelection(int category) { 70 mCategory = category; 71 mSelectedPosition = -1; 72 if (category == MainPanel.LOOKS) { 73 mSelectedPosition = 0; 74 } 75 if (category == MainPanel.BORDERS) { 76 mSelectedPosition = 0; 77 } 78 } 79 80 @Override 81 public View getView(int position, View convertView, ViewGroup parent) { 82 if (mUseFilterIconButton) { 83 if (convertView == null) { 84 LayoutInflater inflater = 85 (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 86 convertView = inflater.inflate(R.layout.filtericonbutton, parent, false); 87 } 88 FilterIconButton view = (FilterIconButton) convertView; 89 Action action = getItem(position); 90 view.setAction(action); 91 view.setup(action.getName(), null, this); 92 view.setLayoutParams( 93 new ListView.LayoutParams(mItemWidth, mItemHeight)); 94 view.setTag(position); 95 if (mCategory == MainPanel.LOOKS || mCategory == MainPanel.BORDERS) { 96 view.setBackgroundResource(0); 97 } 98 return view; 99 } 100 if (convertView == null) { 101 convertView = new CategoryView(getContext()); 102 } 103 CategoryView view = (CategoryView) convertView; 104 view.setOrientation(mOrientation); 105 view.setAction(getItem(position), this); 106 view.setLayoutParams( 107 new ListView.LayoutParams(mItemWidth, mItemHeight)); 108 view.setTag(position); 109 view.invalidate(); 110 return view; 111 } 112 113 public void setSelected(View v) { 114 int old = mSelectedPosition; 115 mSelectedPosition = (Integer) v.getTag(); 116 if (old != -1) { 117 invalidateView(old); 118 } 119 invalidateView(mSelectedPosition); 120 } 121 122 public boolean isSelected(View v) { 123 return (Integer) v.getTag() == mSelectedPosition; 124 } 125 126 private void invalidateView(int position) { 127 View child = null; 128 if (mContainer instanceof ListView) { 129 ListView lv = (ListView) mContainer; 130 child = lv.getChildAt(position - lv.getFirstVisiblePosition()); 131 } else { 132 CategoryTrack ct = (CategoryTrack) mContainer; 133 child = ct.getChildAt(position); 134 } 135 if (child != null) { 136 child.invalidate(); 137 } 138 } 139 140 public void setContainer(View container) { 141 mContainer = container; 142 } 143 144 public void imageLoaded() { 145 notifyDataSetChanged(); 146 } 147 148 public void setUseFilterIconButton(boolean useFilterIconButton) { 149 mUseFilterIconButton = useFilterIconButton; 150 } 151 152 public boolean isUseFilterIconButton() { 153 return mUseFilterIconButton; 154 } 155 156 public FilterRepresentation getTinyPlanet() { 157 for (int i = 0; i < getCount(); i++) { 158 Action action = getItem(i); 159 if (action.getRepresentation() != null 160 && action.getRepresentation() 161 instanceof FilterTinyPlanetRepresentation) { 162 return action.getRepresentation(); 163 } 164 } 165 return null; 166 } 167 168 public void removeTinyPlanet() { 169 for (int i = 0; i < getCount(); i++) { 170 Action action = getItem(i); 171 if (action.getRepresentation() != null 172 && action.getRepresentation() 173 instanceof FilterTinyPlanetRepresentation) { 174 remove(action); 175 return; 176 } 177 } 178 } 179 180 public void setOrientation(int orientation) { 181 mOrientation = orientation; 182 } 183 184 public void reflectImagePreset(ImagePreset preset) { 185 int selected = 0; // if nothing found, select "none" (first element) 186 FilterRepresentation rep = null; 187 if (mCategory == MainPanel.LOOKS) { 188 int pos = preset.getPositionForType(FilterRepresentation.TYPE_FX); 189 if (pos != -1) { 190 rep = preset.getFilterRepresentation(pos); 191 } 192 } else if (mCategory == MainPanel.BORDERS) { 193 int pos = preset.getPositionForType(FilterRepresentation.TYPE_BORDER); 194 if (pos != -1) { 195 rep = preset.getFilterRepresentation(pos); 196 } 197 } 198 if (rep != null) { 199 for (int i = 0; i < getCount(); i++) { 200 if (rep.getName().equalsIgnoreCase( 201 getItem(i).getRepresentation().getName())) { 202 selected = i; 203 } 204 } 205 } 206 if (mSelectedPosition != selected) { 207 mSelectedPosition = selected; 208 this.notifyDataSetChanged(); 209 } 210 211 } 212} 213