Enable support for same page hotseat migration

Bug: 142753423
Test: Manual
Change-Id: Ib53a64629a595c412e30ca5ff46077efc73a3f3e
diff --git a/quickstep/recents_ui_overrides/src/com/android/launcher3/hybridhotseat/HotseatEduDialog.java b/quickstep/recents_ui_overrides/src/com/android/launcher3/hybridhotseat/HotseatEduDialog.java
index 8926246..538b7f3 100644
--- a/quickstep/recents_ui_overrides/src/com/android/launcher3/hybridhotseat/HotseatEduDialog.java
+++ b/quickstep/recents_ui_overrides/src/com/android/launcher3/hybridhotseat/HotseatEduDialog.java
@@ -26,6 +26,7 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.Button;
+import android.widget.TextView;
 import android.widget.Toast;
 
 import com.android.launcher3.CellLayout;
@@ -34,7 +35,9 @@
 import com.android.launcher3.Launcher;
 import com.android.launcher3.R;
 import com.android.launcher3.WorkspaceItemInfo;
+import com.android.launcher3.WorkspaceLayoutManager;
 import com.android.launcher3.anim.Interpolators;
+import com.android.launcher3.config.FeatureFlags;
 import com.android.launcher3.logging.UserEventDispatcher;
 import com.android.launcher3.uioverrides.PredictedAppIcon;
 import com.android.launcher3.userevent.nano.LauncherLogProto;
