Home | History | Annotate | Download | only in documentsui
      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;
     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 
     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
     62     public @Nullable DocumentStack run(Uri... uris) {
     63         // assert(Features.OMC_RUNTIME);
     64         if (mDocs.isDocumentUri(uris[0])) {
     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 
     75             try {
     76                 final Path path = mDocs.findDocumentPath(docUri);
     77                 if (path != null) {
     78                     return buildStack(docUri.getAuthority(), path);
     79                 } else {
     80                     Log.i(TAG, "Remote provider doesn't support findDocumentPath.");
     81                 }
     82             } catch (Exception e) {
     83                 Log.e(TAG, "Failed to build document stack for uri: " + docUri, e);
     84             }
     85         }
     86 
     87         return null;
     88     }
     89 
     90     @Override
     91     public void finish(@Nullable DocumentStack stack){
     92         mCallback.onDocumentStackLoaded(stack);
     93     }
     94 
     95     private DocumentStack buildStack(String authority, Path path) throws Exception {
     96         final String rootId = path.getRootId();
     97         if (rootId == null) {
     98             throw new IllegalStateException("Provider doesn't provider root id.");
     99         }
    100 
    101         RootInfo root = mProviders.getRootOneshot(authority, path.getRootId());
    102         if (root == null) {
    103             throw new IllegalStateException("Failed to load root for authority: " + authority +
    104                     " and root ID: " + path.getRootId() + ".");
    105         }
    106 
    107         List<DocumentInfo> docs = mDocs.getDocuments(authority, path.getPath());
    108 
    109         return new DocumentStack(root, docs);
    110     }
    111 
    112     @FunctionalInterface
    113     public interface LoadDocStackCallback {
    114         void onDocumentStackLoaded(@Nullable DocumentStack stack);
    115     }
    116 }
    117