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.view.View; 21 import android.view.ViewGroup; 22 import android.widget.ArrayAdapter; 23 import android.widget.ListView; 24 25 import com.android.gallery3d.R; 26 import com.android.gallery3d.filtershow.FilterShowActivity; 27 import com.android.gallery3d.filtershow.filters.FilterRepresentation; 28 import com.android.gallery3d.filtershow.filters.FilterTinyPlanetRepresentation; 29 import com.android.gallery3d.filtershow.pipeline.ImagePreset; 30 31 public class CategoryAdapter extends ArrayAdapter<Action> { 32 33 private static final String LOGTAG = "CategoryAdapter"; 34 private int mItemHeight; 35 private View mContainer; 36 private int mItemWidth = ListView.LayoutParams.MATCH_PARENT; 37 private int mSelectedPosition; 38 int mCategory; 39 private int mOrientation; 40 private boolean mShowAddButton = false; 41 private String mAddButtonText; 42 CategoryAdapter(Context context, int textViewResourceId)43 public CategoryAdapter(Context context, int textViewResourceId) { 44 super(context, textViewResourceId); 45 mItemHeight = (int) (context.getResources().getDisplayMetrics().density * 100); 46 } 47 CategoryAdapter(Context context)48 public CategoryAdapter(Context context) { 49 this(context, 0); 50 } 51 52 @Override clear()53 public void clear() { 54 for (int i = 0; i < getCount(); i++) { 55 Action action = getItem(i); 56 action.clearBitmap(); 57 } 58 super.clear(); 59 } 60 setItemHeight(int height)61 public void setItemHeight(int height) { 62 mItemHeight = height; 63 } 64 setItemWidth(int width)65 public void setItemWidth(int width) { 66 mItemWidth = width; 67 } 68 69 @Override add(Action action)70 public void add(Action action) { 71 super.add(action); 72 action.setAdapter(this); 73 } 74 initializeSelection(int category)75 public void initializeSelection(int category) { 76 mCategory = category; 77 mSelectedPosition = -1; 78 if (category == MainPanel.LOOKS) { 79 mSelectedPosition = 0; 80 mAddButtonText = getContext().getString(R.string.filtershow_add_button_looks); 81 } 82 if (category == MainPanel.BORDERS) { 83 mSelectedPosition = 0; 84 } 85 if (category == MainPanel.VERSIONS) { 86 mAddButtonText = getContext().getString(R.string.filtershow_add_button_versions); 87 } 88 } 89 90 @Override getView(int position, View convertView, ViewGroup parent)91 public View getView(int position, View convertView, ViewGroup parent) { 92 if (convertView == null) { 93 convertView = new CategoryView(getContext()); 94 } 95 CategoryView view = (CategoryView) convertView; 96 view.setOrientation(mOrientation); 97 Action action = getItem(position); 98 view.setAction(action, this); 99 int width = mItemWidth; 100 int height = mItemHeight; 101 if (action.getType() == Action.SPACER) { 102 if (mOrientation == CategoryView.HORIZONTAL) { 103 width = width / 2; 104 } else { 105 height = height / 2; 106 } 107 } 108 if (action.getType() == Action.ADD_ACTION 109 && mOrientation == CategoryView.VERTICAL) { 110 height = height / 2; 111 } 112 view.setLayoutParams( 113 new ListView.LayoutParams(width, height)); 114 view.setTag(position); 115 view.invalidate(); 116 return view; 117 } 118 setSelected(View v)119 public void setSelected(View v) { 120 int old = mSelectedPosition; 121 mSelectedPosition = (Integer) v.getTag(); 122 if (old != -1) { 123 invalidateView(old); 124 } 125 invalidateView(mSelectedPosition); 126 } 127 isSelected(View v)128 public boolean isSelected(View v) { 129 return (Integer) v.getTag() == mSelectedPosition; 130 } 131 invalidateView(int position)132 private void invalidateView(int position) { 133 View child = null; 134 if (mContainer instanceof ListView) { 135 ListView lv = (ListView) mContainer; 136 child = lv.getChildAt(position - lv.getFirstVisiblePosition()); 137 } else { 138 CategoryTrack ct = (CategoryTrack) mContainer; 139 child = ct.getChildAt(position); 140 } 141 if (child != null) { 142 child.invalidate(); 143 } 144 } 145 setContainer(View container)146 public void setContainer(View container) { 147 mContainer = container; 148 } 149 imageLoaded()150 public void imageLoaded() { 151 notifyDataSetChanged(); 152 } 153 getTinyPlanet()154 public FilterRepresentation getTinyPlanet() { 155 for (int i = 0; i < getCount(); i++) { 156 Action action = getItem(i); 157 if (action.getRepresentation() != null 158 && action.getRepresentation() 159 instanceof FilterTinyPlanetRepresentation) { 160 return action.getRepresentation(); 161 } 162 } 163 return null; 164 } 165 removeTinyPlanet()166 public void removeTinyPlanet() { 167 for (int i = 0; i < getCount(); i++) { 168 Action action = getItem(i); 169 if (action.getRepresentation() != null 170 && action.getRepresentation() 171 instanceof FilterTinyPlanetRepresentation) { 172 super.remove(action); 173 return; 174 } 175 } 176 } 177 178 @Override remove(Action action)179 public void remove(Action action) { 180 if (!(mCategory == MainPanel.VERSIONS 181 || mCategory == MainPanel.LOOKS)) { 182 return; 183 } 184 super.remove(action); 185 FilterShowActivity activity = (FilterShowActivity) getContext(); 186 if (mCategory == MainPanel.LOOKS) { 187 activity.removeLook(action); 188 } else if (mCategory == MainPanel.VERSIONS) { 189 activity.removeVersion(action); 190 } 191 } 192 setOrientation(int orientation)193 public void setOrientation(int orientation) { 194 mOrientation = orientation; 195 } 196 reflectImagePreset(ImagePreset preset)197 public void reflectImagePreset(ImagePreset preset) { 198 if (preset == null) { 199 return; 200 } 201 int selected = 0; // if nothing found, select "none" (first element) 202 FilterRepresentation rep = null; 203 if (mCategory == MainPanel.LOOKS) { 204 int pos = preset.getPositionForType(FilterRepresentation.TYPE_FX); 205 if (pos != -1) { 206 rep = preset.getFilterRepresentation(pos); 207 } 208 } else if (mCategory == MainPanel.BORDERS) { 209 int pos = preset.getPositionForType(FilterRepresentation.TYPE_BORDER); 210 if (pos != -1) { 211 rep = preset.getFilterRepresentation(pos); 212 } 213 } 214 if (rep != null) { 215 for (int i = 0; i < getCount(); i++) { 216 FilterRepresentation itemRep = getItem(i).getRepresentation(); 217 if (itemRep == null) { 218 continue; 219 } 220 if (rep.getName().equalsIgnoreCase( 221 itemRep.getName())) { 222 selected = i; 223 break; 224 } 225 } 226 } 227 if (mSelectedPosition != selected) { 228 mSelectedPosition = selected; 229 this.notifyDataSetChanged(); 230 } 231 } 232 showAddButton()233 public boolean showAddButton() { 234 return mShowAddButton; 235 } 236 setShowAddButton(boolean showAddButton)237 public void setShowAddButton(boolean showAddButton) { 238 mShowAddButton = showAddButton; 239 } 240 getAddButtonText()241 public String getAddButtonText() { 242 return mAddButtonText; 243 } 244 } 245