1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.documentsui;
import static android.content.ContentResolver.wrap;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.provider.DocumentsContract;
import android.provider.DocumentsContract.Path;
import android.util.Log;
import androidx.annotation.Nullable;
import com.android.documentsui.archives.ArchivesProvider;
import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.RootInfo;
import com.android.documentsui.base.State;
import com.android.documentsui.base.UserId;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
/**
* Provides synchronous access to {@link DocumentInfo} instances given some identifying information
* and some documents API.
*/
public interface DocumentsAccess {
@Nullable DocumentInfo getRootDocument(RootInfo root);
@Nullable DocumentInfo getDocument(Uri uri, UserId userId);
@Nullable DocumentInfo getArchiveDocument(Uri uri, UserId userId);
boolean isDocumentUri(Uri uri);
@Nullable
Path findDocumentPath(Uri uri, UserId userId)
throws RemoteException, FileNotFoundException, CrossProfileNoPermissionException;
List<DocumentInfo> getDocuments(UserId userId, String authority, List<String> docIds)
throws RemoteException, CrossProfileNoPermissionException;
@Nullable Uri createDocument(DocumentInfo parentDoc, String mimeType, String displayName);
public static DocumentsAccess create(Context context, State state) {
return new RuntimeDocumentAccess(context, state);
}
public final class RuntimeDocumentAccess implements DocumentsAccess {
private static final String TAG = "DocumentAccess";
private final Context mContext;
private final State mState;
private RuntimeDocumentAccess(Context context, State state) {
mContext = context;
mState = state;
}
@Override
@Nullable
public DocumentInfo getRootDocument(RootInfo root) {
return getDocument(DocumentsContract.buildDocumentUri(root.authority, root.documentId),
root.userId);
}
@Override
public @Nullable DocumentInfo getDocument(Uri uri, UserId userId) {
try {
if (mState.canInteractWith(userId)) {
return DocumentInfo.fromUri(userId.getContentResolver(mContext), uri, userId);
}
} catch (FileNotFoundException e) {
Log.w(TAG, "Couldn't create DocumentInfo for uri: " + uri);
}
return null;
}
@Override
public List<DocumentInfo> getDocuments(UserId userId, String authority, List<String> docIds)
throws RemoteException, CrossProfileNoPermissionException {
if (!mState.canInteractWith(userId)) {
throw new CrossProfileNoPermissionException();
}
try (ContentProviderClient client = DocumentsApplication.acquireUnstableProviderOrThrow(
userId.getContentResolver(mContext), authority)) {
List<DocumentInfo> result = new ArrayList<>(docIds.size());
for (String docId : docIds) {
final Uri uri = DocumentsContract.buildDocumentUri(authority, docId);
try (final Cursor cursor = client.query(uri, null, null, null, null)) {
if (!cursor.moveToNext()) {
Log.e(TAG, "Couldn't create DocumentInfo for Uri: " + uri);
throw new RemoteException("Failed to move cursor.");
}
result.add(DocumentInfo.fromCursor(cursor, userId, authority));
}
}
return result;
}
}
@Override
public DocumentInfo getArchiveDocument(Uri uri, UserId userId) {
return getDocument(
ArchivesProvider.buildUriForArchive(uri, ParcelFileDescriptor.MODE_READ_ONLY),
userId);
}
@Override
public boolean isDocumentUri(Uri uri) {
return DocumentsContract.isDocumentUri(mContext, uri);
}
@Override
public Path findDocumentPath(Uri docUri, UserId userId)
throws RemoteException, FileNotFoundException, CrossProfileNoPermissionException {
if (!mState.canInteractWith(userId)) {
throw new CrossProfileNoPermissionException();
}
final ContentResolver resolver = userId.getContentResolver(mContext);
try (final ContentProviderClient client = DocumentsApplication
.acquireUnstableProviderOrThrow(resolver, docUri.getAuthority())) {
return DocumentsContract.findDocumentPath(wrap(client), docUri);
}
}
@Override
public Uri createDocument(DocumentInfo parentDoc, String mimeType, String displayName) {
final ContentResolver resolver = parentDoc.userId.getContentResolver(mContext);
try (ContentProviderClient client = DocumentsApplication.acquireUnstableProviderOrThrow(
resolver, parentDoc.derivedUri.getAuthority())) {
Uri createUri = DocumentsContract.createDocument(
wrap(client), parentDoc.derivedUri, mimeType, displayName);
// If the document info's user is the current user, we can simply return the uri.
// Otherwise, we need to create document with the content resolver from the other
// user. The uri returned from that content resolver does not contain the user
// info. Hence we need to append the other user info to the uri otherwise an app
// will think the uri is from the current user.
// The way to append a userInfo is to use the authority which contains user info
// obtained from the parentDoc.getDocumentUri().
return UserId.CURRENT_USER.equals(parentDoc.userId)
? createUri : appendEncodedParentAuthority(parentDoc, createUri);
} catch (Exception e) {
Log.w(TAG, "Failed to create document", e);
return null;
}
}
private Uri appendEncodedParentAuthority(DocumentInfo parentDoc, Uri uri) {
return uri.buildUpon().encodedAuthority(
parentDoc.getDocumentUri().getAuthority()).build();
}
}
}
|