2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.filtershow.category;
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;
27 import com.android.gallery3d.R;
28 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
29 import com.android.gallery3d.filtershow.filters.FilterTinyPlanetRepresentation;
30 import com.android.gallery3d.filtershow.filters.ImageFilterTinyPlanet;
31 import com.android.gallery3d.filtershow.imageshow.MasterImage;
32 import com.android.gallery3d.filtershow.presets.ImagePreset;
33 import com.android.gallery3d.filtershow.ui.FilterIconButton;
35 public class CategoryAdapter extends ArrayAdapter<Action> {
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;
44 private int mOrientation;
46 public CategoryAdapter(Context context, int textViewResourceId) {
47 super(context, textViewResourceId);
48 mItemHeight = (int) (context.getResources().getDisplayMetrics().density * 100);
51 public CategoryAdapter(Context context) {
55 public void setItemHeight(int height) {
59 public void setItemWidth(int width) {
64 public void add(Action action) {
66 action.setAdapter(this);
69 public void initializeSelection(int category) {
71 mSelectedPosition = -1;
72 if (category == MainPanel.LOOKS) {
73 mSelectedPosition = 0;
75 if (category == MainPanel.BORDERS) {
76 mSelectedPosition = 0;
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);
88 FilterIconButton view = (FilterIconButton) convertView;
89 Action action = getItem(position);
90 view.setAction(action);
91 view.setup(action.getName(), null, this);
93 new ListView.LayoutParams(mItemWidth, mItemHeight));
94 view.setTag(position);
95 if (mCategory == MainPanel.LOOKS || mCategory == MainPanel.BORDERS) {
96 view.setBackgroundResource(0);
100 if (convertView == null) {
101 convertView = new CategoryView(getContext());
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);
113 public void setSelected(View v) {
114 int old = mSelectedPosition;
115 mSelectedPosition = (Integer) v.getTag();
119 invalidateView(mSelectedPosition);
122 public boolean isSelected(View v) {
123 return (Integer) v.getTag() == mSelectedPosition;
126 private void invalidateView(int position) {
128 if (mContainer instanceof ListView) {
129 ListView lv = (ListView) mContainer;
130 child = lv.getChildAt(position - lv.getFirstVisiblePosition());
132 CategoryTrack ct = (CategoryTrack) mContainer;
133 child = ct.getChildAt(position);
140 public void setContainer(View container) {
141 mContainer = container;
144 public void imageLoaded() {
145 notifyDataSetChanged();
148 public void setUseFilterIconButton(boolean useFilterIconButton) {
149 mUseFilterIconButton = useFilterIconButton;
152 public boolean isUseFilterIconButton() {
153 return mUseFilterIconButton;
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();
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) {
180 public void setOrientation(int orientation) {
181 mOrientation = orientation;
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);
190 rep = preset.getFilterRepresentation(pos);
192 } else if (mCategory == MainPanel.BORDERS) {
193 int pos = preset.getPositionForType(FilterRepresentation.TYPE_BORDER);
195 rep = preset.getFilterRepresentation(pos);
199 for (int i = 0; i < getCount(); i++) {
200 if (rep.getName().equalsIgnoreCase(
201 getItem(i).getRepresentation().getName())) {
206 if (mSelectedPosition != selected) {
207 mSelectedPosition = selected;
208 this.notifyDataSetChanged();