blob: 404bb1bf9b45a282aa46dbbeb44e8ca2d398a100 [file] [log] [blame]
Garfield Tan16868832016-09-26 10:01:45 -07001/*
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
17package com.android.documentsui;
18
Jeff Sharkeya4ff00f2018-07-09 14:57:51 -060019import androidx.annotation.Nullable;
Garfield Tan16868832016-09-26 10:01:45 -070020import android.app.Activity;
21import android.net.Uri;
Garfield Tanfc726172016-11-04 17:49:29 -070022import android.provider.DocumentsContract;
Garfield Tan16868832016-09-26 10:01:45 -070023import android.provider.DocumentsContract.Path;
24import android.util.Log;
25
26import com.android.documentsui.base.DocumentInfo;
27import com.android.documentsui.base.DocumentStack;
28import com.android.documentsui.base.PairedTask;
29import com.android.documentsui.base.RootInfo;
Jon Mann9bd40992017-03-24 12:34:34 -070030import com.android.documentsui.roots.ProvidersAccess;
Garfield Tan16868832016-09-26 10:01:45 -070031
32import 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 */
Garfield Tan2a837422016-10-19 11:50:45 -070043public class LoadDocStackTask extends PairedTask<Activity, Uri, DocumentStack> {
Garfield Tan16868832016-09-26 10:01:45 -070044 private static final String TAG = "LoadDocStackTask";
45
Jon Mann9bd40992017-03-24 12:34:34 -070046 private final ProvidersAccess mProviders;
Garfield Tan16868832016-09-26 10:01:45 -070047 private final DocumentsAccess mDocs;
Garfield Tan16868832016-09-26 10:01:45 -070048 private final LoadDocStackCallback mCallback;
49
50 public LoadDocStackTask(
51 Activity activity,
Jon Mann9bd40992017-03-24 12:34:34 -070052 ProvidersAccess providers,
Garfield Tan16868832016-09-26 10:01:45 -070053 DocumentsAccess docs,
Garfield Tan16868832016-09-26 10:01:45 -070054 LoadDocStackCallback callback) {
55 super(activity);
Jon Mann9bd40992017-03-24 12:34:34 -070056 mProviders = providers;
Garfield Tan16868832016-09-26 10:01:45 -070057 mDocs = docs;
Garfield Tan16868832016-09-26 10:01:45 -070058 mCallback = callback;
59 }
60
61 @Override
Garfield Tan2a837422016-10-19 11:50:45 -070062 public @Nullable DocumentStack run(Uri... uris) {
Steve McKay98f8c5f2017-03-03 13:52:14 -080063 if (mDocs.isDocumentUri(uris[0])) {
Garfield Tanfc726172016-11-04 17:49:29 -070064 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
Garfield Tan16868832016-09-26 10:01:45 -070074 try {
Garfield Tanb00bbc52016-11-01 14:23:35 -070075 final Path path = mDocs.findDocumentPath(docUri);
Garfield Tan16868832016-09-26 10:01:45 -070076 if (path != null) {
Garfield Tan2a837422016-10-19 11:50:45 -070077 return buildStack(docUri.getAuthority(), path);
Garfield Tan16868832016-09-26 10:01:45 -070078 } else {
Garfield Tanb00bbc52016-11-01 14:23:35 -070079 Log.i(TAG, "Remote provider doesn't support findDocumentPath.");
Garfield Tan16868832016-09-26 10:01:45 -070080 }
81 } catch (Exception e) {
Garfield Tan2a837422016-10-19 11:50:45 -070082 Log.e(TAG, "Failed to build document stack for uri: " + docUri, e);
Garfield Tan16868832016-09-26 10:01:45 -070083 }
84 }
85
86 return null;
87 }
88
89 @Override
90 public void finish(@Nullable DocumentStack stack){
91 mCallback.onDocumentStackLoaded(stack);
92 }
93
Garfield Tan2a837422016-10-19 11:50:45 -070094 private DocumentStack buildStack(String authority, Path path) throws Exception {
Garfield Tan16868832016-09-26 10:01:45 -070095 final String rootId = path.getRootId();
96 if (rootId == null) {
Garfield Tan2a837422016-10-19 11:50:45 -070097 throw new IllegalStateException("Provider doesn't provider root id.");
Garfield Tan16868832016-09-26 10:01:45 -070098 }
99
Jon Mann9bd40992017-03-24 12:34:34 -0700100 RootInfo root = mProviders.getRootOneshot(authority, path.getRootId());
Garfield Tan2a837422016-10-19 11:50:45 -0700101 if (root == null) {
102 throw new IllegalStateException("Failed to load root for authority: " + authority +
103 " and root ID: " + path.getRootId() + ".");
Garfield Tan16868832016-09-26 10:01:45 -0700104 }
105
Garfield Tan2a837422016-10-19 11:50:45 -0700106 List<DocumentInfo> docs = mDocs.getDocuments(authority, path.getPath());
107
Garfield Tan16868832016-09-26 10:01:45 -0700108 return new DocumentStack(root, docs);
109 }
110
111 @FunctionalInterface
112 public interface LoadDocStackCallback {
113 void onDocumentStackLoaded(@Nullable DocumentStack stack);
114 }
115}