blob: 0018d01aa85c2c3195872bc3995dfb61e82fe053 [file] [log] [blame]
Aga Wronska3b327ef2016-01-20 16:32:33 -08001/*
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
17package com.android.documentsui.dirlist;
18
19import static com.android.documentsui.Shared.TAG;
Aga Wronska3b327ef2016-01-20 16:32:33 -080020
21import android.app.AlertDialog;
22import android.app.Dialog;
23import android.app.DialogFragment;
24import android.app.FragmentManager;
25import android.content.ContentProviderClient;
26import android.content.ContentResolver;
27import android.content.Context;
28import android.content.DialogInterface;
29import android.content.DialogInterface.OnClickListener;
30import android.net.Uri;
31import android.os.AsyncTask;
32import android.os.Bundle;
33import android.provider.DocumentsContract;
34import android.support.annotation.Nullable;
35import android.support.design.widget.Snackbar;
36import android.util.Log;
37import android.view.KeyEvent;
38import android.view.LayoutInflater;
39import android.view.View;
Steve McKaya1f76802016-02-25 13:34:03 -080040import android.view.inputmethod.EditorInfo;
Aga Wronska3b327ef2016-01-20 16:32:33 -080041import android.widget.EditText;
42import android.widget.TextView;
43import android.widget.TextView.OnEditorActionListener;
44
45import com.android.documentsui.BaseActivity;
46import com.android.documentsui.DocumentsApplication;
Aga Wronska3b327ef2016-01-20 16:32:33 -080047import com.android.documentsui.R;
Aga Wronska71138722016-02-22 17:57:31 -080048import com.android.documentsui.Shared;
Aga Wronska3b327ef2016-01-20 16:32:33 -080049import com.android.documentsui.Snackbars;
Ben Kwae3aee182016-02-02 12:11:10 -080050import com.android.documentsui.model.DocumentInfo;
Aga Wronska40416802016-02-08 09:14:25 -080051
Aga Wronska3b327ef2016-01-20 16:32:33 -080052/**
53 * Dialog to rename file or directory.
54 */
Ben Kwae3aee182016-02-02 12:11:10 -080055public class RenameDocumentFragment extends DialogFragment {
Aga Wronska3b327ef2016-01-20 16:32:33 -080056 private static final String TAG_RENAME_DOCUMENT = "rename_document";
57 private DocumentInfo mDocument;
Aga Wronska71138722016-02-22 17:57:31 -080058 private EditText mEditText;
Aga Wronska3b327ef2016-01-20 16:32:33 -080059
60 public static void show(FragmentManager fm, DocumentInfo document) {
61 final RenameDocumentFragment dialog = new RenameDocumentFragment();
62 dialog.mDocument = document;
63 dialog.show(fm, TAG_RENAME_DOCUMENT);
64 }
65
Aga Wronska71138722016-02-22 17:57:31 -080066 /**
67 * Creates the dialog UI.
68 * @param savedInstanceState
69 * @return
70 */
Aga Wronska3b327ef2016-01-20 16:32:33 -080071 @Override
72 public Dialog onCreateDialog(Bundle savedInstanceState) {
73 Context context = getActivity();
74 AlertDialog.Builder builder = new AlertDialog.Builder(context);
75 LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
76 View view = dialogInflater.inflate(R.layout.dialog_file_name, null, false);
77
Aga Wronska71138722016-02-22 17:57:31 -080078 mEditText = (EditText) view.findViewById(android.R.id.text1);
Aga Wronska3b327ef2016-01-20 16:32:33 -080079 builder.setTitle(R.string.menu_rename);
80 builder.setView(view);
81
82 builder.setPositiveButton(
83 android.R.string.ok,
84 new OnClickListener() {
85 @Override
86 public void onClick(DialogInterface dialog, int which) {
Aga Wronska71138722016-02-22 17:57:31 -080087 renameDocuments(mEditText.getText().toString());
Aga Wronska3b327ef2016-01-20 16:32:33 -080088 }
89 });
90
91 builder.setNegativeButton(android.R.string.cancel, null);
92
93 final AlertDialog dialog = builder.create();
94
Aga Wronska71138722016-02-22 17:57:31 -080095 mEditText.setOnEditorActionListener(
Aga Wronska3b327ef2016-01-20 16:32:33 -080096 new OnEditorActionListener() {
97 @Override
98 public boolean onEditorAction(
99 TextView view, int actionId, @Nullable KeyEvent event) {
Aga Wronska40416802016-02-08 09:14:25 -0800100 if ((actionId == EditorInfo.IME_ACTION_DONE) || (event != null
Aga Wronska3b327ef2016-01-20 16:32:33 -0800101 && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
Aga Wronska40416802016-02-08 09:14:25 -0800102 && event.hasNoModifiers())) {
Aga Wronska71138722016-02-22 17:57:31 -0800103 renameDocuments(mEditText.getText().toString());
Aga Wronska3b327ef2016-01-20 16:32:33 -0800104 dialog.dismiss();
105 return true;
106 }
107 return false;
108 }
109 });
Aga Wronska3b327ef2016-01-20 16:32:33 -0800110 return dialog;
111 }
112
Aga Wronska40416802016-02-08 09:14:25 -0800113 /**
Aga Wronska71138722016-02-22 17:57:31 -0800114 * Sets/Restores the data.
115 * @param savedInstanceState
116 * @return
117 */
118 @Override
119 public void onActivityCreated(Bundle savedInstanceState) {
120 super.onActivityCreated(savedInstanceState);
121
122 if(savedInstanceState == null) {
123 // Fragment created for the first time, we set the text.
124 // mDocument value was set in show
125 mEditText.setText(mDocument.displayName);
126 }
127 else {
128 // Fragment restored, text was restored automatically.
129 // mDocument value needs to be restored.
130 mDocument = savedInstanceState.getParcelable(Shared.EXTRA_DOC);
131 }
132 // Do selection in both cases, because we cleared it.
133 selectFileName(mEditText);
134 }
135
136 @Override
137 public void onSaveInstanceState(Bundle outState) {
138 // Clear selection before storing state and restore it manually,
139 // because otherwise after rotation selection is displayed with cut/copy menu visible :/
140 clearFileNameSelection(mEditText);
141
142 super.onSaveInstanceState(outState);
143
144 outState.putParcelable(Shared.EXTRA_DOC, mDocument);
145 }
146
147 /**
Aga Wronska40416802016-02-08 09:14:25 -0800148 * Validates if string is a proper document name.
149 * Checks if string is not empty. More rules might be added later.
150 * @param docName string representing document name
151 * @returns true if string is a valid name.
152 **/
153 private boolean isValidDocumentName(String docName) {
154 return !docName.isEmpty();
155 }
156
157 /**
158 * Fills text field with the file name and selects the name without extension.
159 *
160 * @param editText text field to be filled
Aga Wronska40416802016-02-08 09:14:25 -0800161 */
Aga Wronska71138722016-02-22 17:57:31 -0800162 private void selectFileName(EditText editText) {
163 String text = editText.getText().toString();
164 int separatorIndex = text.indexOf(".");
Aga Wronska568b3b32016-02-23 10:49:29 -0800165 editText.setSelection(0,
166 (separatorIndex == -1 || mDocument.isDirectory()) ? text.length() : separatorIndex);
Aga Wronska71138722016-02-22 17:57:31 -0800167 }
168
169 /**
170 * Clears selection in text field.
171 *
172 * @param editText text field to be cleared.
173 */
174 private void clearFileNameSelection(EditText editText) {
175 editText.setSelection(0, 0);
Aga Wronska40416802016-02-08 09:14:25 -0800176 }
177
Aga Wronska3b327ef2016-01-20 16:32:33 -0800178 private void renameDocuments(String newDisplayName) {
179 BaseActivity activity = (BaseActivity) getActivity();
180
Aga Wronska40416802016-02-08 09:14:25 -0800181 if (isValidDocumentName(newDisplayName)) {
182 new RenameDocumentsTask(activity, newDisplayName).execute(mDocument);
183 } else {
184 Log.w(TAG, "Failed to rename file - invalid name:" + newDisplayName);
185 Snackbars.makeSnackbar(getActivity(), R.string.rename_error,
186 Snackbar.LENGTH_SHORT).show();
187 }
188
Aga Wronska3b327ef2016-01-20 16:32:33 -0800189 }
190
191 private class RenameDocumentsTask extends AsyncTask<DocumentInfo, Void, DocumentInfo> {
192 private final BaseActivity mActivity;
193 private final String mNewDisplayName;
194
195 public RenameDocumentsTask(BaseActivity activity, String newDisplayName) {
196 mActivity = activity;
197 mNewDisplayName = newDisplayName;
198 }
199
200 @Override
201 protected void onPreExecute() {
202 mActivity.setPending(true);
203 }
204
205 @Override
206 protected DocumentInfo doInBackground(DocumentInfo... document) {
Steve McKaya1f76802016-02-25 13:34:03 -0800207 assert(document.length == 1);
Aga Wronska3b327ef2016-01-20 16:32:33 -0800208 final ContentResolver resolver = mActivity.getContentResolver();
209 ContentProviderClient client = null;
210
211 try {
212 client = DocumentsApplication.acquireUnstableProviderOrThrow(
213 resolver, document[0].derivedUri.getAuthority());
214 Uri newUri = DocumentsContract.renameDocument(
215 client, document[0].derivedUri, mNewDisplayName);
216 return DocumentInfo.fromUri(resolver, newUri);
217 } catch (Exception e) {
218 Log.w(TAG, "Failed to rename file", e);
219 return null;
220 } finally {
221 ContentProviderClient.releaseQuietly(client);
222 }
223 }
224
225 @Override
226 protected void onPostExecute(DocumentInfo result) {
227 if (result == null) {
228 Snackbars.makeSnackbar(mActivity, R.string.rename_error, Snackbar.LENGTH_SHORT)
229 .show();
230 }
231
232 mActivity.setPending(false);
233 }
234 }
235}