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 java.util.ArrayList;
20 
21 public class ClusterAlbum extends MediaSet implements ContentListener {
22     @SuppressWarnings("unused")
23     private static final String TAG = "ClusterAlbum";
24     private ArrayList<Path> mPaths = new ArrayList<Path>();
25     private String mName = "";
26     private DataManager mDataManager;
27     private MediaSet mClusterAlbumSet;
28     private MediaItem mCover;
29 
ClusterAlbum(Path path, DataManager dataManager, MediaSet clusterAlbumSet)30     public ClusterAlbum(Path path, DataManager dataManager,
31             MediaSet clusterAlbumSet) {
32         super(path, nextVersionNumber());
33         mDataManager = dataManager;
34         mClusterAlbumSet = clusterAlbumSet;
35         mClusterAlbumSet.addContentListener(this);
36     }
37 
setCoverMediaItem(MediaItem cover)38     public void setCoverMediaItem(MediaItem cover) {
39         mCover = cover;
40     }
41 
42     @Override
getCoverMediaItem()43     public MediaItem getCoverMediaItem() {
44         return mCover != null ? mCover : super.getCoverMediaItem();
45     }
46 
setMediaItems(ArrayList<Path> paths)47     void setMediaItems(ArrayList<Path> paths) {
48         mPaths = paths;
49     }
50 
getMediaItems()51     ArrayList<Path> getMediaItems() {
52         return mPaths;
53     }
54 
setName(String name)55     public void setName(String name) {
56         mName = name;
57     }
58 
59     @Override
getName()60     public String getName() {
61         return mName;
62     }
63 
64     @Override
getMediaItemCount()65     public int getMediaItemCount() {
66         return mPaths.size();
67     }
68 
69     @Override
getMediaItem(int start, int count)70     public ArrayList<MediaItem> getMediaItem(int start, int count) {
71         return getMediaItemFromPath(mPaths, start, count, mDataManager);
72     }
73 
getMediaItemFromPath( ArrayList<Path> paths, int start, int count, DataManager dataManager)74     public static ArrayList<MediaItem> getMediaItemFromPath(
75             ArrayList<Path> paths, int start, int count,
76             DataManager dataManager) {
77         if (start >= paths.size()) {
78             return new ArrayList<MediaItem>();
79         }
80         int end = Math.min(start + count, paths.size());
81         ArrayList<Path> subset = new ArrayList<Path>(paths.subList(start, end));
82         final MediaItem[] buf = new MediaItem[end - start];
83         ItemConsumer consumer = new ItemConsumer() {
84             @Override
85             public void consume(int index, MediaItem item) {
86                 buf[index] = item;
87             }
88         };
89         dataManager.mapMediaItems(subset, consumer, 0);
90         ArrayList<MediaItem> result = new ArrayList<MediaItem>(end - start);
91         for (int i = 0; i < buf.length; i++) {
92             result.add(buf[i]);
93         }
94         return result;
95     }
96 
97     @Override
enumerateMediaItems(ItemConsumer consumer, int startIndex)98     protected int enumerateMediaItems(ItemConsumer consumer, int startIndex) {
99         mDataManager.mapMediaItems(mPaths, consumer, startIndex);
100         return mPaths.size();
101     }
102 
103     @Override
getTotalMediaItemCount()104     public int getTotalMediaItemCount() {
105         return mPaths.size();
106     }
107 
108     @Override
reload()109     public long reload() {
110         if (mClusterAlbumSet.reload() > mDataVersion) {
111             mDataVersion = nextVersionNumber();
112         }
113         return mDataVersion;
114     }
115 
116     @Override
onContentDirty()117     public void onContentDirty() {
118         notifyContentChanged();
119     }
120 
121     @Override
getSupportedOperations()122     public int getSupportedOperations() {
123         return SUPPORT_SHARE | SUPPORT_DELETE | SUPPORT_INFO;
124     }
125 
126     @Override
delete()127     public void delete() {
128         ItemConsumer consumer = new ItemConsumer() {
129             @Override
130             public void consume(int index, MediaItem item) {
131                 if ((item.getSupportedOperations() & SUPPORT_DELETE) != 0) {
132                     item.delete();
133                 }
134             }
135         };
136         mDataManager.mapMediaItems(mPaths, consumer, 0);
137     }
138 
139     @Override
isLeafAlbum()140     public boolean isLeafAlbum() {
141         return true;
142     }
143 }
144