Separate root and document management.

Two hidden intents for managing roots and documents, used to support
Downloads UI.  Touching an item tries launching as MANAGE_DOCUMENT
first before falling back to VIEW.  Provide MIME type for roots.

Bug: 10446265, 10531347, 10599641
Change-Id: Ia5584bd6ce3e5a9b0048e8caf1447e3053664413
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 45e2650..4c91bd3 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -30,11 +30,10 @@
                 <category android:name="android.intent.category.OPENABLE" />
                 <data android:mimeType="*/*" />
             </intent-filter>
-            <!-- data expected to point at existing root to manage -->
             <intent-filter>
-                <action android:name="android.provider.action.MANAGE_DOCUMENTS" />
+                <action android:name="android.provider.action.MANAGE_ROOT" />
                 <category android:name="android.intent.category.DEFAULT" />
-                <data android:mimeType="vnd.android.document/directory" />
+                <data android:mimeType="vnd.android.document/root" />
             </intent-filter>
         </activity>
 
diff --git a/src/com/android/documentsui/DocumentsActivity.java b/src/com/android/documentsui/DocumentsActivity.java
index 4da6567..8715055 100644
--- a/src/com/android/documentsui/DocumentsActivity.java
+++ b/src/com/android/documentsui/DocumentsActivity.java
@@ -145,7 +145,7 @@
             mState.action = ACTION_CREATE;
         } else if (Intent.ACTION_GET_CONTENT.equals(action)) {
             mState.action = ACTION_GET_CONTENT;
-        } else if (DocumentsContract.ACTION_MANAGE_DOCUMENTS.equals(action)) {
+        } else if (DocumentsContract.ACTION_MANAGE_ROOT.equals(action)) {
             mState.action = ACTION_MANAGE;
         }
 
@@ -171,12 +171,13 @@
         }
 
         if (mState.action == ACTION_MANAGE) {
-            final Uri rootUri = intent.getData();
-            final RootInfo root = mRoots.findRoot(rootUri);
+            final Uri uri = intent.getData();
+            final String rootId = DocumentsContract.getRootId(uri);
+            final RootInfo root = mRoots.getRoot(uri.getAuthority(), rootId);
             if (root != null) {
                 onRootPicked(root, true);
             } else {
-                Log.w(TAG, "Failed to find root: " + rootUri);
+                Log.w(TAG, "Failed to find root: " + uri);
                 finish();
             }
 
@@ -626,14 +627,24 @@
             // Replace selected file
             SaveFragment.get(fm).setReplaceTarget(doc);
         } else if (mState.action == ACTION_MANAGE) {
-            // Open the document
-            final Intent intent = new Intent(Intent.ACTION_VIEW);
-            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
-            intent.setData(doc.uri);
+            // First try managing the document; we expect manager to filter
+            // based on authority, so we don't grant.
+            final Intent manage = new Intent(DocumentsContract.ACTION_MANAGE_DOCUMENT);
+            manage.setData(doc.uri);
+
             try {
-                startActivity(intent);
+                startActivity(manage);
             } catch (ActivityNotFoundException ex) {
-                Toast.makeText(this, R.string.toast_no_application, Toast.LENGTH_SHORT).show();
+                // Fall back to viewing
+                final Intent view = new Intent(Intent.ACTION_VIEW);
+                view.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
+                view.setData(doc.uri);
+
+                try {
+                    startActivity(view);
+                } catch (ActivityNotFoundException ex2) {
+                    Toast.makeText(this, R.string.toast_no_application, Toast.LENGTH_SHORT).show();
+                }
             }
         }
     }
diff --git a/src/com/android/documentsui/MimePredicate.java b/src/com/android/documentsui/MimePredicate.java
index c65464a..85d0988 100644
--- a/src/com/android/documentsui/MimePredicate.java
+++ b/src/com/android/documentsui/MimePredicate.java
@@ -16,13 +16,9 @@
 
 package com.android.documentsui;
 
-import android.util.Log;
-
 import com.android.documentsui.model.DocumentInfo;
 import com.android.internal.util.Predicate;
 
-import java.util.Arrays;
-
 public class MimePredicate implements Predicate<DocumentInfo> {
     private final String[] mFilters;