blob: 1ce4630c9510d992d3b6abfabbe893b1b5e0d7d2 [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
19import android.annotation.Nullable;
20import 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 // assert(Features.OMC_RUNTIME);
64 if (mDocs.isDocumentUri(uris[0])) {
Garfield Tanfc726172016-11-04 17:49:29 -070065 final Uri docUri;
66 if (DocumentsContract.isTreeUri(uris[0])) {
67 // Reconstruct tree URI into a plain document URI so that we can get the full path
68 // to the root.
69 final String docId = DocumentsContract.getDocumentId(uris[0]);
70 docUri = DocumentsContract.buildDocumentUri(uris[0].getAuthority(), docId);
71 } else {
72 docUri = uris[0];
73 }
74
Garfield Tan16868832016-09-26 10:01:45 -070075 try {
Garfield Tanb00bbc52016-11-01 14:23:35 -070076 final Path path = mDocs.findDocumentPath(docUri);
Garfield Tan16868832016-09-26 10:01:45 -070077 if (path != null) {
Garfield Tan2a837422016-10-19 11:50:45 -070078 return buildStack(docUri.getAuthority(), path);
Garfield Tan16868832016-09-26 10:01:45 -070079 } else {
Garfield Tanb00bbc52016-11-01 14:23:35 -070080 Log.i(TAG, "Remote provider doesn't support findDocumentPath.");
Garfield Tan16868832016-09-26 10:01:45 -070081 }
82 } catch (Exception e) {
Garfield Tan2a837422016-10-19 11:50:45 -070083 Log.e(TAG, "Failed to build document stack for uri: " + docUri, e);
Garfield Tan16868832016-09-26 10:01:45 -070084 }
85 }
86
87 return null;
88 }
89
90 @Override
91 public void finish(@Nullable DocumentStack stack){
92 mCallback.onDocumentStackLoaded(stack);
93 }
94
Garfield Tan2a837422016-10-19 11:50:45 -070095 private DocumentStack buildStack(String authority, Path path) throws Exception {
Garfield Tan16868832016-09-26 10:01:45 -070096 final String rootId = path.getRootId();
97 if (rootId == null) {
Garfield Tan2a837422016-10-19 11:50:45 -070098 throw new IllegalStateException("Provider doesn't provider root id.");
Garfield Tan16868832016-09-26 10:01:45 -070099 }
100
Jon Mann9bd40992017-03-24 12:34:34 -0700101 RootInfo root = mProviders.getRootOneshot(authority, path.getRootId());
Garfield Tan2a837422016-10-19 11:50:45 -0700102 if (root == null) {
103 throw new IllegalStateException("Failed to load root for authority: " + authority +
104 " and root ID: " + path.getRootId() + ".");
Garfield Tan16868832016-09-26 10:01:45 -0700105 }
106
Garfield Tan2a837422016-10-19 11:50:45 -0700107 List<DocumentInfo> docs = mDocs.getDocuments(authority, path.getPath());
108
Garfield Tan16868832016-09-26 10:01:45 -0700109 return new DocumentStack(root, docs);
110 }
111
112 @FunctionalInterface
113 public interface LoadDocStackCallback {
114 void onDocumentStackLoaded(@Nullable DocumentStack stack);
115 }
116}