Use DocumentInfo.userId to get content resolver.

Test: atest DocumentsUIGoogleTests
Bug: 148264822
Change-Id: Iebb39751f236dc10169f2faddc809f029bc07aca
diff --git a/src/com/android/documentsui/CreateDirectoryFragment.java b/src/com/android/documentsui/CreateDirectoryFragment.java
index 3bd344f..35854fb 100644
--- a/src/com/android/documentsui/CreateDirectoryFragment.java
+++ b/src/com/android/documentsui/CreateDirectoryFragment.java
@@ -143,7 +143,7 @@
 
         @Override
         protected DocumentInfo doInBackground(Void... params) {
-            final ContentResolver resolver = mActivity.getContentResolver();
+            final ContentResolver resolver = mCwd.userId.getContentResolver(mActivity);
             ContentProviderClient client = null;
             try {
                 client = DocumentsApplication.acquireUnstableProviderOrThrow(
diff --git a/src/com/android/documentsui/DirectoryLoader.java b/src/com/android/documentsui/DirectoryLoader.java
index 04928b1..1d7ccf6 100644
--- a/src/com/android/documentsui/DirectoryLoader.java
+++ b/src/com/android/documentsui/DirectoryLoader.java
@@ -106,7 +106,7 @@
             mSignal = new CancellationSignal();
         }
 
-        final ContentResolver resolver = getContext().getContentResolver();
+        final ContentResolver resolver = mDoc.userId.getContentResolver(getContext());
         final String authority = mUri.getAuthority();
 
         final DirectoryResult result = new DirectoryResult();
@@ -235,6 +235,6 @@
         FileUtils.closeQuietly(mResult);
         mResult = null;
 
-        getContext().getContentResolver().unregisterContentObserver(mObserver);
+        mDoc.userId.getContentResolver(getContext()).unregisterContentObserver(mObserver);
     }
 }
