Implement new search bar style for new landing page design

The visual is customize theme on toolbar to make it looks like search
view. Only landing page showing as search view style, others
(Image, Video, Download...) showing as legacy toolbar.

Bug: 111914879
Bug: 116186158
Test: atest DocumentsUITests
Change-Id: I025852cf23913435241ca0a8df602f77176862cb
diff --git a/res/drawable/search_bar_background.xml b/res/drawable/search_bar_background.xml
new file mode 100644
index 0000000..b3b9479
--- /dev/null
+++ b/res/drawable/search_bar_background.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2018 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.
+-->
+<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
+    <item>
+        <shape android:shape="rectangle">
+            <solid android:color="@android:color/transparent"/>
+        </shape>
+    </item>
+    <item
+        android:start="@dimen/search_bar_background_margin_start"
+        android:end="@dimen/search_bar_background_margin_end">
+        <shape android:shape="rectangle">
+            <solid android:color="?colorBackgroundFloating"/>
+            <corners android:radius="@dimen/search_bar_radius"/>
+        </shape>
+    </item>
+</layer-list>
\ No newline at end of file
diff --git a/res/layout-sw720dp/directory_app_bar.xml b/res/layout-sw720dp/directory_app_bar.xml
index 4e935db..664dbaf 100644
--- a/res/layout-sw720dp/directory_app_bar.xml
+++ b/res/layout-sw720dp/directory_app_bar.xml
@@ -26,11 +26,26 @@
         android:id="@+id/toolbar"
         android:layout_width="match_parent"
         android:layout_height="?android:attr/actionBarSize"
+        android:layout_margin="@dimen/search_bar_margin"
         android:background="?android:attr/colorBackground"
         android:theme="?actionBarTheme"
         android:popupTheme="?actionBarPopupTheme"
+        android:elevation="3dp"
         app:layout_collapseMode="pin">
 
+        <TextView
+            android:id="@+id/searchbar_title"
+            android:layout_width="match_parent"
+            android:layout_height="?android:attr/actionBarSize"
+            android:layout_marginStart="@dimen/search_bar_text_margin_start"
+            android:layout_marginEnd="@dimen/search_bar_text_margin_end"
+            android:paddingStart="@dimen/search_bar_icon_padding"
+            android:gravity="center_vertical"
+            android:text="@string/search_bar_hint"
+            android:textAppearance="@style/SearchBarTitle"
+            android:drawableStart="@drawable/ic_menu_search"
+            android:drawablePadding="@dimen/search_bar_icon_padding"/>
+
         <com.android.documentsui.DropdownBreadcrumb
             android:id="@+id/dropdown_breadcrumb"
             android:layout_width="wrap_content"
diff --git a/res/layout/action_bar_space.xml b/res/layout/action_bar_space.xml
index 3d59ce8..6bd68ed 100644
--- a/res/layout/action_bar_space.xml
+++ b/res/layout/action_bar_space.xml
@@ -16,4 +16,4 @@
 <android.widget.Space
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="?android:attr/actionBarSize"/>
\ No newline at end of file
+    android:layout_height="@dimen/action_bar_space_height"/>
\ No newline at end of file
diff --git a/res/layout/directory_app_bar.xml b/res/layout/directory_app_bar.xml
index 554163a..83cbaa0 100644
--- a/res/layout/directory_app_bar.xml
+++ b/res/layout/directory_app_bar.xml
@@ -26,22 +26,38 @@
         android:id="@+id/collapsing_toolbar"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:background="?android:attr/colorBackground"
         android:elevation="3dp"
         app:titleEnabled="false"
-        app:layout_scrollFlags="scroll|exitUntilCollapsed">
+        app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
 
         <include layout="@layout/directory_header"/>
 
+        <View
+            android:id="@+id/toolbar_background_layout"
+            android:layout_width="match_parent"
+            android:layout_height="@dimen/action_bar_space_height"
+            android:background="?android:attr/colorBackground"
+            app:layout_collapseMode="pin"/>
+
         <androidx.appcompat.widget.Toolbar
             android:id="@+id/toolbar"
             android:layout_width="match_parent"
             android:layout_height="?android:attr/actionBarSize"
