Refactoring relationship between FileActivities and FileFragments (WIP)
[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-2013 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 if (mAccount != null) {
156 OCFile file = mContainerActivity.getStorageManager().
157 getFileByPath(getFile().getRemotePath());
158 if (file != null) {
159 setFile(file);
160 }
161 }
162 }
163
164
165 @Override
166 public void onSaveInstanceState(Bundle outState) {
167 super.onSaveInstanceState(outState);
168 outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
169 outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
170 }
171
172 @Override
173 public void onStart() {
174 super.onStart();
175 listenForTransferProgress();
176 }
177
178 @Override
179 public void onResume() {
180 super.onResume();
181 mUploadFinishReceiver = new UploadFinishReceiver();
182 IntentFilter filter = new IntentFilter(FileUploader.getUploadFinishMessage());
183 getActivity().registerReceiver(mUploadFinishReceiver, filter);
184
185 }
186
187
188 @Override
189 public void onPause() {
190 super.onPause();
191 if (mUploadFinishReceiver != null) {
192 getActivity().unregisterReceiver(mUploadFinishReceiver);
193 mUploadFinishReceiver = null;
194 }
195 }
196
197
198 @Override
199 public void onStop() {
200 super.onStop();
201 leaveTransferProgress();
202 }
203
204
205 @Override
206 public View getView() {
207 return super.getView() == null ? mView : super.getView();
208 }
209
210
211 /**
212 * {@inheritDoc}
213 */
214 @Override
215 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
216 super.onCreateOptionsMenu(menu, inflater);
217 inflater.inflate(R.menu.file_actions_menu, menu);
218 MenuItem item = menu.findItem(R.id.action_see_details);
219 if (item != null) {
220 item.setVisible(false);
221 item.setEnabled(false);
222 }
223
224 // Send file
225 item = menu.findItem(R.id.action_send_file);
226 boolean sendEnabled = getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on");
227 if (item != null) {
228 if (sendEnabled) {
229 item.setVisible(true);
230 item.setEnabled(true);
231 } else {
232 item.setVisible(false);
233 item.setEnabled(false);
234
235 }
236 }
237 }
238
239
240 /**
241 * {@inheritDoc}
242 */
243 @Override
244 public void onPrepareOptionsMenu (Menu menu) {
245 super.onPrepareOptionsMenu(menu);
246
247 List<Integer> toHide = new ArrayList<Integer>();
248 List<Integer> toShow = new ArrayList<Integer>();
249 OCFile file = getFile();
250
251 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
252 boolean downloading = downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file);
253 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
254 boolean uploading = uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile());
255
256 if (downloading || uploading) {
257 toHide.add(R.id.action_download_file);
258 toHide.add(R.id.action_rename_file);
259 toHide.add(R.id.action_remove_file);
260 toHide.add(R.id.action_open_file_with);
261 if (!downloading) {
262 toHide.add(R.id.action_cancel_download);
263 toShow.add(R.id.action_cancel_upload);
264 } else {
265 toHide.add(R.id.action_cancel_upload);
266 toShow.add(R.id.action_cancel_download);
267 }
268
269 } else if (file != null && file.isDown()) {
270 toHide.add(R.id.action_download_file);
271 toHide.add(R.id.action_cancel_download);
272 toHide.add(R.id.action_cancel_upload);
273
274 toShow.add(R.id.action_rename_file);
275 toShow.add(R.id.action_remove_file);
276 toShow.add(R.id.action_open_file_with);
277 toShow.add(R.id.action_sync_file);
278
279 } else if (file != null) {
280 toHide.add(R.id.action_open_file_with);
281 toHide.add(R.id.action_cancel_download);
282 toHide.add(R.id.action_cancel_upload);
283 toHide.add(R.id.action_sync_file);
284
285 toShow.add(R.id.action_rename_file);
286 toShow.add(R.id.action_remove_file);
287 toShow.add(R.id.action_download_file);
288
289 } else {
290 toHide.add(R.id.action_open_file_with);
291 toHide.add(R.id.action_cancel_download);
292 toHide.add(R.id.action_cancel_upload);
293 toHide.add(R.id.action_sync_file);
294 toHide.add(R.id.action_download_file);
295 toHide.add(R.id.action_rename_file);
296 toHide.add(R.id.action_remove_file);
297
298 }
299
300 // Options shareLink
301 if (!file.isShareByLink()) {
302 toHide.add(R.id.action_unshare_file);
303 } else {
304 toShow.add(R.id.action_unshare_file);
305 }
306
307 MenuItem item = null;
308 for (int i : toHide) {
309 item = menu.findItem(i);
310 if (item != null) {
311 item.setVisible(false);
312 item.setEnabled(false);
313 }
314 }
315 for (int i : toShow) {
316 item = menu.findItem(i);
317 if (item != null) {
318 item.setVisible(true);
319 item.setEnabled(true);
320 }
321 }
322 }
323
324
325 /**
326 * {@inheritDoc}
327 */
328 @Override
329 public boolean onOptionsItemSelected(MenuItem item) {
330 switch (item.getItemId()) {
331 case R.id.action_share_file: {
332 mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
333 return true;
334 }
335 case R.id.action_unshare_file: {
336 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
337 return true;
338 }
339 case R.id.action_open_file_with: {
340 mContainerActivity.getFileOperationsHelper().openFile(getFile());
341 return true;
342 }
343 case R.id.action_remove_file: {
344 showDialogToRemoveFile();
345 return true;
346 }
347 case R.id.action_rename_file: {
348 showDialogToRenameFile();
349 return true;
350 }
351 case R.id.action_cancel_download:
352 case R.id.action_cancel_upload: {
353 ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
354 return true;
355 }
356 case R.id.action_download_file:
357 case R.id.action_sync_file: {
358 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
359 return true;
360 }
361 case R.id.action_send_file: {
362 // Obtain the file
363 if (!getFile().isDown()) { // Download the file
364 Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
365 ((FileDisplayActivity)mContainerActivity).startDownloadForSending(getFile());
366
367 } else {
368 ((FileDisplayActivity)mContainerActivity).getFileOperationsHelper().sendDownloadedFile(getFile());
369 }
370 return true;
371 }
372 default:
373 return false;
374 }
375 }
376
377 @Override
378 public void onClick(View v) {
379 switch (v.getId()) {
380 case R.id.fdKeepInSync: {
381 toggleKeepInSync();
382 break;
383 }
384 case R.id.fdCancelBtn: {
385 ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
386 break;
387 }
388 default:
389 Log_OC.e(TAG, "Incorrect view clicked!");
390 }
391 }
392
393
394 private void toggleKeepInSync() {
395 CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);
396 OCFile file = getFile();
397 file.setKeepInSync(cb.isChecked());
398 mContainerActivity.getStorageManager().saveFile(file);
399
400 /// register the OCFile instance in the observer service to monitor local updates;
401 /// if necessary, the file is download
402 Intent intent = new Intent(getActivity().getApplicationContext(),
403 FileObserverService.class);
404 intent.putExtra(FileObserverService.KEY_FILE_CMD,
405 (cb.isChecked()?
406 FileObserverService.CMD_ADD_OBSERVED_FILE:
407 FileObserverService.CMD_DEL_OBSERVED_FILE));
408 intent.putExtra(FileObserverService.KEY_CMD_ARG_FILE, file);
409 intent.putExtra(FileObserverService.KEY_CMD_ARG_ACCOUNT, mAccount);
410 getActivity().startService(intent);
411
412 if (file.keepInSync()) {
413 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
414 }
415 }
416
417 private void showDialogToRemoveFile() {
418 OCFile file = getFile();
419 ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
420 R.string.confirmation_remove_alert,
421 new String[]{file.getFileName()},
422 file.isDown() ? R.string.confirmation_remove_remote_and_local : R.string.confirmation_remove_remote,
423 file.isDown() ? R.string.confirmation_remove_local : -1,
424 R.string.common_cancel);
425 confDialog.setOnConfirmationListener(this);
426 confDialog.show(getFragmentManager(), FTAG_CONFIRMATION);
427 }
428
429
430 private void showDialogToRenameFile() {
431 OCFile file = getFile();
432 String fileName = file.getFileName();
433 int extensionStart = file.isFolder() ? -1 : fileName.lastIndexOf(".");
434 int selectionEnd = (extensionStart >= 0) ? extensionStart : fileName.length();
435 EditNameDialog dialog = EditNameDialog.newInstance(getString(R.string.rename_dialog_title), fileName, 0, selectionEnd, this);
436 dialog.show(getFragmentManager(), "nameeditdialog");
437 }
438
439
440 @Override
441 public void onConfirmation(String callerTag) {
442 OCFile file = getFile();
443 if (callerTag.equals(FTAG_CONFIRMATION)) {
444 if (mContainerActivity.getStorageManager().getFileById(file.getFileId()) != null) {
445 mContainerActivity.getFileOperationsHelper().removeFile(file, true);
446 }
447 }
448 }
449
450 @Override
451 public void onNeutral(String callerTag) {
452 OCFile file = getFile();
453 mContainerActivity.getStorageManager().removeFile(file, false, true); // TODO perform in background task / new thread
454 if (file.getStoragePath() != null) {
455 file.setStoragePath(null);
456 updateFileDetails(file, mAccount);
457 }
458 }
459
460 @Override
461 public void onCancel(String callerTag) {
462 Log_OC.d(TAG, "REMOVAL CANCELED");
463 }
464
465
466 /**
467 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
468 *
469 * @return True when the fragment was created with the empty layout.
470 */
471 public boolean isEmpty() {
472 return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
473 }
474
475
476 /**
477 * Use this method to signal this Activity that it shall update its view.
478 *
479 * @param file : An {@link OCFile}
480 */
481 public void updateFileDetails(OCFile file, Account ocAccount) {
482 setFile(file);
483 mAccount = ocAccount;
484 updateFileDetails(false, false);
485 }
486
487 /**
488 * Updates the view with all relevant details about that file.
489 *
490 * TODO Remove parameter when the transferring state of files is kept in database.
491 *
492 * TODO REFACTORING! this method called 5 times before every time the fragment is shown!
493 *
494 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
495 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
496 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
497 *
498 * @param refresh If 'true', try to refresh the whole file from the database
499 */
500 public void updateFileDetails(boolean transferring, boolean refresh) {
501
502 if (readyToShow()) {
503 FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
504 if (refresh && storageManager != null) {
505 setFile(storageManager.getFileByPath(getFile().getRemotePath()));
506 }
507 OCFile file = getFile();
508
509 // set file details
510 setFilename(file.getFileName());
511 setFiletype(file.getMimetype());
512 setFilesize(file.getFileLength());
513 if(ocVersionSupportsTimeCreated()){
514 setTimeCreated(file.getCreationTimestamp());
515 }
516
517 setTimeModified(file.getModificationTimestamp());
518
519 CheckBox cb = (CheckBox)getView().findViewById(R.id.fdKeepInSync);
520 cb.setChecked(file.keepInSync());
521
522 // configure UI for depending upon local state of the file
523 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
524 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
525 if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) || (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))) {
526 setButtonsForTransferring();
527
528 } else if (file.isDown()) {
529
530 setButtonsForDown();
531
532 } else {
533 // TODO load default preview image; when the local file is removed, the preview remains there
534 setButtonsForRemote();
535 }
536 }
537 getView().invalidate();
538 }
539
540 /**
541 * Checks if the fragment is ready to show details of a OCFile
542 *
543 * @return 'True' when the fragment is ready to show details of a file
544 */
545 private boolean readyToShow() {
546 return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
547 }
548
549
550 /**
551 * Updates the filename in view
552 * @param filename to set
553 */
554 private void setFilename(String filename) {
555 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
556 if (tv != null)
557 tv.setText(filename);
558 }
559
560 /**
561 * Updates the MIME type in view
562 * @param mimetype to set
563 */
564 private void setFiletype(String mimetype) {
565 TextView tv = (TextView) getView().findViewById(R.id.fdType);
566 if (tv != null) {
567 String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);;
568 tv.setText(printableMimetype);
569 }
570 ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
571 if (iv != null) {
572 iv.setImageResource(DisplayUtils.getResourceId(mimetype));
573 }
574 }
575
576 /**
577 * Updates the file size in view
578 * @param filesize in bytes to set
579 */
580 private void setFilesize(long filesize) {
581 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
582 if (tv != null)
583 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
584 }
585
586 /**
587 * Updates the time that the file was created in view
588 * @param milliseconds Unix time to set
589 */
590 private void setTimeCreated(long milliseconds){
591 TextView tv = (TextView) getView().findViewById(R.id.fdCreated);
592 TextView tvLabel = (TextView) getView().findViewById(R.id.fdCreatedLabel);
593 if(tv != null){
594 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
595 tv.setVisibility(View.VISIBLE);
596 tvLabel.setVisibility(View.VISIBLE);
597 }
598 }
599
600 /**
601 * Updates the time that the file was last modified
602 * @param milliseconds Unix time to set
603 */
604 private void setTimeModified(long milliseconds){
605 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
606 if(tv != null){
607 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
608 }
609 }
610
611 /**
612 * Enables or disables buttons for a file being downloaded
613 */
614 private void setButtonsForTransferring() {
615 if (!isEmpty()) {
616 // let's protect the user from himself ;)
617 getView().findViewById(R.id.fdKeepInSync).setEnabled(false);
618
619 // show the progress bar for the transfer
620 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
621 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
622 progressText.setVisibility(View.VISIBLE);
623 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
624 FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
625 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
626 progressText.setText(R.string.downloader_download_in_progress_ticker);
627 } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
628 progressText.setText(R.string.uploader_upload_in_progress_ticker);
629 }
630 }
631 }
632
633 /**
634 * Enables or disables buttons for a file locally available
635 */
636 private void setButtonsForDown() {
637 if (!isEmpty()) {
638 getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
639
640 // hides the progress bar
641 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
642 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
643 progressText.setVisibility(View.GONE);
644 }
645 }
646
647 /**
648 * Enables or disables buttons for a file not locally available
649 */
650 private void setButtonsForRemote() {
651 if (!isEmpty()) {
652 getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
653
654 // hides the progress bar
655 getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
656 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
657 progressText.setVisibility(View.GONE);
658 }
659 }
660
661
662 /**
663 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
664 * the time that the file was created. There is a chance that this will
665 * be fixed in future versions. Use this method to check if this version of
666 * ownCloud has this fix.
667 * @return True, if ownCloud the ownCloud version is supporting creation time
668 */
669 private boolean ocVersionSupportsTimeCreated(){
670 /*if(mAccount != null){
671 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
672 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
673 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
674 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
675 return true;
676 }
677 }*/
678 return false;
679 }
680
681
682 /**
683 * Once the file upload has finished -> update view
684 *
685 * Being notified about the finish of an upload is necessary for the next sequence:
686 * 1. Upload a big file.
687 * 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
688 * of its containing folder; the the server includes it in the PROPFIND requests although it's not fully upload.
689 * 3. Click the file in the list to see its details.
690 * 4. Wait for the upload finishes; at this moment, the details view must be refreshed to enable the action buttons.
691 */
692 private class UploadFinishReceiver extends BroadcastReceiver {
693 @Override
694 public void onReceive(Context context, Intent intent) {
695 String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);
696
697 if (!isEmpty() && accountName.equals(mAccount.name)) {
698 boolean uploadWasFine = intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT, false);
699 String uploadRemotePath = intent.getStringExtra(FileUploader.EXTRA_REMOTE_PATH);
700 boolean renamedInUpload = getFile().getRemotePath().equals(intent.getStringExtra(FileUploader.EXTRA_OLD_REMOTE_PATH));
701 if (getFile().getRemotePath().equals(uploadRemotePath) ||
702 renamedInUpload) {
703 if (uploadWasFine) {
704 setFile(mContainerActivity.getStorageManager().getFileByPath(uploadRemotePath));
705 }
706 if (renamedInUpload) {
707 String newName = (new File(uploadRemotePath)).getName();
708 Toast msg = Toast.makeText(getActivity().getApplicationContext(), String.format(getString(R.string.filedetails_renamed_in_upload_msg), newName), Toast.LENGTH_LONG);
709 msg.show();
710 }
711 getSherlockActivity().removeStickyBroadcast(intent); // not the best place to do this; a small refactorization of BroadcastReceivers should be done
712
713 updateFileDetails(false, false); // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server
714
715 // Force the preview if the file is an image
716 if (uploadWasFine && PreviewImageFragment.canBePreviewed(getFile())) {
717 ((FileDisplayActivity)mContainerActivity).startImagePreview(getFile());
718 }
719 }
720 }
721 }
722 }
723
724
725 public void onDismiss(EditNameDialog dialog) {
726 if (dialog.getResult()) {
727 String newFilename = dialog.getNewFilename();
728 Log_OC.d(TAG, "name edit dialog dismissed with new name " + newFilename);
729 mContainerActivity.getFileOperationsHelper().renameFile(getFile(), newFilename);
730 }
731 }
732
733
734 public void listenForTransferProgress() {
735 if (mProgressListener != null) {
736 if (mContainerActivity.getFileDownloaderBinder() != null) {
737 mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile());
738 }
739 if (mContainerActivity.getFileUploaderBinder() != null) {
740 mContainerActivity.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile());
741 }
742 }
743 }
744
745
746 public void leaveTransferProgress() {
747 if (mProgressListener != null) {
748 if (mContainerActivity.getFileDownloaderBinder() != null) {
749 mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
750 }
751 if (mContainerActivity.getFileUploaderBinder() != null) {
752 mContainerActivity.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
753 }
754 }
755 }
756
757
758
759 /**
760 * Helper class responsible for updating the progress bar shown for file uploading or downloading
761 *
762 * @author David A. Velasco
763 */
764 private class ProgressListener implements OnDatatransferProgressListener {
765 int mLastPercent = 0;
766 WeakReference<ProgressBar> mProgressBar = null;
767
768 ProgressListener(ProgressBar progressBar) {
769 mProgressBar = new WeakReference<ProgressBar>(progressBar);
770 }
771
772 @Override
773 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) {
774 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
775 if (percent != mLastPercent) {
776 ProgressBar pb = mProgressBar.get();
777 if (pb != null) {
778 pb.setProgress(percent);
779 pb.postInvalidate();
780 }
781 }
782 mLastPercent = percent;
783 }
784
785 };
786
787 }