Merge "Revert "Fix issue of extra blank screen after uninstall (issue 12372657)"" into ub-now-lunchbox
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index be3a28b..d4cd6a2 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -106,7 +106,7 @@
 
         <activity
             android:name="com.android.launcher3.LauncherWallpaperPickerActivity"
-            android:theme="@style/Theme.WallpaperCropper"
+            android:theme="@style/Theme.WallpaperPicker"
             android:label="@string/pick_wallpaper"
             android:icon="@mipmap/ic_launcher_wallpaper"
             android:finishOnCloseSystemDialogs="true"
diff --git a/WallpaperPicker/res/layout/actionbar_set_wallpaper.xml b/WallpaperPicker/res/layout/actionbar_set_wallpaper.xml
index 2a0188a..8e349b7 100644
--- a/WallpaperPicker/res/layout/actionbar_set_wallpaper.xml
+++ b/WallpaperPicker/res/layout/actionbar_set_wallpaper.xml
@@ -18,18 +18,15 @@
 */
 -->
 
-<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    style="?android:actionButtonStyle"
+<com.android.launcher3.AlphaDisableableButton
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    style="@style/ActionBarSetWallpaperStyle"
     android:id="@+id/set_wallpaper_button"
     android:layout_width="match_parent"
-    android:layout_height="match_parent" >
-    <TextView style="?android:actionBarTabTextStyle"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_gravity="start|center_vertical"
-        android:paddingRight="20dp"
-        android:drawableLeft="@drawable/ic_actionbar_accept"
-        android:drawablePadding="8dp"
-        android:gravity="center_vertical"
-        android:text="@string/wallpaper_instructions" />
-</FrameLayout>
+    android:layout_height="match_parent"
+    android:paddingRight="20dp"
+    android:drawableLeft="@drawable/ic_actionbar_accept"
+    android:drawablePadding="8dp"
+    android:gravity="start|center_vertical"
+    android:text="@string/wallpaper_instructions">
+</com.android.launcher3.AlphaDisableableButton>
diff --git a/WallpaperPicker/res/values/styles.xml b/WallpaperPicker/res/values/styles.xml
index 6d600bc..16b11f2 100644
--- a/WallpaperPicker/res/values/styles.xml
+++ b/WallpaperPicker/res/values/styles.xml
@@ -24,6 +24,12 @@
         <item name="android:windowActionBarOverlay">true</item>
     </style>
 
+    <style name="Theme.WallpaperPicker" parent="Theme.WallpaperCropper">
+        <item name="android:windowBackground">@android:color/transparent</item>
+        <item name="android:colorBackgroundCacheHint">@null</item>
+        <item name="android:windowShowWallpaper">true</item>
+    </style>
+
     <style name="WallpaperCropperActionBar" parent="android:style/Widget.Holo.ActionBar">
         <item name="android:displayOptions">showCustom</item>
         <item name="android:background">#88000000</item>
@@ -31,4 +37,8 @@
 
     <style name="Theme" parent="@android:style/Theme.Holo.Wallpaper.NoTitleBar">
     </style>
+
+    <style name="ActionBarSetWallpaperStyle" parent="@android:style/Widget.Holo.ActionButton">
+        <item name="android:textColor">#ffffffff</item>
+    </style>
 </resources>
