summaryrefslogtreecommitdiffstats
path: root/src/com/android/dreams/phototable/PhotoSource.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/android/dreams/phototable/PhotoSource.java')
-rw-r--r--src/com/android/dreams/phototable/PhotoSource.java78
1 files changed, 73 insertions, 5 deletions
diff --git a/src/com/android/dreams/phototable/PhotoSource.java b/src/com/android/dreams/phototable/PhotoSource.java
index 670bd02..fc4cf7b 100644
--- a/src/com/android/dreams/phototable/PhotoSource.java
+++ b/src/com/android/dreams/phototable/PhotoSource.java
@@ -23,16 +23,15 @@ import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
-import android.net.Uri;
-import android.provider.MediaStore;
import android.util.Log;
+import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
-import java.io.InputStream;
import java.io.IOException;
-import java.io.BufferedInputStream;
+import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
import java.util.LinkedList;
import java.util.Random;
@@ -52,9 +51,22 @@ public abstract class PhotoSource {
public String url;
public int orientation;
+ protected String albumId;
+ protected Cursor cursor;
+ protected int position;
+
InputStream getStream(int longSide) {
return PhotoSource.this.getStream(this, longSide);
}
+ ImageData naturalNext() {
+ return PhotoSource.this.naturalNext(this);
+ }
+ ImageData naturalPrevious() {
+ return PhotoSource.this.naturalPrevious(this);
+ }
+ public void donePaging() {
+ PhotoSource.this.donePaging(this);
+ }
}
public class AlbumData {
@@ -76,6 +88,7 @@ public abstract class PhotoSource {
private final float mMaxCropRatio;
private final int mBadImageSkipLimit;
private final PhotoSource mFallbackSource;
+ private final HashMap<Bitmap, ImageData> mImageMap;
protected final Context mContext;
protected final Resources mResources;
@@ -99,6 +112,7 @@ public abstract class PhotoSource {
mMaxQueueSize = mResources.getInteger(R.integer.image_queue_size);
mMaxCropRatio = mResources.getInteger(R.integer.max_crop_ratio) / 1000000f;
mBadImageSkipLimit = mResources.getInteger(R.integer.bad_image_skip_limit);
+ mImageMap = new HashMap<Bitmap, ImageData>();
mRNG = new Random();
mFallbackSource = fallbackSource;
}
@@ -121,11 +135,11 @@ public abstract class PhotoSource {
if (mImageQueue.isEmpty()) {
fillQueue();
}
-
imageData = mImageQueue.poll();
}
if (imageData != null) {
image = load(imageData, options, longSide, shortSide);
+ mImageMap.put(image, imageData);
imageData = null;
}
@@ -248,7 +262,61 @@ public abstract class PhotoSource {
}
}
+ protected int pickRandomStart(int total, int max) {
+ if (max >= total) {
+ return -1;
+ } else {
+ return (mRNG.nextInt() % (total - max)) - 1;
+ }
+ }
+
+ public Bitmap naturalNext(Bitmap current, BitmapFactory.Options options,
+ int longSide, int shortSide) {
+ Bitmap image = null;
+ ImageData data = mImageMap.get(current);
+ if (data != null) {
+ ImageData next = data.naturalNext();
+ if (next != null) {
+ image = load(next, options, longSide, shortSide);
+ mImageMap.put(image, next);
+ }
+ }
+ return image;
+ }
+
+ public Bitmap naturalPrevious(Bitmap current, BitmapFactory.Options options,
+ int longSide, int shortSide) {
+ Bitmap image = null;
+ ImageData data = mImageMap.get(current);
+ if (current != null) {
+ ImageData prev = data.naturalPrevious();
+ if (prev != null) {
+ image = load(prev, options, longSide, shortSide);
+ mImageMap.put(image, prev);
+ }
+ }
+ return image;
+ }
+
+ public void donePaging(Bitmap current) {
+ ImageData data = mImageMap.get(current);
+ if (data != null) {
+ data.donePaging();
+ }
+ }
+
+ public void recycle(Bitmap trash) {
+ if (trash != null) {
+ mImageMap.remove(trash);
+ trash.recycle();
+ }
+ }
+
protected abstract InputStream getStream(ImageData data, int longSide);
protected abstract Collection<ImageData> findImages(int howMany);
+ protected abstract ImageData naturalNext(ImageData current);
+ protected abstract ImageData naturalPrevious(ImageData current);
+ protected abstract void donePaging(ImageData current);
+
public abstract Collection<AlbumData> findAlbums();
}