Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.dirlist; |
| 18 | |
| 19 | import static com.android.documentsui.Shared.TAG; |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 20 | |
| 21 | import android.app.AlertDialog; |
| 22 | import android.app.Dialog; |
| 23 | import android.app.DialogFragment; |
| 24 | import android.app.FragmentManager; |
| 25 | import android.content.ContentProviderClient; |
| 26 | import android.content.ContentResolver; |
| 27 | import android.content.Context; |
| 28 | import android.content.DialogInterface; |
| 29 | import android.content.DialogInterface.OnClickListener; |
| 30 | import android.net.Uri; |
| 31 | import android.os.AsyncTask; |
| 32 | import android.os.Bundle; |
| 33 | import android.provider.DocumentsContract; |
| 34 | import android.support.annotation.Nullable; |
| 35 | import android.support.design.widget.Snackbar; |
| 36 | import android.util.Log; |
| 37 | import android.view.KeyEvent; |
| 38 | import android.view.LayoutInflater; |
| 39 | import android.view.View; |
Steve McKay | a1f7680 | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 40 | import android.view.inputmethod.EditorInfo; |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 41 | import android.widget.EditText; |
| 42 | import android.widget.TextView; |
| 43 | import android.widget.TextView.OnEditorActionListener; |
| 44 | |
| 45 | import com.android.documentsui.BaseActivity; |
| 46 | import com.android.documentsui.DocumentsApplication; |
Aga Wronska | 4e8c7f6 | 2016-03-30 10:57:04 -0700 | [diff] [blame] | 47 | import com.android.documentsui.Metrics; |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 48 | import com.android.documentsui.R; |
Aga Wronska | 7113872 | 2016-02-22 17:57:31 -0800 | [diff] [blame] | 49 | import com.android.documentsui.Shared; |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 50 | import com.android.documentsui.Snackbars; |
Ben Kwa | e3aee18 | 2016-02-02 12:11:10 -0800 | [diff] [blame] | 51 | import com.android.documentsui.model.DocumentInfo; |
Aga Wronska | 4041680 | 2016-02-08 09:14:25 -0800 | [diff] [blame] | 52 | |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Dialog to rename file or directory. |
| 55 | */ |
Ben Kwa | e3aee18 | 2016-02-02 12:11:10 -0800 | [diff] [blame] | 56 | public class RenameDocumentFragment extends DialogFragment { |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 57 | private static final String TAG_RENAME_DOCUMENT = "rename_document"; |
| 58 | private DocumentInfo mDocument; |
Aga Wronska | 7113872 | 2016-02-22 17:57:31 -0800 | [diff] [blame] | 59 | private EditText mEditText; |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 60 | |
| 61 | public static void show(FragmentManager fm, DocumentInfo document) { |
| 62 | final RenameDocumentFragment dialog = new RenameDocumentFragment(); |
| 63 | dialog.mDocument = document; |
| 64 | dialog.show(fm, TAG_RENAME_DOCUMENT); |
| 65 | } |
| 66 | |
Aga Wronska | 7113872 | 2016-02-22 17:57:31 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Creates the dialog UI. |
| 69 | * @param savedInstanceState |
| 70 | * @return |
| 71 | */ |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 72 | @Override |
| 73 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 74 | Context context = getActivity(); |
| 75 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 76 | LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 77 | View view = dialogInflater.inflate(R.layout.dialog_file_name, null, false); |
| 78 | |
Aga Wronska | 7113872 | 2016-02-22 17:57:31 -0800 | [diff] [blame] | 79 | mEditText = (EditText) view.findViewById(android.R.id.text1); |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 80 | builder.setTitle(R.string.menu_rename); |
| 81 | builder.setView(view); |
| 82 | |
| 83 | builder.setPositiveButton( |
| 84 | android.R.string.ok, |
| 85 | new OnClickListener() { |
| 86 | @Override |
| 87 | public void onClick(DialogInterface dialog, int which) { |
Aga Wronska | 7113872 | 2016-02-22 17:57:31 -0800 | [diff] [blame] | 88 | renameDocuments(mEditText.getText().toString()); |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 89 | } |
| 90 | }); |
| 91 | |
| 92 | builder.setNegativeButton(android.R.string.cancel, null); |
| 93 | |
| 94 | final AlertDialog dialog = builder.create(); |
| 95 | |
Aga Wronska | 02c532f | 2016-03-02 16:00:22 -0800 | [diff] [blame] | 96 | // Workaround for the problem - virtual keyboard doesn't show on the phone. |
| 97 | Shared.ensureKeyboardPresent(context, dialog); |
| 98 | |
Aga Wronska | 7113872 | 2016-02-22 17:57:31 -0800 | [diff] [blame] | 99 | mEditText.setOnEditorActionListener( |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 100 | new OnEditorActionListener() { |
| 101 | @Override |
| 102 | public boolean onEditorAction( |
| 103 | TextView view, int actionId, @Nullable KeyEvent event) { |
Aga Wronska | 4041680 | 2016-02-08 09:14:25 -0800 | [diff] [blame] | 104 | if ((actionId == EditorInfo.IME_ACTION_DONE) || (event != null |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 105 | && event.getKeyCode() == KeyEvent.KEYCODE_ENTER |
Aga Wronska | 4041680 | 2016-02-08 09:14:25 -0800 | [diff] [blame] | 106 | && event.hasNoModifiers())) { |
Aga Wronska | 7113872 | 2016-02-22 17:57:31 -0800 | [diff] [blame] | 107 | renameDocuments(mEditText.getText().toString()); |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 108 | dialog.dismiss(); |
| 109 | return true; |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | }); |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 114 | return dialog; |
| 115 | } |
| 116 | |
Aga Wronska | 4041680 | 2016-02-08 09:14:25 -0800 | [diff] [blame] | 117 | /** |
Aga Wronska | 7113872 | 2016-02-22 17:57:31 -0800 | [diff] [blame] | 118 | * Sets/Restores the data. |
| 119 | * @param savedInstanceState |
| 120 | * @return |
| 121 | */ |
| 122 | @Override |
| 123 | public void onActivityCreated(Bundle savedInstanceState) { |
| 124 | super.onActivityCreated(savedInstanceState); |
| 125 | |
| 126 | if(savedInstanceState == null) { |
| 127 | // Fragment created for the first time, we set the text. |
| 128 | // mDocument value was set in show |
| 129 | mEditText.setText(mDocument.displayName); |
| 130 | } |
| 131 | else { |
| 132 | // Fragment restored, text was restored automatically. |
| 133 | // mDocument value needs to be restored. |
| 134 | mDocument = savedInstanceState.getParcelable(Shared.EXTRA_DOC); |
| 135 | } |
| 136 | // Do selection in both cases, because we cleared it. |
| 137 | selectFileName(mEditText); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void onSaveInstanceState(Bundle outState) { |
| 142 | // Clear selection before storing state and restore it manually, |
| 143 | // because otherwise after rotation selection is displayed with cut/copy menu visible :/ |
| 144 | clearFileNameSelection(mEditText); |
| 145 | |
| 146 | super.onSaveInstanceState(outState); |
| 147 | |
| 148 | outState.putParcelable(Shared.EXTRA_DOC, mDocument); |
| 149 | } |
| 150 | |
| 151 | /** |
Aga Wronska | 4041680 | 2016-02-08 09:14:25 -0800 | [diff] [blame] | 152 | * Validates if string is a proper document name. |
| 153 | * Checks if string is not empty. More rules might be added later. |
| 154 | * @param docName string representing document name |
| 155 | * @returns true if string is a valid name. |
| 156 | **/ |
| 157 | private boolean isValidDocumentName(String docName) { |
| 158 | return !docName.isEmpty(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Fills text field with the file name and selects the name without extension. |
| 163 | * |
| 164 | * @param editText text field to be filled |
Aga Wronska | 4041680 | 2016-02-08 09:14:25 -0800 | [diff] [blame] | 165 | */ |
Aga Wronska | 7113872 | 2016-02-22 17:57:31 -0800 | [diff] [blame] | 166 | private void selectFileName(EditText editText) { |
| 167 | String text = editText.getText().toString(); |
| 168 | int separatorIndex = text.indexOf("."); |
Aga Wronska | 568b3b3 | 2016-02-23 10:49:29 -0800 | [diff] [blame] | 169 | editText.setSelection(0, |
| 170 | (separatorIndex == -1 || mDocument.isDirectory()) ? text.length() : separatorIndex); |
Aga Wronska | 7113872 | 2016-02-22 17:57:31 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Clears selection in text field. |
| 175 | * |
| 176 | * @param editText text field to be cleared. |
| 177 | */ |
| 178 | private void clearFileNameSelection(EditText editText) { |
| 179 | editText.setSelection(0, 0); |
Aga Wronska | 4041680 | 2016-02-08 09:14:25 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 182 | private void renameDocuments(String newDisplayName) { |
| 183 | BaseActivity activity = (BaseActivity) getActivity(); |
| 184 | |
Aga Wronska | 4041680 | 2016-02-08 09:14:25 -0800 | [diff] [blame] | 185 | if (isValidDocumentName(newDisplayName)) { |
| 186 | new RenameDocumentsTask(activity, newDisplayName).execute(mDocument); |
| 187 | } else { |
| 188 | Log.w(TAG, "Failed to rename file - invalid name:" + newDisplayName); |
| 189 | Snackbars.makeSnackbar(getActivity(), R.string.rename_error, |
| 190 | Snackbar.LENGTH_SHORT).show(); |
| 191 | } |
| 192 | |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | private class RenameDocumentsTask extends AsyncTask<DocumentInfo, Void, DocumentInfo> { |
| 196 | private final BaseActivity mActivity; |
| 197 | private final String mNewDisplayName; |
| 198 | |
| 199 | public RenameDocumentsTask(BaseActivity activity, String newDisplayName) { |
| 200 | mActivity = activity; |
| 201 | mNewDisplayName = newDisplayName; |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | protected void onPreExecute() { |
| 206 | mActivity.setPending(true); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | protected DocumentInfo doInBackground(DocumentInfo... document) { |
Steve McKay | a1f7680 | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 211 | assert(document.length == 1); |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 212 | final ContentResolver resolver = mActivity.getContentResolver(); |
| 213 | ContentProviderClient client = null; |
| 214 | |
| 215 | try { |
| 216 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 217 | resolver, document[0].derivedUri.getAuthority()); |
| 218 | Uri newUri = DocumentsContract.renameDocument( |
| 219 | client, document[0].derivedUri, mNewDisplayName); |
| 220 | return DocumentInfo.fromUri(resolver, newUri); |
| 221 | } catch (Exception e) { |
| 222 | Log.w(TAG, "Failed to rename file", e); |
| 223 | return null; |
| 224 | } finally { |
| 225 | ContentProviderClient.releaseQuietly(client); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | protected void onPostExecute(DocumentInfo result) { |
Aga Wronska | 4e8c7f6 | 2016-03-30 10:57:04 -0700 | [diff] [blame] | 231 | if (result != null) { |
| 232 | Metrics.logRenameFileOperation(getContext()); |
| 233 | } else { |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 234 | Snackbars.makeSnackbar(mActivity, R.string.rename_error, Snackbar.LENGTH_SHORT) |
| 235 | .show(); |
Aga Wronska | 4e8c7f6 | 2016-03-30 10:57:04 -0700 | [diff] [blame] | 236 | Metrics.logRenameFileError(getContext()); |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 237 | } |
Aga Wronska | 3b327ef | 2016-01-20 16:32:33 -0800 | [diff] [blame] | 238 | mActivity.setPending(false); |
| 239 | } |
| 240 | } |
| 241 | } |