Home | History | Annotate | Download | only in data
      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 
     17 package com.android.gallery3d.data;
     18 
     19 import com.android.gallery3d.util.Future;
     20 
     21 import 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)
     26 public class ComboAlbum extends MediaSet implements ContentListener {
     27     private static final String TAG = "ComboAlbum";
     28     private final MediaSet[] mSets;
     29     private final String mName;
     30 
     31     public ComboAlbum(Path path, MediaSet[] mediaSets, String name) {
     32         super(path, nextVersionNumber());
     33         mSets = mediaSets;
     34         for (MediaSet set : mSets) {
     35             set.addContentListener(this);
     36         }
     37         mName = name;
     38     }
     39 
     40     @Override
     41     public ArrayList<MediaItem> getMediaItem(int start, int count) {
     42         ArrayList<MediaItem> items = new ArrayList<MediaItem>();
     43         for (MediaSet set : mSets) {
     44             int size = set.getMediaItemCount();
     45             if (count < 1) break;
     46             if (start < size) {
     47                 int fetchCount = (start + count <= size) ? count : size - start;
     48                 ArrayList<MediaItem> fetchItems = set.getMediaItem(start, fetchCount);
     49                 items.addAll(fetchItems);
     50                 count -= fetchItems.size();
     51                 start = 0;
     52             } else {
     53                 start -= size;
     54             }
     55         }
     56         return items;
     57     }
     58 
     59     @Override
     60     public int getMediaItemCount() {
     61         int count = 0;
     62         for (MediaSet set : mSets) {
     63             count += set.getMediaItemCount();
     64         }
     65         return count;
     66     }
     67 
     68     @Override
     69     public boolean isLeafAlbum() {
     70         return true;
     71     }
     72 
     73     @Override
     74     public String getName() {
     75         return mName;
     76     }
     77 
     78     @Override
     79     public long reload() {
     80         boolean changed = false;
     81         for (int i = 0, n = mSets.length; i < n; ++i) {
     82             long version = mSets[i].reload();
     83             if (version > mDataVersion) changed = true;
     84         }
     85         if (changed) mDataVersion = nextVersionNumber();
     86         return mDataVersion;
     87     }
     88 
     89     public void onContentDirty() {
     90         notifyContentChanged();
     91     }
     92 
     93     @Override
     94     public Future<Integer> requestSync(SyncListener listener) {
     95         return requestSyncOnMultipleSets(mSets, listener);
     96     }
     97 }
     98