Merge "Update bottom widget picker and pin widget dialog layout in landscape mode." into sc-dev
diff --git a/res/layout/add_item_confirmation_activity.xml b/res/layout/add_item_confirmation_activity.xml
index 9439baf..1aeda50 100644
--- a/res/layout/add_item_confirmation_activity.xml
+++ b/res/layout/add_item_confirmation_activity.xml
@@ -26,79 +26,82 @@
     android:importantForAccessibility="no">
 
     <com.android.launcher3.widget.AddItemWidgetsBottomSheet
-        xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/add_item_bottom_sheet"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:background="@drawable/add_item_dialog_background"
-        android:paddingTop="24dp"
         android:theme="?attr/widgetsTheme"
         android:layout_gravity="bottom"
         android:orientation="vertical">
 
-        <TextView
-            style="@style/TextHeadline"
-            android:id="@+id/widget_appName"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:gravity="center_horizontal"
-            android:paddingHorizontal="24dp"
-            android:textColor="?android:attr/textColorPrimary"
-            android:textSize="24sp"
-            android:ellipsize="end"
-            android:fadingEdge="horizontal"
-            android:singleLine="true"
-            android:maxLines="1" />
-
-        <TextView
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:gravity="center_horizontal"
-            android:paddingHorizontal="24dp"
-            android:paddingTop="8dp"
-            android:text="@string/add_item_request_drag_hint"
-            android:textSize="14sp"
-            android:textColor="?android:attr/textColorSecondary"
-            android:alpha="0.7"
-            android:importantForAccessibility="no"/>
-
-        <include layout="@layout/widget_cell"
-            android:id="@+id/widget_cell"
-            android:layout_width="match_parent"
-            android:layout_height="0dp"
-            android:layout_weight="1"
-            android:layout_marginVertical="16dp" />
-
         <LinearLayout
+            android:id="@+id/add_item_bottom_sheet_content"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:gravity="center_vertical|end"
-            android:paddingHorizontal="24dp"
-            android:paddingVertical="8dp"
-            android:orientation="horizontal">
-            <Button
-                style="@style/Button.FullRounded.Colored"
-                android:layout_width="wrap_content"
-                android:layout_height="36dp"
-                android:paddingHorizontal="16dp"
-                android:textSize="14sp"
-                android:textColor="@color/button_text"
-                android:text="@android:string/cancel"
-                android:onClick="onCancelClick"/>
+            android:padding="24dp"
+            android:background="@drawable/add_item_dialog_background"
+            android:orientation="vertical" >
 
-            <Space
-                android:layout_width="8dp"
-                android:layout_height="wrap_content" />
+            <TextView
+                style="@style/TextHeadline"
+                android:id="@+id/widget_appName"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:gravity="center_horizontal"
+                android:textColor="?android:attr/textColorPrimary"
+                android:textSize="24sp"
+                android:ellipsize="end"
+                android:fadingEdge="horizontal"
+                android:singleLine="true"
+                android:maxLines="1" />
 
-            <Button
-                style="@style/Button.FullRounded.Colored"
-                android:layout_width="wrap_content"
-                android:layout_height="36dp"
-                android:paddingHorizontal="16dp"
+            <TextView
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:gravity="center_horizontal"
+                android:paddingTop="8dp"
+                android:text="@string/add_item_request_drag_hint"
                 android:textSize="14sp"
