1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.files
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
23 import android
.accounts
.Account
;
24 import android
.content
.Context
;
25 import android
.view
.Menu
;
26 import android
.view
.MenuItem
;
28 import com
.owncloud
.android
.R
;
29 import com
.owncloud
.android
.datamodel
.OCFile
;
30 import com
.owncloud
.android
.files
.services
.FileDownloader
;
31 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
32 import com
.owncloud
.android
.files
.services
.FileUploader
;
33 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
34 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
37 * Filters out the file actions available in a given {@link Menu} for a given {@link OCFile}
38 * according to the current state of the latest.
40 * @author David A. Velasco
42 public class FileMenuFilter
{
45 private ComponentsGetter mComponentsGetter
;
46 private Account mAccount
;
47 private Context mContext
;
52 * @param targetFile {@link OCFile} target of the action to filter in the {@link Menu}.
53 * @param account ownCloud {@link Account} holding targetFile.
54 * @param cg Accessor to app components, needed to get access the
55 * {@link FileUploader} and {@link FileDownloader} services.
56 * @param context Android {@link Context}, needed to access build setup resources.
58 public FileMenuFilter(OCFile targetFile
, Account account
, ComponentsGetter cg
, Context context
) {
61 mComponentsGetter
= cg
;
67 * Filters out the file actions available in the passed {@link Menu} taken into account
68 * the state of the {@link OCFile} held by the filter.
70 * @param menu Options or context menu to filter.
72 public void filter(Menu menu
) {
73 List
<Integer
> toShow
= new ArrayList
<Integer
>();
74 List
<Integer
> toHide
= new ArrayList
<Integer
>();
76 filter(toShow
, toHide
);
79 for (int i
: toShow
) {
80 item
= menu
.findItem(i
);
82 item
.setVisible(true
);
83 item
.setEnabled(true
);
87 for (int i
: toHide
) {
88 item
= menu
.findItem(i
);
90 item
.setVisible(false
);
91 item
.setEnabled(false
);
97 * Filters out the file actions available in the passed {@link Menu} taken into account
98 * the state of the {@link OCFile} held by the filter.
100 * Second method needed thanks to ActionBarSherlock.
102 * TODO Get rid of it when ActionBarSherlock is replaced for newer Android Support Library.
104 * @param menu Options or context menu to filter.
106 public void filter(com
.actionbarsherlock
.view
.Menu menu
) {
108 List
<Integer
> toShow
= new ArrayList
<Integer
>();
109 List
<Integer
> toHide
= new ArrayList
<Integer
>();
111 filter(toShow
, toHide
);
113 com
.actionbarsherlock
.view
.MenuItem item
= null
;
114 for (int i
: toShow
) {
115 item
= menu
.findItem(i
);
117 item
.setVisible(true
);
118 item
.setEnabled(true
);
121 for (int i
: toHide
) {
122 item
= menu
.findItem(i
);
124 item
.setVisible(false
);
125 item
.setEnabled(false
);
131 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
133 * Decides what actions must be shown and hidden.
135 * @param toShow List to save the options that must be shown in the menu.
136 * @param toHide List to save the options that must be shown in the menu.
138 private void filter(List
<Integer
> toShow
, List
<Integer
> toHide
) {
139 boolean downloading
= false
;
140 boolean uploading
= false
;
141 if (mComponentsGetter
!= null
&& mFile
!= null
&& mAccount
!= null
) {
142 FileDownloaderBinder downloaderBinder
= mComponentsGetter
.getFileDownloaderBinder();
143 downloading
= downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
);
144 FileUploaderBinder uploaderBinder
= mComponentsGetter
.getFileUploaderBinder();
145 uploading
= uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, mFile
);
148 /// decision is taken for each possible action on a file in the menu
151 if (mFile
== null
|| mFile
.isFolder() || mFile
.isDown() || downloading
|| uploading
) {
152 toHide
.add(R
.id
.action_download_file
);
155 toShow
.add(R
.id
.action_download_file
);
159 if (mFile
== null
|| downloading
|| uploading
) {
160 toHide
.add(R
.id
.action_rename_file
);
163 toShow
.add(R
.id
.action_rename_file
);
167 if (mFile
== null
|| downloading
|| uploading
) {
168 toHide
.add(R
.id
.action_remove_file
);
171 toShow
.add(R
.id
.action_remove_file
);
174 // OPEN WITH (different to preview!)
175 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || downloading
|| uploading
) {
176 toHide
.add(R
.id
.action_open_file_with
);
179 toShow
.add(R
.id
.action_open_file_with
);
184 if (mFile
== null
|| !downloading
|| mFile
.isFolder()) {
185 toHide
.add(R
.id
.action_cancel_download
);
187 toShow
.add(R
.id
.action_cancel_download
);
191 if (mFile
== null
|| !uploading
|| mFile
.isFolder()) {
192 toHide
.add(R
.id
.action_cancel_upload
);
194 toShow
.add(R
.id
.action_cancel_upload
);
197 // SYNC FILE CONTENTS
198 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || downloading
|| uploading
) {
199 toHide
.add(R
.id
.action_sync_file
);
201 toShow
.add(R
.id
.action_sync_file
);
205 // TODO add check on SHARE available on server side?
207 toHide
.add(R
.id
.action_share_file
);
209 toShow
.add(R
.id
.action_share_file
);
213 // TODO add check on SHARE available on server side?
214 if (mFile
== null
|| !mFile
.isShareByLink()) {
215 toHide
.add(R
.id
.action_unshare_file
);
217 toShow
.add(R
.id
.action_unshare_file
);
222 if (mFile
== null
|| mFile
.isFolder()) {
223 toHide
.add(R
.id
.action_see_details
);
225 toShow
.add(R
.id
.action_see_details
);
229 boolean sendAllowed
= (mContext
!= null
&&
230 mContext
.getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on"));
231 if (mFile
== null
|| !sendAllowed
|| mFile
.isFolder() || uploading
|| downloading
) {
232 toHide
.add(R
.id
.action_send_file
);
234 toShow
.add(R
.id
.action_send_file
);