RenameFileDialogFragment.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.support.v7.app.AlertDialog;
  27. import android.app.Dialog;
  28. import android.content.DialogInterface;
  29. import android.os.Bundle;
  30. import android.support.v4.app.DialogFragment;
  31. import android.view.LayoutInflater;
  32. import android.view.View;
  33. import android.view.WindowManager.LayoutParams;
  34. import android.widget.EditText;
  35. import android.widget.TextView;
  36. import android.widget.Toast;
  37. import com.owncloud.android.R;
  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. /**
  42. * Dialog to input a new name for a file or folder to rename.
  43. *
  44. * Triggers the rename operation when name is confirmed.
  45. */
  46. public class RenameFileDialogFragment
  47. extends DialogFragment implements DialogInterface.OnClickListener {
  48. private static final String ARG_TARGET_FILE = "TARGET_FILE";
  49. /**
  50. * Public factory method to create new RenameFileDialogFragment instances.
  51. *
  52. * @param file File to rename.
  53. * @return Dialog ready to show.
  54. */
  55. public static RenameFileDialogFragment newInstance(OCFile file) {
  56. RenameFileDialogFragment frag = new RenameFileDialogFragment();
  57. Bundle args = new Bundle();
  58. args.putParcelable(ARG_TARGET_FILE, file);
  59. frag.setArguments(args);
  60. return frag;
  61. }
  62. private OCFile mTargetFile;
  63. @Override
  64. public Dialog onCreateDialog(Bundle savedInstanceState) {
  65. mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE);
  66. // Inflate the layout for the dialog
  67. LayoutInflater inflater = getActivity().getLayoutInflater();
  68. View v = inflater.inflate(R.layout.edit_box_dialog, null);
  69. // Setup layout
  70. String currentName = mTargetFile.getFileName();
  71. EditText inputText = ((EditText)v.findViewById(R.id.user_input));
  72. inputText.setText(currentName);
  73. int selectionStart = 0;
  74. int extensionStart = mTargetFile.isFolder() ? -1 : currentName.lastIndexOf('.');
  75. int selectionEnd = (extensionStart >= 0) ? extensionStart : currentName.length();
  76. if (selectionStart >= 0 && selectionEnd >= 0) {
  77. inputText.setSelection(
  78. Math.min(selectionStart, selectionEnd),
  79. Math.max(selectionStart, selectionEnd));
  80. }
  81. inputText.requestFocus();
  82. // Build the dialog
  83. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  84. builder.setView(v)
  85. .setPositiveButton(R.string.common_ok, this)
  86. .setNegativeButton(R.string.common_cancel, this)
  87. .setTitle(R.string.rename_dialog_title);
  88. Dialog d = builder.create();
  89. d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  90. return d;
  91. }
  92. @Override
  93. public void onClick(DialogInterface dialog, int which) {
  94. if (which == AlertDialog.BUTTON_POSITIVE) {
  95. String newFileName =
  96. ((TextView)(getDialog().findViewById(R.id.user_input)))
  97. .getText().toString().trim();
  98. if (newFileName.length() <= 0) {
  99. Toast.makeText(
  100. getActivity(),
  101. R.string.filename_empty,
  102. Toast.LENGTH_LONG).show();
  103. return;
  104. }
  105. boolean serverWithForbiddenChars = ((ComponentsGetter)getActivity()).
  106. getFileOperationsHelper().isVersionWithForbiddenCharacters();
  107. if (!FileUtils.isValidName(newFileName, serverWithForbiddenChars)) {
  108. int messageId = 0;
  109. if (serverWithForbiddenChars) {
  110. messageId = R.string.filename_forbidden_charaters_from_server;
  111. } else {
  112. messageId = R.string.filename_forbidden_characters;
  113. }
  114. Toast.makeText(getActivity(), messageId, Toast.LENGTH_LONG).show();
  115. return;
  116. }
  117. ((ComponentsGetter)getActivity()).getFileOperationsHelper().
  118. renameFile(mTargetFile, newFileName);
  119. }
  120. }
  121. }