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 | |
| 19 | import android.annotation.Nullable; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 20 | import android.content.ContentProviderClient; |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 21 | import android.content.ContentResolver; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 23 | import android.database.Cursor; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 24 | import android.net.Uri; |
Tomasz Mikolajewski | c8fb30f | 2017-01-24 18:54:42 +0900 | [diff] [blame] | 25 | import android.os.ParcelFileDescriptor; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 26 | import android.os.RemoteException; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 27 | import android.provider.DocumentsContract; |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 28 | import android.provider.DocumentsContract.Path; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | |
Tomasz Mikolajewski | dbd6b8b | 2016-09-29 15:27:37 +0900 | [diff] [blame] | 31 | import com.android.documentsui.archives.ArchivesProvider; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 32 | import com.android.documentsui.base.DocumentInfo; |
| 33 | import com.android.documentsui.base.RootInfo; |
| 34 | |
| 35 | import java.io.FileNotFoundException; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
| 37 | import java.util.List; |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 40 | * Provides synchronous access to {@link DocumentInfo} instances given some identifying information |
| 41 | * and some documents API. |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 42 | */ |
| 43 | public interface DocumentsAccess { |
| 44 | |
| 45 | @Nullable DocumentInfo getRootDocument(RootInfo root); |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 46 | @Nullable DocumentInfo getDocument(Uri uri); |
Tomasz Mikolajewski | dbd6b8b | 2016-09-29 15:27:37 +0900 | [diff] [blame] | 47 | @Nullable DocumentInfo getArchiveDocument(Uri uri); |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 48 | |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 49 | boolean isDocumentUri(Uri uri); |
Garfield Tan | b00bbc5 | 2016-11-01 14:23:35 -0700 | [diff] [blame] | 50 | @Nullable Path findDocumentPath(Uri uri) throws RemoteException; |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 51 | |
| 52 | List<DocumentInfo> getDocuments(String authority, List<String> docIds) throws RemoteException; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 53 | |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 54 | @Nullable Uri createDocument(DocumentInfo parentDoc, String mimeType, String displayName); |
| 55 | |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 56 | public static DocumentsAccess create(Context context) { |
| 57 | return new RuntimeDocumentAccess(context); |
| 58 | } |
| 59 | |
| 60 | public final class RuntimeDocumentAccess implements DocumentsAccess { |
| 61 | |
| 62 | private static final String TAG = "DocumentAccess"; |
| 63 | |
| 64 | private final Context mContext; |
| 65 | |
| 66 | private RuntimeDocumentAccess(Context context) { |
| 67 | mContext = context; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public @Nullable DocumentInfo getRootDocument(RootInfo root) { |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 72 | return getDocument( |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 73 | DocumentsContract.buildDocumentUri(root.authority, root.documentId)); |
| 74 | } |
| 75 | |
| 76 | @Override |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 77 | public @Nullable DocumentInfo getDocument(Uri uri) { |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 78 | try { |
| 79 | return DocumentInfo.fromUri(mContext.getContentResolver(), uri); |
| 80 | } catch (FileNotFoundException e) { |
| 81 | Log.w(TAG, "Couldn't create DocumentInfo for uri: " + uri); |
| 82 | } |
| 83 | |
| 84 | return null; |
| 85 | } |
Tomasz Mikolajewski | dbd6b8b | 2016-09-29 15:27:37 +0900 | [diff] [blame] | 86 | |
| 87 | @Override |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 88 | public List<DocumentInfo> getDocuments(String authority, List<String> docIds) |
| 89 | throws RemoteException { |
| 90 | |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 91 | try(final ContentProviderClient client = DocumentsApplication |
| 92 | .acquireUnstableProviderOrThrow(mContext.getContentResolver(), authority)) { |
| 93 | |
| 94 | List<DocumentInfo> result = new ArrayList<>(docIds.size()); |
| 95 | for (String docId : docIds) { |
| 96 | final Uri uri = DocumentsContract.buildDocumentUri(authority, docId); |
| 97 | try (final Cursor cursor = client.query(uri, null, null, null, null)) { |
| 98 | if (!cursor.moveToNext()) { |
| 99 | Log.e(TAG, "Couldn't create DocumentInfo for Uri: " + uri); |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 100 | throw new RemoteException("Failed to move cursor."); |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | result.add(DocumentInfo.fromCursor(cursor, authority)); |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | return result; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | @Override |
Tomasz Mikolajewski | dbd6b8b | 2016-09-29 15:27:37 +0900 | [diff] [blame] | 112 | public DocumentInfo getArchiveDocument(Uri uri) { |
Tomasz Mikolajewski | c8fb30f | 2017-01-24 18:54:42 +0900 | [diff] [blame] | 113 | return getDocument(ArchivesProvider.buildUriForArchive(uri, |
| 114 | ParcelFileDescriptor.MODE_READ_ONLY)); |
Tomasz Mikolajewski | dbd6b8b | 2016-09-29 15:27:37 +0900 | [diff] [blame] | 115 | } |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 116 | |
| 117 | @Override |
| 118 | public boolean isDocumentUri(Uri uri) { |
| 119 | return DocumentsContract.isDocumentUri(mContext, uri); |
| 120 | } |
| 121 | |
| 122 | @Override |
Garfield Tan | b00bbc5 | 2016-11-01 14:23:35 -0700 | [diff] [blame] | 123 | public Path findDocumentPath(Uri docUri) throws RemoteException { |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 124 | final ContentResolver resolver = mContext.getContentResolver(); |
| 125 | try (final ContentProviderClient client = DocumentsApplication |
| 126 | .acquireUnstableProviderOrThrow(resolver, docUri.getAuthority())) { |
Garfield Tan | b00bbc5 | 2016-11-01 14:23:35 -0700 | [diff] [blame] | 127 | return DocumentsContract.findDocumentPath(client, docUri); |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 130 | |
| 131 | @Override |
| 132 | public Uri createDocument(DocumentInfo parentDoc, String mimeType, String displayName) { |
| 133 | final ContentResolver resolver = mContext.getContentResolver(); |
| 134 | try (ContentProviderClient client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 135 | resolver, parentDoc.derivedUri.getAuthority())) { |
| 136 | return DocumentsContract.createDocument( |
| 137 | client, parentDoc.derivedUri, mimeType, displayName); |
| 138 | } catch (Exception e) { |
| 139 | Log.w(TAG, "Failed to create document", e); |
| 140 | return null; |
| 141 | } |
| 142 | } |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 143 | } |
| 144 | } |