Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 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.support.design.widget.Snackbar; |
| 25 | import android.util.Log; |
| 26 | |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 27 | import com.android.documentsui.DocumentsAccess; |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 28 | import com.android.documentsui.DocumentsApplication; |
| 29 | import com.android.documentsui.R; |
| 30 | import com.android.documentsui.base.BooleanConsumer; |
| 31 | import com.android.documentsui.base.DocumentInfo; |
| 32 | import com.android.documentsui.base.DocumentStack; |
| 33 | import com.android.documentsui.base.PairedTask; |
| 34 | import com.android.documentsui.ui.Snackbars; |
| 35 | |
| 36 | import java.util.function.Consumer; |
| 37 | |
| 38 | /** |
| 39 | * Task that creates a new document in the background. |
| 40 | */ |
| 41 | class CreatePickedDocumentTask extends PairedTask<Activity, Void, Uri> { |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 42 | private final LastAccessedStorage mLastAccessed; |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 43 | private final DocumentsAccess mDocs; |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 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, |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 52 | DocumentsAccess docs, |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 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; |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 61 | mDocs = docs; |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 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 | |
Garfield Tan | 894d487 | 2017-03-17 12:23:22 -0700 | [diff] [blame] | 78 | Uri childUri = mDocs.createDocument(cwd, mMimeType, mDisplayName); |
Garfield Tan | 23ac60c | 2017-03-13 17:40:43 -0700 | [diff] [blame] | 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 | } |