• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.roots;
18 
19 import static com.android.documentsui.base.SharedMinimal.DEBUG;
20 
21 import android.app.Activity;
22 import android.net.Uri;
23 import android.provider.DocumentsContract;
24 import android.util.Log;
25 
26 import androidx.annotation.Nullable;
27 
28 import com.android.documentsui.AbstractActionHandler.CommonAddons;
29 import com.android.documentsui.base.PairedTask;
30 import com.android.documentsui.base.RootInfo;
31 import com.android.documentsui.base.UserId;
32 
33 public class LoadRootTask<T extends Activity & CommonAddons>
34         extends PairedTask<T, Void, RootInfo> {
35     private static final String TAG = "LoadRootTask";
36 
37     protected final ProvidersAccess mProviders;
38     private final Uri mRootUri;
39     private final UserId mUserId;
40     private final LoadRootCallback mCallback;
41 
LoadRootTask( T activity, ProvidersAccess providers, Uri rootUri, UserId userId, LoadRootCallback callback)42     public LoadRootTask(
43             T activity,
44             ProvidersAccess providers,
45             Uri rootUri,
46             UserId userId,
47             LoadRootCallback callback) {
48         super(activity);
49         mProviders = providers;
50         mRootUri = rootUri;
51         mUserId = userId;
52         mCallback = callback;
53     }
54 
55     @Override
run(Void... params)56     protected RootInfo run(Void... params) {
57         if (DEBUG) {
58             Log.d(TAG, "Loading root: " + mRootUri + " on user " + mUserId);
59         }
60 
61         return mProviders.getRootOneshot(mUserId, mRootUri.getAuthority(), getRootId(mRootUri));
62     }
63 
64     @Override
finish(RootInfo root)65     protected void finish(RootInfo root) {
66         if (root != null) {
67             if (DEBUG) {
68                 Log.d(TAG, "Loaded root: " + root);
69             }
70         } else {
71             Log.w(TAG, "Failed to find root: " + mRootUri + " on user " + mUserId);
72         }
73 
74         mCallback.onRootLoaded(root);
75     }
76 
getRootId(Uri rootUri)77     protected String getRootId(Uri rootUri) {
78         return DocumentsContract.getRootId(rootUri);
79     }
80 
81     /**
82      * Callback for task finished.
83      */
84     @FunctionalInterface
85     public interface LoadRootCallback {
86         /**
87          * Return the RootInfo of input uri, null if the uri is invalid.
88          */
onRootLoaded(@ullable RootInfo root)89         void onRootLoaded(@Nullable RootInfo root);
90     }
91 }
92