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 
21 import com.android.gallery3d.data.MediaSet.ItemConsumer;
22 
23 import java.util.ArrayList;
24 
25 public abstract class MediaSource {
26     private static final String TAG = "MediaSource";
27     private String mPrefix;
28 
MediaSource(String prefix)29     protected MediaSource(String prefix) {
30         mPrefix = prefix;
31     }
32 
getPrefix()33     public String getPrefix() {
34         return mPrefix;
35     }
36 
findPathByUri(Uri uri, String type)37     public Path findPathByUri(Uri uri, String type) {
38         return null;
39     }
40 
createMediaObject(Path path)41     public abstract MediaObject createMediaObject(Path path);
42 
pause()43     public void pause() {
44     }
45 
resume()46     public void resume() {
47     }
48 
getDefaultSetOf(Path item)49     public Path getDefaultSetOf(Path item) {
50         return null;
51     }
52 
getTotalUsedCacheSize()53     public long getTotalUsedCacheSize() {
54         return 0;
55     }
56 
getTotalTargetCacheSize()57     public long getTotalTargetCacheSize() {
58         return 0;
59     }
60 
61     public static class PathId {
PathId(Path path, int id)62         public PathId(Path path, int id) {
63             this.path = path;
64             this.id = id;
65         }
66         public Path path;
67         public int id;
68     }
69 
70     // Maps a list of Paths (all belong to this MediaSource) to MediaItems,
71     // and invoke consumer.consume() for each MediaItem with the given id.
72     //
73     // This default implementation uses getMediaObject for each Path. Subclasses
74     // may override this and provide more efficient implementation (like
75     // batching the database query).
mapMediaItems(ArrayList<PathId> list, ItemConsumer consumer)76     public void mapMediaItems(ArrayList<PathId> list, ItemConsumer consumer) {
77         int n = list.size();
78         for (int i = 0; i < n; i++) {
79             PathId pid = list.get(i);
80             MediaObject obj;
81             synchronized (DataManager.LOCK) {
82                 obj = pid.path.getObject();
83                 if (obj == null) {
84                     try {
85                         obj = createMediaObject(pid.path);
86                     } catch (Throwable th) {
87                         Log.w(TAG, "cannot create media object: " + pid.path, th);
88                     }
89                 }
90             }
91             if (obj != null) {
92                 consumer.consume(pid.id, (MediaItem) obj);
93             }
94         }
95     }
96 }
97