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());
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
);
262 public void onPrepareOptionsMenu (Menu menu
) {
263 super.onPrepareOptionsMenu(menu
);
265 List
<Integer
> toHide
= new ArrayList
<Integer
>();
266 List
<Integer
> toShow
= new ArrayList
<Integer
>();
267 OCFile file
= getFile();
269 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
270 boolean downloading
= downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
);
271 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
272 boolean uploading
= uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile());
274 if (downloading
|| uploading
) {
275 toHide
.add(R
.id
.action_download_file
);
276 toHide
.add(R
.id
.action_rename_file
);
277 toHide
.add(R
.id
.action_remove_file
);
278 toHide
.add(R
.id
.action_open_file_with
);
280 toHide
.add(R
.id
.action_cancel_download
);
281 toShow
.add(R
.id
.action_cancel_upload
);
283 toHide
.add(R
.id
.action_cancel_upload
);
284 toShow
.add(R
.id
.action_cancel_download
);
287 } else if (file
!= null
&& file
.isDown()) {
288 toHide
.add(R
.id
.action_download_file
);
289 toHide
.add(R
.id
.action_cancel_download
);
290 toHide
.add(R
.id
.action_cancel_upload
);
292 toShow
.add(R
.id
.action_rename_file
);
293 toShow
.add(R
.id
.action_remove_file
);
294 toShow
.add(R
.id
.action_open_file_with
);
295 toShow
.add(R
.id
.action_sync_file
);
297 } else if (file
!= null
) {
298 toHide
.add(R
.id
.action_open_file_with
);
299 toHide
.add(R
.id
.action_cancel_download
);
300 toHide
.add(R
.id
.action_cancel_upload
);
301 toHide
.add(R
.id
.action_sync_file
);
303 toShow
.add(R
.id
.action_rename_file
);
304 toShow
.add(R
.id
.action_remove_file
);
305 toShow
.add(R
.id
.action_download_file
);
308 toHide
.add(R
.id
.action_open_file_with
);
309 toHide
.add(R
.id
.action_cancel_download
);
310 toHide
.add(R
.id
.action_cancel_upload
);
311 toHide
.add(R
.id
.action_sync_file
);
312 toHide
.add(R
.id
.action_download_file
);
313 toHide
.add(R
.id
.action_rename_file
);
314 toHide
.add(R
.id
.action_remove_file
);
319 if (!file
.isShareByLink()) {
320 toHide
.add(R
.id
.action_unshare_file
);
322 toShow
.add(R
.id
.action_unshare_file
);
325 MenuItem item
= null
;
326 for (int i
: toHide
) {
327 item
= menu
.findItem(i
);
329 item
.setVisible(false
);
330 item
.setEnabled(false
);
333 for (int i
: toShow
) {
334 item
= menu
.findItem(i
);
336 item
.setVisible(true
);
337 item
.setEnabled(true
);
347 public boolean onOptionsItemSelected(MenuItem item
) {
348 switch (item
.getItemId()) {
349 case R
.id
.action_share_file
: {
350 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
351 activity
.getFileOperationsHelper().shareFileWithLink(getFile(), activity
);
354 case R
.id
.action_unshare_file
: {
355 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
356 activity
.getFileOperationsHelper().unshareFileWithLink(getFile(), activity
);
359 case R
.id
.action_open_file_with
: {
360 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
361 activity
.getFileOperationsHelper().openFile(getFile(), activity
);
364 case R
.id
.action_remove_file
: {
368 case R
.id
.action_rename_file
: {
372 case R
.id
.action_download_file
:
373 case R
.id
.action_cancel_download
:
374 case R
.id
.action_cancel_upload
:
375 case R
.id
.action_sync_file
: {
379 case R
.id
.action_send_file
: {
380 FileDisplayActivity activity
= (FileDisplayActivity
) getSherlockActivity();
382 if (!getFile().isDown()) { // Download the file
383 //activity.showLoadingDialog();
384 Log_OC
.d(TAG
, getFile().getRemotePath() + " : File must be downloaded");
385 activity
.startDownloadForSending(getFile());
388 activity
.getFileOperationsHelper().sendDownloadedFile(getFile(), activity
);
398 public void onClick(View v
) {
400 case R
.id
.fdKeepInSync
: {
404 case R
.id
.fdCancelBtn
: {
409 Log_OC
.e(TAG
, "Incorrect view clicked!");
414 private void toggleKeepInSync() {
415 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
416 OCFile file
= getFile();
417 file
.setKeepInSync(cb
.isChecked());
418 mStorageManager
.saveFile(file
);
420 /// register the OCFile instance in the observer service to monitor local updates;
421 /// if necessary, the file is download
422 Intent intent
= new Intent(getActivity().getApplicationContext(),
423 FileObserverService
.class);
424 intent
.putExtra(FileObserverService
.KEY_FILE_CMD
,
426 FileObserverService
.CMD_ADD_OBSERVED_FILE
:
427 FileObserverService
.CMD_DEL_OBSERVED_FILE
));
428 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_FILE
, file
);
429 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_ACCOUNT
, mAccount
);
430 getActivity().startService(intent
);
432 if (file
.keepInSync()) {
433 synchronizeFile(); // force an immediate synchronization
437 private void removeFile() {
438 OCFile file
= getFile();
439 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
440 R
.string
.confirmation_remove_alert
,
441 new String
[]{file
.getFileName()},
442 file
.isDown() ? R
.string
.confirmation_remove_remote_and_local
: R
.string
.confirmation_remove_remote
,
443 file
.isDown() ? R
.string
.confirmation_remove_local
: -1,
444 R
.string
.common_cancel
);
445 confDialog
.setOnConfirmationListener(this);
446 confDialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
450 private void renameFile() {
451 OCFile file
= getFile();
452 String fileName
= file
.getFileName();
453 int extensionStart
= file
.isFolder() ?
-1 : fileName
.lastIndexOf(".");
454 int selectionEnd
= (extensionStart
>= 0) ? extensionStart
: fileName
.length();
455 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), fileName
, 0, selectionEnd
, this);
456 dialog
.show(getFragmentManager(), "nameeditdialog");
459 private void synchronizeFile() {
460 OCFile file
= getFile();
461 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
462 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
463 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) {
464 downloaderBinder
.cancel(mAccount
, file
);
468 setButtonsForRemote();
471 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
)) {
472 uploaderBinder
.cancel(mAccount
, file
);
473 if (!file
.fileExists()) {
474 // TODO make something better
475 ((FileDisplayActivity
)getActivity()).cleanSecondFragment();
477 } else if (file
.isDown()) {
480 setButtonsForRemote();
484 mLastRemoteOperation
= new SynchronizeFileOperation(file
, null
, mStorageManager
, mAccount
, true
, getActivity());
485 mLastRemoteOperation
.execute(mAccount
, getSherlockActivity(), this, mHandler
, getSherlockActivity());
488 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
494 public void onConfirmation(String callerTag
) {
495 OCFile file
= getFile();
496 if (callerTag
.equals(FTAG_CONFIRMATION
)) {
497 if (mStorageManager
.getFileById(file
.getFileId()) != null
) {
498 mLastRemoteOperation
= new RemoveFileOperation( file
,
501 mLastRemoteOperation
.execute(mAccount
, getSherlockActivity(), this, mHandler
, getSherlockActivity());
503 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
509 public void onNeutral(String callerTag
) {
510 OCFile file
= getFile();
511 mStorageManager
.removeFile(file
, false
, true
); // TODO perform in background task / new thread
512 if (file
.getStoragePath() != null
) {
513 file
.setStoragePath(null
);
514 updateFileDetails(file
, mAccount
);
519 public void onCancel(String callerTag
) {
520 Log_OC
.d(TAG
, "REMOVAL CANCELED");
525 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
527 * @return True when the fragment was created with the empty layout.
529 public boolean isEmpty() {
530 return (mLayout
== R
.layout
.file_details_empty
|| getFile() == null
|| mAccount
== null
);
535 * Use this method to signal this Activity that it shall update its view.
537 * @param file : An {@link OCFile}
539 public void updateFileDetails(OCFile file
, Account ocAccount
) {
541 if (ocAccount
!= null
&& (
542 mStorageManager
== null
||
543 (mAccount
!= null
&& !mAccount
.equals(ocAccount
))
545 mStorageManager
= new FileDataStorageManager(ocAccount
, getActivity().getApplicationContext().getContentResolver());
547 mAccount
= ocAccount
;
548 updateFileDetails(false
, false
);
552 * Updates the view with all relevant details about that file.
554 * TODO Remove parameter when the transferring state of files is kept in database.
556 * TODO REFACTORING! this method called 5 times before every time the fragment is shown!
558 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
559 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
560 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
562 * @param refresh If 'true', try to refresh the hold file from the database
564 public void updateFileDetails(boolean transferring
, boolean refresh
) {
568 if (refresh
&& mStorageManager
!= null
) {
569 setFile(mStorageManager
.getFileByPath(getFile().getRemotePath()));
571 OCFile file
= getFile();
574 setFilename(file
.getFileName());
575 setFiletype(file
.getMimetype());
576 setFilesize(file
.getFileLength());
577 if(ocVersionSupportsTimeCreated()){
578 setTimeCreated(file
.getCreationTimestamp());
581 setTimeModified(file
.getModificationTimestamp());
583 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
584 cb
.setChecked(file
.keepInSync());
586 // configure UI for depending upon local state of the file
587 //if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) || FileUploader.isUploading(mAccount, mFile.getRemotePath())) {
588 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
589 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
590 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, file
)) || (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, file
))) {
591 setButtonsForTransferring();
593 } else if (file
.isDown()) {
598 // TODO load default preview image; when the local file is removed, the preview remains there
599 setButtonsForRemote();
602 getView().invalidate();
606 * Checks if the fragment is ready to show details of a OCFile
608 * @return 'True' when the fragment is ready to show details of a file
610 private boolean readyToShow() {
611 return (getFile() != null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
);
616 * Updates the filename in view
617 * @param filename to set
619 private void setFilename(String filename
) {
620 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
622 tv
.setText(filename
);
626 * Updates the MIME type in view
627 * @param mimetype to set
629 private void setFiletype(String mimetype
) {
630 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
632 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);;
633 tv
.setText(printableMimetype
);
635 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
637 iv
.setImageResource(DisplayUtils
.getResourceId(mimetype
));
642 * Updates the file size in view
643 * @param filesize in bytes to set
645 private void setFilesize(long filesize
) {
646 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
648 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
652 * Updates the time that the file was created in view
653 * @param milliseconds Unix time to set
655 private void setTimeCreated(long milliseconds
){
656 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
657 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
659 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
660 tv
.setVisibility(View
.VISIBLE
);
661 tvLabel
.setVisibility(View
.VISIBLE
);
666 * Updates the time that the file was last modified
667 * @param milliseconds Unix time to set
669 private void setTimeModified(long milliseconds
){
670 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
672 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
677 * Enables or disables buttons for a file being downloaded
679 private void setButtonsForTransferring() {
681 // let's protect the user from himself ;)
682 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(false
);
684 // show the progress bar for the transfer
685 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.VISIBLE
);
686 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
687 progressText
.setVisibility(View
.VISIBLE
);
688 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
689 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
690 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
691 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
692 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, getFile())) {
693 progressText
.setText(R
.string
.uploader_upload_in_progress_ticker
);
699 * Enables or disables buttons for a file locally available
701 private void setButtonsForDown() {
703 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
705 // hides the progress bar
706 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
707 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
708 progressText
.setVisibility(View
.GONE
);
713 * Enables or disables buttons for a file not locally available
715 private void setButtonsForRemote() {
717 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
719 // hides the progress bar
720 getView().findViewById(R
.id
.fdProgressBlock
).setVisibility(View
.GONE
);
721 TextView progressText
= (TextView
)getView().findViewById(R
.id
.fdProgressText
);
722 progressText
.setVisibility(View
.GONE
);
728 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
729 * the time that the file was created. There is a chance that this will
730 * be fixed in future versions. Use this method to check if this version of
731 * ownCloud has this fix.
732 * @return True, if ownCloud the ownCloud version is supporting creation time
734 private boolean ocVersionSupportsTimeCreated(){
735 /*if(mAccount != null){
736 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
737 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
738 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
739 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
748 * Once the file upload has finished -> update view
750 * Being notified about the finish of an upload is necessary for the next sequence:
751 * 1. Upload a big file.
752 * 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
753 * of its containing folder; the the server includes it in the PROPFIND requests although it's not fully upload.
754 * 3. Click the file in the list to see its details.
755 * 4. Wait for the upload finishes; at this moment, the details view must be refreshed to enable the action buttons.
757 private class UploadFinishReceiver
extends BroadcastReceiver
{
759 public void onReceive(Context context
, Intent intent
) {
760 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
762 if (!isEmpty() && accountName
.equals(mAccount
.name
)) {
763 boolean uploadWasFine
= intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
, false
);
764 String uploadRemotePath
= intent
.getStringExtra(FileUploader
.EXTRA_REMOTE_PATH
);
765 boolean renamedInUpload
= getFile().getRemotePath().equals(intent
.getStringExtra(FileUploader
.EXTRA_OLD_REMOTE_PATH
));
766 if (getFile().getRemotePath().equals(uploadRemotePath
) ||
769 setFile(mStorageManager
.getFileByPath(uploadRemotePath
));
771 if (renamedInUpload
) {
772 String newName
= (new File(uploadRemotePath
)).getName();
773 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), String
.format(getString(R
.string
.filedetails_renamed_in_upload_msg
), newName
), Toast
.LENGTH_LONG
);
776 getSherlockActivity().removeStickyBroadcast(intent
); // not the best place to do this; a small refactorization of BroadcastReceivers should be done
778 updateFileDetails(false
, false
); // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server
780 // Force the preview if the file is an image
781 if (uploadWasFine
&& PreviewImageFragment
.canBePreviewed(getFile())) {
782 ((FileDisplayActivity
) mContainerActivity
).startImagePreview(getFile());
790 public void onDismiss(EditNameDialog dialog
) {
791 if (dialog
.getResult()) {
792 String newFilename
= dialog
.getNewFilename();
793 Log_OC
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
794 mLastRemoteOperation
= new RenameFileOperation( getFile(),
797 new FileDataStorageManager(mAccount
, getActivity().getContentResolver()));
798 mLastRemoteOperation
.execute(mAccount
, getSherlockActivity(), this, mHandler
, getSherlockActivity());
799 ((FileDisplayActivity
) getActivity()).showLoadingDialog();
808 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
809 if (operation
.equals(mLastRemoteOperation
)) {
810 if (operation
instanceof RemoveFileOperation
) {
811 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
813 } else if (operation
instanceof RenameFileOperation
) {
814 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
816 } else if (operation
instanceof SynchronizeFileOperation
) {
817 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
823 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
824 ((FileDisplayActivity
) getActivity()).dismissLoadingDialog();
825 if (result
.isSuccess()) {
826 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
828 ((FileDisplayActivity
)getActivity()).cleanSecondFragment();
831 Toast msg
= Toast
.makeText(getActivity(), R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
833 if (result
.isSslRecoverableException()) {
834 // TODO show the SSL warning dialog
839 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
840 ((FileDisplayActivity
) getActivity()).dismissLoadingDialog();
842 if (result
.isSuccess()) {
843 updateFileDetails(((RenameFileOperation
)operation
).getFile(), mAccount
);
844 mContainerActivity
.onFileStateChanged();
847 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
848 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
850 // TODO throw again the new rename dialog
851 } if (result
.getCode().equals(ResultCode
.INVALID_CHARACTER_IN_NAME
)) {
852 Toast msg
= Toast
.makeText(getActivity(), R
.string
.filename_forbidden_characters
, Toast
.LENGTH_LONG
);
855 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
857 if (result
.isSslRecoverableException()) {
858 // TODO show the SSL warning dialog
864 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
, RemoteOperationResult result
) {
865 ((FileDisplayActivity
) getActivity()).dismissLoadingDialog();
866 OCFile file
= getFile();
867 if (!result
.isSuccess()) {
868 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
869 Intent i
= new Intent(getActivity(), ConflictsResolveActivity
.class);
870 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, file
);
871 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, mAccount
);
880 setButtonsForRemote();
884 if (operation
.transferWasRequested()) {
885 setButtonsForTransferring();
886 mContainerActivity
.onFileStateChanged(); // this is not working; FileDownloader won't do NOTHING at all until this method finishes, so
887 // checking the service to see if the file is downloading results in FALSE
889 Toast msg
= Toast
.makeText(getActivity(), R
.string
.sync_file_nothing_to_do_msg
, Toast
.LENGTH_LONG
);
895 setButtonsForRemote();
902 public void listenForTransferProgress() {
903 if (mProgressListener
!= null
) {
904 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
905 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
907 if (mContainerActivity
.getFileUploaderBinder() != null
) {
908 mContainerActivity
.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
914 public void leaveTransferProgress() {
915 if (mProgressListener
!= null
) {
916 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
917 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
919 if (mContainerActivity
.getFileUploaderBinder() != null
) {
920 mContainerActivity
.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
928 * Helper class responsible for updating the progress bar shown for file uploading or downloading
930 * @author David A. Velasco
932 private class ProgressListener
implements OnDatatransferProgressListener
{
933 int mLastPercent
= 0;
934 WeakReference
<ProgressBar
> mProgressBar
= null
;
936 ProgressListener(ProgressBar progressBar
) {
937 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
941 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
942 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
943 if (percent
!= mLastPercent
) {
944 ProgressBar pb
= mProgressBar
.get();
946 pb
.setProgress(percent
);
950 mLastPercent
= percent
;