diff --git a/src/com/android/documentsui/DocumentsAccess.java b/src/com/android/documentsui/DocumentsAccess.java
index 6445a7c..9108b15 100644
--- a/src/com/android/documentsui/DocumentsAccess.java
+++ b/src/com/android/documentsui/DocumentsAccess.java
@@ -140,7 +140,7 @@
 
         @Override
         public Uri createDocument(DocumentInfo parentDoc, String mimeType, String displayName) {
-            final ContentResolver resolver = mContext.getContentResolver();
+            final ContentResolver resolver = parentDoc.userId.getContentResolver(mContext);
             try (ContentProviderClient client = DocumentsApplication.acquireUnstableProviderOrThrow(
                         resolver, parentDoc.derivedUri.getAuthority())) {
                 return DocumentsContract.createDocument(
diff --git a/src/com/android/documentsui/files/ActionHandler.java b/src/com/android/documentsui/files/ActionHandler.java
index 9849a4e..efa8ae6 100644
--- a/src/com/android/documentsui/files/ActionHandler.java
+++ b/src/com/android/documentsui/files/ActionHandler.java
@@ -178,7 +178,7 @@
 
     @Override
     public @Nullable DocumentInfo renameDocument(String name, DocumentInfo document) {
-        ContentResolver resolver = mActivity.getContentResolver();
+        ContentResolver resolver = document.userId.getContentResolver(mActivity);
         ContentProviderClient client = null;
 
         try {
diff --git a/src/com/android/documentsui/inspector/InspectorController.java b/src/com/android/documentsui/inspector/InspectorController.java
index 3a9255b..4a88606 100644
--- a/src/com/android/documentsui/inspector/InspectorController.java
+++ b/src/com/android/documentsui/inspector/InspectorController.java
@@ -187,6 +187,7 @@
             if (docInfo.isMetadataSupported()) {
                 mLoader.getDocumentMetadata(
                         docInfo.derivedUri,
+                        docInfo.userId,
                         (Bundle bundle) -> {
                             onDocumentMetadataLoaded(docInfo, bundle);
                         });
@@ -304,7 +305,7 @@
          * @param uri
          * @param callback
          */
-        void getDocumentMetadata(Uri uri, Consumer<Bundle> callback);
+        void getDocumentMetadata(Uri uri, UserId userId, Consumer<Bundle> callback);
     }
 
     /**
diff --git a/src/com/android/documentsui/inspector/MetadataLoader.java b/src/com/android/documentsui/inspector/MetadataLoader.java
index 3d12d4e..f9e6b9f 100644
--- a/src/com/android/documentsui/inspector/MetadataLoader.java
+++ b/src/com/android/documentsui/inspector/MetadataLoader.java
@@ -15,39 +15,42 @@
  */
 package com.android.documentsui.inspector;
 
+import android.content.ContentResolver;
 import android.content.Context;
 import android.net.Uri;
 import android.os.Bundle;
 import android.provider.DocumentsContract;
+import android.util.Log;
+
 import androidx.annotation.Nullable;
 import androidx.loader.content.AsyncTaskLoader;
 
-import android.util.Log;
-
 import java.io.FileNotFoundException;
 
 /**
- * Loads metadata from {@link DocumentsContract#getDocumentMetadata(android.content.ContentProviderClient, Uri, String[])}
+ * Loads metadata from
+ * {@link DocumentsContract#getDocumentMetadata(android.content.ContentProviderClient,
+ * Uri)}
  */
 final class MetadataLoader extends AsyncTaskLoader<Bundle> {
 
     private static final String TAG = "MetadataLoader";
 
-    private final Context mContext;
     private final Uri mUri;
+    private final ContentResolver mContentResolver;
 
     private @Nullable Bundle mMetadata;
 
-    MetadataLoader(Context context, Uri uri) {
+    MetadataLoader(Context context, Uri uri, ContentResolver contentResolver) {
         super(context);
-        mContext = context;
         mUri = uri;
+        mContentResolver = contentResolver;
     }
 
     @Override
     public Bundle loadInBackground() {
         try {
-            return DocumentsContract.getDocumentMetadata(mContext.getContentResolver(), mUri);
+            return DocumentsContract.getDocumentMetadata(mContentResolver, mUri);
         } catch (FileNotFoundException | RuntimeException e) {
             Log.e(TAG, "Failed to load metadata for doc: " + mUri, e);
         }
diff --git a/src/com/android/documentsui/inspector/RuntimeDataSupplier.java b/src/com/android/documentsui/inspector/RuntimeDataSupplier.java
index 01672ca..e608369 100644
--- a/src/com/android/documentsui/inspector/RuntimeDataSupplier.java
+++ b/src/com/android/documentsui/inspector/RuntimeDataSupplier.java
@@ -109,11 +109,11 @@
     }
 
     @Override
-    public void getDocumentMetadata(Uri uri, Consumer<Bundle> callback) {
+    public void getDocumentMetadata(Uri uri, UserId userId, Consumer<Bundle> callback) {
         mMetadataCallbacks = new LoaderCallbacks<Bundle>() {
             @Override
             public Loader<Bundle> onCreateLoader(int id, Bundle unused) {
-                return new MetadataLoader(mContext, uri);
+                return new MetadataLoader(mContext, uri, userId.getContentResolver(mContext));
             }
 
             @Override
diff --git a/src/com/android/documentsui/services/CopyJob.java b/src/com/android/documentsui/services/CopyJob.java
index c0ef0c9..6d46cd4 100644
--- a/src/com/android/documentsui/services/CopyJob.java
+++ b/src/com/android/documentsui/services/CopyJob.java
@@ -394,7 +394,8 @@
         if (src.isVirtual()) {
             String[] streamTypes = null;
             try {
-                streamTypes = getContentResolver().getStreamTypes(src.derivedUri, "*/*");
+                streamTypes = src.userId.getContentResolver(service).getStreamTypes(src.derivedUri,
+                        "*/*");
             } catch (RuntimeException e) {
                 Metrics.logFileOperationFailure(
                         appContext, MetricConsts.SUBFILEOP_OBTAIN_STREAM_TYPE, src.derivedUri);
@@ -443,7 +444,8 @@
 
         DocumentInfo dstInfo = null;
         try {
-            dstInfo = DocumentInfo.fromUri(getContentResolver(), dstUri, dest.userId);
+            dstInfo = DocumentInfo.fromUri(dest.userId.getContentResolver(service), dstUri,
+                    dest.userId);
         } catch (FileNotFoundException | RuntimeException e) {
             Metrics.logFileOperationFailure(
                     appContext, MetricConsts.SUBFILEOP_QUERY_DOCUMENT, dstUri);
diff --git a/tests/unit/com/android/documentsui/inspector/DocumentLoaderTest.java b/tests/unit/com/android/documentsui/inspector/DocumentLoaderTest.java
index c453ed2..fe97c2c 100644
--- a/tests/unit/com/android/documentsui/inspector/DocumentLoaderTest.java
+++ b/tests/unit/com/android/documentsui/inspector/DocumentLoaderTest.java
@@ -183,7 +183,7 @@
                 InspectorProvider.AUTHORITY, InspectorProvider.TEST_JPEG);
         LatchedConsumer<Bundle> consumer = new LatchedConsumer<>(1);
 
-        mLoader.getDocumentMetadata(uri, consumer);
+        mLoader.getDocumentMetadata(uri, mUserId, consumer);
         mLoaderManager.getLoader(0).startLoading();
 
         consumer.assertCalled(100, TimeUnit.MILLISECONDS);
@@ -200,7 +200,7 @@
                 InspectorProvider.AUTHORITY, InspectorProvider.INVALID_JPEG);
         LatchedConsumer<Bundle> consumer = new LatchedConsumer<>(1);
 
-        mLoader.getDocumentMetadata(uri, consumer);
+        mLoader.getDocumentMetadata(uri, mUserId, consumer);
         mLoaderManager.getLoader(0).startLoading();
 
         consumer.assertCalled(100, TimeUnit.MILLISECONDS);
diff --git a/tests/unit/com/android/documentsui/inspector/InspectorControllerTest.java b/tests/unit/com/android/documentsui/inspector/InspectorControllerTest.java
index d98f955..d35a6de 100644
--- a/tests/unit/com/android/documentsui/inspector/InspectorControllerTest.java
+++ b/tests/unit/com/android/documentsui/inspector/InspectorControllerTest.java
@@ -420,7 +420,7 @@
         }
 
         @Override
-        public void getDocumentMetadata(Uri uri, Consumer<Bundle> callback) {
+        public void getDocumentMetadata(Uri uri, UserId userId, Consumer<Bundle> callback) {
             callback.accept(mMetadata);
         }