8d6ad641a4d3d79bf381fe371f88e60484536fe5
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / FileDetailFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2014 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18 package com.owncloud.android.ui.fragment;
19
20 import java.io.File;
21 import java.lang.ref.WeakReference;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import android.accounts.Account;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.IntentFilter;
30 import android.os.Bundle;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.view.View.OnClickListener;
34 import android.view.ViewGroup;
35 import android.widget.CheckBox;
36 import android.widget.ImageView;
37 import android.widget.ProgressBar;
38 import android.widget.TextView;
39 import android.widget.Toast;
40
41 import com.actionbarsherlock.view.Menu;
42 import com.actionbarsherlock.view.MenuInflater;
43 import com.actionbarsherlock.view.MenuItem;
44 import com.owncloud.android.R;
45 import com.owncloud.android.datamodel.FileDataStorageManager;
46 import com.owncloud.android.datamodel.OCFile;
47 import com.owncloud.android.files.services.FileObserverService;
48 import com.owncloud.android.files.services.FileUploader;
49 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
50 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
51 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
52 import com.owncloud.android.ui.activity.FileActivity;
53 import com.owncloud.android.ui.activity.FileDisplayActivity;
54 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
55 import com.owncloud.android.ui.dialog.EditNameDialog;
56 import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
57 import com.owncloud.android.ui.preview.PreviewImageFragment;
58 import com.owncloud.android.utils.DisplayUtils;
59 import com.owncloud.android.utils.Log_OC;
60
61
62 /**
63 * This Fragment is used to display the details about a file.
64 *
65 * @author Bartek Przybylski
66 * @author David A. Velasco
67 */
68 public class FileDetailFragment extends FileFragment implements
69 OnClickListener,
70 ConfirmationDialogFragment.ConfirmationDialogFragmentListener, EditNameDialogListener {
71
72 private FileFragment.ContainerActivity mContainerActivity;
73
74 private int mLayout;
75 private View mView;
76 private Account mAccount;
77
78 private UploadFinishReceiver mUploadFinishReceiver;
79 public ProgressListener mProgressListener;
80
81 private static final String TAG = FileDetailFragment.class.getSimpleName();
82 public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";
83
84
85 /**
86 * Creates an empty details fragment.
87 *
88 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
89 */
90 public FileDetailFragment() {
91 super();
92 mAccount = null;
93 mLayout = R.layout.file_details_empty;
94 mProgressListener = null;
95 }
96
97 /**
98 * Creates a details fragment.
99 *
100 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
101 *
102 * @param fileToDetail An {@link OCFile} to show in the fragment
103 * @param ocAccount An ownCloud account; needed to start downloads
104 */
105 public FileDetailFragment(OCFile fileToDetail, Account ocAccount) {
106 super(fileToDetail);
107 mAccount = ocAccount;
108 mLayout = R.layout.file_details_empty;
109 mProgressListener = null;
110 }
111
112
113 @Override
114 public void onCreate(Bundle savedInstanceState) {
115 super.onCreate(savedInstanceState);
116 setHasOptionsMenu(true);
117 }
118
119
120 @Override
121 public View onCreateView(LayoutInflater inflater, ViewGroup container,
122 Bundle savedInstanceState) {
123 //super.onCreateView(inflater, container, savedInstanceState);
124
125 if (savedInstanceState != null) {
126 setFile((OCFile)savedInstanceState.getParcelable(FileActivity.EXTRA_FILE));
127 mAccount = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
128 }
129
130 if(getFile() != null && mAccount != null) {
131 mLayout = R.layout.file_details_fragment;
132 }
133
134 View view = null;
135 view = inflater.inflate(mLayout, null);
136 mView = view;
137
138 if (mLayout == R.layout.file_details_fragment) {
139 mView.findViewById(R.id.fdKeepInSync).setOnClickListener(this);
140 ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.fdProgressBar);
141 mProgressListener = new ProgressListener(progressBar);
142 mView.findViewById(R.id.fdCancelBtn).setOnClickListener(this);
143 }
144
145 updateFileDetails(false, false);
146 return view;
147 }
148
149 /**
150 * {@inheritDoc}
151 */
152 @Override
153 public void onActivityCreated(Bundle savedInstanceState) {
154 super.onActivityCreated(savedInstanceState);
155 // TODO: Remove this code. (Updated no needed)
156 // if (mAccount != null) {
157 // OCFile file = mContainerActivity.getStorageManager().
158 // getFileByPath(getFile().getRemotePath());
159 // if (file != null) {
160 // setFile(file);
161 // }
162 // }
163 }
164
165
166 @Override
167 public void onSaveInstanceState(Bundle outState) {
168 super.onSaveInstanceState(outState);
169 outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
170 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
171 }
172
173 @Override
174 public void onStart() {
175 super.onStart();
176 listenForTransferProgress();
177 }
178
179 @Override
180 public void onResume() {
181 super.onResume();
182 mUploadFinishReceiver = new UploadFinishReceiver();
183 IntentFilter filter = new IntentFilter(FileUploader.getUploadFinishMessage());
184 getActivity().registerReceiver(mUploadFinishReceiver, filter);
185
186 }
187
188
189 @Override
190 public void onPause() {
191 super.onPause();
192 if (mUploadFinishReceiver != null) {
193 getActivity().unregisterReceiver(mUploadFinishReceiver);
194 mUploadFinishReceiver = null;
195 }
196 }
197
198
199 @Override
200 public void onStop() {
201 super.onStop();
202 leaveTransferProgress();
203 }
204
205
206 @Override
207 public View getView() {
208 return super.getView() == null ? mView : super.getView();
209 }
210
211
212 /**
213 * {@inheritDoc}
214 */
215 @Override
216 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
217 super.onCreateOptionsMenu(menu, inflater);
218 inflater.inflate(R.menu.file_actions_menu, menu);
219 MenuItem item = menu.findItem(R.id.action_see_details);
220 if (item != null) {
221 item.setVisible(false);
222 item.setEnabled(false);
223 }
224
225 // Send file
226 item = menu.findItem(R.id.action_send_file);
227 boolean sendEnabled = getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on");
228 if (item != null) {
229 if (sendEnabled) {
230 item.setVisible(true);
231 item.setEnabled(true);
232 } else {
233 item.setVisible(false);
234 item.setEnabled(false);
235
236 }
237 }
238 }
239
240
241 /**
242 * {@inheritDoc}
243 */
244 @Override
245 public void onPrepareOptionsMenu (Menu menu) {
246 super.onPrepareOptionsMenu(menu);
247
248 List<Integer> toHide = new ArrayList<Integer>();
249 List<Integer> toShow = new ArrayList<Integer>();
250 OCFile file = getFile();
251
252 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
253 boolean downloading = downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file);
254 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
255 boolean uploading = uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile());
256
257 if (downloading || uploading) {
258 toHide.add(R.id.action_download_file);
259 toHide.add(R.id.action_rename_file);
260 toHide.add(R.id.action_remove_file);
261 toHide.add(R.id.action_open_file_with);
262 if (!downloading) {
263 toHide.add(R.id.action_cancel_download);
264 toShow.add(R.id.action_cancel_upload);
265 } else {
266 toHide.add(R.id.action_cancel_upload);
267 toShow.add(R.id.action_cancel_download);
268 }
269
270 } else if (file != null && file.isDown()) {
271 toHide.add(R.id.action_download_file);
272 toHide.add(R.id.action_cancel_download);
273 toHide.add(R.id.action_cancel_upload);
274
275 toShow.add(R.id.action_rename_file);
276 toShow.add(R.id.action_remove_file);
277 toShow.add(R.id.action_open_file_with);
278 toShow.add(R.id.action_sync_file);
279
280 } else if (file != null) {
281 toHide.add(R.id.action_open_file_with);
282 toHide.add(R.id.action_cancel_download);
283 toHide.add(R.id.action_cancel_upload);
284 toHide.add(R.id.action_sync_file);
285
286 toShow.add(R.id.action_rename_file);
287 toShow.add(R.id.action_remove_file);
288 toShow.add(R.id.action_download_file);
289
290 } else {
291 toHide.add(R.id.action_open_file_with);
292 toHide.add(R.id.action_cancel_download);
293 toHide.add(R.id.action_cancel_upload);
294 toHide.add(R.id.action_sync_file);
295 toHide.add(R.id.action_download_file);
296 toHide.add(R.id.action_rename_file);
297 toHide.add(R.id.action_remove_file);
298
299 }
300
301 // Options shareLink
302 if (!file.isShareByLink()) {
303 toHide.add(R.id.action_unshare_file);
304 } else {
305 toShow.add(R.id.action_unshare_file);
306 }
307
308 MenuItem item = null;
309 for (int i : toHide) {
310 item = menu.findItem(i);
311 if (item != null) {
312 item.setVisible(false);
313 item.setEnabled(false);
314 }
315 }
316 for (int i : toShow) {
317 item = menu.findItem(i);
318 if (item != null) {
319 item.setVisible(true);
320 item.setEnabled(true);
321 }
322 }
323 }
324
325
326 /**
327 * {@inheritDoc}
328 */
329 @Override
330 public boolean onOptionsItemSelected(MenuItem item) {
331 switch (item.getItemId()) {
332 case R.id.action_share_file: {
333 mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
334 return true;
335 }
336 case R.id.action_unshare_file: {
337 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
338 return true;
339 }
340 case R.id.action_open_file_with: {
341 mContainerActivity.getFileOperationsHelper().openFile(getFile());
342 return true;
343 }
344 case R.id.action_remove_file: {
345 showDialogToRemoveFile();
346 return true;
347 }
348 case R.id.action_rename_file: {
349 showDialogToRenameFile();
350 return true;
351 }
352 case R.id.action_cancel_download:
353 case R.id.action_cancel_upload: {
354 ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
355 return true;
356 }
357 case R.id.action_download_file:
358 case R.id.action_sync_file: {
359 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
360 return true;
361 }
362 case R.id.action_send_file: {
363 // Obtain the file
364 if (!getFile().isDown()) { // Download the file
365 Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
366 ((FileDisplayActivity)mContainerActivity).startDownloadForSending(getFile());
367
368 } else {
369 ((FileDisplayActivity)mContainerActivity).getFileOperationsHelper().sendDownloadedFile(getFile());
370 }
371 return true;
372 }
373 default:
374 return false;
375 }
376 }
377
378 @Override
379 public void onClick(View v) {
380 switch (v.getId()) {
381 case R.id.fdKeepInSync: {
382 toggleKeepInSync();
383 break;
384 }
385 case R.id.fdCancelBtn: {
386 ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
387 break;
388 }
389 default:
390 Log_OC.e(TAG, "Incorrect view clicked!");
391 }
392 }
393
394
395 private void toggleKeepInSync() {
396 CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);
397 OCFile file = getFile();
398 file.setKeepInSync(cb.isChecked());
399 mContainerActivity.getStorageManager().saveFile(file);
400
401 /// register the OCFile instance in the observer service to monitor local updates;
402 /// if necessary, the file is download
403 Intent intent = new Intent(getActivity().getApplicationContext(),
404 FileObserverService.class);
405 intent.putExtra(FileObserverService.KEY_FILE_CMD,
406 (cb.isChecked()?
407 FileObserverService.CMD_ADD_OBSERVED_FILE:
408 FileObserverService.CMD_DEL_OBSERVED_FILE));
409 intent.putExtra(FileObserverService.KEY_CMD_ARG_FILE, file);
410 intent.putExtra(FileObserverService.KEY_CMD_ARG_ACCOUNT, mAccount);
411 getActivity().startService(intent);
412
413 if (file.keepInSync()) {
414 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
415 }
416 }
417
418 private void showDialogToRemoveFile() {
419 OCFile file = getFile();
420 ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
421 R.string.confirmation_remove_alert,
422 new String[]{file.getFileName()},
423 file.isDown() ? R.string.confirmation_remove_remote_and_local : R.string.confirmation_remove_remote,
424 file.isDown() ? R.string.confirmation_remove_local : -1,
425 R.string.common_cancel);
426 confDialog.setOnConfirmationListener(this);
427 confDialog.show(getFragmentManager(), FTAG_CONFIRMATION);
428 }
429
430
431 private void showDialogToRenameFile() {
432 OCFile file = getFile();
433 String fileName = file.getFileName();
434 int extensionStart = file.isFolder() ? -1 : fileName.lastIndexOf(".");
435 int selectionEnd = (extensionStart >= 0) ? extensionStart : fileName.length();
436 EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), fileName, 0, selectionEnd, this);
437 dialog.show(getFragmentManager(), "nameeditdialog");
438 }
439
440
441 @Override
442 public void onConfirmation(String callerTag) {
443 OCFile file = getFile();
444 if (callerTag.equals(FTAG_CONFIRMATION)) {
445 if (mContainerActivity.getStorageManager().getFileById(file.getFileId()) != null) {
446 mContainerActivity.getFileOperationsHelper().removeFile(file, true);
447 }
448 }
449 }
450
451 @Override
452 public void onNeutral(String callerTag) {
453 OCFile file = getFile();
454 mContainerActivity.getStorageManager().removeFile(file, false, true); // TODO perform in background task / new thread
455 if (file.getStoragePath() != null) {
456 file.setStoragePath(null);
457 updateFileDetails(file, mAccount);
458 }
459 }
460
461 @Override
462 public void onCancel(String callerTag) {
463 Log_OC.d(TAG, "REMOVAL CANCELED");
464 }
465
466
467 /**
468 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
469 *
470 * @return True when the fragment was created with the empty layout.
471 */
472 public boolean isEmpty() {
473 return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
474 }
475
476
477 /**
478 * Use this method to signal this Activity that it shall update its view.
479 *
480 * @param file : An {@link OCFile}
481 */
482 public void updateFileDetails(OCFile file, Account ocAccount) {
483 setFile(file);
484 mAccount = ocAccount;
485 updateFileDetails(false, false);
486 }
487
488 /**
489 * Updates the view with all relevant details about that file.
490 *
491 * TODO Remove parameter when the transferring state of files is kept in database.
492 *
493 * TODO REFACTORING! this method called 5 times before every time the fragment is shown!
494 *
495 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
496 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
497 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
498 *
499 * @param refresh If 'true', try to refresh the whole file from the database
500 */
501 public void updateFileDetails(boolean transferring, boolean refresh) {
502
503 if (readyToShow()) {
504 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
505 if (refresh && storageManager != null) {
506 setFile(storageManager.getFileByPath(getFile().getRemotePath()));
507 }
508 OCFile file = getFile();
509
510 // set file details
511 setFilename(file.getFileName());
512 setFiletype(file.getMimetype());
513 setFilesize(file.getFileLength());
514 if(ocVersionSupportsTimeCreated()){
515 setTimeCreated(file.getCreationTimestamp());
516 }
517
518 setTimeModified(file.getModificationTimestamp());
519
520 CheckBox cb = (CheckBox)getView().findViewById(R.id.fdKeepInSync);
521 cb.setChecked(file.keepInSync());
522
523 // configure UI for depending upon local state of the file
524 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
525 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
526 if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) || (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))) {
527 setButtonsForTransferring();
528
529 } else if (file.isDown()) {
530
531 setButtonsForDown();
532
533 } else {
534 // TODO load default preview image; when the local file is removed, the preview remains there
535 setButtonsForRemote();
536 }
537 }
538 getView().invalidate();
539 }
540
541 /**
542 * Checks if the fragment is ready to show details of a OCFile
543 *
544 * @return 'True' when the fragment is ready to show details of a file
545 */
546 private boolean readyToShow() {
547 return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
548 }
549
550
551 /**
552 * Updates the filename in view
553 * @param filename to set
554 */
555 private void setFilename(String filename) {
556 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
557 if (tv != null)
558 tv.setText(filename);
559 }
560
561 /**
562 * Updates the MIME type in view
563 * @param mimetype to set
564 */
565 private void setFiletype(String mimetype) {
566 TextView tv = (TextView) getView().findViewById(R.id.fdType);
567 if (tv != null) {
568 String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);;
569 tv.setText(printableMimetype);
570 }
571 ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
572 if (iv != null) {
573 iv.setImageResource(DisplayUtils.getResourceId(mimetype));
574 }
575 }
576
577 /**
578 * Updates the file size in view
579 * @param filesize in bytes to set
580 */
581 private void setFilesize(long filesize) {
582 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
583 if (tv != null)
584 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
585 }
586
587 /**
588 * Updates the time that the file was created in view
589 * @param milliseconds Unix time to set
590 */
591 private void setTimeCreated(long milliseconds){
592 TextView tv = (TextView) getView().findViewById(R.id.fdCreated);
593 TextView tvLabel = (TextView) getView().findViewById(R.id.fdCreatedLabel);
594 if(tv != null){
595 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
596 tv.setVisibility(View.VISIBLE);
597 tvLabel.setVisibility(View.VISIBLE);
598 }
599 }
600
601 /**
602 * Updates the time that the file was last modified
603 * @param milliseconds Unix time to set
604 */
605 private void setTimeModified(long milliseconds){
606 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
607 if(tv != null){
608 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
609 }
610 }
611
612 /**
613 * Enables or disables buttons for a file being downloaded
614 */
615 private void setButtonsForTransferring() {
616 if (!isEmpty()) {
617 // let's protect the user from himself ;)
618 getView().findViewById(R.id.fdKeepInSync).setEnabled(false);
619
620 // show the progress bar for the transfer
621 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
622 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
623 progressText.setVisibility(View.VISIBLE);
624 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
625 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
626 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
627 progressText.setText(R.string.downloader_download_in_progress_ticker);
628 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
629 progressText.setText(R.string.uploader_upload_in_progress_ticker);
630 }
631 }
632 }
633
634 /**
635 * Enables or disables buttons for a file locally available
636 */
637 private void setButtonsForDown() {
638 if (!isEmpty()) {
639 getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
640
641 // hides the progress bar
642 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
643 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
644 progressText.setVisibility(View.GONE);
645 }
646 }
647
648 /**
649 * Enables or disables buttons for a file not locally available
650 */
651 private void setButtonsForRemote() {
652 if (!isEmpty()) {
653 getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
654
655 // hides the progress bar
656 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
657 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
658 progressText.setVisibility(View.GONE);
659 }
660 }
661
662
663 /**
664 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
665 * the time that the file was created. There is a chance that this will
666 * be fixed in future versions. Use this method to check if this version of
667 * ownCloud has this fix.
668 * @return True, if ownCloud the ownCloud version is supporting creation time
669 */
670 private boolean ocVersionSupportsTimeCreated(){
671 /*if(mAccount != null){
672 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
673 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
674 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
675 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
676 return true;
677 }
678 }*/
679 return false;
680 }
681
682
683 /**
684 * Once the file upload has finished -> update view
685 *
686 * Being notified about the finish of an upload is necessary for the next sequence:
687 * 1. Upload a big file.
688 * 2. Force a synchronization; if it finished before the upload, the file in transfer will be included in the local database and in the file list
689 * of its containing folder; the the server includes it in the PROPFIND requests although it's not fully upload.
690 * 3. Click the file in the list to see its details.
691 * 4. Wait for the upload finishes; at this moment, the details view must be refreshed to enable the action buttons.
692 */
693 private class UploadFinishReceiver extends BroadcastReceiver {
694 @Override
695 public void onReceive(Context context, Intent intent) {
696 String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);
697
698 if (!isEmpty() && accountName.equals(mAccount.name)) {
699 boolean uploadWasFine = intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT, false);
700 String uploadRemotePath = intent.getStringExtra(FileUploader.EXTRA_REMOTE_PATH);
701 boolean renamedInUpload = getFile().getRemotePath().equals(intent.getStringExtra(FileUploader.EXTRA_OLD_REMOTE_PATH));
702 if (getFile().getRemotePath().equals(uploadRemotePath) ||
703 renamedInUpload) {
704 if (uploadWasFine) {
705 setFile(mContainerActivity.getStorageManager().getFileByPath(uploadRemotePath));
706 }
707 if (renamedInUpload) {
708 String newName = (new File(uploadRemotePath)).getName();
709 Toast msg = Toast.makeText(getActivity().getApplicationContext(), String.format(getString(R.string.filedetails_renamed_in_upload_msg), newName), Toast.LENGTH_LONG);
710 msg.show();
711 }
712 getSherlockActivity().removeStickyBroadcast(intent); // not the best place to do this; a small refactorization of BroadcastReceivers should be done
713
714 updateFileDetails(false, false); // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server
715
716 // Force the preview if the file is an image
717 if (uploadWasFine && PreviewImageFragment.canBePreviewed(getFile())) {
718 ((FileDisplayActivity)mContainerActivity).startImagePreview(getFile());
719 }
720 }
721 }
722 }
723 }
724
725
726 public void onDismiss(EditNameDialog dialog) {
727 if (dialog.getResult()) {
728 String newFilename = dialog.getNewFilename();
729 Log_OC.d(TAG, "name edit dialog dismissed with new name " + newFilename);
730 mContainerActivity.getFileOperationsHelper().renameFile(getFile(), newFilename);
731 }
732 }
733
734
735 public void listenForTransferProgress() {
736 if (mProgressListener != null) {
737 if (mContainerActivity.getFileDownloaderBinder() != null) {
738 mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile());
739 }
740 if (mContainerActivity.getFileUploaderBinder() != null) {
741 mContainerActivity.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile());
742 }
743 }
744 }
745
746
747 public void leaveTransferProgress() {
748 if (mProgressListener != null) {
749 if (mContainerActivity.getFileDownloaderBinder() != null) {
750 mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
751 }
752 if (mContainerActivity.getFileUploaderBinder() != null) {
753 mContainerActivity.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
754 }
755 }
756 }
757
758
759
760 /**
761 * Helper class responsible for updating the progress bar shown for file uploading or downloading
762 *
763 * @author David A. Velasco
764 */
765 private class ProgressListener implements OnDatatransferProgressListener {
766 int mLastPercent = 0;
767 WeakReference<ProgressBar> mProgressBar = null;
768
769 ProgressListener(ProgressBar progressBar) {
770 mProgressBar = new WeakReference<ProgressBar>(progressBar);
771 }
772
773 @Override
774 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) {
775 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
776 if (percent != mLastPercent) {
777 ProgressBar pb = mProgressBar.get();
778 if (pb != null) {
779 pb.setProgress(percent);
780 pb.postInvalidate();
781 }
782 }
783 mLastPercent = percent;
784 }
785
786 };
787
788 }