b363f5581aa1eeee7453e33c9aad25680fee469a
[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.datamodel.OCFile;
33 import com.owncloud.android.files.services.FileDownloader;
34 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
35 import com.owncloud.android.files.services.FileUploader;
36 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
37 import com.owncloud.android.services.OperationsService;
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, Context context) {
62 mFile = targetFile;
63 mAccount = account;
64 mComponentsGetter = cg;
65 mContext = context;
66 }
67
68
69 /**
70 * Filters out the file actions available in the passed {@link Menu} taken into account
71 * the state of the {@link OCFile} held by the filter.
72 *
73 * @param menu Options or context menu to filter.
74 */
75 public void filter(Menu menu) {
76 List<Integer> toShow = new ArrayList<Integer>();
77 List<Integer> toHide = new ArrayList<Integer>();
78
79 filter(toShow, toHide);
80
81 MenuItem item = null;
82 for (int i : toShow) {
83 item = menu.findItem(i);
84 if (item != null) {
85 item.setVisible(true);
86 item.setEnabled(true);
87 }
88 }
89
90 for (int i : toHide) {
91 item = menu.findItem(i);
92 if (item != null) {
93 item.setVisible(false);
94 item.setEnabled(false);
95 }
96 }
97 }
98
99
100 /**
101 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
102 *
103 * Decides what actions must be shown and hidden.
104 *
105 * @param toShow List to save the options that must be shown in the menu.
106 * @param toHide List to save the options that must be shown in the menu.
107 */
108 private void filter(List<Integer> toShow, List <Integer> toHide) {
109 boolean synchronizing = false;
110 if (mComponentsGetter != null && mFile != null && mAccount != null) {
111 OperationsServiceBinder opsBinder = mComponentsGetter.getOperationsServiceBinder();
112 FileUploaderBinder uploaderBinder = mComponentsGetter.getFileUploaderBinder();
113 FileDownloaderBinder downloaderBinder = mComponentsGetter.getFileDownloaderBinder();
114 synchronizing = (
115 // comparing local and remote
116 (opsBinder != null && opsBinder.isSynchronizing(mAccount, mFile.getRemotePath())) ||
117 // downloading
118 (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) ||
119 // uploading
120 (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile))
121 );
122 }
123
124 /*
125 boolean downloading = false;
126 boolean uploading = false;
127 if (mComponentsGetter != null && mFile != null && mAccount != null) {
128 FileDownloaderBinder downloaderBinder = mComponentsGetter.getFileDownloaderBinder();
129 downloading = (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile));
130 OperationsServiceBinder opsBinder = mComponentsGetter.getOperationsServiceBinder();
131 downloading |= (opsBinder != null && opsBinder.isSynchronizing(mAccount, mFile.getRemotePath()));
132 FileUploaderBinder uploaderBinder = mComponentsGetter.getFileUploaderBinder();
133 uploading = (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile));
134 }
135 */
136
137 /// decision is taken for each possible action on a file in the menu
138
139 // DOWNLOAD
140 if (mFile == null || mFile.isDown() || synchronizing) {
141 toHide.add(R.id.action_download_file);
142
143 } else {
144 toShow.add(R.id.action_download_file);
145 }
146
147 // RENAME
148 if (mFile == null || synchronizing) {
149 toHide.add(R.id.action_rename_file);
150
151 } else {
152 toShow.add(R.id.action_rename_file);
153 }
154
155 // MOVE & COPY
156 if (mFile == null || synchronizing) {
157 toHide.add(R.id.action_move);
158 toHide.add(R.id.action_copy);
159 } else {
160 toShow.add(R.id.action_move);
161 toShow.add(R.id.action_copy);
162 }
163
164 // REMOVE
165 if (mFile == null || synchronizing) {
166 toHide.add(R.id.action_remove_file);
167
168 } else {
169 toShow.add(R.id.action_remove_file);
170 }
171
172 // OPEN WITH (different to preview!)
173 if (mFile == null || mFile.isFolder() || !mFile.isDown() || synchronizing) {
174 toHide.add(R.id.action_open_file_with);
175
176 } else {
177 toShow.add(R.id.action_open_file_with);
178 }
179
180 // CANCEL SYNCHRONIZATION
181 if (mFile == null || !synchronizing) {
182 toHide.add(R.id.action_cancel_sync);
183
184 } else {
185 toShow.add(R.id.action_cancel_sync);
186 }
187
188 // SYNC CONTENTS (BOTH FILE AND FOLDER)
189 if (mFile == null || (!mFile.isFolder() && !mFile.isDown()) || synchronizing) {
190 toHide.add(R.id.action_sync_file);
191
192 } else {
193 toShow.add(R.id.action_sync_file);
194 }
195
196 // SHARE FILE
197 // TODO add check on SHARE available on server side?
198 boolean shareAllowed = (mContext != null &&
199 mContext.getString(R.string.share_feature).equalsIgnoreCase("on"));
200 if (!shareAllowed || mFile == null) {
201 toHide.add(R.id.action_share_file);
202 } else {
203 toShow.add(R.id.action_share_file);
204 }
205
206 // UNSHARE FILE
207 // TODO add check on SHARE available on server side?
208 if ( !shareAllowed || (mFile == null || !mFile.isShareByLink())) {
209 toHide.add(R.id.action_unshare_file);
210 } else {
211 toShow.add(R.id.action_unshare_file);
212 }
213
214 // SEE DETAILS
215 if (mFile == null || mFile.isFolder()) {
216 toHide.add(R.id.action_see_details);
217 } else {
218 toShow.add(R.id.action_see_details);
219 }
220
221 // SEND
222 boolean sendAllowed = (mContext != null &&
223 mContext.getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on"));
224 if (mFile == null || !sendAllowed || mFile.isFolder() || synchronizing) {
225 toHide.add(R.id.action_send_file);
226 } else {
227 toShow.add(R.id.action_send_file);
228 }
229
230 // FAVORITES
231 if (mFile == null || synchronizing || mFile.isFolder() || mFile.isFavorite()) {
232 toHide.add(R.id.action_favorite_file);
233 } else {
234 toShow.add(R.id.action_favorite_file);
235 }
236
237 // UNFAVORITES
238 if (mFile == null || synchronizing || mFile.isFolder() || !mFile.isFavorite()) {
239 toHide.add(R.id.action_unfavorite_file);
240 } else {
241 toShow.add(R.id.action_unfavorite_file);
242 }
243
244 }
245
246 }