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
.authentication
.AccountUtils
;
33 import com
.owncloud
.android
.datamodel
.OCFile
;
34 import com
.owncloud
.android
.files
.services
.FileDownloader
;
35 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
36 import com
.owncloud
.android
.files
.services
.FileUploader
;
37 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
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
,
65 mComponentsGetter
= cg
;
71 * Filters out the file actions available in the passed {@link Menu} taken into account
72 * the state of the {@link OCFile} held by the filter.
74 * @param menu Options or context menu to filter.
76 public void filter(Menu menu
) {
77 List
<Integer
> toShow
= new ArrayList
<Integer
>();
78 List
<Integer
> toHide
= new ArrayList
<Integer
>();
80 filter(toShow
, toHide
);
83 for (int i
: toShow
) {
84 item
= menu
.findItem(i
);
86 item
.setVisible(true
);
87 item
.setEnabled(true
);
91 for (int i
: toHide
) {
92 item
= menu
.findItem(i
);
94 item
.setVisible(false
);
95 item
.setEnabled(false
);
102 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
104 * Decides what actions must be shown and hidden.
106 * @param toShow List to save the options that must be shown in the menu.
107 * @param toHide List to save the options that must be shown in the menu.
109 private void filter(List
<Integer
> toShow
, List
<Integer
> toHide
) {
110 boolean shareWithUsersEnable
= false
;
111 boolean synchronizing
= false
;
112 if (mComponentsGetter
!= null
&& mFile
!= null
&& mAccount
!= null
) {
113 OperationsServiceBinder opsBinder
= mComponentsGetter
.getOperationsServiceBinder();
114 FileUploaderBinder uploaderBinder
= mComponentsGetter
.getFileUploaderBinder();
115 FileDownloaderBinder downloaderBinder
= mComponentsGetter
.getFileDownloaderBinder();
117 // comparing local and remote
118 (opsBinder
!= null
&& opsBinder
.isSynchronizing(mAccount
, mFile
.getRemotePath())) ||
120 (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
)) ||
122 (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, mFile
))
124 shareWithUsersEnable
= AccountUtils
.hasSearchUsersSupport(mAccount
);
127 /// decision is taken for each possible action on a file in the menu
130 if (mFile
== null
|| mFile
.isDown() || mFile
.isFolder() || synchronizing
) {
131 toHide
.add(R
.id
.action_download_file
);
134 toShow
.add(R
.id
.action_download_file
);
138 if (mFile
== null
|| synchronizing
) {
139 toHide
.add(R
.id
.action_rename_file
);
142 toShow
.add(R
.id
.action_rename_file
);
146 if (mFile
== null
|| synchronizing
) {
147 toHide
.add(R
.id
.action_move
);
148 toHide
.add(R
.id
.action_copy
);
150 toShow
.add(R
.id
.action_move
);
151 toShow
.add(R
.id
.action_copy
);
155 if (mFile
== null
|| synchronizing
) {
156 toHide
.add(R
.id
.action_remove_file
);
159 toShow
.add(R
.id
.action_remove_file
);
162 // OPEN WITH (different to preview!)
163 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || synchronizing
) {
164 toHide
.add(R
.id
.action_open_file_with
);
167 toShow
.add(R
.id
.action_open_file_with
);
170 // CANCEL SYNCHRONIZATION
171 if (mFile
== null
|| !synchronizing
) {
172 toHide
.add(R
.id
.action_cancel_sync
);
175 toShow
.add(R
.id
.action_cancel_sync
);
178 // SYNC CONTENTS (BOTH FILE AND FOLDER)
179 if (mFile
== null
|| (!mFile
.isFolder() && !mFile
.isDown()) || synchronizing
) {
180 toHide
.add(R
.id
.action_sync_file
);
183 toShow
.add(R
.id
.action_sync_file
);
187 // TODO add check on SHARE available on server side?
188 boolean shareAllowed
= (mContext
!= null
&&
189 mContext
.getString(R
.string
.share_feature
).equalsIgnoreCase("on"));
190 if (!shareAllowed
|| mFile
== null
) {
191 toHide
.add(R
.id
.action_share_file
);
193 toShow
.add(R
.id
.action_share_file
);
197 // TODO add check on SHARE available on server side?
198 if ( !shareAllowed
|| (mFile
== null
|| !mFile
.isSharedViaLink())) {
199 toHide
.add(R
.id
.action_unshare_file
);
201 toShow
.add(R
.id
.action_unshare_file
);
204 // SHARE FILE, with Users
205 if (!shareAllowed
|| !shareWithUsersEnable
|| mFile
== null
) {
206 toHide
.add(R
.id
.action_share_with_users
);
208 toShow
.add(R
.id
.action_share_with_users
);
213 if (mFile
== null
|| mFile
.isFolder()) {
214 toHide
.add(R
.id
.action_see_details
);
216 toShow
.add(R
.id
.action_see_details
);
220 boolean sendAllowed
= (mContext
!= null
&&
221 mContext
.getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on"));
222 if (mFile
== null
|| !sendAllowed
|| mFile
.isFolder() || synchronizing
) {
223 toHide
.add(R
.id
.action_send_file
);
225 toShow
.add(R
.id
.action_send_file
);
229 if (mFile
== null
|| synchronizing
|| mFile
.isFolder() || mFile
.isFavorite()) {
230 toHide
.add(R
.id
.action_favorite_file
);
232 toShow
.add(R
.id
.action_favorite_file
);
236 if (mFile
== null
|| synchronizing
|| mFile
.isFolder() || !mFile
.isFavorite()) {
237 toHide
.add(R
.id
.action_unfavorite_file
);
239 toShow
.add(R
.id
.action_unfavorite_file
);