1/*
2 * Copyright (C) 2011 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 com.android.gallery3d.util.Future;
20
21import java.util.ArrayList;
22
23// ComboAlbum combines multiple media sets into one. It lists all media items
24// from the input albums.
25// This only handles SubMediaSets, not MediaItems. (That's all we need now)
26public class ComboAlbum extends MediaSet implements ContentListener {
27    @SuppressWarnings("unused")
28    private static final String TAG = "ComboAlbum";
29    private final MediaSet[] mSets;
30    private String mName;
31
32    public ComboAlbum(Path path, MediaSet[] mediaSets, String name) {
33        super(path, nextVersionNumber());
34        mSets = mediaSets;
35        for (MediaSet set : mSets) {
36            set.addContentListener(this);
37        }
38        mName = name;
39    }
40
41    @Override
42    public ArrayList<MediaItem> getMediaItem(int start, int count) {
43        ArrayList<MediaItem> items = new ArrayList<MediaItem>();
44        for (MediaSet set : mSets) {
45            int size = set.getMediaItemCount();
46            if (count < 1) break;
47            if (start < size) {
48                int fetchCount = (start + count <= size) ? count : size - start;
49                ArrayList<MediaItem> fetchItems = set.getMediaItem(start, fetchCount);
50                items.addAll(fetchItems);
51                count -= fetchItems.size();
52                start = 0;
53            } else {
54                start -= size;
55            }
56        }
57        return items;
58    }
59
60    @Override
61    public int getMediaItemCount() {
62        int count = 0;
63        for (MediaSet set : mSets) {
64            count += set.getMediaItemCount();
65        }
66        return count;
67    }
68
69    @Override
70    public boolean isLeafAlbum() {
71        return true;
72    }
73
74    @Override
75    public String getName() {
76        return mName;
77    }
78
79    public void useNameOfChild(int i) {
80        if (i < mSets.length) mName = mSets[i].getName();
81    }
82
83    @Override
84    public long reload() {
85        boolean changed = false;
86        for (int i = 0, n = mSets.length; i < n; ++i) {
87            long version = mSets[i].reload();
88            if (version > mDataVersion) changed = true;
89        }
90        if (changed) mDataVersion = nextVersionNumber();
91        return mDataVersion;
92    }
93
94    @Override
95    public void onContentDirty() {
96        notifyContentChanged();
97    }
98
99    @Override
100    public Future<Integer> requestSync(SyncListener listener) {
101        return requestSyncOnMultipleSets(mSets, listener);
102    }
103}
104