+            android:layout_margin="@dimen/search_bar_margin"
             android:background="?android:attr/colorBackground"
             android:theme="?actionBarTheme"
             android:popupTheme="?actionBarPopupTheme"
+            android:elevation="3dp"
             app:layout_collapseMode="pin">
 
+            <TextView
+                android:id="@+id/searchbar_title"
+                android:layout_width="match_parent"
+                android:layout_height="?android:attr/actionBarSize"
+                android:gravity="center_vertical"
+                android:text="@string/search_bar_hint"
+                android:textAppearance="@style/SearchBarTitle"/>
+
             <com.android.documentsui.DropdownBreadcrumb
                 android:id="@+id/dropdown_breadcrumb"
                 android:layout_width="wrap_content"
diff --git a/res/layout/drawer_layout.xml b/res/layout/drawer_layout.xml
index e3e3377..6752fde 100644
--- a/res/layout/drawer_layout.xml
+++ b/res/layout/drawer_layout.xml
@@ -33,12 +33,10 @@
             android:layout_height="match_parent"
             android:orientation="vertical">
 
-            <include layout="@layout/directory_app_bar"/>
-
             <FrameLayout
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
-                app:layout_behavior="@string/appbar_scrolling_view_behavior">
+                app:layout_behavior="@string/scrolling_behavior">
 
                 <LinearLayout
                     android:layout_width="match_parent"
@@ -71,6 +69,8 @@
                 android:background="?android:attr/colorBackground"
                 android:elevation="8dp" />
 
+            <include layout="@layout/directory_app_bar"/>
+
         </androidx.coordinatorlayout.widget.CoordinatorLayout>
 
         <LinearLayout
diff --git a/res/layout/fixed_layout.xml b/res/layout/fixed_layout.xml
index a8f007b..4b8511f 100644
--- a/res/layout/fixed_layout.xml
+++ b/res/layout/fixed_layout.xml
@@ -32,11 +32,25 @@
             android:id="@+id/toolbar"
             android:layout_width="match_parent"
             android:layout_height="?android:attr/actionBarSize"
+            android:layout_margin="@dimen/search_bar_margin"
             android:background="?android:attr/colorBackground"
-            android:elevation="8dp"
+            android:elevation="3dp"
             android:theme="?actionBarTheme"
             android:popupTheme="?actionBarPopupTheme">
 
+            <TextView
+                android:id="@+id/searchbar_title"
+                android:layout_width="match_parent"
+                android:layout_height="?android:attr/actionBarSize"
+                android:layout_marginStart="@dimen/search_bar_text_margin_start"
+                android:layout_marginEnd="@dimen/search_bar_text_margin_end"
+                android:paddingStart="@dimen/search_bar_icon_padding"
+                android:gravity="center_vertical"
+                android:text="@string/search_bar_hint"
+                android:textAppearance="@style/SearchBarTitle"
+                android:drawableStart="@drawable/ic_menu_search"
+                android:drawablePadding="@dimen/search_bar_icon_padding"/>
+
             <com.android.documentsui.HorizontalBreadcrumb
                 android:id="@+id/horizontal_breadcrumb"
                 android:layout_marginRight="20dp"
diff --git a/res/layout/fragment_directory.xml b/res/layout/fragment_directory.xml
index 7ded8cf..dda8412 100644
--- a/res/layout/fragment_directory.xml
+++ b/res/layout/fragment_directory.xml
@@ -49,11 +49,8 @@
             android:clipToPadding="false"
             android:scrollbars="none"
             android:drawSelectorOnTop="true"
