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 synchronizing
= false
;
111 if (mComponentsGetter
!= null
&& mFile
!= null
&& mAccount
!= null
) {
112 OperationsServiceBinder opsBinder
= mComponentsGetter
.getOperationsServiceBinder();
113 FileUploaderBinder uploaderBinder
= mComponentsGetter
.getFileUploaderBinder();
114 FileDownloaderBinder downloaderBinder
= mComponentsGetter
.getFileDownloaderBinder();
116 // comparing local and remote
117 (opsBinder
!= null
&& opsBinder
.isSynchronizing(mAccount
, mFile
.getRemotePath())) ||
119 (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
)) ||
121 (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, mFile
))
125 /// decision is taken for each possible action on a file in the menu
128 if (mFile
== null
|| mFile
.isDown() || mFile
.isFolder() || synchronizing
) {
129 toHide
.add(R
.id
.action_download_file
);
132 toShow
.add(R
.id
.action_download_file
);
136 if (mFile
== null
|| synchronizing
) {
137 toHide
.add(R
.id
.action_rename_file
);
140 toShow
.add(R
.id
.action_rename_file
);
144 if (mFile
== null
|| synchronizing
) {
145 toHide
.add(R
.id
.action_move
);
146 toHide
.add(R
.id
.action_copy
);
148 toShow
.add(R
.id
.action_move
);
149 toShow
.add(R
.id
.action_copy
);
153 if (mFile
== null
|| synchronizing
) {
154 toHide
.add(R
.id
.action_remove_file
);
157 toShow
.add(R
.id
.action_remove_file
);
160 // OPEN WITH (different to preview!)
161 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || synchronizing
) {
162 toHide
.add(R
.id
.action_open_file_with
);
165 toShow
.add(R
.id
.action_open_file_with
);
168 // CANCEL SYNCHRONIZATION
169 if (mFile
== null
|| !synchronizing
) {
170 toHide
.add(R
.id
.action_cancel_sync
);
173 toShow
.add(R
.id
.action_cancel_sync
);
176 // SYNC CONTENTS (BOTH FILE AND FOLDER)
177 if (mFile
== null
|| (!mFile
.isFolder() && !mFile
.isDown()) || synchronizing
) {
178 toHide
.add(R
.id
.action_sync_file
);
181 toShow
.add(R
.id
.action_sync_file
);
185 // TODO add check on SHARE available on server side?
186 boolean shareAllowed
= (mContext
!= null
&&
187 mContext
.getString(R
.string
.share_feature
).equalsIgnoreCase("on"));
188 if (!shareAllowed
|| mFile
== null
) {
189 toHide
.add(R
.id
.action_share_file
);
191 toShow
.add(R
.id
.action_share_file
);
195 // TODO add check on SHARE available on server side?
196 if ( !shareAllowed
|| (mFile
== null
|| !mFile
.isSharedViaLink())) {
197 toHide
.add(R
.id
.action_unshare_file
);
199 toShow
.add(R
.id
.action_unshare_file
);
202 // SHARE FILE, with Users
203 if (!shareAllowed
|| mFile
== null
) {
204 toHide
.add(R
.id
.action_share_with_users
);
206 toShow
.add(R
.id
.action_share_with_users
);
211 if (mFile
== null
|| mFile
.isFolder()) {
212 toHide
.add(R
.id
.action_see_details
);
214 toShow
.add(R
.id
.action_see_details
);
218 boolean sendAllowed
= (mContext
!= null
&&
219 mContext
.getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on"));
220 if (mFile
== null
|| !sendAllowed
|| mFile
.isFolder() || synchronizing
) {
221 toHide
.add(R
.id
.action_send_file
);
223 toShow
.add(R
.id
.action_send_file
);
227 if (mFile
== null
|| synchronizing
|| mFile
.isFolder() || mFile
.isFavorite()) {
228 toHide
.add(R
.id
.action_favorite_file
);
230 toShow
.add(R
.id
.action_favorite_file
);
234 if (mFile
== null
|| synchronizing
|| mFile
.isFolder() || !mFile
.isFavorite()) {
235 toHide
.add(R
.id
.action_unfavorite_file
);
237 toShow
.add(R
.id
.action_unfavorite_file
);