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
);
101 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
103 * Decides what actions must be shown and hidden.
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.
108 private void filter(List
<Integer
> toShow
, List
<Integer
> toHide
) {
109 boolean downloading
= false
;
110 boolean uploading
= false
;
111 if (mComponentsGetter
!= null
&& mFile
!= null
&& mAccount
!= null
) {
112 FileDownloaderBinder downloaderBinder
= mComponentsGetter
.getFileDownloaderBinder();
113 downloading
= (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
));
114 OperationsServiceBinder opsBinder
= mComponentsGetter
.getOperationsServiceBinder();
115 downloading
|= (opsBinder
!= null
&& opsBinder
.isSynchronizing(mAccount
, mFile
.getRemotePath()));
116 FileUploaderBinder uploaderBinder
= mComponentsGetter
.getFileUploaderBinder();
117 uploading
= (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, mFile
));
120 /// decision is taken for each possible action on a file in the menu
123 if (mFile
== null
|| mFile
.isDown() || downloading
|| uploading
) {
124 toHide
.add(R
.id
.action_download_file
);
127 toShow
.add(R
.id
.action_download_file
);
131 if (mFile
== null
|| downloading
|| uploading
) {
132 toHide
.add(R
.id
.action_rename_file
);
135 toShow
.add(R
.id
.action_rename_file
);
139 if (mFile
== null
|| downloading
|| uploading
) {
140 toHide
.add(R
.id
.action_move
);
141 toHide
.add(R
.id
.action_copy
);
143 toShow
.add(R
.id
.action_move
);
144 toShow
.add(R
.id
.action_copy
);
148 if (mFile
== null
|| downloading
|| uploading
) {
149 toHide
.add(R
.id
.action_remove_file
);
152 toShow
.add(R
.id
.action_remove_file
);
155 // OPEN WITH (different to preview!)
156 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || downloading
|| uploading
) {
157 toHide
.add(R
.id
.action_open_file_with
);
160 toShow
.add(R
.id
.action_open_file_with
);
165 if (mFile
== null
|| !downloading
) {
166 toHide
.add(R
.id
.action_cancel_download
);
168 toShow
.add(R
.id
.action_cancel_download
);
172 if (mFile
== null
|| !uploading
|| mFile
.isFolder()) {
173 toHide
.add(R
.id
.action_cancel_upload
);
175 toShow
.add(R
.id
.action_cancel_upload
);
178 // SYNC FILE CONTENTS
179 if (mFile
== null
|| (!mFile
.isFolder() && !mFile
.isDown()) || downloading
|| uploading
) {
180 toHide
.add(R
.id
.action_sync_file
);
182 toShow
.add(R
.id
.action_sync_file
);
186 // TODO add check on SHARE available on server side?
187 boolean shareAllowed
= (mContext
!= null
&&
188 mContext
.getString(R
.string
.share_feature
).equalsIgnoreCase("on"));
189 if (!shareAllowed
|| mFile
== null
) {
190 toHide
.add(R
.id
.action_share_file
);
192 toShow
.add(R
.id
.action_share_file
);
196 // TODO add check on SHARE available on server side?
197 if ( !shareAllowed
|| (mFile
== null
|| !mFile
.isShareByLink())) {
198 toHide
.add(R
.id
.action_unshare_file
);
200 toShow
.add(R
.id
.action_unshare_file
);
204 if (mFile
== null
|| mFile
.isFolder()) {
205 toHide
.add(R
.id
.action_see_details
);
207 toShow
.add(R
.id
.action_see_details
);
211 boolean sendAllowed
= (mContext
!= null
&&
212 mContext
.getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on"));
213 if (mFile
== null
|| !sendAllowed
|| mFile
.isFolder() || uploading
|| downloading
) {
214 toHide
.add(R
.id
.action_send_file
);
216 toShow
.add(R
.id
.action_send_file
);
220 if (mFile
== null
|| downloading
|| uploading
|| mFile
.isFolder() || mFile
.isFavorite()) {
221 toHide
.add(R
.id
.action_favorite_file
);
223 toShow
.add(R
.id
.action_favorite_file
);
227 if (mFile
== null
|| downloading
|| uploading
|| mFile
.isFolder() || !mFile
.isFavorite()) {
228 toHide
.add(R
.id
.action_unfavorite_file
);
230 toShow
.add(R
.id
.action_unfavorite_file
);