-            app:fastScrollEnabled="true"
-            app:fastScrollVerticalThumbDrawable="@drawable/fast_scroll_thumb_drawable"
-            app:fastScrollVerticalTrackDrawable="@drawable/fast_scroll_track_drawable"
-            app:fastScrollHorizontalThumbDrawable="@drawable/fast_scroll_thumb_drawable"
-            app:fastScrollHorizontalTrackDrawable="@drawable/fast_scroll_track_drawable"/>
+            android:overScrollMode="never"
+            app:fastScrollEnabled="false"/>
 
     </com.android.documentsui.dirlist.DocumentsSwipeRefreshLayout>
 
diff --git a/res/menu/activity.xml b/res/menu/activity.xml
index d9b509b..ac16981 100644
--- a/res/menu/activity.xml
+++ b/res/menu/activity.xml
@@ -31,7 +31,7 @@
         android:icon="@drawable/ic_menu_search"
         android:imeOptions="actionSearch"
         android:visible="false"
-        app:showAsAction="always"
+        app:showAsAction="always|collapseActionView"
         app:actionViewClass="androidx.appcompat.widget.SearchView"/>
 <!-- This group is being hidden when searching is in full bar mode-->
     <group android:id="@+id/group_hide_when_searching">
diff --git a/res/values-night/themes.xml b/res/values-night/themes.xml
index 67d66c8..6111c86 100644
--- a/res/values-night/themes.xml
+++ b/res/values-night/themes.xml
@@ -17,6 +17,7 @@
 <resources xmlns:android="http://schemas.android.com/apk/res/android">
     <style name="DocumentsTheme" parent="@style/Theme.MaterialComponents.NoActionBar">
 
+        <item name="actionModeStyle">@style/ActionModeStyle</item>
         <item name="bottomSheetDialogTheme">@style/BottomSheetDialogStyle</item>
         <item name="snackbarButtonStyle">@style/SnackbarButtonStyle</item>
 
@@ -47,5 +48,6 @@
         <item name="android:windowLightNavigationBar">false</item>
         <item name="android:listDivider">@drawable/list_divider</item>
         <item name="android:windowSoftInputMode">stateUnspecified|adjustUnspecified</item>
+        <item name="android:actionBarSize">@dimen/actionBarSize</item>
     </style>
 </resources>
diff --git a/res/values-sw720dp-land/dimens.xml b/res/values-sw720dp-land/dimens.xml
index adf091d..07a1887 100644
--- a/res/values-sw720dp-land/dimens.xml
+++ b/res/values-sw720dp-land/dimens.xml
@@ -22,4 +22,9 @@
     <dimen name="list_divider_inset">80dp</dimen>
 
     <dimen name="max_drawer_width">320dp</dimen>
+
+    <dimen name="search_bar_background_margin_start">280dp</dimen>
+    <dimen name="search_bar_background_margin_end">280dp</dimen>
+    <dimen name="search_bar_text_margin_start">270dp</dimen>
+    <dimen name="search_bar_text_margin_end">182dp</dimen>
 </resources>
diff --git a/res/values-sw720dp/config.xml b/res/values-sw720dp/config.xml
index 4898e74..5519e8c 100644
--- a/res/values-sw720dp/config.xml
+++ b/res/values-sw720dp/config.xml
@@ -17,4 +17,6 @@
 <resources>
     <!-- Indicates if search view is taking the whole toolbar space -->
     <bool name="full_bar_search_view">false</bool>
+
+    <string name="scrolling_behavior">@string/appbar_scrolling_view_behavior</string>
 </resources>
diff --git a/res/values-sw720dp/dimens.xml b/res/values-sw720dp/dimens.xml
index 9f2d017..85c370a 100644
--- a/res/values-sw720dp/dimens.xml
+++ b/res/values-sw720dp/dimens.xml
@@ -22,4 +22,10 @@
     <dimen name="list_item_width">80dp</dimen>
 
     <dimen name="max_drawer_width">320dp</dimen>
+
+    <dimen name="search_bar_background_margin_start">120dp</dimen>
+    <dimen name="search_bar_background_margin_end">120dp</dimen>
+    <dimen name="search_bar_text_margin_start">55dp</dimen>
+    <dimen name="search_bar_text_margin_end">24dp</dimen>
+    <dimen name="search_bar_icon_padding">16dp</dimen>
 </resources>
