1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.documentsui; 18 19 import androidx.annotation.Nullable; 20 21 import static android.content.ContentResolver.wrap; 22 23 import android.content.ContentProviderClient; 24 import android.content.ContentResolver; 25 import android.content.Context; 26 import android.database.Cursor; 27 import android.net.Uri; 28 import android.os.ParcelFileDescriptor; 29 import android.os.RemoteException; 30 import android.provider.DocumentsContract; 31 import android.provider.DocumentsContract.Path; 32 import android.util.Log; 33 34 import com.android.documentsui.archives.ArchivesProvider; 35 import com.android.documentsui.base.DocumentInfo; 36 import com.android.documentsui.base.RootInfo; 37 38 import java.io.FileNotFoundException; 39 import java.util.ArrayList; 40 import java.util.List; 41 42 /** 43 * Provides synchronous access to {@link DocumentInfo} instances given some identifying information 44 * and some documents API. 45 */ 46 public interface DocumentsAccess { 47 getRootDocument(RootInfo root)48 @Nullable DocumentInfo getRootDocument(RootInfo root); getDocument(Uri uri)49 @Nullable DocumentInfo getDocument(Uri uri); getArchiveDocument(Uri uri)50 @Nullable DocumentInfo getArchiveDocument(Uri uri); 51 isDocumentUri(Uri uri)52 boolean isDocumentUri(Uri uri); findDocumentPath(Uri uri)53 @Nullable Path findDocumentPath(Uri uri) throws RemoteException, FileNotFoundException; 54 getDocuments(String authority, List<String> docIds)55 List<DocumentInfo> getDocuments(String authority, List<String> docIds) throws RemoteException; 56 createDocument(DocumentInfo parentDoc, String mimeType, String displayName)57 @Nullable Uri createDocument(DocumentInfo parentDoc, String mimeType, String displayName); 58 create(Context context)59 public static DocumentsAccess create(Context context) { 60 return new RuntimeDocumentAccess(context); 61 } 62 63 public final class RuntimeDocumentAccess implements DocumentsAccess { 64 65 private static final String TAG = "DocumentAccess"; 66 67 private final Context mContext; 68 RuntimeDocumentAccess(Context context)69 private RuntimeDocumentAccess(Context context) { 70 mContext = context; 71 } 72 73 @Override getRootDocument(RootInfo root)74 public @Nullable DocumentInfo getRootDocument(RootInfo root) { 75 return getDocument( 76 DocumentsContract.buildDocumentUri(root.authority, root.documentId)); 77 } 78 79 @Override getDocument(Uri uri)80 public @Nullable DocumentInfo getDocument(Uri uri) { 81 try { 82 return DocumentInfo.fromUri(mContext.getContentResolver(), uri); 83 } catch (FileNotFoundException e) { 84 Log.w(TAG, "Couldn't create DocumentInfo for uri: " + uri); 85 } 86 87 return null; 88 } 89 90 @Override getDocuments(String authority, List<String> docIds)91 public List<DocumentInfo> getDocuments(String authority, List<String> docIds) 92 throws RemoteException { 93 94 try(final ContentProviderClient client = DocumentsApplication 95 .acquireUnstableProviderOrThrow(mContext.getContentResolver(), authority)) { 96 97 List<DocumentInfo> result = new ArrayList<>(docIds.size()); 98 for (String docId : docIds) { 99 final Uri uri = DocumentsContract.buildDocumentUri(authority, docId); 100 try (final Cursor cursor = client.query(uri, null, null, null, null)) { 101 if (!cursor.moveToNext()) { 102 Log.e(TAG, "Couldn't create DocumentInfo for Uri: " + uri); 103 throw new RemoteException("Failed to move cursor."); 104 } 105 106 result.add(DocumentInfo.fromCursor(cursor, authority)); 107 } 108 } 109 110 return result; 111 } 112 } 113 114 @Override getArchiveDocument(Uri uri)115 public DocumentInfo getArchiveDocument(Uri uri) { 116 return getDocument(ArchivesProvider.buildUriForArchive(uri, 117 ParcelFileDescriptor.MODE_READ_ONLY)); 118 } 119 120 @Override isDocumentUri(Uri uri)121 public boolean isDocumentUri(Uri uri) { 122 return DocumentsContract.isDocumentUri(mContext, uri); 123 } 124 125 @Override findDocumentPath(Uri docUri)126 public Path findDocumentPath(Uri docUri) throws RemoteException, FileNotFoundException { 127 final ContentResolver resolver = mContext.getContentResolver(); 128 try (final ContentProviderClient client = DocumentsApplication 129 .acquireUnstableProviderOrThrow(resolver, docUri.getAuthority())) { 130 return DocumentsContract.findDocumentPath(wrap(client), docUri); 131 } 132 } 133 134 @Override createDocument(DocumentInfo parentDoc, String mimeType, String displayName)135 public Uri createDocument(DocumentInfo parentDoc, String mimeType, String displayName) { 136 final ContentResolver resolver = mContext.getContentResolver(); 137 try (ContentProviderClient client = DocumentsApplication.acquireUnstableProviderOrThrow( 138 resolver, parentDoc.derivedUri.getAuthority())) { 139 return DocumentsContract.createDocument( 140 wrap(client), parentDoc.derivedUri, mimeType, displayName); 141 } catch (Exception e) { 142 Log.w(TAG, "Failed to create document", e); 143 return null; 144 } 145 } 146 } 147 } 148