Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 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 | |
Jeff Sharkey | a4ff00f | 2018-07-09 14:57:51 -0600 | [diff] [blame] | 19 | import androidx.annotation.Nullable; |
Jeff Sharkey | 2c0b485 | 2019-02-15 15:53:47 -0700 | [diff] [blame] | 20 | |
| 21 | import static android.content.ContentResolver.wrap; |
| 22 | |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 23 | import android.content.ContentProviderClient; |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 24 | import android.content.ContentResolver; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 26 | import android.database.Cursor; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 27 | import android.net.Uri; |
Tomasz Mikolajewski | c8fb30f | 2017-01-24 18:54:42 +0900 | [diff] [blame] | 28 | import android.os.ParcelFileDescriptor; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 29 | import android.os.RemoteException; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 30 | import android.provider.DocumentsContract; |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 31 | import android.provider.DocumentsContract.Path; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 32 | import android.util.Log; |
| 33 | |
Tomasz Mikolajewski | dbd6b8b | 2016-09-29 15:27:37 +0900 | [diff] [blame] | 34 | import com.android.documentsui.archives.ArchivesProvider; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 35 | import com.android.documentsui.base.DocumentInfo; |
| 36 | import com.android.documentsui.base.RootInfo; |
| 37 | |
| 38 | import java.io.FileNotFoundException; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 39 | import java.util.ArrayList; |
| 40 | import java.util.List; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 43 | * Provides synchronous access to {@link DocumentInfo} instances given some identifying information |
| 44 | * and some documents API. |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 45 | */ |
| 46 | public interface DocumentsAccess { |
| 47 | |
| 48 | @Nullable DocumentInfo getRootDocument(RootInfo root); |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 49 | @Nullable DocumentInfo getDocument(Uri uri); |
Tomasz Mikolajewski | dbd6b8b | 2016-09-29 15:27:37 +0900 | [diff] [blame] | 50 | @Nullable DocumentInfo getArchiveDocument(Uri uri); |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 51 | |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 52 | boolean isDocumentUri(Uri uri); |
Jeff Sharkey | 844989e | 2018-12-07 15:16:08 -0700 | [diff] [blame] | 53 | @Nullable Path findDocumentPath(Uri uri) throws RemoteException, FileNotFoundException; |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 54 | |
| 55 | List<DocumentInfo> getDocuments(String authority, List<String> docIds) throws RemoteException; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 56 | |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 57 | @Nullable Uri createDocument(DocumentInfo parentDoc, String mimeType, String displayName); |
| 58 | |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 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 | |
| 69 | private RuntimeDocumentAccess(Context context) { |
| 70 | mContext = context; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public @Nullable DocumentInfo getRootDocument(RootInfo root) { |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 75 | return getDocument( |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 76 | DocumentsContract.buildDocumentUri(root.authority, root.documentId)); |
| 77 | } |
| 78 | |
| 79 | @Override |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 80 | public @Nullable DocumentInfo getDocument(Uri uri) { |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 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 | } |
Tomasz Mikolajewski | dbd6b8b | 2016-09-29 15:27:37 +0900 | [diff] [blame] | 89 | |
| 90 | @Override |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 91 | public List<DocumentInfo> getDocuments(String authority, List<String> docIds) |
| 92 | throws RemoteException { |
| 93 | |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 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); |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 103 | throw new RemoteException("Failed to move cursor."); |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | result.add(DocumentInfo.fromCursor(cursor, authority)); |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | return result; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | @Override |
Tomasz Mikolajewski | dbd6b8b | 2016-09-29 15:27:37 +0900 | [diff] [blame] | 115 | public DocumentInfo getArchiveDocument(Uri uri) { |
Tomasz Mikolajewski | c8fb30f | 2017-01-24 18:54:42 +0900 | [diff] [blame] | 116 | return getDocument(ArchivesProvider.buildUriForArchive(uri, |
| 117 | ParcelFileDescriptor.MODE_READ_ONLY)); |
Tomasz Mikolajewski | dbd6b8b | 2016-09-29 15:27:37 +0900 | [diff] [blame] | 118 | } |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 119 | |
| 120 | @Override |
| 121 | public boolean isDocumentUri(Uri uri) { |
| 122 | return DocumentsContract.isDocumentUri(mContext, uri); |
| 123 | } |
| 124 | |
| 125 | @Override |
Jeff Sharkey | 844989e | 2018-12-07 15:16:08 -0700 | [diff] [blame] | 126 | public Path findDocumentPath(Uri docUri) throws RemoteException, FileNotFoundException { |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 127 | final ContentResolver resolver = mContext.getContentResolver(); |
| 128 | try (final ContentProviderClient client = DocumentsApplication |
| 129 | .acquireUnstableProviderOrThrow(resolver, docUri.getAuthority())) { |
Jeff Sharkey | 2c0b485 | 2019-02-15 15:53:47 -0700 | [diff] [blame] | 130 | return DocumentsContract.findDocumentPath(wrap(client), docUri); |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 133 | |
| 134 | @Override |
| 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( |
Jeff Sharkey | 2c0b485 | 2019-02-15 15:53:47 -0700 | [diff] [blame] | 140 | wrap(client), parentDoc.derivedUri, mimeType, displayName); |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 141 | } catch (Exception e) { |
| 142 | Log.w(TAG, "Failed to create document", e); |
| 143 | return null; |
| 144 | } |
| 145 | } |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 146 | } |
| 147 | } |