diff --git a/res/values/config.xml b/res/values/config.xml
index 443d918..96a2aee 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -63,4 +63,6 @@
     <!-- This option allows us to hide the Files app icon from launcher on Android Go device.
     Android Go device will have Files Go app for file management. -->
     <bool name="is_launcher_enabled">true</bool>
+
+    <string name="scrolling_behavior">com.android.documentsui.ui.SearchBarScrollingViewBehavior</string>
 </resources>
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 2a3ab85..c3896ab 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -78,7 +78,7 @@
 
     <dimen name="inspector_header_height">350dp</dimen>
 
-    <dimen name="root_info_header_height">48dp</dimen>
+    <dimen name="root_info_header_height">60dp</dimen>
     <dimen name="root_info_header_horizontal_padding">24dp</dimen>
 
     <dimen name="search_chip_group_margin">8dp</dimen>
@@ -100,4 +100,15 @@
     <dimen name="apps_row_exit_icon_margin_top">2dp</dimen>
     <dimen name="apps_row_exit_icon_margin_bottom">6dp</dimen>
     <dimen name="apps_row_item_text_margin_horizontal">8dp</dimen>
+
+    <dimen name="search_bar_radius">8dp</dimen>
+    <dimen name="search_bar_background_margin_start">0dp</dimen>
+    <dimen name="search_bar_background_margin_end">0dp</dimen>
+    <dimen name="search_bar_margin">8dp</dimen>
+    <dimen name="search_bar_text_size">16dp</dimen>
+    <dimen name="actionBarSize">48dp</dimen>
+    <!--This value should equal actionBarSize + (2 x search_bar_margin)-->
+    <dimen name="action_bar_space_height">64dp</dimen>
+
+    <dimen name="refresh_icon_range">64dp</dimen>
 </resources>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 7c110de..c5dc20c 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -451,4 +451,7 @@
     <string name="open_tree_dialog_title">Allow access to \"<xliff:g id="directory" example="DCIM">%1$s</xliff:g>\" on \"<xliff:g id="root" example="SD card">%2$s</xliff:g>\"?</string>
     <!-- Confrim dialog message show on open document tree flow.-->
     <string name="open_tree_dialog_message">It will allow \"<xliff:g id="appName" example="Drive">%1$s</xliff:g>\" to have full access to all files currently stored under this location, and any future content stored here.</string>
+
+    <!-- Search hint on search view. [CHAR LIMIT=48] -->
+    <string name="search_bar_hint">Search this phone</string>
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 5c707bc..d095f5b 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -15,6 +15,12 @@
 -->
 
 <resources xmlns:android="http://schemas.android.com/apk/res/android">
+    <style name="ActionModeStyle" parent="Widget.AppCompat.ActionMode">
+        <!-- attr "height" was used by support lib should not in overlay scope -->
+        <item name="height">@dimen/action_bar_space_height</item>
+        <item name="android:layout_margin">@dimen/search_bar_margin</item>
+    </style>
+
     <style name="CardViewStyle" parent="@style/Widget.MaterialComponents.CardView">
         <item name="colorBackgroundFloating">@drawable/grid_item_background</item>
         <item name="cardPreventCornerOverlap">false</item>
@@ -33,6 +39,11 @@
         <item name="android:textSize">14sp</item>
     </style>
 
+    <style name="SearchBarTitle"
+           parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
+        <item name="android:textSize">@dimen/search_bar_text_size</item>
+    </style>
+
     <style name="InspectorHeaderTitle" parent="@style/TextAppearance.MaterialComponents.Headline6">
         <item name="android:textColor">?android:attr/textColorSecondary</item>
     </style>
diff --git a/res/values/themes.xml b/res/values/themes.xml
index 7d4a50f..5f0ff7f 100644
--- a/res/values/themes.xml
+++ b/res/values/themes.xml
@@ -17,6 +17,7 @@
 <resources>
     <style name="DocumentsTheme" parent="@style/Theme.MaterialComponents.Light.NoActionBar">
 
