1 /*
2  * Copyright (C) 2016 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 androidx.annotation.Nullable;
20 import android.app.Activity;
21 import android.net.Uri;
22 import android.provider.DocumentsContract;
23 import android.provider.DocumentsContract.Path;
24 import android.util.Log;
25 
26 import com.android.documentsui.base.DocumentInfo;
27 import com.android.documentsui.base.DocumentStack;
28 import com.android.documentsui.base.PairedTask;
29 import com.android.documentsui.base.RootInfo;
30 import com.android.documentsui.roots.ProvidersAccess;
31 
32 import java.util.List;
33 
34 /**
35  * Loads {@link DocumentStack} for given document. It provides its best effort to find the path of
36  * the given document.
37  *
38  * If it fails to load correct path it calls callback with different result
39  * depending on the nullness of given root. If given root is null it calls callback with null. If
40  * given root is not null it calls callback with a {@link DocumentStack} as if the given doc lives
41  * under the root doc.
42  */
43 public class LoadDocStackTask extends PairedTask<Activity, Uri, DocumentStack> {
44     private static final String TAG = "LoadDocStackTask";
45 
46     private final ProvidersAccess mProviders;
47     private final DocumentsAccess mDocs;
48     private final LoadDocStackCallback mCallback;
49 
LoadDocStackTask( Activity activity, ProvidersAccess providers, DocumentsAccess docs, LoadDocStackCallback callback)50     public LoadDocStackTask(
51             Activity activity,
52             ProvidersAccess providers,
53             DocumentsAccess docs,
54             LoadDocStackCallback callback) {
55         super(activity);
56         mProviders = providers;
57         mDocs = docs;
58         mCallback = callback;
59     }
60 
61     @Override
run(Uri... uris)62     public @Nullable DocumentStack run(Uri... uris) {
63         if (mDocs.isDocumentUri(uris[0])) {
64             final Uri docUri;
65             if (DocumentsContract.isTreeUri(uris[0])) {
66                 // Reconstruct tree URI into a plain document URI so that we can get the full path
67                 // to the root.
68                 final String docId = DocumentsContract.getDocumentId(uris[0]);
69                 docUri = DocumentsContract.buildDocumentUri(uris[0].getAuthority(), docId);
70             } else {
71                 docUri = uris[0];
72             }
73 
74             try {
75                 final Path path = mDocs.findDocumentPath(docUri);
76                 if (path != null) {
77                     return buildStack(docUri.getAuthority(), path);
78                 } else {
79                     Log.i(TAG, "Remote provider doesn't support findDocumentPath.");
80                 }
81             } catch (Exception e) {
82                 Log.e(TAG, "Failed to build document stack for uri: " + docUri, e);
83             }
84         }
85 
86         return null;
87     }
88 
89     @Override
finish(@ullable DocumentStack stack)90     public void finish(@Nullable DocumentStack stack){
91         mCallback.onDocumentStackLoaded(stack);
92     }
93 
buildStack(String authority, Path path)94     private DocumentStack buildStack(String authority, Path path) throws Exception {
95         final String rootId = path.getRootId();
96         if (rootId == null) {
97             throw new IllegalStateException("Provider doesn't provider root id.");
98         }
99 
100         RootInfo root = mProviders.getRootOneshot(authority, path.getRootId());
101         if (root == null) {
102             throw new IllegalStateException("Failed to load root for authority: " + authority +
103                     " and root ID: " + path.getRootId() + ".");
104         }
105 
106         List<DocumentInfo> docs = mDocs.getDocuments(authority, path.getPath());
107 
108         return new DocumentStack(root, docs);
109     }
110 
111     @FunctionalInterface
112     public interface LoadDocStackCallback {
onDocumentStackLoaded(@ullable DocumentStack stack)113         void onDocumentStackLoaded(@Nullable DocumentStack stack);
114     }
115 }
116