Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 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 android.annotation.Nullable; |
| 20 | import android.app.Activity; |
| 21 | import android.net.Uri; |
Garfield Tan | fc72617 | 2016-11-04 17:49:29 -0700 | [diff] [blame] | 22 | import android.provider.DocumentsContract; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 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; |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 30 | import com.android.documentsui.roots.ProvidersAccess; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 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 | */ |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 43 | public class LoadDocStackTask extends PairedTask<Activity, Uri, DocumentStack> { |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 44 | private static final String TAG = "LoadDocStackTask"; |
| 45 | |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 46 | private final ProvidersAccess mProviders; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 47 | private final DocumentsAccess mDocs; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 48 | private final LoadDocStackCallback mCallback; |
| 49 | |
| 50 | public LoadDocStackTask( |
| 51 | Activity activity, |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 52 | ProvidersAccess providers, |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 53 | DocumentsAccess docs, |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 54 | LoadDocStackCallback callback) { |
| 55 | super(activity); |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 56 | mProviders = providers; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 57 | mDocs = docs; |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 58 | mCallback = callback; |
| 59 | } |
| 60 | |
| 61 | @Override |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 62 | public @Nullable DocumentStack run(Uri... uris) { |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 63 | // assert(Features.OMC_RUNTIME); |
| 64 | if (mDocs.isDocumentUri(uris[0])) { |
Garfield Tan | fc72617 | 2016-11-04 17:49:29 -0700 | [diff] [blame] | 65 | 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 Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 75 | try { |
Garfield Tan | b00bbc5 | 2016-11-01 14:23:35 -0700 | [diff] [blame] | 76 | final Path path = mDocs.findDocumentPath(docUri); |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 77 | if (path != null) { |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 78 | return buildStack(docUri.getAuthority(), path); |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 79 | } else { |
Garfield Tan | b00bbc5 | 2016-11-01 14:23:35 -0700 | [diff] [blame] | 80 | Log.i(TAG, "Remote provider doesn't support findDocumentPath."); |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 81 | } |
| 82 | } catch (Exception e) { |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 83 | Log.e(TAG, "Failed to build document stack for uri: " + docUri, e); |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void finish(@Nullable DocumentStack stack){ |
| 92 | mCallback.onDocumentStackLoaded(stack); |
| 93 | } |
| 94 | |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 95 | private DocumentStack buildStack(String authority, Path path) throws Exception { |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 96 | final String rootId = path.getRootId(); |
| 97 | if (rootId == null) { |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 98 | throw new IllegalStateException("Provider doesn't provider root id."); |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jon Mann | 9bd4099 | 2017-03-24 12:34:34 -0700 | [diff] [blame] | 101 | RootInfo root = mProviders.getRootOneshot(authority, path.getRootId()); |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 102 | if (root == null) { |
| 103 | throw new IllegalStateException("Failed to load root for authority: " + authority + |
| 104 | " and root ID: " + path.getRootId() + "."); |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 107 | List<DocumentInfo> docs = mDocs.getDocuments(authority, path.getPath()); |
| 108 | |
Garfield Tan | 1686883 | 2016-09-26 10:01:45 -0700 | [diff] [blame] | 109 | return new DocumentStack(root, docs); |
| 110 | } |
| 111 | |
| 112 | @FunctionalInterface |
| 113 | public interface LoadDocStackCallback { |
| 114 | void onDocumentStackLoaded(@Nullable DocumentStack stack); |
| 115 | } |
| 116 | } |