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 synchronizing
= false
;
110 if (mComponentsGetter
!= null
&& mFile
!= null
&& mAccount
!= null
) {
111 OperationsServiceBinder opsBinder
= mComponentsGetter
.getOperationsServiceBinder();
112 FileUploaderBinder uploaderBinder
= mComponentsGetter
.getFileUploaderBinder();
113 FileDownloaderBinder downloaderBinder
= mComponentsGetter
.getFileDownloaderBinder();
115 // comparing local and remote
116 (opsBinder
!= null
&& opsBinder
.isSynchronizing(mAccount
, mFile
.getRemotePath())) ||
118 (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
)) ||
120 (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, mFile
))
125 boolean downloading = false;
126 boolean uploading = false;
127 if (mComponentsGetter != null && mFile != null && mAccount != null) {
128 FileDownloaderBinder downloaderBinder = mComponentsGetter.getFileDownloaderBinder();
129 downloading = (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile));
130 OperationsServiceBinder opsBinder = mComponentsGetter.getOperationsServiceBinder();
131 downloading |= (opsBinder != null && opsBinder.isSynchronizing(mAccount, mFile.getRemotePath()));
132 FileUploaderBinder uploaderBinder = mComponentsGetter.getFileUploaderBinder();
133 uploading = (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile));
137 /// decision is taken for each possible action on a file in the menu
140 if (mFile
== null
|| mFile
.isDown() || synchronizing
) {
141 toHide
.add(R
.id
.action_download_file
);
144 toShow
.add(R
.id
.action_download_file
);
148 if (mFile
== null
|| synchronizing
) {
149 toHide
.add(R
.id
.action_rename_file
);
152 toShow
.add(R
.id
.action_rename_file
);
156 if (mFile
== null
|| synchronizing
) {
157 toHide
.add(R
.id
.action_move
);
158 toHide
.add(R
.id
.action_copy
);
160 toShow
.add(R
.id
.action_move
);
161 toShow
.add(R
.id
.action_copy
);
165 if (mFile
== null
|| synchronizing
) {
166 toHide
.add(R
.id
.action_remove_file
);
169 toShow
.add(R
.id
.action_remove_file
);
172 // OPEN WITH (different to preview!)
173 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || synchronizing
) {
174 toHide
.add(R
.id
.action_open_file_with
);
177 toShow
.add(R
.id
.action_open_file_with
);
180 // CANCEL SYNCHRONIZATION
181 if (mFile
== null
|| !synchronizing
) {
182 toHide
.add(R
.id
.action_cancel_sync
);
185 toShow
.add(R
.id
.action_cancel_sync
);
188 // SYNC CONTENTS (BOTH FILE AND FOLDER)
189 if (mFile
== null
|| (!mFile
.isFolder() && !mFile
.isDown()) || synchronizing
) {
190 toHide
.add(R
.id
.action_sync_file
);
193 toShow
.add(R
.id
.action_sync_file
);
197 // TODO add check on SHARE available on server side?
198 boolean shareAllowed
= (mContext
!= null
&&
199 mContext
.getString(R
.string
.share_feature
).equalsIgnoreCase("on"));
200 if (!shareAllowed
|| mFile
== null
) {
201 toHide
.add(R
.id
.action_share_file
);
203 toShow
.add(R
.id
.action_share_file
);
207 // TODO add check on SHARE available on server side?
208 if ( !shareAllowed
|| (mFile
== null
|| !mFile
.isShareByLink())) {
209 toHide
.add(R
.id
.action_unshare_file
);
211 toShow
.add(R
.id
.action_unshare_file
);
215 if (mFile
== null
|| mFile
.isFolder()) {
216 toHide
.add(R
.id
.action_see_details
);
218 toShow
.add(R
.id
.action_see_details
);
222 boolean sendAllowed
= (mContext
!= null
&&
223 mContext
.getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on"));
224 if (mFile
== null
|| !sendAllowed
|| mFile
.isFolder() || synchronizing
) {
225 toHide
.add(R
.id
.action_send_file
);
227 toShow
.add(R
.id
.action_send_file
);
231 if (mFile
== null
|| synchronizing
|| mFile
.isFolder() || mFile
.isFavorite()) {
232 toHide
.add(R
.id
.action_favorite_file
);
234 toShow
.add(R
.id
.action_favorite_file
);
238 if (mFile
== null
|| synchronizing
|| mFile
.isFolder() || !mFile
.isFavorite()) {
239 toHide
.add(R
.id
.action_unfavorite_file
);
241 toShow
.add(R
.id
.action_unfavorite_file
);