blob: 38a71ec2d14933442f3de5a29e004e1525d7024c [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;
20import static com.android.internal.util.Preconditions.checkArgument;
21
22import android.app.AlertDialog;
23import android.app.Dialog;
24import android.app.DialogFragment;
25import android.app.FragmentManager;
26import android.content.ContentProviderClient;
27import android.content.ContentResolver;
28import android.content.Context;
29import android.content.DialogInterface;
30import android.content.DialogInterface.OnClickListener;
31import android.net.Uri;
32import android.os.AsyncTask;
33import android.os.Bundle;
34import android.provider.DocumentsContract;
35import android.support.annotation.Nullable;
36import android.support.design.widget.Snackbar;
37import android.util.Log;
38import android.view.KeyEvent;
Aga Wronska40416802016-02-08 09:14:25 -080039import android.view.inputmethod.EditorInfo;
Aga Wronska3b327ef2016-01-20 16:32:33 -080040import android.view.LayoutInflater;
41import android.view.View;
42import android.widget.EditText;
43import android.widget.TextView;
44import android.widget.TextView.OnEditorActionListener;
45
46import com.android.documentsui.BaseActivity;
47import com.android.documentsui.DocumentsApplication;
Aga Wronska3b327ef2016-01-20 16:32:33 -080048import com.android.documentsui.R;
Aga Wronska71138722016-02-22 17:57:31 -080049import com.android.documentsui.Shared;
Aga Wronska3b327ef2016-01-20 16:32:33 -080050import com.android.documentsui.Snackbars;
Ben Kwae3aee182016-02-02 12:11:10 -080051import com.android.documentsui.model.DocumentInfo;
Aga Wronska40416802016-02-08 09:14:25 -080052
Aga Wronska3b327ef2016-01-20 16:32:33 -080053/**
54 * Dialog to rename file or directory.
55 */
Ben Kwae3aee182016-02-02 12:11:10 -080056public class RenameDocumentFragment extends DialogFragment {
Aga Wronska3b327ef2016-01-20 16:32:33 -080057 private static final String TAG_RENAME_DOCUMENT = "rename_document";
58 private DocumentInfo mDocument;
Aga Wronska71138722016-02-22 17:57:31 -080059 private EditText mEditText;
Aga Wronska3b327ef2016-01-20 16:32:33 -080060
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 Wronska71138722016-02-22 17:57:31 -080067 /**
68 * Creates the dialog UI.
69 * @param savedInstanceState
70 * @return
71 */
Aga Wronska3b327ef2016-01-20 16:32:33 -080072 @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 Wronska71138722016-02-22 17:57:31 -080079 mEditText = (EditText) view.findViewById(android.R.id.text1);
Aga Wronska3b327ef2016-01-20 16:32:33 -080080 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 Wronska71138722016-02-22 17:57:31 -080088 renameDocuments(mEditText.getText().toString());
Aga Wronska3b327ef2016-01-20 16:32:33 -080089 }
90 });
91
92 builder.setNegativeButton(android.R.string.cancel, null);
93
94 final AlertDialog dialog = builder.create();
95
Aga Wronska71138722016-02-22 17:57:31 -080096 mEditText.setOnEditorActionListener(
Aga Wronska3b327ef2016-01-20 16:32:33 -080097 new OnEditorActionListener() {
98 @Override
99 public boolean onEditorAction(
100 TextView view, int actionId, @Nullable KeyEvent event) {
Aga Wronska40416802016-02-08 09:14:25 -0800101 if ((actionId == EditorInfo.IME_ACTION_DONE) || (event != null
Aga Wronska3b327ef2016-01-20 16:32:33 -0800102 && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
Aga Wronska40416802016-02-08 09:14:25 -0800103 && event.hasNoModifiers())) {
Aga Wronska71138722016-02-22 17:57:31 -0800104 renameDocuments(mEditText.getText().toString());
Aga Wronska3b327ef2016-01-20 16:32:33 -0800105 dialog.dismiss();
106 return true;
107 }
108 return false;
109 }
110 });
Aga Wronska3b327ef2016-01-20 16:32:33 -0800111 return dialog;
112 }
113
Aga Wronska40416802016-02-08 09:14:25 -0800114 /**
Aga Wronska71138722016-02-22 17:57:31 -0800115 * Sets/Restores the data.
116 * @param savedInstanceState
117 * @return
118 */
119 @Override
120 public void onActivityCreated(Bundle savedInstanceState) {
121 super.onActivityCreated(savedInstanceState);
122
123 if(savedInstanceState == null) {
124 // Fragment created for the first time, we set the text.
125 // mDocument value was set in show
126 mEditText.setText(mDocument.displayName);
127 }
128 else {
129 // Fragment restored, text was restored automatically.
130 // mDocument value needs to be restored.
131 mDocument = savedInstanceState.getParcelable(Shared.EXTRA_DOC);
132 }
133 // Do selection in both cases, because we cleared it.
134 selectFileName(mEditText);
135 }
136
137 @Override
138 public void onSaveInstanceState(Bundle outState) {
139 // Clear selection before storing state and restore it manually,
140 // because otherwise after rotation selection is displayed with cut/copy menu visible :/
141 clearFileNameSelection(mEditText);
142
143 super.onSaveInstanceState(outState);
144
145 outState.putParcelable(Shared.EXTRA_DOC, mDocument);
146 }
147
148 /**
Aga Wronska40416802016-02-08 09:14:25 -0800149 * Validates if string is a proper document name.
150 * Checks if string is not empty. More rules might be added later.
151 * @param docName string representing document name
152 * @returns true if string is a valid name.
153 **/
154 private boolean isValidDocumentName(String docName) {
155 return !docName.isEmpty();
156 }
157
158 /**
159 * Fills text field with the file name and selects the name without extension.
160 *
161 * @param editText text field to be filled
Aga Wronska40416802016-02-08 09:14:25 -0800162 */
Aga Wronska71138722016-02-22 17:57:31 -0800163 private void selectFileName(EditText editText) {
164 String text = editText.getText().toString();
165 int separatorIndex = text.indexOf(".");
Aga Wronska568b3b32016-02-23 10:49:29 -0800166 editText.setSelection(0,
167 (separatorIndex == -1 || mDocument.isDirectory()) ? text.length() : separatorIndex);
Aga Wronska71138722016-02-22 17:57:31 -0800168 }
169
170 /**
171 * Clears selection in text field.
172 *
173 * @param editText text field to be cleared.
174 */
175 private void clearFileNameSelection(EditText editText) {
176 editText.setSelection(0, 0);
Aga Wronska40416802016-02-08 09:14:25 -0800177 }
178
Aga Wronska3b327ef2016-01-20 16:32:33 -0800179 private void renameDocuments(String newDisplayName) {
180 BaseActivity activity = (BaseActivity) getActivity();
181
Aga Wronska40416802016-02-08 09:14:25 -0800182 if (isValidDocumentName(newDisplayName)) {
183 new RenameDocumentsTask(activity, newDisplayName).execute(mDocument);
184 } else {
185 Log.w(TAG, "Failed to rename file - invalid name:" + newDisplayName);
186 Snackbars.makeSnackbar(getActivity(), R.string.rename_error,
187 Snackbar.LENGTH_SHORT).show();
188 }
189
Aga Wronska3b327ef2016-01-20 16:32:33 -0800190 }
191
192 private class RenameDocumentsTask extends AsyncTask<DocumentInfo, Void, DocumentInfo> {
193 private final BaseActivity mActivity;
194 private final String mNewDisplayName;
195
196 public RenameDocumentsTask(BaseActivity activity, String newDisplayName) {
197 mActivity = activity;
198 mNewDisplayName = newDisplayName;
199 }
200
201 @Override
202 protected void onPreExecute() {
203 mActivity.setPending(true);
204 }
205
206 @Override
207 protected DocumentInfo doInBackground(DocumentInfo... document) {
208 checkArgument(document.length == 1);
209 final ContentResolver resolver = mActivity.getContentResolver();
210 ContentProviderClient client = null;
211
212 try {
213 client = DocumentsApplication.acquireUnstableProviderOrThrow(
214 resolver, document[0].derivedUri.getAuthority());
215 Uri newUri = DocumentsContract.renameDocument(
216 client, document[0].derivedUri, mNewDisplayName);
217 return DocumentInfo.fromUri(resolver, newUri);
218 } catch (Exception e) {
219 Log.w(TAG, "Failed to rename file", e);
220 return null;
221 } finally {
222 ContentProviderClient.releaseQuietly(client);
223 }
224 }
225
226 @Override
227 protected void onPostExecute(DocumentInfo result) {
228 if (result == null) {
229 Snackbars.makeSnackbar(mActivity, R.string.rename_error, Snackbar.LENGTH_SHORT)
230 .show();
231 }
232
233 mActivity.setPending(false);
234 }
235 }
236}