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.Shared.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 com.android.documentsui.AbstractActionHandler.CommonAddons;
27 import com.android.documentsui.base.PairedTask;
28 import com.android.documentsui.base.RootInfo;
29 import com.android.documentsui.base.State;
30 
31 public final class LoadRootTask<T extends Activity & CommonAddons>
32         extends PairedTask<T, Void, RootInfo> {
33     private static final String TAG = "LoadRootTask";
34 
35     private final State mState;
36     private final ProvidersAccess mProviders;
37     private final Uri mRootUri;
38 
39 
LoadRootTask(T activity, ProvidersAccess providers, State state, Uri rootUri)40     public LoadRootTask(T activity, ProvidersAccess providers, State state, Uri rootUri) {
41         super(activity);
42         mState = state;
43         mProviders = providers;
44         mRootUri = rootUri;
45     }
46 
47     @Override
run(Void... params)48     protected RootInfo run(Void... params) {
49         if (DEBUG) Log.d(TAG, "Loading root: " + mRootUri);
50 
51         String rootId = DocumentsContract.getRootId(mRootUri);
52         return mProviders.getRootOneshot(mRootUri.getAuthority(), rootId);
53     }
54 
55     @Override
finish(RootInfo root)56     protected void finish(RootInfo root) {
57         if (root != null) {
58             if (DEBUG) Log.d(TAG, "Loaded root: " + root);
59             mOwner.onRootPicked(root);
60         } else {
61             Log.w(TAG, "Failed to find root: " + mRootUri);
62             mOwner.finish();
63         }
64     }
65 }
66