@@ -49,12 +52,24 @@
 public class HotseatEduDialog extends AbstractSlideInView implements Insettable {
 
     private static final int DEFAULT_CLOSE_DURATION = 200;
+    protected static final int FINAL_SCRIM_BG_COLOR = 0x88000000;
 
-    public static boolean shown = false;
+    // We don't migrate if user has more than SAME_PAGE_MAX_ROWS rows of item in their screen
+    private static final int SAME_PAGE_MAX_ROWS = 2;
+
+    private static final int MIGRATE_SAME_PAGE = 0;
+    private static final int MIGRATE_NEW_PAGE = 1;
+    private static final int MIGRATE_NO_MIGRATE = 2;
+
 
     private final Rect mInsets = new Rect();
     private View mHotseatWrapper;
     private CellLayout mSampleHotseat;
+    private TextView mEduHeading;
+    private TextView mEduContent;
+    private Button mDismissBtn;
+
+    private int mMigrationMode = MIGRATE_SAME_PAGE;
 
     public void setHotseatEduController(HotseatEduController hotseatEduController) {
         mHotseatEduController = hotseatEduController;
@@ -78,6 +93,8 @@
         super.onFinishInflate();
         mHotseatWrapper = findViewById(R.id.hotseat_wrapper);
         mSampleHotseat = findViewById(R.id.sample_prediction);
+        mEduHeading = findViewById(R.id.hotseat_edu_heading);
+        mEduContent = findViewById(R.id.hotseat_edu_content);
 
         DeviceProfile grid = mLauncher.getDeviceProfile();
         Rect padding = grid.getHotseatLayoutPadding();
@@ -87,24 +104,27 @@
         mSampleHotseat.setPadding(padding.left, 0, padding.right, 0);
 
         Button turnOnBtn = findViewById(R.id.turn_predictions_on);
-        turnOnBtn.setOnClickListener(this::onMigrate);
+        turnOnBtn.setOnClickListener(this::onAccept);
 
-        Button learnMoreBtn = findViewById(R.id.no_thanks);
-        learnMoreBtn.setOnClickListener(this::onKeepDefault);
+        mDismissBtn = findViewById(R.id.no_thanks);
+        mDismissBtn.setOnClickListener(this::onDismiss);
 
     }
 
-    private void onMigrate(View v) {
-        if (mHotseatEduController == null) return;
+    private void onAccept(View v) {
+        if (mMigrationMode == MIGRATE_NO_MIGRATE || !mHotseatEduController.migrate()) {
+            onDismiss(v);
+            return;
+        }
         handleClose(true);
-        mHotseatEduController.migrate();
         mHotseatEduController.finishOnboarding();
         logUserAction(true);
-        Toast.makeText(mLauncher, R.string.hotseat_items_migrated, Toast.LENGTH_LONG).show();
+        int toastStringRes = mMigrationMode == MIGRATE_SAME_PAGE
+                ? R.string.hotseat_items_migrated : R.string.hotseat_items_migrated_alt;
+        Toast.makeText(mLauncher, toastStringRes, Toast.LENGTH_LONG).show();
     }
 
-    private void onKeepDefault(View v) {
-        if (mHotseatEduController == null) return;
+    private void onDismiss(View v) {
         Toast.makeText(getContext(), R.string.hotseat_no_migration, Toast.LENGTH_LONG).show();
         mHotseatEduController.finishOnboarding();
         logUserAction(false);
@@ -148,6 +168,8 @@
         target.tipType = LauncherLogProto.TipType.HYBRID_HOTSEAT;
         target.controlType = migrated ? LauncherLogProto.ControlType.HYBRID_HOTSEAT_ACCEPTED
                 : HYBRID_HOTSEAT_CANCELED;
+        // encoding migration type on pageIndex
+        target.pageIndex = mMigrationMode;
         LauncherLogProto.LauncherEvent event = newLauncherEvent(action, target);
         UserEventDispatcher.newInstance(getContext()).dispatchUserEvent(event, null);
     }
@@ -161,6 +183,7 @@
         LauncherLogProto.LauncherEvent event = newLauncherEvent(action, target);
         UserEventDispatcher.newInstance(getContext()).dispatchUserEvent(event, null);
     }
+
     private void animateOpen() {
         if (mIsOpen || mOpenCloseAnimator.isRunning()) {
             return;
@@ -183,17 +206,12 @@
         handleClose(false);
     }
 
-    /**
-     * Opens User education dialog with a list of suggested apps
-     */
-    public void show(List<WorkspaceItemInfo> predictions) {
-        if (getParent() != null
-                || predictions.size() < mLauncher.getDeviceProfile().inv.numHotseatIcons) {
-            return;
-        }
-        attachToContainer();
-        logOnBoardingSeen();
-        animateOpen();
+    @Override
+    protected int getScrimColor(Context context) {
+        return FINAL_SCRIM_BG_COLOR;
+    }
+
+    private void populatePreview(List<WorkspaceItemInfo> predictions) {
         for (int i = 0; i < mLauncher.getDeviceProfile().inv.numHotseatIcons; i++) {
             WorkspaceItemInfo info = predictions.get(i);
             PredictedAppIcon icon = PredictedAppIcon.createIcon(mSampleHotseat, info);
@@ -204,6 +222,43 @@
         }
     }
 
+    @Override
+    protected void attachToContainer() {
+        super.attachToContainer();
+        if (FeatureFlags.HOTSEAT_MIGRATE_NEW_PAGE.get()) {
+            mEduContent.setText(R.string.hotseat_edu_message_migrate_alt);
+            mMigrationMode = MIGRATE_NEW_PAGE;
+            return;
+        }
+        CellLayout page = mLauncher.getWorkspace().getScreenWithId(
+                WorkspaceLayoutManager.FIRST_SCREEN_ID);
+
+        int maxItemsOnPage = SAME_PAGE_MAX_ROWS * mLauncher.getDeviceProfile().inv.numColumns
+                + (FeatureFlags.QSB_ON_FIRST_SCREEN ? 1 : 0);
+        if (page.getShortcutsAndWidgets().getChildCount() > maxItemsOnPage
+                || !page.makeSpaceForHotseatMigration(false)) {
+            mMigrationMode = MIGRATE_NO_MIGRATE;
+            mEduContent.setText(R.string.hotseat_edu_message_no_migrate);
+            mEduHeading.setText(R.string.hotseat_edu_title_no_migrate);
+            mDismissBtn.setVisibility(GONE);
+        }
+    }
+
+    /**
+     * Opens User education dialog with a list of suggested apps
+     */
+    public void show(List<WorkspaceItemInfo> predictions) {
+        if (getParent() != null
+                || predictions.size() < mLauncher.getDeviceProfile().inv.numHotseatIcons
+                || mHotseatEduController == null) {
+            return;
+        }
+        attachToContainer();
+        logOnBoardingSeen();
+        animateOpen();
+        populatePreview(predictions);
+    }
+
     /**
      * Factory method for HotseatPredictionUserEdu dialog
      */