RemoveFilesDialogFragment.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. package com.owncloud.android.ui.dialog;
  20. import android.app.Dialog;
  21. import android.os.Bundle;
  22. import android.view.ActionMode;
  23. import androidx.annotation.NonNull;
  24. import androidx.appcompat.app.AlertDialog;
  25. import com.owncloud.android.R;
  26. import com.owncloud.android.datamodel.OCFile;
  27. import com.owncloud.android.ui.activity.ComponentsGetter;
  28. import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
  29. import com.owncloud.android.utils.ThemeUtils;
  30. import java.util.ArrayList;
  31. import java.util.Collection;
  32. /**
  33. * Dialog requiring confirmation before removing a collection of given OCFiles.
  34. *
  35. * Triggers the removal according to the user response.
  36. */
  37. public class RemoveFilesDialogFragment extends ConfirmationDialogFragment implements
  38. ConfirmationDialogFragmentListener {
  39. private static final int SINGLE_SELECTION = 1;
  40. private static final String ARG_TARGET_FILES = "TARGET_FILES";
  41. private Collection<OCFile> mTargetFiles;
  42. private ActionMode actionMode;
  43. /**
  44. * Public factory method to create new RemoveFilesDialogFragment instances.
  45. *
  46. * @param files Files to remove.
  47. * @param actionMode ActionMode to finish on confirmation
  48. * @return Dialog ready to show.
  49. */
  50. public static RemoveFilesDialogFragment newInstance(ArrayList<OCFile> files, ActionMode actionMode) {
  51. RemoveFilesDialogFragment dialogFragment = newInstance(files);
  52. dialogFragment.setActionMode(actionMode);
  53. return dialogFragment;
  54. }
  55. /**
  56. * Public factory method to create new RemoveFilesDialogFragment instances.
  57. *
  58. * @param files Files to remove.
  59. * @return Dialog ready to show.
  60. */
  61. public static RemoveFilesDialogFragment newInstance(ArrayList<OCFile> files) {
  62. RemoveFilesDialogFragment frag = new RemoveFilesDialogFragment();
  63. Bundle args = new Bundle();
  64. int messageStringId;
  65. boolean containsFolder = false;
  66. boolean containsDown = false;
  67. for (OCFile file: files) {
  68. containsFolder |= file.isFolder();
  69. containsDown |= file.isDown();
  70. }
  71. if (files.size() == SINGLE_SELECTION) {
  72. // choose message for a single file
  73. OCFile file = files.get(0);
  74. messageStringId = file.isFolder() ?
  75. R.string.confirmation_remove_folder_alert :
  76. R.string.confirmation_remove_file_alert;
  77. } else {
  78. // choose message for more than one file
  79. messageStringId = containsFolder ?
  80. R.string.confirmation_remove_folders_alert :
  81. R.string.confirmation_remove_files_alert;
  82. }
  83. int localRemoveButton = (containsFolder || containsDown) ? R.string.confirmation_remove_local : -1;
  84. args.putInt(ARG_MESSAGE_RESOURCE_ID, messageStringId);
  85. if (files.size() == SINGLE_SELECTION) {
  86. args.putStringArray(ARG_MESSAGE_ARGUMENTS, new String[]{files.get(0).getFileName()});
  87. }
  88. args.putInt(ARG_POSITIVE_BTN_RES, R.string.file_delete);
  89. args.putInt(ARG_NEUTRAL_BTN_RES, R.string.file_keep);
  90. args.putInt(ARG_NEGATIVE_BTN_RES, localRemoveButton);
  91. args.putParcelableArrayList(ARG_TARGET_FILES, files);
  92. frag.setArguments(args);
  93. return frag;
  94. }
  95. /**
  96. * Convenience factory method to create new RemoveFilesDialogFragment instances for a single file
  97. *
  98. * @param file File to remove.
  99. * @return Dialog ready to show.
  100. */
  101. public static RemoveFilesDialogFragment newInstance(OCFile file) {
  102. ArrayList<OCFile> list = new ArrayList<>();
  103. list.add(file);
  104. return newInstance(list);
  105. }
  106. @Override
  107. public void onStart() {
  108. super.onStart();
  109. int color = ThemeUtils.primaryAccentColor(getActivity());
  110. AlertDialog alertDialog = (AlertDialog) getDialog();
  111. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
  112. alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color);
  113. alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(color);
  114. }
  115. @NonNull
  116. @Override
  117. public Dialog onCreateDialog(Bundle savedInstanceState) {
  118. Dialog dialog = super.onCreateDialog(savedInstanceState);
  119. mTargetFiles = getArguments().getParcelableArrayList(ARG_TARGET_FILES);
  120. setOnConfirmationListener(this);
  121. return dialog;
  122. }
  123. /**
  124. * Performs the removal of the target file, both locally and in the server and
  125. * finishes the supplied ActionMode if one was given.
  126. */
  127. @Override
  128. public void onConfirmation(String callerTag) {
  129. ComponentsGetter cg = (ComponentsGetter) getActivity();
  130. cg.getFileOperationsHelper().removeFiles(mTargetFiles, false, false);
  131. finishActionMode();
  132. }
  133. /**
  134. * Performs the removal of the local copy of the target file
  135. */
  136. @Override
  137. public void onCancel(String callerTag) {
  138. ComponentsGetter cg = (ComponentsGetter) getActivity();
  139. cg.getFileOperationsHelper().removeFiles(mTargetFiles, true, false);
  140. finishActionMode();
  141. }
  142. @Override
  143. public void onNeutral(String callerTag) {
  144. // nothing to do here
  145. }
  146. private void setActionMode(ActionMode actionMode) {
  147. this.actionMode = actionMode;
  148. }
  149. /**
  150. * This is used when finishing an actionMode,
  151. * for example if we want to exit the selection mode
  152. * after deleting the target files.
  153. */
  154. private void finishActionMode() {
  155. if (actionMode != null) {
  156. actionMode.finish();
  157. }
  158. }
  159. }