Merge "Announce wallpaper selection for accessibility" into jb-ub-now-indigo-rose
diff --git a/src/com/android/launcher3/CellLayout.java b/src/com/android/launcher3/CellLayout.java
index 1d10553..b17f5b4 100644
--- a/src/com/android/launcher3/CellLayout.java
+++ b/src/com/android/launcher3/CellLayout.java
@@ -115,7 +115,6 @@
 
     // If we're actively dragging something over this screen, mIsDragOverlapping is true
     private boolean mIsDragOverlapping = false;
-    private final Point mDragCenter = new Point();
     boolean mUseActiveGlowBackground = false;
 
     // These arrays are used to implement the drag visualization on x-large screens.
@@ -1025,7 +1024,12 @@
     @Override
     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
         super.onSizeChanged(w, h, oldw, oldh);
-        mBackgroundRect.set(0, 0, w, h);
+
+        // Expand the background drawing bounds by the padding baked into the background drawable
+        Rect padding = new Rect();
+        mNormalBackground.getPadding(padding);
+        mBackgroundRect.set(-padding.left, -padding.top, w + padding.right, h + padding.bottom);
+
         mForegroundRect.set(mForegroundPadding, mForegroundPadding,
                 w - mForegroundPadding, h - mForegroundPadding);
     }
@@ -1199,12 +1203,6 @@
         final int oldDragCellX = mDragCell[0];
         final int oldDragCellY = mDragCell[1];
 
-        if (v != null && dragOffset == null) {
-            mDragCenter.set(originX + (v.getWidth() / 2), originY + (v.getHeight() / 2));
-        } else {
-            mDragCenter.set(originX, originY);
-        }
-
         if (dragOutline == null && v == null) {
             return;
         }
@@ -1219,11 +1217,6 @@
             int left = topLeft[0];
             int top = topLeft[1];
 
-            // Offset icons by their padding
-            if (v instanceof BubbleTextView) {
-                top += v.getPaddingTop();
-            }
-
             if (v != null && dragOffset == null) {
                 // When drawing the drag outline, it did not account for margin offsets
                 // added by the view's parent.
@@ -1244,7 +1237,9 @@
                     // outline offset
                     left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
                              - dragRegion.width()) / 2;
-                    top += dragOffset.y;
+                    int cHeight = getShortcutsAndWidgets().getCellContentHeight();
+                    int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f));
+                    top += dragOffset.y + cellPaddingY;
                 } else {
                     // Center the drag outline in the cell
                     left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap)
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index cfa5545..5aec399 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -270,6 +270,7 @@
     private IconCache mIconCache;
     private boolean mUserPresent = true;
     private boolean mVisible = false;
+    private boolean mHasFocus = false;
     private boolean mAttached = false;
     private static final boolean DISABLE_CLINGS = false;
     private static final boolean DISABLE_CUSTOM_CLINGS = true;
@@ -1009,25 +1010,11 @@
     }
 
     // We can't hide the IME if it was forced open.  So don't bother
-    /*
     @Override
     public void onWindowFocusChanged(boolean hasFocus) {
         super.onWindowFocusChanged(hasFocus);
-
-        if (hasFocus) {
-            final InputMethodManager inputManager = (InputMethodManager)
-                    getSystemService(Context.INPUT_METHOD_SERVICE);
-            WindowManager.LayoutParams lp = getWindow().getAttributes();
-            inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
-                        android.os.Handler()) {
-                        protected void onReceiveResult(int resultCode, Bundle resultData) {
-                            Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
-                        }
-                    });
-            Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
-        }
+        mHasFocus = hasFocus;
     }
-    */
 
     private boolean acceptFilter() {
         final InputMethodManager inputManager = (InputMethodManager)
@@ -1485,7 +1472,7 @@
                 // Reset AllApps to its initial state only if we are not in the middle of
                 // processing a multi-step drop
                 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
-                    showWorkspaceAndExitOverviewMode(false);
+                    showWorkspace(false);
                 }
             } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
                 mUserPresent = true;
@@ -1669,9 +1656,9 @@
             // also will cancel mWaitingForResult.
             closeSystemDialogs();
 
-            final boolean alreadyOnHome =
-                    ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
-                        != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
+            final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
+                    Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
+                    != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
 
             if (mWorkspace == null) {
                 // Can be cases where mWorkspace is null, this prevents a NPE
@@ -1691,7 +1678,7 @@
             // If we are already on home, then just animate back to the workspace,
             // otherwise, just wait until onResume to set the state back to Workspace
             if (alreadyOnHome) {
-                showWorkspaceAndExitOverviewMode(true);
+                showWorkspace(true);
             } else {
                 mOnResumeState = State.WORKSPACE;
             }
@@ -1708,21 +1695,12 @@
                 mAppsCustomizeTabHost.reset();
             }
         }
+
         if (DEBUG_RESUME_TIME) {
             Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
         }
     }
 
