bf99932a1c21c741aa5fe3ed913a4c41399d5933
[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.OperationsServiceBinder;
38 import com.owncloud.android.ui.activity.ComponentsGetter;
39
40 /**
41 * Filters out the file actions available in a given {@link Menu} for a given {@link OCFile}
42 * according to the current state of the latest.
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 /**
100 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
101 *
102 * Decides what actions must be shown and hidden.
103 *
104 * @param toShow List to save the options that must be shown in the menu.
105 * @param toHide List to save the options that must be shown in the menu.
106 */
107 private void filter(List<Integer> toShow, List <Integer> toHide) {
108 boolean synchronizing = false;
109 if (mComponentsGetter != null && mFile != null && mAccount != null) {
110 OperationsServiceBinder opsBinder = mComponentsGetter.getOperationsServiceBinder();
111 FileUploaderBinder uploaderBinder = mComponentsGetter.getFileUploaderBinder();
112 FileDownloaderBinder downloaderBinder = mComponentsGetter.getFileDownloaderBinder();
113 synchronizing = (
114 // comparing local and remote
115 (opsBinder != null && opsBinder.isSynchronizing(mAccount, mFile.getRemotePath())) ||
116 // downloading
117 (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) ||
118 // uploading
119 (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile))
120 );
121 }
122
123 /// decision is taken for each possible action on a file in the menu
124
125 // DOWNLOAD
126 if (mFile == null || mFile.isDown() || mFile.isFolder() || synchronizing) {
127 toHide.add(R.id.action_download_file);
128
129 } else {
130 toShow.add(R.id.action_download_file);
131 }
132
133 // RENAME
134 if (mFile == null || synchronizing) {
135 toHide.add(R.id.action_rename_file);
136
137 } else {
138 toShow.add(R.id.action_rename_file);
139 }
140
141 // MOVE & COPY
142 if (mFile == null || synchronizing) {
143 toHide.add(R.id.action_move);
144 toHide.add(R.id.action_copy);
145 } else {
146 toShow.add(R.id.action_move);
147 toShow.add(R.id.action_copy);
148 }
149
150 // REMOVE
151 if (mFile == null || synchronizing) {
152 toHide.add(R.id.action_remove_file);
153
154 } else {
155 toShow.add(R.id.action_remove_file);
156 }
157
158 // OPEN WITH (different to preview!)
159 if (mFile == null || mFile.isFolder() || !mFile.isDown() || synchronizing) {
160 toHide.add(R.id.action_open_file_with);
161
162 } else {
163 toShow.add(R.id.action_open_file_with);
164 }
165
166 // CANCEL SYNCHRONIZATION
167 if (mFile == null || !synchronizing) {
168 toHide.add(R.id.action_cancel_sync);
169
170 } else {
171 toShow.add(R.id.action_cancel_sync);
172 }
173
174 // SYNC CONTENTS (BOTH FILE AND FOLDER)
175 if (mFile == null || (!mFile.isFolder() && !mFile.isDown()) || synchronizing) {
176 toHide.add(R.id.action_sync_file);
177
178 } else {
179 toShow.add(R.id.action_sync_file);
180 }
181
182 // SHARE FILE
183 // TODO add check on SHARE available on server side?
184 boolean shareAllowed = (mContext != null &&
185 mContext.getString(R.string.share_feature).equalsIgnoreCase("on"));
186 if (!shareAllowed || mFile == null) {
187 toHide.add(R.id.action_share_file);
188 } else {
189 toShow.add(R.id.action_share_file);
190 }
191
192 // UNSHARE FILE
193 // TODO add check on SHARE available on server side?
194 if ( !shareAllowed || (mFile == null || !mFile.isShareByLink())) {
195 toHide.add(R.id.action_unshare_file);
196 } else {
197 toShow.add(R.id.action_unshare_file);
198 }
199
200 // SEE DETAILS
201 if (mFile == null || mFile.isFolder()) {
202 toHide.add(R.id.action_see_details);
203 } else {
204 toShow.add(R.id.action_see_details);
205 }
206
207 // SEND
208 boolean sendAllowed = (mContext != null &&
209 mContext.getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on"));
210 if (mFile == null || !sendAllowed || mFile.isFolder() || synchronizing) {
211 toHide.add(R.id.action_send_file);
212 } else {
213 toShow.add(R.id.action_send_file);
214 }
215
216 // FAVORITES
217 if (mFile == null || synchronizing || mFile.isFolder() || mFile.isFavorite()) {
218 toHide.add(R.id.action_favorite_file);
219 } else {
220 toShow.add(R.id.action_favorite_file);
221 }
222
223 // UNFAVORITES
224 if (mFile == null || synchronizing || mFile.isFolder() || !mFile.isFavorite()) {
225 toHide.add(R.id.action_unfavorite_file);
226 } else {
227 toShow.add(R.id.action_unfavorite_file);
228 }
229
230 // SET PICTURE AS
231 if (mFile == null || (!mFile.isAudio() && !mFile.isVideo())){
232 toHide.add(R.id.action_set_as_wallpaper);
233 } else {
234 toShow.add(R.id.action_set_as_wallpaper);
235 }
236
237 }
238
239 }