1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.ui
.fragment
;
21 import java
.lang
.ref
.WeakReference
;
22 import java
.util
.ArrayList
;
23 import java
.util
.List
;
25 import android
.accounts
.Account
;
26 import android
.app
.Activity
;
27 import android
.content
.BroadcastReceiver
;
28 import android
.content
.Context
;
29 import android
.content
.Intent
;
30 import android
.content
.IntentFilter
;
31 import android
.os
.Bundle
;
32 import android
.os
.Handler
;
33 import android
.view
.LayoutInflater
;
34 import android
.view
.View
;
35 import android
.view
.View
.OnClickListener
;
36 import android
.view
.ViewGroup
;
37 import android
.widget
.CheckBox
;
38 import android
.widget
.ImageView
;
39 import android
.widget
.ProgressBar
;
40 import android
.widget
.TextView
;
41 import android
.widget
.Toast
;
43 import com
.actionbarsherlock
.view
.Menu
;
44 import com
.actionbarsherlock
.view
.MenuInflater
;
45 import com
.actionbarsherlock
.view
.MenuItem
;
46 import com
.owncloud
.android
.R
;
47 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
48 import com
.owncloud
.android
.datamodel
.OCFile
;
49 import com
.owncloud
.android
.files
.services
.FileObserverService
;
50 import com
.owncloud
.android
.files
.services
.FileUploader
;
51 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
52 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
53 import com
.owncloud
.android
.lib
.network
.OnDatatransferProgressListener
;
54 import com
.owncloud
.android
.lib
.operations
.common
.OnRemoteOperationListener
;
55 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperation
;
56 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
;
57 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
.ResultCode
;
58 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
59 import com
.owncloud
.android
.operations
.RenameFileOperation
;
60 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
61 import com
.owncloud
.android
.ui
.activity
.ConflictsResolveActivity
;
62 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
63 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
64 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
65 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
66 import com
.owncloud
.android
.ui
.preview
.PreviewImageFragment
;
67 import com
.owncloud
.android
.utils
.DisplayUtils
;
68 import com
.owncloud
.android
.utils
.Log_OC
;
72 * This Fragment is used to display the details about a file.
74 * @author Bartek Przybylski
75 * @author David A. Velasco
77 public class FileDetailFragment
extends FileFragment
implements
79 ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
, OnRemoteOperationListener
, EditNameDialogListener
{
81 private FileFragment
.ContainerActivity mContainerActivity
;
85 private Account mAccount
;
86 private FileDataStorageManager mStorageManager
;
88 private UploadFinishReceiver mUploadFinishReceiver
;
89 public ProgressListener mProgressListener
;
91 private Handler mHandler
;
92 private RemoteOperation mLastRemoteOperation
;
94 private static final String TAG
= FileDetailFragment
.class.getSimpleName();
95 public static final String FTAG_CONFIRMATION
= "REMOVE_CONFIRMATION_FRAGMENT";
99 * Creates an empty details fragment.
101 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
103 public FileDetailFragment() {
106 mStorageManager
= null
;
107 mLayout
= R
.layout
.file_details_empty
;
108 mProgressListener
= null
;
112 * Creates a details fragment.
114 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
116 * @param fileToDetail An {@link OCFile} to show in the fragment
117 * @param ocAccount An ownCloud account; needed to start downloads
119 public FileDetailFragment(OCFile fileToDetail
, Account ocAccount
) {
121 mAccount
= ocAccount
;
122 mStorageManager
= null
; // we need a context to init this; the container activity is not available yet at this moment
123 mLayout
= R
.layout
.file_details_empty
;
124 mProgressListener
= null
;
129 public void onCreate(Bundle savedInstanceState
) {
130 super.onCreate(savedInstanceState
);
131 mHandler
= new Handler();
132 setHasOptionsMenu(true
);
137 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
138 Bundle savedInstanceState
) {
139 //super.onCreateView(inflater, container, savedInstanceState);
141 if (savedInstanceState
!= null
) {
142 setFile((OCFile
)savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
));
143 mAccount
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
146 if(getFile() != null
&& mAccount
!= null
) {
147 mLayout
= R
.layout
.file_details_fragment
;
151 //view = inflater.inflate(mLayout, container, false);
152 view
= inflater
.inflate(mLayout
, null
);
155 if (mLayout
== R
.layout
.file_details_fragment
) {
156 mView
.findViewById(R
.id
.fdKeepInSync
).setOnClickListener(this);
157 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.fdProgressBar
);
158 mProgressListener
= new ProgressListener(progressBar
);
159 mView
.findViewById(R
.id
.fdCancelBtn
).setOnClickListener(this);
162 updateFileDetails(false
, false
);
170 public void onAttach(Activity activity
) {
171 super.onAttach(activity
);
173 mContainerActivity
= (ContainerActivity
) activity
;
175 } catch (ClassCastException e
) {
176 throw new ClassCastException(activity
.toString() + " must implement " + FileDetailFragment
.ContainerActivity
.class.getSimpleName());
185 public void onActivityCreated(Bundle savedInstanceState
) {
186 super.onActivityCreated(savedInstanceState
);
187 if (mAccount
!= null
) {
188 mStorageManager
= new FileDataStorageManager(mAccount
, getActivity().getApplicationContext().getContentResolver());
194 public void onSaveInstanceState(Bundle outState
) {
195 super.onSaveInstanceState(outState
);
196 outState
.putParcelable(FileActivity
.EXTRA_FILE
, getFile());
197 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
201 public void onStart() {
203 listenForTransferProgress();
207 public void onResume() {
209 mUploadFinishReceiver
= new UploadFinishReceiver();
210 IntentFilter filter
= new IntentFilter(FileUploader
.getUploadFinishMessage());
211 getActivity().registerReceiver(mUploadFinishReceiver
, filter
);
217 public void onPause() {
219 if (mUploadFinishReceiver
!= null
) {
220 getActivity().unregisterReceiver(mUploadFinishReceiver
);
221 mUploadFinishReceiver
= null
;
227 public void onStop() {
229 leaveTransferProgress();
234 public View
getView() {
235 return super.getView() == null ? mView
: super.getView();
243 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
244 super.onCreateOptionsMenu(menu
, inflater
);
245 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
246 MenuItem item
= menu
.findItem(R
.id
.action_see_details
);
248 item
.setVisible(false
);
249 item
.setEnabled(false
);
258 public void onPrepareOptionsMenu (Menu menu
) {
259 super.onPrepareOptionsMenu(menu
);
261 List
<Integer
> toHide
= new ArrayList
<Integer
>();
262 List
<Integer
> toShow
= new ArrayList
<Integer
>();
263 OCFile file
= getFile();
265 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
266 boolean downloading
= downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
);
267 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
268 boolean uploading
= uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile());
270 if (downloading
|| uploading
) {
271 toHide
.add(R
.id
.action_download_file
);
272 toHide
.add(R
.id
.action_rename_file
);
273 toHide
.add(R
.id
.action_remove_file
);
274 toHide
.add(R
.id
.action_open_file_with
);
276 toHide
.add(R
.id
.action_cancel_download
);
277 toShow
.add(R
.id
.action_cancel_upload
);
279 toHide
.add(R
.id
.action_cancel_upload
);
280 toShow
.add(R
.id
.action_cancel_download
);
283 } else if (file
!= null
&& file
.isDown()) {
284 toHide
.add(R
.id
.action_download_file
);
285 toHide
.add(R
.id
.action_cancel_download
);
286 toHide
.add(R
.id
.action_cancel_upload
);
288 toShow
.add(R
.id
.action_rename_file
);
289 toShow
.add(R
.id
.action_remove_file
);
290 toShow
.add(R
.id
.action_open_file_with
);
291 toShow
.add(R
.id
.action_sync_file
);
293 } else if (file
!= null
) {
294 toHide
.add(R
.id
.action_open_file_with
);
295 toHide
.add(R
.id
.action_cancel_download
);
296 toHide
.add(R
.id
.action_cancel_upload
);
297 toHide
.add(R
.id
.action_sync_file
);
299 toShow
.add(R
.id
.action_rename_file
);
300 toShow
.add(R
.id
.action_remove_file
);
301 toShow
.add(R
.id
.action_download_file
);
304 toHide
.add(R
.id
.action_open_file_with
);
305 toHide
.add(R
.id
.action_cancel_download
);
306 toHide
.add(R
.id
.action_cancel_upload
);
307 toHide
.add(R
.id
.action_sync_file
);
308 toHide
.add(R
.id
.action_download_file
);
309 toHide
.add(R
.id
.action_rename_file
);
310 toHide
.add(R
.id
.action_remove_file
);
315 if (!file
.isShareByLink()) {
316 toHide
.add(R
.id
.action_unshare_file
);
319 MenuItem item
= null
;
320 for (int i
: toHide
) {
321 item
= menu
.findItem(i
);
323 item
.setVisible(false
);
324 item
.setEnabled(false
);
327 for (int i
: toShow
) {
328 item
= menu
.findItem(i
);
330 item
.setVisible(true
);
331 item
.setEnabled(true
);
341 public boolean onOptionsItemSelected(MenuItem item
) {
342 switch (item
.getItemId()) {
343 case R
.id
.action_open_file_with
: {
344 mContainerActivity
.openFile(getFile());
347 case R
.id
.action_remove_file
: {
351 case R
.id
.action_rename_file
: {
355 case R
.id
.action_download_file
:
356 case R
.id
.action_cancel_download
:
357 case R
.id
.action_cancel_upload
:
358 case R
.id
.action_sync_file
: {
368 public void onClick(View v
) {
370 case R
.id
.fdKeepInSync
: {
374 case R
.id
.fdCancelBtn
: {
379 Log_OC
.e(TAG
, "Incorrect view clicked!");
384 private void toggleKeepInSync() {
385 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
386 OCFile file
= getFile();
387 file
.setKeepInSync(cb
.isChecked());
388 mStorageManager
.saveFile(file
);
390 /// register the OCFile instance in the observer service to monitor local updates;
391 /// if necessary, the file is download
392 Intent intent
= new Intent(getActivity().getApplicationContext(),
393 FileObserverService
.class);
394 intent
.putExtra(FileObserverService
.KEY_FILE_CMD
,
396 FileObserverService
.CMD_ADD_OBSERVED_FILE
:
397 FileObserverService
.CMD_DEL_OBSERVED_FILE
));
398 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_FILE
, file
);
399 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_ACCOUNT
, mAccount
);
400 getActivity().startService(intent
);
402 if (file
.keepInSync()) {
403 synchronizeFile(); // force an immediate synchronization
408 private void removeFile() {
409 OCFile file
= getFile();
410 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
411 R
.string
.confirmation_remove_alert
,
412 new String
[]{file
.getFileName()},
413 file
.isDown() ? R
.string
.confirmation_remove_remote_and_local
: R
.string
.confirmation_remove_remote
,
414 file
.isDown() ? R
.string
.confirmation_remove_local
: -1,
415 R
.string
.common_cancel
);
416 confDialog
.setOnConfirmationListener(this);
417 confDialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
421 private void renameFile() {
422 OCFile file
= getFile();
423 String fileName
= file
.getFileName();
424 int extensionStart
= file
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
425 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
426 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
427 dialog
.show(getFragmentManager(), "nameeditdialog");
430 private void synchronizeFile() {
431 OCFile file
= getFile();
432 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
433 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
434 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
435 downloaderBinder
.cancel(mAccount
, file
);
439 setButtonsForRemote();
442 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
443 uploaderBinder
.cancel(mAccount
, file
);
444 if (!file
.fileExists()) {
445 // TODO make something better
446 ((FileDisplayActivity
)getActivity()).cleanSecondFragment();
448 } else if (file
.isDown()) {
451 setButtonsForRemote();
455 mLastRemoteOperation
= new SynchronizeFileOperation(file
, null
, mStorageManager
, mAccount
, true
, getActivity());
456 mLastRemoteOperation
.execute(mAccount
, getSherlockActivity(), this, mHandler
, getSherlockActivity());
459 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
465 public void onConfirmation(String callerTag
) {
466 OCFile file
= getFile();
467 if (callerTag
.equals(FTAG_CONFIRMATION
)) {
468 if (mStorageManager
.getFileById(file
.getFileId()) != null
) {
469 mLastRemoteOperation
= new RemoveFileOperation( file
,
472 mLastRemoteOperation
.execute(mAccount
, getSherlockActivity(), this, mHandler
, getSherlockActivity());
474 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
480 public void onNeutral(String callerTag
) {
481 OCFile file
= getFile();
482 mStorageManager
.removeFile(file
, false
, true
); // TODO perform in background task / new thread
483 if (file
.getStoragePath() != null
) {
484 file
.setStoragePath(null
);
485 updateFileDetails(file
, mAccount
);
490 public void onCancel(String callerTag
) {
491 Log_OC
.d(TAG
, "REMOVAL CANCELED");
496 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
498 * @return True when the fragment was created with the empty layout.
500 public boolean isEmpty() {
501 return (mLayout
== R
.layout
.file_details_empty
|| getFile() == null
|| mAccount
== null
);
506 * Use this method to signal this Activity that it shall update its view.
508 * @param file : An {@link OCFile}
510 public void updateFileDetails(OCFile file
, Account ocAccount
) {
512 if (ocAccount
!= null
&& (
513 mStorageManager
== null
||
514 (mAccount
!= null
&& !mAccount
.equals(ocAccount
))
516 mStorageManager
= new FileDataStorageManager(ocAccount
, getActivity().getApplicationContext().getContentResolver());
518 mAccount
= ocAccount
;
519 updateFileDetails(false
, false
);
523 * Updates the view with all relevant details about that file.
525 * TODO Remove parameter when the transferring state of files is kept in database.
527 * TODO REFACTORING! this method called 5 times before every time the fragment is shown!
529 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
530 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
531 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
533 * @param refresh If 'true', try to refresh the hold file from the database
535 public void updateFileDetails(boolean transferring
, boolean refresh
) {
539 if (refresh
&& mStorageManager
!= null
) {
540 setFile(mStorageManager
.getFileByPath(getFile().getRemotePath()));
542 OCFile file
= getFile();
545 setFilename(file
.getFileName());
546 setFiletype(file
.getMimetype());
547 setFilesize(file
.getFileLength());
548 if(ocVersionSupportsTimeCreated()){
549 setTimeCreated(file
.getCreationTimestamp());
552 setTimeModified(file
.getModificationTimestamp());
554 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
555 cb
.setChecked(file
.keepInSync());
557 // configure UI for depending upon local state of the file
558 //if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) || FileUploader.isUploading(mAccount, mFile.getRemotePath())) {
559 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
560 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
561 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) || (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
))) {
562 setButtonsForTransferring();
564 } else if (file
.isDown()) {
569 // TODO load default preview image; when the local file is removed, the preview remains there
570 setButtonsForRemote();
573 getView().invalidate();
577 * Checks if the fragment is ready to show details of a OCFile
579 * @return 'True' when the fragment is ready to show details of a file
581 private boolean readyToShow() {
582 return (getFile() != null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
);
587 * Updates the filename in view
588 * @param filename to set
590 private void setFilename(String filename
) {
591 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
593 tv
.setText(filename
);
597 * Updates the MIME type in view
598 * @param mimetype to set
600 private void setFiletype(String mimetype
) {
601 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
603 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);;
604 tv
.setText(printableMimetype
);
606 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
608 iv
.setImageResource(DisplayUtils
.getResourceId(mimetype
));
613 * Updates the file size in view
614 * @param filesize in bytes to set
616 private void setFilesize(long filesize
) {
617 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
619 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
623 * Updates the time that the file was created in view
624 * @param milliseconds Unix time to set
626 private void setTimeCreated(long milliseconds
){
627 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
628 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
630 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
631 tv
.setVisibility(View
.VISIBLE
);
632 tvLabel
.setVisibility(View
.VISIBLE
);
637 * Updates the time that the file was last modified
638 * @param milliseconds Unix time to set
640 private void setTimeModified(long milliseconds
){
641 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
643 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
648 * Enables or disables buttons for a file being downloaded
650 private void setButtonsForTransferring() {
652 // let's protect the user from himself ;)
653 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(false
);
655 // show the progress bar for the transfer
656 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.VISIBLE
);
657 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
658 progressText
.setVisibility(View
.VISIBLE
);
659 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
660 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
661 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
662 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
663 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile())) {
664 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
670 * Enables or disables buttons for a file locally available
672 private void setButtonsForDown() {
674 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
676 // hides the progress bar
677 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
678 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
679 progressText
.setVisibility(View
.GONE
);
684 * Enables or disables buttons for a file not locally available
686 private void setButtonsForRemote() {
688 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
690 // hides the progress bar
691 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
692 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
693 progressText
.setVisibility(View
.GONE
);
699 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
700 * the time that the file was created. There is a chance that this will
701 * be fixed in future versions. Use this method to check if this version of
702 * ownCloud has this fix.
703 * @return True, if ownCloud the ownCloud version is supporting creation time
705 private boolean ocVersionSupportsTimeCreated(){
706 /*if(mAccount != null){
707 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
708 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
709 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
710 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
719 * Once the file upload has finished -> update view
721 * Being notified about the finish of an upload is necessary for the next sequence:
722 * 1. Upload a big file.
723 * 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
724 * of its containing folder; the the server includes it in the PROPFIND requests although it's not fully upload.
725 * 3. Click the file in the list to see its details.
726 * 4. Wait for the upload finishes; at this moment, the details view must be refreshed to enable the action buttons.
728 private class UploadFinishReceiver
extends BroadcastReceiver
{
730 public void onReceive(Context context
, Intent intent
) {
731 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
733 if (!isEmpty() && accountName
.equals(mAccount
.name
)) {
734 boolean uploadWasFine
= intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
, false
);
735 String uploadRemotePath
= intent
.getStringExtra(FileUploader
.EXTRA_REMOTE_PATH
);
736 boolean renamedInUpload
= getFile().getRemotePath().equals(intent
.getStringExtra(FileUploader
.EXTRA_OLD_REMOTE_PATH
));
737 if (getFile().getRemotePath().equals(uploadRemotePath
) ||
740 setFile(mStorageManager
.getFileByPath(uploadRemotePath
));
742 if (renamedInUpload
) {
743 String newName
= (new File(uploadRemotePath
)).getName();
744 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), String
.format(getString(R
.string
.filedetails_renamed_in_upload_msg
), newName
), Toast
.LENGTH_LONG
);
747 getSherlockActivity().removeStickyBroadcast(intent
); // not the best place to do this; a small refactorization of BroadcastReceivers should be done
749 updateFileDetails(false
, false
); // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server
751 // Force the preview if the file is an image
752 if (uploadWasFine
&& PreviewImageFragment
.canBePreviewed(getFile())) {
753 ((FileDisplayActivity
) mContainerActivity
).startImagePreview(getFile());
761 public void onDismiss(EditNameDialog dialog
) {
762 if (dialog
.getResult()) {
763 String newFilename
= dialog
.getNewFilename();
764 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
765 mLastRemoteOperation
= new RenameFileOperation( getFile(),
768 new FileDataStorageManager(mAccount
, getActivity().getContentResolver()));
769 mLastRemoteOperation
.execute(mAccount
, getSherlockActivity(), this, mHandler
, getSherlockActivity());
770 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
779 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
780 if (operation
.equals(mLastRemoteOperation
)) {
781 if (operation
instanceof RemoveFileOperation
) {
782 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
784 } else if (operation
instanceof RenameFileOperation
) {
785 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
787 } else if (operation
instanceof SynchronizeFileOperation
) {
788 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
794 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
795 ((FileDisplayActivity
) getActivity()).dismissLoadingDialog();
796 if (result
.isSuccess()) {
797 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
799 ((FileDisplayActivity
)getActivity()).cleanSecondFragment();
802 Toast msg
= Toast
.makeText(getActivity(), R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
804 if (result
.isSslRecoverableException()) {
805 // TODO show the SSL warning dialog
810 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
811 ((FileDisplayActivity
) getActivity()).dismissLoadingDialog();
813 if (result
.isSuccess()) {
814 updateFileDetails(((RenameFileOperation
)operation
).getFile(), mAccount
);
815 mContainerActivity
.onFileStateChanged();
818 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
819 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
821 // TODO throw again the new rename dialog
822 } if (result
.getCode().equals(ResultCode
.INVALID_CHARACTER_IN_NAME
)) {
823 Toast msg
= Toast
.makeText(getActivity(), R
.string
.filename_forbidden_characters
, Toast
.LENGTH_LONG
);
826 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
828 if (result
.isSslRecoverableException()) {
829 // TODO show the SSL warning dialog
835 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
, RemoteOperationResult result
) {
836 ((FileDisplayActivity
) getActivity()).dismissLoadingDialog();
837 OCFile file
= getFile();
838 if (!result
.isSuccess()) {
839 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
840 Intent i
= new Intent(getActivity(), ConflictsResolveActivity
.class);
841 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, file
);
842 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, mAccount
);
851 setButtonsForRemote();
855 if (operation
.transferWasRequested()) {
856 setButtonsForTransferring();
857 mContainerActivity
.onFileStateChanged(); // this is not working; FileDownloader won't do NOTHING at all until this method finishes, so
858 // checking the service to see if the file is downloading results in FALSE
860 Toast msg
= Toast
.makeText(getActivity(), R
.string
.sync_file_nothing_to_do_msg
, Toast
.LENGTH_LONG
);
866 setButtonsForRemote();
873 public void listenForTransferProgress() {
874 if (mProgressListener
!= null
) {
875 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
876 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
878 if (mContainerActivity
.getFileUploaderBinder() != null
) {
879 mContainerActivity
.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
885 public void leaveTransferProgress() {
886 if (mProgressListener
!= null
) {
887 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
888 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
890 if (mContainerActivity
.getFileUploaderBinder() != null
) {
891 mContainerActivity
.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
899 * Helper class responsible for updating the progress bar shown for file uploading or downloading
901 * @author David A. Velasco
903 private class ProgressListener
implements OnDatatransferProgressListener
{
904 int mLastPercent
= 0;
905 WeakReference
<ProgressBar
> mProgressBar
= null
;
907 ProgressListener(ProgressBar progressBar
) {
908 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
912 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
913 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
914 if (percent
!= mLastPercent
) {
915 ProgressBar pb
= mProgressBar
.get();
917 pb
.setProgress(percent
);
921 mLastPercent
= percent
;