1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.dialog
;
21 * Dialog requiring confirmation before removing a given OCFile.
23 * Triggers the removal according to the user response.
25 * @author David A. Velasco
28 import java
.util
.Vector
;
30 import android
.app
.Dialog
;
31 import android
.content
.Intent
;
32 import android
.media
.MediaScannerConnection
;
33 import android
.net
.Uri
;
34 import android
.os
.Bundle
;
36 import com
.owncloud
.android
.MainApp
;
37 import com
.owncloud
.android
.R
;
38 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
39 import com
.owncloud
.android
.datamodel
.OCFile
;
40 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
41 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
42 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
44 public class RemoveFileDialogFragment
extends ConfirmationDialogFragment
45 implements ConfirmationDialogFragmentListener
{
47 private static final String ARG_TARGET_FILE
= "TARGET_FILE";
50 * Public factory method to create new RemoveFileDialogFragment instances.
52 * @param file File to remove.
53 * @return Dialog ready to show.
55 public static RemoveFileDialogFragment
newInstance(OCFile file
) {
56 RemoveFileDialogFragment frag
= new RemoveFileDialogFragment();
57 Bundle args
= new Bundle();
59 int messageStringId
= R
.string
.confirmation_remove_alert
;
61 int posBtn
= R
.string
.confirmation_remove_remote
;
63 if (file
.isFolder()) {
64 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
65 posBtn
= R
.string
.confirmation_remove_remote_and_local
;
66 neuBtn
= R
.string
.confirmation_remove_folder_local
;
67 } else if (file
.isDown()) {
68 posBtn
= R
.string
.confirmation_remove_remote_and_local
;
69 neuBtn
= R
.string
.confirmation_remove_local
;
73 args
.putInt(ARG_CONF_RESOURCE_ID
, messageStringId
);
74 args
.putStringArray(ARG_CONF_ARGUMENTS
, new String
[]{file
.getFileName()});
75 args
.putInt(ARG_POSITIVE_BTN_RES
, posBtn
);
76 args
.putInt(ARG_NEUTRAL_BTN_RES
, neuBtn
);
77 args
.putInt(ARG_NEGATIVE_BTN_RES
, R
.string
.common_cancel
);
78 args
.putParcelable(ARG_TARGET_FILE
, file
);
79 frag
.setArguments(args
);
84 private OCFile mTargetFile
;
87 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
88 Dialog dialog
= super.onCreateDialog(savedInstanceState
);
89 mTargetFile
= getArguments().getParcelable(ARG_TARGET_FILE
);
91 setOnConfirmationListener(this);
97 * Performs the removal of the target file, both locally and in the server.
100 public void onConfirmation(String callerTag
) {
101 ComponentsGetter cg
= (ComponentsGetter
)getSherlockActivity();
102 FileDataStorageManager storageManager
= cg
.getStorageManager();
103 if (storageManager
.getFileById(mTargetFile
.getFileId()) != null
) {
104 String path
= mTargetFile
.getStoragePath();
105 cg
.getFileOperationsHelper().removeFile(mTargetFile
, false
);
106 triggerMediaScan(path
);
111 * Performs the removal of the local copy of the target file
114 public void onNeutral(String callerTag
) {
115 String path
= mTargetFile
.getStoragePath();
116 ComponentsGetter cg
= (ComponentsGetter
)getSherlockActivity();
117 cg
.getFileOperationsHelper().removeFile(mTargetFile
, true
);
119 FileDataStorageManager storageManager
= cg
.getStorageManager();
121 boolean containsKeepInSync
= false
;
122 if (mTargetFile
.isFolder()) {
123 Vector
<OCFile
> files
= storageManager
.getFolderContent(mTargetFile
);
124 for(OCFile file
: files
) {
125 containsKeepInSync
= file
.keepInSync() || containsKeepInSync
;
127 if (containsKeepInSync
)
132 // Remove etag for parent, if file is a keep_in_sync
133 // or is a folder and contains keep_in_sync
134 if (mTargetFile
.keepInSync() || containsKeepInSync
) {
135 OCFile folder
= null
;
136 if (mTargetFile
.isFolder()) {
137 folder
= mTargetFile
;
139 folder
= storageManager
.getFileById(mTargetFile
.getParentId());
143 storageManager
.saveFile(folder
);
147 triggerMediaScan(path
);
151 public void onCancel(String callerTag
) {
152 // nothing to do here
155 private void triggerMediaScan(String path
){
157 Intent intent
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
158 intent
.setData(Uri
.fromFile(new File(path
)));
159 MainApp
.getAppContext().sendBroadcast(intent
);
160 } catch (Exception e
){
161 Log_OC
.d("Trigger", "exception: " + e
);