Add work toggle to work apps paused message

This also fixes scrolling issues in landscape

Bug: 186857875
Bug: 161929312
Bug: 189983516
Bug: 190567199
screenshot: https://screenshot.googleplex.com/BCjcyJFqJo6W4Tr
Test: local
Change-Id: I7bcf23cc4be2fca936eba41342731789eb8f3292
diff --git a/res/layout/work_apps_edu.xml b/res/layout/work_apps_edu.xml
index 2c58907..ffbf962 100644
--- a/res/layout/work_apps_edu.xml
+++ b/res/layout/work_apps_edu.xml
@@ -16,30 +16,38 @@
 <com.android.launcher3.allapps.WorkEduCard xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
-    android:padding="@dimen/work_edu_card_margin"
-    android:orientation="vertical"
-    android:background="@drawable/work_card"
     android:gravity="center">
 
-    <TextView
-        style="@style/PrimaryHeadline"
-        android:textColor="?android:attr/textColorPrimary"
-        android:id="@+id/work_apps_paused_title"
-        android:layout_width="wrap_content"
+    <LinearLayout
+        android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:layout_marginTop="8dp"
-        android:layout_marginBottom="8dp"
-        android:text="@string/work_profile_edu_work_apps"
-        android:textAlignment="center"
-        android:textSize="20sp" />
+        android:orientation="vertical"
+        android:padding="@dimen/work_edu_card_margin"
+        android:background="@drawable/work_card"
+        android:layout_gravity="center_horizontal"
+        android:gravity="center"
+        android:id="@+id/wrapper">
 
-    <Button
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:id="@+id/action_btn"
-        android:textColor="?attr/workProfileOverlayTextColor"
-        android:text="@string/work_profile_edu_accept"
-        android:textAlignment="center"
-        android:background="@drawable/work_card_btn"
-        android:textSize="14sp" />
+        <TextView
+            style="@style/PrimaryHeadline"
+            android:textColor="?android:attr/textColorPrimary"
+            android:id="@+id/work_apps_paused_title"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginTop="8dp"
+            android:layout_marginBottom="8dp"
+            android:text="@string/work_profile_edu_work_apps"
+            android:textAlignment="center"
+            android:textSize="20sp" />
+
+        <Button
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:id="@+id/action_btn"
+            android:textColor="?attr/workProfileOverlayTextColor"
+            android:text="@string/work_profile_edu_accept"
+            android:textAlignment="center"
+            android:background="@drawable/work_card_btn"
+            android:textSize="14sp" />
+    </LinearLayout>
 </com.android.launcher3.allapps.WorkEduCard>
\ No newline at end of file
diff --git a/res/layout/work_apps_paused.xml b/res/layout/work_apps_paused.xml
index 7f1107f..14dcb8c 100644
--- a/res/layout/work_apps_paused.xml
+++ b/res/layout/work_apps_paused.xml
@@ -12,11 +12,10 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<com.android.launcher3.allapps.WorkPausedCard xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:background="?attr/allAppsScrimColor"
-    android:padding="48dp"
+    android:layout_height="wrap_content"
+    android:padding="@dimen/work_edu_card_margin"
     android:orientation="vertical"
     android:gravity="center">
 
@@ -39,5 +38,16 @@
         android:textColor="?attr/workProfileOverlayTextColor"
         android:text="@string/work_apps_paused_body"
         android:textAlignment="center"
+        android:layout_marginBottom="8dp"
         android:textSize="16sp" />
-</LinearLayout>
\ No newline at end of file
+
+    <Button
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:id="@+id/enable_work_apps"
+        android:textColor="?attr/workProfileOverlayTextColor"
+        android:text="@string/work_apps_enable_btn_text"
+        android:textAlignment="center"
+        android:background="@drawable/work_card_btn"
+        android:textSize="14sp" />
+</com.android.launcher3.allapps.WorkPausedCard>
\ No newline at end of file
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 430a1ab..44c864b 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -94,7 +94,7 @@
     <dimen name="all_apps_background_canvas_height">475dp</dimen>
     <dimen name="all_apps_header_pill_height">48dp</dimen>
     <dimen name="all_apps_header_pill_corner_radius">18dp</dimen>
