Add <intent-filter> support to <provider>.

For the new documents work, we're only interested in the subset of
ContentProviders that actually implement DocumentsContract.  Instead
of returning all providers, add <intent-filter> support to make it
easier to limit the set of returned ProviderInfo.

Define a well-known action for DocumentsProviders, and start using it
when querying for roots.  Continue supporting the old <meta-data>
approach until all apps have been updated.

Bug: 8599233
Change-Id: I05f049bba21311f5421738002f99ee214447c909
diff --git a/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java b/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java
index bad0a96..eb56765 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/RootsCache.java
@@ -21,9 +21,11 @@
 import android.content.ContentProviderClient;
 import android.content.ContentResolver;
 import android.content.Context;
+import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.ProviderInfo;
+import android.content.pm.ResolveInfo;
 import android.database.ContentObserver;
 import android.database.Cursor;
 import android.net.Uri;
@@ -158,6 +160,9 @@
     private class UpdateTask extends AsyncTask<Void, Void, Void> {
         private final String mFilterPackage;
 
+        private final Multimap<String, RootInfo> mTaskRoots = ArrayListMultimap.create();
+        private final HashSet<String> mTaskStoppedAuthorities = Sets.newHashSet();
+
         /**
          * Update all roots.
          */
@@ -177,54 +182,64 @@
         protected Void doInBackground(Void... params) {
             final long start = SystemClock.elapsedRealtime();
 
-            final Multimap<String, RootInfo> roots = ArrayListMultimap.create();
-            final HashSet<String> stoppedAuthorities = Sets.newHashSet();
-
-            roots.put(mRecentsRoot.authority, mRecentsRoot);
+            mTaskRoots.put(mRecentsRoot.authority, mRecentsRoot);
 
             final ContentResolver resolver = mContext.getContentResolver();
             final PackageManager pm = mContext.getPackageManager();
-            final List<ProviderInfo> providers = pm.queryContentProviders(
+
+            // Pick up provider with action string
+            final Intent intent = new Intent(DocumentsContract.PROVIDER_INTERFACE);
+            final List<ResolveInfo> providers = pm.queryIntentContentProviders(intent, 0);
+            for (ResolveInfo info : providers) {
+                handleDocumentsProvider(info.providerInfo);
+            }
+
+            // Pick up legacy providers
+            final List<ProviderInfo> legacyProviders = pm.queryContentProviders(
                     null, -1, PackageManager.GET_META_DATA);
-            for (ProviderInfo info : providers) {
+            for (ProviderInfo info : legacyProviders) {
                 if (info.metaData != null && info.metaData.containsKey(
                         DocumentsContract.META_DATA_DOCUMENT_PROVIDER)) {
-                    // Ignore stopped packages for now; we might query them
-                    // later during UI interaction.
-                    if ((info.applicationInfo.flags & ApplicationInfo.FLAG_STOPPED) != 0) {
-                        if (LOGD) Log.d(TAG, "Ignoring stopped authority " + info.authority);
-                        stoppedAuthorities.add(info.authority);
-                        continue;
-                    }
-
-                    // Try using cached roots if filtering
-                    boolean cacheHit = false;
-                    if (mFilterPackage != null && !mFilterPackage.equals(info.packageName)) {
-                        synchronized (mLock) {
-                            if (roots.putAll(info.authority, mRoots.get(info.authority))) {
-                                if (LOGD) Log.d(TAG, "Used cached roots for " + info.authority);
-                                cacheHit = true;
-                            }
-                        }
-                    }
-
-                    // Cache miss, or loading everything
-                    if (!cacheHit) {
-                        roots.putAll(
-                                info.authority, loadRootsForAuthority(resolver, info.authority));
-                    }
+                    handleDocumentsProvider(info);
                 }
             }
 
             final long delta = SystemClock.elapsedRealtime() - start;
-            Log.d(TAG, "Update found " + roots.size() + " roots in " + delta + "ms");
+            Log.d(TAG, "Update found " + mTaskRoots.size() + " roots in " + delta + "ms");
             synchronized (mLock) {
-                mStoppedAuthorities = stoppedAuthorities;
-                mRoots = roots;
+                mRoots = mTaskRoots;
+                mStoppedAuthorities = mTaskStoppedAuthorities;
             }
             mFirstLoad.countDown();
             return null;
         }
+
+        private void handleDocumentsProvider(ProviderInfo info) {
+            // Ignore stopped packages for now; we might query them
+            // later during UI interaction.
+            if ((info.applicationInfo.flags & ApplicationInfo.FLAG_STOPPED) != 0) {
+                if (LOGD) Log.d(TAG, "Ignoring stopped authority " + info.authority);
+                mTaskStoppedAuthorities.add(info.authority);
+                return;
+            }
+
+            // Try using cached roots if filtering
+            boolean cacheHit = false;
+            if (mFilterPackage != null && !mFilterPackage.equals(info.packageName)) {
+                synchronized (mLock) {
+                    if (mTaskRoots.putAll(info.authority, mRoots.get(info.authority))) {
+                        if (LOGD) Log.d(TAG, "Used cached roots for " + info.authority);
+                        cacheHit = true;
+                    }
+                }
+            }
+
+            // Cache miss, or loading everything
+            if (!cacheHit) {
+                mTaskRoots.putAll(info.authority,
+                        loadRootsForAuthority(mContext.getContentResolver(), info.authority));
+            }
+        }
     }
 
     /**
diff --git a/packages/ExternalStorageProvider/AndroidManifest.xml b/packages/ExternalStorageProvider/AndroidManifest.xml
index 7094efc..99a4260 100644
--- a/packages/ExternalStorageProvider/AndroidManifest.xml
+++ b/packages/ExternalStorageProvider/AndroidManifest.xml
@@ -11,9 +11,9 @@
             android:grantUriPermissions="true"
             android:exported="true"
             android:permission="android.permission.MANAGE_DOCUMENTS">
-            <meta-data
-                android:name="android.content.DOCUMENT_PROVIDER"
-                android:value="true" />
+            <intent-filter>
+                <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
+            </intent-filter>
         </provider>
 
         <!-- TODO: find a better place for tests to live -->
@@ -24,9 +24,9 @@
             android:exported="true"
             android:permission="android.permission.MANAGE_DOCUMENTS"
             android:enabled="false">
-            <meta-data
-                android:name="android.content.DOCUMENT_PROVIDER"
-                android:value="true" />
+            <intent-filter>
+                <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
+            </intent-filter>
         </provider>
     </application>
 </manifest>