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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.ui
.fragment
;
22 import java
.util
.ArrayList
;
23 import java
.util
.List
;
25 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
26 import org
.apache
.commons
.httpclient
.methods
.PostMethod
;
27 import org
.apache
.commons
.httpclient
.methods
.StringRequestEntity
;
28 import org
.apache
.commons
.httpclient
.params
.HttpConnectionManagerParams
;
29 import org
.apache
.http
.HttpStatus
;
30 import org
.apache
.http
.NameValuePair
;
31 import org
.apache
.http
.client
.utils
.URLEncodedUtils
;
32 import org
.apache
.http
.message
.BasicNameValuePair
;
33 import org
.apache
.http
.protocol
.HTTP
;
34 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
35 import org
.json
.JSONObject
;
37 import android
.accounts
.Account
;
38 import android
.accounts
.AccountManager
;
39 import android
.annotation
.SuppressLint
;
40 import android
.app
.Activity
;
41 import android
.content
.ActivityNotFoundException
;
42 import android
.content
.BroadcastReceiver
;
43 import android
.content
.Context
;
44 import android
.content
.Intent
;
45 import android
.content
.IntentFilter
;
46 import android
.graphics
.Bitmap
;
47 import android
.graphics
.BitmapFactory
;
48 import android
.graphics
.BitmapFactory
.Options
;
49 import android
.graphics
.Point
;
50 import android
.net
.Uri
;
51 import android
.os
.AsyncTask
;
52 import android
.os
.Bundle
;
53 import android
.os
.Handler
;
54 import android
.support
.v4
.app
.DialogFragment
;
55 import android
.support
.v4
.app
.FragmentTransaction
;
56 import android
.util
.Log
;
57 import android
.view
.Display
;
58 import android
.view
.LayoutInflater
;
59 import android
.view
.View
;
60 import android
.view
.View
.OnClickListener
;
61 import android
.view
.ViewGroup
;
62 import android
.webkit
.MimeTypeMap
;
63 import android
.widget
.Button
;
64 import android
.widget
.CheckBox
;
65 import android
.widget
.ImageView
;
66 import android
.widget
.TextView
;
67 import android
.widget
.Toast
;
69 import com
.actionbarsherlock
.app
.SherlockFragment
;
70 import com
.owncloud
.android
.AccountUtils
;
71 import com
.owncloud
.android
.DisplayUtils
;
72 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
;
73 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
74 import com
.owncloud
.android
.datamodel
.OCFile
;
75 import com
.owncloud
.android
.files
.services
.FileDownloader
;
76 import com
.owncloud
.android
.files
.services
.FileObserverService
;
77 import com
.owncloud
.android
.files
.services
.FileUploader
;
78 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
79 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
80 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
81 import com
.owncloud
.android
.operations
.OnRemoteOperationListener
;
82 import com
.owncloud
.android
.operations
.RemoteOperation
;
83 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
84 import com
.owncloud
.android
.operations
.RemoteOperationResult
.ResultCode
;
85 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
86 import com
.owncloud
.android
.operations
.RenameFileOperation
;
87 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
88 import com
.owncloud
.android
.ui
.activity
.ConflictsResolveActivity
;
89 import com
.owncloud
.android
.ui
.activity
.FileDetailActivity
;
90 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
91 import com
.owncloud
.android
.ui
.activity
.TransferServiceGetter
;
92 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
;
93 import com
.owncloud
.android
.ui
.dialog
.EditNameDialog
.EditNameDialogListener
;
94 import com
.owncloud
.android
.utils
.OwnCloudVersion
;
96 import com
.owncloud
.android
.R
;
97 import eu
.alefzero
.webdav
.WebdavClient
;
98 import eu
.alefzero
.webdav
.WebdavUtils
;
101 * This Fragment is used to display the details about a file.
103 * @author Bartek Przybylski
106 public class FileDetailFragment
extends SherlockFragment
implements
107 OnClickListener
, ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
, OnRemoteOperationListener
, EditNameDialogListener
{
109 public static final String EXTRA_FILE
= "FILE";
110 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
112 private FileDetailFragment
.ContainerActivity mContainerActivity
;
116 private OCFile mFile
;
117 private Account mAccount
;
118 private FileDataStorageManager mStorageManager
;
119 private ImageView mPreview
;
121 private DownloadFinishReceiver mDownloadFinishReceiver
;
122 private UploadFinishReceiver mUploadFinishReceiver
;
124 private Handler mHandler
;
125 private RemoteOperation mLastRemoteOperation
;
126 private DialogFragment mCurrentDialog
;
128 private static final String TAG
= FileDetailFragment
.class.getSimpleName();
129 public static final String FTAG
= "FileDetails";
130 public static final String FTAG_CONFIRMATION
= "REMOVE_CONFIRMATION_FRAGMENT";
134 * Creates an empty details fragment.
136 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
138 public FileDetailFragment() {
141 mStorageManager
= null
;
142 mLayout
= R
.layout
.file_details_empty
;
147 * Creates a details fragment.
149 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
151 * @param fileToDetail An {@link OCFile} to show in the fragment
152 * @param ocAccount An ownCloud account; needed to start downloads
154 public FileDetailFragment(OCFile fileToDetail
, Account ocAccount
) {
155 mFile
= fileToDetail
;
156 mAccount
= ocAccount
;
157 mStorageManager
= null
; // we need a context to init this; the container activity is not available yet at this moment
158 mLayout
= R
.layout
.file_details_empty
;
163 public void onCreate(Bundle savedInstanceState
) {
164 super.onCreate(savedInstanceState
);
165 mHandler
= new Handler();
170 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
171 Bundle savedInstanceState
) {
172 super.onCreateView(inflater
, container
, savedInstanceState
);
174 if (savedInstanceState
!= null
) {
175 mFile
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_FILE
);
176 mAccount
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_ACCOUNT
);
179 if(mFile
!= null
&& mAccount
!= null
) {
180 mLayout
= R
.layout
.file_details_fragment
;
184 view
= inflater
.inflate(mLayout
, container
, false
);
187 if (mLayout
== R
.layout
.file_details_fragment
) {
188 mView
.findViewById(R
.id
.fdKeepInSync
).setOnClickListener(this);
189 mView
.findViewById(R
.id
.fdRenameBtn
).setOnClickListener(this);
190 mView
.findViewById(R
.id
.fdDownloadBtn
).setOnClickListener(this);
191 mView
.findViewById(R
.id
.fdOpenBtn
).setOnClickListener(this);
192 mView
.findViewById(R
.id
.fdRemoveBtn
).setOnClickListener(this);
193 //mView.findViewById(R.id.fdShareBtn).setOnClickListener(this);
194 mPreview
= (ImageView
)mView
.findViewById(R
.id
.fdPreview
);
197 updateFileDetails(false
);
206 public void onAttach(Activity activity
) {
207 super.onAttach(activity
);
209 mContainerActivity
= (ContainerActivity
) activity
;
210 } catch (ClassCastException e
) {
211 throw new ClassCastException(activity
.toString() + " must implement " + FileDetailFragment
.ContainerActivity
.class.getSimpleName());
220 public void onActivityCreated(Bundle savedInstanceState
) {
221 super.onActivityCreated(savedInstanceState
);
222 if (mAccount
!= null
) {
223 mStorageManager
= new FileDataStorageManager(mAccount
, getActivity().getApplicationContext().getContentResolver());;
229 public void onSaveInstanceState(Bundle outState
) {
230 Log
.i(getClass().toString(), "onSaveInstanceState() start");
231 super.onSaveInstanceState(outState
);
232 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, mFile
);
233 outState
.putParcelable(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
234 Log
.i(getClass().toString(), "onSaveInstanceState() end");
239 public void onResume() {
242 mDownloadFinishReceiver
= new DownloadFinishReceiver();
243 IntentFilter filter
= new IntentFilter(
244 FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
245 getActivity().registerReceiver(mDownloadFinishReceiver
, filter
);
247 mUploadFinishReceiver
= new UploadFinishReceiver();
248 filter
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
);
249 getActivity().registerReceiver(mUploadFinishReceiver
, filter
);
251 mPreview
= (ImageView
)mView
.findViewById(R
.id
.fdPreview
);
255 public void onPause() {
258 getActivity().unregisterReceiver(mDownloadFinishReceiver
);
259 mDownloadFinishReceiver
= null
;
261 getActivity().unregisterReceiver(mUploadFinishReceiver
);
262 mUploadFinishReceiver
= null
;
264 if (mPreview
!= null
) {
270 public View
getView() {
271 return super.getView() == null ? mView
: super.getView();
277 public void onClick(View v
) {
279 case R
.id
.fdDownloadBtn
: {
280 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
281 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
282 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
)) {
283 downloaderBinder
.cancel(mAccount
, mFile
);
284 if (mFile
.isDown()) {
287 setButtonsForRemote();
290 } else if (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, mFile
)) {
291 uploaderBinder
.cancel(mAccount
, mFile
);
292 if (!mFile
.fileExists()) {
293 // TODO make something better
294 if (getActivity() instanceof FileDisplayActivity
) {
296 FragmentTransaction transaction
= getActivity().getSupportFragmentManager().beginTransaction();
297 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
), FTAG
); // empty FileDetailFragment
298 transaction
.commit();
299 mContainerActivity
.onFileStateChanged();
301 getActivity().finish();
304 } else if (mFile
.isDown()) {
307 setButtonsForRemote();
311 mLastRemoteOperation
= new SynchronizeFileOperation(mFile
, null
, mStorageManager
, mAccount
, true
, false
, getActivity());
312 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, getSherlockActivity().getApplicationContext());
313 mLastRemoteOperation
.execute(wc
, this, mHandler
);
316 boolean inDisplayActivity
= getActivity() instanceof FileDisplayActivity
;
317 getActivity().showDialog((inDisplayActivity
)? FileDisplayActivity
.DIALOG_SHORT_WAIT
: FileDetailActivity
.DIALOG_SHORT_WAIT
);
318 setButtonsForTransferring(); // disable button immediately, although the synchronization does not result in a file transference
323 case R
.id
.fdKeepInSync
: {
324 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
325 mFile
.setKeepInSync(cb
.isChecked());
326 mStorageManager
.saveFile(mFile
);
328 /// register the OCFile instance in the observer service to monitor local updates;
329 /// if necessary, the file is download
330 Intent intent
= new Intent(getActivity().getApplicationContext(),
331 FileObserverService
.class);
332 intent
.putExtra(FileObserverService
.KEY_FILE_CMD
,
334 FileObserverService
.CMD_ADD_OBSERVED_FILE
:
335 FileObserverService
.CMD_DEL_OBSERVED_FILE
));
336 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_FILE
, mFile
);
337 intent
.putExtra(FileObserverService
.KEY_CMD_ARG_ACCOUNT
, mAccount
);
338 Log
.e(TAG
, "starting observer service");
339 getActivity().startService(intent
);
341 if (mFile
.keepInSync()) {
342 onClick(getView().findViewById(R
.id
.fdDownloadBtn
)); // force an immediate synchronization
346 case R
.id
.fdRenameBtn
: {
347 EditNameDialog dialog
= EditNameDialog
.newInstance(getString(R
.string
.rename_dialog_title
), mFile
.getFileName(), this);
348 dialog
.show(getFragmentManager(), "nameeditdialog");
351 case R
.id
.fdRemoveBtn
: {
352 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
353 R
.string
.confirmation_remove_alert
,
354 new String
[]{mFile
.getFileName()},
355 mFile
.isDown() ? R
.string
.confirmation_remove_remote_and_local
: R
.string
.confirmation_remove_remote
,
356 mFile
.isDown() ? R
.string
.confirmation_remove_local
: -1,
357 R
.string
.common_cancel
);
358 confDialog
.setOnConfirmationListener(this);
359 mCurrentDialog
= confDialog
;
360 mCurrentDialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
363 case R
.id
.fdOpenBtn
: {
364 String storagePath
= mFile
.getStoragePath();
365 String encodedStoragePath
= WebdavUtils
.encodePath(storagePath
);
367 Intent i
= new Intent(Intent
.ACTION_VIEW
);
368 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), mFile
.getMimetype());
369 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
372 } catch (Throwable t
) {
373 Log
.e(TAG
, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile
.getMimetype());
374 boolean toastIt
= true
;
375 String mimeType
= "";
377 Intent i
= new Intent(Intent
.ACTION_VIEW
);
378 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
379 if (mimeType
== null
|| !mimeType
.equals(mFile
.getMimetype())) {
380 if (mimeType
!= null
) {
381 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), mimeType
);
384 i
.setDataAndType(Uri
.parse("file://"+ encodedStoragePath
), "*/*");
386 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
391 } catch (IndexOutOfBoundsException e
) {
392 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + storagePath
);
394 } catch (ActivityNotFoundException e
) {
395 Log
.e(TAG
, "No activity found to handle: " + storagePath
+ " with MIME type " + mimeType
+ " obtained from extension");
397 } catch (Throwable th
) {
398 Log
.e(TAG
, "Unexpected problem when opening: " + storagePath
, th
);
402 Toast
.makeText(getActivity(), "There is no application to handle file " + mFile
.getFileName(), Toast
.LENGTH_SHORT
).show();
410 Log
.e(TAG
, "Incorrect view clicked!");
413 /* else if (v.getId() == R.id.fdShareBtn) {
414 Thread t = new Thread(new ShareRunnable(mFile.getRemotePath()));
421 public void onConfirmation(String callerTag
) {
422 if (callerTag
.equals(FTAG_CONFIRMATION
)) {
423 if (mStorageManager
.getFileById(mFile
.getFileId()) != null
) {
424 mLastRemoteOperation
= new RemoveFileOperation( mFile
,
427 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, getSherlockActivity().getApplicationContext());
428 mLastRemoteOperation
.execute(wc
, this, mHandler
);
430 boolean inDisplayActivity
= getActivity() instanceof FileDisplayActivity
;
431 getActivity().showDialog((inDisplayActivity
)? FileDisplayActivity
.DIALOG_SHORT_WAIT
: FileDetailActivity
.DIALOG_SHORT_WAIT
);
434 mCurrentDialog
.dismiss();
435 mCurrentDialog
= null
;
439 public void onNeutral(String callerTag
) {
441 if (mFile
.isDown() && (f
= new File(mFile
.getStoragePath())).exists()) {
443 mFile
.setStoragePath(null
);
444 mStorageManager
.saveFile(mFile
);
445 updateFileDetails(mFile
, mAccount
);
447 mCurrentDialog
.dismiss();
448 mCurrentDialog
= null
;
452 public void onCancel(String callerTag
) {
453 Log
.d(TAG
, "REMOVAL CANCELED");
454 mCurrentDialog
.dismiss();
455 mCurrentDialog
= null
;
460 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
462 * @return True when the fragment was created with the empty layout.
464 public boolean isEmpty() {
465 return (mLayout
== R
.layout
.file_details_empty
|| mFile
== null
|| mAccount
== null
);
470 * Can be used to get the file that is currently being displayed.
471 * @return The file on the screen.
473 public OCFile
getDisplayedFile(){
478 * Use this method to signal this Activity that it shall update its view.
480 * @param file : An {@link OCFile}
482 public void updateFileDetails(OCFile file
, Account ocAccount
) {
484 if (ocAccount
!= null
&& (
485 mStorageManager
== null
||
486 (mAccount
!= null
&& !mAccount
.equals(ocAccount
))
488 mStorageManager
= new FileDataStorageManager(ocAccount
, getActivity().getApplicationContext().getContentResolver());
490 mAccount
= ocAccount
;
491 updateFileDetails(false
);
496 * Updates the view with all relevant details about that file.
498 * TODO Remove parameter when the transferring state of files is kept in database.
500 * @param transferring Flag signaling if the file should be considered as downloading or uploading,
501 * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
502 * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
505 public void updateFileDetails(boolean transferring
) {
507 if (mFile
!= null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
) {
510 setFilename(mFile
.getFileName());
511 setFiletype(mFile
.getMimetype());
512 setFilesize(mFile
.getFileLength());
513 if(ocVersionSupportsTimeCreated()){
514 setTimeCreated(mFile
.getCreationTimestamp());
517 setTimeModified(mFile
.getModificationTimestamp());
519 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
520 cb
.setChecked(mFile
.keepInSync());
522 // configure UI for depending upon local state of the file
523 //if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) || FileUploader.isUploading(mAccount, mFile.getRemotePath())) {
524 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
525 FileUploaderBinder uploaderBinder
= mContainerActivity
.getFileUploaderBinder();
526 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
)) || (uploaderBinder
!= null
&& uploaderBinder
.isUploading(mAccount
, mFile
))) {
527 setButtonsForTransferring();
529 } else if (mFile
.isDown()) {
531 if (mFile
.getMimetype().startsWith("image/")) {
532 BitmapLoader bl
= new BitmapLoader();
533 bl
.execute(new String
[]{mFile
.getStoragePath()});
539 // TODO load default preview image; when the local file is removed, the preview remains there
540 setButtonsForRemote();
543 getView().invalidate();
548 * Updates the filename in view
549 * @param filename to set
551 private void setFilename(String filename
) {
552 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
554 tv
.setText(filename
);
558 * Updates the MIME type in view
559 * @param mimetype to set
561 private void setFiletype(String mimetype
) {
562 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
564 String printableMimetype
= DisplayUtils
.convertMIMEtoPrettyPrint(mimetype
);;
565 tv
.setText(printableMimetype
);
567 ImageView iv
= (ImageView
) getView().findViewById(R
.id
.fdIcon
);
569 iv
.setImageResource(DisplayUtils
.getResourceId(mimetype
));
574 * Updates the file size in view
575 * @param filesize in bytes to set
577 private void setFilesize(long filesize
) {
578 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
580 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
584 * Updates the time that the file was created in view
585 * @param milliseconds Unix time to set
587 private void setTimeCreated(long milliseconds
){
588 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
589 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
591 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
592 tv
.setVisibility(View
.VISIBLE
);
593 tvLabel
.setVisibility(View
.VISIBLE
);
598 * Updates the time that the file was last modified
599 * @param milliseconds Unix time to set
601 private void setTimeModified(long milliseconds
){
602 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
604 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
609 * Enables or disables buttons for a file being downloaded
611 private void setButtonsForTransferring() {
613 Button downloadButton
= (Button
) getView().findViewById(R
.id
.fdDownloadBtn
);
614 downloadButton
.setText(R
.string
.common_cancel
);
615 //downloadButton.setEnabled(false);
617 // let's protect the user from himself ;)
618 ((Button
) getView().findViewById(R
.id
.fdOpenBtn
)).setEnabled(false
);
619 ((Button
) getView().findViewById(R
.id
.fdRenameBtn
)).setEnabled(false
);
620 ((Button
) getView().findViewById(R
.id
.fdRemoveBtn
)).setEnabled(false
);
621 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(false
);
626 * Enables or disables buttons for a file locally available
628 private void setButtonsForDown() {
630 Button downloadButton
= (Button
) getView().findViewById(R
.id
.fdDownloadBtn
);
631 downloadButton
.setText(R
.string
.filedetails_sync_file
);
633 ((Button
) getView().findViewById(R
.id
.fdOpenBtn
)).setEnabled(true
);
634 ((Button
) getView().findViewById(R
.id
.fdRenameBtn
)).setEnabled(true
);
635 ((Button
) getView().findViewById(R
.id
.fdRemoveBtn
)).setEnabled(true
);
636 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
641 * Enables or disables buttons for a file not locally available
643 private void setButtonsForRemote() {
645 Button downloadButton
= (Button
) getView().findViewById(R
.id
.fdDownloadBtn
);
646 downloadButton
.setText(R
.string
.filedetails_download
);
648 ((Button
) getView().findViewById(R
.id
.fdOpenBtn
)).setEnabled(false
);
649 ((Button
) getView().findViewById(R
.id
.fdRenameBtn
)).setEnabled(true
);
650 ((Button
) getView().findViewById(R
.id
.fdRemoveBtn
)).setEnabled(true
);
651 getView().findViewById(R
.id
.fdKeepInSync
).setEnabled(true
);
657 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
658 * the time that the file was created. There is a chance that this will
659 * be fixed in future versions. Use this method to check if this version of
660 * ownCloud has this fix.
661 * @return True, if ownCloud the ownCloud version is supporting creation time
663 private boolean ocVersionSupportsTimeCreated(){
664 /*if(mAccount != null){
665 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
666 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
667 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
668 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
677 * Interface to implement by any Activity that includes some instance of FileDetailFragment
679 * @author David A. Velasco
681 public interface ContainerActivity
extends TransferServiceGetter
{
684 * Callback method invoked when the detail fragment wants to notice its container
685 * activity about a relevant state the file shown by the fragment.
687 * Added to notify to FileDisplayActivity about the need of refresh the files list.
689 * Currently called when:
690 * - a download is started;
691 * - a rename is completed;
692 * - a deletion is completed;
693 * - the 'inSync' flag is changed;
695 public void onFileStateChanged();
701 * Once the file download has finished -> update view
702 * @author Bartek Przybylski
704 private class DownloadFinishReceiver
extends BroadcastReceiver
{
706 public void onReceive(Context context
, Intent intent
) {
707 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
709 if (!isEmpty() && accountName
.equals(mAccount
.name
)) {
710 boolean downloadWasFine
= intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
);
711 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
712 if (mFile
.getRemotePath().equals(downloadedRemotePath
)) {
713 if (downloadWasFine
) {
714 mFile
= mStorageManager
.getFileByPath(downloadedRemotePath
);
716 updateFileDetails(false
); // it updates the buttons; must be called although !downloadWasFine
724 * Once the file upload has finished -> update view
726 * Being notified about the finish of an upload is necessary for the next sequence:
727 * 1. Upload a big file.
728 * 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
729 * of its containing folder; the the server includes it in the PROPFIND requests although it's not fully upload.
730 * 3. Click the file in the list to see its details.
731 * 4. Wait for the upload finishes; at this moment, the details view must be refreshed to enable the action buttons.
733 private class UploadFinishReceiver
extends BroadcastReceiver
{
735 public void onReceive(Context context
, Intent intent
) {
736 String accountName
= intent
.getStringExtra(FileUploader
.ACCOUNT_NAME
);
738 if (!isEmpty() && accountName
.equals(mAccount
.name
)) {
739 boolean uploadWasFine
= intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
, false
);
740 String uploadRemotePath
= intent
.getStringExtra(FileUploader
.EXTRA_REMOTE_PATH
);
741 boolean renamedInUpload
= mFile
.getRemotePath().equals(intent
.getStringExtra(FileUploader
.EXTRA_OLD_REMOTE_PATH
));
742 if (mFile
.getRemotePath().equals(uploadRemotePath
) ||
745 mFile
= mStorageManager
.getFileByPath(uploadRemotePath
);
747 if (renamedInUpload
) {
748 String newName
= (new File(uploadRemotePath
)).getName();
749 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), String
.format(getString(R
.string
.filedetails_renamed_in_upload_msg
), newName
), Toast
.LENGTH_LONG
);
752 getSherlockActivity().removeStickyBroadcast(intent
); // not the best place to do this; a small refactorization of BroadcastReceivers should be done
753 updateFileDetails(false
); // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server
760 // this is a temporary class for sharing purposes, it need to be replaced in transfer service
761 @SuppressWarnings("unused")
762 private class ShareRunnable
implements Runnable
{
763 private String mPath
;
765 public ShareRunnable(String path
) {
770 AccountManager am
= AccountManager
.get(getActivity());
771 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
772 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
));
773 String url
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + AccountUtils
.getWebdavPath(ocv
);
775 Log
.d("share", "sharing for version " + ocv
.toString());
777 if (ocv
.compareTo(new OwnCloudVersion(0x040000)) >= 0) {
778 String APPS_PATH
= "/apps/files_sharing/";
779 String SHARE_PATH
= "ajax/share.php";
781 String SHARED_PATH
= "/apps/files_sharing/get.php?token=";
783 final String WEBDAV_SCRIPT
= "webdav.php";
784 final String WEBDAV_FILES_LOCATION
= "/files/";
786 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(account
, getActivity().getApplicationContext());
787 HttpConnectionManagerParams params
= new HttpConnectionManagerParams();
788 params
.setMaxConnectionsPerHost(wc
.getHostConfiguration(), 5);
790 //wc.getParams().setParameter("http.protocol.single-cookie-header", true);
791 //wc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
793 PostMethod post
= new PostMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + APPS_PATH
+ SHARE_PATH
);
795 post
.addRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8" );
796 post
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
797 List
<NameValuePair
> formparams
= new ArrayList
<NameValuePair
>();
798 Log
.d("share", mPath
+"");
799 formparams
.add(new BasicNameValuePair("sources",mPath
));
800 formparams
.add(new BasicNameValuePair("uid_shared_with", "public"));
801 formparams
.add(new BasicNameValuePair("permissions", "0"));
802 post
.setRequestEntity(new StringRequestEntity(URLEncodedUtils
.format(formparams
, HTTP
.UTF_8
)));
806 PropFindMethod find
= new PropFindMethod(url
+"/");
807 find
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
808 Log
.d("sharer", ""+ url
+"/");
810 for (org
.apache
.commons
.httpclient
.Header a
: find
.getRequestHeaders()) {
811 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
814 int status2
= wc
.executeMethod(find
);
816 Log
.d("sharer", "propstatus "+status2
);
818 GetMethod get
= new GetMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + "/");
819 get
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
821 status2
= wc
.executeMethod(get
);
823 Log
.d("sharer", "getstatus "+status2
);
824 Log
.d("sharer", "" + get
.getResponseBodyAsString());
826 for (org
.apache
.commons
.httpclient
.Header a
: get
.getResponseHeaders()) {
827 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
830 status
= wc
.executeMethod(post
);
831 for (org
.apache
.commons
.httpclient
.Header a
: post
.getRequestHeaders()) {
832 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
834 for (org
.apache
.commons
.httpclient
.Header a
: post
.getResponseHeaders()) {
835 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
837 String resp
= post
.getResponseBodyAsString();
838 Log
.d("share", ""+post
.getURI().toString());
839 Log
.d("share", "returned status " + status
);
840 Log
.d("share", " " +resp
);
842 if(status
!= HttpStatus
.SC_OK
||resp
== null
|| resp
.equals("") || resp
.startsWith("false")) {
846 JSONObject jsonObject
= new JSONObject (resp
);
847 String jsonStatus
= jsonObject
.getString("status");
848 if(!jsonStatus
.equals("success")) throw new Exception("Error while sharing file status != success");
850 String token
= jsonObject
.getString("data");
851 String uri
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + SHARED_PATH
+ token
;
852 Log
.d("Actions:shareFile ok", "url: " + uri
);
854 } catch (Exception e
) {
858 } else if (ocv
.compareTo(new OwnCloudVersion(0x030000)) >= 0) {
864 public void onDismiss(EditNameDialog dialog
) {
865 if (dialog
.getResult()) {
866 String newFilename
= dialog
.getNewFilename();
867 Log
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
868 mLastRemoteOperation
= new RenameFileOperation( mFile
,
871 new FileDataStorageManager(mAccount
, getActivity().getContentResolver()));
872 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(mAccount
, getSherlockActivity().getApplicationContext());
873 mLastRemoteOperation
.execute(wc
, this, mHandler
);
874 boolean inDisplayActivity
= getActivity() instanceof FileDisplayActivity
;
875 getActivity().showDialog((inDisplayActivity
)? FileDisplayActivity
.DIALOG_SHORT_WAIT
: FileDetailActivity
.DIALOG_SHORT_WAIT
);
880 class BitmapLoader
extends AsyncTask
<String
, Void
, Bitmap
> {
881 @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20
883 protected Bitmap
doInBackground(String
... params
) {
884 Bitmap result
= null
;
885 if (params
.length
!= 1) return result
;
886 String storagePath
= params
[0];
889 BitmapFactory
.Options options
= new Options();
890 options
.inScaled
= true
;
891 options
.inPurgeable
= true
;
892 options
.inJustDecodeBounds
= true
;
893 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
894 options
.inPreferQualityOverSpeed
= false
;
896 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
897 options
.inMutable
= false
;
900 result
= BitmapFactory
.decodeFile(storagePath
, options
);
901 options
.inJustDecodeBounds
= false
;
903 int width
= options
.outWidth
;
904 int height
= options
.outHeight
;
906 if (width
>= 2048 || height
>= 2048) {
907 scale
= (int) Math
.ceil((Math
.ceil(Math
.max(height
, width
) / 2048.)));
908 options
.inSampleSize
= scale
;
910 Display display
= getActivity().getWindowManager().getDefaultDisplay();
911 Point size
= new Point();
913 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB_MR2
) {
914 display
.getSize(size
);
915 screenwidth
= size
.x
;
917 screenwidth
= display
.getWidth();
920 Log
.e("ASD", "W " + width
+ " SW " + screenwidth
);
922 if (width
> screenwidth
) {
923 scale
= (int) Math
.ceil((float)width
/ screenwidth
);
924 options
.inSampleSize
= scale
;
927 result
= BitmapFactory
.decodeFile(storagePath
, options
);
929 Log
.e("ASD", "W " + options
.outWidth
+ " SW " + options
.outHeight
);
931 } catch (OutOfMemoryError e
) {
933 Log
.e(TAG
, "Out of memory occured for file with size " + storagePath
);
935 } catch (NoSuchFieldError e
) {
937 Log
.e(TAG
, "Error from access to unexisting field despite protection " + storagePath
);
939 } catch (Throwable t
) {
941 Log
.e(TAG
, "Unexpected error while creating image preview " + storagePath
, t
);
946 protected void onPostExecute(Bitmap result
) {
947 if (result
!= null
&& mPreview
!= null
) {
948 mPreview
.setImageBitmap(result
);
958 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
959 if (operation
.equals(mLastRemoteOperation
)) {
960 if (operation
instanceof RemoveFileOperation
) {
961 onRemoveFileOperationFinish((RemoveFileOperation
)operation
, result
);
963 } else if (operation
instanceof RenameFileOperation
) {
964 onRenameFileOperationFinish((RenameFileOperation
)operation
, result
);
966 } else if (operation
instanceof SynchronizeFileOperation
) {
967 onSynchronizeFileOperationFinish((SynchronizeFileOperation
)operation
, result
);
973 private void onRemoveFileOperationFinish(RemoveFileOperation operation
, RemoteOperationResult result
) {
974 boolean inDisplayActivity
= getActivity() instanceof FileDisplayActivity
;
975 getActivity().dismissDialog((inDisplayActivity
)? FileDisplayActivity
.DIALOG_SHORT_WAIT
: FileDetailActivity
.DIALOG_SHORT_WAIT
);
977 if (result
.isSuccess()) {
978 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
980 if (inDisplayActivity
) {
982 FragmentTransaction transaction
= getActivity().getSupportFragmentManager().beginTransaction();
983 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
)); // empty FileDetailFragment
984 transaction
.commit();
985 mContainerActivity
.onFileStateChanged();
987 getActivity().finish();
991 Toast msg
= Toast
.makeText(getActivity(), R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
993 if (result
.isSslRecoverableException()) {
994 // TODO show the SSL warning dialog
999 private void onRenameFileOperationFinish(RenameFileOperation operation
, RemoteOperationResult result
) {
1000 boolean inDisplayActivity
= getActivity() instanceof FileDisplayActivity
;
1001 getActivity().dismissDialog((inDisplayActivity
)? FileDisplayActivity
.DIALOG_SHORT_WAIT
: FileDetailActivity
.DIALOG_SHORT_WAIT
);
1003 if (result
.isSuccess()) {
1004 updateFileDetails(((RenameFileOperation
)operation
).getFile(), mAccount
);
1005 mContainerActivity
.onFileStateChanged();
1008 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
1009 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_local_fail_msg
, Toast
.LENGTH_LONG
);
1011 // TODO throw again the new rename dialog
1013 Toast msg
= Toast
.makeText(getActivity(), R
.string
.rename_server_fail_msg
, Toast
.LENGTH_LONG
);
1015 if (result
.isSslRecoverableException()) {
1016 // TODO show the SSL warning dialog
1022 private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation
, RemoteOperationResult result
) {
1023 boolean inDisplayActivity
= getActivity() instanceof FileDisplayActivity
;
1024 getActivity().dismissDialog((inDisplayActivity
)? FileDisplayActivity
.DIALOG_SHORT_WAIT
: FileDetailActivity
.DIALOG_SHORT_WAIT
);
1026 if (!result
.isSuccess()) {
1027 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
1028 Intent i
= new Intent(getActivity(), ConflictsResolveActivity
.class);
1029 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, mFile
);
1030 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, mAccount
);
1034 Toast msg
= Toast
.makeText(getActivity(), R
.string
.sync_file_fail_msg
, Toast
.LENGTH_LONG
);
1038 if (mFile
.isDown()) {
1039 setButtonsForDown();
1042 setButtonsForRemote();
1046 if (operation
.transferWasRequested()) {
1047 mContainerActivity
.onFileStateChanged(); // this is not working; FileDownloader won't do NOTHING at all until this method finishes, so
1048 // checking the service to see if the file is downloading results in FALSE
1050 Toast msg
= Toast
.makeText(getActivity(), R
.string
.sync_file_nothing_to_do_msg
, Toast
.LENGTH_LONG
);
1052 if (mFile
.isDown()) {
1053 setButtonsForDown();
1056 setButtonsForRemote();