blob: dcaa87a103388297b993daf6267f629966467318 [file] [log] [blame]
Jeff Sharkey76112212013-08-06 11:26:10 -07001/*
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
17package com.android.documentsui;
18
Jeff Sharkey2c0b4852019-02-15 15:53:47 -070019import static android.content.ContentResolver.wrap;
20
Felipe Leme9de58072018-01-19 16:40:04 -080021import static com.android.documentsui.base.SharedMinimal.TAG;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070022
Jeff Sharkey76112212013-08-06 11:26:10 -070023import android.app.Dialog;
Jeff Sharkey3fd11772013-09-30 14:26:27 -070024import android.content.ContentProviderClient;
Jeff Sharkey76112212013-08-06 11:26:10 -070025import android.content.ContentResolver;
Jeff Sharkey76112212013-08-06 11:26:10 -070026import android.content.Context;
27import android.content.DialogInterface;
28import android.content.DialogInterface.OnClickListener;
29import android.net.Uri;
Jeff Sharkey9dd02622013-09-27 16:44:11 -070030import android.os.AsyncTask;
Jeff Sharkey76112212013-08-06 11:26:10 -070031import android.os.Bundle;
Jeff Sharkeybb68a652019-02-19 11:17:30 -070032import android.os.FileUtils;
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -070033import android.provider.DocumentsContract;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070034import android.provider.DocumentsContract.Document;
Tony Huang03ef7412019-05-03 19:18:02 +080035import android.util.Log;
36import android.view.KeyEvent;
37import android.view.LayoutInflater;
38import android.view.View;
39import android.view.inputmethod.EditorInfo;
40import android.widget.EditText;
41import android.widget.TextView;
42import android.widget.TextView.OnEditorActionListener;
43
KOUSHIK PANUGANTI6ca7acc2018-04-17 16:00:10 -070044import androidx.annotation.Nullable;
Tony Huangeb43e962018-12-19 15:45:36 +080045import androidx.appcompat.app.AlertDialog;
Tony Huang8d8d92f2018-09-13 14:41:16 +080046import androidx.fragment.app.DialogFragment;
47import androidx.fragment.app.FragmentManager;
48
Steve McKayd0805062016-09-15 14:30:38 -070049import com.android.documentsui.base.DocumentInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070050import com.android.documentsui.base.Shared;
Steve McKayc8889af2016-09-23 11:22:41 -070051import com.android.documentsui.ui.Snackbars;
Jeff Sharkey76112212013-08-06 11:26:10 -070052
Tony Huang03ef7412019-05-03 19:18:02 +080053import com.google.android.material.dialog.MaterialAlertDialogBuilder;
54import com.google.android.material.snackbar.Snackbar;
55import com.google.android.material.textfield.TextInputLayout;
56
Jeff Sharkey76112212013-08-06 11:26:10 -070057/**
58 * Dialog to create a new directory.
59 */
60public class CreateDirectoryFragment extends DialogFragment {
61 private static final String TAG_CREATE_DIRECTORY = "create_directory";
62
63 public static void show(FragmentManager fm) {
64 final CreateDirectoryFragment dialog = new CreateDirectoryFragment();
65 dialog.show(fm, TAG_CREATE_DIRECTORY);
66 }
67
68 @Override
69 public Dialog onCreateDialog(Bundle savedInstanceState) {
70 final Context context = getActivity();
Jeff Sharkey76112212013-08-06 11:26:10 -070071
Tony Huang03ef7412019-05-03 19:18:02 +080072 final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);
Jeff Sharkey76112212013-08-06 11:26:10 -070073 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
74
Aga Wronska3c237182016-01-20 16:32:33 -080075 final View view = dialogInflater.inflate(R.layout.dialog_file_name, null, false);
Steve McKayceeb3f72015-05-19 16:10:25 -070076 final EditText editText = (EditText) view.findViewById(android.R.id.text1);
Jeff Sharkey76112212013-08-06 11:26:10 -070077
Tony Huangdde4cdd2019-01-04 15:15:31 +080078 final TextInputLayout inputWrapper = view.findViewById(R.id.input_wrapper);
79 inputWrapper.setHint(getString(R.string.input_hint_new_folder));
80
Jeff Sharkey76112212013-08-06 11:26:10 -070081 builder.setTitle(R.string.menu_create_dir);
82 builder.setView(view);
83
Steve McKayceeb3f72015-05-19 16:10:25 -070084 builder.setPositiveButton(
85 android.R.string.ok,
86 new OnClickListener() {
87 @Override
88 public void onClick(DialogInterface dialog, int which) {
89 createDirectory(editText.getText().toString());
90 }
91 });
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070092
Jeff Sharkey76112212013-08-06 11:26:10 -070093 builder.setNegativeButton(android.R.string.cancel, null);
Steve McKayceeb3f72015-05-19 16:10:25 -070094 final AlertDialog dialog = builder.create();
Jeff Sharkey76112212013-08-06 11:26:10 -070095
Aga Wronska741ac6f2016-03-02 16:00:22 -080096 // Workaround for the problem - virtual keyboard doesn't show on the phone.
97 Shared.ensureKeyboardPresent(context, dialog);
98
Steve McKayceeb3f72015-05-19 16:10:25 -070099 editText.setOnEditorActionListener(
100 new OnEditorActionListener() {
101 @Override
Steve McKaye9ec61e2015-12-01 15:00:43 -0800102 public boolean onEditorAction(
103 TextView view, int actionId, @Nullable KeyEvent event) {
Aga Wronska279ec312016-02-08 09:14:25 -0800104 if ((actionId == EditorInfo.IME_ACTION_DONE) || (event != null
Steve McKaye9ec61e2015-12-01 15:00:43 -0800105 && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
Aga Wronska279ec312016-02-08 09:14:25 -0800106 && event.hasNoModifiers())) {
Steve McKayceeb3f72015-05-19 16:10:25 -0700107 createDirectory(editText.getText().toString());
108 dialog.dismiss();
109 return true;
110 }
111 return false;
112 }
113 });
Tony Huangdde4cdd2019-01-04 15:15:31 +0800114 editText.requestFocus();
Steve McKayceeb3f72015-05-19 16:10:25 -0700115
116 return dialog;
117 }
118
119 private void createDirectory(String name) {
120 final BaseActivity activity = (BaseActivity) getActivity();
121 final DocumentInfo cwd = activity.getCurrentDirectory();
122
123 new CreateDirectoryTask(activity, cwd, name).executeOnExecutor(
124 ProviderExecutor.forAuthority(cwd.authority));
Jeff Sharkey76112212013-08-06 11:26:10 -0700125 }
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700126
127 private class CreateDirectoryTask extends AsyncTask<Void, Void, DocumentInfo> {
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700128 private final BaseActivity mActivity;
Jeff Sharkey0aa26922013-10-08 17:09:18 -0700129 private final DocumentInfo mCwd;
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700130 private final String mDisplayName;
131
Jeff Sharkey0aa26922013-10-08 17:09:18 -0700132 public CreateDirectoryTask(
Steve McKayd0a2a2c2015-03-25 14:35:33 -0700133 BaseActivity activity, DocumentInfo cwd, String displayName) {
Jeff Sharkey0aa26922013-10-08 17:09:18 -0700134 mActivity = activity;
135 mCwd = cwd;
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700136 mDisplayName = displayName;
137 }
138
139 @Override
Jeff Sharkey4be51f12013-10-23 15:46:38 -0700140 protected void onPreExecute() {
141 mActivity.setPending(true);
142 }
143
144 @Override
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700145 protected DocumentInfo doInBackground(Void... params) {
Jeff Sharkey0aa26922013-10-08 17:09:18 -0700146 final ContentResolver resolver = mActivity.getContentResolver();
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700147 ContentProviderClient client = null;
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700148 try {
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700149 client = DocumentsApplication.acquireUnstableProviderOrThrow(
Jeff Sharkey0aa26922013-10-08 17:09:18 -0700150 resolver, mCwd.derivedUri.getAuthority());
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700151 final Uri childUri = DocumentsContract.createDocument(
Jeff Sharkey2c0b4852019-02-15 15:53:47 -0700152 wrap(client), mCwd.derivedUri, Document.MIME_TYPE_DIR, mDisplayName);
Takamasa Kuramitsuf49dfeb2017-12-05 10:55:44 +0900153 DocumentInfo doc = DocumentInfo.fromUri(resolver, childUri);
154 return doc.isDirectory() ? doc : null;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700155 } catch (Exception e) {
156 Log.w(TAG, "Failed to create directory", e);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700157 return null;
Jeff Sharkey3fd11772013-09-30 14:26:27 -0700158 } finally {
Jeff Sharkeybb68a652019-02-19 11:17:30 -0700159 FileUtils.closeQuietly(client);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700160 }
161 }
162
163 @Override
164 protected void onPostExecute(DocumentInfo result) {
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700165 if (result != null) {
166 // Navigate into newly created child
Steve McKay351a7492015-08-04 10:11:01 -0700167 mActivity.onDirectoryCreated(result);
shawnlin9cee68f2019-01-25 11:20:18 +0800168 Metrics.logCreateDirOperation();
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700169 } else {
Aga Wronska46a868a2016-03-30 10:57:04 -0700170 Snackbars.makeSnackbar(mActivity, R.string.create_error, Snackbar.LENGTH_SHORT)
171 .show();
shawnlin9cee68f2019-01-25 11:20:18 +0800172 Metrics.logCreateDirError();
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700173 }
Jeff Sharkey4be51f12013-10-23 15:46:38 -0700174 mActivity.setPending(false);
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700175 }
176 }
Jeff Sharkey76112212013-08-06 11:26:10 -0700177}