From: Wu-cheng Li Date: Wed, 5 Sep 2012 09:29:41 +0000 (+0800) Subject: Update media items when a picture is deleted in secure album. X-Git-Tag: android-x86-6.0-r3~2044^2~1107 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=5ca6f98efb5b1d2a48b55a3a5e4cf4ef8e8e4d78;p=android-x86%2Fpackages-apps-Camera2.git Update media items when a picture is deleted in secure album. Query the database and only show the media items that still exist. bug:7053266 Change-Id: Ic8c1b8c514e59741436bca14e918ef100eaf14f8 --- diff --git a/src/com/android/gallery3d/data/SecureAlbum.java b/src/com/android/gallery3d/data/SecureAlbum.java index a8de5ddcf..01f86ff0c 100644 --- a/src/com/android/gallery3d/data/SecureAlbum.java +++ b/src/com/android/gallery3d/data/SecureAlbum.java @@ -16,7 +16,10 @@ package com.android.gallery3d.data; +import android.content.Context; +import android.database.Cursor; import android.net.Uri; +import android.provider.MediaStore.MediaColumns; import android.provider.MediaStore.Images; import android.provider.MediaStore.Video; @@ -28,7 +31,17 @@ import java.util.ArrayList; public class SecureAlbum extends MediaSet { @SuppressWarnings("unused") private static final String TAG = "SecureAlbum"; - private ArrayList mItems = new ArrayList(); + private static final String[] PROJECTION = {MediaColumns._ID}; + private int mMinImageId = Integer.MAX_VALUE; // the smallest id of images + private int mMaxImageId = Integer.MIN_VALUE; // the biggest id in images + private int mMinVideoId = Integer.MAX_VALUE; // the smallest id of videos + private int mMaxVideoId = Integer.MIN_VALUE; // the biggest id of videos + // All the media items added by the client. + private ArrayList mAllItems = new ArrayList(); + // The types of items in mAllItems. True is video and false is image. + private ArrayList mAllItemTypes = new ArrayList(); + private ArrayList mExistingItems = new ArrayList(); + private Context mContext; private DataManager mDataManager; private static final Uri[] mWatchUris = {Images.Media.EXTERNAL_CONTENT_URI, Video.Media.EXTERNAL_CONTENT_URI}; @@ -36,26 +49,33 @@ public class SecureAlbum extends MediaSet { public SecureAlbum(Path path, GalleryApp application) { super(path, nextVersionNumber()); + mContext = application.getAndroidContext(); mDataManager = application.getDataManager(); mNotifier = new ChangeNotifier(this, mWatchUris, application); } public void addMediaItem(boolean isVideo, int id) { if (isVideo) { - mItems.add(0, Path.fromString("/local/video/item/" + id)); + mAllItems.add(Path.fromString("/local/video/item/" + id)); + mMinVideoId = Math.min(mMinVideoId, id); + mMaxVideoId = Math.max(mMaxVideoId, id); } else { - mItems.add(0, Path.fromString("/local/image/item/" + id)); + mAllItems.add(Path.fromString("/local/image/item/" + id)); + mMinImageId = Math.min(mMinImageId, id); + mMaxImageId = Math.max(mMaxImageId, id); } + mAllItemTypes.add(isVideo); mNotifier.fakeChange(); } @Override public ArrayList getMediaItem(int start, int count) { - if (start >= mItems.size()) { + if (start >= mExistingItems.size()) { return new ArrayList(); } - int end = Math.min(start + count, mItems.size()); - ArrayList subset = new ArrayList(mItems.subList(start, end)); + int end = Math.min(start + count, mExistingItems.size()); + ArrayList subset = new ArrayList( + mExistingItems.subList(start, end)); final MediaItem[] buf = new MediaItem[end - start]; ItemConsumer consumer = new ItemConsumer() { @Override @@ -73,7 +93,7 @@ public class SecureAlbum extends MediaSet { @Override public int getMediaItemCount() { - return mItems.size(); + return mExistingItems.size(); } @Override @@ -85,10 +105,52 @@ public class SecureAlbum extends MediaSet { public long reload() { if (mNotifier.isDirty()) { mDataVersion = nextVersionNumber(); + updateExistingItems(); } return mDataVersion; } + private ArrayList queryExistingIds(Uri uri, int minId, int maxId) { + ArrayList ids = new ArrayList(); + if (minId == Integer.MAX_VALUE || maxId == Integer.MIN_VALUE) return ids; + + String[] selectionArgs = {String.valueOf(minId), String.valueOf(maxId)}; + Cursor cursor = mContext.getContentResolver().query(uri, PROJECTION, + "_id BETWEEN ? AND ?", selectionArgs, null); + if (cursor == null) return ids; + try { + while (cursor.moveToNext()) { + ids.add(cursor.getInt(0)); + } + } finally { + cursor.close(); + } + return ids; + } + + private void updateExistingItems() { + if (mAllItems.size() == 0) return; + + // Query existing ids. + ArrayList imageIds = queryExistingIds( + Images.Media.EXTERNAL_CONTENT_URI, mMinImageId, mMaxImageId); + ArrayList videoIds = queryExistingIds( + Video.Media.EXTERNAL_CONTENT_URI, mMinVideoId, mMaxVideoId); + + // Construct the existing items list. + mExistingItems.clear(); + for (int i = mAllItems.size() - 1; i >= 0; i--) { + Path path = mAllItems.get(i); + boolean isVideo = mAllItemTypes.get(i); + int id = Integer.parseInt(path.getSuffix()); + if (isVideo) { + if (videoIds.contains(id)) mExistingItems.add(path); + } else { + if (imageIds.contains(id)) mExistingItems.add(path); + } + } + } + @Override public boolean isLeafAlbum() { return true;