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
.OperationsServiceBinder
;
35 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
38 * Filters out the file actions available in a given {@link Menu} for a given {@link OCFile}
39 * according to the current state of the latest.
41 * @author David A. Velasco
43 public class FileMenuFilter
{
46 private ComponentsGetter mComponentsGetter
;
47 private Account mAccount
;
48 private Context mContext
;
53 * @param targetFile {@link OCFile} target of the action to filter in the {@link Menu}.
54 * @param account ownCloud {@link Account} holding targetFile.
55 * @param cg Accessor to app components, needed to access the
56 * {@link FileUploader} and {@link FileDownloader} services
57 * @param context Android {@link Context}, needed to access build setup resources.
59 public FileMenuFilter(OCFile targetFile
, Account account
, ComponentsGetter cg
, Context context
) {
62 mComponentsGetter
= cg
;
68 * Filters out the file actions available in the passed {@link Menu} taken into account
69 * the state of the {@link OCFile} held by the filter.
71 * @param menu Options or context menu to filter.
73 public void filter(Menu menu
) {
74 List
<Integer
> toShow
= new ArrayList
<Integer
>();
75 List
<Integer
> toHide
= new ArrayList
<Integer
>();
77 filter(toShow
, toHide
);
80 for (int i
: toShow
) {
81 item
= menu
.findItem(i
);
83 item
.setVisible(true
);
84 item
.setEnabled(true
);
88 for (int i
: toHide
) {
89 item
= menu
.findItem(i
);
91 item
.setVisible(false
);
92 item
.setEnabled(false
);
98 * Filters out the file actions available in the passed {@link Menu} taken into account
99 * the state of the {@link OCFile} held by the filter.
101 * Second method needed thanks to ActionBarSherlock.
103 * TODO Get rid of it when ActionBarSherlock is replaced for newer Android Support Library.
105 * @param menu Options or context menu to filter.
107 public void filter(com
.actionbarsherlock
.view
.Menu menu
) {
109 List
<Integer
> toShow
= new ArrayList
<Integer
>();
110 List
<Integer
> toHide
= new ArrayList
<Integer
>();
112 filter(toShow
, toHide
);
114 com
.actionbarsherlock
.view
.MenuItem item
= null
;
115 for (int i
: toShow
) {
116 item
= menu
.findItem(i
);
118 item
.setVisible(true
);
119 item
.setEnabled(true
);
122 for (int i
: toHide
) {
123 item
= menu
.findItem(i
);
125 item
.setVisible(false
);
126 item
.setEnabled(false
);
132 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
134 * Decides what actions must be shown and hidden.
136 * @param toShow List to save the options that must be shown in the menu.
137 * @param toHide List to save the options that must be shown in the menu.
139 private void filter(List
<Integer
> toShow
, List
<Integer
> toHide
) {
140 boolean downloading
= false
;
141 boolean uploading
= false
;
142 if (mComponentsGetter
!= null
&& mFile
!= null
&& mAccount
!= null
) {
143 //downloading = mFile.isDownloading() || mFile.isSynchronizing();
144 FileDownloaderBinder downloaderBinder
= mComponentsGetter
.getFileDownloaderBinder();
145 downloading
= (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
));
146 FileUploaderBinder uploaderBinder
= mComponentsGetter
.getFileUploaderBinder();
147 uploading
= (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, mFile
));
150 /// decision is taken for each possible action on a file in the menu
153 if (mFile
== null
|| mFile
.isDown() || downloading
|| uploading
) {
154 toHide
.add(R
.id
.action_download_file
);
157 toShow
.add(R
.id
.action_download_file
);
161 if (mFile
== null
|| downloading
|| uploading
) {
162 toHide
.add(R
.id
.action_rename_file
);
165 toShow
.add(R
.id
.action_rename_file
);
169 if (mFile
== null
|| downloading
|| uploading
) {
170 toHide
.add(R
.id
.action_move
);
173 toShow
.add(R
.id
.action_move
);
177 if (mFile
== null
|| downloading
|| uploading
) {
178 toHide
.add(R
.id
.action_remove_file
);
181 toShow
.add(R
.id
.action_remove_file
);
184 // OPEN WITH (different to preview!)
185 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || downloading
|| uploading
) {
186 toHide
.add(R
.id
.action_open_file_with
);
189 toShow
.add(R
.id
.action_open_file_with
);
194 if (mFile
== null
|| !downloading
) {
195 toHide
.add(R
.id
.action_cancel_download
);
197 toShow
.add(R
.id
.action_cancel_download
);
201 if (mFile
== null
|| !uploading
|| mFile
.isFolder()) {
202 toHide
.add(R
.id
.action_cancel_upload
);
204 toShow
.add(R
.id
.action_cancel_upload
);
207 // SYNC FILE CONTENTS
208 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || downloading
|| uploading
) {
209 toHide
.add(R
.id
.action_sync_file
);
211 toShow
.add(R
.id
.action_sync_file
);
215 // TODO add check on SHARE available on server side?
217 toHide
.add(R
.id
.action_share_file
);
219 toShow
.add(R
.id
.action_share_file
);
223 // TODO add check on SHARE available on server side?
224 if (mFile
== null
|| !mFile
.isShareByLink()) {
225 toHide
.add(R
.id
.action_unshare_file
);
227 toShow
.add(R
.id
.action_unshare_file
);
232 if (mFile
== null
|| mFile
.isFolder()) {
233 toHide
.add(R
.id
.action_see_details
);
235 toShow
.add(R
.id
.action_see_details
);
239 boolean sendAllowed
= (mContext
!= null
&&
240 mContext
.getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on"));
241 if (mFile
== null
|| !sendAllowed
|| mFile
.isFolder() || uploading
|| downloading
) {
242 toHide
.add(R
.id
.action_send_file
);
244 toShow
.add(R
.id
.action_send_file
);