DocumentsUI tablet support.

Shows as a faux dialog on larger tablets, since the normal dialog
themes from framework don't support action bars.  Instead, this hacks
itself to look like a dialog with an InsetDrawable.  Detects touches
in dim area to dismiss dialog.

Show all action bar icons on tablets, and roots panel is always
visible with a different shadow. Show settings in dialog. Allow mode
switches in recents.

Bug: 10329832
Change-Id: Id02982ce7b0d4694962d32f8d5286fdfcc207208
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 4c91bd3..1ef7bff 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -11,7 +11,7 @@
         <!-- TODO: allow rotation when state saving is in better shape -->
         <activity
             android:name=".DocumentsActivity"
-            android:theme="@android:style/Theme.Holo.Light">
+            android:theme="@style/Theme">
             <intent-filter android:priority="100">
                 <action android:name="android.intent.action.OPEN_DOCUMENT" />
                 <category android:name="android.intent.category.DEFAULT" />
@@ -39,8 +39,8 @@
 
         <activity
             android:name=".SettingsActivity"
-            android:title="@string/menu_settings"
-            android:theme="@android:style/Theme.Holo.Light"
+            android:label="@string/menu_settings"
+            android:theme="@android:style/Theme.Holo.Light.DialogWhenLarge"
             android:exported="false" />
 
         <provider
diff --git a/res/drawable/item_root.xml b/res/drawable/item_root.xml
index 183d273..6f201cc 100644
--- a/res/drawable/item_root.xml
+++ b/res/drawable/item_root.xml
@@ -18,5 +18,5 @@
     <item android:state_pressed="true" android:drawable="@color/item_root_activated" />
     <item android:state_activated="true" android:drawable="@color/item_root_activated" />
     <item android:state_focused="true" android:drawable="@color/item_root_activated" />
-    <item android:drawable="@android:color/white" />
+    <item android:drawable="@android:color/transparent" />
 </selector>
