cb9e478bf35ac49381f320580d229e51824e2396
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / FileMenuFilter.java
1 package com.owncloud.android.files;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.accounts.Account;
7 import android.content.Context;
8 import android.view.Menu;
9 import android.view.MenuItem;
10
11 import com.actionbarsherlock.app.SherlockFragment;
12 import com.owncloud.android.R;
13 import com.owncloud.android.datamodel.OCFile;
14 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
15 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
16 import com.owncloud.android.ui.activity.ComponentsGetter;
17 import com.owncloud.android.ui.fragment.FileDetailFragment;
18 import com.owncloud.android.ui.fragment.OCFileListFragment;
19 import com.owncloud.android.ui.preview.PreviewImageFragment;
20 import com.owncloud.android.ui.preview.PreviewMediaFragment;
21
22 public class FileMenuFilter {
23
24 private OCFile mFile;
25 private ComponentsGetter mComponentsGetter;
26 private Account mAccount;
27 private Context mContext;
28 private SherlockFragment mFragment;
29
30 public void setFile(OCFile targetFile) {
31 mFile = targetFile;
32 }
33
34 public void setAccount(Account account) {
35 mAccount = account;
36 }
37
38 public void setComponentGetter(ComponentsGetter cg) {
39 mComponentsGetter = cg;
40 }
41
42 public void setContext(Context context) {
43 mContext = context;
44 }
45
46 public void setFragment(SherlockFragment fragment) {
47 mFragment = fragment;
48 }
49
50 public void filter(Menu menu) {
51 List<Integer> toShow = new ArrayList<Integer>();
52 List<Integer> toDisable = new ArrayList<Integer>();
53 List<Integer> toHide = new ArrayList<Integer>();
54
55 filter(toShow, toDisable, toHide);
56
57 MenuItem item = null;
58 for (int i : toShow) {
59 item = menu.findItem(i);
60 if (item != null) {
61 item.setVisible(true);
62 item.setEnabled(true);
63 }
64 }
65
66 for (int i : toDisable) {
67 item = menu.findItem(i);
68 if (item != null) {
69 item.setVisible(true);
70 item.setEnabled(false);
71 }
72 }
73
74 for (int i : toHide) {
75 item = menu.findItem(i);
76 if (item != null) {
77 item.setVisible(false);
78 item.setEnabled(false);
79 }
80 }
81 }
82
83 /**
84 * ActionBarSherlock...
85 *
86 */
87 public void filter(com.actionbarsherlock.view.Menu menu) {
88
89 List<Integer> toShow = new ArrayList<Integer>();
90 List<Integer> toDisable = new ArrayList<Integer>();
91 List<Integer> toHide = new ArrayList<Integer>();
92
93 filter(toShow, toDisable, toHide);
94
95 com.actionbarsherlock.view.MenuItem item = null;
96 for (int i : toShow) {
97 item = menu.findItem(i);
98 if (item != null) {
99 item.setVisible(true);
100 item.setEnabled(true);
101 }
102 }
103 for (int i : toDisable) {
104 item = menu.findItem(i);
105 if (item != null) {
106 item.setVisible(true);
107 item.setEnabled(false);
108 }
109 }
110 for (int i : toHide) {
111 item = menu.findItem(i);
112 if (item != null) {
113 item.setVisible(false);
114 item.setEnabled(false);
115 }
116 }
117 }
118
119 private void filter(List<Integer> toShow, List<Integer> toDisable, List <Integer> toHide) {
120 boolean downloading = false;
121 boolean uploading = false;
122 if (mComponentsGetter != null && mFile != null && mAccount != null) {
123 FileDownloaderBinder downloaderBinder = mComponentsGetter.getFileDownloaderBinder();
124 downloading = downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile);
125 FileUploaderBinder uploaderBinder = mComponentsGetter.getFileUploaderBinder();
126 uploading = uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile);
127 }
128
129 // R.id.action_download_file
130 if (mFile == null || mFile.isFolder() || mFile.isDown() || downloading || uploading ||
131 (mFragment != null && (
132 mFragment instanceof PreviewImageFragment ||
133 mFragment instanceof PreviewMediaFragment
134 )
135 )
136 ) {
137 toHide.add(R.id.action_download_file);
138
139 } else {
140 toShow.add(R.id.action_download_file);
141 }
142
143 // R.id.action_rename_file
144 if ((downloading || uploading) &&
145 (mFragment != null && mFragment instanceof OCFileListFragment)
146 ) {
147 toDisable.add(R.id.action_rename_file);
148
149 } else if (mFile == null || downloading || uploading ||
150 (mFragment != null && (
151 mFragment instanceof PreviewImageFragment ||
152 mFragment instanceof PreviewMediaFragment
153 )
154 )
155 ) {
156 toHide.add(R.id.action_rename_file);
157
158 } else {
159 toShow.add(R.id.action_rename_file);
160 }
161
162 // R.id.action_remove_file
163 if ((downloading || uploading) &&
164 (mFragment != null && mFragment instanceof OCFileListFragment)
165 ) {
166 toDisable.add(R.id.action_remove_file);
167
168 } else if (mFile == null || downloading || uploading) {
169 toHide.add(R.id.action_remove_file);
170
171 } else {
172 toShow.add(R.id.action_remove_file);
173 }
174
175 // R.id.action_open_file_with
176 if (mFile == null || mFile.isFolder() || !mFile.isDown() || downloading || uploading ||
177 (mFragment != null && mFragment instanceof OCFileListFragment)) {
178 toHide.add(R.id.action_open_file_with);
179
180 } else {
181 toShow.add(R.id.action_open_file_with);
182 }
183
184
185 // R.id.action_cancel_download
186 if (mFile == null || !downloading || mFile.isFolder() ||
187 (mFragment != null && (
188 (mFragment instanceof PreviewImageFragment) ||
189 (mFragment instanceof PreviewMediaFragment)
190 )
191 )
192 ) {
193 toHide.add(R.id.action_cancel_download);
194 } else {
195 toShow.add(R.id.action_cancel_download);
196 }
197
198 // R.id.action_cancel_upload
199 if (mFile == null || !uploading || mFile.isFolder() ||
200 (mFragment != null && (
201 (mFragment instanceof PreviewImageFragment) ||
202 (mFragment instanceof PreviewMediaFragment)
203 )
204 )
205 ) {
206 toHide.add(R.id.action_cancel_upload);
207 } else {
208 toShow.add(R.id.action_cancel_upload);
209 }
210
211 // R.id.action_sync_file
212 if (mFile == null || mFile.isFolder() || !mFile.isDown() || downloading || uploading ||
213 (mFragment != null && mFragment instanceof PreviewMediaFragment)
214 ) {
215 toHide.add(R.id.action_sync_file);
216 } else {
217 toShow.add(R.id.action_sync_file);
218 }
219
220 // R.id.action_share_file // TODO add check on SHARE available on server side?
221 if (mFile == null) {
222 toHide.add(R.id.action_share_file);
223 } else {
224 toShow.add(R.id.action_share_file);
225 }
226
227 // R.id.action_unshare_file // TODO add check on SHARE available on server side?
228 if (mFile == null || !mFile.isShareByLink()) {
229 toHide.add(R.id.action_unshare_file);
230 } else {
231 toShow.add(R.id.action_unshare_file);
232 }
233
234
235 // R.id.action_see_details
236 if (mFile == null || mFile.isFolder() || (mFragment != null && mFragment instanceof FileDetailFragment)) {
237 // TODO check dual pane when FileDetailFragment is shown
238 toHide.add(R.id.action_see_details);
239 } else {
240 toShow.add(R.id.action_see_details);
241 }
242
243 // R.id.action_send_file
244 boolean sendEnabled = (mContext != null &&
245 mContext.getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on"));
246 if (mFile != null && sendEnabled && !mFile.isFolder()) {
247 toShow.add(R.id.action_send_file);
248 } else {
249 toHide.add(R.id.action_send_file);
250 }
251
252 }
253
254 }