-    <dimen name="all_apps_header_pills_width">320dp</dimen>
+    <dimen name="all_apps_header_pills_width">348dp</dimen>
     <dimen name="all_apps_header_tab_height">48dp</dimen>
     <dimen name="all_apps_tabs_indicator_height">2dp</dimen>
     <dimen name="all_apps_header_top_padding">36dp</dimen>
diff --git a/src/com/android/launcher3/allapps/AllAppsContainerView.java b/src/com/android/launcher3/allapps/AllAppsContainerView.java
index d749e02..7003df1 100644
--- a/src/com/android/launcher3/allapps/AllAppsContainerView.java
+++ b/src/com/android/launcher3/allapps/AllAppsContainerView.java
@@ -23,7 +23,6 @@
 
 import android.animation.ValueAnimator;
 import android.content.Context;
-import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.graphics.Canvas;
 import android.graphics.Color;
@@ -494,15 +493,6 @@
         }
     }
 
-    @Override
-    protected void onConfigurationChanged(Configuration newConfig) {
-        super.onConfigurationChanged(newConfig);
-        View overlay = mAH[AdapterHolder.WORK].getOverlayView();
-        int v = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ? GONE : VISIBLE;
-        overlay.findViewById(R.id.work_apps_paused_title).setVisibility(v);
-        overlay.findViewById(R.id.work_apps_paused_content).setVisibility(v);
-    }
-
     private void replaceRVContainer(boolean showTabs) {
         for (int i = 0; i < mAH.length; i++) {
             if (mAH[i].recyclerView != null) {
@@ -544,9 +534,6 @@
                     && mAllAppsStore.hasModelFlag(
                     FLAG_HAS_SHORTCUT_PERMISSION | FLAG_QUIET_MODE_CHANGE_PERMISSION));
         }
-        if (mSearchUiManager != null && mSearchUiManager.getEditText() != null) {
-            mSearchUiManager.getEditText().hideKeyboard();
-        }
     }
 
     // Used by tests only
@@ -704,7 +691,9 @@
         mHeaderPaint.setColor(mHeaderColor);
         mHeaderPaint.setAlpha((int) (getAlpha() * Color.alpha(mHeaderColor)));
         if (mHeaderPaint.getColor() != mScrimColor && mHeaderPaint.getColor() != 0) {
-            canvas.drawRect(0, 0, getWidth(), mSearchContainer.getTop() + getTranslationY(),
+            int bottom = mUsingTabs && mHeader.mHeaderCollapsed ? mHeader.getVisibleBottomBound()
+                    : mSearchContainer.getBottom();
+            canvas.drawRect(0, 0, getWidth(), bottom + getTranslationY(),
                     mHeaderPaint);
         }
     }
@@ -781,13 +770,6 @@
             mAH[AdapterHolder.MAIN].recyclerView.setVerticalFadingEdgeEnabled(!mUsingTabs
                     && verticalFadingEdge);
         }
-
-        private View getOverlayView() {
-            if (mOverlay == null) {
-                mOverlay = mLauncher.getLayoutInflater().inflate(R.layout.work_apps_paused, null);
-            }
-            return mOverlay;
-        }
     }
 
 
diff --git a/src/com/android/launcher3/allapps/AllAppsRecyclerView.java b/src/com/android/launcher3/allapps/AllAppsRecyclerView.java
index 5bbd02b..4ad694b 100644
--- a/src/com/android/launcher3/allapps/AllAppsRecyclerView.java
+++ b/src/com/android/launcher3/allapps/AllAppsRecyclerView.java
@@ -447,4 +447,14 @@
     public boolean hasOverlappingRendering() {
         return false;
     }
+
+    /**
+     * Returns distance between left and right app icons
+     */
+    public int getTabWidth() {
+        DeviceProfile grid = BaseDraggingActivity.fromContext(getContext()).getDeviceProfile();
+        int totalWidth = (grid.availableWidthPx - getPaddingLeft() - getPaddingRight());
+        int iconPadding = totalWidth / grid.numShownAllAppsColumns - grid.allAppsIconSizePx;
+        return totalWidth - iconPadding;
+    }
 }
