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 | |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 54 | public static DocumentsAccess create(Context context) { |
| 55 | return new RuntimeDocumentAccess(context); |
| 56 | } |
| 57 | |
| 58 | public final class RuntimeDocumentAccess implements DocumentsAccess { |
| 59 | |
| 60 | private static final String TAG = "DocumentAccess"; |
| 61 | |
| 62 | private final Context mContext; |
| 63 | |
| 64 | private RuntimeDocumentAccess(Context context) { |
| 65 | mContext = context; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public @Nullable DocumentInfo getRootDocument(RootInfo root) { |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 70 | return getDocument( |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 71 | DocumentsContract.buildDocumentUri(root.authority, root.documentId)); |
| 72 | } |
| 73 | |
| 74 | @Override |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 75 | public @Nullable DocumentInfo getDocument(Uri uri) { |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 76 | try { |
| 77 | return DocumentInfo.fromUri(mContext.getContentResolver(), uri); |
| 78 | } catch (FileNotFoundException e) { |
| 79 | Log.w(TAG, "Couldn't create DocumentInfo for uri: " + uri); |
| 80 | } |
| 81 | |
| 82 | return null; |
| 83 | } |
Tomasz Mikolajewski | dbd6b8b | 2016-09-29 15:27:37 +0900 | [diff] [blame] | 84 | |
| 85 | @Override |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 86 | public List<DocumentInfo> getDocuments(String authority, List<String> docIds) |
| 87 | throws RemoteException { |
| 88 | |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 89 | try(final ContentProviderClient client = DocumentsApplication |
| 90 | .acquireUnstableProviderOrThrow(mContext.getContentResolver(), authority)) { |
| 91 | |
| 92 | List<DocumentInfo> result = new ArrayList<>(docIds.size()); |
| 93 | for (String docId : docIds) { |
| 94 | final Uri uri = DocumentsContract.buildDocumentUri(authority, docId); |
| 95 | try (final Cursor cursor = client.query(uri, null, null, null, null)) { |
| 96 | if (!cursor.moveToNext()) { |
| 97 | Log.e(TAG, "Couldn't create DocumentInfo for Uri: " + uri); |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 98 | throw new RemoteException("Failed to move cursor."); |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | result.add(DocumentInfo.fromCursor(cursor, authority)); |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | return result; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | @Override |
Tomasz Mikolajewski | dbd6b8b | 2016-09-29 15:27:37 +0900 | [diff] [blame] | 110 | public DocumentInfo getArchiveDocument(Uri uri) { |
Tomasz Mikolajewski | c8fb30f | 2017-01-24 18:54:42 +0900 | [diff] [blame] | 111 | return getDocument(ArchivesProvider.buildUriForArchive(uri, |
| 112 | ParcelFileDescriptor.MODE_READ_ONLY)); |
Tomasz Mikolajewski | dbd6b8b | 2016-09-29 15:27:37 +0900 | [diff] [blame] | 113 | } |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 114 | |
| 115 | @Override |
| 116 | public boolean isDocumentUri(Uri uri) { |
| 117 | return DocumentsContract.isDocumentUri(mContext, uri); |
| 118 | } |
| 119 | |
| 120 | @Override |
Garfield Tan | b00bbc5 | 2016-11-01 14:23:35 -0700 | [diff] [blame] | 121 | public Path findDocumentPath(Uri docUri) throws RemoteException { |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 122 | final ContentResolver resolver = mContext.getContentResolver(); |
| 123 | try (final ContentProviderClient client = DocumentsApplication |
| 124 | .acquireUnstableProviderOrThrow(resolver, docUri.getAuthority())) { |
Garfield Tan | b00bbc5 | 2016-11-01 14:23:35 -0700 | [diff] [blame] | 125 | return DocumentsContract.findDocumentPath(client, docUri); |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
Steve McKay | def4868 | 2016-10-03 09:07:38 -0700 | [diff] [blame] | 128 | } |
| 129 | } |