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.base.SharedMinimal.TAG;
20 
21 import android.app.Dialog;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.os.AsyncTask;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.view.KeyEvent;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.inputmethod.EditorInfo;
31 import android.widget.Button;
32 import android.widget.EditText;
33 import android.widget.TextView;
34 import android.widget.TextView.OnEditorActionListener;
35 
36 import androidx.annotation.Nullable;
37 import androidx.appcompat.app.AlertDialog;
38 import androidx.fragment.app.DialogFragment;
39 import androidx.fragment.app.FragmentManager;
40 
41 import com.android.documentsui.BaseActivity;
42 import com.android.documentsui.Metrics;
43 import com.android.documentsui.R;
44 import com.android.documentsui.base.DocumentInfo;
45 import com.android.documentsui.base.Shared;
46 import com.android.documentsui.ui.Snackbars;
47 
48 import com.google.android.material.dialog.MaterialAlertDialogBuilder;
49 import com.google.android.material.textfield.TextInputLayout;
50 
51 /**
52  * Dialog to rename file or directory.
53  */
54 public class RenameDocumentFragment extends DialogFragment {
55     private static final String TAG_RENAME_DOCUMENT = "rename_document";
56     private DocumentInfo mDocument;
57     private EditText mEditText;
58     private TextInputLayout mRenameInputWrapper;
59     private @Nullable DialogInterface mDialog;
60 
show(FragmentManager fm, DocumentInfo document)61     public static void show(FragmentManager fm, DocumentInfo document) {
62         if (fm.isStateSaved()) {
63             Log.w(TAG, "Skip show rename dialog because state saved");
64             return;
65         }
66 
67         final RenameDocumentFragment dialog = new RenameDocumentFragment();
68         dialog.mDocument = document;
69         dialog.show(fm, TAG_RENAME_DOCUMENT);
70     }
71 
72     /**
73      * Creates the dialog UI.
74      * @param savedInstanceState
75      * @return
76      */
77     @Override
onCreateDialog(Bundle savedInstanceState)78     public Dialog onCreateDialog(Bundle savedInstanceState) {
79         Context context = getActivity();
80         MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);
81         LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
82         View view = dialogInflater.inflate(R.layout.dialog_file_name, null, false);
83 
84         mEditText = (EditText) view.findViewById(android.R.id.text1);
85         mRenameInputWrapper = (TextInputLayout) view.findViewById(R.id.input_wrapper);
86         mRenameInputWrapper.setHint(getString(R.string.input_hint_rename));
87         builder.setTitle(R.string.menu_rename);
88         builder.setView(view);
89         builder.setPositiveButton(android.R.string.ok, null);
90         builder.setNegativeButton(android.R.string.cancel, null);
91 
92         final AlertDialog dialog = builder.create();
93 
94         dialog.setOnShowListener(this::onShowDialog);
95 
96         // Workaround for the problem - virtual keyboard doesn't show on the phone.
97         Shared.ensureKeyboardPresent(context, dialog);
98 
99         mEditText.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                             renameDocuments(mEditText.getText().toString());
108                         }
109                         return false;
110                     }
111                 });
112         mEditText.requestFocus();
113         return dialog;
114     }
115 
onShowDialog(DialogInterface dialog)116     private void onShowDialog(DialogInterface dialog){
117         mDialog = dialog;
118         Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
119         button.setOnClickListener(this::onClickDialog);
120     }
121 
onClickDialog(View view)122     private void onClickDialog(View view) {
123         renameDocuments(mEditText.getText().toString());
124     }
125 
126     /**
127      * Sets/Restores the data.
128      * @param savedInstanceState
129      * @return
130      */
131     @Override
onActivityCreated(Bundle savedInstanceState)132     public void onActivityCreated(Bundle savedInstanceState) {
133         super.onActivityCreated(savedInstanceState);
134 
135         if(savedInstanceState == null) {
136             // Fragment created for the first time, we set the text.
137             // mDocument value was set in show
138             mEditText.setText(mDocument.displayName);
139         }
140         else {
141             // Fragment restored, text was restored automatically.
142             // mDocument value needs to be restored.
143             mDocument = savedInstanceState.getParcelable(Shared.EXTRA_DOC);
144         }
145         // Do selection in both cases, because we cleared it.
146         selectFileName(mEditText);
147     }
148 
149     @Override
onSaveInstanceState(Bundle outState)150     public void onSaveInstanceState(Bundle outState) {
151         // Clear selection before storing state and restore it manually,
152         // because otherwise after rotation selection is displayed with cut/copy menu visible :/
153         clearFileNameSelection(mEditText);
154 
155         super.onSaveInstanceState(outState);
156 
157         outState.putParcelable(Shared.EXTRA_DOC, mDocument);
158     }
159 
160     /**
161      * Fills text field with the file name and selects the name without extension.
162      *
163      * @param editText text field to be filled
164      */
selectFileName(EditText editText)165     private void selectFileName(EditText editText) {
166         String text = editText.getText().toString();
167         int separatorIndex = text.lastIndexOf(".");
168         editText.setSelection(0,
169                 (separatorIndex == -1 || mDocument.isDirectory()) ? text.length() : separatorIndex);
170     }
171 
172     /**
173      * Clears selection in text field.
174      *
175      * @param editText text field to be cleared.
176      */
clearFileNameSelection(EditText editText)177     private void clearFileNameSelection(EditText editText) {
178         editText.setSelection(0, 0);
179     }
180 
renameDocuments(String newDisplayName)181     private void renameDocuments(String newDisplayName) {
182         BaseActivity activity = (BaseActivity) getActivity();
183 
184         if (newDisplayName.equals(mDocument.displayName)) {
185             mDialog.dismiss();
186         } else if (newDisplayName.isEmpty()) {
187             mRenameInputWrapper.setError(getContext().getString(R.string.missing_rename_error));
188         } else if (activity.getInjector().getModel().hasFileWithName(newDisplayName)) {
189             mRenameInputWrapper.setError(getContext().getString(R.string.name_conflict));
190             selectFileName(mEditText);
191         } else {
192             new RenameDocumentsTask(activity, newDisplayName).execute(mDocument);
193             if (mDialog != null) {
194                 mDialog.dismiss();
195             }
196             activity.getInjector().selectionMgr.clearSelection();
197         }
198 
199     }
200 
201     private class RenameDocumentsTask extends AsyncTask<DocumentInfo, Void, DocumentInfo> {
202         private final BaseActivity mActivity;
203         private final String mNewDisplayName;
204 
RenameDocumentsTask(BaseActivity activity, String newDisplayName)205         public RenameDocumentsTask(BaseActivity activity, String newDisplayName) {
206             mActivity = activity;
207             mNewDisplayName = newDisplayName;
208         }
209 
210         @Override
doInBackground(DocumentInfo... document)211         protected DocumentInfo doInBackground(DocumentInfo... document) {
212             assert(document.length == 1);
213 
214             return mActivity.getInjector().actions.renameDocument(mNewDisplayName, document[0]);
215         }
216 
217         @Override
onPostExecute(DocumentInfo result)218         protected void onPostExecute(DocumentInfo result) {
219             if (result != null) {
220                 Metrics.logRenameFileOperation();
221             } else {
222                 Snackbars.showRenameFailed(mActivity);
223                 Metrics.logRenameFileError();
224             }
225             mActivity.reloadDocumentsIfNeeded();
226         }
227     }
228 }
229