RemoveFilesDialogFragment.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.AlertDialog;
  21. import android.app.Dialog;
  22. import android.os.Bundle;
  23. import android.support.annotation.NonNull;
  24. import android.view.ActionMode;
  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. boolean containsFavorite = false;
  68. for (OCFile file: files) {
  69. containsFolder |= file.isFolder();
  70. containsDown |= file.isDown();
  71. containsFavorite |= file.isAvailableOffline();
  72. }
  73. if (files.size() == SINGLE_SELECTION) {
  74. // choose message for a single file
  75. OCFile file = files.get(0);
  76. messageStringId = file.isFolder() ?
  77. R.string.confirmation_remove_folder_alert :
  78. R.string.confirmation_remove_file_alert;
  79. } else {
  80. // choose message for more than one file
  81. messageStringId = containsFolder ?
  82. R.string.confirmation_remove_folders_alert :
  83. R.string.confirmation_remove_files_alert;
  84. }
  85. int localRemoveButton = (!containsFavorite && (containsFolder || containsDown)) ?
  86. R.string.confirmation_remove_local :
  87. -1;
  88. args.putInt(ARG_MESSAGE_RESOURCE_ID, messageStringId);
  89. if (files.size() == 1) {
  90. args.putStringArray(ARG_MESSAGE_ARGUMENTS, new String[]{files.get(0).getFileName()});
  91. }
  92. args.putInt(ARG_POSITIVE_BTN_RES, R.string.file_delete);
  93. args.putInt(ARG_NEUTRAL_BTN_RES, R.string.file_keep);
  94. args.putInt(ARG_NEGATIVE_BTN_RES, localRemoveButton);
  95. args.putParcelableArrayList(ARG_TARGET_FILES, files);
  96. frag.setArguments(args);
  97. return frag;
  98. }
  99. /**
  100. * Convenience factory method to create new RemoveFilesDialogFragment instances for a single file
  101. *
  102. * @param file File to remove.
  103. * @return Dialog ready to show.
  104. */
  105. public static RemoveFilesDialogFragment newInstance(OCFile file) {
  106. ArrayList<OCFile> list = new ArrayList<>();
  107. list.add(file);
  108. return newInstance(list);
  109. }
  110. @Override
  111. public void onStart() {
  112. super.onStart();
  113. int color = ThemeUtils.primaryAccentColor(getActivity());
  114. android.support.v7.app.AlertDialog alertDialog = (android.support.v7.app.AlertDialog) getDialog();
  115. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
  116. alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color);
  117. alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(color);
  118. }
  119. @NonNull
  120. @Override
  121. public Dialog onCreateDialog(Bundle savedInstanceState) {
  122. Dialog dialog = super.onCreateDialog(savedInstanceState);
  123. mTargetFiles = getArguments().getParcelableArrayList(ARG_TARGET_FILES);
  124. setOnConfirmationListener(this);
  125. return dialog;
  126. }
  127. /**
  128. * Performs the removal of the target file, both locally and in the server and
  129. * finishes the supplied ActionMode if one was given.
  130. */
  131. @Override
  132. public void onConfirmation(String callerTag) {
  133. ComponentsGetter cg = (ComponentsGetter) getActivity();
  134. cg.getFileOperationsHelper().removeFiles(mTargetFiles, false, false);
  135. finishActionMode();
  136. }
  137. /**
  138. * Performs the removal of the local copy of the target file
  139. */
  140. @Override
  141. public void onCancel(String callerTag) {
  142. ComponentsGetter cg = (ComponentsGetter) getActivity();
  143. cg.getFileOperationsHelper().removeFiles(mTargetFiles, true, false);
  144. finishActionMode();
  145. }
  146. @Override
  147. public void onNeutral(String callerTag) {
  148. // nothing to do here
  149. }
  150. private void setActionMode(ActionMode actionMode) {
  151. this.actionMode = actionMode;
  152. }
  153. /**
  154. * This is used when finishing an actionMode,
  155. * for example if we want to exit the selection mode
  156. * after deleting the target files.
  157. */
  158. private void finishActionMode() {
  159. if (actionMode != null) {
  160. actionMode.finish();
  161. }
  162. }
  163. }