Integrate MediaItem/MediaSets with SlotView.Model.

Change-Id: If575448e1bbde01e7e6b924dd25ed9dfef61083e
diff --git a/new3d/src/com/android/gallery3d/app/Gallery.java b/new3d/src/com/android/gallery3d/app/Gallery.java
index 07d454d..7870d69 100644
--- a/new3d/src/com/android/gallery3d/app/Gallery.java
+++ b/new3d/src/com/android/gallery3d/app/Gallery.java
@@ -22,9 +22,12 @@
 import android.util.Log;
 
 import com.android.gallery3d.R;
+import com.android.gallery3d.data.LocalMediaSet;
+import com.android.gallery3d.data.MediaDbAccessor;
+import com.android.gallery3d.data.MediaSet;
 import com.android.gallery3d.ui.GLRootView;
 import com.android.gallery3d.ui.SlotView;
-import com.android.gallery3d.ui.SlotViewMockData;
+import com.android.gallery3d.ui.MediaSetSlotAdapter;
 
 public final class Gallery extends Activity {
     public static final String REVIEW_ACTION = "com.android.gallery3d.app.REVIEW";
@@ -41,7 +44,9 @@
 
         mSlotView = new SlotView(this);
         mGLRootView.setContentPane(mSlotView);
-        mSlotView.setModel(new SlotViewMockData(this));
+        MediaSet rootSet = MediaDbAccessor.getMediaSets(this);
+        ((LocalMediaSet) rootSet).printOut();
+        mSlotView.setModel(new MediaSetSlotAdapter(this, rootSet));
     }
 
     @Override
