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