diff --git a/src/com/android/launcher3/allapps/FloatingHeaderView.java b/src/com/android/launcher3/allapps/FloatingHeaderView.java
index 450d2e2..f1381e9 100644
--- a/src/com/android/launcher3/allapps/FloatingHeaderView.java
+++ b/src/com/android/launcher3/allapps/FloatingHeaderView.java
@@ -72,8 +72,13 @@
                     }
 
                     int current = -mCurrentRV.getCurrentScrollY();
+                    boolean headerCollapsed = mHeaderCollapsed;
                     moved(current);
                     applyVerticalMove();
+                    if (headerCollapsed != mHeaderCollapsed) {
+                        AllAppsContainerView parent = (AllAppsContainerView) getParent();
+                        parent.invalidateHeader();
+                    }
                 }
             };
 
@@ -219,6 +224,8 @@
 
         mTabsHidden = tabsHidden;
         mTabLayout.setVisibility(tabsHidden ? View.GONE : View.VISIBLE);
+        mTabLayout.getLayoutParams().width =
+                mAH[AllAppsContainerView.AdapterHolder.MAIN].recyclerView.getTabWidth();
         mMainRV = setupRV(mMainRV, mAH[AllAppsContainerView.AdapterHolder.MAIN].recyclerView);
         mWorkRV = setupRV(mWorkRV, mAH[AllAppsContainerView.AdapterHolder.WORK].recyclerView);
         mParent = (ViewGroup) mMainRV.getParent();
@@ -429,6 +436,13 @@
         }
         return null;
     }
+
+    /**
+     * Returns visible height of FloatingHeaderView contents
+     */
+    public int getVisibleBottomBound() {
+        return getBottom() + mTranslationY;
+    }
 }
 
 
diff --git a/src/com/android/launcher3/allapps/WorkEduCard.java b/src/com/android/launcher3/allapps/WorkEduCard.java
index 29a42f8..9db7bf0 100644
--- a/src/com/android/launcher3/allapps/WorkEduCard.java
+++ b/src/com/android/launcher3/allapps/WorkEduCard.java
@@ -21,7 +21,7 @@
 import android.view.ViewGroup;
 import android.view.animation.Animation;
 import android.view.animation.AnimationUtils;
-import android.widget.LinearLayout;
+import android.widget.FrameLayout;
 
 import com.android.launcher3.Launcher;
 import com.android.launcher3.R;
@@ -29,7 +29,7 @@
 /**
  * Work profile toggle switch shown at the bottom of AllApps work tab
  */
-public class WorkEduCard extends LinearLayout implements View.OnClickListener,
+public class WorkEduCard extends FrameLayout implements View.OnClickListener,
         Animation.AnimationListener {
 
     private final Launcher mLauncher;
@@ -52,11 +52,24 @@
         mDismissAnim.setAnimationListener(this);
     }
 
+    @Override
+    protected void onAttachedToWindow() {
+        super.onAttachedToWindow();
+        mDismissAnim.reset();
+    }
+
+    @Override
+    protected void onDetachedFromWindow() {
+        super.onDetachedFromWindow();
+        mDismissAnim.cancel();
+    }
 
     @Override
     protected void onFinishInflate() {
         super.onFinishInflate();
         findViewById(R.id.action_btn).setOnClickListener(this);
+        MarginLayoutParams lp = ((MarginLayoutParams) findViewById(R.id.wrapper).getLayoutParams());
+        lp.width = mLauncher.getAppsView().getActiveRecyclerView().getTabWidth();
     }
 
     @Override
diff --git a/src/com/android/launcher3/allapps/WorkModeSwitch.java b/src/com/android/launcher3/allapps/WorkModeSwitch.java
index c22cd63..866694f 100644
--- a/src/com/android/launcher3/allapps/WorkModeSwitch.java
+++ b/src/com/android/launcher3/allapps/WorkModeSwitch.java
@@ -31,7 +31,6 @@
 import androidx.annotation.RequiresApi;
 
 import com.android.launcher3.Insettable;
-import com.android.launcher3.R;
 import com.android.launcher3.Utilities;
 import com.android.launcher3.pm.UserCache;
 