-                android:textColor="@color/button_text"
-                android:text="@string/add_to_home_screen"
-                android:onClick="onPlaceAutomaticallyClick"/>
+                android:textColor="?android:attr/textColorSecondary"
+                android:alpha="0.7"
+                android:importantForAccessibility="no"/>
+
+            <include layout="@layout/widget_cell"
+                android:id="@+id/widget_cell"
+                android:layout_width="match_parent"
+                android:layout_height="0dp"
+                android:layout_weight="1"
+                android:layout_marginVertical="16dp" />
+
+            <LinearLayout
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:gravity="center_vertical|end"
+                android:paddingVertical="8dp"
+                android:orientation="horizontal">
+                <Button
+                    style="@style/Button.FullRounded.Colored"
+                    android:layout_width="wrap_content"
+                    android:layout_height="36dp"
+                    android:paddingHorizontal="16dp"
+                    android:textSize="14sp"
+                    android:textColor="@color/button_text"
+                    android:text="@android:string/cancel"
+                    android:onClick="onCancelClick"/>
+
+                <Space
+                    android:layout_width="8dp"
+                    android:layout_height="wrap_content" />
+
+                <Button
+                    style="@style/Button.FullRounded.Colored"
+                    android:layout_width="wrap_content"
+                    android:layout_height="36dp"
+                    android:paddingHorizontal="16dp"
+                    android:textSize="14sp"
+                    android:textColor="@color/button_text"
+                    android:text="@string/add_to_home_screen"
+                    android:onClick="onPlaceAutomaticallyClick"/>
+            </LinearLayout>
         </LinearLayout>
     </com.android.launcher3.widget.AddItemWidgetsBottomSheet>
 
diff --git a/res/layout/widgets_bottom_sheet.xml b/res/layout/widgets_bottom_sheet.xml
index 1859bd8..bbb08fa 100644
--- a/res/layout/widgets_bottom_sheet.xml
+++ b/res/layout/widgets_bottom_sheet.xml
@@ -19,8 +19,6 @@
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
-    android:paddingTop="16dp"
-    android:background="@drawable/widgets_bottom_sheet_background"
     android:layout_gravity="bottom"
     android:theme="?attr/widgetsTheme">
 
diff --git a/res/layout/widgets_bottom_sheet_content.xml b/res/layout/widgets_bottom_sheet_content.xml
index 85c6488..3b3ff8b 100644
--- a/res/layout/widgets_bottom_sheet_content.xml
+++ b/res/layout/widgets_bottom_sheet_content.xml
@@ -14,32 +14,40 @@
      limitations under the License.
 -->
 <merge xmlns:android="http://schemas.android.com/apk/res/android">
-    <View
-        android:id="@+id/collapse_handle"
-        android:layout_width="48dp"
-        android:layout_height="2dp"
-        android:layout_gravity="center_horizontal"
-        android:layout_marginBottom="16dp"
-        android:visibility="gone"
-        android:background="?android:attr/textColorSecondary"/>
-    <TextView
-        style="@style/TextHeadline"
-        android:id="@+id/title"
+    <LinearLayout
+        android:id="@+id/widgets_bottom_sheet"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:gravity="center_horizontal"
-        android:textColor="?android:attr/textColorPrimary"
-        android:textSize="24sp"/>
-
-    <ScrollView
-        android:id="@+id/widgets_table_scroll_view"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:fadeScrollbars="false"
-        android:layout_marginVertical="16dp">
-        <include layout="@layout/widgets_table_container"
+        android:background="@drawable/widgets_bottom_sheet_background"
+        android:paddingTop="16dp"
+        android:orientation="vertical">
+        <View
+            android:id="@+id/collapse_handle"
+            android:layout_width="48dp"
+            android:layout_height="2dp"
+            android:layout_gravity="center_horizontal"
+            android:layout_marginBottom="16dp"
+            android:visibility="gone"
+            android:background="?android:attr/textColorSecondary"/>
+        <TextView
+            style="@style/TextHeadline"
+            android:id="@+id/title"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:layout_gravity="center_horizontal" />
-    </ScrollView>
+            android:gravity="center_horizontal"
+            android:textColor="?android:attr/textColorPrimary"
+            android:textSize="24sp"/>
+
+        <ScrollView
+            android:id="@+id/widgets_table_scroll_view"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:fadeScrollbars="false"
+            android:layout_marginVertical="16dp">
+            <include layout="@layout/widgets_table_container"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_gravity="center_horizontal" />
+        </ScrollView>
+    </LinearLayout>
 </merge>
