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 17 package com.android.gallery3d.filtershow.history; 18 19 import android.graphics.drawable.Drawable; 20 import android.view.MenuItem; 21 22 import java.util.Vector; 23 24 public class HistoryManager { 25 private static final String LOGTAG = "HistoryManager"; 26 27 private Vector<HistoryItem> mHistoryItems = new Vector<HistoryItem>(); 28 private int mCurrentPresetPosition = 0; 29 private MenuItem mUndoMenuItem = null; 30 private MenuItem mRedoMenuItem = null; 31 private MenuItem mResetMenuItem = null; 32 setMenuItems(MenuItem undoItem, MenuItem redoItem, MenuItem resetItem)33 public void setMenuItems(MenuItem undoItem, MenuItem redoItem, MenuItem resetItem) { 34 mUndoMenuItem = undoItem; 35 mRedoMenuItem = redoItem; 36 mResetMenuItem = resetItem; 37 updateMenuItems(); 38 } 39 getCount()40 private int getCount() { 41 return mHistoryItems.size(); 42 } 43 getItem(int position)44 public HistoryItem getItem(int position) { 45 if (position > mHistoryItems.size() - 1) { 46 return null; 47 } 48 return mHistoryItems.elementAt(position); 49 } 50 clear()51 private void clear() { 52 mHistoryItems.clear(); 53 } 54 add(HistoryItem item)55 private void add(HistoryItem item) { 56 mHistoryItems.add(item); 57 } 58 notifyDataSetChanged()59 private void notifyDataSetChanged() { 60 // TODO 61 } 62 canReset()63 public boolean canReset() { 64 if (getCount() <= 0) { 65 return false; 66 } 67 return true; 68 } 69 canUndo()70 public boolean canUndo() { 71 if (mCurrentPresetPosition == getCount() - 1) { 72 return false; 73 } 74 return true; 75 } 76 canRedo()77 public boolean canRedo() { 78 if (mCurrentPresetPosition == 0) { 79 return false; 80 } 81 return true; 82 } 83 updateMenuItems()84 public void updateMenuItems() { 85 if (mUndoMenuItem != null) { 86 setEnabled(mUndoMenuItem, canUndo()); 87 } 88 if (mRedoMenuItem != null) { 89 setEnabled(mRedoMenuItem, canRedo()); 90 } 91 if (mResetMenuItem != null) { 92 setEnabled(mResetMenuItem, canReset()); 93 } 94 } 95 setEnabled(MenuItem item, boolean enabled)96 private void setEnabled(MenuItem item, boolean enabled) { 97 item.setEnabled(enabled); 98 Drawable drawable = item.getIcon(); 99 if (drawable != null) { 100 drawable.setAlpha(enabled ? 255 : 80); 101 } 102 } 103 setCurrentPreset(int n)104 public void setCurrentPreset(int n) { 105 mCurrentPresetPosition = n; 106 updateMenuItems(); 107 notifyDataSetChanged(); 108 } 109 reset()110 public void reset() { 111 if (getCount() == 0) { 112 return; 113 } 114 clear(); 115 updateMenuItems(); 116 } 117 getLast()118 public HistoryItem getLast() { 119 if (getCount() == 0) { 120 return null; 121 } 122 return getItem(0); 123 } 124 getCurrent()125 public HistoryItem getCurrent() { 126 return getItem(mCurrentPresetPosition); 127 } 128 addHistoryItem(HistoryItem preset)129 public void addHistoryItem(HistoryItem preset) { 130 insert(preset, 0); 131 updateMenuItems(); 132 } 133 insert(HistoryItem preset, int position)134 private void insert(HistoryItem preset, int position) { 135 if (mCurrentPresetPosition != 0) { 136 // in this case, let's discount the presets before the current one 137 Vector<HistoryItem> oldItems = new Vector<HistoryItem>(); 138 for (int i = mCurrentPresetPosition; i < getCount(); i++) { 139 oldItems.add(getItem(i)); 140 } 141 clear(); 142 for (int i = 0; i < oldItems.size(); i++) { 143 add(oldItems.elementAt(i)); 144 } 145 mCurrentPresetPosition = position; 146 notifyDataSetChanged(); 147 } 148 mHistoryItems.insertElementAt(preset, position); 149 mCurrentPresetPosition = position; 150 notifyDataSetChanged(); 151 } 152 redo()153 public int redo() { 154 mCurrentPresetPosition--; 155 if (mCurrentPresetPosition < 0) { 156 mCurrentPresetPosition = 0; 157 } 158 notifyDataSetChanged(); 159 updateMenuItems(); 160 return mCurrentPresetPosition; 161 } 162 undo()163 public int undo() { 164 mCurrentPresetPosition++; 165 if (mCurrentPresetPosition >= getCount()) { 166 mCurrentPresetPosition = getCount() - 1; 167 } 168 notifyDataSetChanged(); 169 updateMenuItems(); 170 return mCurrentPresetPosition; 171 } 172 173 } 174