diff --git a/WallpaperPicker/src/com/android/launcher3/AlphaDisableableButton.java b/WallpaperPicker/src/com/android/launcher3/AlphaDisableableButton.java
new file mode 100644
index 0000000..f0796c3
--- /dev/null
+++ b/WallpaperPicker/src/com/android/launcher3/AlphaDisableableButton.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2014 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.util.AttributeSet;
+import android.widget.Button;
+
+/**
+ * A Button which becomes translucent when it is disabled
+ */
+public class AlphaDisableableButton extends Button {
+    public static float DISABLED_ALPHA_VALUE = 0.4f;
+    public AlphaDisableableButton(Context context) {
+        this(context, null);
+    }
+
+    public AlphaDisableableButton(Context context, AttributeSet attrs) {
+        this(context, attrs, 0);
+    }
+
+    public AlphaDisableableButton(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+        setLayerType(LAYER_TYPE_HARDWARE, null);
+    }
+
+    @Override
+    public void setEnabled(boolean enabled) {
+        super.setEnabled(enabled);
+        if(enabled) {
+            setAlpha(1.0f);
+        } else {
+            setAlpha(DISABLED_ALPHA_VALUE);
+        }
+    }
+}
diff --git a/WallpaperPicker/src/com/android/launcher3/LiveWallpaperListAdapter.java b/WallpaperPicker/src/com/android/launcher3/LiveWallpaperListAdapter.java
index 60b2537..88f4461 100644
--- a/WallpaperPicker/src/com/android/launcher3/LiveWallpaperListAdapter.java
+++ b/WallpaperPicker/src/com/android/launcher3/LiveWallpaperListAdapter.java
@@ -122,7 +122,7 @@
             Intent preview = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
             preview.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
                     mInfo.getComponent());
-            a.onLiveWallpaperPickerLaunch();
+            a.onLiveWallpaperPickerLaunch(mInfo);
             a.startActivityForResultSafely(preview, WallpaperPickerActivity.PICK_LIVE_WALLPAPER);
         }
     }
diff --git a/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java b/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java
index 23fbe73..561c4bb 100644
--- a/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java
+++ b/WallpaperPicker/src/com/android/launcher3/WallpaperCropActivity.java
@@ -75,7 +75,7 @@
 
     protected CropView mCropView;
     protected Uri mUri;
-    private View mSetWallpaperButton;
+    protected View mSetWallpaperButton;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -117,7 +117,7 @@
         // Load image in background
         final BitmapRegionTileSource.UriBitmapSource bitmapSource =
                 new BitmapRegionTileSource.UriBitmapSource(this, imageUri, 1024);
-        mSetWallpaperButton.setVisibility(View.INVISIBLE);
+        mSetWallpaperButton.setEnabled(false);
         Runnable onLoad = new Runnable() {
             public void run() {
                 if (bitmapSource.getLoadingState() != BitmapSource.State.LOADED) {
@@ -126,7 +126,7 @@
                             Toast.LENGTH_LONG).show();
                     finish();
                 } else {
-                    mSetWallpaperButton.setVisibility(View.VISIBLE);
+                    mSetWallpaperButton.setEnabled(true);
                 }
             }
         };
diff --git a/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java b/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java
index ab25fa9..013606a 100644
--- a/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java
+++ b/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java
@@ -58,6 +58,7 @@
 import android.view.ViewPropertyAnimator;
 import android.view.ViewTreeObserver;
 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
+import android.view.WindowManager;
 import android.view.animation.AccelerateInterpolator;
 import android.view.animation.DecelerateInterpolator;
 import android.widget.BaseAdapter;
@@ -86,9 +87,9 @@
     private static final String SELECTED_INDEX = "SELECTED_INDEX";
     private static final String OLD_DEFAULT_WALLPAPER_THUMBNAIL_FILENAME = "default_thumb.jpg";
     private static final String DEFAULT_WALLPAPER_THUMBNAIL_FILENAME = "default_thumb2.jpg";
+    private static final int FLAG_POST_DELAY_MILLIS = 200;
 
     private View mSelectedTile;
-    private View mSetWallpaperButton;
     private boolean mIgnoreNextTap;
     private OnClickListener mThumbnailOnClickListener;
 
@@ -103,7 +104,8 @@
     ArrayList<Uri> mTempWallpaperTiles = new ArrayList<Uri>();
     private SavedWallpaperImages mSavedImages;
     private WallpaperInfo mLiveWallpaperInfoOnPickerLaunch;