diff --git a/res/layout-sw720dp-land/item_doc_list.xml b/res/layout-sw720dp-land/item_doc_list.xml
new file mode 100644
index 0000000..3bea166
--- /dev/null
+++ b/res/layout-sw720dp-land/item_doc_list.xml
@@ -0,0 +1,115 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 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.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:background="@drawable/item_background"
+    android:minHeight="?android:attr/listPreferredItemHeight"
+    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
+    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
+    android:paddingTop="8dip"
+    android:paddingBottom="8dip"
+    android:orientation="horizontal">
+
+    <FrameLayout
+        android:id="@android:id/icon"
+        android:layout_width="@dimen/icon_size"
+        android:layout_height="@dimen/icon_size"
+        android:layout_marginStart="12dp"
+        android:layout_marginEnd="20dp"
+        android:layout_gravity="center_vertical">
+
+        <ImageView
+            android:id="@+id/icon_mime"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:scaleType="centerInside"
+            android:contentDescription="@null" />
+
+        <ImageView
+            android:id="@+id/icon_thumb"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:scaleType="centerCrop"
+            android:contentDescription="@null" />
+
+    </FrameLayout>
+
+    <LinearLayout
+        android:layout_width="0dip"
+        android:layout_height="wrap_content"
+        android:layout_weight="1"
+        android:layout_gravity="center_vertical"
+        android:orientation="horizontal">
+
+        <TextView
+            android:id="@android:id/title"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="0.5"
+            android:layout_marginEnd="12dp"
+            android:singleLine="true"
+            android:ellipsize="marquee"
+            android:textAlignment="viewStart"
+            style="@style/TextAppearance.Medium" />
+
+        <ImageView
+            android:id="@android:id/icon1"
+            android:layout_width="@dimen/root_icon_size"
+            android:layout_height="@dimen/root_icon_size"
+            android:layout_marginEnd="8dp"
+            android:scaleType="centerInside"
+            android:contentDescription="@null" />
+
+        <TextView
+            android:id="@android:id/summary"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="0.25"
+            android:layout_marginEnd="12dp"
+            android:singleLine="true"
+            android:ellipsize="marquee"
+            android:textAlignment="viewStart"
+            style="@style/TextAppearance.Small" />
+
+        <TextView
+            android:id="@+id/size"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="0.125"
+            android:layout_marginEnd="12dp"
+            android:minWidth="70dp"
+            android:singleLine="true"
+            android:ellipsize="marquee"
+            android:textAlignment="viewEnd"
+            style="@style/TextAppearance.Small" />
+
+        <TextView
+            android:id="@+id/date"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_weight="0.125"
+            android:layout_marginEnd="12dp"
+            android:minWidth="70dp"
+            android:singleLine="true"
+            android:ellipsize="marquee"
+            android:textAlignment="viewEnd"
+            style="@style/TextAppearance.Small" />
+
+    </LinearLayout>
+
+</LinearLayout>
diff --git a/res/layout-sw720dp/activity.xml b/res/layout-sw720dp/activity.xml
new file mode 100644
index 0000000..584a44d
--- /dev/null
+++ b/res/layout-sw720dp/activity.xml
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 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.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="horizontal">
+
+    <FrameLayout
+        android:layout_width="wrap_content"
+        android:layout_height="match_parent"
+        android:id="@+id/dialog_roots">
+
+        <FrameLayout
+            android:id="@+id/container_roots"
+            android:layout_width="250dp"
+            android:layout_height="match_parent" />
+
+        <ImageView
+            android:layout_width="wrap_content"
+            android:layout_height="match_parent"
+            android:layout_gravity="end"
+            android:scaleType="fitXY"
+            android:src="@drawable/ic_drawer_shadow_tablet" />
+
+    </FrameLayout>
+
+    <LinearLayout
+        android:layout_width="0dp"
+        android:layout_height="match_parent"
+        android:layout_weight="1"
+        android:orientation="vertical">
+
+        <FrameLayout
+            android:id="@+id/container_directory"
+            android:layout_width="match_parent"
+            android:layout_height="0dip"
+            android:layout_weight="1" />
+
+        <FrameLayout
+            android:id="@+id/container_save"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content" />
+
+    </LinearLayout>
+
+</LinearLayout>
diff --git a/res/layout/item_doc_grid.xml b/res/layout/item_doc_grid.xml
index eea90b5..3cfae64 100644
--- a/res/layout/item_doc_grid.xml
+++ b/res/layout/item_doc_grid.xml
@@ -121,18 +121,6 @@
                 android:textAlignment="viewStart"
                 style="@style/TextAppearance.Small" />
 
-            <TextView
-                android:id="@android:id/summary"
-                android:layout_width="0dp"
-                android:layout_height="wrap_content"
-                android:layout_weight="1"
-                android:layout_gravity="center_vertical"
-                android:layout_marginStart="8dp"
-                android:singleLine="true"
-                android:ellipsize="marquee"
-                android:textAlignment="viewStart"
-                style="@style/TextAppearance.Small" />
-
         </LinearLayout>
 
     </LinearLayout>
diff --git a/res/values-sw720dp-land/dimens.xml b/res/values-sw720dp-land/dimens.xml
new file mode 100644
index 0000000..961608c
--- /dev/null
+++ b/res/values-sw720dp-land/dimens.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 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.
+-->
+
+<resources>
+    <bool name="always_show_summary">true</bool>
+</resources>
diff --git a/res/values-sw720dp/dimens.xml b/res/values-sw720dp/dimens.xml
new file mode 100644
index 0000000..3be243a
--- /dev/null
+++ b/res/values-sw720dp/dimens.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 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.
+-->
+
+<resources>
+    <bool name="show_as_dialog">true</bool>
+
+    <item type="dimen" name="dialog_width">85%</item>
+    <item type="dimen" name="dialog_height">90%</item>
+</resources>
diff --git a/res/values-sw720dp/styles.xml b/res/values-sw720dp/styles.xml
new file mode 100644
index 0000000..4ff1c60
--- /dev/null
+++ b/res/values-sw720dp/styles.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2013 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.
+-->
+
+<resources xmlns:android="http://schemas.android.com/apk/res/android">
+    <style name="Theme" parent="@android:style/Theme.Holo.Light">
+        <item name="android:windowBackground">@*android:drawable/dialog_full_holo_light</item>
+        <item name="android:colorBackgroundCacheHint">@null</item>
+        <item name="android:windowIsTranslucent">true</item>
+    </style>
+</resources>
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index e5b5b4e..25b0f84 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -19,4 +19,7 @@
     <dimen name="root_icon_size">24dp</dimen>
     <dimen name="grid_width">180dp</dimen>
     <dimen name="grid_height">180dp</dimen>
