Use square thumbnails from MediaProvider and adjust layout.

Change-Id: I8a8127f9cc62804ff96f30c5a139b591010362e5
diff --git a/new3d/src/com/android/gallery3d/app/AlbumPage.java b/new3d/src/com/android/gallery3d/app/AlbumPage.java
index 2b9354b..92b1636 100644
--- a/new3d/src/com/android/gallery3d/app/AlbumPage.java
+++ b/new3d/src/com/android/gallery3d/app/AlbumPage.java
@@ -38,8 +38,6 @@
 
     private static final int CHANGE_BACKGROUND = 1;
     private static final int MARGIN_HUD_SLOTVIEW = 5;
-    private static final int HORIZONTAL_GAP_SLOTS = 5;
-    private static final int VERTICAL_GAP_SLOTS = 5;
     private static final int CACHE_SIZE = 64;
 
     private AdaptiveBackground mBackground;
@@ -126,7 +124,6 @@
         mRootPane.addComponent(mSlotView);
         mHud = new HeadUpDisplay(mContext.getAndroidContext());
         mRootPane.addComponent(mHud);
-        mSlotView.setSlotGaps(HORIZONTAL_GAP_SLOTS, VERTICAL_GAP_SLOTS);
         mSlotView.setSlotTapListener(this);
 
         loadBackgroundBitmap(R.drawable.square,
diff --git a/new3d/src/com/android/gallery3d/data/ImageService.java b/new3d/src/com/android/gallery3d/data/ImageService.java
index 33498ab..5438434 100644
--- a/new3d/src/com/android/gallery3d/data/ImageService.java
+++ b/new3d/src/com/android/gallery3d/data/ImageService.java
@@ -130,29 +130,25 @@
 
         private Bitmap getThumbnail() {
             Bitmap result = null;
+            int kind;
 
             switch (mType) {
                 case MediaItem.TYPE_THUMBNAIL:
-                    if (mState.compareAndSet(STATE_READY, STATE_RUNNING)) {
-                        result =  Images.Thumbnails.getThumbnail(
-                                mResolver, mId, Images.Thumbnails.MINI_KIND, null);
-                        mState.compareAndSet(STATE_RUNNING, STATE_RAN);
-                    }
+                    kind = Images.Thumbnails.MINI_KIND;
                     break;
-                case MediaItem.TYPE_MICROTHUMBNAIL: {
-                    if (mState.compareAndSet(STATE_READY, STATE_RUNNING)) {
-                        result = Images.Thumbnails.getThumbnail(
-                            mResolver, mId, Images.Thumbnails.MINI_KIND, null);
-                        mState.compareAndSet(STATE_RUNNING, STATE_RAN);
-                        if (result != null) {
-                            result = Utils.resize(result, MICRO_TARGET_PIXELS);
-                        }
-                    }
+                case MediaItem.TYPE_MICROTHUMBNAIL:
+                    kind = Images.Thumbnails.MICRO_KIND;
                     break;
-                }
                 default:
                     throw new IllegalArgumentException();
             }
+
+            if (mState.compareAndSet(STATE_READY, STATE_RUNNING)) {
+                result = Images.Thumbnails.getThumbnail(
+                        mResolver, mId, kind, null);
+                mState.compareAndSet(STATE_RUNNING, STATE_RAN);
+            }
+
             return result;
         }
     }