diff --git a/res/values-night/styles.xml b/res/values-night/styles.xml
index 427525b..d41eb7e 100644
--- a/res/values-night/styles.xml
+++ b/res/values-night/styles.xml
@@ -20,5 +20,6 @@
 <resources>
     <style name="AddItemActivityTheme" parent="@android:style/Theme.Translucent.NoTitleBar">
         <item name="widgetsTheme">@style/WidgetContainerTheme.Dark</item>
+        <item name="android:windowTranslucentStatus">true</item>
     </style>
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index eceec8d..6f7d727 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -316,5 +316,6 @@
     <style name="AddItemActivityTheme" parent="@android:style/Theme.Translucent.NoTitleBar">
         <item name="widgetsTheme">@style/WidgetContainerTheme</item>
         <item name="android:windowLightStatusBar">true</item>
+        <item name="android:windowTranslucentStatus">true</item>
     </style>
 </resources>
diff --git a/src/com/android/launcher3/dragndrop/AddItemActivity.java b/src/com/android/launcher3/dragndrop/AddItemActivity.java
index b4288ce..01f1282 100644
--- a/src/com/android/launcher3/dragndrop/AddItemActivity.java
+++ b/src/com/android/launcher3/dragndrop/AddItemActivity.java
@@ -125,7 +125,6 @@
                 WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
         mDragLayer = findViewById(R.id.add_item_drag_layer);
         mDragLayer.recreateControllers();