-    private int mSelectedIndex;
+    private int mSelectedIndex = -1;
+    private WallpaperInfo mLastClickedLiveWallpaperInfo;
 
     public static abstract class WallpaperTileInfo {
         protected View mView;
@@ -145,13 +147,13 @@
                 onLoad = null;
             } else {
                 mFirstClick = false;
-                a.mSetWallpaperButton.setVisibility(View.INVISIBLE);
+                a.mSetWallpaperButton.setEnabled(false);
                 onLoad = new Runnable() {
                     public void run() {
                         if (mBitmapSource != null &&
                                 mBitmapSource.getLoadingState() == BitmapSource.State.LOADED) {
                             a.selectTile(mView);
-                            a.mSetWallpaperButton.setVisibility(View.VISIBLE);
+                            a.mSetWallpaperButton.setEnabled(true);
                         } else {
                             ViewGroup parent = (ViewGroup) mView.getParent();
                             if (parent != null) {
@@ -218,6 +220,7 @@
                     wallpaperSize.x, wallpaperSize.y, false);
             v.setScale(wallpaperSize.x / crop.width());
             v.setTouchEnabled(false);
+            a.setSystemWallpaperVisiblity(false);
         }
         @Override
         public void onSave(WallpaperPickerActivity a) {
@@ -256,6 +259,7 @@
                     new DrawableTileSource(a, defaultWallpaper, DrawableTileSource.MAX_PREVIEW_SIZE), null);
             c.setScale(1f);
             c.setTouchEnabled(false);
+            a.setSystemWallpaperVisiblity(false);
         }
         @Override
         public void onSave(WallpaperPickerActivity a) {
@@ -281,11 +285,70 @@
         mWallpaperStrip.setPadding(0, 0, 0, (int) offset);
     }
 
+    /**
+     * shows the system wallpaper behind the window and hides the {@link
+     * #mCropView} if visible
+     * @param visible should the system wallpaper be shown
+     */
+    protected void setSystemWallpaperVisiblity(final boolean visible) {
+        // hide our own wallpaper preview if necessary
+        if(!visible) {
+            mCropView.setVisibility(View.VISIBLE);
+        } else {
+            changeWallpaperFlags(visible);
+        }
+        // the change of the flag must be delayed in order to avoid flickering,
+        // a simple post / double post does not suffice here
+        mCropView.postDelayed(new Runnable() {
+            @Override
+            public void run() {
+                if(!visible) {
+                    changeWallpaperFlags(visible);
+                } else {
+                    mCropView.setVisibility(View.INVISIBLE);
+                }
+            }
+        }, FLAG_POST_DELAY_MILLIS);
+    }
+
+    private void changeWallpaperFlags(boolean visible) {
+        int desiredWallpaperFlag = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
+        int currentWallpaperFlag = getWindow().getAttributes().flags
+                & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
+        if (desiredWallpaperFlag != currentWallpaperFlag) {
+            getWindow().setFlags(desiredWallpaperFlag,
+                    WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
+        }
+    }
+
+    @Override
+    public void setCropViewTileSource(BitmapSource bitmapSource,
+                                      boolean touchEnabled,
+                                      boolean moveToLeft,
+                                      final Runnable postExecute) {
+        // we also want to show our own wallpaper instead of the one in the background
+        Runnable showPostExecuteRunnable = new Runnable() {
+            @Override
+            public void run() {
+                if(postExecute != null) {
+                    postExecute.run();
+                }
+                setSystemWallpaperVisiblity(false);
+            }
+        };
+        super.setCropViewTileSource(bitmapSource,
+                touchEnabled,
+                moveToLeft,
+                showPostExecuteRunnable);
+    }
+
     // called by onCreate; this is subclassed to overwrite WallpaperCropActivity
     protected void init() {
         setContentView(R.layout.wallpaper_picker);
 
         mCropView = (CropView) findViewById(R.id.cropView);
+        mCropView.setVisibility(View.INVISIBLE);
+
         mWallpaperStrip = findViewById(R.id.wallpaper_strip);
         mCropView.setTouchCallback(new CropView.TouchCallback() {
             ViewPropertyAnimator mAnim;
@@ -339,7 +402,7 @@
                     }
                     return;
                 }
-                mSetWallpaperButton.setVisibility(View.VISIBLE);
+                mSetWallpaperButton.setEnabled(true);
                 WallpaperTileInfo info = (WallpaperTileInfo) v.getTag();
                 if (info.isSelectable() && v.getVisibility() == View.VISIBLE) {
                     selectTile(v);
@@ -445,6 +508,7 @@
                     if (mSelectedIndex >= 0 && mSelectedIndex < mWallpapersView.getChildCount()) {
                         mThumbnailOnClickListener.onClick(
                                 mWallpapersView.getChildAt(mSelectedIndex));
+                        setSystemWallpaperVisiblity(false);
                     }
                     v.removeOnLayoutChangeListener(this);
                 }
@@ -474,6 +538,10 @@
                         if (mSelectedTile != null) {
                             WallpaperTileInfo info = (WallpaperTileInfo) mSelectedTile.getTag();
                             info.onSave(WallpaperPickerActivity.this);
+                        } else {
+                            // no tile was selected, so we just finish the activity and go back
+                            setResult(Activity.RESULT_OK);
+                            finish();
                         }
                     }
                 });
@@ -524,6 +592,7 @@
                 if (itemId == R.id.menu_delete) {
                     int childCount = mWallpapersView.getChildCount();
                     ArrayList<View> viewsToRemove = new ArrayList<View>();
+                    boolean selectedTileRemoved = false;
                     for (int i = 0; i < childCount; i++) {
                         CheckableFrameLayout c =
                                 (CheckableFrameLayout) mWallpapersView.getChildAt(i);
@@ -531,11 +600,19 @@
                             WallpaperTileInfo info = (WallpaperTileInfo) c.getTag();
                             info.onDelete(WallpaperPickerActivity.this);
                             viewsToRemove.add(c);
+                            if (i == mSelectedIndex) {
+                                selectedTileRemoved = true;
+                            }
                         }
                     }
                     for (View v : viewsToRemove) {
                         mWallpapersView.removeView(v);
                     }
+                    if (selectedTileRemoved) {
+                        mSelectedIndex = -1;
+                        mSelectedTile = null;
+                        setSystemWallpaperVisiblity(true);
+                    }
                     updateTileIndices();
                     mode.finish(); // Action picked, so close the CAB
                     return true;
@@ -552,7 +629,9 @@
                     CheckableFrameLayout c = (CheckableFrameLayout) mWallpapersView.getChildAt(i);
                     c.setChecked(false);
                 }