@@ -201,29 +197,26 @@
 
         private Bitmap getThumbnail() {
             Bitmap result = null;
+            int kind;
+
             switch (mType) {
                 case MediaItem.TYPE_FULL_IMAGE:
                 case MediaItem.TYPE_THUMBNAIL:
-                    if (mState.compareAndSet(STATE_READY, STATE_RUNNING)) {
-                        result =  Images.Thumbnails.getThumbnail(
-                                mResolver, mId, Images.Thumbnails.MINI_KIND, null);
-                        mState.compareAndSet(STATE_RUNNING, STATE_RAN);
-                    }
+                    kind = Video.Thumbnails.MINI_KIND;
                     break;
-                case MediaItem.TYPE_MICROTHUMBNAIL: {
-                    if (mState.compareAndSet(STATE_READY, STATE_RUNNING)) {
-                        result = Video.Thumbnails.getThumbnail(
-                                mResolver, mId, Video.Thumbnails.MINI_KIND, null);
-                        mState.compareAndSet(STATE_RUNNING, STATE_RAN);
-                        if (result != null) {
-                            result = Utils.resize(result, MICRO_TARGET_PIXELS);
-                        }
-                    }
+                case MediaItem.TYPE_MICROTHUMBNAIL:
+                    kind = Video.Thumbnails.MICRO_KIND;
                     break;
-                }
                 default:
                     throw new IllegalArgumentException();
             }
+
+            if (mState.compareAndSet(STATE_READY, STATE_RUNNING)) {
+                result = Video.Thumbnails.getThumbnail(
+                        mResolver, mId, kind, null);
+                mState.compareAndSet(STATE_RUNNING, STATE_RAN);
+            }
+
             return result;
         }
     }
diff --git a/new3d/src/com/android/gallery3d/ui/AlbumDataAdapter.java b/new3d/src/com/android/gallery3d/ui/AlbumDataAdapter.java
index 2415740..684f395 100644
--- a/new3d/src/com/android/gallery3d/ui/AlbumDataAdapter.java
+++ b/new3d/src/com/android/gallery3d/ui/AlbumDataAdapter.java
@@ -62,7 +62,7 @@
         mSize = source.getMediaItemCount();
 
         mWaitLoadingTexture = new ColorTexture(Color.GRAY);
-        mWaitLoadingTexture.setSize(64, 48);
+        mWaitLoadingTexture.setSize(1, 1);
 
         mHandler = new SynchronizedHandler(context.getGLRoot()) {
             @Override
@@ -280,23 +280,13 @@
             int width = mContent.getWidth();
             int height = mContent.getHeight();
 
-            float scale = (float) Math.sqrt(
-                    GalleryView.EXPECTED_AREA / (width * height));
-            width = (int) (width * scale + 0.5f);
-            height = (int) (height * scale + 0.5f);
+            float scalex = AlbumView.SLOT_WIDTH / (float) width;
+            float scaley = AlbumView.SLOT_HEIGHT / (float) height;
+            float scale = Math.min(scalex, scaley);
 
-            int widthLimit = GalleryView.LENGTH_LIMIT;
-            int heightLimit = GalleryView.LENGTH_LIMIT;
+            width = (int) Math.floor(width * scale);
+            height = (int) Math.floor(height * scale);
 
-            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, height);
         }
 
