diff options
author | Chih-Chung Chang <chihchung@google.com> | 2012-06-07 20:09:13 +0800 |
---|---|---|
committer | Chih-Chung Chang <chihchung@google.com> | 2012-06-18 17:59:58 +0800 |
commit | 6b891c6a3739f8c49d42f9db6fc76cb92c7c5f25 (patch) | |
tree | 78e1fc1d4ffb3acb2cd9bead495cc56451c822e9 /src/com/android/gallery3d/data | |
parent | 6dd670997a88fe7502f9ce0e4f9c872b7352027f (diff) | |
download | android_packages_apps_Gallery2-6b891c6a3739f8c49d42f9db6fc76cb92c7c5f25.tar.gz android_packages_apps_Gallery2-6b891c6a3739f8c49d42f9db6fc76cb92c7c5f25.tar.bz2 android_packages_apps_Gallery2-6b891c6a3739f8c49d42f9db6fc76cb92c7c5f25.zip |
Add swipe-to-delete gesture.
Change-Id: I992e59702f9dfff17da2f4464e48c9228d42b1b3
Diffstat (limited to 'src/com/android/gallery3d/data')
-rw-r--r-- | src/com/android/gallery3d/data/FilterDeleteSet.java | 115 | ||||
-rw-r--r-- | src/com/android/gallery3d/data/FilterSource.java | 24 | ||||
-rw-r--r-- | src/com/android/gallery3d/data/FilterTypeSet.java (renamed from src/com/android/gallery3d/data/FilterSet.java) | 10 | ||||
-rw-r--r-- | src/com/android/gallery3d/data/MtpSource.java | 6 |
4 files changed, 140 insertions, 15 deletions
diff --git a/src/com/android/gallery3d/data/FilterDeleteSet.java b/src/com/android/gallery3d/data/FilterDeleteSet.java new file mode 100644 index 000000000..fc94eb8e0 --- /dev/null +++ b/src/com/android/gallery3d/data/FilterDeleteSet.java @@ -0,0 +1,115 @@ +/* + * 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.data; + +import java.util.ArrayList; + +// FilterDeleteSet filters a base MediaSet to remove a deletion item. The user +// can use the following method to change the deletion item: +// +// void setDeletion(Path path, int index); +// +// If the path is null, there is no deletion item. +public class FilterDeleteSet extends MediaSet implements ContentListener { + private static final String TAG = "FilterDeleteSet"; + + private final MediaSet mBaseSet; + private Path mDeletionPath; + private int mDeletionIndexHint; + private boolean mNewDeletionSettingPending = false; + + // This is set to true or false in reload(), so we know if the given + // mDelectionPath is still in the mBaseSet, and if so we can adjust the + // index and items. + private boolean mDeletionInEffect; + private int mDeletionIndex; + + public FilterDeleteSet(Path path, MediaSet baseSet) { + super(path, INVALID_DATA_VERSION); + mBaseSet = baseSet; + mBaseSet.addContentListener(this); + } + + @Override + public String getName() { + return mBaseSet.getName(); + } + + @Override + public int getMediaItemCount() { + if (mDeletionInEffect) { + return mBaseSet.getMediaItemCount() - 1; + } else { + return mBaseSet.getMediaItemCount(); + } + } + + @Override + public ArrayList<MediaItem> getMediaItem(int start, int count) { + if (!mDeletionInEffect || mDeletionIndex >= start + count) { + return mBaseSet.getMediaItem(start, count); + } + if (mDeletionIndex < start) { + return mBaseSet.getMediaItem(start + 1, count); + } + ArrayList<MediaItem> base = mBaseSet.getMediaItem(start, count + 1); + base.remove(mDeletionIndex - start); + return base; + } + + @Override + public long reload() { + boolean newData = mBaseSet.reload() > mDataVersion; + if (!newData && !mNewDeletionSettingPending) return mDataVersion; + mNewDeletionSettingPending = false; + mDeletionInEffect = false; + if (mDeletionPath != null) { + // See if mDeletionPath can be found in the MediaSet. We don't want + // to search the whole mBaseSet, so we just search a small window + // that is close the the index hint. + int n = mBaseSet.getMediaItemCount(); + int from = Math.max(mDeletionIndexHint - 5, 0); + int to = Math.min(mDeletionIndexHint + 5, n); + ArrayList<MediaItem> items = mBaseSet.getMediaItem(from, to - from); + for (int i = 0; i < items.size(); i++) { + MediaItem item = items.get(i); + if (item != null && item.getPath() == mDeletionPath) { + mDeletionInEffect = true; + mDeletionIndex = i + from; + } + } + // We cannot find this path. Set it to null to avoid further search. + if (!mDeletionInEffect) { + mDeletionPath = null; + } + } + mDataVersion = nextVersionNumber(); + return mDataVersion; + } + + @Override + public void onContentDirty() { + notifyContentChanged(); + } + + public void setDeletion(Path path, int indexHint) { + mDeletionPath = path; + mDeletionIndexHint = indexHint; + mNewDeletionSettingPending = true; + notifyContentChanged(); + } +} diff --git a/src/com/android/gallery3d/data/FilterSource.java b/src/com/android/gallery3d/data/FilterSource.java index d1a04c995..b3e6ee356 100644 --- a/src/com/android/gallery3d/data/FilterSource.java +++ b/src/com/android/gallery3d/data/FilterSource.java @@ -21,6 +21,7 @@ import com.android.gallery3d.app.GalleryApp; class FilterSource extends MediaSource { private static final String TAG = "FilterSource"; private static final int FILTER_BY_MEDIATYPE = 0; + private static final int FILTER_BY_DELETE = 1; private GalleryApp mApplication; private PathMatcher mMatcher; @@ -30,21 +31,28 @@ class FilterSource extends MediaSource { mApplication = application; mMatcher = new PathMatcher(); mMatcher.add("/filter/mediatype/*/*", FILTER_BY_MEDIATYPE); + mMatcher.add("/filter/delete/*", FILTER_BY_DELETE); } - // The name we accept is: - // /filter/mediatype/k/{set} - // where k is the media type we want. + // The name we accept are: + // /filter/mediatype/k/{set} where k is the media type we want. + // /filter/delete/{set} @Override public MediaObject createMediaObject(Path path) { int matchType = mMatcher.match(path); - int mediaType = mMatcher.getIntVar(0); - String setsName = mMatcher.getVar(1); DataManager dataManager = mApplication.getDataManager(); - MediaSet[] sets = dataManager.getMediaSetsFromString(setsName); switch (matchType) { - case FILTER_BY_MEDIATYPE: - return new FilterSet(path, dataManager, sets[0], mediaType); + case FILTER_BY_MEDIATYPE: { + int mediaType = mMatcher.getIntVar(0); + String setsName = mMatcher.getVar(1); + MediaSet[] sets = dataManager.getMediaSetsFromString(setsName); + return new FilterTypeSet(path, dataManager, sets[0], mediaType); + } + case FILTER_BY_DELETE: { + String setsName = mMatcher.getVar(0); + MediaSet[] sets = dataManager.getMediaSetsFromString(setsName); + return new FilterDeleteSet(path, sets[0]); + } default: throw new RuntimeException("bad path: " + path); } diff --git a/src/com/android/gallery3d/data/FilterSet.java b/src/com/android/gallery3d/data/FilterTypeSet.java index 9cb7e02ef..1983a39f1 100644 --- a/src/com/android/gallery3d/data/FilterSet.java +++ b/src/com/android/gallery3d/data/FilterTypeSet.java @@ -18,12 +18,10 @@ package com.android.gallery3d.data; import java.util.ArrayList; -// FilterSet filters a base MediaSet according to a condition. Currently the -// condition is a matching media type. It can be extended to other conditions -// if needed. -public class FilterSet extends MediaSet implements ContentListener { +// FilterTypeSet filters a base MediaSet according to a matching media type. +public class FilterTypeSet extends MediaSet implements ContentListener { @SuppressWarnings("unused") - private static final String TAG = "FilterSet"; + private static final String TAG = "FilterTypeSet"; private final DataManager mDataManager; private final MediaSet mBaseSet; @@ -31,7 +29,7 @@ public class FilterSet extends MediaSet implements ContentListener { private final ArrayList<Path> mPaths = new ArrayList<Path>(); private final ArrayList<MediaSet> mAlbums = new ArrayList<MediaSet>(); - public FilterSet(Path path, DataManager dataManager, MediaSet baseSet, + public FilterTypeSet(Path path, DataManager dataManager, MediaSet baseSet, int mediaType) { super(path, INVALID_DATA_VERSION); mDataManager = dataManager; diff --git a/src/com/android/gallery3d/data/MtpSource.java b/src/com/android/gallery3d/data/MtpSource.java index 683a40291..aaf50ad4c 100644 --- a/src/com/android/gallery3d/data/MtpSource.java +++ b/src/com/android/gallery3d/data/MtpSource.java @@ -18,7 +18,7 @@ package com.android.gallery3d.data; import com.android.gallery3d.app.GalleryApp; -class MtpSource extends MediaSource { +public class MtpSource extends MediaSource { private static final String TAG = "MtpSource"; private static final int MTP_DEVICESET = 0; @@ -68,4 +68,8 @@ class MtpSource extends MediaSource { public void resume() { mMtpContext.resume(); } + + public static boolean isMtpPath(String s) { + return s != null && Path.fromString(s).getPrefix().equals("mtp"); + } } |