CreateFolderDialogFragment.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2015 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. import android.app.Dialog;
  22. import android.content.DialogInterface;
  23. import android.os.Bundle;
  24. import android.support.v4.app.DialogFragment;
  25. import android.support.v7.app.AlertDialog;
  26. import android.view.LayoutInflater;
  27. import android.view.View;
  28. import android.view.WindowManager.LayoutParams;
  29. import android.widget.EditText;
  30. import android.widget.TextView;
  31. import android.widget.Toast;
  32. import com.owncloud.android.R;
  33. import com.owncloud.android.datamodel.OCFile;
  34. import com.owncloud.android.lib.resources.files.FileUtils;
  35. import com.owncloud.android.ui.activity.ComponentsGetter;
  36. /**
  37. * Dialog to input the name for a new folder to create.
  38. *
  39. * Triggers the folder creation when name is confirmed.
  40. */
  41. public class CreateFolderDialogFragment
  42. extends DialogFragment implements DialogInterface.OnClickListener {
  43. private static final String ARG_PARENT_FOLDER = "PARENT_FOLDER";
  44. public static final String CREATE_FOLDER_FRAGMENT = "CREATE_FOLDER_FRAGMENT";
  45. private OCFile mParentFolder;
  46. /**
  47. * Public factory method to create new CreateFolderDialogFragment instances.
  48. *
  49. * @param parentFolder Folder to create
  50. * @return Dialog ready to show.
  51. */
  52. public static CreateFolderDialogFragment newInstance(OCFile parentFolder) {
  53. CreateFolderDialogFragment frag = new CreateFolderDialogFragment();
  54. Bundle args = new Bundle();
  55. args.putParcelable(ARG_PARENT_FOLDER, parentFolder);
  56. frag.setArguments(args);
  57. return frag;
  58. }
  59. @Override
  60. public Dialog onCreateDialog(Bundle savedInstanceState) {
  61. mParentFolder = getArguments().getParcelable(ARG_PARENT_FOLDER);
  62. // Inflate the layout for the dialog
  63. LayoutInflater inflater = getActivity().getLayoutInflater();
  64. View v = inflater.inflate(R.layout.edit_box_dialog, null);
  65. // Setup layout
  66. EditText inputText = ((EditText)v.findViewById(R.id.user_input));
  67. inputText.setText("");
  68. inputText.requestFocus();
  69. // Build the dialog
  70. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  71. builder.setView(v)
  72. .setPositiveButton(R.string.common_ok, this)
  73. .setNegativeButton(R.string.common_cancel, this)
  74. .setTitle(R.string.uploader_info_dirname);
  75. Dialog d = builder.create();
  76. d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  77. return d;
  78. }
  79. @Override
  80. public void onClick(DialogInterface dialog, int which) {
  81. if (which == AlertDialog.BUTTON_POSITIVE) {
  82. String newFolderName =
  83. ((TextView)(getDialog().findViewById(R.id.user_input)))
  84. .getText().toString().trim();
  85. if (newFolderName.length() <= 0) {
  86. Toast.makeText(
  87. getActivity(),
  88. R.string.filename_empty,
  89. Toast.LENGTH_LONG).show();
  90. return;
  91. }
  92. boolean serverWithForbiddenChars = ((ComponentsGetter)getActivity()).
  93. getFileOperationsHelper().isVersionWithForbiddenCharacters();
  94. if (!FileUtils.isValidName(newFolderName, serverWithForbiddenChars)) {
  95. int messageId = 0;
  96. if (serverWithForbiddenChars) {
  97. messageId = R.string.filename_forbidden_charaters_from_server;
  98. } else {
  99. messageId = R.string.filename_forbidden_characters;
  100. }
  101. Toast.makeText(getActivity(), messageId, Toast.LENGTH_LONG).show();
  102. return;
  103. }
  104. String path = mParentFolder.getRemotePath();
  105. path += newFolderName + OCFile.PATH_SEPARATOR;
  106. ((ComponentsGetter)getActivity()).
  107. getFileOperationsHelper().createFolder(path, false);
  108. }
  109. }
  110. }