diff --git a/new3d/src/com/android/gallery3d/ui/AlbumView.java b/new3d/src/com/android/gallery3d/ui/AlbumView.java
index b2f6c3f..f174571 100644
--- a/new3d/src/com/android/gallery3d/ui/AlbumView.java
+++ b/new3d/src/com/android/gallery3d/ui/AlbumView.java
@@ -24,10 +24,10 @@
 public class AlbumView implements SlotView.Listener {
     private static final String TAG = "AlbumView";
 
-    private static final int SLOT_WIDTH = 162;
-    private static final int SLOT_HEIGHT = 132;
-    private static final int HORIZONTAL_GAP = 5;
-    private static final int VERTICAL_GAP = 5;
+    static final int SLOT_WIDTH = 144;
+    static final int SLOT_HEIGHT = 144;
+    private static final int HORIZONTAL_GAP = 0;
+    private static final int VERTICAL_GAP = 0;
 
     private final SlotView mSlotView;
 
@@ -41,8 +41,8 @@
         mModel = model;
         mModel.setListener(new MyDataModelListener());
         mSlotView.setSlotSize(SLOT_WIDTH, SLOT_HEIGHT);
+        mSlotView.setSlotGaps(HORIZONTAL_GAP, VERTICAL_GAP, true);
         mSlotView.setSlotCount(model.size());
-        mSlotView.setSlotGaps(HORIZONTAL_GAP, VERTICAL_GAP);
     }
 
     private void putSlotContent(int slotIndex, DisplayItem item) {
diff --git a/new3d/src/com/android/gallery3d/ui/GalleryAdapter.java b/new3d/src/com/android/gallery3d/ui/GalleryAdapter.java
index 5531a5e..fe2a739 100644
--- a/new3d/src/com/android/gallery3d/ui/GalleryAdapter.java
+++ b/new3d/src/com/android/gallery3d/ui/GalleryAdapter.java
@@ -76,7 +76,7 @@
         mSize = source.getSubMediaSetCount();
 
         mWaitLoadingTexture = new ColorTexture(Color.GRAY);
-        mWaitLoadingTexture.setSize(64, 48);
+        mWaitLoadingTexture.setSize(1, 1);
 
         mHandler = new SynchronizedHandler(context.getGLRoot()) {
             @Override
@@ -357,23 +357,13 @@
             int width = mContent.getWidth();
             int height = mContent.getHeight();
 
-            float scale = (float) Math.sqrt(
-                    GalleryView.EXPECTED_AREA / (width * height));
-            width = (int) (width * scale + 0.5f);
-            height = (int) (height * scale + 0.5f);
+            float scalex = GalleryView.SLOT_WIDTH / (float) width;
+            float scaley = GalleryView.SLOT_HEIGHT / (float) height;
+            float scale = Math.min(scalex, scaley);
 
-            int widthLimit = GalleryView.LENGTH_LIMIT;
-            int heightLimit = GalleryView.LENGTH_LIMIT;
+            width = (int) Math.floor(width * scale);
+            height = (int) Math.floor(height * scale);
 
-            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, height);
         }
 
