1/*
2 * Copyright (C) 2017 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;
20import android.content.ContentProviderClient;
21import android.content.ContentResolver;
22import android.net.Uri;
23import android.provider.DocumentsContract;
24import android.support.design.widget.Snackbar;
25import android.util.Log;
26
27import com.android.documentsui.DocumentsAccess;
28import com.android.documentsui.DocumentsApplication;
29import com.android.documentsui.R;
30import com.android.documentsui.base.BooleanConsumer;
31import com.android.documentsui.base.DocumentInfo;
32import com.android.documentsui.base.DocumentStack;
33import com.android.documentsui.base.PairedTask;
34import com.android.documentsui.ui.Snackbars;
35
36import java.util.function.Consumer;
37
38/**
39 * Task that creates a new document in the background.
40 */
41class CreatePickedDocumentTask extends PairedTask<Activity, Void, Uri> {
42    private final LastAccessedStorage mLastAccessed;
43    private final DocumentsAccess mDocs;
44    private final DocumentStack mStack;
45    private final String mMimeType;
46    private final String mDisplayName;
47    private final BooleanConsumer mInProgressStateListener;
48    private final Consumer<Uri> mCallback;
49
50    CreatePickedDocumentTask(
51            Activity activity,
52            DocumentsAccess docs,
53            LastAccessedStorage lastAccessed,
54            DocumentStack stack,
55            String mimeType,
56            String displayName,
57            BooleanConsumer inProgressStateListener,
58            Consumer<Uri> callback) {
59        super(activity);
60        mLastAccessed = lastAccessed;
61        mDocs = docs;
62        mStack = stack;
63        mMimeType = mimeType;
64        mDisplayName = displayName;
65        mInProgressStateListener = inProgressStateListener;
66        mCallback = callback;
67    }
68
69    @Override
70    protected void prepare() {
71        mInProgressStateListener.accept(true);
72    }
73
74    @Override
75    protected Uri run(Void... params) {
76        DocumentInfo cwd = mStack.peek();
77
78        Uri childUri = mDocs.createDocument(cwd, mMimeType, mDisplayName);
79
80        if (childUri != null) {
81            mLastAccessed.setLastAccessed(mOwner, mStack);
82        }
83
84        return childUri;
85    }
86
87    @Override
88    protected void finish(Uri result) {
89        if (result != null) {
90            mCallback.accept(result);
91        } else {
92            Snackbars.makeSnackbar(
93                    mOwner, R.string.save_error, Snackbar.LENGTH_SHORT).show();
94        }
95
96        mInProgressStateListener.accept(false);
97    }
98}
99