1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.files
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
23 import android
.accounts
.Account
;
24 import android
.content
.Context
;
25 import android
.view
.Menu
;
26 import android
.view
.MenuItem
;
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
;
35 import com
.owncloud
.android
.services
.OperationsService
.OperationsServiceBinder
;
36 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
39 * Filters out the file actions available in a given {@link Menu} for a given {@link OCFile}
40 * according to the current state of the latest.
42 * @author David A. Velasco
44 public class FileMenuFilter
{
47 private ComponentsGetter mComponentsGetter
;
48 private Account mAccount
;
49 private Context mContext
;
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.
60 public FileMenuFilter(OCFile targetFile
, Account account
, ComponentsGetter cg
, Context context
) {
63 mComponentsGetter
= cg
;
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.
72 * @param menu Options or context menu to filter.
74 public void filter(Menu menu
) {
75 List
<Integer
> toShow
= new ArrayList
<Integer
>();
76 List
<Integer
> toHide
= new ArrayList
<Integer
>();
78 filter(toShow
, toHide
);
81 for (int i
: toShow
) {
82 item
= menu
.findItem(i
);
84 item
.setVisible(true
);
85 item
.setEnabled(true
);
89 for (int i
: toHide
) {
90 item
= menu
.findItem(i
);
92 item
.setVisible(false
);
93 item
.setEnabled(false
);
99 * Filters out the file actions available in the passed {@link Menu} taken into account
100 * the state of the {@link OCFile} held by the filter.
102 * Second method needed thanks to ActionBarSherlock.
104 * TODO Get rid of it when ActionBarSherlock is replaced for newer Android Support Library.
106 * @param menu Options or context menu to filter.
108 public void filter(com
.actionbarsherlock
.view
.Menu menu
) {
110 List
<Integer
> toShow
= new ArrayList
<Integer
>();
111 List
<Integer
> toHide
= new ArrayList
<Integer
>();
113 filter(toShow
, toHide
);
115 com
.actionbarsherlock
.view
.MenuItem item
= null
;
116 for (int i
: toShow
) {
117 item
= menu
.findItem(i
);
119 item
.setVisible(true
);
120 item
.setEnabled(true
);
123 for (int i
: toHide
) {
124 item
= menu
.findItem(i
);
126 item
.setVisible(false
);
127 item
.setEnabled(false
);
133 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
135 * Decides what actions must be shown and hidden.
137 * @param toShow List to save the options that must be shown in the menu.
138 * @param toHide List to save the options that must be shown in the menu.
140 private void filter(List
<Integer
> toShow
, List
<Integer
> toHide
) {
141 boolean downloading
= false
;
142 boolean uploading
= false
;
143 if (mComponentsGetter
!= null
&& mFile
!= null
&& mAccount
!= null
) {
144 FileDownloaderBinder downloaderBinder
= mComponentsGetter
.getFileDownloaderBinder();
145 downloading
= (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
));
146 OperationsServiceBinder opsBinder
= mComponentsGetter
.getOperationsServiceBinder();
147 downloading
|= (opsBinder
!= null
&& opsBinder
.isSynchronizing(mAccount
, mFile
.getRemotePath()));
148 FileUploaderBinder uploaderBinder
= mComponentsGetter
.getFileUploaderBinder();
149 uploading
= (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, mFile
));
152 /// decision is taken for each possible action on a file in the menu
155 if (mFile
== null
|| mFile
.isDown() || downloading
|| uploading
) {
156 toHide
.add(R
.id
.action_download_file
);
159 toShow
.add(R
.id
.action_download_file
);
163 if (mFile
== null
|| downloading
|| uploading
) {
164 toHide
.add(R
.id
.action_rename_file
);
167 toShow
.add(R
.id
.action_rename_file
);
171 if (mFile
== null
|| downloading
|| uploading
) {
172 toHide
.add(R
.id
.action_move
);
175 toShow
.add(R
.id
.action_move
);
179 if (mFile
== null
|| downloading
|| uploading
) {
180 toHide
.add(R
.id
.action_remove_file
);
183 toShow
.add(R
.id
.action_remove_file
);
186 // OPEN WITH (different to preview!)
187 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || downloading
|| uploading
) {
188 toHide
.add(R
.id
.action_open_file_with
);
191 toShow
.add(R
.id
.action_open_file_with
);
196 if (mFile
== null
|| !downloading
) {
197 toHide
.add(R
.id
.action_cancel_download
);
199 toShow
.add(R
.id
.action_cancel_download
);
203 if (mFile
== null
|| !uploading
|| mFile
.isFolder()) {
204 toHide
.add(R
.id
.action_cancel_upload
);
206 toShow
.add(R
.id
.action_cancel_upload
);
209 // SYNC FILE CONTENTS
210 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || downloading
|| uploading
) {
211 toHide
.add(R
.id
.action_sync_file
);
213 toShow
.add(R
.id
.action_sync_file
);
217 // TODO add check on SHARE available on server side?
219 toHide
.add(R
.id
.action_share_file
);
221 toShow
.add(R
.id
.action_share_file
);
225 // TODO add check on SHARE available on server side?
226 if (mFile
== null
|| !mFile
.isShareByLink()) {
227 toHide
.add(R
.id
.action_unshare_file
);
229 toShow
.add(R
.id
.action_unshare_file
);
234 if (mFile
== null
|| mFile
.isFolder()) {
235 toHide
.add(R
.id
.action_see_details
);
237 toShow
.add(R
.id
.action_see_details
);
241 boolean sendAllowed
= (mContext
!= null
&&
242 mContext
.getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on"));
243 if (mFile
== null
|| !sendAllowed
|| mFile
.isFolder() || uploading
|| downloading
) {
244 toHide
.add(R
.id
.action_send_file
);
246 toShow
.add(R
.id
.action_send_file
);