+
+    <bool name="show_as_dialog">false</bool>
+    <bool name="always_show_summary">false</bool>
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 59fbd6f..945e7ae 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -14,7 +14,7 @@
      limitations under the License.
 -->
 
-<resources>
+<resources xmlns:android="http://schemas.android.com/apk/res/android">
     <style name="TextAppearance" />
 
     <style name="TextAppearance.Medium">
@@ -26,4 +26,8 @@
         <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
         <item name="android:textColor">?android:attr/textColorTertiary</item>
     </style>
+
+    <!-- Normally just a redirection, but this is used to make ourselves a
+         dialog on large tablets -->
+    <style name="Theme" parent="@android:style/Theme.Holo.Light" />
 </resources>
diff --git a/src/com/android/documentsui/DirectoryFragment.java b/src/com/android/documentsui/DirectoryFragment.java
index ba5a511..b2981db 100644
--- a/src/com/android/documentsui/DirectoryFragment.java
+++ b/src/com/android/documentsui/DirectoryFragment.java
@@ -228,7 +228,9 @@
 
                 // Push latest state up to UI
                 // TODO: if mode change was racing with us, don't overwrite it
-                state.derivedMode = result.mode;
+                if (result.mode != MODE_UNKNOWN) {
+                    state.derivedMode = result.mode;
+                }
                 state.derivedSortOrder = result.sortOrder;
                 ((DocumentsActivity) context).onStateChanged();
 
@@ -254,8 +256,8 @@
     }
 
     @Override
