0d138c81476839bfec91f2a14dc8c542232f686e
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
.services
.OperationsService
.OperationsServiceBinder
;
35 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
38 * Filters out the file actions available in a given {@link Menu} for a given {@link OCFile}
39 * according to the current state of the latest.
41 * @author David A. Velasco
43 public class FileMenuFilter
{
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 context Android {@link Context}, needed to access build setup resources.
56 public FileMenuFilter(OCFile targetFile
, Account account
, Context context
) {
64 * Filters out the file actions available in the passed {@link Menu} taken into account
65 * the state of the {@link OCFile} held by the filter.
67 * @param menu Options or context menu to filter.
69 public void filter(Menu menu
) {
70 List
<Integer
> toShow
= new ArrayList
<Integer
>();
71 List
<Integer
> toHide
= new ArrayList
<Integer
>();
73 filter(toShow
, toHide
);
76 for (int i
: toShow
) {
77 item
= menu
.findItem(i
);
79 item
.setVisible(true
);
80 item
.setEnabled(true
);
84 for (int i
: toHide
) {
85 item
= menu
.findItem(i
);
87 item
.setVisible(false
);
88 item
.setEnabled(false
);
94 * Filters out the file actions available in the passed {@link Menu} taken into account
95 * the state of the {@link OCFile} held by the filter.
97 * Second method needed thanks to ActionBarSherlock.
99 * TODO Get rid of it when ActionBarSherlock is replaced for newer Android Support Library.
101 * @param menu Options or context menu to filter.
103 public void filter(com
.actionbarsherlock
.view
.Menu menu
) {
105 List
<Integer
> toShow
= new ArrayList
<Integer
>();
106 List
<Integer
> toHide
= new ArrayList
<Integer
>();
108 filter(toShow
, toHide
);
110 com
.actionbarsherlock
.view
.MenuItem item
= null
;
111 for (int i
: toShow
) {
112 item
= menu
.findItem(i
);
114 item
.setVisible(true
);
115 item
.setEnabled(true
);
118 for (int i
: toHide
) {
119 item
= menu
.findItem(i
);
121 item
.setVisible(false
);
122 item
.setEnabled(false
);
128 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
130 * Decides what actions must be shown and hidden.
132 * @param toShow List to save the options that must be shown in the menu.
133 * @param toHide List to save the options that must be shown in the menu.
135 private void filter(List
<Integer
> toShow
, List
<Integer
> toHide
) {
136 boolean downloading
= false
;
137 boolean uploading
= false
;
138 if (mFile
!= null
&& mAccount
!= null
) {
139 downloading
= mFile
.isDownloading() || mFile
.isSynchronizing();
140 uploading
= mFile
.isUploading();
143 /// decision is taken for each possible action on a file in the menu
146 if (mFile
== null
|| mFile
.isDown() || downloading
|| uploading
) {
147 toHide
.add(R
.id
.action_download_file
);
150 toShow
.add(R
.id
.action_download_file
);
154 if (mFile
== null
|| downloading
|| uploading
) {
155 toHide
.add(R
.id
.action_rename_file
);
158 toShow
.add(R
.id
.action_rename_file
);
162 if (mFile
== null
|| downloading
|| uploading
) {
163 toHide
.add(R
.id
.action_move
);
166 toShow
.add(R
.id
.action_move
);
170 if (mFile
== null
|| downloading
|| uploading
) {
171 toHide
.add(R
.id
.action_remove_file
);
174 toShow
.add(R
.id
.action_remove_file
);
177 // OPEN WITH (different to preview!)
178 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || downloading
|| uploading
) {
179 toHide
.add(R
.id
.action_open_file_with
);
182 toShow
.add(R
.id
.action_open_file_with
);
187 if (mFile
== null
|| !downloading
) {
188 toHide
.add(R
.id
.action_cancel_download
);
190 toShow
.add(R
.id
.action_cancel_download
);
194 if (mFile
== null
|| !uploading
|| mFile
.isFolder()) {
195 toHide
.add(R
.id
.action_cancel_upload
);
197 toShow
.add(R
.id
.action_cancel_upload
);
200 // SYNC FILE CONTENTS
201 if (mFile
== null
|| mFile
.isFolder() || !mFile
.isDown() || downloading
|| uploading
) {
202 toHide
.add(R
.id
.action_sync_file
);
204 toShow
.add(R
.id
.action_sync_file
);
208 // TODO add check on SHARE available on server side?
210 toHide
.add(R
.id
.action_share_file
);
212 toShow
.add(R
.id
.action_share_file
);
216 // TODO add check on SHARE available on server side?
217 if (mFile
== null
|| !mFile
.isShareByLink()) {
218 toHide
.add(R
.id
.action_unshare_file
);
220 toShow
.add(R
.id
.action_unshare_file
);
225 if (mFile
== null
|| mFile
.isFolder()) {
226 toHide
.add(R
.id
.action_see_details
);
228 toShow
.add(R
.id
.action_see_details
);
232 boolean sendAllowed
= (mContext
!= null
&&
233 mContext
.getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on"));
234 if (mFile
== null
|| !sendAllowed
|| mFile
.isFolder() || uploading
|| downloading
) {
235 toHide
.add(R
.id
.action_send_file
);
237 toShow
.add(R
.id
.action_send_file
);