-        mDragLayer.setInsets(mDeviceProfile.getInsets());
         mWidgetCell = findViewById(R.id.widget_cell);
 
         if (mRequest.getRequestType() == PinItemRequest.REQUEST_TYPE_SHORTCUT) {
@@ -322,7 +321,7 @@
     @Override
     public void onBackPressed() {
         logCommand(LAUNCHER_ADD_EXTERNAL_ITEM_BACK);
-        super.onBackPressed();
+        mSlideInView.close(/* animate= */ true);
     }
 
     @Override
diff --git a/src/com/android/launcher3/widget/AddItemWidgetsBottomSheet.java b/src/com/android/launcher3/widget/AddItemWidgetsBottomSheet.java
index 804973f..1cc7f53 100644
--- a/src/com/android/launcher3/widget/AddItemWidgetsBottomSheet.java
+++ b/src/com/android/launcher3/widget/AddItemWidgetsBottomSheet.java
@@ -16,17 +16,21 @@
 
 package com.android.launcher3.widget;
 
+import static com.android.launcher3.Utilities.ATLEAST_R;
 import static com.android.launcher3.anim.Interpolators.FAST_OUT_SLOW_IN;
 
 import android.animation.PropertyValuesHolder;
+import android.annotation.SuppressLint;
 import android.content.Context;
-import android.content.res.Configuration;
+import android.graphics.Insets;
 import android.graphics.Rect;
 import android.util.AttributeSet;
+import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewParent;
+import android.view.WindowInsets;
 
-import com.android.launcher3.Insettable;
+import com.android.launcher3.DeviceProfile;
 import com.android.launcher3.R;
 import com.android.launcher3.dragndrop.AddItemActivity;
 import com.android.launcher3.views.AbstractSlideInView;
@@ -34,13 +38,12 @@
 /**
  * Bottom sheet for the pin widget.
  */
-public class AddItemWidgetsBottomSheet extends AbstractSlideInView<AddItemActivity>
-        implements Insettable {
+public class AddItemWidgetsBottomSheet extends AbstractSlideInView<AddItemActivity> implements
+        View.OnApplyWindowInsetsListener {
 
     private static final int DEFAULT_CLOSE_DURATION = 200;
 
-    private Rect mInsets;
-    private Configuration mCurrentConfiguration;
+    private final Rect mInsets;
 
     public AddItemWidgetsBottomSheet(Context context, AttributeSet attrs) {
         this(context, attrs, 0);
@@ -48,9 +51,7 @@
 
     public AddItemWidgetsBottomSheet(Context context, AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
-        mContent = this;
         mInsets = new Rect();
-        mCurrentConfiguration = new Configuration(getResources().getConfiguration());
     }
 
     /**
@@ -62,15 +63,49 @@
             ((ViewGroup) parent).removeView(this);
         }
         attachToContainer();
+        setOnApplyWindowInsetsListener(this);
         animateOpen();
     }
 
     @Override
     protected void onLayout(boolean changed, int l, int t, int r, int b) {
-        super.onLayout(changed, l, t, r, b);
+        int width = r - l;
+        int height = b - t;
+
+        // Lay out content as center bottom aligned.
+        int contentWidth = mContent.getMeasuredWidth();
+        int contentLeft = (width - contentWidth - mInsets.left - mInsets.right) / 2 + mInsets.left;
+        mContent.layout(contentLeft, height - mContent.getMeasuredHeight(),
+                contentLeft + contentWidth, height);
+
         setTranslationShift(mTranslationShift);
     }
 
+    @Override
+    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+        DeviceProfile deviceProfile = mActivityContext.getDeviceProfile();
+        int widthUsed;
+        if (mInsets.bottom > 0) {
+            widthUsed = mInsets.left + mInsets.right;
+        } else {
+            Rect padding = deviceProfile.workspacePadding;
+            widthUsed = Math.max(padding.left + padding.right,
+                    2 * (mInsets.left + mInsets.right));
+        }
+
+        int heightUsed = mInsets.top + deviceProfile.edgeMarginPx;
+        measureChildWithMargins(mContent, widthMeasureSpec,
+                widthUsed, heightMeasureSpec, heightUsed);
+        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
+                MeasureSpec.getSize(heightMeasureSpec));
+    }
+
+    @Override
+    protected void onFinishInflate() {
+        super.onFinishInflate();
+        mContent = findViewById(R.id.add_item_bottom_sheet_content);
+    }
+
     private void animateOpen() {
         if (mIsOpen || mOpenCloseAnimator.isRunning()) {
             return;
@@ -93,25 +128,24 @@
     }
 
     @Override
-    public void setInsets(Rect insets) {
-        // Extend behind left, right, and bottom insets.
-        int leftInset = insets.left - mInsets.left;
-        int rightInset = insets.right - mInsets.right;
-        int bottomInset = insets.bottom - mInsets.bottom;
-        mInsets.set(insets);
-        setPadding(leftInset, getPaddingTop(), rightInset, bottomInset);
-    }
-
-    @Override
-    protected void onConfigurationChanged(Configuration newConfig) {
-        if (mCurrentConfiguration.orientation != newConfig.orientation) {
-            mInsets.setEmpty();
-        }
-        mCurrentConfiguration.updateFrom(newConfig);
-    }
-
-    @Override
     protected int getScrimColor(Context context) {
         return context.getResources().getColor(R.color.widgets_picker_scrim);
     }
+
+    @SuppressLint("NewApi") // Already added API check.
+    @Override
+    public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
+        if (ATLEAST_R) {
+            Insets insets = windowInsets.getInsets(WindowInsets.Type.systemBars());
+            mInsets.set(insets.left, insets.top, insets.right, insets.bottom);
+        } else {
+            mInsets.set(windowInsets.getSystemWindowInsetLeft(),
+                    windowInsets.getSystemWindowInsetTop(),
+                    windowInsets.getSystemWindowInsetRight(),
+                    windowInsets.getSystemWindowInsetBottom());
+        }
+        mContent.setPadding(mContent.getPaddingStart(),
+                mContent.getPaddingTop(), mContent.getPaddingEnd(), mInsets.bottom);
+        return windowInsets;
+    }
 }
diff --git a/src/com/android/launcher3/widget/WidgetsBottomSheet.java b/src/com/android/launcher3/widget/WidgetsBottomSheet.java
index 81df100..d0e69fa 100644
--- a/src/com/android/launcher3/widget/WidgetsBottomSheet.java
+++ b/src/com/android/launcher3/widget/WidgetsBottomSheet.java
@@ -20,7 +20,6 @@
 
 import android.animation.PropertyValuesHolder;
 import android.content.Context;
-import android.content.res.Configuration;
 import android.graphics.Rect;
 import android.util.AttributeSet;
 import android.util.IntProperty;
@@ -71,10 +70,9 @@
     private static final long EDUCATION_TIP_DELAY_MS = 300;
 
     private ItemInfo mOriginalItemInfo;
-    private Rect mInsets;
+    private final Rect mInsets;
     private final int mMaxTableHeight;
     private int mMaxHorizontalSpan = 4;
-    private Configuration mCurrentConfiguration;
 
     private final OnLayoutChangeListener mLayoutChangeListenerToShowTips =
             new OnLayoutChangeListener() {
@@ -113,20 +111,38 @@
         super(context, attrs, defStyleAttr);
         setWillNotDraw(false);
         mInsets = new Rect();
-        mContent = this;
         DeviceProfile deviceProfile = mActivityContext.getDeviceProfile();
         // Set the max table height to 2 / 3 of the grid height so that the bottom picker won't
         // take over the entire view vertically.
         mMaxTableHeight = deviceProfile.inv.numRows * 2 / 3  * deviceProfile.cellHeightPx;
-        mCurrentConfiguration = new Configuration(getResources().getConfiguration());
         if (!hasSeenEducationTip()) {
             addOnLayoutChangeListener(mLayoutChangeListenerToShowTips);
         }
     }
 
     @Override
+    protected void onFinishInflate() {
+        super.onFinishInflate();
+        mContent = findViewById(R.id.widgets_bottom_sheet);
+    }
+
+    @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+        DeviceProfile deviceProfile = mActivityContext.getDeviceProfile();
+        int widthUsed;
+        if (mInsets.bottom > 0) {
+            widthUsed = mInsets.left + mInsets.right;
+        } else {
+            Rect padding = deviceProfile.workspacePadding;
+            widthUsed = Math.max(padding.left + padding.right,
+                    2 * (mInsets.left + mInsets.right));
+        }
+
+        int heightUsed = mInsets.top + deviceProfile.edgeMarginPx;
+        measureChildWithMargins(mContent, widthMeasureSpec,
+                widthUsed, heightMeasureSpec, heightUsed);
+        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
+                MeasureSpec.getSize(heightMeasureSpec));
 
         int paddingPx = 2 * getResources().getDimensionPixelOffset(
                 R.dimen.widget_cell_horizontal_padding);
@@ -142,7 +158,15 @@
 
     @Override
     protected void onLayout(boolean changed, int l, int t, int r, int b) {
-        super.onLayout(changed, l, t, r, b);
+        int width = r - l;
+        int height = b - t;
+
+        // Content is laid out as center bottom aligned.
+        int contentWidth = mContent.getMeasuredWidth();
+        int contentLeft = (width - contentWidth - mInsets.left - mInsets.right) / 2 + mInsets.left;
+        mContent.layout(contentLeft, height - mContent.getMeasuredHeight(),
+                contentLeft + contentWidth, height);
+
         setTranslationShift(mTranslationShift);
 
         // Ensure the scroll view height is not larger than mMaxTableHeight, which is a value
@@ -241,20 +265,14 @@
 
     @Override
     public void setInsets(Rect insets) {
-        // Extend behind left, right, and bottom insets.
-        int leftInset = insets.left - mInsets.left;
-        int rightInset = insets.right - mInsets.right;
-        int bottomInset = insets.bottom - mInsets.bottom;
         mInsets.set(insets);
-        setPadding(leftInset, getPaddingTop(), rightInset, bottomInset);
-    }
-
-    @Override
-    protected void onConfigurationChanged(Configuration newConfig) {
-        if (mCurrentConfiguration.orientation != newConfig.orientation) {
-            mInsets.setEmpty();
+        mContent.setPadding(mContent.getPaddingStart(),
+                mContent.getPaddingTop(), mContent.getPaddingEnd(), insets.bottom);
+        if (insets.bottom > 0) {
+            setupNavBarColor();
+        } else {
+            clearNavBarColor();
         }
-        mCurrentConfiguration.updateFrom(newConfig);
     }
 
     @Override