CreateFolderDialogFragment.java 5.0 KB

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