Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.data; |
| 18 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 19 | import android.content.ContentResolver; |
| 20 | import android.database.Cursor; |
| 21 | import android.net.Uri; |
| 22 | import android.provider.MediaStore.Files; |
| 23 | import android.provider.MediaStore.Files.FileColumns; |
| 24 | import android.provider.MediaStore.Images; |
| 25 | import android.provider.MediaStore.Images.ImageColumns; |
| 26 | import android.provider.MediaStore.Video; |
Owen Lin | e80b476 | 2012-03-13 14:17:01 +0800 | [diff] [blame^] | 27 | import android.util.Log; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 28 | |
Owen Lin | 73a04ff | 2012-03-14 17:27:24 +0800 | [diff] [blame] | 29 | import com.android.gallery3d.R; |
| 30 | import com.android.gallery3d.app.GalleryApp; |
| 31 | import com.android.gallery3d.common.Utils; |
| 32 | import com.android.gallery3d.util.GalleryUtils; |
| 33 | import com.android.gallery3d.util.MediaSetUtils; |
| 34 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 35 | import java.util.ArrayList; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 36 | import java.util.Comparator; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 37 | |
| 38 | // LocalAlbumSet lists all image or video albums in the local storage. |
| 39 | // The path should be "/local/image", "local/video" or "/local/all" |
| 40 | public class LocalAlbumSet extends MediaSet { |
| 41 | public static final Path PATH_ALL = Path.fromString("/local/all"); |
| 42 | public static final Path PATH_IMAGE = Path.fromString("/local/image"); |
| 43 | public static final Path PATH_VIDEO = Path.fromString("/local/video"); |
| 44 | |
| 45 | private static final String TAG = "LocalAlbumSet"; |
| 46 | private static final String EXTERNAL_MEDIA = "external"; |
| 47 | |
| 48 | // The indices should match the following projections. |
| 49 | private static final int INDEX_BUCKET_ID = 0; |
| 50 | private static final int INDEX_MEDIA_TYPE = 1; |
| 51 | private static final int INDEX_BUCKET_NAME = 2; |
| 52 | |
| 53 | private static final Uri mBaseUri = Files.getContentUri(EXTERNAL_MEDIA); |
| 54 | private static final Uri mWatchUriImage = Images.Media.EXTERNAL_CONTENT_URI; |
| 55 | private static final Uri mWatchUriVideo = Video.Media.EXTERNAL_CONTENT_URI; |
| 56 | |
Chih-Chung Chang | fbef386 | 2011-09-29 19:07:00 +0800 | [diff] [blame] | 57 | // BUCKET_DISPLAY_NAME is a string like "Camera" which is the directory |
| 58 | // name of where an image or video is in. BUCKET_ID is a hash of the path |
| 59 | // name of that directory (see computeBucketValues() in MediaProvider for |
| 60 | // details). MEDIA_TYPE is video, image, audio, etc. |
| 61 | // |
| 62 | // The "albums" are not explicitly recorded in the database, but each image |
| 63 | // or video has the two columns (BUCKET_ID, MEDIA_TYPE). We define an |
| 64 | // "album" to be the collection of images/videos which have the same value |
| 65 | // for the two columns. |
| 66 | // |
| 67 | // The goal of the query (used in loadSubMediaSets()) is to find all albums, |
| 68 | // that is, all unique values for (BUCKET_ID, MEDIA_TYPE). In the meantime |
| 69 | // sort them by the timestamp of the latest image/video in each of the album. |
| 70 | // |
| 71 | // The order of columns below is important: it must match to the index in |
| 72 | // MediaStore. |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 73 | private static final String[] PROJECTION_BUCKET = { |
| 74 | ImageColumns.BUCKET_ID, |
| 75 | FileColumns.MEDIA_TYPE, |
| 76 | ImageColumns.BUCKET_DISPLAY_NAME }; |
| 77 | |
Chih-Chung Chang | fbef386 | 2011-09-29 19:07:00 +0800 | [diff] [blame] | 78 | // We want to order the albums by reverse chronological order. We abuse the |
| 79 | // "WHERE" parameter to insert a "GROUP BY" clause into the SQL statement. |
| 80 | // The template for "WHERE" parameter is like: |
| 81 | // SELECT ... FROM ... WHERE (%s) |
| 82 | // and we make it look like: |
| 83 | // SELECT ... FROM ... WHERE (1) GROUP BY 1,(2) |
| 84 | // The "(1)" means true. The "1,(2)" means the first two columns specified |
| 85 | // after SELECT. Note that because there is a ")" in the template, we use |
| 86 | // "(2" to match it. |
| 87 | private static final String BUCKET_GROUP_BY = |
| 88 | "1) GROUP BY 1,(2"; |
| 89 | private static final String BUCKET_ORDER_BY = "MAX(datetaken) DESC"; |
| 90 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 91 | private final GalleryApp mApplication; |
| 92 | private final int mType; |
| 93 | private ArrayList<MediaSet> mAlbums = new ArrayList<MediaSet>(); |
| 94 | private final ChangeNotifier mNotifierImage; |
| 95 | private final ChangeNotifier mNotifierVideo; |
| 96 | private final String mName; |
| 97 | |
| 98 | public LocalAlbumSet(Path path, GalleryApp application) { |
| 99 | super(path, nextVersionNumber()); |
| 100 | mApplication = application; |
| 101 | mType = getTypeFromPath(path); |
| 102 | mNotifierImage = new ChangeNotifier(this, mWatchUriImage, application); |
| 103 | mNotifierVideo = new ChangeNotifier(this, mWatchUriVideo, application); |
| 104 | mName = application.getResources().getString( |
| 105 | R.string.set_label_local_albums); |
| 106 | } |
| 107 | |
| 108 | private static int getTypeFromPath(Path path) { |
| 109 | String name[] = path.split(); |
| 110 | if (name.length < 2) { |
| 111 | throw new IllegalArgumentException(path.toString()); |
| 112 | } |
| 113 | if ("all".equals(name[1])) return MEDIA_TYPE_ALL; |
| 114 | if ("image".equals(name[1])) return MEDIA_TYPE_IMAGE; |
| 115 | if ("video".equals(name[1])) return MEDIA_TYPE_VIDEO; |
| 116 | throw new IllegalArgumentException(path.toString()); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public MediaSet getSubMediaSet(int index) { |
| 121 | return mAlbums.get(index); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public int getSubMediaSetCount() { |
| 126 | return mAlbums.size(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String getName() { |
| 131 | return mName; |
| 132 | } |
| 133 | |
Owen Lin | e80b476 | 2012-03-13 14:17:01 +0800 | [diff] [blame^] | 134 | private BucketEntry[] loadBucketEntries() { |
| 135 | Uri uri = mBaseUri; |
| 136 | |
| 137 | Log.v("DebugLoadingTime", "start quering media provider"); |
| 138 | Cursor cursor = mApplication.getContentResolver().query( |
| 139 | uri, PROJECTION_BUCKET, BUCKET_GROUP_BY, null, BUCKET_ORDER_BY); |
| 140 | if (cursor == null) { |
| 141 | Log.w(TAG, "cannot open local database: " + uri); |
| 142 | return new BucketEntry[0]; |
| 143 | } |
| 144 | |
Chih-Chung Chang | fbef386 | 2011-09-29 19:07:00 +0800 | [diff] [blame] | 145 | ArrayList<BucketEntry> buffer = new ArrayList<BucketEntry>(); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 146 | int typeBits = 0; |
| 147 | if ((mType & MEDIA_TYPE_IMAGE) != 0) { |
| 148 | typeBits |= (1 << FileColumns.MEDIA_TYPE_IMAGE); |
| 149 | } |
| 150 | if ((mType & MEDIA_TYPE_VIDEO) != 0) { |
| 151 | typeBits |= (1 << FileColumns.MEDIA_TYPE_VIDEO); |
| 152 | } |
| 153 | try { |
| 154 | while (cursor.moveToNext()) { |
| 155 | if ((typeBits & (1 << cursor.getInt(INDEX_MEDIA_TYPE))) != 0) { |
Chih-Chung Chang | fbef386 | 2011-09-29 19:07:00 +0800 | [diff] [blame] | 156 | BucketEntry entry = new BucketEntry( |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 157 | cursor.getInt(INDEX_BUCKET_ID), |
Chih-Chung Chang | fbef386 | 2011-09-29 19:07:00 +0800 | [diff] [blame] | 158 | cursor.getString(INDEX_BUCKET_NAME)); |
| 159 | if (!buffer.contains(entry)) { |
| 160 | buffer.add(entry); |
| 161 | } |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 162 | } |
| 163 | } |
Owen Lin | e80b476 | 2012-03-13 14:17:01 +0800 | [diff] [blame^] | 164 | Log.v("DebugLoadingTime", "got " + buffer.size() + " buckets"); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 165 | } finally { |
| 166 | cursor.close(); |
| 167 | } |
| 168 | return buffer.toArray(new BucketEntry[buffer.size()]); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | private static int findBucket(BucketEntry entries[], int bucketId) { |
| 173 | for (int i = 0, n = entries.length; i < n ; ++i) { |
| 174 | if (entries[i].bucketId == bucketId) return i; |
| 175 | } |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | @SuppressWarnings("unchecked") |
| 180 | protected ArrayList<MediaSet> loadSubMediaSets() { |
| 181 | // Note: it will be faster if we only select media_type and bucket_id. |
| 182 | // need to test the performance if that is worth |
| 183 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 184 | GalleryUtils.assertNotInRenderThread(); |
Owen Lin | e80b476 | 2012-03-13 14:17:01 +0800 | [diff] [blame^] | 185 | BucketEntry[] entries = loadBucketEntries(); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 186 | int offset = 0; |
| 187 | |
Chih-Chung Chang | fbef386 | 2011-09-29 19:07:00 +0800 | [diff] [blame] | 188 | // Move camera and download bucket to the front, while keeping the |
| 189 | // order of others. |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 190 | int index = findBucket(entries, MediaSetUtils.CAMERA_BUCKET_ID); |
| 191 | if (index != -1) { |
Chih-Chung Chang | fbef386 | 2011-09-29 19:07:00 +0800 | [diff] [blame] | 192 | circularShiftRight(entries, offset++, index); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 193 | } |
| 194 | index = findBucket(entries, MediaSetUtils.DOWNLOAD_BUCKET_ID); |
| 195 | if (index != -1) { |
Chih-Chung Chang | fbef386 | 2011-09-29 19:07:00 +0800 | [diff] [blame] | 196 | circularShiftRight(entries, offset++, index); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 197 | } |
| 198 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 199 | ArrayList<MediaSet> albums = new ArrayList<MediaSet>(); |
| 200 | DataManager dataManager = mApplication.getDataManager(); |
| 201 | for (BucketEntry entry : entries) { |
| 202 | albums.add(getLocalAlbum(dataManager, |
| 203 | mType, mPath, entry.bucketId, entry.bucketName)); |
| 204 | } |
| 205 | for (int i = 0, n = albums.size(); i < n; ++i) { |
| 206 | albums.get(i).reload(); |
| 207 | } |
| 208 | return albums; |
| 209 | } |
| 210 | |
| 211 | private MediaSet getLocalAlbum( |
| 212 | DataManager manager, int type, Path parent, int id, String name) { |
| 213 | Path path = parent.getChild(id); |
| 214 | MediaObject object = manager.peekMediaObject(path); |
| 215 | if (object != null) return (MediaSet) object; |
| 216 | switch (type) { |
| 217 | case MEDIA_TYPE_IMAGE: |
| 218 | return new LocalAlbum(path, mApplication, id, true, name); |
| 219 | case MEDIA_TYPE_VIDEO: |
| 220 | return new LocalAlbum(path, mApplication, id, false, name); |
| 221 | case MEDIA_TYPE_ALL: |
| 222 | Comparator<MediaItem> comp = DataManager.sDateTakenComparator; |
| 223 | return new LocalMergeAlbum(path, comp, new MediaSet[] { |
| 224 | getLocalAlbum(manager, MEDIA_TYPE_IMAGE, PATH_IMAGE, id, name), |
Ray Chen | f3f7f56 | 2012-03-06 17:24:28 +0800 | [diff] [blame] | 225 | getLocalAlbum(manager, MEDIA_TYPE_VIDEO, PATH_VIDEO, id, name)}, id); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 226 | } |
| 227 | throw new IllegalArgumentException(String.valueOf(type)); |
| 228 | } |
| 229 | |
| 230 | public static String getBucketName(ContentResolver resolver, int bucketId) { |
| 231 | Uri uri = mBaseUri.buildUpon() |
| 232 | .appendQueryParameter("limit", "1") |
| 233 | .build(); |
| 234 | |
| 235 | Cursor cursor = resolver.query( |
| 236 | uri, PROJECTION_BUCKET, "bucket_id = ?", |
| 237 | new String[]{String.valueOf(bucketId)}, null); |
| 238 | |
| 239 | if (cursor == null) { |
| 240 | Log.w(TAG, "query fail: " + uri); |
| 241 | return ""; |
| 242 | } |
| 243 | try { |
| 244 | return cursor.moveToNext() |
| 245 | ? cursor.getString(INDEX_BUCKET_NAME) |
| 246 | : ""; |
| 247 | } finally { |
| 248 | cursor.close(); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public long reload() { |
| 254 | // "|" is used instead of "||" because we want to clear both flags. |
| 255 | if (mNotifierImage.isDirty() | mNotifierVideo.isDirty()) { |
| 256 | mDataVersion = nextVersionNumber(); |
| 257 | mAlbums = loadSubMediaSets(); |
| 258 | } |
| 259 | return mDataVersion; |
| 260 | } |
| 261 | |
| 262 | // For debug only. Fake there is a ContentObserver.onChange() event. |
| 263 | void fakeChange() { |
| 264 | mNotifierImage.fakeChange(); |
| 265 | mNotifierVideo.fakeChange(); |
| 266 | } |
| 267 | |
| 268 | private static class BucketEntry { |
| 269 | public String bucketName; |
| 270 | public int bucketId; |
| 271 | |
| 272 | public BucketEntry(int id, String name) { |
| 273 | bucketId = id; |
| 274 | bucketName = Utils.ensureNotNull(name); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public int hashCode() { |
| 279 | return bucketId; |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public boolean equals(Object object) { |
| 284 | if (!(object instanceof BucketEntry)) return false; |
| 285 | BucketEntry entry = (BucketEntry) object; |
| 286 | return bucketId == entry.bucketId; |
| 287 | } |
| 288 | } |
Chih-Chung Chang | fbef386 | 2011-09-29 19:07:00 +0800 | [diff] [blame] | 289 | |
| 290 | // Circular shift the array range from a[i] to a[j] (inclusive). That is, |
| 291 | // a[i] -> a[i+1] -> a[i+2] -> ... -> a[j], and a[j] -> a[i] |
| 292 | private static <T> void circularShiftRight(T[] array, int i, int j) { |
| 293 | T temp = array[j]; |
| 294 | for (int k = j; k > i; k--) { |
| 295 | array[k] = array[k - 1]; |
| 296 | } |
| 297 | array[i] = temp; |
| 298 | } |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 299 | } |