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
17package com.android.documentsui.picker;
18
19import android.app.Activity;
20
21import com.android.documentsui.AbstractActionHandler.CommonAddons;
22import com.android.documentsui.base.DocumentStack;
23import com.android.documentsui.base.PairedTask;
24import com.android.documentsui.base.State;
25import com.android.documentsui.roots.ProvidersAccess;
26
27import java.util.function.Consumer;
28
29import javax.annotation.Nullable;
30
31/**
32 * Loads the last used path (stack) from Recents (history).
33 * The path selected is based on the calling package name. So the last
34 * path for an app like Gmail can be different than the last path
35 * for an app like DropBox.
36 */
37final class LoadLastAccessedStackTask<T extends Activity & CommonAddons>
38        extends PairedTask<T, Void, DocumentStack> {
39
40    private final LastAccessedStorage mLastAccessed;
41    private final State mState;
42    private final ProvidersAccess mProviders;
43    private final Consumer<DocumentStack> mCallback;
44
45    LoadLastAccessedStackTask(
46            T activity,
47            LastAccessedStorage lastAccessed,
48            State state,
49            ProvidersAccess providers,
50            Consumer<DocumentStack> callback) {
51        super(activity);
52        mLastAccessed = lastAccessed;
53        mProviders = providers;
54        mState = state;
55        mCallback = callback;
56    }
57
58    @Override
59    protected DocumentStack run(Void... params) {
60        return mLastAccessed.getLastAccessed(mOwner, mProviders, mState);
61    }
62
63    @Override
64    protected void finish(@Nullable DocumentStack stack) {
65        mCallback.accept(stack);
66    }
67}
68