CategoryAdapter.java revision 4b82f6f516ae100ae7c88441f4372dda65f01179
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.View;
21import android.view.ViewGroup;
22import android.widget.ArrayAdapter;
23import android.widget.ListView;
24
25import com.android.gallery3d.R;
26import com.android.gallery3d.filtershow.FilterShowActivity;
27import com.android.gallery3d.filtershow.filters.FilterRepresentation;
28import com.android.gallery3d.filtershow.filters.FilterTinyPlanetRepresentation;
29import com.android.gallery3d.filtershow.pipeline.ImagePreset;
30
31public 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
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            mAddButtonText = getContext().getString(R.string.filtershow_add_button_looks);
72        }
73        if (category == MainPanel.BORDERS) {
74            mSelectedPosition = 0;
75        }
76        if (category == MainPanel.VERSIONS) {
77            mAddButtonText = getContext().getString(R.string.filtershow_add_button_versions);
78        }
79    }
80
81    @Override
82    public View getView(int position, View convertView, ViewGroup parent) {
83        if (convertView == null) {
84            convertView = new CategoryView(getContext());
85        }
86        CategoryView view = (CategoryView) convertView;
87        view.setOrientation(mOrientation);
88        Action action = getItem(position);
89        view.setAction(action, this);
90        int width = mItemWidth;
91        int height = mItemHeight;
92        if (action.getType() == Action.SPACER) {
93            if (mOrientation == CategoryView.HORIZONTAL) {
94                width = width / 2;
95            } else {
96                height = height / 2;
97            }
98        }
99        if (action.getType() == Action.ADD_ACTION
100                && mOrientation == CategoryView.VERTICAL) {
101            height = height / 2;
102        }
103        view.setLayoutParams(
104                new ListView.LayoutParams(width, height));
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 FilterRepresentation getTinyPlanet() {
146        for (int i = 0; i < getCount(); i++) {
147            Action action = getItem(i);
148            if (action.getRepresentation() != null
149                    && action.getRepresentation()
150                    instanceof FilterTinyPlanetRepresentation) {
151                return action.getRepresentation();
152            }
153        }
154        return null;
155    }
156
157    public void removeTinyPlanet() {
158        for (int i = 0; i < getCount(); i++) {
159            Action action = getItem(i);
160            if (action.getRepresentation() != null
161                    && action.getRepresentation()
162                    instanceof FilterTinyPlanetRepresentation) {
163                super.remove(action);
164                return;
165            }
166        }
167    }
168
169    @Override
170    public void remove(Action action) {
171        if (!(mCategory == MainPanel.VERSIONS
172                || mCategory == MainPanel.LOOKS)) {
173            return;
174        }
175        super.remove(action);
176        FilterShowActivity activity = (FilterShowActivity) getContext();
177        if (mCategory == MainPanel.LOOKS) {
178            activity.removeLook(action);
179        } else if (mCategory == MainPanel.VERSIONS) {
180            activity.removeVersion(action);
181        }
182    }
183
184    public void setOrientation(int orientation) {
185        mOrientation = orientation;
186    }
187
188    public void reflectImagePreset(ImagePreset preset) {
189        if (preset == null) {
190            return;
191        }
192        int selected = 0; // if nothing found, select "none" (first element)
193        FilterRepresentation rep = null;
194        if (mCategory == MainPanel.LOOKS) {
195            int pos = preset.getPositionForType(FilterRepresentation.TYPE_FX);
196            if (pos != -1) {
197                rep = preset.getFilterRepresentation(pos);
198            }
199        } else if (mCategory == MainPanel.BORDERS) {
200            int pos = preset.getPositionForType(FilterRepresentation.TYPE_BORDER);
201            if (pos != -1) {
202                rep = preset.getFilterRepresentation(pos);
203            }
204        }
205        if (rep != null) {
206            for (int i = 0; i < getCount(); i++) {
207                FilterRepresentation itemRep = getItem(i).getRepresentation();
208                if (itemRep == null) {
209                    continue;
210                }
211                if (rep.getName().equalsIgnoreCase(
212                        itemRep.getName())) {
213                    selected = i;
214                    break;
215                }
216            }
217        }
218        if (mSelectedPosition != selected) {
219            mSelectedPosition = selected;
220            this.notifyDataSetChanged();
221        }
222    }
223
224    public boolean showAddButton() {
225        return mShowAddButton;
226    }
227
228    public void setShowAddButton(boolean showAddButton) {
229        mShowAddButton = showAddButton;
230    }
231
232    public String getAddButtonText() {
233        return mAddButtonText;
234    }
235}
236