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