Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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; |
| 18 | |
Jeff Sharkey | 2c0b485 | 2019-02-15 15:53:47 -0700 | [diff] [blame] | 19 | import static android.content.ContentResolver.wrap; |
| 20 | |
Felipe Leme | 9de5807 | 2018-01-19 16:40:04 -0800 | [diff] [blame] | 21 | import static com.android.documentsui.base.SharedMinimal.TAG; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 22 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 23 | import android.app.Dialog; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 24 | import android.content.ContentProviderClient; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 25 | import android.content.ContentResolver; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.DialogInterface; |
| 28 | import android.content.DialogInterface.OnClickListener; |
| 29 | import android.net.Uri; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 30 | import android.os.AsyncTask; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 31 | import android.os.Bundle; |
Jeff Sharkey | bb68a65 | 2019-02-19 11:17:30 -0700 | [diff] [blame] | 32 | import android.os.FileUtils; |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 33 | import android.provider.DocumentsContract; |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 34 | import android.provider.DocumentsContract.Document; |
Tony Huang | 03ef741 | 2019-05-03 19:18:02 +0800 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | import android.view.KeyEvent; |
| 37 | import android.view.LayoutInflater; |
| 38 | import android.view.View; |
| 39 | import android.view.inputmethod.EditorInfo; |
| 40 | import android.widget.EditText; |
| 41 | import android.widget.TextView; |
| 42 | import android.widget.TextView.OnEditorActionListener; |
| 43 | |
KOUSHIK PANUGANTI | 6ca7acc | 2018-04-17 16:00:10 -0700 | [diff] [blame] | 44 | import androidx.annotation.Nullable; |
Tony Huang | eb43e96 | 2018-12-19 15:45:36 +0800 | [diff] [blame] | 45 | import androidx.appcompat.app.AlertDialog; |
Tony Huang | 8d8d92f | 2018-09-13 14:41:16 +0800 | [diff] [blame] | 46 | import androidx.fragment.app.DialogFragment; |
| 47 | import androidx.fragment.app.FragmentManager; |
| 48 | |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 49 | import com.android.documentsui.base.DocumentInfo; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 50 | import com.android.documentsui.base.Shared; |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 51 | import com.android.documentsui.ui.Snackbars; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 52 | |
Tony Huang | 03ef741 | 2019-05-03 19:18:02 +0800 | [diff] [blame] | 53 | import com.google.android.material.dialog.MaterialAlertDialogBuilder; |
| 54 | import com.google.android.material.snackbar.Snackbar; |
| 55 | import com.google.android.material.textfield.TextInputLayout; |
| 56 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Dialog to create a new directory. |
| 59 | */ |
| 60 | public class CreateDirectoryFragment extends DialogFragment { |
| 61 | private static final String TAG_CREATE_DIRECTORY = "create_directory"; |
| 62 | |
| 63 | public static void show(FragmentManager fm) { |
Tony Huang | 2732923 | 2019-10-03 14:37:47 +0800 | [diff] [blame] | 64 | if (fm.isStateSaved()) { |
| 65 | Log.w(TAG, "Skip show create folder dialog because state saved"); |
| 66 | return; |
| 67 | } |
| 68 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 69 | final CreateDirectoryFragment dialog = new CreateDirectoryFragment(); |
| 70 | dialog.show(fm, TAG_CREATE_DIRECTORY); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 75 | final Context context = getActivity(); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 76 | |
Tony Huang | 03ef741 | 2019-05-03 19:18:02 +0800 | [diff] [blame] | 77 | final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 78 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 79 | |
Aga Wronska | 3c23718 | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 80 | final View view = dialogInflater.inflate(R.layout.dialog_file_name, null, false); |
Steve McKay | ceeb3f7 | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 81 | final EditText editText = (EditText) view.findViewById(android.R.id.text1); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 82 | |
Tony Huang | dde4cdd | 2019-01-04 15:15:31 +0800 | [diff] [blame] | 83 | final TextInputLayout inputWrapper = view.findViewById(R.id.input_wrapper); |
| 84 | inputWrapper.setHint(getString(R.string.input_hint_new_folder)); |
| 85 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 86 | builder.setTitle(R.string.menu_create_dir); |
| 87 | builder.setView(view); |
| 88 | |
Steve McKay | ceeb3f7 | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 89 | builder.setPositiveButton( |
| 90 | android.R.string.ok, |
| 91 | new OnClickListener() { |
| 92 | @Override |
| 93 | public void onClick(DialogInterface dialog, int which) { |
| 94 | createDirectory(editText.getText().toString()); |
| 95 | } |
| 96 | }); |
Jeff Sharkey | f63b777 | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 97 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 98 | builder.setNegativeButton(android.R.string.cancel, null); |
Steve McKay | ceeb3f7 | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 99 | final AlertDialog dialog = builder.create(); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 100 | |
Aga Wronska | 741ac6f | 2016-03-02 16:00:22 -0800 | [diff] [blame] | 101 | // Workaround for the problem - virtual keyboard doesn't show on the phone. |
| 102 | Shared.ensureKeyboardPresent(context, dialog); |
| 103 | |
Steve McKay | ceeb3f7 | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 104 | editText.setOnEditorActionListener( |
| 105 | new OnEditorActionListener() { |
| 106 | @Override |
Steve McKay | e9ec61e | 2015-12-01 15:00:43 -0800 | [diff] [blame] | 107 | public boolean onEditorAction( |
| 108 | TextView view, int actionId, @Nullable KeyEvent event) { |
Aga Wronska | 279ec31 | 2016-02-08 09:14:25 -0800 | [diff] [blame] | 109 | if ((actionId == EditorInfo.IME_ACTION_DONE) || (event != null |
Steve McKay | e9ec61e | 2015-12-01 15:00:43 -0800 | [diff] [blame] | 110 | && event.getKeyCode() == KeyEvent.KEYCODE_ENTER |
Aga Wronska | 279ec31 | 2016-02-08 09:14:25 -0800 | [diff] [blame] | 111 | && event.hasNoModifiers())) { |
Steve McKay | ceeb3f7 | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 112 | createDirectory(editText.getText().toString()); |
| 113 | dialog.dismiss(); |
| 114 | return true; |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | }); |
Tony Huang | dde4cdd | 2019-01-04 15:15:31 +0800 | [diff] [blame] | 119 | editText.requestFocus(); |
Steve McKay | ceeb3f7 | 2015-05-19 16:10:25 -0700 | [diff] [blame] | 120 | |
| 121 | return dialog; |
| 122 | } |
| 123 | |
| 124 | private void createDirectory(String name) { |
| 125 | final BaseActivity activity = (BaseActivity) getActivity(); |
| 126 | final DocumentInfo cwd = activity.getCurrentDirectory(); |
| 127 | |
| 128 | new CreateDirectoryTask(activity, cwd, name).executeOnExecutor( |
| 129 | ProviderExecutor.forAuthority(cwd.authority)); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 130 | } |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 131 | |
| 132 | private class CreateDirectoryTask extends AsyncTask<Void, Void, DocumentInfo> { |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 133 | private final BaseActivity mActivity; |
Jeff Sharkey | 0aa2692 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 134 | private final DocumentInfo mCwd; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 135 | private final String mDisplayName; |
| 136 | |
Jeff Sharkey | 0aa2692 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 137 | public CreateDirectoryTask( |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 138 | BaseActivity activity, DocumentInfo cwd, String displayName) { |
Jeff Sharkey | 0aa2692 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 139 | mActivity = activity; |
| 140 | mCwd = cwd; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 141 | mDisplayName = displayName; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | protected DocumentInfo doInBackground(Void... params) { |
Jeff Sharkey | 0aa2692 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 146 | final ContentResolver resolver = mActivity.getContentResolver(); |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 147 | ContentProviderClient client = null; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 148 | try { |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 149 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
Jeff Sharkey | 0aa2692 | 2013-10-08 17:09:18 -0700 | [diff] [blame] | 150 | resolver, mCwd.derivedUri.getAuthority()); |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 151 | final Uri childUri = DocumentsContract.createDocument( |
Jeff Sharkey | 2c0b485 | 2019-02-15 15:53:47 -0700 | [diff] [blame] | 152 | wrap(client), mCwd.derivedUri, Document.MIME_TYPE_DIR, mDisplayName); |
Takamasa Kuramitsu | f49dfeb | 2017-12-05 10:55:44 +0900 | [diff] [blame] | 153 | DocumentInfo doc = DocumentInfo.fromUri(resolver, childUri); |
| 154 | return doc.isDirectory() ? doc : null; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 155 | } catch (Exception e) { |
| 156 | Log.w(TAG, "Failed to create directory", e); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 157 | return null; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 158 | } finally { |
Jeff Sharkey | bb68a65 | 2019-02-19 11:17:30 -0700 | [diff] [blame] | 159 | FileUtils.closeQuietly(client); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | protected void onPostExecute(DocumentInfo result) { |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 165 | if (result != null) { |
| 166 | // Navigate into newly created child |
Steve McKay | 351a749 | 2015-08-04 10:11:01 -0700 | [diff] [blame] | 167 | mActivity.onDirectoryCreated(result); |
shawnlin | 9cee68f | 2019-01-25 11:20:18 +0800 | [diff] [blame] | 168 | Metrics.logCreateDirOperation(); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 169 | } else { |
Aga Wronska | 46a868a | 2016-03-30 10:57:04 -0700 | [diff] [blame] | 170 | Snackbars.makeSnackbar(mActivity, R.string.create_error, Snackbar.LENGTH_SHORT) |
| 171 | .show(); |
shawnlin | 9cee68f | 2019-01-25 11:20:18 +0800 | [diff] [blame] | 172 | Metrics.logCreateDirError(); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 176 | } |