Merge "[DO NOT MERGE] Make the cutout slightly smaller" into sc-dev
diff --git a/quickstep/src/com/android/quickstep/AbsSwipeUpHandler.java b/quickstep/src/com/android/quickstep/AbsSwipeUpHandler.java
index ad7c710..e867f07 100644
--- a/quickstep/src/com/android/quickstep/AbsSwipeUpHandler.java
+++ b/quickstep/src/com/android/quickstep/AbsSwipeUpHandler.java
@@ -233,6 +233,7 @@
 
     // Used to control launcher components throughout the swipe gesture.
     private AnimatorControllerWithResistance mLauncherTransitionController;
+    private boolean mHasEndedLauncherTransition;
 
     private AnimationFactory mAnimationFactory = (t) -> { };
 
@@ -617,11 +618,11 @@
 
     /**
      * We don't want to change mLauncherTransitionController if mGestureState.getEndTarget() == HOME
-     * (it has its own animation).
+     * (it has its own animation) or if we explicitly ended the controller already.
      * @return Whether we can create the launcher controller or update its progress.
      */
     private boolean canCreateNewOrUpdateExistingLauncherTransitionController() {
-        return mGestureState.getEndTarget() != HOME;
+        return mGestureState.getEndTarget() != HOME && !mHasEndedLauncherTransition;
     }
 
     @Override
@@ -1455,6 +1456,8 @@
     }
 
     private void endLauncherTransitionController() {
+        mHasEndedLauncherTransition = true;
+
         if (mLauncherTransitionController != null) {
             // End the animation, but stay at the same visual progress.
             mLauncherTransitionController.getNormalController().dispatchSetInterpolator(
diff --git a/quickstep/src/com/android/quickstep/views/RecentsView.java b/quickstep/src/com/android/quickstep/views/RecentsView.java
index fb5fdac..d9b229a 100644
--- a/quickstep/src/com/android/quickstep/views/RecentsView.java
+++ b/quickstep/src/com/android/quickstep/views/RecentsView.java
@@ -863,10 +863,12 @@
     }
 
     public void launchSideTaskInLiveTileModeForRestartedApp(int taskId) {
-        if (mRunningTaskId != -1 && mRunningTaskId == taskId &&
-                getLiveTileParams().getTargetSet().findTask(taskId) != null) {
+        if (mRunningTaskId != -1 && mRunningTaskId == taskId) {
             RemoteAnimationTargets targets = getLiveTileParams().getTargetSet();
-            launchSideTaskInLiveTileMode(taskId, targets.apps, targets.wallpapers, targets.nonApps);
+            if (targets != null && targets.findTask(taskId) != null) {
+                launchSideTaskInLiveTileMode(taskId, targets.apps, targets.wallpapers,
+                        targets.nonApps);
+            }
         }
     }
 
@@ -1284,8 +1286,14 @@
     }
 
     private void updateOrientationHandler() {
+        updateOrientationHandler(true);
+    }
+
+    private void updateOrientationHandler(boolean forceRecreateDragLayerControllers) {
         // Handle orientation changes.
+        PagedOrientationHandler oldOrientationHandler = mOrientationHandler;
         mOrientationHandler = mOrientationState.getOrientationHandler();
+
         mIsRtl = mOrientationHandler.getRecentsRtlSetting(getResources());
         setLayoutDirection(mIsRtl
                 ? View.LAYOUT_DIRECTION_RTL
@@ -1294,7 +1302,13 @@
                 ? View.LAYOUT_DIRECTION_LTR
                 : View.LAYOUT_DIRECTION_RTL);
         mClearAllButton.setRotation(mOrientationHandler.getDegreesRotated());
-        mActivity.getDragLayer().recreateControllers();
+
+        if (forceRecreateDragLayerControllers
+                || !mOrientationHandler.equals(oldOrientationHandler)) {
+            // Changed orientations, update controllers so they intercept accordingly.
+            mActivity.getDragLayer().recreateControllers();
+        }
+
         boolean isInLandscape = mOrientationState.getTouchRotation() != ROTATION_0
                 || mOrientationState.getRecentsActivityRotation() != ROTATION_0;
         mActionsView.updateHiddenFlags(HIDDEN_NON_ZERO_ROTATION,
@@ -1614,7 +1628,7 @@
             setCurrentPage(0);
             LayoutUtils.setViewEnabled(mActionsView, true);
             if (mOrientationState.setGestureActive(false)) {
-                updateOrientationHandler();
+                updateOrientationHandler(/* forceRecreateDragLayerControllers = */ false);
             }
         });
     }
diff --git a/src/com/android/launcher3/allapps/AllAppsContainerView.java b/src/com/android/launcher3/allapps/AllAppsContainerView.java
index d1e643f..516af59 100644
--- a/src/com/android/launcher3/allapps/AllAppsContainerView.java
+++ b/src/com/android/launcher3/allapps/AllAppsContainerView.java
@@ -51,7 +51,6 @@
 import androidx.annotation.StringRes;
 import androidx.annotation.VisibleForTesting;
 import androidx.core.graphics.ColorUtils;
-import androidx.core.os.BuildCompat;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
 
@@ -188,11 +187,11 @@
         Bundle state = (Bundle) sparseArray.get(R.id.work_tab_state_id, null);
         if (state != null) {
             int currentPage = state.getInt(BUNDLE_KEY_CURRENT_PAGE, 0);
-            if (currentPage != 0) {
+            if (currentPage != 0 && mViewPager != null) {
                 mViewPager.setCurrentPage(currentPage);
                 rebindAdapters(true);
             } else {
-                mSearchUiManager.resetSearch();
+                reset(true);
             }
         }
 
@@ -260,21 +259,6 @@
         mWorkAdapterProvider.updateCurrentState(isEnabled);
     }
 
-    private void hideInput() {
-        if (!BuildCompat.isAtLeastR() || !FeatureFlags.ENABLE_DEVICE_SEARCH.get()) return;
-
-        WindowInsets insets = getRootWindowInsets();
-        if (insets == null) return;
-
-        if (insets.isVisible(WindowInsets.Type.ime())) {
-            hideIme();
-        }
-    }
-
-    protected void hideIme() {
-        getWindowInsetsController().hide(WindowInsets.Type.ime());
-    }
-
     /**
      * Returns whether the view itself will handle the touch event or not.
      */
@@ -290,7 +274,6 @@
         }
         if (rv.getScrollbar().getThumbOffsetY() >= 0 &&
                 mLauncher.getDragLayer().isEventOverView(rv.getScrollbar(), ev)) {
-            hideInput();
             return false;
         }
         return rv.shouldContainerScroll(ev, mLauncher.getDragLayer());