-    public void onStart() {
-        super.onStart();
+    public void onResume() {
+        super.onResume();
         updateDisplayState();
     }
 
@@ -272,18 +274,20 @@
         final RootInfo root = getArguments().getParcelable(EXTRA_ROOT);
         final DocumentInfo doc = getArguments().getParcelable(EXTRA_DOC);
 
-        final Uri stateUri = RecentsProvider.buildState(
-                root.authority, root.rootId, doc.documentId);
-        final ContentValues values = new ContentValues();
-        values.put(StateColumns.MODE, state.userMode);
+        if (root != null) {
+            final Uri stateUri = RecentsProvider.buildState(
+                    root.authority, root.rootId, doc.documentId);
+            final ContentValues values = new ContentValues();
+            values.put(StateColumns.MODE, state.userMode);
 
-        new AsyncTask<Void, Void, Void>() {
-            @Override
-            protected Void doInBackground(Void... params) {
-                resolver.insert(stateUri, values);
-                return null;
-            }
-        }.execute();
+            new AsyncTask<Void, Void, Void>() {
+                @Override
+                protected Void doInBackground(Void... params) {
+                    resolver.insert(stateUri, values);
+                    return null;
+                }
+            }.execute();
+        }
 
         // Mode change is just visual change; no need to kick loader, and
         // deliver change event immediately.
@@ -733,23 +737,35 @@
                 icon1.setVisibility(View.VISIBLE);
                 icon1.setImageDrawable(iconDrawable);
 
-                if (iconDrawable != null && roots.isIconUnique(root)) {
-                    // No summary needed if icon speaks for itself
-                    summary.setVisibility(View.INVISIBLE);
-                } else {
-                    summary.setText(root.getDirectoryString());
-                    summary.setVisibility(View.VISIBLE);
-                    summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
-                    hasLine2 = true;
+                if (summary != null) {
+                    final boolean alwaysShowSummary = getResources()
+                            .getBoolean(R.bool.always_show_summary);
+                    if (alwaysShowSummary) {
+                        summary.setText(root.getDirectoryString());
+                        summary.setVisibility(View.VISIBLE);
+                        hasLine2 = true;
+                    } else {
+                        if (iconDrawable != null && roots.isIconUnique(root)) {
+                            // No summary needed if icon speaks for itself
+                            summary.setVisibility(View.INVISIBLE);
+                        } else {
+                            summary.setText(root.getDirectoryString());
+                            summary.setVisibility(View.VISIBLE);
+                            summary.setTextAlignment(TextView.TEXT_ALIGNMENT_TEXT_END);
+                            hasLine2 = true;
+                        }
+                    }
                 }
             } else {
                 icon1.setVisibility(View.GONE);
-                if (docSummary != null) {
-                    summary.setText(docSummary);
-                    summary.setVisibility(View.VISIBLE);
-                    hasLine2 = true;
-                } else {
-                    summary.setVisibility(View.INVISIBLE);
+                if (summary != null) {
+                    if (docSummary != null) {
+                        summary.setText(docSummary);
+                        summary.setVisibility(View.VISIBLE);
+                        hasLine2 = true;
+                    } else {
+                        summary.setVisibility(View.INVISIBLE);
+                    }
                 }
             }
 
@@ -772,7 +788,9 @@
                 size.setVisibility(View.GONE);
             }
 
-            line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
+            if (line2 != null) {
+                line2.setVisibility(hasLine2 ? View.VISIBLE : View.GONE);
+            }
 
             final boolean enabled = Document.MIME_TYPE_DIR.equals(docMimeType)
                     || MimePredicate.mimeMatches(state.acceptMimes, docMimeType);
diff --git a/src/com/android/documentsui/DocumentsActivity.java b/src/com/android/documentsui/DocumentsActivity.java
index e89d388..eb51fb5 100644
--- a/src/com/android/documentsui/DocumentsActivity.java
+++ b/src/com/android/documentsui/DocumentsActivity.java
@@ -22,7 +22,6 @@
 import static com.android.documentsui.DocumentsActivity.State.ACTION_OPEN;
 import static com.android.documentsui.DocumentsActivity.State.MODE_GRID;
 import static com.android.documentsui.DocumentsActivity.State.MODE_LIST;
-import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_LAST_MODIFIED;
 
 import android.app.ActionBar;
 import android.app.ActionBar.OnNavigationListener;
@@ -36,8 +35,12 @@
 import android.content.ContentValues;
 import android.content.Intent;
 import android.content.pm.ResolveInfo;
+import android.content.res.Resources;
 import android.database.Cursor;
+import android.graphics.Point;
 import android.graphics.drawable.ColorDrawable;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.InsetDrawable;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Parcel;
@@ -51,19 +54,20 @@
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.MenuItem.OnActionExpandListener;
+import android.view.MotionEvent;
 import android.view.View;
+import android.view.View.OnTouchListener;
 import android.view.ViewGroup;
+import android.view.WindowManager;
 import android.widget.BaseAdapter;
 import android.widget.ImageView;
 import android.widget.SearchView;
-import android.widget.SearchView.OnCloseListener;
 import android.widget.SearchView.OnQueryTextListener;
 import android.widget.TextView;
 import android.widget.Toast;
 
 import com.android.documentsui.RecentsProvider.RecentColumns;
 import com.android.documentsui.RecentsProvider.ResumeColumns;
-import com.android.documentsui.RecentsProvider.StateColumns;
 import com.android.documentsui.model.DocumentInfo;
 import com.android.documentsui.model.DocumentStack;
 import com.android.documentsui.model.DurableUtils;
@@ -79,15 +83,18 @@
 public class DocumentsActivity extends Activity {
     public static final String TAG = "Documents";
 
-    private SearchView mSearchView;
-
-    private View mRootsContainer;
-    private DrawerLayout mDrawerLayout;
-    private ActionBarDrawerToggle mDrawerToggle;
-
     private static final String EXTRA_STATE = "state";
 
+    private boolean mShowAsDialog;
+
+    private SearchView mSearchView;
+
+    private DrawerLayout mDrawerLayout;
+    private ActionBarDrawerToggle mDrawerToggle;
+    private View mRootsContainer;
+
     private boolean mIgnoreNextNavigation;
+    private boolean mIgnoreNextClose;
     private boolean mIgnoreNextCollapse;
 
     private RootsCache mRoots;
@@ -102,15 +109,60 @@
         setResult(Activity.RESULT_CANCELED);
         setContentView(R.layout.activity);
 
-        mRootsContainer = findViewById(R.id.container_roots);
+        final Resources res = getResources();
+        mShowAsDialog = res.getBoolean(R.bool.show_as_dialog);
 
-        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
+        if (mShowAsDialog) {
+            // backgroundDimAmount from theme isn't applied; do it manually
+            final WindowManager.LayoutParams a = getWindow().getAttributes();
+            a.dimAmount = 0.6f;
+            getWindow().setAttributes(a);
 
-        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
-                R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);
+            getWindow().setFlags(0, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
+            getWindow().setFlags(~0, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
 
-        mDrawerLayout.setDrawerListener(mDrawerListener);
-        mDrawerLayout.setDrawerShadow(R.drawable.ic_drawer_shadow, GravityCompat.START);
+            // Inset ourselves to look like a dialog
+            final Point size = new Point();
+            getWindowManager().getDefaultDisplay().getSize(size);
+
+            final int width = (int) res.getFraction(R.dimen.dialog_width, size.x, size.x);
+            final int height = (int) res.getFraction(R.dimen.dialog_height, size.y, size.y);
+            final int insetX = (size.x - width) / 2;
+            final int insetY = (size.y - height) / 2;
+
+            final Drawable before = getWindow().getDecorView().getBackground();
+            final Drawable after = new InsetDrawable(before, insetX, insetY, insetX, insetY);
+            getWindow().getDecorView().setBackground(after);
+
+            // Dismiss when touch down in the dimmed inset area
+            getWindow().getDecorView().setOnTouchListener(new OnTouchListener() {
+                @Override
+                public boolean onTouch(View v, MotionEvent event) {
+                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
+                        final float x = event.getX();
+                        final float y = event.getY();
+                        if (x < insetX || x > v.getWidth() - insetX || y < insetY
+                                || y > v.getHeight() - insetY) {
+                            finish();
+                            return true;
+                        }
+                    }
+                    return false;
+                }
+            });
+
+        } else {
+            // Non-dialog means we have a drawer
+            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
+
+            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
+                    R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);
+
+            mDrawerLayout.setDrawerListener(mDrawerListener);
+            mDrawerLayout.setDrawerShadow(R.drawable.ic_drawer_shadow, GravityCompat.START);
+
+            mRootsContainer = findViewById(R.id.container_roots);
+        }
 
         if (icicle != null) {
             mState = icicle.getParcelable(EXTRA_STATE);
@@ -118,8 +170,13 @@
             buildDefaultState();
         }
 
+        // Hide roots when we're managing a specific root
         if (mState.action == ACTION_MANAGE) {
-            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
+            if (mShowAsDialog) {
+                findViewById(R.id.dialog_roots).setVisibility(View.GONE);
+            } else {
+                mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
+            }
         }
 
         if (mState.action == ACTION_CREATE) {
@@ -210,14 +267,14 @@
 
             // Only open drawer when showing recents
             if (mState.stack.isRecents()) {
-                mDrawerLayout.openDrawer(mRootsContainer);
+                setRootsDrawerOpen(true);
             }
         }
     }
 
     @Override
