0c3f18a10463ab61f963e0f98506378b3ab463d4
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / FileMenuFilter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.files;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import android.accounts.Account;
24 import android.content.Context;
25 import android.view.Menu;
26 import android.view.MenuItem;
27
28 import com.owncloud.android.R;
29 import com.owncloud.android.datamodel.OCFile;
30 import com.owncloud.android.files.services.FileDownloader;
31 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
32 import com.owncloud.android.files.services.FileUploader;
33 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
34 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
35 import com.owncloud.android.ui.activity.ComponentsGetter;
36
37 /**
38 * Filters out the file actions available in a given {@link Menu} for a given {@link OCFile}
39 * according to the current state of the latest.
40 *
41 * @author David A. Velasco
42 */
43 public class FileMenuFilter {
44
45 private OCFile mFile;
46 private ComponentsGetter mComponentsGetter;
47 private Account mAccount;
48 private Context mContext;
49
50 /**
51 * Constructor
52 *
53 * @param targetFile {@link OCFile} target of the action to filter in the {@link Menu}.
54 * @param account ownCloud {@link Account} holding targetFile.
55 * @param cg Accessor to app components, needed to get access the
56 * {@link FileUploader} and {@link FileDownloader} services.
57 * @param context Android {@link Context}, needed to access build setup resources.
58 */
59 public FileMenuFilter(OCFile targetFile, Account account, ComponentsGetter cg, Context context) {
60 mFile = targetFile;
61 mAccount = account;
62 mComponentsGetter = cg;
63 mContext = context;
64 }
65
66
67 /**
68 * Filters out the file actions available in the passed {@link Menu} taken into account
69 * the state of the {@link OCFile} held by the filter.
70 *
71 * @param menu Options or context menu to filter.
72 */
73 public void filter(Menu menu) {
74 List<Integer> toShow = new ArrayList<Integer>();
75 List<Integer> toHide = new ArrayList<Integer>();
76
77 filter(toShow, toHide);
78
79 MenuItem item = null;
80 for (int i : toShow) {
81 item = menu.findItem(i);
82 if (item != null) {
83 item.setVisible(true);
84 item.setEnabled(true);
85 }
86 }
87
88 for (int i : toHide) {
89 item = menu.findItem(i);
90 if (item != null) {
91 item.setVisible(false);
92 item.setEnabled(false);
93 }
94 }
95 }
96
97 /**
98 * Filters out the file actions available in the passed {@link Menu} taken into account
99 * the state of the {@link OCFile} held by the filter.
100 *
101 * Second method needed thanks to ActionBarSherlock.
102 *
103 * TODO Get rid of it when ActionBarSherlock is replaced for newer Android Support Library.
104 *
105 * @param menu Options or context menu to filter.
106 */
107 public void filter(com.actionbarsherlock.view.Menu menu) {
108
109 List<Integer> toShow = new ArrayList<Integer>();
110 List<Integer> toHide = new ArrayList<Integer>();
111
112 filter(toShow, toHide);
113
114 com.actionbarsherlock.view.MenuItem item = null;
115 for (int i : toShow) {
116 item = menu.findItem(i);
117 if (item != null) {
118 item.setVisible(true);
119 item.setEnabled(true);
120 }
121 }
122 for (int i : toHide) {
123 item = menu.findItem(i);
124 if (item != null) {
125 item.setVisible(false);
126 item.setEnabled(false);
127 }
128 }
129 }
130
131 /**
132 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
133 *
134 * Decides what actions must be shown and hidden.
135 *
136 * @param toShow List to save the options that must be shown in the menu.
137 * @param toHide List to save the options that must be shown in the menu.
138 */
139 private void filter(List<Integer> toShow, List <Integer> toHide) {
140 boolean downloading = false;
141 boolean uploading = false;
142 if (mComponentsGetter != null && mFile != null && mAccount != null) {
143 FileDownloaderBinder downloaderBinder = mComponentsGetter.getFileDownloaderBinder();
144 downloading = downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile);
145 OperationsServiceBinder opsBinder = mComponentsGetter.getOperationsServiceBinder();
146 downloading |= (
147 mFile.isFolder() && opsBinder != null && opsBinder.isSynchronizing(mAccount, mFile.getRemotePath())
148 );
149 FileUploaderBinder uploaderBinder = mComponentsGetter.getFileUploaderBinder();
150 uploading = uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile);
151 }
152
153 /// decision is taken for each possible action on a file in the menu
154
155 // DOWNLOAD
156 if (mFile == null || mFile.isDown() || downloading || uploading) {
157 toHide.add(R.id.action_download_file);
158
159 } else {
160 toShow.add(R.id.action_download_file);
161 }
162
163 // RENAME
164 if (mFile == null || downloading || uploading) {
165 toHide.add(R.id.action_rename_file);
166
167 } else {
168 toShow.add(R.id.action_rename_file);
169 }
170
171 // MOVE
172 if (mFile == null || downloading || uploading) {
173 toHide.add(R.id.action_move);
174
175 } else {
176 toShow.add(R.id.action_move);
177 }
178
179 // REMOVE
180 if (mFile == null || downloading || uploading) {
181 toHide.add(R.id.action_remove_file);
182
183 } else {
184 toShow.add(R.id.action_remove_file);
185 }
186
187 // OPEN WITH (different to preview!)
188 if (mFile == null || mFile.isFolder() || !mFile.isDown() || downloading || uploading) {
189 toHide.add(R.id.action_open_file_with);
190
191 } else {
192 toShow.add(R.id.action_open_file_with);
193 }
194
195
196 // CANCEL DOWNLOAD
197 if (mFile == null || !downloading) {
198 toHide.add(R.id.action_cancel_download);
199 } else {
200 toShow.add(R.id.action_cancel_download);
201 }
202
203 // CANCEL UPLOAD
204 if (mFile == null || !uploading || mFile.isFolder()) {
205 toHide.add(R.id.action_cancel_upload);
206 } else {
207 toShow.add(R.id.action_cancel_upload);
208 }
209
210 // SYNC FILE CONTENTS
211 if (mFile == null || mFile.isFolder() || !mFile.isDown() || downloading || uploading) {
212 toHide.add(R.id.action_sync_file);
213 } else {
214 toShow.add(R.id.action_sync_file);
215 }
216
217 // SHARE FILE
218 // TODO add check on SHARE available on server side?
219 if (mFile == null) {
220 toHide.add(R.id.action_share_file);
221 } else {
222 toShow.add(R.id.action_share_file);
223 }
224
225 // UNSHARE FILE
226 // TODO add check on SHARE available on server side?
227 if (mFile == null || !mFile.isShareByLink()) {
228 toHide.add(R.id.action_unshare_file);
229 } else {
230 toShow.add(R.id.action_unshare_file);
231 }
232
233
234 // SEE DETAILS
235 if (mFile == null || mFile.isFolder()) {
236 toHide.add(R.id.action_see_details);
237 } else {
238 toShow.add(R.id.action_see_details);
239 }
240
241 // SEND
242 boolean sendAllowed = (mContext != null &&
243 mContext.getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on"));
244 if (mFile == null || !sendAllowed || mFile.isFolder() || uploading || downloading) {
245 toHide.add(R.id.action_send_file);
246 } else {
247 toShow.add(R.id.action_send_file);
248 }
249
250 }
251
252 }