Stop using a separate ImageView for default wallpaper

Fixes janky transition when going from default
wallpaper to another wallpaper

Bug: 11278179

Change-Id: I738d1ae7bbc5e2d139a084e721b53fed9008367f
diff --git a/res/layout/wallpaper_picker.xml b/res/layout/wallpaper_picker.xml
index 620ce1f..c36493d 100644
--- a/res/layout/wallpaper_picker.xml
+++ b/res/layout/wallpaper_picker.xml
@@ -27,11 +27,6 @@
         android:id="@+id/cropView"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
-    <ImageView
-        android:id="@+id/defaultWallpaperView"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:visibility="invisible" />
     <ProgressBar
         android:id="@+id/loading"
         style="@android:style/Widget.Holo.ProgressBar.Large"
diff --git a/src/com/android/launcher3/DrawableTileSource.java b/src/com/android/launcher3/DrawableTileSource.java
new file mode 100644
index 0000000..c1f2eff
--- /dev/null
+++ b/src/com/android/launcher3/DrawableTileSource.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.launcher3;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Rect;
+import android.graphics.drawable.Drawable;
+
+import com.android.gallery3d.glrenderer.BasicTexture;
+import com.android.gallery3d.glrenderer.BitmapTexture;
+import com.android.photos.views.TiledImageRenderer;
+
+public class DrawableTileSource implements TiledImageRenderer.TileSource {
+    private static final int GL_SIZE_LIMIT = 2048;
+    // This must be no larger than half the size of the GL_SIZE_LIMIT
+    // due to decodePreview being allowed to be up to 2x the size of the target
+    public static final int MAX_PREVIEW_SIZE = GL_SIZE_LIMIT / 2;
+
+    private int mTileSize;
+    private int mPreviewSize;
+    private Drawable mDrawable;
+    private BitmapTexture mPreview;
+
+    public DrawableTileSource(Context context, Drawable d, int previewSize) {
+        mTileSize = TiledImageRenderer.suggestedTileSize(context);
+        mDrawable = d;
+        mPreviewSize = Math.min(previewSize, MAX_PREVIEW_SIZE);
+    }
+
+    @Override
+    public int getTileSize() {
+        return mTileSize;
+    }
+
+    @Override
+    public int getImageWidth() {
+        return mDrawable.getIntrinsicWidth();
+    }
+
+    @Override
+    public int getImageHeight() {
+        return mDrawable.getIntrinsicHeight();
+    }
+
+    @Override
+    public int getRotation() {
+        return 0;
+    }
+
+    @Override
+    public BasicTexture getPreview() {
+        if (mPreviewSize == 0) {
+            return null;
+        }
+        if (mPreview == null){
+            float width = getImageWidth();
+            float height = getImageHeight();
+            while (width > MAX_PREVIEW_SIZE || height > MAX_PREVIEW_SIZE) {
+                width /= 2;
+                height /= 2;
+            }
+            Bitmap b = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);
+            Canvas c = new Canvas(b);
+            mDrawable.setBounds(new Rect(0, 0, (int) width, (int) height));
+            mDrawable.draw(c);
+            c.setBitmap(null);
+            mPreview = new BitmapTexture(b);
+        }
+        return mPreview;
+    }
+
+    @Override
+    public Bitmap getTile(int level, int x, int y, Bitmap bitmap) {
+        int tileSize = getTileSize();
+        if (bitmap == null) {
+            bitmap = Bitmap.createBitmap(tileSize, tileSize, Bitmap.Config.ARGB_8888);
+        }
+        Canvas c = new Canvas(bitmap);
+        Rect bounds = new Rect(0, 0, getImageWidth(), getImageHeight());
+        bounds.offset(-x, -y);
+        mDrawable.setBounds(bounds);
+        mDrawable.draw(c);
+        c.setBitmap(null);
+        return bitmap;
+    }
+}
diff --git a/src/com/android/launcher3/WallpaperPickerActivity.java b/src/com/android/launcher3/WallpaperPickerActivity.java
index 9c6ee6e..c58d660 100644
--- a/src/com/android/launcher3/WallpaperPickerActivity.java
+++ b/src/com/android/launcher3/WallpaperPickerActivity.java
@@ -90,7 +90,6 @@
 
     private LinearLayout mWallpapersView;
     private View mWallpaperStrip;
-    private ImageView mDefaultWallpaperView;
 
     private ActionMode.Callback mActionModeCallback;
     private ActionMode mActionMode;
