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
.common
.network
.OnDatatransferProgressListener
;
54 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
55 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
56 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
57 import com
.owncloud
.android
.lib
.common
.operations
.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());
189 OCFile file
= mStorageManager
.getFileByPath(getFile().getRemotePath());
198 public void onSaveInstanceState(Bundle outState
) {
199 super.onSaveInstanceState(outState
);
200 outState
.putParcelable(FileActivity
.EXTRA_FILE
, getFile());
201 outState
.putParcelable(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
205 public void onStart() {
207 listenForTransferProgress();
211 public void onResume() {
213 mUploadFinishReceiver
= new UploadFinishReceiver();
214 IntentFilter filter
= new IntentFilter(FileUploader
.getUploadFinishMessage());
215 getActivity().registerReceiver(mUploadFinishReceiver
, filter
);
221 public void onPause() {
223 if (mUploadFinishReceiver
!= null
) {
224 getActivity().unregisterReceiver(mUploadFinishReceiver
);
225 mUploadFinishReceiver
= null
;
231 public void onStop() {
233 leaveTransferProgress();
238 public View
getView() {
239 return super.getView() == null ? mView
: super.getView();
247 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
248 super.onCreateOptionsMenu(menu
, inflater
);
249 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
250 MenuItem item
= menu
.findItem(R
.id
.action_see_details
);
252 item
.setVisible(false
);
253 item
.setEnabled(false
);
257 item
= menu
.findItem(R
.id
.action_send_file
);
258 boolean sendEnabled
= getString(R
.string
.send_files_to_other_apps
).equalsIgnoreCase("on");
259 if (item
!= null
&& sendEnabled
) {
260 item
.setVisible(true
);
261 item
.setEnabled(true
);
263 item
.setVisible(false
);
264 item
.setEnabled(false
);
273 public void onPrepareOptionsMenu (Menu menu
) {
274 super.onPrepareOptionsMenu(menu
);
276 List
<Integer
> toHide
= new ArrayList
<Integer
>();
277 List
<Integer
> toShow
= new ArrayList
<Integer
>();
278 OCFile file
= getFile();
280 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
281 boolean downloading
= downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
);
282 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
283 boolean uploading
= uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile());
285 if (downloading
|| uploading
) {
286 toHide
.add(R
.id
.action_download_file
);
287 toHide
.add(R
.id
.action_rename_file
);
288 toHide
.add(R
.id
.action_remove_file
);
289 toHide
.add(R
.id
.action_open_file_with
);
291 toHide
.add(R
.id
.action_cancel_download
);
292 toShow
.add(R
.id
.action_cancel_upload
);
294 toHide
.add(R
.id
.action_cancel_upload
);
295 toShow
.add(R
.id
.action_cancel_download
);
298 } else if (file
!= null
&& file
.isDown()) {
299 toHide
.add(R
.id
.action_download_file
);
300 toHide
.add(R
.id
.action_cancel_download
);
301 toHide
.add(R
.id
.action_cancel_upload
);
303 toShow
.add(R
.id
.action_rename_file
);
304 toShow
.add(R
.id
.action_remove_file
);
305 toShow
.add(R
.id
.action_open_file_with
);
306 toShow
.add(R
.id
.action_sync_file
);
308 } else if (file
!= null
) {
309 toHide
.add(R
.id
.action_open_file_with
);
310 toHide
.add(R
.id
.action_cancel_download
);
311 toHide
.add(R
.id
.action_cancel_upload
);
312 toHide
.add(R
.id
.action_sync_file
);
314 toShow
.add(R
.id
.action_rename_file
);
315 toShow
.add(R
.id
.action_remove_file
);
316 toShow
.add(R
.id
.action_download_file
);
319 toHide
.add(R
.id
.action_open_file_with
);
320 toHide
.add(R
.id
.action_cancel_download
);
321 toHide
.add(R
.id
.action_cancel_upload
);
322 toHide
.add(R
.id
.action_sync_file
);
323 toHide
.add(R
.id
.action_download_file
);
324 toHide
.add(R
.id
.action_rename_file
);
325 toHide
.add(R
.id
.action_remove_file
);
330 if (!file
.isShareByLink()) {
331 toHide
.add(R
.id
.action_unshare_file
);
333 toShow
.add(R
.id
.action_unshare_file
);
336 MenuItem item
= null
;
337 for (int i
: toHide
) {
338 item
= menu
.findItem(i
);
340 item
.setVisible(false
);
341 item
.setEnabled(false
);
344 for (int i
: toShow
) {
345 item
= menu
.findItem(i
);
347 item
.setVisible(true
);
348 item
.setEnabled(true
);
358 public boolean onOptionsItemSelected(MenuItem item
) {
359 switch (item
.getItemId()) {
360 case R
.id
.action_share_file
: {
361 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
362 activity
.getFileOperationsHelper().shareFileWithLink(getFile(), activity
);
365 case R
.id
.action_unshare_file
: {
366 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
367 activity
.getFileOperationsHelper().unshareFileWithLink(getFile(), activity
);
370 case R
.id
.action_open_file_with
: {
371 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
372 activity
.getFileOperationsHelper().openFile(getFile(), activity
);
375 case R
.id
.action_remove_file
: {
379 case R
.id
.action_rename_file
: {
383 case R
.id
.action_download_file
:
384 case R
.id
.action_cancel_download
:
385 case R
.id
.action_cancel_upload
:
386 case R
.id
.action_sync_file
: {
390 case R
.id
.action_send_file
: {
391 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
393 if (!getFile().isDown()) { // Download the file
394 //activity.showLoadingDialog();
395 Log_OC
.d(TAG
, getFile().getRemotePath() + " : File must be downloaded");
396 activity
.startDownloadForSending(getFile());
399 activity
.getFileOperationsHelper().sendDownloadedFile(getFile(), activity
);
409 public void onClick(View v
) {
411 case R
.id
.fdKeepInSync
: {
415 case R
.id
.fdCancelBtn
: {
420 Log_OC
.e(TAG
, "Incorrect view clicked!");
425 private void toggleKeepInSync() {
426 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
427 OCFile file
= getFile();
428 file
.setKeepInSync(cb
.isChecked());
429 mStorageManager
.saveFile(file
);
431 /// register the OCFile instance in the observer service to monitor local updates;
432 /// if necessary, the file is download
433 Intent intent
= new Intent(getActivity().getApplicationContext(),
434 FileObserverService
.class);
435 intent
.putExtra(FileObserverService
.KEY_FILE_CMD
,
437 FileObserverService
.CMD_ADD_OBSERVED_FILE
:
438 FileObserverService
.CMD_DEL_OBSERVED_FILE
));
439 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_FILE
, file
);
440 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_ACCOUNT
, mAccount
);
441 getActivity().startService(intent
);
443 if (file
.keepInSync()) {
444 synchronizeFile(); // force an immediate synchronization
448 private void removeFile() {
449 OCFile file
= getFile();
450 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
451 R
.string
.confirmation_remove_alert
,
452 new String
[]{file
.getFileName()},
453 file
.isDown() ? R
.string
.confirmation_remove_remote_and_local
: R
.string
.confirmation_remove_remote
,
454 file
.isDown() ? R
.string
.confirmation_remove_local
: -1,
455 R
.string
.common_cancel
);
456 confDialog
.setOnConfirmationListener(this);
457 confDialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
461 private void renameFile() {
462 OCFile file
= getFile();
463 String fileName
= file
.getFileName();
464 int extensionStart
= file
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
465 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
466 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
467 dialog
.show(getFragmentManager(), "nameeditdialog");
470 private void synchronizeFile() {
471 OCFile file
= getFile();
472 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
473 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
474 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
475 downloaderBinder
.cancel(mAccount
, file
);
479 setButtonsForRemote();
482 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
483 uploaderBinder
.cancel(mAccount
, file
);
484 if (!file
.fileExists()) {
485 // TODO make something better
486 ((FileDisplayActivity
)getActivity()).cleanSecondFragment();
488 } else if (file
.isDown()) {
491 setButtonsForRemote();
495 mLastRemoteOperation
= new SynchronizeFileOperation(file
, null
, mStorageManager
, mAccount
, true
, getActivity());
496 mLastRemoteOperation
.execute(mAccount
, getSherlockActivity(), this, mHandler
, getSherlockActivity());
499 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
505 public void onConfirmation(String callerTag
) {
506 OCFile file
= getFile();
507 if (callerTag
.equals(FTAG_CONFIRMATION
)) {
508 if (mStorageManager
.getFileById(file
.getFileId()) != null
) {
509 mLastRemoteOperation
= new RemoveFileOperation( file
,
512 mLastRemoteOperation
.execute(mAccount
, getSherlockActivity(), this, mHandler
, getSherlockActivity());
514 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
520 public void onNeutral(String callerTag
) {
521 OCFile file
= getFile();
522 mStorageManager
.removeFile(file
, false
, true
); // TODO perform in background task / new thread
523 if (file
.getStoragePath() != null
) {
524 file
.setStoragePath(null
);
525 updateFileDetails(file
, mAccount
);
530 public void onCancel(String callerTag
) {
531 Log_OC
.d(TAG
, "REMOVAL CANCELED");
536 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
538 * @return True when the fragment was created with the empty layout.
540 public boolean isEmpty() {
541 return (mLayout
== R
.layout
.file_details_empty
|| getFile() == null
|| mAccount
== null
);
546 * Use this method to signal this Activity that it shall update its view.
548 * @param file : An {@link OCFile}
550 public void updateFileDetails(OCFile file
, Account ocAccount
) {
552 if (ocAccount
!= null
&& (
553 mStorageManager
== null
||
554 (mAccount
!= null
&& !mAccount
.equals(ocAccount
))
556 mStorageManager
= new FileDataStorageManager(ocAccount
, getActivity().getApplicationContext().getContentResolver());
558 mAccount
= ocAccount
;
559 updateFileDetails(false
, false
);
563 * Updates the view with all relevant details about that file.
565 * TODO Remove parameter when the transferring state of files is kept in database.
567 * TODO REFACTORING! this method called 5 times before every time the fragment is shown!
569 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
570 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
571 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
573 * @param refresh If 'true', try to refresh the hold file from the database
575 public void updateFileDetails(boolean transferring
, boolean refresh
) {
579 if (refresh
&& mStorageManager
!= null
) {
580 setFile(mStorageManager
.getFileByPath(getFile().getRemotePath()));
582 OCFile file
= getFile();
585 setFilename(file
.getFileName());
586 setFiletype(file
.getMimetype());
587 setFilesize(file
.getFileLength());
588 if(ocVersionSupportsTimeCreated()){
589 setTimeCreated(file
.getCreationTimestamp());
592 setTimeModified(file
.getModificationTimestamp());
594 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
595 cb
.setChecked(file
.keepInSync());
597 // configure UI for depending upon local state of the file
598 //if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) || FileUploader.isUploading(mAccount, mFile.getRemotePath())) {
599 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
600 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
601 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) || (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
))) {
602 setButtonsForTransferring();
604 } else if (file
.isDown()) {
609 // TODO load default preview image; when the local file is removed, the preview remains there
610 setButtonsForRemote();
613 getView().invalidate();
617 * Checks if the fragment is ready to show details of a OCFile
619 * @return 'True' when the fragment is ready to show details of a file
621 private boolean readyToShow() {
622 return (getFile() != null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
);
627 * Updates the filename in view
628 * @param filename to set
630 private void setFilename(String filename
) {
631 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
633 tv
.setText(filename
);
637 * Updates the MIME type in view
638 * @param mimetype to set
640 private void setFiletype(String mimetype
) {
641 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
643 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);;
644 tv
.setText(printableMimetype
);
646 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
648 iv
.setImageResource(DisplayUtils
.getResourceId(mimetype
));
653 * Updates the file size in view
654 * @param filesize in bytes to set
656 private void setFilesize(long filesize
) {
657 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
659 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
663 * Updates the time that the file was created in view
664 * @param milliseconds Unix time to set
666 private void setTimeCreated(long milliseconds
){
667 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
668 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
670 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
671 tv
.setVisibility(View
.VISIBLE
);
672 tvLabel
.setVisibility(View
.VISIBLE
);
677 * Updates the time that the file was last modified
678 * @param milliseconds Unix time to set
680 private void setTimeModified(long milliseconds
){
681 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
683 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
688 * Enables or disables buttons for a file being downloaded
690 private void setButtonsForTransferring() {
692 // let's protect the user from himself ;)
693 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(false
);
695 // show the progress bar for the transfer
696 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.VISIBLE
);
697 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
698 progressText
.setVisibility(View
.VISIBLE
);
699 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
700 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
701 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
702 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
703 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile())) {
704 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
710 * Enables or disables buttons for a file locally available
712 private void setButtonsForDown() {
714 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
716 // hides the progress bar
717 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
718 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
719 progressText
.setVisibility(View
.GONE
);
724 * Enables or disables buttons for a file not locally available
726 private void setButtonsForRemote() {
728 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
730 // hides the progress bar
731 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
732 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
733 progressText
.setVisibility(View
.GONE
);
739 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
740 * the time that the file was created. There is a chance that this will
741 * be fixed in future versions. Use this method to check if this version of
742 * ownCloud has this fix.
743 * @return True, if ownCloud the ownCloud version is supporting creation time
745 private boolean ocVersionSupportsTimeCreated(){
746 /*if(mAccount != null){
747 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
748 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
749 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
750 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
759 * Once the file upload has finished -> update view
761 * Being notified about the finish of an upload is necessary for the next sequence:
762 * 1. Upload a big file.
763 * 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
764 * of its containing folder; the the server includes it in the PROPFIND requests although it's not fully upload.
765 * 3. Click the file in the list to see its details.
766 * 4. Wait for the upload finishes; at this moment, the details view must be refreshed to enable the action buttons.
768 private class UploadFinishReceiver
extends BroadcastReceiver
{
770 public void onReceive(Context context
, Intent intent
) {
771 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
773 if (!isEmpty() && accountName
.equals(mAccount
.name
)) {
774 boolean uploadWasFine
= intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
, false
);
775 String uploadRemotePath
= intent
.getStringExtra(FileUploader
.EXTRA_REMOTE_PATH
);
776 boolean renamedInUpload
= getFile().getRemotePath().equals(intent
.getStringExtra(FileUploader
.EXTRA_OLD_REMOTE_PATH
));
777 if (getFile().getRemotePath().equals(uploadRemotePath
) ||
780 setFile(mStorageManager
.getFileByPath(uploadRemotePath
));
782 if (renamedInUpload
) {
783 String newName
= (new File(uploadRemotePath
)).getName();
784 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), String
.format(getString(R
.string
.filedetails_renamed_in_upload_msg
), newName
), Toast
.LENGTH_LONG
);
787 getSherlockActivity().removeStickyBroadcast(intent
); // not the best place to do this; a small refactorization of BroadcastReceivers should be done
789 updateFileDetails(false
, false
); // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server
791 // Force the preview if the file is an image
792 if (uploadWasFine
&& PreviewImageFragment
.canBePreviewed(getFile())) {
793 ((FileDisplayActivity
) mContainerActivity
).startImagePreview(getFile());
801 public void onDismiss(EditNameDialog dialog
) {
802 if (dialog
.getResult()) {
803 String newFilename
= dialog
.getNewFilename();
804 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
805 mLastRemoteOperation
= new RenameFileOperation( getFile(),
808 new FileDataStorageManager(mAccount
, getActivity().getContentResolver()));
809 mLastRemoteOperation
.execute(mAccount
, getSherlockActivity(), this, mHandler
, getSherlockActivity());
810 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
819 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
820 if (operation
.equals(mLastRemoteOperation
)) {
821 if (operation
instanceof RemoveFileOperation
) {
822 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
824 } else if (operation
instanceof RenameFileOperation
) {
825 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
827 } else if (operation
instanceof SynchronizeFileOperation
) {
828 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
834 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
835 ((FileDisplayActivity
) getActivity()).dismissLoadingDialog();
836 if (result
.isSuccess()) {
837 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
839 ((FileDisplayActivity
)getActivity()).cleanSecondFragment();
842 Toast msg
= Toast
.makeText(getActivity(), R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
844 if (result
.isSslRecoverableException()) {
845 // TODO show the SSL warning dialog
850 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
851 ((FileDisplayActivity
) getActivity()).dismissLoadingDialog();
853 if (result
.isSuccess()) {
854 updateFileDetails(((RenameFileOperation
)operation
).getFile(), mAccount
);
855 mContainerActivity
.onFileStateChanged();
858 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
859 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
861 // TODO throw again the new rename dialog
862 } if (result
.getCode().equals(ResultCode
.INVALID_CHARACTER_IN_NAME
)) {
863 Toast msg
= Toast
.makeText(getActivity(), R
.string
.filename_forbidden_characters
, Toast
.LENGTH_LONG
);
866 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
868 if (result
.isSslRecoverableException()) {
869 // TODO show the SSL warning dialog
875 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
, RemoteOperationResult result
) {
876 ((FileDisplayActivity
) getActivity()).dismissLoadingDialog();
877 OCFile file
= getFile();
878 if (!result
.isSuccess()) {
879 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
880 Intent i
= new Intent(getActivity(), ConflictsResolveActivity
.class);
881 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, file
);
882 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, mAccount
);
891 setButtonsForRemote();
895 if (operation
.transferWasRequested()) {
896 setButtonsForTransferring();
897 mContainerActivity
.onFileStateChanged(); // this is not working; FileDownloader won't do NOTHING at all until this method finishes, so
898 // checking the service to see if the file is downloading results in FALSE
900 Toast msg
= Toast
.makeText(getActivity(), R
.string
.sync_file_nothing_to_do_msg
, Toast
.LENGTH_LONG
);
906 setButtonsForRemote();
913 public void listenForTransferProgress() {
914 if (mProgressListener
!= null
) {
915 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
916 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
918 if (mContainerActivity
.getFileUploaderBinder() != null
) {
919 mContainerActivity
.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
925 public void leaveTransferProgress() {
926 if (mProgressListener
!= null
) {
927 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
928 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
930 if (mContainerActivity
.getFileUploaderBinder() != null
) {
931 mContainerActivity
.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
939 * Helper class responsible for updating the progress bar shown for file uploading or downloading
941 * @author David A. Velasco
943 private class ProgressListener
implements OnDatatransferProgressListener
{
944 int mLastPercent
= 0;
945 WeakReference
<ProgressBar
> mProgressBar
= null
;
947 ProgressListener(ProgressBar progressBar
) {
948 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
952 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
953 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
954 if (percent
!= mLastPercent
) {
955 ProgressBar pb
= mProgressBar
.get();
957 pb
.setProgress(percent
);
961 mLastPercent
= percent
;