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.app.GalleryApp;
     20 import com.android.gallery3d.util.Future;
     21 
     22 import java.util.ArrayList;
     23 
     24 // ComboAlbum combines multiple media sets into one. It lists all media items
     25 // from the input albums.
     26 // This only handles SubMediaSets, not MediaItems. (That's all we need now)
     27 public class ComboAlbum extends MediaSet implements ContentListener {
     28     private static final String TAG = "ComboAlbum";
     29     private final MediaSet[] mSets;
     30     private final 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     @Override
     80     public long reload() {
     81         boolean changed = false;
     82         for (int i = 0, n = mSets.length; i < n; ++i) {
     83             long version = mSets[i].reload();
     84             if (version > mDataVersion) changed = true;
     85         }
     86         if (changed) mDataVersion = nextVersionNumber();
     87         return mDataVersion;
     88     }
     89 
     90     public void onContentDirty() {
     91         notifyContentChanged();
     92     }
     93 
     94     @Override
     95     public Future<Integer> requestSync(SyncListener listener) {
     96         return requestSyncOnMultipleSets(mSets, listener);
     97     }
     98 }
     99