Merge "Preventing springloaded mode from snapping back due to timeout. (Bug 5649189)" into ics-mr1
diff --git a/src/com/android/launcher2/AppsCustomizePagedView.java b/src/com/android/launcher2/AppsCustomizePagedView.java
index 0189d6c..ca02e1e 100644
--- a/src/com/android/launcher2/AppsCustomizePagedView.java
+++ b/src/com/android/launcher2/AppsCustomizePagedView.java
@@ -393,15 +393,17 @@
         mWidgetSpacingLayout.measure(widthSpec, heightSpec);
         mContentWidth = mWidgetSpacingLayout.getContentWidth();
 
+        AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
+        final boolean hostIsTransitioning = host.isTransitioning();
+
         // Restore the page
         int page = getPageForComponent(mSaveInstanceStateItemIndex);
-        invalidatePageData(Math.max(0, page));
+        invalidatePageData(Math.max(0, page), hostIsTransitioning);
 
         // Show All Apps cling if we are finished transitioning, otherwise, we will try again when
         // the transition completes in AppsCustomizeTabHost (otherwise the wrong offsets will be
         // returned while animating)
-        AppsCustomizeTabHost host = (AppsCustomizeTabHost) getTabHost();
-        if (!host.isTransitioning()) {
+        if (!hostIsTransitioning) {
             post(new Runnable() {
                 @Override
                 public void run() {
@@ -662,6 +664,18 @@
         cancelAllTasks();
     }
 
+    public void clearAllWidgetPages() {
+        cancelAllTasks();
+        int count = getChildCount();
+        for (int i = 0; i < count; i++) {
+            View v = getPageAt(i);
+            if (v instanceof PagedViewGridLayout) {
+                ((PagedViewGridLayout) v).removeAllViewsOnPage();
+                mDirtyPageContent.set(i, true);
+            }
+        }
+    }
+
     private void cancelAllTasks() {
         // Clean up all the async tasks
         Iterator<AppsCustomizeAsyncTask> iter = mRunningTasks.iterator();
diff --git a/src/com/android/launcher2/AppsCustomizeTabHost.java b/src/com/android/launcher2/AppsCustomizeTabHost.java
index 778d6bc..2963240 100644
--- a/src/com/android/launcher2/AppsCustomizeTabHost.java
+++ b/src/com/android/launcher2/AppsCustomizeTabHost.java
@@ -366,6 +366,11 @@
         }
         mContent.setVisibility(VISIBLE);
 
+        if (!toWorkspace) {
+            // Make sure the current page is loaded (we start loading the side pages after the
+            // transition to prevent slowing down the animation)
+            mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage(), true);
+        }
         if (animated && !delayLauncherTransitionUntilLayout) {
             enableAndBuildHardwareLayer();
         }
@@ -391,6 +396,9 @@
             // Dismiss the workspace cling and show the all apps cling (if not already shown)
             l.dismissWorkspaceCling(null);
             mAppsCustomizePane.showAllAppsCling();
+            // Make sure adjacent pages are loaded (we wait until after the transition to
+            // prevent slowing down the animation)
+            mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage());
 
             if (!LauncherApplication.isScreenLarge()) {
                 mAppsCustomizePane.hideScrollingIndicator(false);
@@ -398,6 +406,23 @@
         }
     }
 
+    public void onResume() {
+        if (getVisibility() == VISIBLE) {
+            mContent.setVisibility(VISIBLE);
+            // We unload the widget previews when the UI is hidden, so need to reload pages
+            // Load the current page synchronously, and the neighboring pages asynchronously
+            mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage(), true);
+            mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage());
+        }
+    }
+
+    public void onTrimMemory() {
+        mContent.setVisibility(GONE);
+        // Clear the widget pages of all their subviews - this will trigger the widget previews
+        // to delete their bitmaps
+        mAppsCustomizePane.clearAllWidgetPages();
+    }
+
     boolean isTransitioning() {
         return mInTransition;
     }
diff --git a/src/com/android/launcher2/HolographicImageView.java b/src/com/android/launcher2/HolographicImageView.java
index 1d34cd6..9e551e0 100644
--- a/src/com/android/launcher2/HolographicImageView.java
+++ b/src/com/android/launcher2/HolographicImageView.java
@@ -39,6 +39,10 @@
         mHolographicHelper = new HolographicViewHelper(context);
     }
 
+    void invalidatePressedFocusedStates() {
+        mHolographicHelper.invalidatePressedFocusedStates(this);
+    }
+
     @Override
     protected void onDraw(Canvas canvas) {
         super.onDraw(canvas);
diff --git a/src/com/android/launcher2/HolographicLinearLayout.java b/src/com/android/launcher2/HolographicLinearLayout.java
index c6a8d6a..a40c727 100644
--- a/src/com/android/launcher2/HolographicLinearLayout.java
+++ b/src/com/android/launcher2/HolographicLinearLayout.java
@@ -66,6 +66,10 @@
         }
     }
 
