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 import android.util.Log; 21 22 import java.io.InputStream; 23 import java.util.Collection; 24 import java.util.ArrayList; 25 26 /** 27 * Picks a random image from the local store. 28 */ 29 public class 30 31 StockSource extends PhotoSource { 32 public static final String ALBUM_ID = "com.android.dreams.phototable.StockSource"; 33 private static final String TAG = "PhotoTable.StockSource"; 34 private static final int[] PHOTOS = { R.drawable.blank_photo }; 35 36 private final ArrayList<ImageData> mImageList; 37 private final ArrayList<AlbumData> mAlbumList; 38 39 private final String mStockPhotoName; 40 private int mNextPosition; 41 StockSource(Context context, SharedPreferences settings)42 public StockSource(Context context, SharedPreferences settings) { 43 super(context, settings, null); 44 mSourceName = TAG; 45 mStockPhotoName = mResources.getString(R.string.stock_photo_album_name, "Default Photos"); 46 mImageList = new ArrayList<ImageData>(PHOTOS.length); 47 mAlbumList = new ArrayList<AlbumData>(1); 48 fillQueue(); 49 } 50 51 @Override findAlbums()52 public Collection<AlbumData> findAlbums() { 53 if (mAlbumList.isEmpty()) { 54 AlbumData data = new AlbumData(); 55 data.id = ALBUM_ID; 56 data.account = mStockPhotoName; 57 data.title = mStockPhotoName; 58 mAlbumList.add(data); 59 } 60 log(TAG, "returning a list of albums: " + mAlbumList.size()); 61 return mAlbumList; 62 } 63 64 @Override findImages(int howMany)65 protected Collection<ImageData> findImages(int howMany) { 66 if (mImageList.isEmpty()) { 67 for (int i = 0; i < PHOTOS.length; i++) { 68 ImageData data = new ImageData(); 69 data.id = Integer.toString(PHOTOS[i]); 70 mImageList.add(data); 71 } 72 } 73 return mImageList; 74 } 75 76 @Override getStream(ImageData data, int longSide)77 protected InputStream getStream(ImageData data, int longSide) { 78 InputStream is = null; 79 try { 80 log(TAG, "opening:" + data.id); 81 is = mResources.openRawResource(Integer.valueOf(data.id)); 82 } catch (Exception ex) { 83 log(TAG, ex.toString()); 84 is = null; 85 } 86 87 return is; 88 } 89 } 90 91