diff --git a/new3d/src/com/android/gallery3d/ui/GalleryView.java b/new3d/src/com/android/gallery3d/ui/GalleryView.java
index 9308529..aca27b1 100644
--- a/new3d/src/com/android/gallery3d/ui/GalleryView.java
+++ b/new3d/src/com/android/gallery3d/ui/GalleryView.java
@@ -27,10 +27,10 @@
 public class GalleryView implements SlotView.Listener {
     private static final String TAG = GalleryView.class.getSimpleName();
 
-    static final int LENGTH_LIMIT = 180;
-    static final double EXPECTED_AREA = LENGTH_LIMIT * LENGTH_LIMIT / 2;
-    private static final int SLOT_WIDTH = 220;
-    private static final int SLOT_HEIGHT = 200;
+    static final int SLOT_WIDTH = 144;
+    static final int SLOT_HEIGHT = 144;
+    private static final int HORIZONTAL_GAP = 42;
+    private static final int VERTICAL_GAP = 42;
 
     final SlotView mSlotView;
 
@@ -51,6 +51,7 @@
         mModel = Utils.checkNotNull(model);
         mSlotView = slotView;
         mSlotView.setSlotSize(SLOT_WIDTH, SLOT_HEIGHT);
+        mSlotView.setSlotGaps(HORIZONTAL_GAP, VERTICAL_GAP, false);
         mSlotView.setSlotCount(model.size());
         mModel.setListener(new MyCacheListener());
         updateVisibleRange(
@@ -72,14 +73,18 @@
         // top of the rest.
         for (int i = items.length -1; i > 0; --i) {
             DisplayItem item = items[i];
-            int dx = mRandom.nextInt(11) - 5;
-            int itemX = (i & 0x01) == 0
-                    ? rect.left + dx + item.getWidth() / 2
-                    : rect.right - dx - item.getWidth() / 2;
-            int dy = mRandom.nextInt(11) - 10;
-            int theta = mRandom.nextInt(31) - 15;
+            int dx = 0;
+            int dy = 0;
+            int theta = 0;
+            if (i != 0) {
+                int seed = i;
+                int sign = (seed % 2 == 0) ? 1 : -1;
+                theta = (int) (30.0f * (0.5f - (float) Math.random()));
+                dx = (int) (sign * 12.0f * seed + (0.5f - mRandom.nextFloat()) * 4 * seed);
+                dy = (int) (sign * 4 + ((sign == 1) ? -8.0f : sign * (mRandom.nextFloat()) * 16.0f));
+            }
             Position position = new Position();
-            position.set(itemX, y + dy, 0, theta, 1f);
+            position.set(rect.left + item.getWidth() / 2 + dx, y + dy, 0, theta, 1f);
             mSlotView.putDisplayItem(position, item);
         }
         if (items.length > 0) {
diff --git a/new3d/src/com/android/gallery3d/ui/SlotView.java b/new3d/src/com/android/gallery3d/ui/SlotView.java
index 2b956e0..ace105d 100644
--- a/new3d/src/com/android/gallery3d/ui/SlotView.java
+++ b/new3d/src/com/android/gallery3d/ui/SlotView.java
@@ -29,6 +29,7 @@
 import java.util.LinkedHashMap;
 
 public class SlotView extends GLView {
+    private static final String TAG = "SlotView";
     private static final int MAX_VELOCITY = 2500;
     private static final int INDEX_NONE = -1;
 
@@ -69,8 +70,8 @@
         mLayout.setSlotSize(slotWidth, slotHeight);
     }
 
-    public void setSlotGaps(int horizontalGap, int verticalGap) {
-        mLayout.setSlotGaps(horizontalGap, verticalGap);
+    public void setSlotGaps(int horizontalGap, int verticalGap, boolean center) {
+        mLayout.setSlotGaps(horizontalGap, verticalGap, center);
     }
 
     public void setListener(Listener listener) {
@@ -226,6 +227,8 @@
 
         private int mVerticalGap;
         private int mHorizontalGap;
+        private boolean mCenter;
+        private int mTopMargin;
 
         private int mRowCount;
         private int mContentLength;
@@ -241,9 +244,10 @@
             initLayoutParameters();
         }
 
-        public void setSlotGaps(int horizontalGap, int verticalGap) {
+        public void setSlotGaps(int horizontalGap, int verticalGap, boolean center) {
             mHorizontalGap = horizontalGap;
             mVerticalGap = verticalGap;
+            mCenter = center;
             initLayoutParameters();
         }
 
@@ -252,7 +256,7 @@
             int row = index - col * mRowCount;
 
             int x = col * (mHorizontalGap + mSlotWidth) + mHorizontalGap;
-            int y = row * (mVerticalGap + mSlotHeight) + mVerticalGap;
+            int y = row * (mVerticalGap + mSlotHeight) + mVerticalGap + mTopMargin;
             return new Rect(x, y, x + mSlotWidth, y + mSlotHeight);
         }
 
@@ -264,6 +268,11 @@
             int rowCount = (mHeight - mVerticalGap) / (mVerticalGap + mSlotHeight);
             if (rowCount == 0) rowCount = 1;
             mRowCount = rowCount;
+            if (mCenter) {
+                mTopMargin = (mHeight - rowCount * (mVerticalGap + mSlotHeight)) / 2;
+            } else {
+                mTopMargin = 0;
+            }
             mContentLength = ((mSlotCount + rowCount - 1) / rowCount)
                     * (mHorizontalGap + mSlotWidth) + mHorizontalGap - mWidth;
             if (mContentLength < 0) mContentLength = 0;
@@ -316,7 +325,7 @@
             }
 
             int rowHeight = mVerticalGap + mSlotHeight;
-            float absoluteY = y;
+            float absoluteY = y - mTopMargin;
             int rowIdx = (int) (absoluteY + 0.5) / rowHeight;
             if (((absoluteY - rowHeight * rowIdx) < mVerticalGap)
                 || rowIdx >= mRowCount) {