diff --git a/src/com/android/launcher3/allapps/LauncherAllAppsContainerView.java b/src/com/android/launcher3/allapps/LauncherAllAppsContainerView.java
index 1eb726c..f64b7cb 100644
--- a/src/com/android/launcher3/allapps/LauncherAllAppsContainerView.java
+++ b/src/com/android/launcher3/allapps/LauncherAllAppsContainerView.java
@@ -15,8 +15,6 @@
  */
 package com.android.launcher3.allapps;
 
-import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ALLAPPS_KEYBOARD_CLOSED;
-
 import android.content.Context;
 import android.graphics.Rect;
 import android.util.AttributeSet;
@@ -88,10 +86,4 @@
     public void onActivePageChanged(int currentActivePage) {
         super.onActivePageChanged(currentActivePage);
     }
-
-    @Override
-    protected void hideIme() {
-        super.hideIme();
-        mLauncher.getStatsLogManager().logger().log(LAUNCHER_ALLAPPS_KEYBOARD_CLOSED);
-    }
 }
diff --git a/src/com/android/launcher3/model/data/SearchActionItemInfo.java b/src/com/android/launcher3/model/data/SearchActionItemInfo.java
index 7ca283e..b3057d5 100644
--- a/src/com/android/launcher3/model/data/SearchActionItemInfo.java
+++ b/src/com/android/launcher3/model/data/SearchActionItemInfo.java
@@ -25,7 +25,6 @@
 
 import androidx.annotation.Nullable;
 
-import com.android.launcher3.Utilities;
 import com.android.launcher3.logger.LauncherAtom.ItemInfo;
 import com.android.launcher3.logger.LauncherAtom.SearchActionItem;
 