-    protected void showWorkspaceAndExitOverviewMode(boolean animate) {
-        showWorkspace(animate);
-        if (mWorkspace.isInOverviewMode()) {
-            mWorkspace.exitOverviewMode(animate);
-        }
-    }
-    protected void showWorkspaceAndExitOverviewMode() {
-        showWorkspaceAndExitOverviewMode(false);
-    }
-
     @Override
     public void onRestoreInstanceState(Bundle state) {
         super.onRestoreInstanceState(state);
@@ -2079,7 +2057,6 @@
     }
 
     protected void startWallpaper() {
-        showWorkspace(true);
         final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
         pickWallpaper.setComponent(getWallpaperPickerComponent());
         startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
@@ -2125,7 +2102,12 @@
     @Override
     public void onBackPressed() {
         if (isAllAppsVisible()) {
-            showWorkspace(true);
+            if (mAppsCustomizeContent.getContentType() ==
+                    AppsCustomizePagedView.ContentType.Applications) {
+                showWorkspace(true);
+            } else {
+                showOverviewMode(true);
+            }
         } else if (mWorkspace.isInOverviewMode()) {
             mWorkspace.exitOverviewMode(true);
         } else if (mWorkspace.getOpenFolder() != null) {
@@ -2943,7 +2925,7 @@
      * This is the opposite of showAppsCustomizeHelper.
      * @param animated If true, the transition will be animated.
      */
-    private void hideAppsCustomizeHelper(State toState, final boolean animated,
+    private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
             final boolean springLoaded, final Runnable onCompleteRunnable) {
 
         if (mStateAnimation != null) {
@@ -2961,13 +2943,14 @@
         final View fromView = mAppsCustomizeTabHost;
         final View toView = mWorkspace;
         Animator workspaceAnim = null;
-        if (toState == State.WORKSPACE) {
+        if (toState == Workspace.State.NORMAL) {
             int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
             workspaceAnim = mWorkspace.getChangeStateAnimation(
-                    Workspace.State.NORMAL, animated, stagger, -1);
-        } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
+                    toState, animated, stagger, -1);
+        } else if (toState == Workspace.State.SPRING_LOADED ||
+                toState == Workspace.State.OVERVIEW) {
             workspaceAnim = mWorkspace.getChangeStateAnimation(
-                    Workspace.State.SPRING_LOADED, animated);
+                    toState, animated);
         }
 
         setPivotsForZoom(fromView, scaleFactor);
@@ -3039,20 +3022,27 @@
         }
     }
 
-    void showWorkspace(boolean animated) {
+    protected void showWorkspace(boolean animated) {
         showWorkspace(animated, null);
     }
 
+    protected void showWorkspace() {
+        showWorkspace(true);
+    }
+
     void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
+        if (mWorkspace.isInOverviewMode()) {
+            mWorkspace.exitOverviewMode(animated);
+        }
         if (mState != State.WORKSPACE) {
             boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
             mWorkspace.setVisibility(View.VISIBLE);
-            hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
+            hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
 
             // Show the search bar (only animate if we were showing the drop target bar in spring
             // loaded mode)
             if (mSearchDropTargetBar != null) {
-                mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
+                mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
             }
 
             // Set focus to the AppsCustomize button
@@ -3075,6 +3065,13 @@
         onWorkspaceShown(animated);
     }
 
+    void showOverviewMode(boolean animated) {
+        mWorkspace.setVisibility(View.VISIBLE);
+        hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
+        mState = State.WORKSPACE;
+        onWorkspaceShown(animated);
+    }
+
     public void onWorkspaceShown(boolean animated) {
     }
 
@@ -3100,7 +3097,7 @@
 
     void enterSpringLoadedDragMode() {
         if (isAllAppsVisible()) {
-            hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
+            hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
             mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
         }
     }
@@ -3931,8 +3928,7 @@
                 mIntentsOnWorkspaceFromUpgradePath = null;
             }
         } else {
-            if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
-                    mAppsCustomizeContent != null) {
+            if (mAppsCustomizeContent != null) {
                 mAppsCustomizeContent.setApps(apps);
             }
         }
diff --git a/src/com/android/launcher3/ShortcutAndWidgetContainer.java b/src/com/android/launcher3/ShortcutAndWidgetContainer.java
index fcd6f19..1cf4c11 100644
--- a/src/com/android/launcher3/ShortcutAndWidgetContainer.java
+++ b/src/com/android/launcher3/ShortcutAndWidgetContainer.java
@@ -123,6 +123,13 @@
         mIsHotseatLayout = isHotseat;
     }
 
+    int getCellContentHeight() {
+        final LauncherAppState app = LauncherAppState.getInstance();
+        final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
+        return Math.min(getMeasuredHeight(), mIsHotseatLayout ?
+                grid.hotseatCellHeightPx : grid.cellHeightPx);
+    }
+
     public void measureChild(View child) {
         final LauncherAppState app = LauncherAppState.getInstance();
         final DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
@@ -137,7 +144,7 @@
                 // Widgets have their own padding, so skip
             } else {
                 // Otherwise, center the icon
-                int cHeight = mIsHotseatLayout ? grid.hotseatCellHeightPx : Math.min(getMeasuredHeight(), grid.cellHeightPx);
+                int cHeight = getCellContentHeight();
                 int cellPaddingY = (int) Math.max(0, ((lp.height - cHeight) / 2f));
                 int cellPaddingX = (int) (grid.edgeMarginPx / 2f);
                 child.setPadding(cellPaddingX, cellPaddingY, cellPaddingX, 0);
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index b9552ea..a23d7d0 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -338,7 +338,8 @@
             ItemInfo itemInfo, boolean springLoaded) {
         int[] size = new int[2];
         if (getChildCount() > 0) {
-            CellLayout cl = (CellLayout) mLauncher.getWorkspace().getChildAt(0);
+            // Use the first non-custom page to estimate the child position
+            CellLayout cl = (CellLayout) getChildAt(numCustomPages());
             Rect r = estimateItemPosition(cl, itemInfo, 0, 0, hSpan, vSpan);
             size[0] = r.width();
             size[1] = r.height();
@@ -2074,7 +2075,7 @@
                     R.integer.config_appsCustomizeSpringLoadedBgAlpha) / 100f, true);
         } else {
             // Fade the background gradient away
-            animateBackgroundGradient(0f, true);
+            animateBackgroundGradient(0f, animated);
         }
         return anim;
     }