-    public void onStart() {
-        super.onStart();
+    public void onResume() {
+        super.onResume();
 
         if (mState.action == ACTION_MANAGE) {
             mState.showSize = true;
@@ -255,7 +312,27 @@
     @Override
     protected void onPostCreate(Bundle savedInstanceState) {
         super.onPostCreate(savedInstanceState);
-        mDrawerToggle.syncState();
+        if (mDrawerToggle != null) {
+            mDrawerToggle.syncState();
+        }
+    }
+
+    public void setRootsDrawerOpen(boolean open) {
+        if (!mShowAsDialog) {
+            if (open) {
+                mDrawerLayout.openDrawer(mRootsContainer);
+            } else {
+                mDrawerLayout.closeDrawer(mRootsContainer);
+            }
+        }
+    }
+
+    private boolean isRootsDrawerOpen() {
+        if (mShowAsDialog) {
+            return false;
+        } else {
+            return mDrawerLayout.isDrawerOpen(mRootsContainer);
+        }
     }
 
     public void updateActionBar() {
@@ -263,15 +340,13 @@
 
         actionBar.setDisplayShowHomeEnabled(true);
 
-        if (mState.action == ACTION_MANAGE) {
-            actionBar.setDisplayHomeAsUpEnabled(false);
-            mDrawerToggle.setDrawerIndicatorEnabled(false);
-        } else {
-            actionBar.setDisplayHomeAsUpEnabled(true);
-            mDrawerToggle.setDrawerIndicatorEnabled(true);
+        final boolean showIndicator = !mShowAsDialog && (mState.action != ACTION_MANAGE);
+        actionBar.setDisplayHomeAsUpEnabled(showIndicator);
+        if (mDrawerToggle != null) {
+            mDrawerToggle.setDrawerIndicatorEnabled(showIndicator);
         }
 
-        if (mDrawerLayout.isDrawerOpen(mRootsContainer)) {
+        if (isRootsDrawerOpen()) {
             actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
             actionBar.setIcon(new ColorDrawable());
 
@@ -302,12 +377,20 @@
         super.onCreateOptionsMenu(menu);
         getMenuInflater().inflate(R.menu.activity, menu);
 
+        // Actions are always visible when showing as dialog
+        if (mShowAsDialog) {
+            for (int i = 0; i < menu.size(); i++) {
+                menu.getItem(i).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
+            }
+        }
+
         final MenuItem searchMenu = menu.findItem(R.id.menu_search);
         mSearchView = (SearchView) searchMenu.getActionView();
         mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
             @Override
             public boolean onQueryTextSubmit(String query) {
                 mState.currentSearch = query;
+                mSearchView.clearFocus();
                 onCurrentDirectoryChanged();
                 return true;
             }
@@ -337,6 +420,20 @@
             }
         });
 
+        mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
+            @Override
+            public boolean onClose() {
+                if (mIgnoreNextClose) {
+                    mIgnoreNextClose = false;
+                    return false;
+                }
+
+                mState.currentSearch = null;
+                onCurrentDirectoryChanged();
+                return false;
+            }
+        });
+
         return true;
     }
 
@@ -356,7 +453,7 @@
         final MenuItem settings = menu.findItem(R.id.menu_settings);
 
         // Open drawer means we hide most actions
-        if (mDrawerLayout.isDrawerOpen(mRootsContainer)) {
+        if (isRootsDrawerOpen()) {
             createDir.setVisible(false);
             search.setVisible(false);
             sort.setVisible(false);
@@ -367,23 +464,24 @@
             return true;
         }
 
-        if (cwd != null) {
-            sort.setVisible(true);
-            grid.setVisible(mState.derivedMode != MODE_GRID);
-            list.setVisible(mState.derivedMode != MODE_LIST);
-        } else {
-            sort.setVisible(false);
-            grid.setVisible(false);
-            list.setVisible(false);
-        }
+        sort.setVisible(cwd != null);
+        grid.setVisible(mState.derivedMode != MODE_GRID);
+        list.setVisible(mState.derivedMode != MODE_LIST);
 
         if (mState.currentSearch != null) {
             // Search uses backend ranking; no sorting
             sort.setVisible(false);
 
             search.expandActionView();
+
+            mSearchView.setIconified(false);
+            mSearchView.clearFocus();
             mSearchView.setQuery(mState.currentSearch, false);
         } else {
+            mIgnoreNextClose = true;
+            mSearchView.setIconified(true);
+            mSearchView.clearFocus();
+
             mIgnoreNextCollapse = true;
             search.collapseActionView();
         }
@@ -418,7 +516,7 @@
 
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
-        if (mDrawerToggle.onOptionsItemSelected(item)) {
+        if (mDrawerToggle != null && mDrawerToggle.onOptionsItemSelected(item)) {
             return true;
         }
 
@@ -488,7 +586,7 @@
         if (size > 1) {
             mState.stack.pop();
             onCurrentDirectoryChanged();
-        } else if (size == 1 && !mDrawerLayout.isDrawerOpen(mRootsContainer)) {
+        } else if (size == 1 && !isRootsDrawerOpen()) {
             // TODO: open root drawer once we can capture back key
             super.onBackPressed();
         } else {
@@ -614,6 +712,12 @@
                 RecentsCreateFragment.show(fm);
             } else {
                 DirectoryFragment.showRecentsOpen(fm);
+
+                // Start recents in relevant mode
+                final boolean acceptImages = MimePredicate.mimeMatches(
+                        "image/*", mState.acceptMimes);
+                mState.userMode = acceptImages ? MODE_GRID : MODE_LIST;
+                mState.derivedMode = mState.userMode;
             }
         } else {
             if (mState.currentSearch != null) {
@@ -666,7 +770,7 @@
         }
 
         if (closeDrawer) {
-            mDrawerLayout.closeDrawers();
+            setRootsDrawerOpen(false);
         }
     }
 
diff --git a/src/com/android/documentsui/RecentLoader.java b/src/com/android/documentsui/RecentLoader.java
index 57442a0..a7173b6 100644
--- a/src/com/android/documentsui/RecentLoader.java
+++ b/src/com/android/documentsui/RecentLoader.java
@@ -17,8 +17,6 @@
 package com.android.documentsui;
 
 import static com.android.documentsui.DocumentsActivity.TAG;
-import static com.android.documentsui.DocumentsActivity.State.MODE_GRID;
-import static com.android.documentsui.DocumentsActivity.State.MODE_LIST;
 import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_LAST_MODIFIED;
 
 import android.content.AsyncTaskLoader;
@@ -194,9 +192,6 @@
         }
 
         final DirectoryResult result = new DirectoryResult();
-
-        final boolean acceptImages = MimePredicate.mimeMatches("image/*", mAcceptMimes);
-        result.mode = acceptImages ? MODE_GRID : MODE_LIST;
         result.sortOrder = SORT_ORDER_LAST_MODIFIED;
 
         if (cursors.size() > 0) {
diff --git a/src/com/android/documentsui/RootsFragment.java b/src/com/android/documentsui/RootsFragment.java
index f3a21c6..54dcf1c 100644
--- a/src/com/android/documentsui/RootsFragment.java
+++ b/src/com/android/documentsui/RootsFragment.java
@@ -86,8 +86,8 @@
     }
 
     @Override
-    public void onStart() {
-        super.onStart();
+    public void onResume() {
+        super.onResume();
         updateRootsAdapter();
     }
 
diff --git a/src/com/android/documentsui/SettingsActivity.java b/src/com/android/documentsui/SettingsActivity.java
index ceeaaae..a85f6a9 100644
--- a/src/com/android/documentsui/SettingsActivity.java
+++ b/src/com/android/documentsui/SettingsActivity.java
@@ -16,6 +16,7 @@
 
 package com.android.documentsui;
 
+import android.app.ActionBar;
 import android.app.Activity;
 import android.content.Context;
 import android.os.Bundle;
@@ -39,8 +40,14 @@
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+
         getFragmentManager()
                 .beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
+
+        final ActionBar bar = getActionBar();
+        if (bar != null) {
+            bar.setDisplayShowHomeEnabled(false);
+        }
     }
 
     public static class SettingsFragment extends PreferenceFragment {