2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.data;
19 import android.net.Uri;
20 import android.os.Handler;
21 import android.provider.MediaStore.Images;
22 import android.provider.MediaStore.Video;
24 import com.android.gallery3d.R;
25 import com.android.gallery3d.app.GalleryApp;
26 import com.android.gallery3d.data.BucketHelper.BucketEntry;
27 import com.android.gallery3d.util.Future;
28 import com.android.gallery3d.util.FutureListener;
29 import com.android.gallery3d.util.MediaSetUtils;
30 import com.android.gallery3d.util.ThreadPool;
31 import com.android.gallery3d.util.ThreadPool.JobContext;
33 import java.util.ArrayList;
34 import java.util.Comparator;
36 // LocalAlbumSet lists all image or video albums in the local storage.
37 // The path should be "/local/image", "local/video" or "/local/all"
38 public class LocalAlbumSet extends MediaSet
39 implements FutureListener<ArrayList<MediaSet>> {
40 public static final Path PATH_ALL = Path.fromString("/local/all");
41 public static final Path PATH_IMAGE = Path.fromString("/local/image");
42 public static final Path PATH_VIDEO = Path.fromString("/local/video");
44 private static final String TAG = "LocalAlbumSet";
46 private static final Uri[] mWatchUris =
47 {Images.Media.EXTERNAL_CONTENT_URI, Video.Media.EXTERNAL_CONTENT_URI};
49 private final GalleryApp mApplication;
50 private final int mType;
51 private ArrayList<MediaSet> mAlbums = new ArrayList<MediaSet>();
52 private final ChangeNotifier mNotifier;
53 private final String mName;
54 private final Handler mHandler;
55 private boolean mIsLoading;
57 private Future<ArrayList<MediaSet>> mLoadTask;
58 private ArrayList<MediaSet> mLoadBuffer;
60 public LocalAlbumSet(Path path, GalleryApp application) {
61 super(path, nextVersionNumber());
62 mApplication = application;
63 mHandler = new Handler(application.getMainLooper());
64 mType = getTypeFromPath(path);
65 mNotifier = new ChangeNotifier(this, mWatchUris, application);
66 mName = application.getResources().getString(
67 R.string.set_label_local_albums);
70 private static int getTypeFromPath(Path path) {
71 String name[] = path.split();
72 if (name.length < 2) {
73 throw new IllegalArgumentException(path.toString());
75 return getTypeFromString(name[1]);
79 public MediaSet getSubMediaSet(int index) {
80 return mAlbums.get(index);
84 public int getSubMediaSetCount() {
85 return mAlbums.size();
89 public String getName() {
93 private static int findBucket(BucketEntry entries[], int bucketId) {
94 for (int i = 0, n = entries.length; i < n ; ++i) {
95 if (entries[i].bucketId == bucketId) return i;
100 private class AlbumsLoader implements ThreadPool.Job<ArrayList<MediaSet>> {
103 @SuppressWarnings("unchecked")
104 public ArrayList<MediaSet> run(JobContext jc) {
105 // Note: it will be faster if we only select media_type and bucket_id.
106 // need to test the performance if that is worth
107 BucketEntry[] entries = BucketHelper.loadBucketEntries(
108 jc, mApplication.getContentResolver(), mType);
110 if (jc.isCancelled()) return null;
113 // Move camera and download bucket to the front, while keeping the
115 int index = findBucket(entries, MediaSetUtils.CAMERA_BUCKET_ID);
117 circularShiftRight(entries, offset++, index);
119 index = findBucket(entries, MediaSetUtils.DOWNLOAD_BUCKET_ID);
121 circularShiftRight(entries, offset++, index);
124 ArrayList<MediaSet> albums = new ArrayList<MediaSet>();
125 DataManager dataManager = mApplication.getDataManager();
126 for (BucketEntry entry : entries) {
127 MediaSet album = getLocalAlbum(dataManager,
128 mType, mPath, entry.bucketId, entry.bucketName);
135 private MediaSet getLocalAlbum(
136 DataManager manager, int type, Path parent, int id, String name) {
137 synchronized (DataManager.LOCK) {
138 Path path = parent.getChild(id);
139 MediaObject object = manager.peekMediaObject(path);
140 if (object != null) return (MediaSet) object;
142 case MEDIA_TYPE_IMAGE:
143 return new LocalAlbum(path, mApplication, id, true, name);
144 case MEDIA_TYPE_VIDEO:
145 return new LocalAlbum(path, mApplication, id, false, name);
147 Comparator<MediaItem> comp = DataManager.sDateTakenComparator;
148 return new LocalMergeAlbum(path, comp, new MediaSet[] {
149 getLocalAlbum(manager, MEDIA_TYPE_IMAGE, PATH_IMAGE, id, name),
150 getLocalAlbum(manager, MEDIA_TYPE_VIDEO, PATH_VIDEO, id, name)}, id);
152 throw new IllegalArgumentException(String.valueOf(type));
157 public synchronized boolean isLoading() {
162 // synchronized on this function for
163 // 1. Prevent calling reload() concurrently.
164 // 2. Prevent calling onFutureDone() and reload() concurrently
165 public synchronized long reload() {
166 if (mNotifier.isDirty()) {
167 if (mLoadTask != null) mLoadTask.cancel();
169 mLoadTask = mApplication.getThreadPool().submit(new AlbumsLoader(), this);
171 if (mLoadBuffer != null) {
172 mAlbums = mLoadBuffer;
174 for (MediaSet album : mAlbums) {
177 mDataVersion = nextVersionNumber();
183 public synchronized void onFutureDone(Future<ArrayList<MediaSet>> future) {
184 if (mLoadTask != future) return; // ignore, wait for the latest task
185 mLoadBuffer = future.get();
187 if (mLoadBuffer == null) mLoadBuffer = new ArrayList<MediaSet>();
188 mHandler.post(new Runnable() {
191 notifyContentChanged();
196 // For debug only. Fake there is a ContentObserver.onChange() event.
198 mNotifier.fakeChange();
201 // Circular shift the array range from a[i] to a[j] (inclusive). That is,
202 // a[i] -> a[i+1] -> a[i+2] -> ... -> a[j], and a[j] -> a[i]
203 private static <T> void circularShiftRight(T[] array, int i, int j) {
205 for (int k = j; k > i; k--) {
206 array[k] = array[k - 1];