RemoveFileDialogFragment.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. import java.util.ArrayList;
  34. public class RemoveFileDialogFragment extends ConfirmationDialogFragment
  35. implements ConfirmationDialogFragmentListener {
  36. private OCFile mTargetFile;
  37. private static final String ARG_TARGET_FILE = "TARGET_FILE";
  38. /**
  39. * Public factory method to create new RemoveFileDialogFragment instances.
  40. *
  41. * @param file File to remove.
  42. * @return Dialog ready to show.
  43. */
  44. public static RemoveFileDialogFragment newInstance(OCFile file) {
  45. RemoveFileDialogFragment frag = new RemoveFileDialogFragment();
  46. Bundle args = new Bundle();
  47. int messageStringId = R.string.confirmation_remove_file_alert;
  48. int localRemoveButton = (!file.isAvailableOffline() && (file.isFolder() || file.isDown())) ?
  49. R.string.confirmation_remove_local : -1;
  50. if (file.isFolder()) {
  51. messageStringId = R.string.confirmation_remove_folder_alert;
  52. }
  53. args.putInt(ARG_MESSAGE_RESOURCE_ID, messageStringId);
  54. args.putStringArray(ARG_MESSAGE_ARGUMENTS, new String[]{file.getFileName()});
  55. args.putInt(ARG_POSITIVE_BTN_RES, R.string.common_yes);
  56. args.putInt(ARG_NEUTRAL_BTN_RES, R.string.common_no);
  57. args.putInt(ARG_NEGATIVE_BTN_RES, localRemoveButton);
  58. args.putParcelable(ARG_TARGET_FILE, file);
  59. frag.setArguments(args);
  60. return frag;
  61. }
  62. @Override
  63. public Dialog onCreateDialog(Bundle savedInstanceState) {
  64. Dialog dialog = super.onCreateDialog(savedInstanceState);
  65. mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE);
  66. setOnConfirmationListener(this);
  67. return dialog;
  68. }
  69. /**
  70. * Performs the removal of the target file, both locally and in the server.
  71. */
  72. @Override
  73. public void onConfirmation(String callerTag) {
  74. ComponentsGetter cg = (ComponentsGetter)getActivity();
  75. FileDataStorageManager storageManager = cg.getStorageManager();
  76. if (storageManager.getFileById(mTargetFile.getFileId()) != null) {
  77. ArrayList<OCFile> list = new ArrayList<>();
  78. list.add(mTargetFile);
  79. cg.getFileOperationsHelper().removeFiles(list, false);
  80. }
  81. }
  82. /**
  83. * Performs the removal of the local copy of the target file
  84. */
  85. @Override
  86. public void onCancel(String callerTag) {
  87. ComponentsGetter cg = (ComponentsGetter)getActivity();
  88. ArrayList<OCFile> list = new ArrayList<>();
  89. list.add(mTargetFile);
  90. cg.getFileOperationsHelper().removeFiles(list, true);
  91. FileDataStorageManager storageManager = cg.getStorageManager();
  92. boolean containsFavorite = false;
  93. if (mTargetFile.isFolder()) {
  94. ArrayList<OCFile> files = storageManager.getFolderContent(mTargetFile, false);
  95. for(OCFile file: files) {
  96. containsFavorite = file.isAvailableOffline() || containsFavorite;
  97. if (containsFavorite) {
  98. break;
  99. }
  100. }
  101. }
  102. // Remove etag for parent, if file is a favorite
  103. // or is a folder and contains favorite
  104. if (mTargetFile.isAvailableOffline() || containsFavorite) {
  105. OCFile folder = null;
  106. if (mTargetFile.isFolder()) {
  107. folder = mTargetFile;
  108. } else {
  109. folder = storageManager.getFileById(mTargetFile.getParentId());
  110. }
  111. folder.setEtag("");
  112. storageManager.saveFile(folder);
  113. }
  114. }
  115. @Override
  116. public void onNeutral(String callerTag) {
  117. // nothing to do here
  118. }
  119. }