@@ -35,7 +34,6 @@
 public class SearchActionItemInfo extends ItemInfoWithIcon {
 
     public static final int FLAG_SHOULD_START = 1 << 1;
-    @Deprecated
     public static final int FLAG_SHOULD_START_FOR_RESULT = FLAG_SHOULD_START | 1 << 2;
     public static final int FLAG_BADGE_WITH_PACKAGE = 1 << 3;
     public static final int FLAG_PRIMARY_ICON_FROM_TITLE = 1 << 4;
@@ -91,13 +89,10 @@
      * Setter for mIntent with assertion for null value mPendingIntent
      */
     public void setIntent(Intent intent) {
-        if (mPendingIntent != null && intent != null && Utilities.IS_DEBUG_DEVICE) {
+        if (mPendingIntent != null && intent != null) {
             throw new RuntimeException(
                     "SearchActionItemInfo can only have either an Intent or a PendingIntent");
         }
-        if (intent != null) {
-            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        }
         mIntent = intent;
     }
 
@@ -109,7 +104,7 @@
      * Setter of mPendingIntent with assertion for null value mIntent
      */
     public void setPendingIntent(PendingIntent pendingIntent) {
-        if (mIntent != null && pendingIntent != null && Utilities.IS_DEBUG_DEVICE) {
+        if (mIntent != null && pendingIntent != null) {
             throw new RuntimeException(
                     "SearchActionItemInfo can only have either an Intent or a PendingIntent");
         }
diff --git a/src/com/android/launcher3/touch/ItemClickHandler.java b/src/com/android/launcher3/touch/ItemClickHandler.java
index 408ba28..b53f96e 100644
--- a/src/com/android/launcher3/touch/ItemClickHandler.java
+++ b/src/com/android/launcher3/touch/ItemClickHandler.java
@@ -262,12 +262,19 @@
      */
     public static void onClickSearchAction(Launcher launcher, SearchActionItemInfo itemInfo) {
         if (itemInfo.getIntent() != null) {
-            launcher.startActivity(itemInfo.getIntent());
+            if (itemInfo.hasFlags(SearchActionItemInfo.FLAG_SHOULD_START_FOR_RESULT)) {
+                launcher.startActivityForResult(itemInfo.getIntent(), 0);
+            } else {
+                launcher.startActivity(itemInfo.getIntent());
+            }
         } else if (itemInfo.getPendingIntent() != null) {
             try {
                 PendingIntent pendingIntent = itemInfo.getPendingIntent();
                 if (!itemInfo.hasFlags(SearchActionItemInfo.FLAG_SHOULD_START)) {
                     pendingIntent.send();
+                } else if (itemInfo.hasFlags(SearchActionItemInfo.FLAG_SHOULD_START_FOR_RESULT)) {
+                    launcher.startIntentSenderForResult(pendingIntent.getIntentSender(), 0, null, 0,
+                            0, 0);
                 } else {
                     launcher.startIntentSender(pendingIntent.getIntentSender(), null, 0, 0, 0);
                 }
diff --git a/src/com/android/launcher3/widget/DatabaseWidgetPreviewLoader.java b/src/com/android/launcher3/widget/DatabaseWidgetPreviewLoader.java
index 867c770..4ec7e60 100644
--- a/src/com/android/launcher3/widget/DatabaseWidgetPreviewLoader.java
+++ b/src/com/android/launcher3/widget/DatabaseWidgetPreviewLoader.java
@@ -417,7 +417,8 @@
             previewHeight = drawable.getIntrinsicHeight();
         } else {
             DeviceProfile dp = launcher.getDeviceProfile();
-            Size widgetSize = WidgetSizes.getWidgetSizePx(dp, spanX, spanY);
+            Size widgetSize = WidgetSizes.getWidgetPaddedSizePx(mContext, info.provider, dp, spanX,
+                    spanY);
             previewWidth = widgetSize.getWidth();
             previewHeight = widgetSize.getHeight();
         }
diff --git a/src/com/android/launcher3/widget/WidgetCell.java b/src/com/android/launcher3/widget/WidgetCell.java
index 8798332..c3edcce 100644
--- a/src/com/android/launcher3/widget/WidgetCell.java
+++ b/src/com/android/launcher3/widget/WidgetCell.java
@@ -363,7 +363,9 @@
     /** Sets the widget preview image size, in number of cells, and preview scale. */
     public Size setPreviewSize(int spanX, int spanY, float previewScale) {
         DeviceProfile deviceProfile = mActivity.getDeviceProfile();
-        Size widgetSize = WidgetSizes.getWidgetSizePx(deviceProfile, spanX, spanY);
+        Size widgetSize =
+                mItem != null ? WidgetSizes.getWidgetItemSizePx(getContext(), deviceProfile, mItem)
+                : WidgetSizes.getWidgetSizePx(deviceProfile, spanX, spanY);
         mPreviewWidth = widgetSize.getWidth();
         mPreviewHeight = widgetSize.getHeight();
         mPreviewScale = previewScale;
diff --git a/src/com/android/launcher3/widget/picker/WidgetsListAdapter.java b/src/com/android/launcher3/widget/picker/WidgetsListAdapter.java
index 08a2263..1125b82 100644
--- a/src/com/android/launcher3/widget/picker/WidgetsListAdapter.java
+++ b/src/com/android/launcher3/widget/picker/WidgetsListAdapter.java
@@ -296,11 +296,8 @@
             for (int i = 0; i < entry.mWidgets.size(); i++) {
                 WidgetItem widgetItem = entry.mWidgets.get(i);
                 DeviceProfile deviceProfile = activity.getDeviceProfile();
-                Size widgetSize =
-                        WidgetSizes.getWidgetSizePx(
-                                deviceProfile,
-                                widgetItem.spanX,
-                                widgetItem.spanY);
+                Size widgetSize = WidgetSizes.getWidgetItemSizePx(mContext, deviceProfile,
+                        widgetItem);
                 if (widgetItem.isShortcut()) {
                     widgetSize =
                             new Size(
diff --git a/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java b/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java
index 6fc6848..3800ede 100644
--- a/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java
+++ b/src/com/android/launcher3/widget/picker/WidgetsRecommendationTableLayout.java
@@ -155,8 +155,8 @@
             float rowHeight = 0;
             for (int j = 0; j < widgetItems.size(); j++) {
                 WidgetItem widgetItem = widgetItems.get(j);
-                Size widgetSize = WidgetSizes.getWidgetSizePx(
-                        deviceProfile, widgetItem.spanX, widgetItem.spanY);
+                Size widgetSize = WidgetSizes.getWidgetItemSizePx(getContext(), deviceProfile,
+                        widgetItem);
                 float previewHeight = widgetSize.getHeight() * previewScale;
                 rowHeight = Math.max(rowHeight,
                         previewHeight + mWidgetCellTextViewsHeight + mWidgetCellVerticalPadding);
diff --git a/src/com/android/launcher3/widget/util/WidgetSizes.java b/src/com/android/launcher3/widget/util/WidgetSizes.java
index 5c8ea72..e2c84b5 100644
--- a/src/com/android/launcher3/widget/util/WidgetSizes.java
+++ b/src/com/android/launcher3/widget/util/WidgetSizes.java
@@ -17,9 +17,7 @@
 
 import static android.appwidget.AppWidgetHostView.getDefaultPaddingForWidget;
 
-import static com.android.launcher3.Utilities.ATLEAST_S;
 
-import android.annotation.SuppressLint;
 import android.appwidget.AppWidgetHostView;
 import android.appwidget.AppWidgetManager;
 import android.content.ComponentName;
@@ -34,24 +32,34 @@
 
 import com.android.launcher3.DeviceProfile;
 import com.android.launcher3.LauncherAppState;
+import com.android.launcher3.model.WidgetItem;
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.stream.Collectors;
 
 /** A utility class for widget sizes related calculations. */
 public final class WidgetSizes {
 
     /**
      * Returns the list of all possible sizes, in dp, for a widget of given spans on this device.
+     *
+     * <p>The returned sizes already take into account the system padding, and whether it is applied
+     * or not in that specific configuration.
      */
-    public static ArrayList<SizeF> getWidgetSizes(Context context, int spanX, int spanY) {
+    public static ArrayList<SizeF> getWidgetPaddedSizes(Context context, ComponentName provider,
+            int spanX, int spanY) {
+        Rect padding = getDefaultPaddingForWidget(context, provider, /* padding= */ null);
+
         ArrayList<SizeF> sizes = new ArrayList<>(2);
         final float density = context.getResources().getDisplayMetrics().density;
         final Point cellSize = new Point();
 
         for (DeviceProfile profile : LauncherAppState.getIDP(context).supportedProfiles) {
             Size widgetSizePx = getWidgetSizePx(profile, spanX, spanY, cellSize);
+            if (!profile.shouldInsetWidgets()) {
+                widgetSizePx = new Size(widgetSizePx.getWidth() - padding.left - padding.right,
+                        widgetSizePx.getHeight() - padding.top - padding.bottom);
+            }
             sizes.add(new SizeF(widgetSizePx.getWidth() / density,
                     widgetSizePx.getHeight() / density));
         }
@@ -63,6 +71,32 @@
         return getWidgetSizePx(profile, spanX, spanY, /* recycledCellSize= */ null);
     }
 
+    /**
+     * Returns the size, in pixels and removing padding, a widget of given spans & {@code profile}.
+     */
+    public static Size getWidgetPaddedSizePx(Context context, ComponentName component,
+            DeviceProfile profile, int spanX, int spanY) {
+        Size size = getWidgetSizePx(profile, spanX, spanY);
+        if (profile.shouldInsetWidgets()) {
+            return size;
+        }
+        Rect padding = getDefaultPaddingForWidget(context, component, /* padding= */ null);
+        return new Size(size.getWidth() - padding.left - padding.right,
+                size.getHeight() - padding.top - padding.bottom);
+    }
+
+    /**
+     * Returns the size of a WidgetItem.
+     */
+    public static Size getWidgetItemSizePx(Context context, DeviceProfile profile,
+            WidgetItem widgetItem) {
+        if (widgetItem.isShortcut()) {
+            return getWidgetSizePx(profile, widgetItem.spanX, widgetItem.spanY);
+        }
+        return getWidgetPaddedSizePx(context, widgetItem.componentName, profile, widgetItem.spanX,
+                widgetItem.spanY);
+    }
+
     private static Size getWidgetSizePx(DeviceProfile profile, int spanX, int spanY,
             @Nullable Point recycledCellSize) {
         final int hBorderSpacing = (spanX - 1) * profile.cellLayoutBorderSpacingPx;
@@ -81,17 +115,22 @@
      * <p>On Android S+, it also updates the given {@code widgetView} with a list of sizes derived
      * from {@code spanX}, {@code spanY} in all supported device profiles.
      */
-    @SuppressLint("NewApi") // Already added API check.
     public static void updateWidgetSizeRanges(AppWidgetHostView widgetView, Context context,
             int spanX, int spanY) {
-        List<SizeF> sizes = getWidgetSizes(context, spanX, spanY);
-        if (ATLEAST_S) {
-            widgetView.updateAppWidgetSize(new Bundle(), sizes);
-        } else {
-            Rect bounds = getMinMaxSizes(sizes);
-            widgetView.updateAppWidgetSize(new Bundle(), bounds.left, bounds.top, bounds.right,
-                    bounds.bottom);
+        AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
+        int widgetId = widgetView.getAppWidgetId();
+        if (widgetId <= 0) {
+            return;
         }
+        Bundle sizeOptions = getWidgetSizeOptions(context, widgetView.getAppWidgetInfo().provider,
+                spanX, spanY);
+        if (sizeOptions.<SizeF>getParcelableArrayList(
+                AppWidgetManager.OPTION_APPWIDGET_SIZES).equals(
+                widgetManager.getAppWidgetOptions(widgetId).<SizeF>getParcelableArrayList(
+                        AppWidgetManager.OPTION_APPWIDGET_SIZES))) {
+            return;
+        }
+        widgetManager.updateAppWidgetOptions(widgetId, sizeOptions);
     }
 
     /**
@@ -99,17 +138,7 @@
      */
     public static Bundle getWidgetSizeOptions(Context context, ComponentName provider, int spanX,
             int spanY) {
-        ArrayList<SizeF> sizes = getWidgetSizes(context, spanX, spanY);
-        Rect padding = getDefaultPaddingForWidget(context, provider, null);
-        float density = context.getResources().getDisplayMetrics().density;
-        float xPaddingDips = (padding.left + padding.right) / density;
-        float yPaddingDips = (padding.top + padding.bottom) / density;
-
-        ArrayList<SizeF> paddedSizes = sizes.stream()
-                .map(size -> new SizeF(
-                        Math.max(0.f, size.getWidth() - xPaddingDips),
-                        Math.max(0.f, size.getHeight() - yPaddingDips)))
-                .collect(Collectors.toCollection(ArrayList::new));
+        ArrayList<SizeF> paddedSizes = getWidgetPaddedSizes(context, provider, spanX, spanY);
 
         Rect rect = getMinMaxSizes(paddedSizes);
         Bundle options = new Bundle();