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
.OperationsServiceBinder
;
38 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
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.
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
);
100 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
102 * Decides what actions must be shown and hidden.
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.
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();
114 // comparing local and remote
115 (opsBinder
!= null
&& opsBinder
.isSynchronizing(mAccount
, mFile
.getRemotePath())) ||
117 (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
)) ||
119 (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, mFile
))
123 /// decision is taken for each possible action on a file in the menu
126 if (mFile
== null
|| mFile
.isDown() || mFile
.isFolder() || synchronizing
) {
127 toHide
.add(R
.id
.action_download_file
);
130 toShow
.add(R
.id
.action_download_file
);
134 if (mFile
== null
|| synchronizing
) {
135 toHide
.add(R
.id
.action_rename_file
);
138 toShow
.add(R
.id
.action_rename_file
);
142 if (mFile
== null
|| synchronizing
) {
143 toHide
.add(R
.id
.action_move
);
144 toHide
.add(R
.id
.action_copy
);
146 toShow
.add(R
.id
.action_move
);
147 toShow
.add(R
.id
.action_copy
);
151 if (mFile
== null
|| synchronizing
) {
152 toHide
.add(R
.id
.action_remove_file
);
155 toShow
.add(R
.id
.action_remove_file
);
158 // OPEN WITH (different to preview!)
159 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || synchronizing
) {
160 toHide
.add(R
.id
.action_open_file_with
);
163 toShow
.add(R
.id
.action_open_file_with
);
166 // CANCEL SYNCHRONIZATION
167 if (mFile
== null
|| !synchronizing
) {
168 toHide
.add(R
.id
.action_cancel_sync
);
171 toShow
.add(R
.id
.action_cancel_sync
);
174 // SYNC CONTENTS (BOTH FILE AND FOLDER)
175 if (mFile
== null
|| (!mFile
.isFolder() && !mFile
.isDown()) || synchronizing
) {
176 toHide
.add(R
.id
.action_sync_file
);
179 toShow
.add(R
.id
.action_sync_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
);
189 toShow
.add(R
.id
.action_share_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
);
197 toShow
.add(R
.id
.action_unshare_file
);
200 // SHARE FILE, with Users
201 if (!shareAllowed
|| mFile
== null
) {
202 toHide
.add(R
.id
.action_share_with_users
);
204 toShow
.add(R
.id
.action_share_with_users
);
207 // UNSHARE FILE, with Users
208 // TODO add check on SHARE available on server side?
209 if ( !shareAllowed
|| (mFile
== null
|| !mFile
.isShareByLink())) {
210 toHide
.add(R
.id
.action_unshare_with_users
);
212 toShow
.add(R
.id
.action_unshare_with_users
);
216 if (mFile
== null
|| mFile
.isFolder()) {
217 toHide
.add(R
.id
.action_see_details
);
219 toShow
.add(R
.id
.action_see_details
);
223 boolean sendAllowed
= (mContext
!= null
&&
224 mContext
.getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on"));
225 if (mFile
== null
|| !sendAllowed
|| mFile
.isFolder() || synchronizing
) {
226 toHide
.add(R
.id
.action_send_file
);
228 toShow
.add(R
.id
.action_send_file
);
232 if (mFile
== null
|| synchronizing
|| mFile
.isFolder() || mFile
.isFavorite()) {
233 toHide
.add(R
.id
.action_favorite_file
);
235 toShow
.add(R
.id
.action_favorite_file
);
239 if (mFile
== null
|| synchronizing
|| mFile
.isFolder() || !mFile
.isFavorite()) {
240 toHide
.add(R
.id
.action_unfavorite_file
);
242 toShow
.add(R
.id
.action_unfavorite_file
);