+    void invalidatePressedFocusedStates() {
+        mHolographicHelper.invalidatePressedFocusedStates(mImageView);
+    }
+
     @Override
     protected void onDraw(Canvas canvas) {
         super.onDraw(canvas);
diff --git a/src/com/android/launcher2/HolographicViewHelper.java b/src/com/android/launcher2/HolographicViewHelper.java
index 11e81b4..fd49908 100644
--- a/src/com/android/launcher2/HolographicViewHelper.java
+++ b/src/com/android/launcher2/HolographicViewHelper.java
@@ -42,7 +42,7 @@
     void generatePressedFocusedStates(ImageView v) {
         if (!mStatesUpdated && v != null) {
             mStatesUpdated = true;
-            Bitmap outline = createGlowingOutline(v, mTempCanvas);
+            Bitmap outline = createPressImage(v, mTempCanvas);
             FastBitmapDrawable d = new FastBitmapDrawable(outline);
 
             StateListDrawable states = new StateListDrawable();
@@ -54,10 +54,20 @@
     }
 
     /**
-     * Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
+     * Invalidates the pressed/focused states.
+     */
+    void invalidatePressedFocusedStates(ImageView v) {
+        mStatesUpdated = false;
+        if (v != null) {
+            v.invalidate();
+        }
+    }
+
+    /**
+     * Creates a new press state image which is the old image with a blue overlay.
      * Responsibility for the bitmap is transferred to the caller.
      */
-    private Bitmap createGlowingOutline(ImageView v, Canvas canvas) {
+    private Bitmap createPressImage(ImageView v, Canvas canvas) {
         final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
         final Bitmap b = Bitmap.createBitmap(
                 v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
diff --git a/src/com/android/launcher2/InstallShortcutReceiver.java b/src/com/android/launcher2/InstallShortcutReceiver.java
index 9d7054c..ed8f299 100644
--- a/src/com/android/launcher2/InstallShortcutReceiver.java
+++ b/src/com/android/launcher2/InstallShortcutReceiver.java
@@ -65,11 +65,15 @@
                 boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
                 if (duplicate || !LauncherModel.shortcutExists(context, name, intent)) {
                     LauncherApplication app = (LauncherApplication) context.getApplicationContext();
-                    app.getModel().addShortcut(context, data,
+                    ShortcutInfo info = app.getModel().addShortcut(context, data,
                             LauncherSettings.Favorites.CONTAINER_DESKTOP, screen, mCoordinates[0],
                             mCoordinates[1], true);
-                    Toast.makeText(context, context.getString(R.string.shortcut_installed, name),
-                            Toast.LENGTH_SHORT).show();
+                    if (info != null) {
+                        Toast.makeText(context, context.getString(R.string.shortcut_installed, name),
+                                Toast.LENGTH_SHORT).show();
+                    } else {
+                        return false;
+                    }
                 } else {
                     Toast.makeText(context, context.getString(R.string.shortcut_duplicate, name),
                             Toast.LENGTH_SHORT).show();
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 6ea14db..33ead5c 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -36,6 +36,7 @@
 import android.content.BroadcastReceiver;
 import android.content.ClipData;
 import android.content.ClipDescription;
+import android.content.ComponentCallbacks2;
 import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -563,6 +564,7 @@
         // When we resume Launcher, a different Activity might be responsible for the app
         // market intent, so refresh the icon
         updateAppMarketIcon();
+        mAppsCustomizeTabHost.onResume();
         if (!mWorkspaceLoading) {
             final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
             final Workspace workspace = mWorkspace;
@@ -873,6 +875,9 @@
         boolean foundCellSpan = false;
 
         ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
+        if (info == null) {
+            return;
+        }
         final View view = createShortcut(info);
 
         // First we check if we already know the exact location where we want to add this item.
@@ -1709,6 +1714,8 @@
      * @param v The view that was clicked.
      */
     public void onClickSearchButton(View v) {
+        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
+
         onSearchRequested();
     }
 
@@ -1718,10 +1725,8 @@
      * @param v The view that was clicked.
      */
     public void onClickVoiceButton(View v) {
-        startVoiceSearch();
-    }
+        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
 
-    private void startVoiceSearch() {
         Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
         startActivity(intent);
@@ -2397,6 +2402,14 @@
         }
     }
 
+    @Override
+    public void onTrimMemory(int level) {
+        super.onTrimMemory(level);
+        if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
+            mAppsCustomizeTabHost.onTrimMemory();
+        }
+    }
+
     void showWorkspace(boolean animated) {
         Resources res = getResources();
         int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
@@ -2661,6 +2674,16 @@
         button.setImageDrawable(d.newDrawable(getResources()));
     }
 
+    private void invalidatePressedFocusedStates(View container, View button) {
+        if (container instanceof HolographicLinearLayout) {
+            HolographicLinearLayout layout = (HolographicLinearLayout) container;
+            layout.invalidatePressedFocusedStates();
+        } else if (button instanceof HolographicImageView) {
+            HolographicImageView view = (HolographicImageView) button;
+            view.invalidatePressedFocusedStates();
+        }
+    }
+
     private boolean updateGlobalSearchIcon() {
         final View searchButtonContainer = findViewById(R.id.search_button_container);
         final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
@@ -2678,6 +2701,7 @@
             if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
             if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
             searchButton.setVisibility(View.VISIBLE);
+            invalidatePressedFocusedStates(searchButtonContainer, searchButton);
             return true;
         } else {
             // We disable both search and voice search when there is no global search provider
@@ -2691,7 +2715,10 @@
     }
 
     private void updateGlobalSearchIcon(Drawable.ConstantState d) {
+        final View searchButtonContainer = findViewById(R.id.search_button_container);
+        final View searchButton = (ImageView) findViewById(R.id.search_button);
         updateButtonWithDrawable(R.id.search_button, d);
+        invalidatePressedFocusedStates(searchButtonContainer, searchButton);
     }
 
     private boolean updateVoiceSearchIcon(boolean searchVisible) {
@@ -2709,6 +2736,7 @@
             if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
             if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
             voiceButton.setVisibility(View.VISIBLE);
+            invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
             return true;
         } else {
             if (searchDivider != null) searchDivider.setVisibility(View.GONE);
@@ -2719,7 +2747,10 @@
     }
 
     private void updateVoiceSearchIcon(Drawable.ConstantState d) {
+        final View voiceButtonContainer = findViewById(R.id.voice_button_container);
+        final View voiceButton = findViewById(R.id.voice_button);
         updateButtonWithDrawable(R.id.voice_button, d);
+        invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
     }
 
     /**
diff --git a/src/com/android/launcher2/LauncherModel.java b/src/com/android/launcher2/LauncherModel.java
index 1755903..50a36a5 100644
--- a/src/com/android/launcher2/LauncherModel.java
+++ b/src/com/android/launcher2/LauncherModel.java
@@ -1772,6 +1772,9 @@
     ShortcutInfo addShortcut(Context context, Intent data, long container, int screen,
             int cellX, int cellY, boolean notify) {
         final ShortcutInfo info = infoFromShortcutIntent(context, data, null);
+        if (info == null) {
+            return null;
+        }
         addItemToDatabase(context, info, container, screen, cellX, cellY, notify);
 
         return info;
@@ -1835,6 +1838,12 @@
         String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
         Parcelable bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON);
 
+        if (intent == null) {
+            // If the intent is null, we can't construct a valid ShortcutInfo, so we return null
+            Log.e(TAG, "Can't construct ShorcutInfo with null intent");
+            return null;
+        }
+
         Bitmap icon = null;
         boolean customIcon = false;
         ShortcutIconResource iconResource = null;
diff --git a/src/com/android/launcher2/PagedView.java b/src/com/android/launcher2/PagedView.java
index ba303a1..3f5652e 100644
--- a/src/com/android/launcher2/PagedView.java
+++ b/src/com/android/launcher2/PagedView.java
@@ -143,7 +143,7 @@
 
     private PageSwitchListener mPageSwitchListener;
 
-    private ArrayList<Boolean> mDirtyPageContent;
+    protected ArrayList<Boolean> mDirtyPageContent;
 
     // choice modes
     protected static final int CHOICE_MODE_NONE = 0;
diff --git a/src/com/android/launcher2/PagedViewGridLayout.java b/src/com/android/launcher2/PagedViewGridLayout.java
index 5c32e09..b1b6215 100644
--- a/src/com/android/launcher2/PagedViewGridLayout.java
+++ b/src/com/android/launcher2/PagedViewGridLayout.java
@@ -68,6 +68,12 @@
                 heightMeasureSpec);
     }
 
+    @Override
+    protected void onDetachedFromWindow() {
+        super.onDetachedFromWindow();
+        mOnLayoutListener = null;
+    }
+
     public void setOnLayoutListener(Runnable r) {
         mOnLayoutListener = r;
     }
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index e122094..60f57c2 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -2481,7 +2481,9 @@
                 if (isShortcut) {
                     final Intent intent = data.getItemAt(index).getIntent();
                     Object info = model.infoFromShortcutIntent(mContext, intent, data.getIcon());
-                    onDropExternal(new int[] { x, y }, info, layout, false);
+                    if (info != null) {
+                        onDropExternal(new int[] { x, y }, info, layout, false);
+                    }
                 } else {
                     if (widgets.size() == 1) {
                         // If there is only one item, then go ahead and add and configure