@@ -134,8 +133,8 @@
         }
         @Override
         public void onClick(WallpaperPickerActivity a) {
-            a.setCropViewTileSource(
-                    new BitmapRegionTileSource.UriBitmapSource(a, mUri, 1024), true, false);
+            a.setCropViewTileSource(new BitmapRegionTileSource.UriBitmapSource(
+                    a, mUri, BitmapRegionTileSource.MAX_PREVIEW_SIZE), true, false);
         }
         @Override
         public void onSave(final WallpaperPickerActivity a) {
@@ -174,11 +173,11 @@
         @Override
         public void onClick(WallpaperPickerActivity a) {
             BitmapRegionTileSource.ResourceBitmapSource bitmapSource =
-                    new BitmapRegionTileSource.ResourceBitmapSource(mResources, mResId, 1024);
+                    new BitmapRegionTileSource.ResourceBitmapSource(
+                            mResources, mResId, BitmapRegionTileSource.MAX_PREVIEW_SIZE);
             bitmapSource.loadInBackground();
             BitmapRegionTileSource source = new BitmapRegionTileSource(a, bitmapSource);
             CropView v = a.getCropView();
-            a.getDefaultWallpaperView().setVisibility(View.INVISIBLE);
             v.setTileSource(source, null);
             Point wallpaperSize = WallpaperCropActivity.getDefaultWallpaperSize(
                     a.getResources(), a.getWindowManager());
@@ -210,15 +209,15 @@
         }
         @Override
         public void onClick(WallpaperPickerActivity a) {
-            a.getCropView().setTouchEnabled(false);
-            ImageView defaultWallpaperView = a.getDefaultWallpaperView();
-            defaultWallpaperView.setVisibility(View.VISIBLE);
+            CropView c = a.getCropView();
+
             Drawable defaultWallpaper = WallpaperManager.getInstance(a).getBuiltInDrawable(
-                    defaultWallpaperView.getWidth(), defaultWallpaperView.getHeight(),
-                    false, 0.5f, 0.5f);
-            if (defaultWallpaper != null) {
-                defaultWallpaperView.setBackgroundDrawable(defaultWallpaper);
-            }
+                    c.getWidth(), c.getHeight(), false, 0.5f, 0.5f);
+
+            c.setTileSource(
+                    new DrawableTileSource(a, defaultWallpaper, DrawableTileSource.MAX_PREVIEW_SIZE), null);
+            c.setScale(1f);
+            c.setTouchEnabled(false);
         }
         @Override
         public void onSave(WallpaperPickerActivity a) {
@@ -248,7 +247,6 @@
         setContentView(R.layout.wallpaper_picker);
 
         mCropView = (CropView) findViewById(R.id.cropView);
-        mDefaultWallpaperView = (ImageView) findViewById(R.id.defaultWallpaperView);
         mWallpaperStrip = findViewById(R.id.wallpaper_strip);
         mCropView.setTouchCallback(new CropView.TouchCallback() {
             LauncherViewPropertyAnimator mAnim;
@@ -409,7 +407,7 @@
 
         // Select the first item; wait for a layout pass so that we initialize the dimensions of
         // cropView or the defaultWallpaperView first
-        mDefaultWallpaperView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
+        mCropView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
             @Override
             public void onLayoutChange(View v, int left, int top, int right, int bottom,
                     int oldLeft, int oldTop, int oldRight, int oldBottom) {
@@ -530,7 +528,6 @@
     @Override
     public void setCropViewTileSource(final BitmapRegionTileSource.BitmapSource bitmapSource,
             final boolean touchEnabled, boolean moveToLeft) {
-        getDefaultWallpaperView().setVisibility(View.INVISIBLE);
         super.setCropViewTileSource(bitmapSource, touchEnabled, moveToLeft);
     }
 
@@ -899,10 +896,6 @@
         return mCropView;
     }
 
-    public ImageView getDefaultWallpaperView() {
-        return mDefaultWallpaperView;
-    }
-
     public SavedWallpaperImages getSavedImages() {
         return mSavedImages;
     }
diff --git a/src/com/android/photos/BitmapRegionTileSource.java b/src/com/android/photos/BitmapRegionTileSource.java
index 74284b2..b5774f4 100644
--- a/src/com/android/photos/BitmapRegionTileSource.java
+++ b/src/com/android/photos/BitmapRegionTileSource.java
@@ -55,7 +55,7 @@
     private static final int GL_SIZE_LIMIT = 2048;
     // This must be no larger than half the size of the GL_SIZE_LIMIT
     // due to decodePreview being allowed to be up to 2x the size of the target
-    private static final int MAX_PREVIEW_SIZE = 1024;
+    public static final int MAX_PREVIEW_SIZE = GL_SIZE_LIMIT / 2;
 
     public static abstract class BitmapSource {
         private BitmapRegionDecoder mDecoder;