-                mSelectedTile.setSelected(true);
+                if (mSelectedTile != null) {
+                    mSelectedTile.setSelected(true);
+                }
                 mActionMode = null;
             }
         };
@@ -624,7 +703,7 @@
         for (Uri uri : uris) {
             addTemporaryWallpaperTile(uri, true);
         }
-        mSelectedIndex = savedInstanceState.getInt(SELECTED_INDEX, 0);
+        mSelectedIndex = savedInstanceState.getInt(SELECTED_INDEX, -1);
     }
 
     private void populateWallpapersFromAdapter(ViewGroup parent, BaseAdapter adapter,
@@ -782,11 +861,15 @@
         } else if (requestCode == PICK_LIVE_WALLPAPER) {
             WallpaperManager wm = WallpaperManager.getInstance(this);
             final WallpaperInfo oldLiveWallpaper = mLiveWallpaperInfoOnPickerLaunch;
+            final WallpaperInfo clickedWallpaper = mLastClickedLiveWallpaperInfo;
             WallpaperInfo newLiveWallpaper = wm.getWallpaperInfo();
             // Try to figure out if a live wallpaper was set;
             if (newLiveWallpaper != null &&
-                    (oldLiveWallpaper == null ||
-                    !oldLiveWallpaper.getComponent().equals(newLiveWallpaper.getComponent()))) {
+                    (oldLiveWallpaper == null
+                            || !oldLiveWallpaper.getComponent()
+                                    .equals(newLiveWallpaper.getComponent())
+                            || clickedWallpaper.getComponent()
+                                    .equals(oldLiveWallpaper.getComponent()))) {
                 // Return if a live wallpaper was set
                 setResult(RESULT_OK);
                 finish();
@@ -946,7 +1029,8 @@
         return mSavedImages;
     }
 
-    public void onLiveWallpaperPickerLaunch() {
+    public void onLiveWallpaperPickerLaunch(WallpaperInfo info) {
+        mLastClickedLiveWallpaperInfo = info;
         mLiveWallpaperInfoOnPickerLaunch = WallpaperManager.getInstance(this).getWallpaperInfo();
     }
 
diff --git a/WallpaperPicker/src/com/android/photos/views/TiledImageView.java b/WallpaperPicker/src/com/android/photos/views/TiledImageView.java
index af4199c..94063b0 100644
--- a/WallpaperPicker/src/com/android/photos/views/TiledImageView.java
+++ b/WallpaperPicker/src/com/android/photos/views/TiledImageView.java
@@ -112,6 +112,18 @@
         //setTileSource(new ColoredTiles());
     }
 
+    @Override
+    public void setVisibility(int visibility) {
+        super.setVisibility(visibility);
+        // need to update inner view's visibility because it seems like we're causing it to draw
+        // from {@link #dispatchDraw} or {@link #invalidate} even if we are invisible.
+        if (USE_TEXTURE_VIEW) {
+            mTextureView.setVisibility(visibility);
+        } else {
+            mGLSurfaceView.setVisibility(visibility);
+        }
+    }
+
     public void destroy() {
         if (!IS_SUPPORTED) {
             return;
diff --git a/src/com/android/launcher3/LauncherClings.java b/src/com/android/launcher3/LauncherClings.java
index 894f64d..952edfd 100644
--- a/src/com/android/launcher3/LauncherClings.java
+++ b/src/com/android/launcher3/LauncherClings.java
@@ -297,12 +297,18 @@
 
     public static void synchonouslyMarkFirstRunClingDismissed(Context ctx) {
         SharedPreferences prefs = ctx.getSharedPreferences(
-                LauncherAppState.getSharedPreferencesKey(),Context.MODE_PRIVATE);
+                LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE);
         SharedPreferences.Editor editor = prefs.edit();
         editor.putBoolean(LauncherClings.FIRST_RUN_CLING_DISMISSED_KEY, true);
         editor.commit();
     }
 
+    public void markFolderClingDismissed() {
+        SharedPreferences.Editor editor = mLauncher.getSharedPrefs().edit();
+        editor.putBoolean(LauncherClings.FOLDER_CLING_DISMISSED_KEY, true);
+        editor.apply();
+    }
+
     /** Removes the cling outright from the DragLayer */
     private void removeCling(int id) {
         final View cling = mLauncher.findViewById(id);
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index 7e01232..567abfa 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -863,7 +863,7 @@
         // Log to disk
         Launcher.addDumpLog(TAG, "11683562 - stripEmptyScreens()", true);
 
-        if (isWorkspaceLoading()) {
+        if (mLauncher.isWorkspaceLoading()) {
             // Don't strip empty screens if the workspace is still loading
             Launcher.addDumpLog(TAG, "    - workspace loading, skip", true);
             return;
@@ -2632,6 +2632,9 @@
         if (child instanceof BubbleTextView) {
             BubbleTextView icon = (BubbleTextView) child;
             icon.clearPressedOrFocusedBackground();
+        } else if (child instanceof FolderIcon) {
+            // Dismiss the folder cling if we haven't already
+            mLauncher.getLauncherClings().markFolderClingDismissed();
         }
 
         if (child.getTag() == null || !(child.getTag() instanceof ItemInfo)) {