Home | History | Annotate | Download | only in phototable
      1 /*
      2  * Copyright (C) 2012 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 package com.android.dreams.phototable;
     17 
     18 import android.content.Context;
     19 import android.content.SharedPreferences;
     20 
     21 import java.io.InputStream;
     22 import java.util.ArrayList;
     23 import java.util.Collection;
     24 
     25 /**
     26  * Picks a random image from the local store.
     27  */
     28 public class StockSource extends PhotoSource {
     29     public static final String ALBUM_ID = "com.android.dreams.phototable.StockSource";
     30     private static final String TAG = "PhotoTable.StockSource";
     31     private static final int[] PHOTOS = { R.drawable.blank_photo };
     32 
     33     private final ArrayList<ImageData> mImageCache;
     34     private final ArrayList<AlbumData> mAlbumCache;
     35 
     36     private final ArrayList<ImageData> mImageList;
     37     private final ArrayList<AlbumData> mAlbumList;
     38 
     39     private final String mStockPhotoName;
     40 
     41     public StockSource(Context context, SharedPreferences settings) {
     42         super(context, settings, null);
     43         mSourceName = TAG;
     44         mStockPhotoName = mResources.getString(R.string.stock_photo_album_name, "Default Photos");
     45         mImageCache = new ArrayList<ImageData>(PHOTOS.length);
     46         mAlbumCache = new ArrayList<AlbumData>(1);
     47         mImageList = new ArrayList<ImageData>(PHOTOS.length);
     48         mAlbumList = new ArrayList<AlbumData>(1);
     49 
     50         AlbumData albumData = new AlbumData();
     51         albumData.id = ALBUM_ID;
     52         albumData.account = mStockPhotoName;
     53         albumData.title = mStockPhotoName;
     54         mAlbumCache.add(albumData);
     55 
     56         for (int i = 0; i < PHOTOS.length; i++) {
     57             ImageData imageData = new ImageData();
     58             imageData.id = Integer.toString(i);
     59             mImageCache.add(imageData);
     60         }
     61 
     62         fillQueue();
     63     }
     64 
     65     @Override
     66     public Collection<AlbumData> findAlbums() {
     67         if (mAlbumList.isEmpty()) {
     68             mAlbumList.addAll(mAlbumCache);
     69         }
     70         log(TAG, "returning a list of albums: " + mAlbumList.size());
     71         return mAlbumList;
     72     }
     73 
     74     @Override
     75     protected Collection<ImageData> findImages(int howMany) {
     76         if (mImageList.isEmpty()) {
     77             mImageList.addAll(mImageCache);
     78         }
     79         return mImageList;
     80     }
     81 
     82     @Override
     83     protected InputStream getStream(ImageData data, int longSide) {
     84         InputStream is = null;
     85         try {
     86             log(TAG, "opening:" + data.id);
     87             int idx = Integer.valueOf(data.id);
     88             is = mResources.openRawResource(PHOTOS[idx]);
     89         } catch (Exception ex) {
     90             log(TAG, ex.toString());
     91             is = null;
     92         }
     93 
     94         return is;
     95     }
     96 
     97     @Override
     98     public ImageData naturalNext(ImageData current) {
     99         int idx = Integer.valueOf(current.id);
    100         idx = (idx + 1) % PHOTOS.length;
    101         return mImageCache.get(idx);
    102     }
    103 
    104     @Override
    105     public ImageData naturalPrevious(ImageData current) {
    106         int idx = Integer.valueOf(current.id);
    107         idx = (PHOTOS.length + idx - 1) % PHOTOS.length;
    108         return mImageCache.get(idx);
    109     }
    110 
    111     @Override
    112     protected void donePaging(ImageData current) {
    113     }
    114 }
    115 
    116