123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- /*
- * ownCloud Android client application
- *
- * @author David A. Velasco
- * Copyright (C) 2015 ownCloud Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.owncloud.android.ui.dialog;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.view.ActionMode;
- import com.owncloud.android.R;
- import com.owncloud.android.datamodel.OCFile;
- import com.owncloud.android.ui.activity.ComponentsGetter;
- import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
- import com.owncloud.android.utils.ThemeUtils;
- import java.util.ArrayList;
- import java.util.Collection;
- /**
- * Dialog requiring confirmation before removing a collection of given OCFiles.
- *
- * Triggers the removal according to the user response.
- */
- public class RemoveFilesDialogFragment extends ConfirmationDialogFragment implements
- ConfirmationDialogFragmentListener {
- private static final int SINGLE_SELECTION = 1;
- private static final String ARG_TARGET_FILES = "TARGET_FILES";
- private Collection<OCFile> mTargetFiles;
- private ActionMode actionMode;
- /**
- * Public factory method to create new RemoveFilesDialogFragment instances.
- *
- * @param files Files to remove.
- * @param actionMode ActionMode to finish on confirmation
- * @return Dialog ready to show.
- */
- public static RemoveFilesDialogFragment newInstance(ArrayList<OCFile> files, ActionMode actionMode) {
- RemoveFilesDialogFragment dialogFragment = newInstance(files);
- dialogFragment.setActionMode(actionMode);
- return dialogFragment;
- }
- /**
- * Public factory method to create new RemoveFilesDialogFragment instances.
- *
- * @param files Files to remove.
- * @return Dialog ready to show.
- */
- public static RemoveFilesDialogFragment newInstance(ArrayList<OCFile> files) {
- RemoveFilesDialogFragment frag = new RemoveFilesDialogFragment();
- Bundle args = new Bundle();
- int messageStringId;
- boolean containsFolder = false;
- boolean containsDown = false;
- boolean containsFavorite = false;
- for (OCFile file: files) {
- containsFolder |= file.isFolder();
- containsDown |= file.isDown();
- containsFavorite |= file.isAvailableOffline();
- }
- if (files.size() == SINGLE_SELECTION) {
- // choose message for a single file
- OCFile file = files.get(0);
- messageStringId = file.isFolder() ?
- R.string.confirmation_remove_folder_alert :
- R.string.confirmation_remove_file_alert;
- } else {
- // choose message for more than one file
- messageStringId = containsFolder ?
- R.string.confirmation_remove_folders_alert :
- R.string.confirmation_remove_files_alert;
- }
- int localRemoveButton = (!containsFavorite && (containsFolder || containsDown)) ?
- R.string.confirmation_remove_local :
- -1;
- args.putInt(ARG_MESSAGE_RESOURCE_ID, messageStringId);
- if (files.size() == 1) {
- args.putStringArray(ARG_MESSAGE_ARGUMENTS, new String[]{files.get(0).getFileName()});
- }
- args.putInt(ARG_POSITIVE_BTN_RES, R.string.file_delete);
- args.putInt(ARG_NEUTRAL_BTN_RES, R.string.file_keep);
- args.putInt(ARG_NEGATIVE_BTN_RES, localRemoveButton);
- args.putParcelableArrayList(ARG_TARGET_FILES, files);
- frag.setArguments(args);
-
- return frag;
- }
- /**
- * Convenience factory method to create new RemoveFilesDialogFragment instances for a single file
- *
- * @param file File to remove.
- * @return Dialog ready to show.
- */
- public static RemoveFilesDialogFragment newInstance(OCFile file) {
- ArrayList<OCFile> list = new ArrayList<>();
- list.add(file);
- return newInstance(list);
- }
- @Override
- public void onStart() {
- super.onStart();
- int color = ThemeUtils.primaryAccentColor(getActivity());
- android.support.v7.app.AlertDialog alertDialog = (android.support.v7.app.AlertDialog) getDialog();
- alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
- alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color);
- alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(color);
- }
- @NonNull
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- Dialog dialog = super.onCreateDialog(savedInstanceState);
- mTargetFiles = getArguments().getParcelableArrayList(ARG_TARGET_FILES);
-
- setOnConfirmationListener(this);
-
- return dialog;
- }
- /**
- * Performs the removal of the target file, both locally and in the server and
- * finishes the supplied ActionMode if one was given.
- */
- @Override
- public void onConfirmation(String callerTag) {
- ComponentsGetter cg = (ComponentsGetter) getActivity();
- cg.getFileOperationsHelper().removeFiles(mTargetFiles, false, false);
- finishActionMode();
- }
-
- /**
- * Performs the removal of the local copy of the target file
- */
- @Override
- public void onCancel(String callerTag) {
- ComponentsGetter cg = (ComponentsGetter) getActivity();
- cg.getFileOperationsHelper().removeFiles(mTargetFiles, true, false);
- finishActionMode();
- }
- @Override
- public void onNeutral(String callerTag) {
- // nothing to do here
- }
- private void setActionMode(ActionMode actionMode) {
- this.actionMode = actionMode;
- }
- /**
- * This is used when finishing an actionMode,
- * for example if we want to exit the selection mode
- * after deleting the target files.
- */
- private void finishActionMode() {
- if (actionMode != null) {
- actionMode.finish();
- }
- }
- }
|