RenameFileDialogFragment.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2014 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.ui.dialog;
  21. /*
  22. * Dialog to input a new name for an {@link OCFile} being renamed.
  23. *
  24. * Triggers the rename operation.
  25. */
  26. import android.app.Dialog;
  27. import android.content.DialogInterface;
  28. import android.os.Bundle;
  29. import android.text.Editable;
  30. import android.text.TextUtils;
  31. import android.text.TextWatcher;
  32. import android.view.LayoutInflater;
  33. import android.view.View;
  34. import android.view.Window;
  35. import android.view.WindowManager.LayoutParams;
  36. import com.owncloud.android.R;
  37. import com.owncloud.android.databinding.EditBoxDialogBinding;
  38. import com.owncloud.android.datamodel.OCFile;
  39. import com.owncloud.android.lib.resources.files.FileUtils;
  40. import com.owncloud.android.ui.activity.ComponentsGetter;
  41. import com.owncloud.android.utils.DisplayUtils;
  42. import com.owncloud.android.utils.theme.ThemeColorUtils;
  43. import com.owncloud.android.utils.theme.ThemeTextInputUtils;
  44. import androidx.annotation.NonNull;
  45. import androidx.appcompat.app.AlertDialog;
  46. import androidx.fragment.app.DialogFragment;
  47. /**
  48. * Dialog to input a new name for a file or folder to rename.
  49. *
  50. * Triggers the rename operation when name is confirmed.
  51. */
  52. public class RenameFileDialogFragment
  53. extends DialogFragment implements DialogInterface.OnClickListener {
  54. private static final String ARG_TARGET_FILE = "TARGET_FILE";
  55. private EditBoxDialogBinding binding;
  56. private OCFile mTargetFile;
  57. /**
  58. * Public factory method to create new RenameFileDialogFragment instances.
  59. *
  60. * @param file File to rename.
  61. * @return Dialog ready to show.
  62. */
  63. public static RenameFileDialogFragment newInstance(OCFile file) {
  64. RenameFileDialogFragment frag = new RenameFileDialogFragment();
  65. Bundle args = new Bundle();
  66. args.putParcelable(ARG_TARGET_FILE, file);
  67. frag.setArguments(args);
  68. return frag;
  69. }
  70. @Override
  71. public void onStart() {
  72. super.onStart();
  73. int color = ThemeColorUtils.primaryAccentColor(getContext());
  74. AlertDialog alertDialog = (AlertDialog) getDialog();
  75. if (alertDialog != null) {
  76. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
  77. alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(color);
  78. }
  79. }
  80. @NonNull
  81. @Override
  82. public Dialog onCreateDialog(Bundle savedInstanceState) {
  83. mTargetFile = requireArguments().getParcelable(ARG_TARGET_FILE);
  84. // Inflate the layout for the dialog
  85. LayoutInflater inflater = requireActivity().getLayoutInflater();
  86. binding = EditBoxDialogBinding.inflate(inflater, null, false);
  87. View view = binding.getRoot();
  88. // Setup layout
  89. String currentName = mTargetFile.getFileName();
  90. binding.userInput.setText(currentName);
  91. ThemeTextInputUtils.colorTextInput(binding.userInputContainer,
  92. binding.userInput,
  93. ThemeColorUtils.primaryColor(getActivity()));
  94. int extensionStart = mTargetFile.isFolder() ? -1 : currentName.lastIndexOf('.');
  95. int selectionEnd = extensionStart >= 0 ? extensionStart : currentName.length();
  96. binding.userInput.setSelection(0, selectionEnd);
  97. binding.userInput.requestFocus();
  98. // Add TextChangedListener to handle showing/hiding the input warning message
  99. binding.userInput.addTextChangedListener(new TextWatcher() {
  100. @Override
  101. public void afterTextChanged(Editable s) {
  102. }
  103. @Override
  104. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  105. }
  106. /**
  107. * When user enters a hidden file name, the 'hidden file' message is shown.
  108. * Otherwise, the message is ensured to be hidden.
  109. */
  110. @Override
  111. public void onTextChanged(CharSequence s, int start, int before, int count) {
  112. String newFileName = "";
  113. if (binding.userInput.getText() != null) {
  114. newFileName = binding.userInput.getText().toString().trim();
  115. }
  116. if (!TextUtils.isEmpty(newFileName) && newFileName.charAt(0) == '.') {
  117. binding.userInputContainer.setError(getText(R.string.hidden_file_name_warning));
  118. }
  119. else if(binding.userInputContainer.getError() != null) {
  120. binding.userInputContainer.setError(null);
  121. // Called to remove extra padding
  122. binding.userInputContainer.setErrorEnabled(false);
  123. }
  124. }
  125. });
  126. // Build the dialog
  127. AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
  128. builder.setView(view)
  129. .setPositiveButton(R.string.file_rename, this)
  130. .setNeutralButton(R.string.common_cancel, this)
  131. .setTitle(R.string.rename_dialog_title);
  132. Dialog d = builder.create();
  133. Window window = d.getWindow();
  134. if (window != null) {
  135. window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  136. }
  137. return d;
  138. }
  139. @Override
  140. public void onClick(DialogInterface dialog, int which) {
  141. if (which == AlertDialog.BUTTON_POSITIVE) {
  142. String newFileName = "";
  143. if (binding.userInput.getText() != null) {
  144. newFileName = binding.userInput.getText().toString().trim();
  145. }
  146. if (TextUtils.isEmpty(newFileName)) {
  147. DisplayUtils.showSnackMessage(requireActivity(), R.string.filename_empty);
  148. return;
  149. }
  150. if (!FileUtils.isValidName(newFileName)) {
  151. DisplayUtils.showSnackMessage(requireActivity(), R.string.filename_forbidden_charaters_from_server);
  152. return;
  153. }
  154. ((ComponentsGetter) requireActivity()).getFileOperationsHelper().renameFile(mTargetFile, newFileName);
  155. }
  156. }
  157. @Override
  158. public void onDestroyView() {
  159. super.onDestroyView();
  160. binding = null;
  161. }
  162. }