DataManager.java revision 2b3ee0ea07246b859a5b75d8a6102a7cce7ec838
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 17package com.android.gallery3d.data; 18 19import android.database.ContentObserver; 20import android.net.Uri; 21import android.os.Handler; 22 23import com.android.gallery3d.app.GalleryApp; 24import com.android.gallery3d.common.Utils; 25import com.android.gallery3d.data.MediaSet.ItemConsumer; 26import com.android.gallery3d.data.MediaSource.PathId; 27import com.android.gallery3d.picasasource.PicasaSource; 28 29import java.util.ArrayList; 30import java.util.Comparator; 31import java.util.HashMap; 32import java.util.LinkedHashMap; 33import java.util.Map.Entry; 34import java.util.WeakHashMap; 35 36// DataManager manages all media sets and media items in the system. 37// 38// Each MediaSet and MediaItem has a unique 64 bits id. The most significant 39// 32 bits represents its parent, and the least significant 32 bits represents 40// the self id. For MediaSet the self id is is globally unique, but for 41// MediaItem it's unique only relative to its parent. 42// 43// To make sure the id is the same when the MediaSet is re-created, a child key 44// is provided to obtainSetId() to make sure the same self id will be used as 45// when the parent and key are the same. A sequence of child keys is called a 46// path. And it's used to identify a specific media set even if the process is 47// killed and re-created, so child keys should be stable identifiers. 48 49public class DataManager { 50 public static final int INCLUDE_IMAGE = 1; 51 public static final int INCLUDE_VIDEO = 2; 52 public static final int INCLUDE_ALL = INCLUDE_IMAGE | INCLUDE_VIDEO; 53 public static final int INCLUDE_LOCAL_ONLY = 4; 54 public static final int INCLUDE_LOCAL_IMAGE_ONLY = 55 INCLUDE_LOCAL_ONLY | INCLUDE_IMAGE; 56 public static final int INCLUDE_LOCAL_VIDEO_ONLY = 57 INCLUDE_LOCAL_ONLY | INCLUDE_VIDEO; 58 public static final int INCLUDE_LOCAL_ALL_ONLY = 59 INCLUDE_LOCAL_ONLY | INCLUDE_IMAGE | INCLUDE_VIDEO; 60 61 // Any one who would like to access data should require this lock 62 // to prevent concurrency issue. 63 public static final Object LOCK = new Object(); 64 65 private static final String TAG = "DataManager"; 66 67 // This is the path for the media set seen by the user at top level. 68 private static final String TOP_SET_PATH = 69 "/combo/{/mtp,/local/all,/picasa/all}"; 70 private static final String TOP_IMAGE_SET_PATH = 71 "/combo/{/mtp,/local/image,/picasa/image}"; 72 private static final String TOP_VIDEO_SET_PATH = 73 "/combo/{/local/video,/picasa/video}"; 74 private static final String TOP_LOCAL_SET_PATH = 75 "/local/all"; 76 private static final String TOP_LOCAL_IMAGE_SET_PATH = 77 "/local/image"; 78 private static final String TOP_LOCAL_VIDEO_SET_PATH = 79 "/local/video"; 80 81 public static final Comparator<MediaItem> sDateTakenComparator = 82 new DateTakenComparator(); 83 84 private static class DateTakenComparator implements Comparator<MediaItem> { 85 public int compare(MediaItem item1, MediaItem item2) { 86 return -Utils.compare(item1.getDateInMs(), item2.getDateInMs()); 87 } 88 } 89 90 private final Handler mDefaultMainHandler; 91 92 private GalleryApp mApplication; 93 private int mActiveCount = 0; 94 95 private HashMap<Uri, NotifyBroker> mNotifierMap = 96 new HashMap<Uri, NotifyBroker>(); 97 98 99 private HashMap<String, MediaSource> mSourceMap = 100 new LinkedHashMap<String, MediaSource>(); 101 102 public DataManager(GalleryApp application) { 103 mApplication = application; 104 mDefaultMainHandler = new Handler(application.getMainLooper()); 105 } 106 107 public synchronized void initializeSourceMap() { 108 if (!mSourceMap.isEmpty()) return; 109 110 // the order matters, the UriSource must come last 111 addSource(new LocalSource(mApplication)); 112 addSource(new PicasaSource(mApplication)); 113 addSource(new MtpSource(mApplication)); 114 addSource(new ComboSource(mApplication)); 115 addSource(new ClusterSource(mApplication)); 116 addSource(new FilterSource(mApplication)); 117 addSource(new UriSource(mApplication)); 118 119 if (mActiveCount > 0) { 120 for (MediaSource source : mSourceMap.values()) { 121 source.resume(); 122 } 123 } 124 } 125 126 public String getTopSetPath(int typeBits) { 127 128 switch (typeBits) { 129 case INCLUDE_IMAGE: return TOP_IMAGE_SET_PATH; 130 case INCLUDE_VIDEO: return TOP_VIDEO_SET_PATH; 131 case INCLUDE_ALL: return TOP_SET_PATH; 132 case INCLUDE_LOCAL_IMAGE_ONLY: return TOP_LOCAL_IMAGE_SET_PATH; 133 case INCLUDE_LOCAL_VIDEO_ONLY: return TOP_LOCAL_VIDEO_SET_PATH; 134 case INCLUDE_LOCAL_ALL_ONLY: return TOP_LOCAL_SET_PATH; 135 default: throw new IllegalArgumentException(); 136 } 137 } 138 139 // open for debug 140 void addSource(MediaSource source) { 141 mSourceMap.put(source.getPrefix(), source); 142 } 143 144 public MediaObject peekMediaObject(Path path) { 145 return path.getObject(); 146 } 147 148 public MediaObject getMediaObject(Path path) { 149 MediaObject obj = path.getObject(); 150 if (obj != null) return obj; 151 152 MediaSource source = mSourceMap.get(path.getPrefix()); 153 if (source == null) { 154 Log.w(TAG, "cannot find media source for path: " + path); 155 return null; 156 } 157 158 try { 159 MediaObject object = source.createMediaObject(path); 160 if (object == null) { 161 Log.w(TAG, "cannot create media object: " + path); 162 } 163 return object; 164 } catch (Throwable t) { 165 Log.w(TAG, "exception in creating media object: " + path, t); 166 return null; 167 } 168 } 169 170 public MediaObject getMediaObject(String s) { 171 return getMediaObject(Path.fromString(s)); 172 } 173 174 public MediaSet getMediaSet(Path path) { 175 return (MediaSet) getMediaObject(path); 176 } 177 178 public MediaSet getMediaSet(String s) { 179 return (MediaSet) getMediaObject(s); 180 } 181 182 public MediaSet[] getMediaSetsFromString(String segment) { 183 String[] seq = Path.splitSequence(segment); 184 int n = seq.length; 185 MediaSet[] sets = new MediaSet[n]; 186 for (int i = 0; i < n; i++) { 187 sets[i] = getMediaSet(seq[i]); 188 } 189 return sets; 190 } 191 192 // Maps a list of Paths to MediaItems, and invoke consumer.consume() 193 // for each MediaItem (may not be in the same order as the input list). 194 // An index number is also passed to consumer.consume() to identify 195 // the original position in the input list of the corresponding Path (plus 196 // startIndex). 197 public void mapMediaItems(ArrayList<Path> list, ItemConsumer consumer, 198 int startIndex) { 199 HashMap<String, ArrayList<PathId>> map = 200 new HashMap<String, ArrayList<PathId>>(); 201 202 // Group the path by the prefix. 203 int n = list.size(); 204 for (int i = 0; i < n; i++) { 205 Path path = list.get(i); 206 String prefix = path.getPrefix(); 207 ArrayList<PathId> group = map.get(prefix); 208 if (group == null) { 209 group = new ArrayList<PathId>(); 210 map.put(prefix, group); 211 } 212 group.add(new PathId(path, i + startIndex)); 213 } 214 215 // For each group, ask the corresponding media source to map it. 216 for (Entry<String, ArrayList<PathId>> entry : map.entrySet()) { 217 String prefix = entry.getKey(); 218 MediaSource source = mSourceMap.get(prefix); 219 source.mapMediaItems(entry.getValue(), consumer); 220 } 221 } 222 223 // The following methods forward the request to the proper object. 224 public int getSupportedOperations(Path path) { 225 return getMediaObject(path).getSupportedOperations(); 226 } 227 228 public void delete(Path path) { 229 getMediaObject(path).delete(); 230 } 231 232 public void rotate(Path path, int degrees) { 233 getMediaObject(path).rotate(degrees); 234 } 235 236 public Uri getContentUri(Path path) { 237 return getMediaObject(path).getContentUri(); 238 } 239 240 public int getMediaType(Path path) { 241 return getMediaObject(path).getMediaType(); 242 } 243 244 public Path findPathByUri(Uri uri) { 245 if (uri == null) return null; 246 for (MediaSource source : mSourceMap.values()) { 247 Path path = source.findPathByUri(uri); 248 if (path != null) return path; 249 } 250 return null; 251 } 252 253 public Path getDefaultSetOf(Path item) { 254 MediaSource source = mSourceMap.get(item.getPrefix()); 255 return source == null ? null : source.getDefaultSetOf(item); 256 } 257 258 // Returns number of bytes used by cached pictures currently downloaded. 259 public long getTotalUsedCacheSize() { 260 long sum = 0; 261 for (MediaSource source : mSourceMap.values()) { 262 sum += source.getTotalUsedCacheSize(); 263 } 264 return sum; 265 } 266 267 // Returns number of bytes used by cached pictures if all pending 268 // downloads and removals are completed. 269 public long getTotalTargetCacheSize() { 270 long sum = 0; 271 for (MediaSource source : mSourceMap.values()) { 272 sum += source.getTotalTargetCacheSize(); 273 } 274 return sum; 275 } 276 277 public void registerChangeNotifier(Uri uri, ChangeNotifier notifier) { 278 NotifyBroker broker = null; 279 synchronized (mNotifierMap) { 280 broker = mNotifierMap.get(uri); 281 if (broker == null) { 282 broker = new NotifyBroker(mDefaultMainHandler); 283 mApplication.getContentResolver() 284 .registerContentObserver(uri, true, broker); 285 mNotifierMap.put(uri, broker); 286 } 287 } 288 broker.registerNotifier(notifier); 289 } 290 291 public void resume() { 292 if (++mActiveCount == 1) { 293 for (MediaSource source : mSourceMap.values()) { 294 source.resume(); 295 } 296 } 297 } 298 299 public void pause() { 300 if (--mActiveCount == 0) { 301 for (MediaSource source : mSourceMap.values()) { 302 source.pause(); 303 } 304 } 305 } 306 307 private static class NotifyBroker extends ContentObserver { 308 private WeakHashMap<ChangeNotifier, Object> mNotifiers = 309 new WeakHashMap<ChangeNotifier, Object>(); 310 311 public NotifyBroker(Handler handler) { 312 super(handler); 313 } 314 315 public synchronized void registerNotifier(ChangeNotifier notifier) { 316 mNotifiers.put(notifier, null); 317 } 318 319 @Override 320 public synchronized void onChange(boolean selfChange) { 321 for(ChangeNotifier notifier : mNotifiers.keySet()) { 322 notifier.onChange(selfChange); 323 } 324 } 325 } 326} 327