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