66ada1c070f8c745c00e6766ae76a8297a64cd39
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / FileMenuFilter.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.files;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import android.accounts.Account;
27 import android.content.Context;
28 import android.view.Menu;
29 import android.view.MenuItem;
30
31 import com.owncloud.android.R;
32 import com.owncloud.android.authentication.AccountUtils;
33 import com.owncloud.android.datamodel.OCFile;
34 import com.owncloud.android.files.services.FileDownloader;
35 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
36 import com.owncloud.android.files.services.FileUploader;
37 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
38 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
39 import com.owncloud.android.ui.activity.ComponentsGetter;
40
41 /**
42 * Filters out the file actions available in a given {@link Menu} for a given {@link OCFile}
43 * according to the current state of the latest.
44 */
45 public class FileMenuFilter {
46
47 private OCFile mFile;
48 private ComponentsGetter mComponentsGetter;
49 private Account mAccount;
50 private Context mContext;
51
52 /**
53 * Constructor
54 *
55 * @param targetFile {@link OCFile} target of the action to filter in the {@link Menu}.
56 * @param account ownCloud {@link Account} holding targetFile.
57 * @param cg Accessor to app components, needed to access the
58 * {@link FileUploader} and {@link FileDownloader} services
59 * @param context Android {@link Context}, needed to access build setup resources.
60 */
61 public FileMenuFilter(OCFile targetFile, Account account, ComponentsGetter cg,
62 Context context) {
63 mFile = targetFile;
64 mAccount = account;
65 mComponentsGetter = cg;
66 mContext = context;
67 }
68
69
70 /**
71 * Filters out the file actions available in the passed {@link Menu} taken into account
72 * the state of the {@link OCFile} held by the filter.
73 *
74 * @param menu Options or context menu to filter.
75 */
76 public void filter(Menu menu) {
77 List<Integer> toShow = new ArrayList<Integer>();
78 List<Integer> toHide = new ArrayList<Integer>();
79
80 filter(toShow, toHide);
81
82 MenuItem item = null;
83 for (int i : toShow) {
84 item = menu.findItem(i);
85 if (item != null) {
86 item.setVisible(true);
87 item.setEnabled(true);
88 }
89 }
90
91 for (int i : toHide) {
92 item = menu.findItem(i);
93 if (item != null) {
94 item.setVisible(false);
95 item.setEnabled(false);
96 }
97 }
98 }
99
100
101 /**
102 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
103 *
104 * Decides what actions must be shown and hidden.
105 *
106 * @param toShow List to save the options that must be shown in the menu.
107 * @param toHide List to save the options that must be shown in the menu.
108 */
109 private void filter(List<Integer> toShow, List <Integer> toHide) {
110 boolean synchronizing = false;
111 if (mComponentsGetter != null && mFile != null && mAccount != null) {
112 OperationsServiceBinder opsBinder = mComponentsGetter.getOperationsServiceBinder();
113 FileUploaderBinder uploaderBinder = mComponentsGetter.getFileUploaderBinder();
114 FileDownloaderBinder downloaderBinder = mComponentsGetter.getFileDownloaderBinder();
115 synchronizing = (
116 // comparing local and remote
117 (opsBinder != null && opsBinder.isSynchronizing(mAccount, mFile.getRemotePath())) ||
118 // downloading
119 (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) ||
120 // uploading
121 (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile))
122 );
123 }
124
125 /// decision is taken for each possible action on a file in the menu
126
127 // DOWNLOAD
128 if (mFile == null || mFile.isDown() || mFile.isFolder() || synchronizing) {
129 toHide.add(R.id.action_download_file);
130
131 } else {
132 toShow.add(R.id.action_download_file);
133 }
134
135 // RENAME
136 if (mFile == null || synchronizing) {
137 toHide.add(R.id.action_rename_file);
138
139 } else {
140 toShow.add(R.id.action_rename_file);
141 }
142
143 // MOVE & COPY
144 if (mFile == null || synchronizing) {
145 toHide.add(R.id.action_move);
146 toHide.add(R.id.action_copy);
147 } else {
148 toShow.add(R.id.action_move);
149 toShow.add(R.id.action_copy);
150 }
151
152 // REMOVE
153 if (mFile == null || synchronizing) {
154 toHide.add(R.id.action_remove_file);
155
156 } else {
157 toShow.add(R.id.action_remove_file);
158 }
159
160 // OPEN WITH (different to preview!)
161 if (mFile == null || mFile.isFolder() || !mFile.isDown() || synchronizing) {
162 toHide.add(R.id.action_open_file_with);
163
164 } else {
165 toShow.add(R.id.action_open_file_with);
166 }
167
168 // CANCEL SYNCHRONIZATION
169 if (mFile == null || !synchronizing) {
170 toHide.add(R.id.action_cancel_sync);
171
172 } else {
173 toShow.add(R.id.action_cancel_sync);
174 }
175
176 // SYNC CONTENTS (BOTH FILE AND FOLDER)
177 if (mFile == null || (!mFile.isFolder() && !mFile.isDown()) || synchronizing) {
178 toHide.add(R.id.action_sync_file);
179
180 } else {
181 toShow.add(R.id.action_sync_file);
182 }
183
184 // SHARE FILE
185 // TODO add check on SHARE available on server side?
186 boolean shareAllowed = (mContext != null &&
187 mContext.getString(R.string.share_feature).equalsIgnoreCase("on"));
188 if (!shareAllowed || mFile == null) {
189 toHide.add(R.id.action_share_file);
190 } else {
191 toShow.add(R.id.action_share_file);
192 }
193
194 // UNSHARE FILE
195 // TODO add check on SHARE available on server side?
196 if ( !shareAllowed || (mFile == null || !mFile.isSharedViaLink())) {
197 toHide.add(R.id.action_unshare_file);
198 } else {
199 toShow.add(R.id.action_unshare_file);
200 }
201
202 // SHARE FILE, with Users
203 if (!shareAllowed || mFile == null) {
204 toHide.add(R.id.action_share_with_users);
205 } else {
206 toShow.add(R.id.action_share_with_users);
207 }
208
209
210 // SEE DETAILS
211 if (mFile == null || mFile.isFolder()) {
212 toHide.add(R.id.action_see_details);
213 } else {
214 toShow.add(R.id.action_see_details);
215 }
216
217 // SEND
218 boolean sendAllowed = (mContext != null &&
219 mContext.getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on"));
220 if (mFile == null || !sendAllowed || mFile.isFolder() || synchronizing) {
221 toHide.add(R.id.action_send_file);
222 } else {
223 toShow.add(R.id.action_send_file);
224 }
225
226 // FAVORITES
227 if (mFile == null || synchronizing || mFile.isFolder() || mFile.isFavorite()) {
228 toHide.add(R.id.action_favorite_file);
229 } else {
230 toShow.add(R.id.action_favorite_file);
231 }
232
233 // UNFAVORITES
234 if (mFile == null || synchronizing || mFile.isFolder() || !mFile.isFavorite()) {
235 toHide.add(R.id.action_unfavorite_file);
236 } else {
237 toShow.add(R.id.action_unfavorite_file);
238 }
239
240 }
241
242 }