blob: 90c344a72c6a2138302321d0823a69be634c3afe [file] [log] [blame]
Steve McKaydef48682016-10-03 09:07:38 -07001/*
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
17package com.android.documentsui;
18
19import android.annotation.Nullable;
Garfield Tan16868832016-09-26 10:01:45 -070020import android.content.ContentProviderClient;
Garfield Tan2a837422016-10-19 11:50:45 -070021import android.content.ContentResolver;
Steve McKaydef48682016-10-03 09:07:38 -070022import android.content.Context;
Garfield Tan16868832016-09-26 10:01:45 -070023import android.database.Cursor;
Steve McKaydef48682016-10-03 09:07:38 -070024import android.net.Uri;
Tomasz Mikolajewskic8fb30f2017-01-24 18:54:42 +090025import android.os.ParcelFileDescriptor;
Garfield Tan16868832016-09-26 10:01:45 -070026import android.os.RemoteException;
Steve McKaydef48682016-10-03 09:07:38 -070027import android.provider.DocumentsContract;
Garfield Tan2a837422016-10-19 11:50:45 -070028import android.provider.DocumentsContract.Path;
Steve McKaydef48682016-10-03 09:07:38 -070029import android.util.Log;
30
Tomasz Mikolajewskidbd6b8b2016-09-29 15:27:37 +090031import com.android.documentsui.archives.ArchivesProvider;
Steve McKaydef48682016-10-03 09:07:38 -070032import com.android.documentsui.base.DocumentInfo;
33import com.android.documentsui.base.RootInfo;
34
35import java.io.FileNotFoundException;
Garfield Tan16868832016-09-26 10:01:45 -070036import java.util.ArrayList;
37import java.util.List;
Steve McKaydef48682016-10-03 09:07:38 -070038
39/**
Garfield Tan2a837422016-10-19 11:50:45 -070040 * Provides synchronous access to {@link DocumentInfo} instances given some identifying information
41 * and some documents API.
Steve McKaydef48682016-10-03 09:07:38 -070042 */
43public interface DocumentsAccess {
44
45 @Nullable DocumentInfo getRootDocument(RootInfo root);
Steve McKaydef48682016-10-03 09:07:38 -070046 @Nullable DocumentInfo getDocument(Uri uri);
Tomasz Mikolajewskidbd6b8b2016-09-29 15:27:37 +090047 @Nullable DocumentInfo getArchiveDocument(Uri uri);
Steve McKaydef48682016-10-03 09:07:38 -070048
Garfield Tan2a837422016-10-19 11:50:45 -070049 boolean isDocumentUri(Uri uri);
Garfield Tanb00bbc52016-11-01 14:23:35 -070050 @Nullable Path findDocumentPath(Uri uri) throws RemoteException;
Garfield Tan2a837422016-10-19 11:50:45 -070051
52 List<DocumentInfo> getDocuments(String authority, List<String> docIds) throws RemoteException;
Garfield Tan16868832016-09-26 10:01:45 -070053
Garfield Tan894d4872017-03-17 12:23:22 -070054 @Nullable Uri createDocument(DocumentInfo parentDoc, String mimeType, String displayName);
55
Steve McKaydef48682016-10-03 09:07:38 -070056 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 Tan16868832016-09-26 10:01:45 -070072 return getDocument(
Steve McKaydef48682016-10-03 09:07:38 -070073 DocumentsContract.buildDocumentUri(root.authority, root.documentId));
74 }
75
76 @Override
Garfield Tan16868832016-09-26 10:01:45 -070077 public @Nullable DocumentInfo getDocument(Uri uri) {
Steve McKaydef48682016-10-03 09:07:38 -070078 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 Mikolajewskidbd6b8b2016-09-29 15:27:37 +090086
87 @Override
Garfield Tan2a837422016-10-19 11:50:45 -070088 public List<DocumentInfo> getDocuments(String authority, List<String> docIds)
89 throws RemoteException {
90
Garfield Tan16868832016-09-26 10:01:45 -070091 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 Tan2a837422016-10-19 11:50:45 -0700100 throw new RemoteException("Failed to move cursor.");
Garfield Tan16868832016-09-26 10:01:45 -0700101 }
102
103 result.add(DocumentInfo.fromCursor(cursor, authority));
Garfield Tan16868832016-09-26 10:01:45 -0700104 }
105 }
106
107 return result;
Garfield Tan16868832016-09-26 10:01:45 -0700108 }
109 }
110
111 @Override
Tomasz Mikolajewskidbd6b8b2016-09-29 15:27:37 +0900112 public DocumentInfo getArchiveDocument(Uri uri) {
Tomasz Mikolajewskic8fb30f2017-01-24 18:54:42 +0900113 return getDocument(ArchivesProvider.buildUriForArchive(uri,
114 ParcelFileDescriptor.MODE_READ_ONLY));
Tomasz Mikolajewskidbd6b8b2016-09-29 15:27:37 +0900115 }
Garfield Tan2a837422016-10-19 11:50:45 -0700116
117 @Override
118 public boolean isDocumentUri(Uri uri) {
119 return DocumentsContract.isDocumentUri(mContext, uri);
120 }
121
122 @Override
Garfield Tanb00bbc52016-11-01 14:23:35 -0700123 public Path findDocumentPath(Uri docUri) throws RemoteException {
Garfield Tan2a837422016-10-19 11:50:45 -0700124 final ContentResolver resolver = mContext.getContentResolver();
125 try (final ContentProviderClient client = DocumentsApplication
126 .acquireUnstableProviderOrThrow(resolver, docUri.getAuthority())) {
Garfield Tanb00bbc52016-11-01 14:23:35 -0700127 return DocumentsContract.findDocumentPath(client, docUri);
Garfield Tan2a837422016-10-19 11:50:45 -0700128 }
129 }
Garfield Tan894d4872017-03-17 12:23:22 -0700130
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 McKaydef48682016-10-03 09:07:38 -0700143 }
144}