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