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
))
124 boolean downloading = false;
125 boolean uploading = false;
126 if (mComponentsGetter != null && mFile != null && mAccount != null) {
127 FileDownloaderBinder downloaderBinder = mComponentsGetter.getFileDownloaderBinder();
128 downloading = (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile));
129 OperationsServiceBinder opsBinder = mComponentsGetter.getOperationsServiceBinder();
130 downloading |= (opsBinder != null && opsBinder.isSynchronizing(mAccount, mFile.getRemotePath()));
131 FileUploaderBinder uploaderBinder = mComponentsGetter.getFileUploaderBinder();
132 uploading = (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile));
136 /// decision is taken for each possible action on a file in the menu
139 if (mFile
== null
|| mFile
.isDown() || synchronizing
) {
140 toHide
.add(R
.id
.action_download_file
);
143 toShow
.add(R
.id
.action_download_file
);
147 if (mFile
== null
|| synchronizing
) {
148 toHide
.add(R
.id
.action_rename_file
);
151 toShow
.add(R
.id
.action_rename_file
);
155 if (mFile
== null
|| synchronizing
) {
156 toHide
.add(R
.id
.action_move
);
157 toHide
.add(R
.id
.action_copy
);
159 toShow
.add(R
.id
.action_move
);
160 toShow
.add(R
.id
.action_copy
);
164 if (mFile
== null
|| synchronizing
) {
165 toHide
.add(R
.id
.action_remove_file
);
168 toShow
.add(R
.id
.action_remove_file
);
171 // OPEN WITH (different to preview!)
172 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || synchronizing
) {
173 toHide
.add(R
.id
.action_open_file_with
);
176 toShow
.add(R
.id
.action_open_file_with
);
179 // CANCEL SYNCHRONIZATION
180 if (mFile
== null
|| !synchronizing
) {
181 toHide
.add(R
.id
.action_cancel_sync
);
184 toShow
.add(R
.id
.action_cancel_sync
);
187 // SYNC CONTENTS (BOTH FILE AND FOLDER)
188 if (mFile
== null
|| (!mFile
.isFolder() && !mFile
.isDown()) || synchronizing
) {
189 toHide
.add(R
.id
.action_sync_file
);
192 toShow
.add(R
.id
.action_sync_file
);
196 // TODO add check on SHARE available on server side?
197 boolean shareAllowed
= (mContext
!= null
&&
198 mContext
.getString(R
.string
.share_feature
).equalsIgnoreCase("on"));
199 if (!shareAllowed
|| mFile
== null
) {
200 toHide
.add(R
.id
.action_share_file
);
202 toShow
.add(R
.id
.action_share_file
);
206 // TODO add check on SHARE available on server side?
207 if ( !shareAllowed
|| (mFile
== null
|| !mFile
.isShareByLink())) {
208 toHide
.add(R
.id
.action_unshare_file
);
210 toShow
.add(R
.id
.action_unshare_file
);
214 if (mFile
== null
|| mFile
.isFolder()) {
215 toHide
.add(R
.id
.action_see_details
);
217 toShow
.add(R
.id
.action_see_details
);
221 boolean sendAllowed
= (mContext
!= null
&&
222 mContext
.getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on"));
223 if (mFile
== null
|| !sendAllowed
|| mFile
.isFolder() || synchronizing
) {
224 toHide
.add(R
.id
.action_send_file
);
226 toShow
.add(R
.id
.action_send_file
);
230 if (mFile
== null
|| synchronizing
|| mFile
.isFolder() || mFile
.isFavorite()) {
231 toHide
.add(R
.id
.action_favorite_file
);
233 toShow
.add(R
.id
.action_favorite_file
);
237 if (mFile
== null
|| synchronizing
|| mFile
.isFolder() || !mFile
.isFavorite()) {
238 toHide
.add(R
.id
.action_unfavorite_file
);
240 toShow
.add(R
.id
.action_unfavorite_file
);