Home | History | Annotate | Download | only in gadget
      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.gadget;
     18 
     19 import android.graphics.Bitmap;
     20 import android.net.Uri;
     21 import android.os.Binder;
     22 
     23 import com.android.gallery3d.common.Utils;
     24 import com.android.gallery3d.data.ContentListener;
     25 import com.android.gallery3d.data.MediaItem;
     26 import com.android.gallery3d.data.MediaObject;
     27 import com.android.gallery3d.data.MediaSet;
     28 
     29 import java.util.ArrayList;
     30 import java.util.Arrays;
     31 
     32 public class MediaSetSource implements WidgetSource, ContentListener {
     33     private static final int CACHE_SIZE = 32;
     34 
     35     private static final String TAG = "MediaSetSource";
     36 
     37     private MediaSet mSource;
     38     private MediaItem mCache[] = new MediaItem[CACHE_SIZE];
     39     private int mCacheStart;
     40     private int mCacheEnd;
     41     private long mSourceVersion = MediaObject.INVALID_DATA_VERSION;
     42 
     43     private ContentListener mContentListener;
     44 
     45     public MediaSetSource(MediaSet source) {
     46         mSource = Utils.checkNotNull(source);
     47         mSource.addContentListener(this);
     48     }
     49 
     50     @Override
     51     public void close() {
     52         mSource.removeContentListener(this);
     53     }
     54 
     55     private void ensureCacheRange(int index) {
     56         if (index >= mCacheStart && index < mCacheEnd) return;
     57 
     58         long token = Binder.clearCallingIdentity();
     59         try {
     60             mCacheStart = index;
     61             ArrayList<MediaItem> items = mSource.getMediaItem(mCacheStart, CACHE_SIZE);
     62             mCacheEnd = mCacheStart + items.size();
     63             items.toArray(mCache);
     64         } finally {
     65             Binder.restoreCallingIdentity(token);
     66         }
     67     }
     68 
     69     @Override
     70     public synchronized Uri getContentUri(int index) {
     71         ensureCacheRange(index);
     72         if (index < mCacheStart || index >= mCacheEnd) return null;
     73         return mCache[index - mCacheStart].getContentUri();
     74     }
     75 
     76     @Override
     77     public synchronized Bitmap getImage(int index) {
     78         ensureCacheRange(index);
     79         if (index < mCacheStart || index >= mCacheEnd) return null;
     80         return WidgetUtils.createWidgetBitmap(mCache[index - mCacheStart]);
     81     }
     82 
     83     @Override
     84     public void reload() {
     85         long version = mSource.reload();
     86         if (mSourceVersion != version) {
     87             mSourceVersion = version;
     88             mCacheStart = 0;
     89             mCacheEnd = 0;
     90             Arrays.fill(mCache, null);
     91         }
     92     }
     93 
     94     @Override
     95     public void setContentListener(ContentListener listener) {
     96         mContentListener = listener;
     97     }
     98 
     99     @Override
    100     public int size() {
    101         long token = Binder.clearCallingIdentity();
    102         try {
    103             return mSource.getMediaItemCount();
    104         } finally {
    105             Binder.restoreCallingIdentity(token);
    106         }
    107     }
    108 
    109     @Override
    110     public void onContentDirty() {
    111         if (mContentListener != null) mContentListener.onContentDirty();
    112     }
    113 }
    114