blob: 63f235c5532310c3091928577c78ae9046048e20 [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;
Garfield Tan16868832016-09-26 10:01:45 -070025import android.os.RemoteException;
Steve McKaydef48682016-10-03 09:07:38 -070026import android.provider.DocumentsContract;
Garfield Tan2a837422016-10-19 11:50:45 -070027import android.provider.DocumentsContract.Path;
Steve McKaydef48682016-10-03 09:07:38 -070028import android.util.Log;
29
Tomasz Mikolajewskidbd6b8b2016-09-29 15:27:37 +090030import com.android.documentsui.archives.ArchivesProvider;
Steve McKaydef48682016-10-03 09:07:38 -070031import com.android.documentsui.base.DocumentInfo;
32import com.android.documentsui.base.RootInfo;
33
34import java.io.FileNotFoundException;
Garfield Tan16868832016-09-26 10:01:45 -070035import java.util.ArrayList;
36import java.util.List;
Steve McKaydef48682016-10-03 09:07:38 -070037
38/**
Garfield Tan2a837422016-10-19 11:50:45 -070039 * Provides synchronous access to {@link DocumentInfo} instances given some identifying information
40 * and some documents API.
Steve McKaydef48682016-10-03 09:07:38 -070041 */
42public interface DocumentsAccess {
43
44 @Nullable DocumentInfo getRootDocument(RootInfo root);
Steve McKaydef48682016-10-03 09:07:38 -070045 @Nullable DocumentInfo getDocument(Uri uri);
Tomasz Mikolajewskidbd6b8b2016-09-29 15:27:37 +090046 @Nullable DocumentInfo getArchiveDocument(Uri uri);
Steve McKaydef48682016-10-03 09:07:38 -070047
Garfield Tan2a837422016-10-19 11:50:45 -070048 boolean isDocumentUri(Uri uri);
Garfield Tan7488a802016-11-01 14:23:35 -070049 @Nullable Path findDocumentPath(Uri uri) throws RemoteException;
Garfield Tan2a837422016-10-19 11:50:45 -070050
51 List<DocumentInfo> getDocuments(String authority, List<String> docIds) throws RemoteException;
Garfield Tan16868832016-09-26 10:01:45 -070052
Steve McKaydef48682016-10-03 09:07:38 -070053 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 Tan16868832016-09-26 10:01:45 -070069 return getDocument(
Steve McKaydef48682016-10-03 09:07:38 -070070 DocumentsContract.buildDocumentUri(root.authority, root.documentId));
71 }
72
73 @Override
Garfield Tan16868832016-09-26 10:01:45 -070074 public @Nullable DocumentInfo getDocument(Uri uri) {
Steve McKaydef48682016-10-03 09:07:38 -070075 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 Mikolajewskidbd6b8b2016-09-29 15:27:37 +090083
84 @Override
Garfield Tan2a837422016-10-19 11:50:45 -070085 public List<DocumentInfo> getDocuments(String authority, List<String> docIds)
86 throws RemoteException {
87
Garfield Tan16868832016-09-26 10:01:45 -070088 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 Tan2a837422016-10-19 11:50:45 -070097 throw new RemoteException("Failed to move cursor.");
Garfield Tan16868832016-09-26 10:01:45 -070098 }
99
100 result.add(DocumentInfo.fromCursor(cursor, authority));
Garfield Tan16868832016-09-26 10:01:45 -0700101 }
102 }
103
104 return result;
Garfield Tan16868832016-09-26 10:01:45 -0700105 }
106 }
107
108 @Override
Tomasz Mikolajewskidbd6b8b2016-09-29 15:27:37 +0900109 public DocumentInfo getArchiveDocument(Uri uri) {
110 return getDocument(ArchivesProvider.buildUriForArchive(uri));
111 }
Garfield Tan2a837422016-10-19 11:50:45 -0700112
113 @Override
114 public boolean isDocumentUri(Uri uri) {
115 return DocumentsContract.isDocumentUri(mContext, uri);
116 }
117
118 @Override
Garfield Tan7488a802016-11-01 14:23:35 -0700119 public Path findDocumentPath(Uri docUri) throws RemoteException {
Garfield Tan2a837422016-10-19 11:50:45 -0700120 final ContentResolver resolver = mContext.getContentResolver();
121 try (final ContentProviderClient client = DocumentsApplication
122 .acquireUnstableProviderOrThrow(resolver, docUri.getAuthority())) {
Garfield Tan7488a802016-11-01 14:23:35 -0700123 return DocumentsContract.findDocumentPath(client, docUri);
Garfield Tan2a837422016-10-19 11:50:45 -0700124 }
125 }
Steve McKaydef48682016-10-03 09:07:38 -0700126 }
127}