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