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 19 import static android.content.ContentResolver.wrap; 20 21 import static com.android.documentsui.base.SharedMinimal.TAG; 22 23 import android.app.Dialog; 24 import android.content.ContentProviderClient; 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.content.DialogInterface; 28 import android.content.DialogInterface.OnClickListener; 29 import android.net.Uri; 30 import android.os.AsyncTask; 31 import android.os.Bundle; 32 import android.os.FileUtils; 33 import android.provider.DocumentsContract; 34 import android.provider.DocumentsContract.Document; 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 44 import androidx.annotation.Nullable; 45 import androidx.appcompat.app.AlertDialog; 46 import androidx.fragment.app.DialogFragment; 47 import androidx.fragment.app.FragmentManager; 48 49 import com.android.documentsui.base.DocumentInfo; 50 import com.android.documentsui.base.Shared; 51 import com.android.documentsui.ui.Snackbars; 52 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 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 show(FragmentManager fm)63 public static void show(FragmentManager fm) { 64 final CreateDirectoryFragment dialog = new CreateDirectoryFragment(); 65 dialog.show(fm, TAG_CREATE_DIRECTORY); 66 } 67 68 @Override onCreateDialog(Bundle savedInstanceState)69 public Dialog onCreateDialog(Bundle savedInstanceState) { 70 final Context context = getActivity(); 71 72 final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context); 73 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); 74 75 final View view = dialogInflater.inflate(R.layout.dialog_file_name, null, false); 76 final EditText editText = (EditText) view.findViewById(android.R.id.text1); 77 78 final TextInputLayout inputWrapper = view.findViewById(R.id.input_wrapper); 79 inputWrapper.setHint(getString(R.string.input_hint_new_folder)); 80 81 builder.setTitle(R.string.menu_create_dir); 82 builder.setView(view); 83 84 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 }); 92 93 builder.setNegativeButton(android.R.string.cancel, null); 94 final AlertDialog dialog = builder.create(); 95 96 // Workaround for the problem - virtual keyboard doesn't show on the phone. 97 Shared.ensureKeyboardPresent(context, dialog); 98 99 editText.setOnEditorActionListener( 100 new OnEditorActionListener() { 101 @Override 102 public boolean onEditorAction( 103 TextView view, int actionId, @Nullable KeyEvent event) { 104 if ((actionId == EditorInfo.IME_ACTION_DONE) || (event != null 105 && event.getKeyCode() == KeyEvent.KEYCODE_ENTER 106 && event.hasNoModifiers())) { 107 createDirectory(editText.getText().toString()); 108 dialog.dismiss(); 109 return true; 110 } 111 return false; 112 } 113 }); 114 editText.requestFocus(); 115 116 return dialog; 117 } 118 createDirectory(String name)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)); 125 } 126 127 private class CreateDirectoryTask extends AsyncTask<Void, Void, DocumentInfo> { 128 private final BaseActivity mActivity; 129 private final DocumentInfo mCwd; 130 private final String mDisplayName; 131 CreateDirectoryTask( BaseActivity activity, DocumentInfo cwd, String displayName)132 public CreateDirectoryTask( 133 BaseActivity activity, DocumentInfo cwd, String displayName) { 134 mActivity = activity; 135 mCwd = cwd; 136 mDisplayName = displayName; 137 } 138 139 @Override onPreExecute()140 protected void onPreExecute() { 141 mActivity.setPending(true); 142 } 143 144 @Override doInBackground(Void... params)145 protected DocumentInfo doInBackground(Void... params) { 146 final ContentResolver resolver = mActivity.getContentResolver(); 147 ContentProviderClient client = null; 148 try { 149 client = DocumentsApplication.acquireUnstableProviderOrThrow( 150 resolver, mCwd.derivedUri.getAuthority()); 151 final Uri childUri = DocumentsContract.createDocument( 152 wrap(client), mCwd.derivedUri, Document.MIME_TYPE_DIR, mDisplayName); 153 DocumentInfo doc = DocumentInfo.fromUri(resolver, childUri); 154 return doc.isDirectory() ? doc : null; 155 } catch (Exception e) { 156 Log.w(TAG, "Failed to create directory", e); 157 return null; 158 } finally { 159 FileUtils.closeQuietly(client); 160 } 161 } 162 163 @Override onPostExecute(DocumentInfo result)164 protected void onPostExecute(DocumentInfo result) { 165 if (result != null) { 166 // Navigate into newly created child 167 mActivity.onDirectoryCreated(result); 168 Metrics.logCreateDirOperation(); 169 } else { 170 Snackbars.makeSnackbar(mActivity, R.string.create_error, Snackbar.LENGTH_SHORT) 171 .show(); 172 Metrics.logCreateDirError(); 173 } 174 mActivity.setPending(false); 175 } 176 } 177 } 178