blob: 25c57f27fd12dce4d60b9bba36df7332a607c1c0 [file] [log] [blame]
Garfield Tan23ac60c2017-03-13 17:40:43 -07001/*
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
Garfield Tan894d4872017-03-17 12:23:22 -070027import com.android.documentsui.DocumentsAccess;
Garfield Tan23ac60c2017-03-13 17:40:43 -070028import 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> {
Garfield Tan23ac60c2017-03-13 17:40:43 -070042 private final LastAccessedStorage mLastAccessed;
Garfield Tan894d4872017-03-17 12:23:22 -070043 private final DocumentsAccess mDocs;
Garfield Tan23ac60c2017-03-13 17:40:43 -070044 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,
Garfield Tan894d4872017-03-17 12:23:22 -070052 DocumentsAccess docs,
Garfield Tan23ac60c2017-03-13 17:40:43 -070053 LastAccessedStorage lastAccessed,
54 DocumentStack stack,
55 String mimeType,
56 String displayName,
57 BooleanConsumer inProgressStateListener,
58 Consumer<Uri> callback) {
59 super(activity);
60 mLastAccessed = lastAccessed;
Garfield Tan894d4872017-03-17 12:23:22 -070061 mDocs = docs;
Garfield Tan23ac60c2017-03-13 17:40:43 -070062 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
Garfield Tan894d4872017-03-17 12:23:22 -070078 Uri childUri = mDocs.createDocument(cwd, mMimeType, mDisplayName);
Garfield Tan23ac60c2017-03-13 17:40:43 -070079
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}