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    private static final String TAG = "ComboAlbumSet";
28    private final MediaSet[] mSets;
29    private final String mName;
30
31    public ComboAlbumSet(Path path, GalleryApp application, MediaSet[] mediaSets) {
32        super(path, nextVersionNumber());
33        mSets = mediaSets;
34        for (MediaSet set : mSets) {
35            set.addContentListener(this);
36        }
37        mName = application.getResources().getString(
38                R.string.set_label_all_albums);
39    }
40
41    @Override
42    public MediaSet getSubMediaSet(int index) {
43        for (MediaSet set : mSets) {
44            int size = set.getSubMediaSetCount();
45            if (index < size) {
46                return set.getSubMediaSet(index);
47            }
48            index -= size;
49        }
50        return null;
51    }
52
53    @Override
54    public int getSubMediaSetCount() {
55        int count = 0;
56        for (MediaSet set : mSets) {
57            count += set.getSubMediaSetCount();
58        }
59        return count;
60    }
61
62    @Override
63    public String getName() {
64        return mName;
65    }
66
67    @Override
68    public boolean isLoading() {
69        for (int i = 0, n = mSets.length; i < n; ++i) {
70            if (mSets[i].isLoading()) return true;
71        }
72        return false;
73    }
74
75    @Override
76    public long reload() {
77        boolean changed = false;
78        for (int i = 0, n = mSets.length; i < n; ++i) {
79            long version = mSets[i].reload();
80            if (version > mDataVersion) changed = true;
81        }
82        if (changed) mDataVersion = nextVersionNumber();
83        return mDataVersion;
84    }
85
86    public void onContentDirty() {
87        notifyContentChanged();
88    }
89
90    @Override
91    public Future<Integer> requestSync(SyncListener listener) {
92        return requestSyncOnMultipleSets(mSets, listener);
93    }
94}
95