1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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 eu
.alefzero
.owncloud
.ui
.fragment
;
21 import java
.io
.IOException
;
22 import java
.util
.ArrayList
;
23 import java
.util
.List
;
25 import org
.apache
.commons
.httpclient
.HostConfiguration
;
26 import org
.apache
.commons
.httpclient
.HttpException
;
27 import org
.apache
.commons
.httpclient
.cookie
.CookiePolicy
;
28 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
29 import org
.apache
.commons
.httpclient
.methods
.PostMethod
;
30 import org
.apache
.commons
.httpclient
.methods
.StringRequestEntity
;
31 import org
.apache
.commons
.httpclient
.params
.HttpConnectionManagerParams
;
32 import org
.apache
.commons
.httpclient
.params
.HttpMethodParams
;
33 import org
.apache
.http
.HttpStatus
;
34 import org
.apache
.http
.NameValuePair
;
35 import org
.apache
.http
.client
.utils
.URLEncodedUtils
;
36 import org
.apache
.http
.message
.BasicNameValuePair
;
37 import org
.apache
.http
.protocol
.HTTP
;
38 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DavMethodBase
;
39 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DeleteMethod
;
40 import org
.apache
.jackrabbit
.webdav
.client
.methods
.MoveMethod
;
41 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
42 import org
.json
.JSONException
;
43 import org
.json
.JSONObject
;
45 import android
.accounts
.Account
;
46 import android
.accounts
.AccountManager
;
47 import android
.app
.AlertDialog
;
48 import android
.app
.Dialog
;
49 import android
.content
.ActivityNotFoundException
;
50 import android
.content
.BroadcastReceiver
;
51 import android
.content
.Context
;
52 import android
.content
.DialogInterface
;
53 import android
.content
.DialogInterface
.OnDismissListener
;
54 import android
.content
.Intent
;
55 import android
.content
.IntentFilter
;
56 import android
.content
.res
.Resources
.NotFoundException
;
57 import android
.graphics
.Bitmap
;
58 import android
.graphics
.BitmapFactory
;
59 import android
.graphics
.BitmapFactory
.Options
;
60 import android
.graphics
.Point
;
61 import android
.graphics
.drawable
.BitmapDrawable
;
62 import android
.graphics
.drawable
.Drawable
;
63 import android
.net
.Uri
;
64 import android
.os
.Bundle
;
65 import android
.os
.Environment
;
66 import android
.os
.Handler
;
67 import android
.preference
.PreferenceActivity
.Header
;
68 import android
.support
.v4
.app
.FragmentTransaction
;
69 import android
.util
.Log
;
70 import android
.view
.Display
;
71 import android
.view
.LayoutInflater
;
72 import android
.view
.View
;
73 import android
.view
.View
.OnClickListener
;
74 import android
.view
.ViewGroup
;
75 import android
.view
.WindowManager
.LayoutParams
;
76 import android
.webkit
.MimeTypeMap
;
77 import android
.widget
.Button
;
78 import android
.widget
.CheckBox
;
79 import android
.widget
.EditText
;
80 import android
.widget
.ImageView
;
81 import android
.widget
.TextView
;
82 import android
.widget
.Toast
;
84 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
85 import com
.actionbarsherlock
.app
.SherlockFragment
;
87 import eu
.alefzero
.owncloud
.AccountUtils
;
88 import eu
.alefzero
.owncloud
.DisplayUtils
;
89 import eu
.alefzero
.owncloud
.R
;
90 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
91 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
92 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
93 import eu
.alefzero
.owncloud
.files
.services
.FileDownloader
;
94 import eu
.alefzero
.owncloud
.ui
.activity
.FileDisplayActivity
;
95 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
96 import eu
.alefzero
.webdav
.WebdavClient
;
99 * This Fragment is used to display the details about a file.
101 * @author Bartek Przybylski
104 public class FileDetailFragment
extends SherlockFragment
implements
105 OnClickListener
, ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
{
107 public static final String EXTRA_FILE
= "FILE";
108 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
112 private OCFile mFile
;
113 private Account mAccount
;
115 private DownloadFinishReceiver mDownloadFinishReceiver
;
117 private static final String TAG
= "FileDetailFragment";
118 public static final String FTAG
= "FileDetails";
119 public static final String FTAG_CONFIRMATION
= "REMOVE_CONFIRMATION_FRAGMENT";
123 * Creates an empty details fragment.
125 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
127 public FileDetailFragment() {
130 mLayout
= R
.layout
.file_details_empty
;
135 * Creates a details fragment.
137 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
139 * @param fileToDetail An {@link OCFile} to show in the fragment
140 * @param ocAccount An ownCloud account; needed to start downloads
142 public FileDetailFragment(OCFile fileToDetail
, Account ocAccount
){
143 mFile
= fileToDetail
;
144 mAccount
= ocAccount
;
145 mLayout
= R
.layout
.file_details_empty
;
147 if(fileToDetail
!= null
&& ocAccount
!= null
) {
148 mLayout
= R
.layout
.file_details_fragment
;
154 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
155 Bundle savedInstanceState
) {
156 super.onCreateView(inflater
, container
, savedInstanceState
);
158 if (savedInstanceState
!= null
) {
159 mFile
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_FILE
);
160 mAccount
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_ACCOUNT
);
164 view
= inflater
.inflate(mLayout
, container
, false
);
167 if (mLayout
== R
.layout
.file_details_fragment
) {
168 mView
.findViewById(R
.id
.fdKeepInSync
).setOnClickListener(this);
169 //mView.findViewById(R.id.fdShareBtn).setOnClickListener(this);
170 mView
.findViewById(R
.id
.fdRenameBtn
).setOnClickListener(this);
171 mView
.findViewById(R
.id
.fdRemoveBtn
).setOnClickListener(this);
180 public void onSaveInstanceState(Bundle outState
) {
181 Log
.i(getClass().toString(), "onSaveInstanceState() start");
182 super.onSaveInstanceState(outState
);
183 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, mFile
);
184 outState
.putParcelable(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
185 Log
.i(getClass().toString(), "onSaveInstanceState() end");
190 public void onResume() {
192 mDownloadFinishReceiver
= new DownloadFinishReceiver();
193 IntentFilter filter
= new IntentFilter(
194 FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
195 getActivity().registerReceiver(mDownloadFinishReceiver
, filter
);
199 public void onPause() {
201 getActivity().unregisterReceiver(mDownloadFinishReceiver
);
202 mDownloadFinishReceiver
= null
;
206 public View
getView() {
207 return super.getView() == null ? mView
: super.getView();
213 public void onClick(View v
) {
215 case R
.id
.fdDownloadBtn
: {
216 //Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
217 Intent i
= new Intent(getActivity(), FileDownloader
.class);
218 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
219 i
.putExtra(FileDownloader
.EXTRA_REMOTE_PATH
, mFile
.getRemotePath());
220 i
.putExtra(FileDownloader
.EXTRA_FILE_PATH
, mFile
.getURLDecodedRemotePath());
221 i
.putExtra(FileDownloader
.EXTRA_FILE_SIZE
, mFile
.getFileLength());
223 getActivity().startService(i
);
226 case R
.id
.fdKeepInSync
: {
227 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
228 mFile
.setKeepInSync(cb
.isChecked());
229 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getApplicationContext().getContentResolver());
230 fdsm
.saveFile(mFile
);
231 if (mFile
.keepInSync() && !mFile
.isDownloaded()) {
232 onClick(getView().findViewById(R
.id
.fdDownloadBtn
));
236 case R
.id
.fdRenameBtn
: {
237 EditNameFragment dialog
= EditNameFragment
.newInstance(mFile
.getFileName());
238 dialog
.show(getFragmentManager(), "nameeditdialog");
239 dialog
.setOnDismissListener(this);
242 case R
.id
.fdRemoveBtn
: {
243 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance("to remove " + mFile
.getFileName());
244 confDialog
.setOnConfirmationListener(this);
245 confDialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
249 Log
.e(TAG
, "Incorrect view clicked!");
252 /* else if (v.getId() == R.id.fdShareBtn) {
253 Thread t = new Thread(new ShareRunnable(mFile.getRemotePath()));
260 public void onConfirmation(boolean confirmation
, String callerTag
) {
261 if (confirmation
&& callerTag
.equals(FTAG_CONFIRMATION
)) {
262 Log
.e("ASD","onConfirmation");
263 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
264 if (fdsm
.getFileById(mFile
.getFileId()) != null
) {
265 new Thread(new RemoveRunnable(mFile
, mAccount
, new Handler())).start();
267 } else if (!confirmation
) Log
.d(TAG
, "REMOVAL CANCELED");
272 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
274 * @return True when the fragment was created with the empty layout.
276 public boolean isEmpty() {
277 return mLayout
== R
.layout
.file_details_empty
;
282 * Can be used to get the file that is currently being displayed.
283 * @return The file on the screen.
285 public OCFile
getDisplayedFile(){
290 * Use this method to signal this Activity that it shall update its view.
292 * @param file : An {@link OCFile}
294 public void updateFileDetails(OCFile file
, Account ocAccount
) {
296 mAccount
= ocAccount
;
302 * Updates the view with all relevant details about that file.
304 public void updateFileDetails() {
306 if (mFile
!= null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
) {
308 Button downloadButton
= (Button
) getView().findViewById(R
.id
.fdDownloadBtn
);
310 setFilename(mFile
.getFileName());
311 setFiletype(DisplayUtils
.convertMIMEtoPrettyPrint(mFile
313 setFilesize(mFile
.getFileLength());
314 if(ocVersionSupportsTimeCreated()){
315 setTimeCreated(mFile
.getCreationTimestamp());
318 setTimeModified(mFile
.getModificationTimestamp());
320 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
321 cb
.setChecked(mFile
.keepInSync());
323 if (mFile
.getStoragePath() != null
) {
325 ImageView preview
= (ImageView
) getView().findViewById(R
.id
.fdPreview
);
327 if (mFile
.getMimetype().startsWith("image/")) {
328 BitmapFactory
.Options options
= new Options();
329 options
.inScaled
= true
;
330 options
.inPurgeable
= true
;
331 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
332 options
.inPreferQualityOverSpeed
= false
;
334 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
335 options
.inMutable
= false
;
338 Bitmap bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath(), options
);
341 int width
= options
.outWidth
;
342 int height
= options
.outHeight
;
344 boolean recycle
= false
;
345 if (width
>= 2048 || height
>= 2048) {
346 scale
= (int) (Math
.ceil(Math
.max(height
, width
)/2048.));
347 options
.inSampleSize
= scale
;
350 Display display
= getActivity().getWindowManager().getDefaultDisplay();
351 Point size
= new Point();
353 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB_MR2
) {
354 display
.getSize(size
);
355 screenwidth
= size
.x
;
357 screenwidth
= display
.getWidth();
360 Log
.e("ASD", "W " + width
+ " SW " + screenwidth
);
362 if (width
> screenwidth
) {
363 scale
= (int) (Math
.ceil(Math
.max(height
, width
)/screenwidth
));
364 options
.inSampleSize
= scale
;
369 if (recycle
) bmp
.recycle();
370 bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath(), options
);
374 preview
.setImageBitmap(bmp
);
377 } catch (OutOfMemoryError e
) {
378 preview
.setVisibility(View
.INVISIBLE
);
379 Log
.e(TAG
, "Out of memory occured for file with size " + mFile
.getFileLength());
381 } catch (NoSuchFieldError e
) {
382 preview
.setVisibility(View
.INVISIBLE
);
383 Log
.e(TAG
, "Error from access to unexisting field despite protection " + mFile
.getFileLength());
385 } catch (Throwable t
) {
386 preview
.setVisibility(View
.INVISIBLE
);
387 Log
.e(TAG
, "Unexpected error while creating image preview " + mFile
.getFileLength(), t
);
390 // Change download button to open button
391 downloadButton
.setText(R
.string
.filedetails_open
);
392 downloadButton
.setOnClickListener(new OnClickListener() {
394 public void onClick(View v
) {
395 String storagePath
= mFile
.getStoragePath();
397 Intent i
= new Intent(Intent
.ACTION_VIEW
);
398 i
.setDataAndType(Uri
.parse("file://"+ storagePath
), mFile
.getMimetype());
399 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
402 } catch (Throwable t
) {
403 Log
.e(TAG
, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile
.getMimetype());
404 boolean toastIt
= true
;
405 String mimeType
= "";
407 Intent i
= new Intent(Intent
.ACTION_VIEW
);
408 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
409 if (mimeType
!= null
&& !mimeType
.equals(mFile
.getMimetype())) {
410 i
.setDataAndType(Uri
.parse("file://"+mFile
.getStoragePath()), mimeType
);
411 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
416 } catch (IndexOutOfBoundsException e
) {
417 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + storagePath
);
419 } catch (ActivityNotFoundException e
) {
420 Log
.e(TAG
, "No activity found to handle: " + storagePath
+ " with MIME type " + mimeType
+ " obtained from extension");
422 } catch (Throwable th
) {
423 Log
.e(TAG
, "Unexpected problem when opening: " + storagePath
, th
);
427 Toast
.makeText(getActivity(), "There is no application to handle file " + mFile
.getFileName(), Toast
.LENGTH_SHORT
).show();
435 // Make download button effective
436 downloadButton
.setOnClickListener(this);
443 * Updates the filename in view
444 * @param filename to set
446 private void setFilename(String filename
) {
447 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
449 tv
.setText(filename
);
453 * Updates the MIME type in view
454 * @param mimetype to set
456 private void setFiletype(String mimetype
) {
457 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
459 tv
.setText(mimetype
);
463 * Updates the file size in view
464 * @param filesize in bytes to set
466 private void setFilesize(long filesize
) {
467 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
469 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
473 * Updates the time that the file was created in view
474 * @param milliseconds Unix time to set
476 private void setTimeCreated(long milliseconds
){
477 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
478 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
480 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
481 tv
.setVisibility(View
.VISIBLE
);
482 tvLabel
.setVisibility(View
.VISIBLE
);
487 * Updates the time that the file was last modified
488 * @param milliseconds Unix time to set
490 private void setTimeModified(long milliseconds
){
491 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
493 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
498 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
499 * the time that the file was created. There is a chance that this will
500 * be fixed in future versions. Use this method to check if this version of
501 * ownCloud has this fix.
502 * @return True, if ownCloud the ownCloud version is supporting creation time
504 private boolean ocVersionSupportsTimeCreated(){
505 /*if(mAccount != null){
506 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
507 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
508 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
509 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
517 * Once the file download has finished -> update view
518 * @author Bartek Przybylski
520 private class DownloadFinishReceiver
extends BroadcastReceiver
{
522 public void onReceive(Context context
, Intent intent
) {
523 if (getView()!=null
&& getView().findViewById(R
.id
.fdDownloadBtn
) != null
)
524 getView().findViewById(R
.id
.fdDownloadBtn
).setEnabled(true
);
526 if (intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
)) {
527 mFile
.setStoragePath(intent
.getStringExtra(FileDownloader
.EXTRA_FILE_PATH
));
529 } else if (intent
.getAction().equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
530 Toast
.makeText(context
, R
.string
.downloader_download_failed
, Toast
.LENGTH_SHORT
).show();
536 // this is a temporary class for sharing purposes, it need to be replaced in transfer service
537 private class ShareRunnable
implements Runnable
{
538 private String mPath
;
540 public ShareRunnable(String path
) {
545 AccountManager am
= AccountManager
.get(getActivity());
546 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
547 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
));
548 String url
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + AccountUtils
.getWebdavPath(ocv
);
550 Log
.d("share", "sharing for version " + ocv
.toString());
552 if (ocv
.compareTo(new OwnCloudVersion(0x040000)) >= 0) {
553 String APPS_PATH
= "/apps/files_sharing/";
554 String SHARE_PATH
= "ajax/share.php";
556 String SHARED_PATH
= "/apps/files_sharing/get.php?token=";
558 final String WEBDAV_SCRIPT
= "webdav.php";
559 final String WEBDAV_FILES_LOCATION
= "/files/";
561 WebdavClient wc
= new WebdavClient();
562 HttpConnectionManagerParams params
= new HttpConnectionManagerParams();
563 params
.setMaxConnectionsPerHost(wc
.getHostConfiguration(), 5);
565 //wc.getParams().setParameter("http.protocol.single-cookie-header", true);
566 //wc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
568 PostMethod post
= new PostMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + APPS_PATH
+ SHARE_PATH
);
570 post
.addRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8" );
571 post
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
572 List
<NameValuePair
> formparams
= new ArrayList
<NameValuePair
>();
573 Log
.d("share", mPath
+"");
574 formparams
.add(new BasicNameValuePair("sources",mPath
));
575 formparams
.add(new BasicNameValuePair("uid_shared_with", "public"));
576 formparams
.add(new BasicNameValuePair("permissions", "0"));
577 post
.setRequestEntity(new StringRequestEntity(URLEncodedUtils
.format(formparams
, HTTP
.UTF_8
)));
581 PropFindMethod find
= new PropFindMethod(url
+"/");
582 find
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
583 Log
.d("sharer", ""+ url
+"/");
584 wc
.setCredentials(account
.name
.substring(0, account
.name
.lastIndexOf('@')), am
.getPassword(account
));
586 for (org
.apache
.commons
.httpclient
.Header a
: find
.getRequestHeaders()) {
587 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
590 int status2
= wc
.executeMethod(find
);
592 Log
.d("sharer", "propstatus "+status2
);
594 GetMethod get
= new GetMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + "/");
595 get
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
597 status2
= wc
.executeMethod(get
);
599 Log
.d("sharer", "getstatus "+status2
);
600 Log
.d("sharer", "" + get
.getResponseBodyAsString());
602 for (org
.apache
.commons
.httpclient
.Header a
: get
.getResponseHeaders()) {
603 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
606 status
= wc
.executeMethod(post
);
607 for (org
.apache
.commons
.httpclient
.Header a
: post
.getRequestHeaders()) {
608 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
610 for (org
.apache
.commons
.httpclient
.Header a
: post
.getResponseHeaders()) {
611 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
613 String resp
= post
.getResponseBodyAsString();
614 Log
.d("share", ""+post
.getURI().toString());
615 Log
.d("share", "returned status " + status
);
616 Log
.d("share", " " +resp
);
618 if(status
!= HttpStatus
.SC_OK
||resp
== null
|| resp
.equals("") || resp
.startsWith("false")) {
622 JSONObject jsonObject
= new JSONObject (resp
);
623 String jsonStatus
= jsonObject
.getString("status");
624 if(!jsonStatus
.equals("success")) throw new Exception("Error while sharing file status != success");
626 String token
= jsonObject
.getString("data");
627 String uri
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + SHARED_PATH
+ token
;
628 Log
.d("Actions:shareFile ok", "url: " + uri
);
630 } catch (HttpException e
) {
631 // TODO Auto-generated catch block
633 } catch (IOException e
) {
634 // TODO Auto-generated catch block
636 } catch (JSONException e
) {
637 // TODO Auto-generated catch block
639 } catch (Exception e
) {
640 // TODO Auto-generated catch block
644 } else if (ocv
.compareTo(new OwnCloudVersion(0x030000)) >= 0) {
650 public void onDismiss(EditNameFragment dialog
) {
651 Log
.e("ASD","ondismiss");
652 if (dialog
instanceof EditNameFragment
) {
653 if (((EditNameFragment
)dialog
).getResult()) {
654 String newFilename
= ((EditNameFragment
)dialog
).getNewFilename();
655 Log
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
656 if (!newFilename
.equals(mFile
.getFileName())) {
657 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
658 if (fdsm
.getFileById(mFile
.getFileId()) != null
) {
659 OCFile newFile
= new OCFile(fdsm
.getFileById(mFile
.getParentId()).getRemotePath()+"/"+newFilename
);
660 newFile
.setCreationTimestamp(mFile
.getCreationTimestamp());
661 newFile
.setFileId(mFile
.getFileId());
662 newFile
.setFileLength(mFile
.getFileLength());
663 newFile
.setKeepInSync(mFile
.keepInSync());
664 newFile
.setLastSyncDate(mFile
.getLastSyncDate());
665 newFile
.setMimetype(mFile
.getMimetype());
666 newFile
.setModificationTimestamp(mFile
.getModificationTimestamp());
667 newFile
.setParentId(mFile
.getParentId());
668 newFile
.setStoragePath(mFile
.getStoragePath());
670 new Thread(new RenameRunnable(mFile
, newFile
, mAccount
, new Handler())).start();
676 Log
.e(TAG
, "Unknown dialog intance passed to onDismissDalog: " + dialog
.getClass().getCanonicalName());
681 private class RenameRunnable
implements Runnable
{
687 public RenameRunnable(OCFile oldFile
, OCFile newFile
, Account account
, Handler handler
) {
695 WebdavClient wc
= new WebdavClient(mAccount
, getSherlockActivity().getApplicationContext());
696 AccountManager am
= AccountManager
.get(getSherlockActivity());
697 String baseUrl
= am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
698 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
699 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
700 Log
.d("ASD", ""+baseUrl
+ webdav_path
+ mOld
.getRemotePath());
702 Log
.e("ASD", Uri
.parse(baseUrl
).getPath() == null ?
"" : Uri
.parse(baseUrl
).getPath() + webdav_path
+ mNew
.getRemotePath());
703 LocalMoveMethod move
= new LocalMoveMethod(baseUrl
+ webdav_path
+ mOld
.getRemotePath(),
704 Uri
.parse(baseUrl
).getPath() == null ?
"" : Uri
.parse(baseUrl
).getPath() + webdav_path
+ mNew
.getRemotePath());
707 int status
= wc
.executeMethod(move
);
708 if (move
.succeeded()) {
709 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
710 fdsm
.removeFile(mOld
);
713 mHandler
.post(new Runnable() {
715 public void run() { updateFileDetails(mFile
, mAccount
); }
718 Log
.e("ASD", ""+move
.getQueryString());
719 Log
.d("move", "returned status " + status
);
720 } catch (HttpException e
) {
721 // TODO Auto-generated catch block
723 } catch (IOException e
) {
724 // TODO Auto-generated catch block
728 private class LocalMoveMethod
extends DavMethodBase
{
730 public LocalMoveMethod(String uri
, String dest
) {
732 addRequestHeader(new org
.apache
.commons
.httpclient
.Header("Destination", dest
));
736 public String
getName() {
741 protected boolean isSuccess(int status
) {
742 return status
== 201 || status
== 204;
748 private static class EditNameFragment
extends SherlockDialogFragment
implements OnClickListener
{
750 private String mNewFilename
;
751 private boolean mResult
;
752 private FileDetailFragment mListener
;
754 static public EditNameFragment
newInstance(String filename
) {
755 EditNameFragment f
= new EditNameFragment();
756 Bundle args
= new Bundle();
757 args
.putString("filename", filename
);
758 f
.setArguments(args
);
763 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
764 View v
= inflater
.inflate(R
.layout
.edit_box_dialog
, container
, false
);
766 String currentName
= getArguments().getString("filename");
767 if (currentName
== null
)
770 ((Button
)v
.findViewById(R
.id
.cancel
)).setOnClickListener(this);
771 ((Button
)v
.findViewById(R
.id
.ok
)).setOnClickListener(this);
772 ((TextView
)v
.findViewById(R
.id
.user_input
)).setText(currentName
);
773 ((TextView
)v
.findViewById(R
.id
.user_input
)).requestFocus();
774 getDialog().getWindow().setSoftInputMode(LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
781 public void onClick(View view
) {
782 switch (view
.getId()) {
784 mNewFilename
= ((TextView
)getView().findViewById(R
.id
.user_input
)).getText().toString();
787 case R
.id
.cancel
: { // fallthought
789 mListener
.onDismiss(this);
794 void setOnDismissListener(FileDetailFragment listener
) {
795 mListener
= listener
;
798 public String
getNewFilename() {
802 // true if user click ok
803 public boolean getResult() {
810 private class RemoveRunnable
implements Runnable
{
813 OCFile mFileToRemove
;
816 public RemoveRunnable(OCFile fileToRemove
, Account account
, Handler handler
) {
817 mFileToRemove
= fileToRemove
;
823 WebdavClient wc
= new WebdavClient(mAccount
, getSherlockActivity().getApplicationContext());
824 AccountManager am
= AccountManager
.get(getSherlockActivity());
825 String baseUrl
= am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
826 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
827 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
828 Log
.d("ASD", ""+baseUrl
+ webdav_path
+ mFileToRemove
.getRemotePath());
830 DeleteMethod delete
= new DeleteMethod(baseUrl
+ webdav_path
+ mFileToRemove
.getRemotePath());
831 HttpMethodParams params
= delete
.getParams();
832 params
.setSoTimeout(1000);
833 delete
.setParams(params
);
835 boolean success
= false
;
837 int status
= wc
.executeMethod(delete
);
838 if (delete
.succeeded()) {
839 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
840 fdsm
.removeFile(mFileToRemove
);
841 mHandler
.post(new Runnable() {
845 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
847 if (getActivity() instanceof FileDisplayActivity
) {
849 FragmentTransaction transaction
= getActivity().getSupportFragmentManager().beginTransaction();
850 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
)); // empty FileDetailFragment
851 transaction
.commit();
854 getActivity().finish();
857 } catch (NotFoundException e
) {
864 Log
.e("ASD", ""+ delete
.getQueryString());
865 Log
.d("delete", "returned status " + status
);
867 } catch (HttpException e
) {
870 } catch (IOException e
) {
875 mHandler
.post(new Runnable() {
879 Toast msg
= Toast
.makeText(getActivity(), R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
882 } catch (NotFoundException e
) {