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