+        <item name="actionModeStyle">@style/ActionModeStyle</item>
         <item name="bottomSheetDialogTheme">@style/BottomSheetDialogStyle</item>
         <item name="snackbarButtonStyle">@style/SnackbarButtonStyle</item>
 
@@ -50,6 +51,6 @@
         <item name="android:windowLightNavigationBar">true</item>
         <item name="android:listDivider">@drawable/list_divider</item>
         <item name="android:windowSoftInputMode">stateUnspecified|adjustUnspecified</item>
-
+        <item name="android:actionBarSize">@dimen/actionBarSize</item>
     </style>
 </resources>
diff --git a/src/com/android/documentsui/BaseActivity.java b/src/com/android/documentsui/BaseActivity.java
index b0ccb16..e1e7af0 100644
--- a/src/com/android/documentsui/BaseActivity.java
+++ b/src/com/android/documentsui/BaseActivity.java
@@ -153,7 +153,7 @@
                 Shared.findView(this, R.id.dropdown_breadcrumb, R.id.horizontal_breadcrumb);
         assert(breadcrumb != null);
 
-        mNavigator = new NavigationViewManager(mDrawer, toolbar, mState, this, breadcrumb);
+        mNavigator = new NavigationViewManager(this, mDrawer, mState, this, breadcrumb);
         SearchManagerListener searchListener = new SearchManagerListener() {
             /**
              * Called when search results changed. Refreshes the content of the directory. It
@@ -206,6 +206,12 @@
             mHasQueryContentFromIntent = mSearchManager.parseQueryContentFromIntent(getIntent(),
                     mState.action);
         }
+
+        mNavigator.setSearchBarClickListener(v -> {
+            mSearchManager.onSearchBarClicked();
+            mNavigator.update();
+        });
+
         mSortController = SortController.create(this, mState.derivedMode, mState.sortModel);
 
         mPreferencesMonitor = new PreferencesMonitor(
diff --git a/src/com/android/documentsui/NavigationViewManager.java b/src/com/android/documentsui/NavigationViewManager.java
index c966826..167f6d6 100644
--- a/src/com/android/documentsui/NavigationViewManager.java
+++ b/src/com/android/documentsui/NavigationViewManager.java
@@ -21,14 +21,22 @@
 import androidx.annotation.Nullable;
 import androidx.appcompat.widget.Toolbar;
 
+import android.app.Activity;
+import android.content.res.Resources;
+import android.graphics.Outline;
 import android.graphics.drawable.Drawable;
 import android.util.Log;
 import android.view.View;
+import android.view.ViewOutlineProvider;
 
+import com.android.documentsui.R;
 import com.android.documentsui.base.RootInfo;
 import com.android.documentsui.base.State;
 import com.android.documentsui.dirlist.AnimationView;
 
+import com.google.android.material.appbar.AppBarLayout;
+import com.google.android.material.appbar.CollapsingToolbarLayout;
+
 import java.util.function.IntConsumer;
 
 /**
@@ -43,15 +51,19 @@
     private final State mState;
     private final NavigationViewManager.Environment mEnv;
     private final Breadcrumb mBreadcrumb;
+    private final View mSearchBarView;
+    private final CollapsingToolbarLayout mCollapsingBarLayout;
+    private final Drawable mDefaultActionBarBackground;
+    private final ViewOutlineProvider mSearchBarOutlineProvider;
 
     public NavigationViewManager(
+            Activity activity,
             DrawerController drawer,
-            Toolbar toolbar,
             State state,
             NavigationViewManager.Environment env,
             Breadcrumb breadcrumb) {
 
-        mToolbar = toolbar;
+        mToolbar = activity.findViewById(R.id.toolbar);
         mDrawer = drawer;
         mState = state;
         mEnv = env;
@@ -65,6 +77,27 @@
                         onNavigationIconClicked();
                     }
                 });
+        mSearchBarView = activity.findViewById(R.id.searchbar_title);
+        mCollapsingBarLayout = activity.findViewById(R.id.collapsing_toolbar);
+        mDefaultActionBarBackground = mToolbar.getBackground();
+
+        final Resources resources = mToolbar.getResources();
+        final int radius = resources.getDimensionPixelSize(R.dimen.search_bar_radius);
+        final int marginStart =
+                resources.getDimensionPixelSize(R.dimen.search_bar_background_margin_start);
+        final int marginEnd =
+                resources.getDimensionPixelSize(R.dimen.search_bar_background_margin_end);
+        mSearchBarOutlineProvider = new ViewOutlineProvider() {
+            @Override
+            public void getOutline(View view, Outline outline) {
+                outline.setRoundRect(marginStart, 0,
+                        view.getWidth() - marginEnd, view.getHeight(), radius);
+            }
+        };
+    }
+
+    public void setSearchBarClickListener(View.OnClickListener listener) {
+        mSearchBarView.setOnClickListener(listener);
     }
 
     private void onNavigationIconClicked() {
@@ -85,6 +118,8 @@
     }
 
     public void update() {
+        updateScrollFlag();
+        updateToolbar();
 
         // TODO: Looks to me like this block is never getting hit.
         if (mEnv.isSearchExpanded()) {
@@ -98,18 +133,60 @@
         mToolbar.setNavigationIcon(getActionBarIcon());
         mToolbar.setNavigationContentDescription(R.string.drawer_open);
 
-        if (mState.stack.size() <= 1) {
+        if (mState.stack.isRecents()) {
             mBreadcrumb.show(false);
+            mToolbar.setTitle(null);
+            mSearchBarView.setVisibility(View.VISIBLE);
+        } else if (mState.stack.size() <= 1) {
+            mBreadcrumb.show(false);
+            mSearchBarView.setVisibility(View.GONE);
             String title = mEnv.getCurrentRoot().title;
             if (VERBOSE) Log.v(TAG, "New toolbar title is: " + title);
             mToolbar.setTitle(title);
         } else {
             mBreadcrumb.show(true);
             mToolbar.setTitle(null);
+            mSearchBarView.setVisibility(View.GONE);
             mBreadcrumb.postUpdate();
         }
+    }
 
-        if (VERBOSE) Log.v(TAG, "Final toolbar title is: " + mToolbar.getTitle());
+    private void updateScrollFlag() {
+        if (mCollapsingBarLayout == null) {
+            return;
+        }
+
+        AppBarLayout.LayoutParams lp =
+                (AppBarLayout.LayoutParams) mCollapsingBarLayout.getLayoutParams();
+        if (shouldShowSearchBar()) {
+            lp.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
+                            | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS
+                            | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED);
+        } else {
+            lp.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
+                            | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
+        }
+        mCollapsingBarLayout.setLayoutParams(lp);
+    }
+
+    private void updateToolbar() {
+        if (shouldShowSearchBar()) {
+            mToolbar.setBackgroundResource(R.drawable.search_bar_background);
+            mToolbar.setOutlineProvider(mSearchBarOutlineProvider);
+        } else {
+            mToolbar.setBackground(mDefaultActionBarBackground);
+            mToolbar.setOutlineProvider(null);
+        }
+
+        if (mCollapsingBarLayout != null) {
+            View overlayBackground =
+                    mCollapsingBarLayout.findViewById(R.id.toolbar_background_layout);
+            overlayBackground.setVisibility(shouldShowSearchBar() ? View.GONE : View.VISIBLE);
+        }
+    }
+
+    private boolean shouldShowSearchBar() {
+        return mState.stack.isRecents() && !mEnv.isSearchExpanded();
     }
 
     // Hamburger if drawer is present, else sad nullness.
diff --git a/src/com/android/documentsui/dirlist/DirectoryFragment.java b/src/com/android/documentsui/dirlist/DirectoryFragment.java
index c7ccae1..c15556f 100644
--- a/src/com/android/documentsui/dirlist/DirectoryFragment.java
+++ b/src/com/android/documentsui/dirlist/DirectoryFragment.java
@@ -521,9 +521,24 @@
         }
 
         int pad = getDirectoryPadding(mode);
-        mRecView.setPadding(pad, pad, pad, pad);
+        int appBarHeight = getAppBarLayoutHeight();
+        mRecView.setPadding(pad, pad + appBarHeight, pad, pad + getSaveLayoutHeight());
         mRecView.requestLayout();
         mIconHelper.setViewMode(mode);
+
+        int range = getResources().getDimensionPixelOffset(R.dimen.refresh_icon_range);
+        mRefreshLayout.setProgressViewOffset(true, appBarHeight, appBarHeight + range);
+    }
+
+    private int getAppBarLayoutHeight() {
+        View appBarLayout = getActivity().findViewById(R.id.app_bar);
+        View collapsingBar = getActivity().findViewById(R.id.collapsing_toolbar);
+        return collapsingBar == null ? 0 : appBarLayout.getHeight();
+    }
+
+    private int getSaveLayoutHeight() {
+        View containerSave = getActivity().findViewById(R.id.container_save);
+        return containerSave == null ? 0 : containerSave.getHeight();
     }
 
     /**
@@ -1077,8 +1092,9 @@
 
             final SortDimension curSortedDimension =
                     mState.sortModel.getDimensionById(curSortedDimensionId);
+            // Default not restore to avoid app bar layout expand to confuse users.
             if (container != null
-                    && !getArguments().getBoolean(Shared.EXTRA_IGNORE_STATE, false)) {
+                    && !getArguments().getBoolean(Shared.EXTRA_IGNORE_STATE, true)) {
                 getView().restoreHierarchyState(container);
             } else if (mLocalState.mLastSortDimensionId != curSortedDimension.getId()
                     || mLocalState.mLastSortDimensionId == SortModel.SORT_DIMENSION_ID_UNKNOWN
diff --git a/src/com/android/documentsui/queries/SearchViewManager.java b/src/com/android/documentsui/queries/SearchViewManager.java
index 1bd0fbf..ed1d817 100644
--- a/src/com/android/documentsui/queries/SearchViewManager.java
+++ b/src/com/android/documentsui/queries/SearchViewManager.java
@@ -186,12 +186,8 @@
         mSearchView.setOnQueryTextFocusChangeListener(this);
 
         mFullBar = isFullBarSearch;
-        if (mFullBar) {
-            mMenuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
-                    | MenuItem.SHOW_AS_ACTION_ALWAYS);
-            mMenuItem.setOnActionExpandListener(this);
-            mSearchView.setMaxWidth(Integer.MAX_VALUE);
-        }
+        mSearchView.setMaxWidth(Integer.MAX_VALUE);
+        mMenuItem.setOnActionExpandListener(this);
 
         restoreSearch();
     }
@@ -261,7 +257,8 @@
             mCurrentSearch = null;
         }
 
-        mMenuItem.setVisible(supportsSearch);
+        // Recent root show open search bar, do not show duplicate search icon.
+        mMenuItem.setVisible(supportsSearch && !stack.isRecents());
 
         // Only Storage roots, Downloads root, media roots and recent root
         // support mime type query now.
@@ -313,17 +310,17 @@
      */
     private void restoreSearch() {
         if (isSearching()) {
-            if (mFullBar) {
-                mMenuItem.expandActionView();
-            } else {
-                mSearchView.setIconified(false);
-            }
-            onSearchExpanded();
+            onSearchBarClicked();
             mSearchView.setQuery(mCurrentSearch, false);
             mSearchView.clearFocus();
         }
     }
 
+    public void onSearchBarClicked() {
+        mMenuItem.expandActionView();
+        onSearchExpanded();
+    }
+
     private void onSearchExpanded() {
         mSearchExpanded = true;
         if (mFullBar) {
diff --git a/src/com/android/documentsui/ui/SearchBarScrollingViewBehavior.java b/src/com/android/documentsui/ui/SearchBarScrollingViewBehavior.java
new file mode 100644
index 0000000..5b54f4c
--- /dev/null
+++ b/src/com/android/documentsui/ui/SearchBarScrollingViewBehavior.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2018 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.documentsui.ui;
+
+import android.content.Context;
+import android.graphics.Color;
+import android.util.AttributeSet;
+import android.view.View;
+
+import androidx.annotation.VisibleForTesting;
+import androidx.coordinatorlayout.widget.CoordinatorLayout;
+
+import com.google.android.material.appbar.AppBarLayout;
+
+/**
+ * This scrolling view behavior will set the background of the {@link AppBarLayout} as
+ * transparent and without the elevation. Also make header overlapped the scrolling child view.
+ */
+public class SearchBarScrollingViewBehavior extends AppBarLayout.ScrollingViewBehavior {
+    private boolean mInitialized;
+
+    public SearchBarScrollingViewBehavior(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    @Override
+    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
+        final boolean changed = super.onDependentViewChanged(parent, child, dependency);
+        if (!mInitialized && dependency instanceof AppBarLayout) {
+            mInitialized = true;
+            final AppBarLayout appBarLayout = (AppBarLayout) dependency;
+            setAppBarLayoutTransparent(appBarLayout);
+        }
+        return changed;
+    }
+
+    private void setAppBarLayoutTransparent(AppBarLayout appBarLayout) {
+        appBarLayout.setBackgroundColor(Color.TRANSPARENT);
+        appBarLayout.setTargetElevation(0);
+    }
+
+    @Override
+    protected boolean shouldHeaderOverlapScrollingChild() {
+        return true;
+    }
+}
+
diff --git a/tests/unit/com/android/documentsui/ui/SearchBarScrollingViewBehaviorTest.java b/tests/unit/com/android/documentsui/ui/SearchBarScrollingViewBehaviorTest.java
new file mode 100644
index 0000000..d79cad9
--- /dev/null
+++ b/tests/unit/com/android/documentsui/ui/SearchBarScrollingViewBehaviorTest.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2018 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.documentsui.ui;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+import android.content.Context;
+import android.graphics.Color;
+import android.graphics.drawable.ColorDrawable;
+
+import androidx.coordinatorlayout.widget.CoordinatorLayout;
+import androidx.test.InstrumentationRegistry;
+
+import com.android.documentsui.R;
+
+import com.google.android.material.appbar.AppBarLayout;
+
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class SearchBarScrollingViewBehaviorTest {
+    private SearchBarScrollingViewBehavior mScrollingViewBehavior;
+    private Context mContext;
+
+    @Before
+    public void setUp() {
+        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
+        mScrollingViewBehavior = new SearchBarScrollingViewBehavior(mContext, null);
+    }
+
+    @Test
+    public void shouldHeaderOverlapScrollingChild_returnTrue() {
+        assertTrue(mScrollingViewBehavior.shouldHeaderOverlapScrollingChild());
+    }
+
+    @Test
+    public void setAppBarLayoutTransparent_defaultWhiteBackground_shouldBeTransparent() {
+        mContext.setTheme(R.style.DocumentsTheme);
+        final CoordinatorLayout coordinatorLayout = new CoordinatorLayout(mContext);
+        final AppBarLayout appBarLayout = new AppBarLayout(mContext);
+        final CoordinatorLayout.LayoutParams lp = mock(CoordinatorLayout.LayoutParams.class);
+        lp.setBehavior(mScrollingViewBehavior);
+        appBarLayout.setLayoutParams(lp);
+        appBarLayout.setBackgroundColor(Color.WHITE);
+        mScrollingViewBehavior.onDependentViewChanged(coordinatorLayout, null, appBarLayout);
+
+        assertEquals(Color.TRANSPARENT, ((ColorDrawable) appBarLayout.getBackground()).getColor());
+    }
+}