CreateFolderDialogFragment.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.graphics.PorterDuff;
  24. import android.os.Bundle;
  25. import android.text.TextUtils;
  26. import android.view.LayoutInflater;
  27. import android.view.View;
  28. import android.view.Window;
  29. import android.view.WindowManager.LayoutParams;
  30. import android.widget.EditText;
  31. import android.widget.TextView;
  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. import com.owncloud.android.utils.DisplayUtils;
  37. import com.owncloud.android.utils.ThemeUtils;
  38. import androidx.annotation.NonNull;
  39. import androidx.appcompat.app.AlertDialog;
  40. import androidx.fragment.app.DialogFragment;
  41. /**
  42. * Dialog to input the name for a new folder to create.
  43. *
  44. * Triggers the folder creation when name is confirmed.
  45. */
  46. public class CreateFolderDialogFragment
  47. extends DialogFragment implements DialogInterface.OnClickListener {
  48. private static final String ARG_PARENT_FOLDER = "PARENT_FOLDER";
  49. public static final String CREATE_FOLDER_FRAGMENT = "CREATE_FOLDER_FRAGMENT";
  50. private OCFile mParentFolder;
  51. /**
  52. * Public factory method to create new CreateFolderDialogFragment instances.
  53. *
  54. * @param parentFolder Folder to create
  55. * @return Dialog ready to show.
  56. */
  57. public static CreateFolderDialogFragment newInstance(OCFile parentFolder) {
  58. CreateFolderDialogFragment frag = new CreateFolderDialogFragment();
  59. Bundle args = new Bundle();
  60. args.putParcelable(ARG_PARENT_FOLDER, parentFolder);
  61. frag.setArguments(args);
  62. return frag;
  63. }
  64. @Override
  65. public void onStart() {
  66. super.onStart();
  67. int color = ThemeUtils.primaryAccentColor(getContext());
  68. AlertDialog alertDialog = (AlertDialog) getDialog();
  69. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
  70. alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color);
  71. }
  72. @NonNull
  73. @Override
  74. public Dialog onCreateDialog(Bundle savedInstanceState) {
  75. int accentColor = ThemeUtils.primaryAccentColor(getContext());
  76. mParentFolder = getArguments().getParcelable(ARG_PARENT_FOLDER);
  77. // Inflate the layout for the dialog
  78. LayoutInflater inflater = getActivity().getLayoutInflater();
  79. View v = inflater.inflate(R.layout.edit_box_dialog, null);
  80. // Setup layout
  81. EditText inputText = v.findViewById(R.id.user_input);
  82. inputText.setText("");
  83. inputText.requestFocus();
  84. inputText.getBackground().setColorFilter(accentColor, PorterDuff.Mode.SRC_ATOP);
  85. inputText.setHighlightColor(ThemeUtils.primaryColor(getActivity()));
  86. // Build the dialog
  87. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  88. builder.setView(v)
  89. .setPositiveButton(R.string.folder_confirm_create, this)
  90. .setNegativeButton(R.string.common_cancel, this)
  91. .setTitle(R.string.uploader_info_dirname);
  92. AlertDialog d = builder.create();
  93. Window window = d.getWindow();
  94. if (window != null) {
  95. window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  96. }
  97. return d;
  98. }
  99. @Override
  100. public void onClick(DialogInterface dialog, int which) {
  101. if (which == AlertDialog.BUTTON_POSITIVE) {
  102. String newFolderName =
  103. ((TextView)(getDialog().findViewById(R.id.user_input)))
  104. .getText().toString().trim();
  105. if (TextUtils.isEmpty(newFolderName)) {
  106. DisplayUtils.showSnackMessage(getActivity(), R.string.filename_empty);
  107. return;
  108. }
  109. if (!FileUtils.isValidName(newFolderName)) {
  110. DisplayUtils.showSnackMessage(getActivity(), R.string.filename_forbidden_charaters_from_server);
  111. return;
  112. }
  113. String path = mParentFolder.getDecryptedRemotePath() + newFolderName + OCFile.PATH_SEPARATOR;
  114. ((ComponentsGetter) getActivity()).getFileOperationsHelper().createFolder(path);
  115. }
  116. }
  117. }