@@ -79,7 +78,9 @@
         clearAnimation();
         if (workTabVisible) {
             setEnabled(true);
-            setVisibility(VISIBLE);
+            if (mWorkEnabled) {
+                setVisibility(VISIBLE);
+            }
             setAlpha(0);
             animate().alpha(1).start();
         } else {
@@ -91,7 +92,7 @@
     public void onClick(View view) {
         if (Utilities.ATLEAST_P) {
             setEnabled(false);
-            UI_HELPER_EXECUTOR.post(() -> setToState(!mWorkEnabled));
+            UI_HELPER_EXECUTOR.post(() -> setWorkProfileEnabled(getContext(), false));
         }
     }
 
@@ -101,20 +102,18 @@
     public void updateCurrentState(boolean active) {
         mWorkEnabled = active;
         setEnabled(true);
-        setCompoundDrawablesRelativeWithIntrinsicBounds(
-                active ? R.drawable.ic_corp_off : R.drawable.ic_corp, 0, 0, 0);
-        setText(active ? R.string.work_apps_pause_btn_text : R.string.work_apps_enable_btn_text);
+        setVisibility(active ? VISIBLE : GONE);
     }
 
     @RequiresApi(Build.VERSION_CODES.P)
-    protected Boolean setToState(boolean toState) {
-        UserManager userManager = getContext().getSystemService(UserManager.class);
+    public static Boolean setWorkProfileEnabled(Context context, boolean enabled) {
+        UserManager userManager = context.getSystemService(UserManager.class);
         boolean showConfirm = false;
-        for (UserHandle userProfile : UserCache.INSTANCE.get(getContext()).getUserProfiles()) {
+        for (UserHandle userProfile : UserCache.INSTANCE.get(context).getUserProfiles()) {
             if (Process.myUserHandle().equals(userProfile)) {
                 continue;
             }
-            showConfirm |= !userManager.requestQuietModeEnabled(!toState, userProfile);
+            showConfirm |= !userManager.requestQuietModeEnabled(!enabled, userProfile);
         }
         return showConfirm;
     }
diff --git a/src/com/android/launcher3/allapps/WorkPausedCard.java b/src/com/android/launcher3/allapps/WorkPausedCard.java
new file mode 100644
index 0000000..3955a9a
--- /dev/null
+++ b/src/com/android/launcher3/allapps/WorkPausedCard.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2020 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.allapps;
+
+import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
+
+import android.content.Context;
+import android.content.res.Configuration;
+import android.util.AttributeSet;
+import android.view.View;
+import android.widget.Button;
+import android.widget.LinearLayout;
+
+import com.android.launcher3.Launcher;
+import com.android.launcher3.R;
+import com.android.launcher3.Utilities;
+
+/**
+ * Work profile toggle switch shown at the bottom of AllApps work tab
+ */
+public class WorkPausedCard extends LinearLayout implements View.OnClickListener {
+
+    private final Launcher mLauncher;
+    private Button mBtn;
+
+    public WorkPausedCard(Context context) {
+        this(context, null, 0);
+    }
+
+    public WorkPausedCard(Context context, AttributeSet attrs) {
+        this(context, attrs, 0);
+    }
+
+    public WorkPausedCard(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+        mLauncher = Launcher.getLauncher(getContext());
+    }
+
+
+    @Override
+    protected void onFinishInflate() {
+        super.onFinishInflate();
+        mBtn = findViewById(R.id.enable_work_apps);
+        mBtn.setOnClickListener(this);
+    }
+
+    @Override
+    public void onClick(View view) {
+        if (Utilities.ATLEAST_P) {
+            setEnabled(false);
+            UI_HELPER_EXECUTOR.post(() -> WorkModeSwitch.setWorkProfileEnabled(getContext(), true));
+        }
+    }
+
+    @Override
+    protected void onLayout(boolean changed, int l, int t, int r, int b) {
+        int orientation = getResources().getConfiguration().orientation;
+        getLayoutParams().height = orientation == Configuration.ORIENTATION_PORTRAIT
+                ? LayoutParams.MATCH_PARENT : LayoutParams.WRAP_CONTENT;
+        super.onLayout(changed, l, t, r, b);
+    }
+}