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
.DisplayUtils
;
47 import com
.owncloud
.android
.Log_OC
;
48 import com
.owncloud
.android
.R
;
49 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
50 import com
.owncloud
.android
.datamodel
.OCFile
;
51 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
52 import com
.owncloud
.android
.files
.services
.FileObserverService
;
53 import com
.owncloud
.android
.files
.services
.FileUploader
;
54 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
55 import com
.owncloud
.android
.operations
.OnRemoteOperationListener
;
56 import com
.owncloud
.android
.operations
.RemoteOperation
;
57 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
58 import com
.owncloud
.android
.operations
.RemoteOperationResult
.ResultCode
;
59 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
60 import com
.owncloud
.android
.operations
.RenameFileOperation
;
61 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
62 import com
.owncloud
.android
.ui
.activity
.ConflictsResolveActivity
;
63 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
64 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
65 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
66 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
68 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
71 * This Fragment is used to display the details about a file.
73 * @author Bartek Przybylski
74 * @author David A. Velasco
76 public class FileDetailFragment
extends FileFragment
implements
78 ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
, OnRemoteOperationListener
, EditNameDialogListener
{
80 private FileFragment
.ContainerActivity mContainerActivity
;
84 private Account mAccount
;
85 private FileDataStorageManager mStorageManager
;
87 private UploadFinishReceiver mUploadFinishReceiver
;
88 public ProgressListener mProgressListener
;
90 private Handler mHandler
;
91 private RemoteOperation mLastRemoteOperation
;
93 private static final String TAG
= FileDetailFragment
.class.getSimpleName();
94 public static final String FTAG_CONFIRMATION
= "REMOVE_CONFIRMATION_FRAGMENT";
98 * Creates an empty details fragment.
100 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
102 public FileDetailFragment() {
105 mStorageManager
= null
;
106 mLayout
= R
.layout
.file_details_empty
;
107 mProgressListener
= null
;
111 * Creates a details fragment.
113 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
115 * @param fileToDetail An {@link OCFile} to show in the fragment
116 * @param ocAccount An ownCloud account; needed to start downloads
118 public FileDetailFragment(OCFile fileToDetail
, Account ocAccount
) {
120 mAccount
= ocAccount
;
121 mStorageManager
= null
; // we need a context to init this; the container activity is not available yet at this moment
122 mLayout
= R
.layout
.file_details_empty
;
123 mProgressListener
= null
;
128 public void onCreate(Bundle savedInstanceState
) {
129 super.onCreate(savedInstanceState
);
130 mHandler
= new Handler();
131 setHasOptionsMenu(true
);
136 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
137 Bundle savedInstanceState
) {
138 //super.onCreateView(inflater, container, savedInstanceState);
140 if (savedInstanceState
!= null
) {
141 setFile((OCFile
)savedInstanceState
.getParcelable(FileActivity
.EXTRA_FILE
));
142 mAccount
= savedInstanceState
.getParcelable(FileActivity
.EXTRA_ACCOUNT
);
145 if(getFile() != null
&& mAccount
!= null
) {
146 mLayout
= R
.layout
.file_details_fragment
;
150 //view = inflater.inflate(mLayout, container, false);
151 view
= inflater
.inflate(mLayout
, null
);
154 if (mLayout
== R
.layout
.file_details_fragment
) {
155 mView
.findViewById(R
.id
.fdKeepInSync
).setOnClickListener(this);
156 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.fdProgressBar
);
157 mProgressListener
= new ProgressListener(progressBar
);
158 mView
.findViewById(R
.id
.fdCancelBtn
).setOnClickListener(this);
161 updateFileDetails(false
, false
);
169 public void onAttach(Activity activity
) {
170 super.onAttach(activity
);
172 mContainerActivity
= (ContainerActivity
) activity
;
174 } catch (ClassCastException e
) {
175 throw new ClassCastException(activity
.toString() + " must implement " + FileDetailFragment
.ContainerActivity
.class.getSimpleName());
184 public void onActivityCreated(Bundle savedInstanceState
) {
185 super.onActivityCreated(savedInstanceState
);
186 if (mAccount
!= null
) {
187 mStorageManager
= new FileDataStorageManager(mAccount
, getActivity().getApplicationContext().getContentResolver());
193 public void onSaveInstanceState(Bundle outState
) {
194 super.onSaveInstanceState(outState
);
195 outState
.putParcelable(FileActivity
.EXTRA_FILE
, getFile());
196 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
200 public void onStart() {
202 listenForTransferProgress();
206 public void onResume() {
208 mUploadFinishReceiver
= new UploadFinishReceiver();
209 IntentFilter filter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
210 getActivity().registerReceiver(mUploadFinishReceiver
, filter
);
216 public void onPause() {
218 if (mUploadFinishReceiver
!= null
) {
219 getActivity().unregisterReceiver(mUploadFinishReceiver
);
220 mUploadFinishReceiver
= null
;
226 public void onStop() {
228 leaveTransferProgress();
233 public View
getView() {
234 return super.getView() == null ? mView
: super.getView();
242 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
243 super.onCreateOptionsMenu(menu
, inflater
);
244 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
245 MenuItem item
= menu
.findItem(R
.id
.action_see_details
);
247 item
.setVisible(false
);
248 item
.setEnabled(false
);
257 public void onPrepareOptionsMenu (Menu menu
) {
258 super.onPrepareOptionsMenu(menu
);
260 List
<Integer
> toHide
= new ArrayList
<Integer
>();
261 List
<Integer
> toShow
= new ArrayList
<Integer
>();
262 OCFile file
= getFile();
264 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
265 boolean downloading
= downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
);
266 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
267 boolean uploading
= uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile());
269 if (downloading
|| uploading
) {
270 toHide
.add(R
.id
.action_download_file
);
271 toHide
.add(R
.id
.action_rename_file
);
272 toHide
.add(R
.id
.action_remove_file
);
273 toHide
.add(R
.id
.action_open_file_with
);
275 toHide
.add(R
.id
.action_cancel_download
);
276 toShow
.add(R
.id
.action_cancel_upload
);
278 toHide
.add(R
.id
.action_cancel_upload
);
279 toShow
.add(R
.id
.action_cancel_download
);
282 } else if (file
!= null
&& file
.isDown()) {
283 toHide
.add(R
.id
.action_download_file
);
284 toHide
.add(R
.id
.action_cancel_download
);
285 toHide
.add(R
.id
.action_cancel_upload
);
287 toShow
.add(R
.id
.action_rename_file
);
288 toShow
.add(R
.id
.action_remove_file
);
289 toShow
.add(R
.id
.action_open_file_with
);
290 toShow
.add(R
.id
.action_sync_file
);
292 } else if (file
!= null
) {
293 toHide
.add(R
.id
.action_open_file_with
);
294 toHide
.add(R
.id
.action_cancel_download
);
295 toHide
.add(R
.id
.action_cancel_upload
);
296 toHide
.add(R
.id
.action_sync_file
);
298 toShow
.add(R
.id
.action_rename_file
);
299 toShow
.add(R
.id
.action_remove_file
);
300 toShow
.add(R
.id
.action_download_file
);
303 toHide
.add(R
.id
.action_open_file_with
);
304 toHide
.add(R
.id
.action_cancel_download
);
305 toHide
.add(R
.id
.action_cancel_upload
);
306 toHide
.add(R
.id
.action_sync_file
);
307 toHide
.add(R
.id
.action_download_file
);
308 toHide
.add(R
.id
.action_rename_file
);
309 toHide
.add(R
.id
.action_remove_file
);
313 MenuItem item
= null
;
314 for (int i
: toHide
) {
315 item
= menu
.findItem(i
);
317 item
.setVisible(false
);
318 item
.setEnabled(false
);
321 for (int i
: toShow
) {
322 item
= menu
.findItem(i
);
324 item
.setVisible(true
);
325 item
.setEnabled(true
);
335 public boolean onOptionsItemSelected(MenuItem item
) {
336 switch (item
.getItemId()) {
337 case R
.id
.action_open_file_with
: {
338 mContainerActivity
.openFile(getFile());
341 case R
.id
.action_remove_file
: {
345 case R
.id
.action_rename_file
: {
349 case R
.id
.action_download_file
:
350 case R
.id
.action_cancel_download
:
351 case R
.id
.action_cancel_upload
:
352 case R
.id
.action_sync_file
: {
362 public void onClick(View v
) {
364 case R
.id
.fdKeepInSync
: {
368 case R
.id
.fdCancelBtn
: {
373 Log_OC
.e(TAG
, "Incorrect view clicked!");
378 private void toggleKeepInSync() {
379 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
380 OCFile file
= getFile();
381 file
.setKeepInSync(cb
.isChecked());
382 mStorageManager
.saveFile(file
);
384 /// register the OCFile instance in the observer service to monitor local updates;
385 /// if necessary, the file is download
386 Intent intent
= new Intent(getActivity().getApplicationContext(),
387 FileObserverService
.class);
388 intent
.putExtra(FileObserverService
.KEY_FILE_CMD
,
390 FileObserverService
.CMD_ADD_OBSERVED_FILE
:
391 FileObserverService
.CMD_DEL_OBSERVED_FILE
));
392 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_FILE
, file
);
393 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_ACCOUNT
, mAccount
);
394 getActivity().startService(intent
);
396 if (file
.keepInSync()) {
397 synchronizeFile(); // force an immediate synchronization
402 private void removeFile() {
403 OCFile file
= getFile();
404 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
405 R
.string
.confirmation_remove_alert
,
406 new String
[]{file
.getFileName()},
407 file
.isDown() ? R
.string
.confirmation_remove_remote_and_local
: R
.string
.confirmation_remove_remote
,
408 file
.isDown() ? R
.string
.confirmation_remove_local
: -1,
409 R
.string
.common_cancel
);
410 confDialog
.setOnConfirmationListener(this);
411 confDialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
415 private void renameFile() {
416 OCFile file
= getFile();
417 String fileName
= file
.getFileName();
418 int extensionStart
= file
.isDirectory() ?
-1 : fileName
.lastIndexOf(".");
419 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
420 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
421 dialog
.show(getFragmentManager(), "nameeditdialog");
424 private void synchronizeFile() {
425 OCFile file
= getFile();
426 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
427 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
428 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
429 downloaderBinder
.cancel(mAccount
, file
);
433 setButtonsForRemote();
436 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
437 uploaderBinder
.cancel(mAccount
, file
);
438 if (!file
.fileExists()) {
439 // TODO make something better
440 ((FileDisplayActivity
)getActivity()).cleanSecondFragment();
442 } else if (file
.isDown()) {
445 setButtonsForRemote();
449 mLastRemoteOperation
= new SynchronizeFileOperation(file
, null
, mStorageManager
, mAccount
, true
, false
, getActivity());
450 mLastRemoteOperation
.execute(mAccount
, getSherlockActivity(), this, mHandler
, getSherlockActivity());
453 //boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
454 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
460 public void onConfirmation(String callerTag
) {
461 OCFile file
= getFile();
462 if (callerTag
.equals(FTAG_CONFIRMATION
)) {
463 if (mStorageManager
.getFileById(file
.getFileId()) != null
) {
464 mLastRemoteOperation
= new RemoveFileOperation( file
,
467 mLastRemoteOperation
.execute(mAccount
, getSherlockActivity(), this, mHandler
, getSherlockActivity());
469 //boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
470 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
476 public void onNeutral(String callerTag
) {
478 OCFile file
= getFile();
479 if (file
.isDown() && (f
= new File(file
.getStoragePath())).exists()) {
481 file
.setStoragePath(null
);
482 mStorageManager
.saveFile(file
);
483 updateFileDetails(file
, mAccount
);
488 public void onCancel(String callerTag
) {
489 Log_OC
.d(TAG
, "REMOVAL CANCELED");
494 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
496 * @return True when the fragment was created with the empty layout.
498 public boolean isEmpty() {
499 return (mLayout
== R
.layout
.file_details_empty
|| getFile() == null
|| mAccount
== null
);
504 * Use this method to signal this Activity that it shall update its view.
506 * @param file : An {@link OCFile}
508 public void updateFileDetails(OCFile file
, Account ocAccount
) {
510 if (ocAccount
!= null
&& (
511 mStorageManager
== null
||
512 (mAccount
!= null
&& !mAccount
.equals(ocAccount
))
514 mStorageManager
= new FileDataStorageManager(ocAccount
, getActivity().getApplicationContext().getContentResolver());
516 mAccount
= ocAccount
;
517 updateFileDetails(false
, false
);
521 * Updates the view with all relevant details about that file.
523 * TODO Remove parameter when the transferring state of files is kept in database.
525 * TODO REFACTORING! this method called 5 times before every time the fragment is shown!
527 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
528 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
529 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
531 * @param refresh If 'true', try to refresh the hold file from the database
533 public void updateFileDetails(boolean transferring
, boolean refresh
) {
537 if (refresh
&& mStorageManager
!= null
) {
538 setFile(mStorageManager
.getFileByPath(getFile().getRemotePath()));
540 OCFile file
= getFile();
543 setFilename(file
.getFileName());
544 setFiletype(file
.getMimetype());
545 setFilesize(file
.getFileLength());
546 if(ocVersionSupportsTimeCreated()){
547 setTimeCreated(file
.getCreationTimestamp());
550 setTimeModified(file
.getModificationTimestamp());
552 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
553 cb
.setChecked(file
.keepInSync());
555 // configure UI for depending upon local state of the file
556 //if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) || FileUploader.isUploading(mAccount, mFile.getRemotePath())) {
557 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
558 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
559 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) || (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
))) {
560 setButtonsForTransferring();
562 } else if (file
.isDown()) {
567 // TODO load default preview image; when the local file is removed, the preview remains there
568 setButtonsForRemote();
571 getView().invalidate();
575 * Checks if the fragment is ready to show details of a OCFile
577 * @return 'True' when the fragment is ready to show details of a file
579 private boolean readyToShow() {
580 return (getFile() != null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
);
585 * Updates the filename in view
586 * @param filename to set
588 private void setFilename(String filename
) {
589 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
591 tv
.setText(filename
);
595 * Updates the MIME type in view
596 * @param mimetype to set
598 private void setFiletype(String mimetype
) {
599 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
601 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);;
602 tv
.setText(printableMimetype
);
604 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
606 iv
.setImageResource(DisplayUtils
.getResourceId(mimetype
));
611 * Updates the file size in view
612 * @param filesize in bytes to set
614 private void setFilesize(long filesize
) {
615 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
617 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
621 * Updates the time that the file was created in view
622 * @param milliseconds Unix time to set
624 private void setTimeCreated(long milliseconds
){
625 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
626 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
628 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
629 tv
.setVisibility(View
.VISIBLE
);
630 tvLabel
.setVisibility(View
.VISIBLE
);
635 * Updates the time that the file was last modified
636 * @param milliseconds Unix time to set
638 private void setTimeModified(long milliseconds
){
639 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
641 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
646 * Enables or disables buttons for a file being downloaded
648 private void setButtonsForTransferring() {
650 // let's protect the user from himself ;)
651 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(false
);
653 // show the progress bar for the transfer
654 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.VISIBLE
);
655 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
656 progressText
.setVisibility(View
.VISIBLE
);
657 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
658 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
659 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
660 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
661 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile())) {
662 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
668 * Enables or disables buttons for a file locally available
670 private void setButtonsForDown() {
672 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
674 // hides the progress bar
675 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
676 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
677 progressText
.setVisibility(View
.GONE
);
682 * Enables or disables buttons for a file not locally available
684 private void setButtonsForRemote() {
686 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
688 // hides the progress bar
689 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
690 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
691 progressText
.setVisibility(View
.GONE
);
697 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
698 * the time that the file was created. There is a chance that this will
699 * be fixed in future versions. Use this method to check if this version of
700 * ownCloud has this fix.
701 * @return True, if ownCloud the ownCloud version is supporting creation time
703 private boolean ocVersionSupportsTimeCreated(){
704 /*if(mAccount != null){
705 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
706 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
707 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
708 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
717 * Once the file upload has finished -> update view
719 * Being notified about the finish of an upload is necessary for the next sequence:
720 * 1. Upload a big file.
721 * 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
722 * of its containing folder; the the server includes it in the PROPFIND requests although it's not fully upload.
723 * 3. Click the file in the list to see its details.
724 * 4. Wait for the upload finishes; at this moment, the details view must be refreshed to enable the action buttons.
726 private class UploadFinishReceiver
extends BroadcastReceiver
{
728 public void onReceive(Context context
, Intent intent
) {
729 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
731 if (!isEmpty() && accountName
.equals(mAccount
.name
)) {
732 boolean uploadWasFine
= intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
, false
);
733 String uploadRemotePath
= intent
.getStringExtra(FileUploader
.EXTRA_REMOTE_PATH
);
734 boolean renamedInUpload
= getFile().getRemotePath().equals(intent
.getStringExtra(FileUploader
.EXTRA_OLD_REMOTE_PATH
));
735 if (getFile().getRemotePath().equals(uploadRemotePath
) ||
738 setFile(mStorageManager
.getFileByPath(uploadRemotePath
));
740 if (renamedInUpload
) {
741 String newName
= (new File(uploadRemotePath
)).getName();
742 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), String
.format(getString(R
.string
.filedetails_renamed_in_upload_msg
), newName
), Toast
.LENGTH_LONG
);
745 getSherlockActivity().removeStickyBroadcast(intent
); // not the best place to do this; a small refactorization of BroadcastReceivers should be done
746 updateFileDetails(false
, false
); // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server
753 public void onDismiss(EditNameDialog dialog
) {
754 if (dialog
.getResult()) {
755 String newFilename
= dialog
.getNewFilename();
756 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
757 mLastRemoteOperation
= new RenameFileOperation( getFile(),
760 new FileDataStorageManager(mAccount
, getActivity().getContentResolver()));
761 mLastRemoteOperation
.execute(mAccount
, getSherlockActivity(), this, mHandler
, getSherlockActivity());
762 //boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
763 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
772 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
773 if (operation
.equals(mLastRemoteOperation
)) {
774 if (operation
instanceof RemoveFileOperation
) {
775 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
777 } else if (operation
instanceof RenameFileOperation
) {
778 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
780 } else if (operation
instanceof SynchronizeFileOperation
) {
781 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
787 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
788 ((FileDisplayActivity
) getActivity()).dismissLoadingDialog();
789 if (result
.isSuccess()) {
790 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
792 ((FileDisplayActivity
)getActivity()).cleanSecondFragment();
795 Toast msg
= Toast
.makeText(getActivity(), R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
797 if (result
.isSslRecoverableException()) {
798 // TODO show the SSL warning dialog
803 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
804 ((FileDisplayActivity
) getActivity()).dismissLoadingDialog();
806 if (result
.isSuccess()) {
807 updateFileDetails(((RenameFileOperation
)operation
).getFile(), mAccount
);
808 mContainerActivity
.onFileStateChanged();
811 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
812 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
814 // TODO throw again the new rename dialog
816 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
818 if (result
.isSslRecoverableException()) {
819 // TODO show the SSL warning dialog
825 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
, RemoteOperationResult result
) {
826 ((FileDisplayActivity
) getActivity()).dismissLoadingDialog();
827 OCFile file
= getFile();
828 if (!result
.isSuccess()) {
829 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
830 Intent i
= new Intent(getActivity(), ConflictsResolveActivity
.class);
831 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, file
);
832 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, mAccount
);
836 Toast msg
= Toast
.makeText(getActivity(), R
.string
.sync_file_fail_msg
, Toast
.LENGTH_LONG
);
844 setButtonsForRemote();
848 if (operation
.transferWasRequested()) {
849 setButtonsForTransferring();
850 mContainerActivity
.onFileStateChanged(); // this is not working; FileDownloader won't do NOTHING at all until this method finishes, so
851 // checking the service to see if the file is downloading results in FALSE
853 Toast msg
= Toast
.makeText(getActivity(), R
.string
.sync_file_nothing_to_do_msg
, Toast
.LENGTH_LONG
);
859 setButtonsForRemote();
866 public void listenForTransferProgress() {
867 if (mProgressListener
!= null
) {
868 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
869 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
871 if (mContainerActivity
.getFileUploaderBinder() != null
) {
872 mContainerActivity
.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
878 public void leaveTransferProgress() {
879 if (mProgressListener
!= null
) {
880 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
881 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
883 if (mContainerActivity
.getFileUploaderBinder() != null
) {
884 mContainerActivity
.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
892 * Helper class responsible for updating the progress bar shown for file uploading or downloading
894 * @author David A. Velasco
896 private class ProgressListener
implements OnDatatransferProgressListener
{
897 int mLastPercent
= 0;
898 WeakReference
<ProgressBar
> mProgressBar
= null
;
900 ProgressListener(ProgressBar progressBar
) {
901 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
905 public void onTransferProgress(long progressRate
) {
906 // old method, nothing here
910 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
911 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
912 if (percent
!= mLastPercent
) {
913 ProgressBar pb
= mProgressBar
.get();
915 pb
.setProgress(percent
);
919 mLastPercent
= percent
;