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