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 androidx.annotation.Nullable;
20 import android.app.Activity;
21 import android.util.Log;
22 
23 import com.android.documentsui.DocumentsAccess;
24 import com.android.documentsui.TimeoutTask;
25 import com.android.documentsui.base.CheckedTask;
26 import com.android.documentsui.base.DocumentInfo;
27 import com.android.documentsui.base.RootInfo;
28 
29 import java.util.function.Consumer;
30 
31 /**
32  * A {@link CheckedTask} that takes {@link RootInfo} and query SAF to obtain the
33  * {@link DocumentInfo} of its root document and call supplied callback to handle the
34  * {@link DocumentInfo}.
35  */
36 public class GetRootDocumentTask extends TimeoutTask<Void, DocumentInfo> {
37 
38     private final static String TAG = "GetRootDocumentTask";
39 
40     private final RootInfo mRootInfo;
41     private final Consumer<DocumentInfo> mCallback;
42     private final DocumentsAccess mDocs;
43 
GetRootDocumentTask( RootInfo rootInfo, Activity activity, long timeout, DocumentsAccess docs, Consumer<DocumentInfo> callback)44     public GetRootDocumentTask(
45             RootInfo rootInfo,
46             Activity activity,
47             long timeout,
48             DocumentsAccess docs,
49             Consumer<DocumentInfo> callback) {
50         super(activity::isDestroyed, timeout);
51         mRootInfo = rootInfo;
52         mDocs = docs;
53         mCallback = callback;
54     }
55 
56     @Override
run(Void... args)57     public @Nullable DocumentInfo run(Void... args) {
58         return mDocs.getRootDocument(mRootInfo);
59     }
60 
61     @Override
finish(@ullable DocumentInfo documentInfo)62     public void finish(@Nullable DocumentInfo documentInfo) {
63         if (documentInfo == null) {
64             Log.e(TAG,
65                     "Cannot find document info for root: " + mRootInfo);
66         }
67 
68         mCallback.accept(documentInfo);
69     }
70 }
71