diff --git a/new3d/src/com/android/gallery3d/ui/ImageTexture.java b/new3d/src/com/android/gallery3d/ui/ImageTexture.java
new file mode 100644
index 0000000..1a92040
--- /dev/null
+++ b/new3d/src/com/android/gallery3d/ui/ImageTexture.java
@@ -0,0 +1,22 @@
+package com.android.gallery3d.ui;
+
+import android.graphics.Bitmap;
+
+public class ImageTexture extends BitmapTexture {
+    protected Bitmap mBitmap;
+
+    public ImageTexture(Bitmap bitmap) {
+        mBitmap = bitmap;
+        setSize(mBitmap.getWidth(), mBitmap.getHeight());
+    }
+
+    @Override
+    protected void freeBitmap(Bitmap bitmap) {
+        // Do nothing.
+    }
+
+    @Override
+    protected Bitmap getBitmap() {
+        return mBitmap;
+    }
+}
diff --git a/new3d/src/com/android/gallery3d/ui/MediaSetSlotAdapter.java b/new3d/src/com/android/gallery3d/ui/MediaSetSlotAdapter.java
new file mode 100644
index 0000000..9844582
--- /dev/null
+++ b/new3d/src/com/android/gallery3d/ui/MediaSetSlotAdapter.java
@@ -0,0 +1,128 @@
+package com.android.gallery3d.ui;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.Rect;
+
+import com.android.gallery3d.R;
+import com.android.gallery3d.data.MediaItem;
+import com.android.gallery3d.data.MediaSet;
+
+import java.util.Random;
+
+public class MediaSetSlotAdapter implements SlotView.Model {
+    private static final int LENGTH_LIMIT = 180;
+    private static final double EXPECTED_AREA = LENGTH_LIMIT * LENGTH_LIMIT / 2;
+    private static final int SLOT_WIDTH = 220;
+    private static final int SLOT_HEIGHT = 200;
+    private static final int MARGIN_TO_SLOTSIDE = 10;
+
+    private final NinePatchTexture mFrame;
+
+    private final Random mRandom = new Random();
+
+    private MediaSet mRootSet;
+    private Context mContext;
+
+
+    public MediaSetSlotAdapter(Context context, MediaSet rootSet) {
+        mContext = context;
+        mRootSet = rootSet;
+        mFrame = new NinePatchTexture(context, R.drawable.stack_frame);
+        Random random = mRandom;
+    }
+
+    public void freeSlot(int index) {
+    }
+
+    public void putSlot(int slotIndex, int x, int y, DisplayItemPanel panel) {
+        MediaSet set = mRootSet.getSubMediaSet(slotIndex);
+        MediaItem[] items = set.getCoverMediaItems();
+        Random random = mRandom;
+        int left = x + MARGIN_TO_SLOTSIDE;
+        int right = x + getSlotWidth() - MARGIN_TO_SLOTSIDE;
+
+        x += getSlotWidth() / 2;
+        y += getSlotHeight() / 2;
+
+        // Put the cover items in reverse order, so that the first item is on
+        // top of the rest.
+        for (int i = items.length -1; i > 0; --i) {
+            Bitmap bitmap = items[i].getImage(mContext.getContentResolver(),
+                    MediaItem.TYPE_THUMBNAIL);
+            DisplayItem displayItem = new MyDisplayItem(
+                    new ImageTexture(bitmap), mFrame);
+            int dx = random.nextInt(11) - 5;
+            int itemX = (i & 0x01) == 0
+                    ? left + dx + displayItem.getWidth() / 2
+                    : right + dx - displayItem.getWidth() / 2;
+            int dy = random.nextInt(11) - 10;
+            int theta = random.nextInt(31) - 15;
+            panel.putDisplayItem(displayItem, itemX, y + dy, theta);
+        }
+        Bitmap bitmap = items[0].getImage(mContext.getContentResolver(),
+                MediaItem.TYPE_THUMBNAIL);
+        DisplayItem displayItem = new MyDisplayItem(
+                new ImageTexture(bitmap), mFrame);
+        panel.putDisplayItem(displayItem, x, y, 0);
+    }
+
+    public int getSlotHeight() {
+        return SLOT_HEIGHT;
+    }
+
+    public int getSlotWidth() {
+        return SLOT_WIDTH;
+    }
+
+    public int size() {
+        return mRootSet.getSubMediaSetCount();
+    }
+
+    private static class MyDisplayItem extends DisplayItem {
+
+        private final BasicTexture mContent;
+        private final NinePatchTexture mFrame;
+
+        public MyDisplayItem(BasicTexture content, NinePatchTexture frame) {
+            mContent = content;
+            mFrame = frame;
+
+            Rect p = frame.getPaddings();
+
+            int width = mContent.getWidth();
+            int height = mContent.getHeight();
+
+            float scale = (float) Math.sqrt(EXPECTED_AREA / (width * height));
+            width = (int) (width * scale + 0.5f);
+            height = (int) (height * scale + 0.5f);
+
+            int widthLimit = LENGTH_LIMIT - p.left - p.right;
+            int heightLimit = LENGTH_LIMIT - p.top - p.bottom;
+
+            if (width > widthLimit || height > heightLimit) {
+                if (width * heightLimit > height * widthLimit) {
+                    height = height * widthLimit / width;
+                    width = widthLimit;
+                } else {
+                    width = width * heightLimit / height;
+                    height = heightLimit;
+                }
+            }
+            setSize(width + p.left + p.right, height + p.top + p.bottom);
+        }
+
+        @Override
+        public void render(GLRootView root) {
+            int x = -mWidth / 2;
+            int y = -mHeight / 2;
+
+            Rect p = mFrame.getPaddings();
+            mContent.draw(root, x + p.left, y + p.top,
+                    mWidth - p.left - p.right, mHeight - p.top - p.bottom);
+            mFrame.draw(root, x, y, mWidth, mHeight);
+        }
+    }
+
+}
+
diff --git a/new3d/src/com/android/gallery3d/ui/SlotView.java b/new3d/src/com/android/gallery3d/ui/SlotView.java
index 0137531..a4b6ada 100644
--- a/new3d/src/com/android/gallery3d/ui/SlotView.java
+++ b/new3d/src/com/android/gallery3d/ui/SlotView.java
@@ -6,6 +6,7 @@
 import android.view.animation.DecelerateInterpolator;
 
 import javax.microedition.khronos.opengles.GL11;
+import android.util.Log;
 
 public class SlotView extends GLView {