OSDN Git Service

Add api check for FLAG_SECURE.
[android-x86/packages-apps-Camera2.git] / src / com / android / gallery3d / data / LocalAlbumSet.java
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
19 import android.net.Uri;
20 import android.os.Handler;
21 import android.provider.MediaStore.Images;
22 import android.provider.MediaStore.Video;
23
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;
32
33 import java.util.ArrayList;
34 import java.util.Comparator;
35
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");
43
44     private static final String TAG = "LocalAlbumSet";
45
46     private static final Uri[] mWatchUris =
47         {Images.Media.EXTERNAL_CONTENT_URI, Video.Media.EXTERNAL_CONTENT_URI};
48
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;
56
57     private Future<ArrayList<MediaSet>> mLoadTask;
58     private ArrayList<MediaSet> mLoadBuffer;
59
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);
68     }
69
70     private static int getTypeFromPath(Path path) {
71         String name[] = path.split();
72         if (name.length < 2) {
73             throw new IllegalArgumentException(path.toString());
74         }
75         return getTypeFromString(name[1]);
76     }
77
78     @Override
79     public MediaSet getSubMediaSet(int index) {
80         return mAlbums.get(index);
81     }
82
83     @Override
84     public int getSubMediaSetCount() {
85         return mAlbums.size();
86     }
87
88     @Override
89     public String getName() {
90         return mName;
91     }
92
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;
96         }
97         return -1;
98     }
99
100     private class AlbumsLoader implements ThreadPool.Job<ArrayList<MediaSet>> {
101
102         @Override
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);
109
110             if (jc.isCancelled()) return null;
111
112             int offset = 0;
113             // Move camera and download bucket to the front, while keeping the
114             // order of others.
115             int index = findBucket(entries, MediaSetUtils.CAMERA_BUCKET_ID);
116             if (index != -1) {
117                 circularShiftRight(entries, offset++, index);
118             }
119             index = findBucket(entries, MediaSetUtils.DOWNLOAD_BUCKET_ID);
120             if (index != -1) {
121                 circularShiftRight(entries, offset++, index);
122             }
123
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);
129                 albums.add(album);
130             }
131             return albums;
132         }
133     }
134
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;
141             switch (type) {
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);
146                 case MEDIA_TYPE_ALL:
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);
151             }
152             throw new IllegalArgumentException(String.valueOf(type));
153         }
154     }
155
156     @Override
157     public synchronized boolean isLoading() {
158         return mIsLoading;
159     }
160
161     @Override
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();
168             mIsLoading = true;
169             mLoadTask = mApplication.getThreadPool().submit(new AlbumsLoader(), this);
170         }
171         if (mLoadBuffer != null) {
172             mAlbums = mLoadBuffer;
173             mLoadBuffer = null;
174             for (MediaSet album : mAlbums) {
175                 album.reload();
176             }
177             mDataVersion = nextVersionNumber();
178         }
179         return mDataVersion;
180     }
181
182     @Override
183     public synchronized void onFutureDone(Future<ArrayList<MediaSet>> future) {
184         if (mLoadTask != future) return; // ignore, wait for the latest task
185         mLoadBuffer = future.get();
186         mIsLoading = false;
187         if (mLoadBuffer == null) mLoadBuffer = new ArrayList<MediaSet>();
188         mHandler.post(new Runnable() {
189             @Override
190             public void run() {
191                 notifyContentChanged();
192             }
193         });
194     }
195
196     // For debug only. Fake there is a ContentObserver.onChange() event.
197     void fakeChange() {
198         mNotifier.fakeChange();
199     }
200
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) {
204         T temp = array[j];
205         for (int k = j; k > i; k--) {
206             array[k] = array[k - 1];
207         }
208         array[i] = temp;
209     }
210 }