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 com.android.gallery3d.R;
20import com.android.gallery3d.app.GalleryApp;
21import com.android.gallery3d.util.Future;
22
23// ComboAlbumSet combines multiple media sets into one. It lists all sub
24// media sets from the input album sets.
25// This only handles SubMediaSets, not MediaItems. (That's all we need now)
26public class ComboAlbumSet extends MediaSet implements ContentListener {
27    @SuppressWarnings("unused")
28    private static final String TAG = "ComboAlbumSet";
29    private final MediaSet[] mSets;
30    private final String mName;
31
32    public ComboAlbumSet(Path path, GalleryApp application, MediaSet[] mediaSets) {
33        super(path, nextVersionNumber());
34        mSets = mediaSets;
35        for (MediaSet set : mSets) {
36            set.addContentListener(this);
37        }
38        mName = application.getResources().getString(
39                R.string.set_label_all_albums);
40    }
41
42    @Override
43    public MediaSet getSubMediaSet(int index) {
44        for (MediaSet set : mSets) {
45            int size = set.getSubMediaSetCount();
46            if (index < size) {
47                return set.getSubMediaSet(index);
48            }
49            index -= size;
50        }
51        return null;
52    }
53
54    @Override
55    public int getSubMediaSetCount() {
56        int count = 0;
57        for (MediaSet set : mSets) {
58            count += set.getSubMediaSetCount();
59        }
60        return count;
61    }
62
63    @Override
64    public String getName() {
65        return mName;
66    }
67
68    @Override
69    public boolean isLoading() {
70        for (int i = 0, n = mSets.length; i < n; ++i) {
71            if (mSets[i].isLoading()) return true;
72        }
73        return false;
74    }
75
76    @Override
77    public long reload() {
78        boolean changed = false;
79        for (int i = 0, n = mSets.length; i < n; ++i) {
80            long version = mSets[i].reload();
81            if (version > mDataVersion) changed = true;
82        }
83        if (changed) mDataVersion = nextVersionNumber();
84        return mDataVersion;
85    }
86
87    @Override
88    public void onContentDirty() {
89        notifyContentChanged();
90    }
91
92    @Override
93    public Future<Integer> requestSync(SyncListener listener) {
94        return requestSyncOnMultipleSets(mSets, listener);
95    }
96}
97