RenameFileDialogFragment.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.TextUtils;
  30. import android.view.LayoutInflater;
  31. import android.view.View;
  32. import android.view.Window;
  33. import android.view.WindowManager.LayoutParams;
  34. import com.owncloud.android.R;
  35. import com.owncloud.android.databinding.EditBoxDialogBinding;
  36. import com.owncloud.android.datamodel.OCFile;
  37. import com.owncloud.android.lib.resources.files.FileUtils;
  38. import com.owncloud.android.ui.activity.ComponentsGetter;
  39. import com.owncloud.android.utils.DisplayUtils;
  40. import com.owncloud.android.utils.theme.ThemeColorUtils;
  41. import com.owncloud.android.utils.theme.ThemeTextInputUtils;
  42. import androidx.annotation.NonNull;
  43. import androidx.appcompat.app.AlertDialog;
  44. import androidx.fragment.app.DialogFragment;
  45. /**
  46. * Dialog to input a new name for a file or folder to rename.
  47. *
  48. * Triggers the rename operation when name is confirmed.
  49. */
  50. public class RenameFileDialogFragment
  51. extends DialogFragment implements DialogInterface.OnClickListener {
  52. private static final String ARG_TARGET_FILE = "TARGET_FILE";
  53. private EditBoxDialogBinding binding;
  54. private OCFile mTargetFile;
  55. /**
  56. * Public factory method to create new RenameFileDialogFragment instances.
  57. *
  58. * @param file File to rename.
  59. * @return Dialog ready to show.
  60. */
  61. public static RenameFileDialogFragment newInstance(OCFile file) {
  62. RenameFileDialogFragment frag = new RenameFileDialogFragment();
  63. Bundle args = new Bundle();
  64. args.putParcelable(ARG_TARGET_FILE, file);
  65. frag.setArguments(args);
  66. return frag;
  67. }
  68. @Override
  69. public void onStart() {
  70. super.onStart();
  71. int color = ThemeColorUtils.primaryAccentColor(getContext());
  72. AlertDialog alertDialog = (AlertDialog) getDialog();
  73. if (alertDialog != null) {
  74. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
  75. alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color);
  76. }
  77. }
  78. @NonNull
  79. @Override
  80. public Dialog onCreateDialog(Bundle savedInstanceState) {
  81. mTargetFile = requireArguments().getParcelable(ARG_TARGET_FILE);
  82. // Inflate the layout for the dialog
  83. LayoutInflater inflater = requireActivity().getLayoutInflater();
  84. binding = EditBoxDialogBinding.inflate(inflater, null, false);
  85. View view = binding.getRoot();
  86. // Setup layout
  87. String currentName = mTargetFile.getFileName();
  88. binding.userInput.setText(currentName);
  89. ThemeTextInputUtils.colorTextInput(binding.userInputContainer,
  90. binding.userInput,
  91. ThemeColorUtils.primaryColor(getActivity()));
  92. int selectionStart = 0;
  93. int extensionStart = mTargetFile.isFolder() ? -1 : currentName.lastIndexOf('.');
  94. int selectionEnd = extensionStart >= 0 ? extensionStart : currentName.length();
  95. binding.userInput.setSelection(Math.min(selectionStart, selectionEnd), Math.max(selectionStart, selectionEnd));
  96. binding.userInput.requestFocus();
  97. // Build the dialog
  98. AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
  99. builder.setView(view)
  100. .setPositiveButton(R.string.file_rename, this)
  101. .setNegativeButton(R.string.common_cancel, this)
  102. .setTitle(R.string.rename_dialog_title);
  103. Dialog d = builder.create();
  104. Window window = d.getWindow();
  105. if (window != null) {
  106. window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  107. }
  108. return d;
  109. }
  110. @Override
  111. public void onClick(DialogInterface dialog, int which) {
  112. if (which == AlertDialog.BUTTON_POSITIVE) {
  113. String newFileName = "";
  114. if (binding.userInput.getText() != null) {
  115. newFileName = binding.userInput.getText().toString().trim();
  116. }
  117. if (TextUtils.isEmpty(newFileName)) {
  118. DisplayUtils.showSnackMessage(requireActivity(), R.string.filename_empty);
  119. return;
  120. }
  121. if (!FileUtils.isValidName(newFileName)) {
  122. DisplayUtils.showSnackMessage(requireActivity(), R.string.filename_forbidden_charaters_from_server);
  123. return;
  124. }
  125. ((ComponentsGetter) requireActivity()).getFileOperationsHelper().renameFile(mTargetFile, newFileName);
  126. }
  127. }
  128. @Override
  129. public void onDestroyView() {
  130. super.onDestroyView();
  131. binding = null;
  132. }
  133. }