moved switch menu item to three dots menu
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / FileDetailFragment.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Bartek Przybylski
5 * @author David A. Velasco
6 * Copyright (C) 2011 Bartek Przybylski
7 * Copyright (C) 2015 ownCloud Inc.
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22 package com.owncloud.android.ui.fragment;
23
24 import java.lang.ref.WeakReference;
25
26 import android.accounts.Account;
27 import android.content.Intent;
28 import android.os.Bundle;
29 import android.view.LayoutInflater;
30 import android.view.Menu;
31 import android.view.MenuInflater;
32 import android.view.MenuItem;
33 import android.view.View;
34 import android.view.View.OnClickListener;
35 import android.view.ViewGroup;
36 import android.widget.CheckBox;
37 import android.widget.ImageView;
38 import android.widget.ProgressBar;
39 import android.widget.TextView;
40
41 import com.owncloud.android.R;
42 import com.owncloud.android.datamodel.FileDataStorageManager;
43 import com.owncloud.android.datamodel.OCFile;
44 import com.owncloud.android.files.FileMenuFilter;
45 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
46 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
47 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
48 import com.owncloud.android.lib.common.utils.Log_OC;
49 import com.owncloud.android.services.observer.FileObserverService;
50 import com.owncloud.android.ui.activity.FileActivity;
51 import com.owncloud.android.ui.activity.FileDisplayActivity;
52 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
53 import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
54 import com.owncloud.android.utils.DisplayUtils;
55
56
57 /**
58 * This Fragment is used to display the details about a file.
59 */
60 public class FileDetailFragment extends FileFragment implements OnClickListener {
61
62 private int mLayout;
63 private View mView;
64 private Account mAccount;
65
66 public ProgressListener mProgressListener;
67
68 private static final String TAG = FileDetailFragment.class.getSimpleName();
69 public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";
70 public static final String FTAG_RENAME_FILE = "RENAME_FILE_FRAGMENT";
71
72 private static final String ARG_FILE = "FILE";
73 private static final String ARG_ACCOUNT = "ACCOUNT";
74
75
76 /**
77 * Public factory method to create new FileDetailFragment instances.
78 *
79 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
80 *
81 * @param fileToDetail An {@link OCFile} to show in the fragment
82 * @param account An ownCloud account; needed to start downloads
83 * @return New fragment with arguments set
84 */
85 public static FileDetailFragment newInstance(OCFile fileToDetail, Account account) {
86 FileDetailFragment frag = new FileDetailFragment();
87 Bundle args = new Bundle();
88 args.putParcelable(ARG_FILE, fileToDetail);
89 args.putParcelable(ARG_ACCOUNT, account);
90 frag.setArguments(args);
91 return frag;
92 }
93
94 /**
95 * Creates an empty details fragment.
96 *
97 * It's necessary to keep a public constructor without parameters; the system uses it when tries
98 * to reinstantiate a fragment automatically.
99 */
100 public FileDetailFragment() {
101 super();
102 mAccount = null;
103 mLayout = R.layout.file_details_empty;
104 mProgressListener = null;
105 }
106
107
108 @Override
109 public void onActivityCreated(Bundle savedInstanceState) {
110 super.onCreate(savedInstanceState);
111 setHasOptionsMenu(true);
112 }
113
114
115 @Override
116 public View onCreateView(LayoutInflater inflater, ViewGroup container,
117 Bundle savedInstanceState) {
118
119 setFile((OCFile) getArguments().getParcelable(ARG_FILE));
120 mAccount = getArguments().getParcelable(ARG_ACCOUNT);
121
122 if (savedInstanceState != null) {
123 setFile((OCFile)savedInstanceState.getParcelable(FileActivity.EXTRA_FILE));
124 mAccount = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
125 }
126
127 if(getFile() != null && mAccount != null) {
128 mLayout = R.layout.file_details_fragment;
129 }
130
131 mView = inflater.inflate(mLayout, null);
132
133 if (mLayout == R.layout.file_details_fragment) {
134 mView.findViewById(R.id.fdFavorite).setOnClickListener(this);
135 ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.fdProgressBar);
136 DisplayUtils.colorPreLollipopHorizontalProgressBar(progressBar);
137 mProgressListener = new ProgressListener(progressBar);
138 mView.findViewById(R.id.fdCancelBtn).setOnClickListener(this);
139 }
140
141 updateFileDetails(false, false);
142 return mView;
143 }
144
145 @Override
146 public void onSaveInstanceState(Bundle outState) {
147 super.onSaveInstanceState(outState);
148 outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
149 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
150 }
151
152 @Override
153 public void onStart() {
154 super.onStart();
155 listenForTransferProgress();
156 }
157
158 @Override
159 public void onStop() {
160 leaveTransferProgress();
161 super.onStop();
162 }
163
164
165 @Override
166 public View getView() {
167 return super.getView() == null ? mView : super.getView();
168 }
169
170
171 /**
172 * {@inheritDoc}
173 */
174 @Override
175 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
176 super.onCreateOptionsMenu(menu, inflater);
177 inflater.inflate(R.menu.file_actions_menu, menu);
178 }
179
180
181 /**
182 * {@inheritDoc}
183 */
184 @Override
185 public void onPrepareOptionsMenu (Menu menu) {
186 super.onPrepareOptionsMenu(menu);
187
188 if (mContainerActivity.getStorageManager() != null) {
189 FileMenuFilter mf = new FileMenuFilter(
190 getFile(),
191 mContainerActivity.getStorageManager().getAccount(),
192 mContainerActivity,
193 getActivity()
194 );
195 mf.filter(menu);
196 }
197
198 // additional restriction for this fragment
199 MenuItem item = menu.findItem(R.id.action_see_details);
200 if (item != null) {
201 item.setVisible(false);
202 item.setEnabled(false);
203 }
204
205 // additional restriction for this fragment
206 item = menu.findItem(R.id.action_move);
207 if (item != null) {
208 item.setVisible(false);
209 item.setEnabled(false);
210 }
211 }
212
213
214 /**
215 * {@inheritDoc}
216 */
217 @Override
218 public boolean onOptionsItemSelected(MenuItem item) {
219 switch (item.getItemId()) {
220 case R.id.action_share_file: {
221 mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
222 return true;
223 }
224 case R.id.action_unshare_file: {
225 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
226 return true;
227 }
228 case R.id.action_open_file_with: {
229 mContainerActivity.getFileOperationsHelper().openFile(getFile());
230 return true;
231 }
232 case R.id.action_remove_file: {
233 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
234 dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
235 return true;
236 }
237 case R.id.action_rename_file: {
238 RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
239 dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
240 return true;
241 }
242 case R.id.action_cancel_download:
243 case R.id.action_cancel_upload: {
244 ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
245 return true;
246 }
247 case R.id.action_download_file:
248 case R.id.action_sync_file: {
249 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
250 return true;
251 }
252 case R.id.action_send_file: {
253 // Obtain the file
254 if (!getFile().isDown()) { // Download the file
255 Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
256 ((FileDisplayActivity)mContainerActivity).startDownloadForSending(getFile());
257
258 } else {
259 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
260 }
261 return true;
262 }
263 case R.id.action_favorite_file:{
264 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), true);
265 return true;
266 }
267 case R.id.action_unfavorite_file:{
268 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), false);
269 return true;
270 }
271 default:
272 return false;
273 }
274 }
275
276 @Override
277 public void onClick(View v) {
278 switch (v.getId()) {
279 case R.id.fdFavorite: {
280 CheckBox cb = (CheckBox) getView().findViewById(R.id.fdFavorite);
281 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(),cb.isChecked());
282 break;
283 }
284 case R.id.fdCancelBtn: {
285 ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
286 break;
287 }
288 default:
289 Log_OC.e(TAG, "Incorrect view clicked!");
290 }
291 }
292
293
294 /**
295 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
296 *
297 * @return True when the fragment was created with the empty layout.
298 */
299 public boolean isEmpty() {
300 return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
301 }
302
303
304 /**
305 * Use this method to signal this Activity that it shall update its view.
306 *
307 * @param file : An {@link OCFile}
308 */
309 public void updateFileDetails(OCFile file, Account ocAccount) {
310 setFile(file);
311 mAccount = ocAccount;
312 updateFileDetails(false, false);
313 }
314
315 /**
316 * Updates the view with all relevant details about that file.
317 *
318 * TODO Remove parameter when the transferring state of files is kept in database.
319 *
320 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
321 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
322 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
323 *
324 * @param refresh If 'true', try to refresh the whole file from the database
325 */
326 public void updateFileDetails(boolean transferring, boolean refresh) {
327 if (readyToShow()) {
328 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
329 if (refresh && storageManager != null) {
330 setFile(storageManager.getFileByPath(getFile().getRemotePath()));
331 }
332 OCFile file = getFile();
333
334 // set file details
335 setFilename(file.getFileName());
336 setFiletype(file.getMimetype(), file.getFileName());
337 setFilesize(file.getFileLength());
338
339 setTimeModified(file.getModificationTimestamp());
340
341 CheckBox cb = (CheckBox)getView().findViewById(R.id.fdFavorite);
342 cb.setChecked(file.isFavorite());
343
344 // configure UI for depending upon local state of the file
345 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
346 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
347 if (transferring ||
348 (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) ||
349 (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))
350 ) {
351 setButtonsForTransferring();
352
353 } else if (file.isDown()) {
354
355 setButtonsForDown();
356
357 } else {
358 // TODO load default preview image; when the local file is removed, the preview
359 // remains there
360 setButtonsForRemote();
361 }
362 }
363 getView().invalidate();
364 }
365
366 /**
367 * Checks if the fragment is ready to show details of a OCFile
368 *
369 * @return 'True' when the fragment is ready to show details of a file
370 */
371 private boolean readyToShow() {
372 return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
373 }
374
375
376 /**
377 * Updates the filename in view
378 * @param filename to set
379 */
380 private void setFilename(String filename) {
381 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
382 if (tv != null)
383 tv.setText(filename);
384 }
385
386 /**
387 * Updates the MIME type in view
388 * @param mimetype MIME type to set
389 * @param filename Name of the file, to deduce the icon to use in case the MIME type is not precise enough
390 */
391 private void setFiletype(String mimetype, String filename) {
392 TextView tv = (TextView) getView().findViewById(R.id.fdType);
393 if (tv != null) {
394 String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);
395 tv.setText(printableMimetype);
396 }
397 ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
398 if (iv != null) {
399 iv.setImageResource(DisplayUtils.getFileTypeIconId(mimetype, filename));
400 }
401 }
402
403 /**
404 * Updates the file size in view
405 * @param filesize in bytes to set
406 */
407 private void setFilesize(long filesize) {
408 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
409 if (tv != null)
410 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
411 }
412
413 /**
414 * Updates the time that the file was last modified
415 * @param milliseconds Unix time to set
416 */
417 private void setTimeModified(long milliseconds){
418 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
419 if(tv != null){
420 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
421 }
422 }
423
424 /**
425 * Enables or disables buttons for a file being downloaded
426 */
427 private void setButtonsForTransferring() {
428 if (!isEmpty()) {
429 // let's protect the user from himself ;)
430 getView().findViewById(R.id.fdFavorite).setEnabled(false);
431
432 // show the progress bar for the transfer
433 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
434 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
435 progressText.setVisibility(View.VISIBLE);
436 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
437 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
438 //if (getFile().isDownloading()) {
439 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
440 progressText.setText(R.string.downloader_download_in_progress_ticker);
441 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
442 progressText.setText(R.string.uploader_upload_in_progress_ticker);
443 }
444 }
445 }
446
447 /**
448 * Enables or disables buttons for a file locally available
449 */
450 private void setButtonsForDown() {
451 if (!isEmpty()) {
452 getView().findViewById(R.id.fdFavorite).setEnabled(true);
453
454 // hides the progress bar
455 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
456 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
457 progressText.setVisibility(View.GONE);
458 }
459 }
460
461 /**
462 * Enables or disables buttons for a file not locally available
463 */
464 private void setButtonsForRemote() {
465 if (!isEmpty()) {
466 getView().findViewById(R.id.fdFavorite).setEnabled(true);
467
468 // hides the progress bar
469 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
470 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
471 progressText.setVisibility(View.GONE);
472 }
473 }
474
475
476 public void listenForTransferProgress() {
477 if (mProgressListener != null) {
478 if (mContainerActivity.getFileDownloaderBinder() != null) {
479 mContainerActivity.getFileDownloaderBinder().
480 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
481 }
482 if (mContainerActivity.getFileUploaderBinder() != null) {
483 mContainerActivity.getFileUploaderBinder().
484 addDatatransferProgressListener(mProgressListener, mAccount, getFile());
485 }
486 }
487 }
488
489
490 public void leaveTransferProgress() {
491 if (mProgressListener != null) {
492 if (mContainerActivity.getFileDownloaderBinder() != null) {
493 mContainerActivity.getFileDownloaderBinder().
494 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
495 }
496 if (mContainerActivity.getFileUploaderBinder() != null) {
497 mContainerActivity.getFileUploaderBinder().
498 removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
499 }
500 }
501 }
502
503
504
505 /**
506 * Helper class responsible for updating the progress bar shown for file uploading or
507 * downloading
508 */
509 private class ProgressListener implements OnDatatransferProgressListener {
510 int mLastPercent = 0;
511 WeakReference<ProgressBar> mProgressBar = null;
512
513 ProgressListener(ProgressBar progressBar) {
514 mProgressBar = new WeakReference<ProgressBar>(progressBar);
515 }
516
517 @Override
518 public void onTransferProgress(long progressRate, long totalTransferredSoFar,
519 long totalToTransfer, String filename) {
520 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
521 if (percent != mLastPercent) {
522 ProgressBar pb = mProgressBar.get();
523 if (pb != null) {
524 pb.setProgress(percent);
525 pb.postInvalidate();
526 }
527 }
528 mLastPercent = percent;
529 }
530
531 }
532
533 }