1/*
2 * Copyright (C) 2012 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;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.Color;
22import android.graphics.drawable.Drawable;
23import android.view.LayoutInflater;
24import android.view.MenuItem;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.ArrayAdapter;
28import android.widget.ImageView;
29import android.widget.TextView;
30
31import com.android.gallery3d.R;
32import com.android.gallery3d.filtershow.presets.ImagePreset;
33
34import java.util.Vector;
35
36public class HistoryAdapter extends ArrayAdapter<ImagePreset> {
37    private static final String LOGTAG = "HistoryAdapter";
38    private int mCurrentPresetPosition = 0;
39    private String mBorders = null;
40    private String mCrop = null;
41    private String mRotate = null;
42    private String mStraighten = null;
43    private String mMirror = null;
44    private MenuItem mUndoMenuItem = null;
45    private MenuItem mRedoMenuItem = null;
46    private MenuItem mResetMenuItem = null;
47
48    private Bitmap mOriginalBitmap = null;
49
50    public HistoryAdapter(Context context, int resource, int textViewResourceId) {
51        super(context, resource, textViewResourceId);
52        FilterShowActivity activity = (FilterShowActivity) context;
53        mBorders = context.getString(R.string.borders);
54        mCrop = context.getString(R.string.crop);
55        mRotate = context.getString(R.string.rotate);
56        mStraighten = context.getString(R.string.straighten);
57        mMirror = context.getString(R.string.mirror);
58    }
59
60    public void setMenuItems(MenuItem undoItem, MenuItem redoItem, MenuItem resetItem) {
61        mUndoMenuItem = undoItem;
62        mRedoMenuItem = redoItem;
63        mResetMenuItem = resetItem;
64        updateMenuItems();
65    }
66
67    public boolean canReset() {
68        if (getCount() <= 1) {
69            return false;
70        }
71        return true;
72    }
73
74    public boolean canUndo() {
75        if (mCurrentPresetPosition == getCount() - 1) {
76            return false;
77        }
78        return true;
79    }
80
81    public boolean canRedo() {
82        if (mCurrentPresetPosition == 0) {
83            return false;
84        }
85        return true;
86    }
87
88    public void updateMenuItems() {
89        if (mUndoMenuItem != null) {
90            setEnabled(mUndoMenuItem, canUndo());
91        }
92        if (mRedoMenuItem != null) {
93            setEnabled(mRedoMenuItem, canRedo());
94        }
95        if (mResetMenuItem != null) {
96            setEnabled(mResetMenuItem, canReset());
97        }
98    }
99
100    private void setEnabled(MenuItem item, boolean enabled) {
101        item.setEnabled(enabled);
102        Drawable drawable = item.getIcon();
103        if (drawable != null) {
104            drawable.setAlpha(enabled ? 255 : 80);
105        }
106    }
107
108    public void setCurrentPreset(int n) {
109        mCurrentPresetPosition = n;
110        updateMenuItems();
111        this.notifyDataSetChanged();
112    }
113
114    public void reset() {
115        if (getCount() == 0) {
116            return;
117        }
118        ImagePreset first = getItem(getCount() - 1);
119        clear();
120        addHistoryItem(first);
121        updateMenuItems();
122    }
123
124    public ImagePreset getLast() {
125        if (getCount() == 0) {
126            return null;
127        }
128        return getItem(0);
129    }
130
131    public ImagePreset getCurrent() {
132        return getItem(mCurrentPresetPosition);
133    }
134
135    public void addHistoryItem(ImagePreset preset) {
136        insert(preset, 0);
137        updateMenuItems();
138    }
139
140    @Override
141    public void insert(ImagePreset preset, int position) {
142        if (mCurrentPresetPosition != 0) {
143            // in this case, let's discount the presets before the current one
144            Vector<ImagePreset> oldItems = new Vector<ImagePreset>();
145            for (int i = mCurrentPresetPosition; i < getCount(); i++) {
146                oldItems.add(getItem(i));
147            }
148            clear();
149            for (int i = 0; i < oldItems.size(); i++) {
150                add(oldItems.elementAt(i));
151            }
152            mCurrentPresetPosition = position;
153            this.notifyDataSetChanged();
154        }
155        super.insert(preset, position);
156        mCurrentPresetPosition = position;
157        this.notifyDataSetChanged();
158    }
159
160    public int redo() {
161        mCurrentPresetPosition--;
162        if (mCurrentPresetPosition < 0) {
163            mCurrentPresetPosition = 0;
164        }
165        this.notifyDataSetChanged();
166        updateMenuItems();
167        return mCurrentPresetPosition;
168    }
169
170    public int undo() {
171        mCurrentPresetPosition++;
172        if (mCurrentPresetPosition >= getCount()) {
173            mCurrentPresetPosition = getCount() - 1;
174        }
175        this.notifyDataSetChanged();
176        updateMenuItems();
177        return mCurrentPresetPosition;
178    }
179
180    @Override
181    public View getView(int position, View convertView, ViewGroup parent) {
182        View view = convertView;
183        if (view == null) {
184            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
185                    Context.LAYOUT_INFLATER_SERVICE);
186            view = inflater.inflate(R.layout.filtershow_history_operation_row, null);
187        }
188
189        ImagePreset item = getItem(position);
190        if (item != null) {
191            TextView itemView = (TextView) view.findViewById(R.id.rowTextView);
192            if (itemView != null) {
193                itemView.setText(item.historyName());
194            }
195            ImageView preview = (ImageView) view.findViewById(R.id.preview);
196            Bitmap bmp = item.getPreviewImage();
197            if (position == getCount()-1 && mOriginalBitmap != null) {
198                bmp = mOriginalBitmap;
199            }
200            if (bmp != null) {
201                preview.setImageBitmap(bmp);
202            } else {
203                preview.setImageResource(android.R.color.transparent);
204            }
205            if (position == mCurrentPresetPosition) {
206                view.setBackgroundColor(Color.WHITE);
207            } else {
208                view.setBackgroundResource(R.color.background_main_toolbar);
209            }
210        }
211
212        return view;
213    }
214
215
216    public void setOriginalBitmap(Bitmap originalBitmap) {
217        mOriginalBitmap = originalBitmap;
218    }
219}
220