diff options
Diffstat (limited to 'src/com/android/gallery3d/ui/SelectionManager.java')
-rw-r--r-- | src/com/android/gallery3d/ui/SelectionManager.java | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/src/com/android/gallery3d/ui/SelectionManager.java b/src/com/android/gallery3d/ui/SelectionManager.java new file mode 100644 index 000000000..b85ca7a41 --- /dev/null +++ b/src/com/android/gallery3d/ui/SelectionManager.java @@ -0,0 +1,221 @@ +/* + * Copyright (C) 2010 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.ui; + +import com.android.gallery3d.app.GalleryContext; +import com.android.gallery3d.data.DataManager; +import com.android.gallery3d.data.MediaItem; +import com.android.gallery3d.data.MediaSet; +import com.android.gallery3d.data.Path; + +import android.content.Context; +import android.os.Vibrator; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; + +public class SelectionManager { + @SuppressWarnings("unused") + private static final String TAG = "SelectionManager"; + + public static final int ENTER_SELECTION_MODE = 1; + public static final int LEAVE_SELECTION_MODE = 2; + public static final int SELECT_ALL_MODE = 3; + + private Set<Path> mClickedSet; + private MediaSet mSourceMediaSet; + private final Vibrator mVibrator; + private SelectionListener mListener; + private DataManager mDataManager; + private boolean mInverseSelection; + private boolean mIsAlbumSet; + private boolean mInSelectionMode; + private boolean mAutoLeave = true; + private int mTotal; + + public interface SelectionListener { + public void onSelectionModeChange(int mode); + public void onSelectionChange(Path path, boolean selected); + } + + public SelectionManager(GalleryContext galleryContext, boolean isAlbumSet) { + Context context = galleryContext.getAndroidContext(); + mDataManager = galleryContext.getDataManager(); + mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); + mClickedSet = new HashSet<Path>(); + mIsAlbumSet = isAlbumSet; + mTotal = -1; + } + + // Whether we will leave selection mode automatically once the number of + // selected items is down to zero. + public void setAutoLeaveSelectionMode(boolean enable) { + mAutoLeave = enable; + } + + public void setSelectionListener(SelectionListener listener) { + mListener = listener; + } + + public void selectAll() { + enterSelectionMode(); + mInverseSelection = true; + mClickedSet.clear(); + if (mListener != null) mListener.onSelectionModeChange(SELECT_ALL_MODE); + } + + public void deSelectAll() { + leaveSelectionMode(); + mInverseSelection = false; + mClickedSet.clear(); + } + + public boolean inSelectAllMode() { + return mInverseSelection; + } + + public boolean inSelectionMode() { + return mInSelectionMode; + } + + public void enterSelectionMode() { + if (mInSelectionMode) return; + + mInSelectionMode = true; + mVibrator.vibrate(100); + if (mListener != null) mListener.onSelectionModeChange(ENTER_SELECTION_MODE); + } + + public void leaveSelectionMode() { + if (!mInSelectionMode) return; + + mInSelectionMode = false; + mInverseSelection = false; + mClickedSet.clear(); + if (mListener != null) mListener.onSelectionModeChange(LEAVE_SELECTION_MODE); + } + + public boolean isItemSelected(Path itemId) { + return mInverseSelection ^ mClickedSet.contains(itemId); + } + + public int getSelectedCount() { + int count = mClickedSet.size(); + if (mInverseSelection) { + if (mTotal < 0) { + mTotal = mIsAlbumSet + ? mSourceMediaSet.getSubMediaSetCount() + : mSourceMediaSet.getMediaItemCount(); + } + count = mTotal - count; + } + return count; + } + + public void toggle(Path path) { + if (mClickedSet.contains(path)) { + mClickedSet.remove(path); + } else { + enterSelectionMode(); + mClickedSet.add(path); + } + + if (mListener != null) mListener.onSelectionChange(path, isItemSelected(path)); + if (getSelectedCount() == 0 && mAutoLeave) { + leaveSelectionMode(); + } + } + + private static void expandMediaSet(ArrayList<Path> items, MediaSet set) { + int subCount = set.getSubMediaSetCount(); + for (int i = 0; i < subCount; i++) { + expandMediaSet(items, set.getSubMediaSet(i)); + } + int total = set.getMediaItemCount(); + int batch = 50; + int index = 0; + + while (index < total) { + int count = index + batch < total + ? batch + : total - index; + ArrayList<MediaItem> list = set.getMediaItem(index, count); + for (MediaItem item : list) { + items.add(item.getPath()); + } + index += batch; + } + } + + public ArrayList<Path> getSelected(boolean expandSet) { + ArrayList<Path> selected = new ArrayList<Path>(); + if (mIsAlbumSet) { + if (mInverseSelection) { + int max = mSourceMediaSet.getSubMediaSetCount(); + for (int i = 0; i < max; i++) { + MediaSet set = mSourceMediaSet.getSubMediaSet(i); + Path id = set.getPath(); + if (!mClickedSet.contains(id)) { + if (expandSet) { + expandMediaSet(selected, set); + } else { + selected.add(id); + } + } + } + } else { + for (Path id : mClickedSet) { + if (expandSet) { + expandMediaSet(selected, mDataManager.getMediaSet(id)); + } else { + selected.add(id); + } + } + } + } else { + if (mInverseSelection) { + + int total = mSourceMediaSet.getMediaItemCount(); + int index = 0; + while (index < total) { + int count = Math.min(total - index, MediaSet.MEDIAITEM_BATCH_FETCH_COUNT); + ArrayList<MediaItem> list = mSourceMediaSet.getMediaItem(index, count); + for (MediaItem item : list) { + Path id = item.getPath(); + if (!mClickedSet.contains(id)) selected.add(id); + } + index += count; + } + } else { + for (Path id : mClickedSet) { + selected.add(id); + } + } + } + return selected; + } + + public void setSourceMediaSet(MediaSet set) { + mSourceMediaSet = set; + mTotal = -1; + } + + public MediaSet getSourceMediaSet() { + return mSourceMediaSet; + } +} |