2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
21 package com
.owncloud
.android
.files
;
23 import java
.util
.ArrayList
;
24 import java
.util
.List
;
26 import android
.accounts
.Account
;
27 import android
.content
.Context
;
28 import android
.view
.Menu
;
29 import android
.view
.MenuItem
;
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
;
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.
45 public class FileMenuFilter
{
48 private ComponentsGetter mComponentsGetter
;
49 private Account mAccount
;
50 private Context mContext
;
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.
61 public FileMenuFilter(OCFile targetFile
, Account account
, ComponentsGetter cg
, Context context
) {
64 mComponentsGetter
= cg
;
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.
73 * @param menu Options or context menu to filter.
75 public void filter(Menu menu
) {
76 List
<Integer
> toShow
= new ArrayList
<Integer
>();
77 List
<Integer
> toHide
= new ArrayList
<Integer
>();
79 filter(toShow
, toHide
);
82 for (int i
: toShow
) {
83 item
= menu
.findItem(i
);
85 item
.setVisible(true
);
86 item
.setEnabled(true
);
90 for (int i
: toHide
) {
91 item
= menu
.findItem(i
);
93 item
.setVisible(false
);
94 item
.setEnabled(false
);
100 * Filters out the file actions available in the passed {@link Menu} taken into account
101 * the state of the {@link OCFile} held by the filter.
103 * Second method needed thanks to ActionBarSherlock.
105 * TODO Get rid of it when ActionBarSherlock is replaced for newer Android Support Library.
107 * @param menu Options or context menu to filter.
109 public void filter(com
.actionbarsherlock
.view
.Menu menu
) {
111 List
<Integer
> toShow
= new ArrayList
<Integer
>();
112 List
<Integer
> toHide
= new ArrayList
<Integer
>();
114 filter(toShow
, toHide
);
116 com
.actionbarsherlock
.view
.MenuItem item
= null
;
117 for (int i
: toShow
) {
118 item
= menu
.findItem(i
);
120 item
.setVisible(true
);
121 item
.setEnabled(true
);
124 for (int i
: toHide
) {
125 item
= menu
.findItem(i
);
127 item
.setVisible(false
);
128 item
.setEnabled(false
);
134 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
136 * Decides what actions must be shown and hidden.
138 * @param toShow List to save the options that must be shown in the menu.
139 * @param toHide List to save the options that must be shown in the menu.
141 private void filter(List
<Integer
> toShow
, List
<Integer
> toHide
) {
142 boolean downloading
= false
;
143 boolean uploading
= false
;
144 if (mComponentsGetter
!= null
&& mFile
!= null
&& mAccount
!= null
) {
145 FileDownloaderBinder downloaderBinder
= mComponentsGetter
.getFileDownloaderBinder();
146 downloading
= (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
));
147 OperationsServiceBinder opsBinder
= mComponentsGetter
.getOperationsServiceBinder();
148 downloading
|= (opsBinder
!= null
&& opsBinder
.isSynchronizing(mAccount
, mFile
.getRemotePath()));
149 FileUploaderBinder uploaderBinder
= mComponentsGetter
.getFileUploaderBinder();
150 uploading
= (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, mFile
));
153 /// decision is taken for each possible action on a file in the menu
156 if (mFile
== null
|| mFile
.isDown() || downloading
|| uploading
) {
157 toHide
.add(R
.id
.action_download_file
);
160 toShow
.add(R
.id
.action_download_file
);
164 if (mFile
== null
|| downloading
|| uploading
) {
165 toHide
.add(R
.id
.action_rename_file
);
168 toShow
.add(R
.id
.action_rename_file
);
172 if (mFile
== null
|| downloading
|| uploading
) {
173 toHide
.add(R
.id
.action_move
);
176 toShow
.add(R
.id
.action_move
);
180 if (mFile
== null
|| downloading
|| uploading
) {
181 toHide
.add(R
.id
.action_remove_file
);
184 toShow
.add(R
.id
.action_remove_file
);
187 // OPEN WITH (different to preview!)
188 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || downloading
|| uploading
) {
189 toHide
.add(R
.id
.action_open_file_with
);
192 toShow
.add(R
.id
.action_open_file_with
);
197 if (mFile
== null
|| !downloading
) {
198 toHide
.add(R
.id
.action_cancel_download
);
200 toShow
.add(R
.id
.action_cancel_download
);
204 if (mFile
== null
|| !uploading
|| mFile
.isFolder()) {
205 toHide
.add(R
.id
.action_cancel_upload
);
207 toShow
.add(R
.id
.action_cancel_upload
);
210 // SYNC FILE CONTENTS
211 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || downloading
|| uploading
) {
212 toHide
.add(R
.id
.action_sync_file
);
214 toShow
.add(R
.id
.action_sync_file
);
218 // TODO add check on SHARE available on server side?
219 boolean shareAllowed
= (mContext
!= null
&&
220 mContext
.getString(R
.string
.share_link
).equalsIgnoreCase("on"));
221 if (!shareAllowed
|| mFile
== null
) {
222 toHide
.add(R
.id
.action_share_file
);
224 toShow
.add(R
.id
.action_share_file
);
228 // TODO add check on SHARE available on server side?
229 if ( !shareAllowed
|| (mFile
== null
|| !mFile
.isShareByLink())) {
230 toHide
.add(R
.id
.action_unshare_file
);
232 toShow
.add(R
.id
.action_unshare_file
);
236 if (mFile
== null
|| mFile
.isFolder()) {
237 toHide
.add(R
.id
.action_see_details
);
239 toShow
.add(R
.id
.action_see_details
);
243 boolean sendAllowed
= (mContext
!= null
&&
244 mContext
.getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on"));
245 if (mFile
== null
|| !sendAllowed
|| mFile
.isFolder() || uploading
|| downloading
) {
246 toHide
.add(R
.id
.action_send_file
);
248 toShow
.add(R
.id
.action_send_file
);