Home | History | Annotate | Download | only in data
      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 
     17 package com.android.gallery3d.data;
     18 
     19 import java.util.ArrayList;
     20 
     21 // FilterTypeSet filters a base MediaSet according to a matching media type.
     22 public class FilterTypeSet extends MediaSet implements ContentListener {
     23     @SuppressWarnings("unused")
     24     private static final String TAG = "FilterTypeSet";
     25 
     26     private final DataManager mDataManager;
     27     private final MediaSet mBaseSet;
     28     private final int mMediaType;
     29     private final ArrayList<Path> mPaths = new ArrayList<Path>();
     30     private final ArrayList<MediaSet> mAlbums = new ArrayList<MediaSet>();
     31 
     32     public FilterTypeSet(Path path, DataManager dataManager, MediaSet baseSet,
     33             int mediaType) {
     34         super(path, INVALID_DATA_VERSION);
     35         mDataManager = dataManager;
     36         mBaseSet = baseSet;
     37         mMediaType = mediaType;
     38         mBaseSet.addContentListener(this);
     39     }
     40 
     41     @Override
     42     public String getName() {
     43         return mBaseSet.getName();
     44     }
     45 
     46     @Override
     47     public MediaSet getSubMediaSet(int index) {
     48         return mAlbums.get(index);
     49     }
     50 
     51     @Override
     52     public int getSubMediaSetCount() {
     53         return mAlbums.size();
     54     }
     55 
     56     @Override
     57     public int getMediaItemCount() {
     58         return mPaths.size();
     59     }
     60 
     61     @Override
     62     public ArrayList<MediaItem> getMediaItem(int start, int count) {
     63         return ClusterAlbum.getMediaItemFromPath(
     64                 mPaths, start, count, mDataManager);
     65     }
     66 
     67     @Override
     68     public long reload() {
     69         if (mBaseSet.reload() > mDataVersion) {
     70             updateData();
     71             mDataVersion = nextVersionNumber();
     72         }
     73         return mDataVersion;
     74     }
     75 
     76     @Override
     77     public void onContentDirty() {
     78         notifyContentChanged();
     79     }
     80 
     81     private void updateData() {
     82         // Albums
     83         mAlbums.clear();
     84         String basePath = "/filter/mediatype/" + mMediaType;
     85 
     86         for (int i = 0, n = mBaseSet.getSubMediaSetCount(); i < n; i++) {
     87             MediaSet set = mBaseSet.getSubMediaSet(i);
     88             String filteredPath = basePath + "/{" + set.getPath().toString() + "}";
     89             MediaSet filteredSet = mDataManager.getMediaSet(filteredPath);
     90             filteredSet.reload();
     91             if (filteredSet.getMediaItemCount() > 0
     92                     || filteredSet.getSubMediaSetCount() > 0) {
     93                 mAlbums.add(filteredSet);
     94             }
     95         }
     96 
     97         // Items
     98         mPaths.clear();
     99         final int total = mBaseSet.getMediaItemCount();
    100         final Path[] buf = new Path[total];
    101 
    102         mBaseSet.enumerateMediaItems(new MediaSet.ItemConsumer() {
    103             @Override
    104             public void consume(int index, MediaItem item) {
    105                 if (item.getMediaType() == mMediaType) {
    106                     if (index < 0 || index >= total) return;
    107                     Path path = item.getPath();
    108                     buf[index] = path;
    109                 }
    110             }
    111         });
    112 
    113         for (int i = 0; i < total; i++) {
    114             if (buf[i] != null) {
    115                 mPaths.add(buf[i]);
    116             }
    117         }
    118     }
    119 
    120     @Override
    121     public int getSupportedOperations() {
    122         return SUPPORT_SHARE | SUPPORT_DELETE;
    123     }
    124 
    125     @Override
    126     public void delete() {
    127         ItemConsumer consumer = new ItemConsumer() {
    128             @Override
    129             public void consume(int index, MediaItem item) {
    130                 if ((item.getSupportedOperations() & SUPPORT_DELETE) != 0) {
    131                     item.delete();
    132                 }
    133             }
    134         };
    135         mDataManager.mapMediaItems(mPaths, consumer, 0);
    136     }
    137 }
    138