am bcfd4439: better memory/cursor management for story mode.

* commit 'bcfd4439d730a4d783a02596c8ab444796323aad':
  better memory/cursor management for story mode.
diff --git a/src/com/android/dreams/phototable/CursorPhotoSource.java b/src/com/android/dreams/phototable/CursorPhotoSource.java
index cb4ce6b..f010a92 100644
--- a/src/com/android/dreams/phototable/CursorPhotoSource.java
+++ b/src/com/android/dreams/phototable/CursorPhotoSource.java
@@ -39,10 +39,10 @@
 
     @Override
     protected ImageData naturalNext(ImageData current) {
-        if (current.cursor == null) {
+        if (current.cursor == null || current.cursor.isClosed()) {
             openCursor(current);
-            findPosition(current);
         }
+        findPosition(current);
         current.cursor.moveToPosition(current.position);
         current.cursor.moveToNext();
         ImageData data = null;
@@ -56,10 +56,10 @@
 
     @Override
     protected ImageData naturalPrevious(ImageData current) {
-        if (current.cursor == null) {
+        if (current.cursor == null || current.cursor.isClosed()) {
             openCursor(current);
-            findPosition(current);
         }
+        findPosition(current);
         current.cursor.moveToPosition(current.position);
         current.cursor.moveToPrevious();
         ImageData data = null;
@@ -71,6 +71,13 @@
         return data;
     }
 
+    @Override
+    protected void donePaging(ImageData current) {
+        if (current.cursor != null && !current.cursor.isClosed()) {
+            current.cursor.close();
+        }
+    }
+
     protected abstract void openCursor(ImageData data);
     protected abstract void findPosition(ImageData data);
     protected abstract ImageData unpackImageData(Cursor cursor, ImageData data);
diff --git a/src/com/android/dreams/phototable/KeyboardInterpreter.java b/src/com/android/dreams/phototable/KeyboardInterpreter.java
index 0b0d6bb..d7b55f4 100644
--- a/src/com/android/dreams/phototable/KeyboardInterpreter.java
+++ b/src/com/android/dreams/phototable/KeyboardInterpreter.java
@@ -37,7 +37,6 @@
     public boolean onKeyDown(int keyCode, KeyEvent event) {
         final View focus = mTable.getFocus();
         boolean consumed = true;
-        Log.d(TAG, "down: " + keyCode);
         if (mTable.hasSelection()) {
             switch (keyCode) {
             case KeyEvent.KEYCODE_ENTER:
diff --git a/src/com/android/dreams/phototable/PhotoCarousel.java b/src/com/android/dreams/phototable/PhotoCarousel.java
index 9e77d06..23939c9 100644
--- a/src/com/android/dreams/phototable/PhotoCarousel.java
+++ b/src/com/android/dreams/phototable/PhotoCarousel.java
@@ -184,7 +184,6 @@
         Bitmap photo = mBitmapQueue.poll();
         if (photo != null) {
             ImageView destination = getBackface();
-            Bitmap old = mBitmapStore.get(destination);
             int width = photo.getWidth();
             int height = photo.getHeight();
             int orientation = (width > height ? LANDSCAPE : PORTRAIT);
@@ -195,11 +194,8 @@
             destination.setTag(R.id.photo_height, Integer.valueOf(height));
             setScaleType(destination);
 
-            mBitmapStore.put(destination, photo);
-
-            if (old != null) {
-                old.recycle();
-            }
+            Bitmap old = mBitmapStore.put(destination, photo);
+            mPhotoSource.recycle(old);
 
             return true;
         } else {
diff --git a/src/com/android/dreams/phototable/PhotoSource.java b/src/com/android/dreams/phototable/PhotoSource.java
index d9d4ab9..fc4cf7b 100644
--- a/src/com/android/dreams/phototable/PhotoSource.java
+++ b/src/com/android/dreams/phototable/PhotoSource.java
@@ -64,6 +64,9 @@
         ImageData naturalPrevious() {
             return PhotoSource.this.naturalPrevious(this);
         }
+        public void donePaging() {
+            PhotoSource.this.donePaging(this);
+        }
     }
 
     public class AlbumData {
@@ -295,10 +298,25 @@
         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();
 }
diff --git a/src/com/android/dreams/phototable/PhotoSourcePlexor.java b/src/com/android/dreams/phototable/PhotoSourcePlexor.java
index 93fdc9e..3733b02 100644
--- a/src/com/android/dreams/phototable/PhotoSourcePlexor.java
+++ b/src/com/android/dreams/phototable/PhotoSourcePlexor.java
@@ -80,4 +80,9 @@
     protected ImageData naturalPrevious(ImageData current) {
         return current.naturalPrevious();
     }
+
+    @Override
+    protected void donePaging(ImageData current) {
+        current.donePaging();
+    }
 }
diff --git a/src/com/android/dreams/phototable/PhotoTable.java b/src/com/android/dreams/phototable/PhotoTable.java
index 7932bda..6fde1ed 100644
--- a/src/com/android/dreams/phototable/PhotoTable.java
+++ b/src/com/android/dreams/phototable/PhotoTable.java
@@ -161,7 +161,9 @@
 
     public void clearSelection() {
         if (hasSelection()) {
-            dropOnTable(getSelection());
+            dropOnTable(mSelection);
+            mPhotoSource.donePaging(getBitmap(mSelection));
+            mSelection = null;
         }
         for (int slot = 0; slot < mOnDeck.length; slot++) {
             if (mOnDeck[slot] != null) {
@@ -174,7 +176,6 @@
                 mLoadOnDeckTasks[slot] = null;
             }
         }
-        mSelection = null;
     }
 
     public void setSelection(View selected) {
@@ -833,10 +834,7 @@
     private void recycle(View photo) {
         if (photo != null) {
             removeView(photo);
-            Bitmap bitmap = getBitmap(photo);
-            if (bitmap != null) {
-                bitmap.recycle();
-            }
+            mPhotoSource.recycle(getBitmap(photo));
         }
     }
 
diff --git a/src/com/android/dreams/phototable/StockSource.java b/src/com/android/dreams/phototable/StockSource.java
index d7b3500..be2a860 100644
--- a/src/com/android/dreams/phototable/StockSource.java
+++ b/src/com/android/dreams/phototable/StockSource.java
@@ -94,16 +94,22 @@
         return is;
     }
 
+    @Override
     public ImageData naturalNext(ImageData current) {
         int idx = Integer.valueOf(current.id);
         idx = (idx + 1) % PHOTOS.length;
         return mImageCache.get(idx);
     }
 
+    @Override
     public ImageData naturalPrevious(ImageData current) {
         int idx = Integer.valueOf(current.id);
         idx = (PHOTOS.length + idx - 1) % PHOTOS.length;
         return mImageCache.get(idx);
     }
+
+    @Override
+    protected void donePaging(ImageData current) {
+    }
 }