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