RemoveFileDialogFragment.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /**
  22. * Dialog requiring confirmation before removing a given OCFile.
  23. *
  24. * Triggers the removal according to the user response.
  25. */
  26. import android.app.Dialog;
  27. import android.os.Bundle;
  28. import com.owncloud.android.R;
  29. import com.owncloud.android.datamodel.FileDataStorageManager;
  30. import com.owncloud.android.datamodel.OCFile;
  31. import com.owncloud.android.ui.activity.ComponentsGetter;
  32. import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
  33. public class RemoveFileDialogFragment extends ConfirmationDialogFragment
  34. implements ConfirmationDialogFragmentListener {
  35. private OCFile mTargetFile;
  36. private static final String ARG_TARGET_FILE = "TARGET_FILE";
  37. /**
  38. * Public factory method to create new RemoveFileDialogFragment instances.
  39. *
  40. * @param file File to remove.
  41. * @return Dialog ready to show.
  42. */
  43. public static RemoveFileDialogFragment newInstance(OCFile file) {
  44. RemoveFileDialogFragment frag = new RemoveFileDialogFragment();
  45. Bundle args = new Bundle();
  46. int messageStringId = R.string.confirmation_remove_alert;
  47. int negBtn = -1;
  48. if (file.isFolder()) {
  49. messageStringId = R.string.confirmation_remove_folder_alert;
  50. negBtn = R.string.confirmation_remove_local;
  51. } else if (file.isDown()) {
  52. negBtn = R.string.confirmation_remove_local;
  53. }
  54. args.putInt(ARG_CONF_RESOURCE_ID, messageStringId);
  55. args.putStringArray(ARG_CONF_ARGUMENTS, new String[]{file.getFileName()});
  56. args.putInt(ARG_POSITIVE_BTN_RES, R.string.common_yes);
  57. args.putInt(ARG_NEUTRAL_BTN_RES, R.string.common_no);
  58. args.putInt(ARG_NEGATIVE_BTN_RES, negBtn);
  59. args.putParcelable(ARG_TARGET_FILE, file);
  60. frag.setArguments(args);
  61. return frag;
  62. }
  63. @Override
  64. public Dialog onCreateDialog(Bundle savedInstanceState) {
  65. Dialog dialog = super.onCreateDialog(savedInstanceState);
  66. mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE);
  67. setOnConfirmationListener(this);
  68. return dialog;
  69. }
  70. /**
  71. * Performs the removal of the target file, both locally and in the server.
  72. */
  73. @Override
  74. public void onConfirmation(String callerTag) {
  75. ComponentsGetter cg = (ComponentsGetter)getActivity();
  76. FileDataStorageManager storageManager = cg.getStorageManager();
  77. if (storageManager.getFileById(mTargetFile.getFileId()) != null) {
  78. cg.getFileOperationsHelper().removeFile(mTargetFile, false);
  79. }
  80. }
  81. /**
  82. * Performs the removal of the local copy of the target file
  83. */
  84. @Override
  85. public void onCancel(String callerTag) {
  86. ComponentsGetter cg = (ComponentsGetter)getActivity();
  87. cg.getFileOperationsHelper().removeFile(mTargetFile, true);
  88. }
  89. @Override
  90. public void onNeutral(String callerTag) {
  91. // nothing to do here
  92. }
  93. }