From 39f4ad6d7e663778d74a47c8b25768c4f221fbdc Mon Sep 17 00:00:00 2001 From: nicolasroard Date: Thu, 27 Jun 2013 14:41:58 -0700 Subject: Refactor HistoryAdapter - Remove the adapter part - Rename into HistoryManager Change-Id: I58de5661770f8796882ff4a633aec700bd8bc1a5 --- .../gallery3d/filtershow/FilterShowActivity.java | 17 +- .../gallery3d/filtershow/cache/ImageLoader.java | 12 +- .../gallery3d/filtershow/editors/EditorPanel.java | 4 +- .../filtershow/history/HistoryAdapter.java | 222 --------------------- .../filtershow/history/HistoryManager.java | 172 ++++++++++++++++ .../filtershow/imageshow/MasterImage.java | 8 +- 6 files changed, 191 insertions(+), 244 deletions(-) delete mode 100644 src/com/android/gallery3d/filtershow/history/HistoryAdapter.java create mode 100644 src/com/android/gallery3d/filtershow/history/HistoryManager.java (limited to 'src/com') diff --git a/src/com/android/gallery3d/filtershow/FilterShowActivity.java b/src/com/android/gallery3d/filtershow/FilterShowActivity.java index cab1af360..f87df7175 100644 --- a/src/com/android/gallery3d/filtershow/FilterShowActivity.java +++ b/src/com/android/gallery3d/filtershow/FilterShowActivity.java @@ -79,7 +79,7 @@ import com.android.gallery3d.filtershow.filters.FilterImageBorderRepresentation; import com.android.gallery3d.filtershow.filters.FilterRepresentation; import com.android.gallery3d.filtershow.filters.FiltersManager; import com.android.gallery3d.filtershow.filters.ImageFilter; -import com.android.gallery3d.filtershow.history.HistoryAdapter; +import com.android.gallery3d.filtershow.history.HistoryManager; import com.android.gallery3d.filtershow.history.HistoryItem; import com.android.gallery3d.filtershow.imageshow.GeometryMetadata; import com.android.gallery3d.filtershow.imageshow.ImageCrop; @@ -262,7 +262,7 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL } public void setupStatePanel() { - mImageLoader.setAdapter(mMasterImage.getHistory()); + mImageLoader.setHistoryManager(mMasterImage.getHistory()); } private void fillFilters() { @@ -772,7 +772,7 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.undoButton: { - HistoryAdapter adapter = mMasterImage.getHistory(); + HistoryManager adapter = mMasterImage.getHistory(); int position = adapter.undo(); mMasterImage.onHistoryItemClick(position); backToMain(); @@ -782,7 +782,7 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL return true; } case R.id.redoButton: { - HistoryAdapter adapter = mMasterImage.getHistory(); + HistoryManager adapter = mMasterImage.getHistory(); int position = adapter.redo(); mMasterImage.onHistoryItemClick(position); invalidateViews(); @@ -894,14 +894,11 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL public void setupMasterImage() { mImageLoader = new ImageLoader(this, getApplicationContext()); - HistoryAdapter mHistoryAdapter = new HistoryAdapter( - this, R.layout.filtershow_history_operation_row, - R.id.rowTextView); - + HistoryManager mHistoryManager = new HistoryManager(); StateAdapter mImageStateAdapter = new StateAdapter(this, 0); MasterImage.reset(); mMasterImage = MasterImage.getImage(); - mMasterImage.setHistoryAdapter(mHistoryAdapter); + mMasterImage.setHistoryManager(mHistoryManager); mMasterImage.setStateAdapter(mImageStateAdapter); mMasterImage.setActivity(this); mMasterImage.setImageLoader(mImageLoader); @@ -914,7 +911,7 @@ public class FilterShowActivity extends FragmentActivity implements OnItemClickL } void resetHistory() { - HistoryAdapter adapter = mMasterImage.getHistory(); + HistoryManager adapter = mMasterImage.getHistory(); adapter.reset(); HistoryItem historyItem = adapter.getItem(0); ImagePreset original = new ImagePreset(historyItem.getImagePreset()); diff --git a/src/com/android/gallery3d/filtershow/cache/ImageLoader.java b/src/com/android/gallery3d/filtershow/cache/ImageLoader.java index cf2cb908a..8513b1c71 100644 --- a/src/com/android/gallery3d/filtershow/cache/ImageLoader.java +++ b/src/com/android/gallery3d/filtershow/cache/ImageLoader.java @@ -36,7 +36,7 @@ import com.android.gallery3d.R; import com.android.gallery3d.common.Utils; import com.android.gallery3d.exif.ExifInterface; import com.android.gallery3d.filtershow.FilterShowActivity; -import com.android.gallery3d.filtershow.history.HistoryAdapter; +import com.android.gallery3d.filtershow.history.HistoryManager; import com.android.gallery3d.filtershow.imageshow.ImageShow; import com.android.gallery3d.filtershow.imageshow.MasterImage; import com.android.gallery3d.filtershow.presets.ImagePreset; @@ -62,7 +62,7 @@ public class ImageLoader { private Bitmap mBackgroundBitmap = null; private int mOrientation = 0; - private HistoryAdapter mAdapter = null; + private HistoryManager mHistoryManager = null; private FilterShowActivity mActivity = null; @@ -501,12 +501,12 @@ public class ImageLoader { return bmap; } - public void setAdapter(HistoryAdapter adapter) { - mAdapter = adapter; + public void setHistoryManager(HistoryManager historyManager) { + mHistoryManager = historyManager; } - public HistoryAdapter getHistory() { - return mAdapter; + public HistoryManager getHistory() { + return mHistoryManager; } public XMPMeta getXmpObject() { diff --git a/src/com/android/gallery3d/filtershow/editors/EditorPanel.java b/src/com/android/gallery3d/filtershow/editors/EditorPanel.java index 82e8aa220..40dfccd02 100644 --- a/src/com/android/gallery3d/filtershow/editors/EditorPanel.java +++ b/src/com/android/gallery3d/filtershow/editors/EditorPanel.java @@ -28,7 +28,7 @@ import android.widget.ImageButton; import android.widget.LinearLayout; import com.android.gallery3d.R; import com.android.gallery3d.filtershow.FilterShowActivity; -import com.android.gallery3d.filtershow.history.HistoryAdapter; +import com.android.gallery3d.filtershow.history.HistoryManager; import com.android.gallery3d.filtershow.category.MainPanel; import com.android.gallery3d.filtershow.imageshow.MasterImage; import com.android.gallery3d.filtershow.state.StatePanel; @@ -54,7 +54,7 @@ public class EditorPanel extends Fragment { public void cancelCurrentFilter() { MasterImage masterImage = MasterImage.getImage(); - HistoryAdapter adapter = masterImage.getHistory(); + HistoryManager adapter = masterImage.getHistory(); int position = adapter.undo(); masterImage.onHistoryItemClick(position); diff --git a/src/com/android/gallery3d/filtershow/history/HistoryAdapter.java b/src/com/android/gallery3d/filtershow/history/HistoryAdapter.java deleted file mode 100644 index 81963a6a0..000000000 --- a/src/com/android/gallery3d/filtershow/history/HistoryAdapter.java +++ /dev/null @@ -1,222 +0,0 @@ -/* - * Copyright (C) 2012 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.gallery3d.filtershow.history; - -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.Color; -import android.graphics.drawable.Drawable; -import android.view.LayoutInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.ImageView; -import android.widget.TextView; - -import com.android.gallery3d.R; -import com.android.gallery3d.filtershow.FilterShowActivity; -import com.android.gallery3d.filtershow.history.HistoryItem; -import com.android.gallery3d.filtershow.presets.ImagePreset; - -import java.util.Vector; - -public class HistoryAdapter extends ArrayAdapter { - private static final String LOGTAG = "HistoryAdapter"; - private int mCurrentPresetPosition = 0; - private String mBorders = null; - private String mCrop = null; - private String mRotate = null; - private String mStraighten = null; - private String mMirror = null; - private MenuItem mUndoMenuItem = null; - private MenuItem mRedoMenuItem = null; - private MenuItem mResetMenuItem = null; - - private Bitmap mOriginalBitmap = null; - - public HistoryAdapter(Context context, int resource, int textViewResourceId) { - super(context, resource, textViewResourceId); - FilterShowActivity activity = (FilterShowActivity) context; - mBorders = context.getString(R.string.borders); - mCrop = context.getString(R.string.crop); - mRotate = context.getString(R.string.rotate); - mStraighten = context.getString(R.string.straighten); - mMirror = context.getString(R.string.mirror); - } - - public void setMenuItems(MenuItem undoItem, MenuItem redoItem, MenuItem resetItem) { - mUndoMenuItem = undoItem; - mRedoMenuItem = redoItem; - mResetMenuItem = resetItem; - updateMenuItems(); - } - - public boolean canReset() { - if (getCount() <= 1) { - return false; - } - return true; - } - - public boolean canUndo() { - if (mCurrentPresetPosition == getCount() - 1) { - return false; - } - return true; - } - - public boolean canRedo() { - if (mCurrentPresetPosition == 0) { - return false; - } - return true; - } - - public void updateMenuItems() { - if (mUndoMenuItem != null) { - setEnabled(mUndoMenuItem, canUndo()); - } - if (mRedoMenuItem != null) { - setEnabled(mRedoMenuItem, canRedo()); - } - if (mResetMenuItem != null) { - setEnabled(mResetMenuItem, canReset()); - } - } - - private void setEnabled(MenuItem item, boolean enabled) { - item.setEnabled(enabled); - Drawable drawable = item.getIcon(); - if (drawable != null) { - drawable.setAlpha(enabled ? 255 : 80); - } - } - - public void setCurrentPreset(int n) { - mCurrentPresetPosition = n; - updateMenuItems(); - this.notifyDataSetChanged(); - } - - public void reset() { - if (getCount() == 0) { - return; - } - HistoryItem first = getItem(getCount() - 1); - clear(); - addHistoryItem(first); - updateMenuItems(); - } - - public HistoryItem getLast() { - if (getCount() == 0) { - return null; - } - return getItem(0); - } - - public HistoryItem getCurrent() { - return getItem(mCurrentPresetPosition); - } - - public void addHistoryItem(HistoryItem preset) { - insert(preset, 0); - updateMenuItems(); - } - - @Override - public void insert(HistoryItem preset, int position) { - if (mCurrentPresetPosition != 0) { - // in this case, let's discount the presets before the current one - Vector oldItems = new Vector(); - for (int i = mCurrentPresetPosition; i < getCount(); i++) { - oldItems.add(getItem(i)); - } - clear(); - for (int i = 0; i < oldItems.size(); i++) { - add(oldItems.elementAt(i)); - } - mCurrentPresetPosition = position; - this.notifyDataSetChanged(); - } - super.insert(preset, position); - mCurrentPresetPosition = position; - this.notifyDataSetChanged(); - } - - public int redo() { - mCurrentPresetPosition--; - if (mCurrentPresetPosition < 0) { - mCurrentPresetPosition = 0; - } - this.notifyDataSetChanged(); - updateMenuItems(); - return mCurrentPresetPosition; - } - - public int undo() { - mCurrentPresetPosition++; - if (mCurrentPresetPosition >= getCount()) { - mCurrentPresetPosition = getCount() - 1; - } - this.notifyDataSetChanged(); - updateMenuItems(); - return mCurrentPresetPosition; - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - View view = convertView; - if (view == null) { - LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( - Context.LAYOUT_INFLATER_SERVICE); - view = inflater.inflate(R.layout.filtershow_history_operation_row, null); - } - - HistoryItem historyItem = getItem(position); - ImagePreset item = historyItem.getImagePreset(); - if (item != null) { - TextView itemView = (TextView) view.findViewById(R.id.rowTextView); - if (itemView != null && historyItem.getFilterRepresentation() != null) { - itemView.setText(historyItem.getFilterRepresentation().getName()); - } - ImageView preview = (ImageView) view.findViewById(R.id.preview); - Bitmap bmp = historyItem.getPreviewImage(); - if (position == getCount()-1 && mOriginalBitmap != null) { - bmp = mOriginalBitmap; - } - if (bmp != null) { - preview.setImageBitmap(bmp); - } else { - preview.setImageResource(android.R.color.transparent); - } - if (position == mCurrentPresetPosition) { - view.setBackgroundColor(Color.WHITE); - } else { - view.setBackgroundResource(R.color.background_main_toolbar); - } - } - - return view; - } - - - public void setOriginalBitmap(Bitmap originalBitmap) { - mOriginalBitmap = originalBitmap; - } -} diff --git a/src/com/android/gallery3d/filtershow/history/HistoryManager.java b/src/com/android/gallery3d/filtershow/history/HistoryManager.java new file mode 100644 index 000000000..755e2ea58 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/history/HistoryManager.java @@ -0,0 +1,172 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.filtershow.history; + +import android.graphics.drawable.Drawable; +import android.view.MenuItem; + +import java.util.Vector; + +public class HistoryManager { + private static final String LOGTAG = "HistoryManager"; + + private Vector mHistoryItems = new Vector(); + private int mCurrentPresetPosition = 0; + private MenuItem mUndoMenuItem = null; + private MenuItem mRedoMenuItem = null; + private MenuItem mResetMenuItem = null; + + public void setMenuItems(MenuItem undoItem, MenuItem redoItem, MenuItem resetItem) { + mUndoMenuItem = undoItem; + mRedoMenuItem = redoItem; + mResetMenuItem = resetItem; + updateMenuItems(); + } + + private int getCount() { + return mHistoryItems.size(); + } + + public HistoryItem getItem(int position) { + return mHistoryItems.elementAt(position); + } + + private void clear() { + mHistoryItems.clear(); + } + + private void add(HistoryItem item) { + mHistoryItems.add(item); + } + + private void notifyDataSetChanged() { + // TODO + } + + public boolean canReset() { + if (getCount() <= 1) { + return false; + } + return true; + } + + public boolean canUndo() { + if (mCurrentPresetPosition == getCount() - 1) { + return false; + } + return true; + } + + public boolean canRedo() { + if (mCurrentPresetPosition == 0) { + return false; + } + return true; + } + + public void updateMenuItems() { + if (mUndoMenuItem != null) { + setEnabled(mUndoMenuItem, canUndo()); + } + if (mRedoMenuItem != null) { + setEnabled(mRedoMenuItem, canRedo()); + } + if (mResetMenuItem != null) { + setEnabled(mResetMenuItem, canReset()); + } + } + + private void setEnabled(MenuItem item, boolean enabled) { + item.setEnabled(enabled); + Drawable drawable = item.getIcon(); + if (drawable != null) { + drawable.setAlpha(enabled ? 255 : 80); + } + } + + public void setCurrentPreset(int n) { + mCurrentPresetPosition = n; + updateMenuItems(); + notifyDataSetChanged(); + } + + public void reset() { + if (getCount() == 0) { + return; + } + HistoryItem first = getItem(getCount() - 1); + clear(); + addHistoryItem(first); + updateMenuItems(); + } + + public HistoryItem getLast() { + if (getCount() == 0) { + return null; + } + return getItem(0); + } + + public HistoryItem getCurrent() { + return getItem(mCurrentPresetPosition); + } + + public void addHistoryItem(HistoryItem preset) { + insert(preset, 0); + updateMenuItems(); + } + + private void insert(HistoryItem preset, int position) { + if (mCurrentPresetPosition != 0) { + // in this case, let's discount the presets before the current one + Vector oldItems = new Vector(); + for (int i = mCurrentPresetPosition; i < getCount(); i++) { + oldItems.add(getItem(i)); + } + clear(); + for (int i = 0; i < oldItems.size(); i++) { + add(oldItems.elementAt(i)); + } + mCurrentPresetPosition = position; + notifyDataSetChanged(); + } + mHistoryItems.insertElementAt(preset, position); + mCurrentPresetPosition = position; + notifyDataSetChanged(); + } + + public int redo() { + mCurrentPresetPosition--; + if (mCurrentPresetPosition < 0) { + mCurrentPresetPosition = 0; + } + notifyDataSetChanged(); + updateMenuItems(); + return mCurrentPresetPosition; + } + + public int undo() { + mCurrentPresetPosition++; + if (mCurrentPresetPosition >= getCount()) { + mCurrentPresetPosition = getCount() - 1; + } + notifyDataSetChanged(); + updateMenuItems(); + return mCurrentPresetPosition; + } + +} diff --git a/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java b/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java index 53e38f4d9..56c4a62b8 100644 --- a/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java +++ b/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java @@ -25,7 +25,7 @@ import android.os.Handler; import android.os.Message; import com.android.gallery3d.filtershow.FilterShowActivity; -import com.android.gallery3d.filtershow.history.HistoryAdapter; +import com.android.gallery3d.filtershow.history.HistoryManager; import com.android.gallery3d.filtershow.history.HistoryItem; import com.android.gallery3d.filtershow.cache.FilteringPipeline; import com.android.gallery3d.filtershow.cache.ImageLoader; @@ -64,7 +64,7 @@ public class MasterImage implements RenderingRequestCaller { private Bitmap mHighresBitmap = null; private ImageLoader mLoader = null; - private HistoryAdapter mHistory = null; + private HistoryManager mHistory = null; private StateAdapter mState = null; private FilterShowActivity mActivity = null; @@ -198,7 +198,7 @@ public class MasterImage implements RenderingRequestCaller { mHistory.setCurrentPreset(position); } - public HistoryAdapter getHistory() { + public HistoryManager getHistory() { return mHistory; } @@ -206,7 +206,7 @@ public class MasterImage implements RenderingRequestCaller { return mState; } - public void setHistoryAdapter(HistoryAdapter adapter) { + public void setHistoryManager(HistoryManager adapter) { mHistory = adapter; } -- cgit v1.2.3