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