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
;
20 import java
.io
.IOException
;
21 import java
.util
.ArrayList
;
22 import java
.util
.List
;
24 import org
.apache
.commons
.httpclient
.HostConfiguration
;
25 import org
.apache
.commons
.httpclient
.HttpException
;
26 import org
.apache
.commons
.httpclient
.cookie
.CookiePolicy
;
27 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
28 import org
.apache
.commons
.httpclient
.methods
.PostMethod
;
29 import org
.apache
.commons
.httpclient
.methods
.StringRequestEntity
;
30 import org
.apache
.commons
.httpclient
.params
.HttpConnectionManagerParams
;
31 import org
.apache
.commons
.httpclient
.params
.HttpMethodParams
;
32 import org
.apache
.http
.HttpStatus
;
33 import org
.apache
.http
.NameValuePair
;
34 import org
.apache
.http
.client
.utils
.URLEncodedUtils
;
35 import org
.apache
.http
.message
.BasicNameValuePair
;
36 import org
.apache
.http
.protocol
.HTTP
;
37 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DavMethodBase
;
38 import org
.apache
.jackrabbit
.webdav
.client
.methods
.MoveMethod
;
39 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
40 import org
.json
.JSONException
;
41 import org
.json
.JSONObject
;
43 import android
.accounts
.Account
;
44 import android
.accounts
.AccountManager
;
45 import android
.content
.ActivityNotFoundException
;
46 import android
.content
.BroadcastReceiver
;
47 import android
.content
.Context
;
48 import android
.content
.DialogInterface
;
49 import android
.content
.DialogInterface
.OnDismissListener
;
50 import android
.content
.Intent
;
51 import android
.content
.IntentFilter
;
52 import android
.graphics
.Bitmap
;
53 import android
.graphics
.BitmapFactory
;
54 import android
.graphics
.BitmapFactory
.Options
;
55 import android
.graphics
.Point
;
56 import android
.graphics
.drawable
.BitmapDrawable
;
57 import android
.graphics
.drawable
.Drawable
;
58 import android
.net
.Uri
;
59 import android
.os
.Bundle
;
60 import android
.os
.Handler
;
61 import android
.preference
.PreferenceActivity
.Header
;
62 import android
.util
.Log
;
63 import android
.view
.Display
;
64 import android
.view
.LayoutInflater
;
65 import android
.view
.View
;
66 import android
.view
.View
.OnClickListener
;
67 import android
.view
.ViewGroup
;
68 import android
.view
.WindowManager
.LayoutParams
;
69 import android
.webkit
.MimeTypeMap
;
70 import android
.widget
.Button
;
71 import android
.widget
.CheckBox
;
72 import android
.widget
.EditText
;
73 import android
.widget
.ImageView
;
74 import android
.widget
.TextView
;
75 import android
.widget
.Toast
;
77 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
78 import com
.actionbarsherlock
.app
.SherlockFragment
;
80 import eu
.alefzero
.owncloud
.AccountUtils
;
81 import eu
.alefzero
.owncloud
.DisplayUtils
;
82 import eu
.alefzero
.owncloud
.R
;
83 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
84 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
85 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
86 import eu
.alefzero
.owncloud
.files
.services
.FileDownloader
;
87 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
88 import eu
.alefzero
.webdav
.WebdavClient
;
91 * This Fragment is used to display the details about a file.
93 * @author Bartek Przybylski
96 public class FileDetailFragment
extends SherlockFragment
implements
99 public static final String EXTRA_FILE
= "FILE";
100 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
104 private OCFile mFile
;
105 private Account mAccount
;
107 private DownloadFinishReceiver mDownloadFinishReceiver
;
109 private static final String TAG
= "FileDetailFragment";
110 public static final String FTAG
= "FileDetails";
114 * Creates an empty details fragment.
116 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
118 public FileDetailFragment() {
121 mLayout
= R
.layout
.file_details_empty
;
126 * Creates a details fragment.
128 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
130 * @param fileToDetail An {@link OCFile} to show in the fragment
131 * @param ocAccount An ownCloud account; needed to start downloads
133 public FileDetailFragment(OCFile fileToDetail
, Account ocAccount
){
134 mFile
= fileToDetail
;
135 mAccount
= ocAccount
;
136 mLayout
= R
.layout
.file_details_empty
;
138 if(fileToDetail
!= null
&& ocAccount
!= null
) {
139 mLayout
= R
.layout
.file_details_fragment
;
145 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
146 Bundle savedInstanceState
) {
147 super.onCreateView(inflater
, container
, savedInstanceState
);
149 if (savedInstanceState
!= null
) {
150 mFile
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_FILE
);
151 mAccount
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_ACCOUNT
);
155 view
= inflater
.inflate(mLayout
, container
, false
);
164 public void onSaveInstanceState(Bundle outState
) {
165 Log
.i(getClass().toString(), "onSaveInstanceState() start");
166 super.onSaveInstanceState(outState
);
167 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, mFile
);
168 outState
.putParcelable(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
169 Log
.i(getClass().toString(), "onSaveInstanceState() end");
174 public void onResume() {
176 mDownloadFinishReceiver
= new DownloadFinishReceiver();
177 IntentFilter filter
= new IntentFilter(
178 FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
179 getActivity().registerReceiver(mDownloadFinishReceiver
, filter
);
183 public void onPause() {
185 getActivity().unregisterReceiver(mDownloadFinishReceiver
);
186 mDownloadFinishReceiver
= null
;
190 public View
getView() {
191 return super.getView() == null ? mView
: super.getView();
197 public void onClick(View v
) {
199 case R
.id
.fdDownloadBtn
: {
200 //Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
201 Intent i
= new Intent(getActivity(), FileDownloader
.class);
202 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
203 i
.putExtra(FileDownloader
.EXTRA_REMOTE_PATH
, mFile
.getRemotePath());
204 i
.putExtra(FileDownloader
.EXTRA_FILE_PATH
, mFile
.getURLDecodedRemotePath());
205 i
.putExtra(FileDownloader
.EXTRA_FILE_SIZE
, mFile
.getFileLength());
207 getActivity().startService(i
);
210 case R
.id
.fdKeepInSync
: {
211 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
212 mFile
.setKeepInSync(cb
.isChecked());
213 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getApplicationContext().getContentResolver());
214 fdsm
.saveFile(mFile
);
215 if (mFile
.keepInSync() && !mFile
.isDownloaded()) {
216 onClick(getView().findViewById(R
.id
.fdDownloadBtn
));
220 case R
.id
.fdRenameBtn
: {
221 EditNameFragment dialog
= EditNameFragment
.newInstance(mFile
.getFileName());
222 dialog
.show(getFragmentManager(), "nameeditdialog");
223 dialog
.setOnDismissListener(this);
227 Log
.e(TAG
, "Incorrect view clicked!");
230 /* else if (v.getId() == R.id.fdShareBtn) {
231 Thread t = new Thread(new ShareRunnable(mFile.getRemotePath()));
238 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
240 * @return True when the fragment was created with the empty layout.
242 public boolean isEmpty() {
243 return mLayout
== R
.layout
.file_details_empty
;
248 * Can be used to get the file that is currently being displayed.
249 * @return The file on the screen.
251 public OCFile
getDisplayedFile(){
256 * Use this method to signal this Activity that it shall update its view.
258 * @param file : An {@link OCFile}
260 public void updateFileDetails(OCFile file
, Account ocAccount
) {
262 mAccount
= ocAccount
;
268 * Updates the view with all relevant details about that file.
270 public void updateFileDetails() {
272 if (mFile
!= null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
) {
274 Button downloadButton
= (Button
) getView().findViewById(R
.id
.fdDownloadBtn
);
276 setFilename(mFile
.getFileName());
277 setFiletype(DisplayUtils
.convertMIMEtoPrettyPrint(mFile
279 setFilesize(mFile
.getFileLength());
280 if(ocVersionSupportsTimeCreated()){
281 setTimeCreated(mFile
.getCreationTimestamp());
284 setTimeModified(mFile
.getModificationTimestamp());
286 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
287 cb
.setChecked(mFile
.keepInSync());
288 cb
.setOnClickListener(this);
289 //getView().findViewById(R.id.fdShareBtn).setOnClickListener(this);
290 getView().findViewById(R
.id
.fdRenameBtn
).setOnClickListener(this);
292 if (mFile
.getStoragePath() != null
) {
294 ImageView preview
= (ImageView
) getView().findViewById(R
.id
.fdPreview
);
296 if (mFile
.getMimetype().startsWith("image/")) {
297 BitmapFactory
.Options options
= new Options();
298 options
.inScaled
= true
;
299 options
.inPurgeable
= true
;
300 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
301 options
.inPreferQualityOverSpeed
= false
;
303 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
304 options
.inMutable
= false
;
307 Bitmap bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath(), options
);
310 int width
= options
.outWidth
;
311 int height
= options
.outHeight
;
313 boolean recycle
= false
;
314 if (width
>= 2048 || height
>= 2048) {
315 scale
= (int) (Math
.ceil(Math
.max(height
, width
)/2048.));
316 options
.inSampleSize
= scale
;
319 Display display
= getActivity().getWindowManager().getDefaultDisplay();
320 Point size
= new Point();
322 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB_MR2
) {
323 display
.getSize(size
);
324 screenwidth
= size
.x
;
326 screenwidth
= display
.getWidth();
329 Log
.e("ASD", "W " + width
+ " SW " + screenwidth
);
331 if (width
> screenwidth
) {
332 scale
= (int) (Math
.ceil(Math
.max(height
, width
)/screenwidth
));
333 options
.inSampleSize
= scale
;
338 if (recycle
) bmp
.recycle();
339 bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath(), options
);
343 preview
.setImageBitmap(bmp
);
346 } catch (OutOfMemoryError e
) {
347 preview
.setVisibility(View
.INVISIBLE
);
348 Log
.e(TAG
, "Out of memory occured for file with size " + mFile
.getFileLength());
350 } catch (NoSuchFieldError e
) {
351 preview
.setVisibility(View
.INVISIBLE
);
352 Log
.e(TAG
, "Error from access to unexisting field despite protection " + mFile
.getFileLength());
354 } catch (Throwable t
) {
355 preview
.setVisibility(View
.INVISIBLE
);
356 Log
.e(TAG
, "Unexpected error while creating image preview " + mFile
.getFileLength(), t
);
359 // Change download button to open button
360 downloadButton
.setText(R
.string
.filedetails_open
);
361 downloadButton
.setOnClickListener(new OnClickListener() {
363 public void onClick(View v
) {
364 String storagePath
= mFile
.getStoragePath();
366 Intent i
= new Intent(Intent
.ACTION_VIEW
);
367 i
.setDataAndType(Uri
.parse("file://"+ storagePath
), mFile
.getMimetype());
368 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
371 } catch (Throwable t
) {
372 Log
.e(TAG
, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile
.getMimetype());
373 boolean toastIt
= true
;
374 String mimeType
= "";
376 Intent i
= new Intent(Intent
.ACTION_VIEW
);
377 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
378 if (mimeType
!= null
&& !mimeType
.equals(mFile
.getMimetype())) {
379 i
.setDataAndType(Uri
.parse("file://"+mFile
.getStoragePath()), mimeType
);
380 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
385 } catch (IndexOutOfBoundsException e
) {
386 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + storagePath
);
388 } catch (ActivityNotFoundException e
) {
389 Log
.e(TAG
, "No activity found to handle: " + storagePath
+ " with MIME type " + mimeType
+ " obtained from extension");
391 } catch (Throwable th
) {
392 Log
.e(TAG
, "Unexpected problem when opening: " + storagePath
, th
);
396 Toast
.makeText(getActivity(), "There is no application to handle file " + mFile
.getFileName(), Toast
.LENGTH_SHORT
).show();
404 // Make download button effective
405 downloadButton
.setOnClickListener(this);
412 * Updates the filename in view
413 * @param filename to set
415 private void setFilename(String filename
) {
416 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
418 tv
.setText(filename
);
422 * Updates the MIME type in view
423 * @param mimetype to set
425 private void setFiletype(String mimetype
) {
426 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
428 tv
.setText(mimetype
);
432 * Updates the file size in view
433 * @param filesize in bytes to set
435 private void setFilesize(long filesize
) {
436 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
438 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
442 * Updates the time that the file was created in view
443 * @param milliseconds Unix time to set
445 private void setTimeCreated(long milliseconds
){
446 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
447 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
449 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
450 tv
.setVisibility(View
.VISIBLE
);
451 tvLabel
.setVisibility(View
.VISIBLE
);
456 * Updates the time that the file was last modified
457 * @param milliseconds Unix time to set
459 private void setTimeModified(long milliseconds
){
460 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
462 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
467 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
468 * the time that the file was created. There is a chance that this will
469 * be fixed in future versions. Use this method to check if this version of
470 * ownCloud has this fix.
471 * @return True, if ownCloud the ownCloud version is supporting creation time
473 private boolean ocVersionSupportsTimeCreated(){
474 /*if(mAccount != null){
475 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
476 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
477 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
478 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
486 * Once the file download has finished -> update view
487 * @author Bartek Przybylski
489 private class DownloadFinishReceiver
extends BroadcastReceiver
{
491 public void onReceive(Context context
, Intent intent
) {
492 if (getView()!=null
&& getView().findViewById(R
.id
.fdDownloadBtn
) != null
)
493 getView().findViewById(R
.id
.fdDownloadBtn
).setEnabled(true
);
495 if (intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
)) {
496 mFile
.setStoragePath(intent
.getStringExtra(FileDownloader
.EXTRA_FILE_PATH
));
498 } else if (intent
.getAction().equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
499 Toast
.makeText(context
, R
.string
.downloader_download_failed
, Toast
.LENGTH_SHORT
).show();
505 // this is a temporary class for sharing purposes, it need to be replacead in transfer service
506 private class ShareRunnable
implements Runnable
{
507 private String mPath
;
509 public ShareRunnable(String path
) {
514 AccountManager am
= AccountManager
.get(getActivity());
515 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
516 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
));
517 String url
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + AccountUtils
.getWebdavPath(ocv
);
519 Log
.d("share", "sharing for version " + ocv
.toString());
521 if (ocv
.compareTo(new OwnCloudVersion(0x040000)) >= 0) {
522 String APPS_PATH
= "/apps/files_sharing/";
523 String SHARE_PATH
= "ajax/share.php";
525 String SHARED_PATH
= "/apps/files_sharing/get.php?token=";
527 final String WEBDAV_SCRIPT
= "webdav.php";
528 final String WEBDAV_FILES_LOCATION
= "/files/";
530 WebdavClient wc
= new WebdavClient();
531 HttpConnectionManagerParams params
= new HttpConnectionManagerParams();
532 params
.setMaxConnectionsPerHost(wc
.getHostConfiguration(), 5);
534 //wc.getParams().setParameter("http.protocol.single-cookie-header", true);
535 //wc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
537 PostMethod post
= new PostMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + APPS_PATH
+ SHARE_PATH
);
539 post
.addRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8" );
540 post
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
541 List
<NameValuePair
> formparams
= new ArrayList
<NameValuePair
>();
542 Log
.d("share", mPath
+"");
543 formparams
.add(new BasicNameValuePair("sources",mPath
));
544 formparams
.add(new BasicNameValuePair("uid_shared_with", "public"));
545 formparams
.add(new BasicNameValuePair("permissions", "0"));
546 post
.setRequestEntity(new StringRequestEntity(URLEncodedUtils
.format(formparams
, HTTP
.UTF_8
)));
550 PropFindMethod find
= new PropFindMethod(url
+"/");
551 find
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
552 Log
.d("sharer", ""+ url
+"/");
553 wc
.setCredentials(account
.name
.substring(0, account
.name
.lastIndexOf('@')), am
.getPassword(account
));
555 for (org
.apache
.commons
.httpclient
.Header a
: find
.getRequestHeaders()) {
556 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
559 int status2
= wc
.executeMethod(find
);
561 Log
.d("sharer", "propstatus "+status2
);
563 GetMethod get
= new GetMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + "/");
564 get
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
566 status2
= wc
.executeMethod(get
);
568 Log
.d("sharer", "getstatus "+status2
);
569 Log
.d("sharer", "" + get
.getResponseBodyAsString());
571 for (org
.apache
.commons
.httpclient
.Header a
: get
.getResponseHeaders()) {
572 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
575 status
= wc
.executeMethod(post
);
576 for (org
.apache
.commons
.httpclient
.Header a
: post
.getRequestHeaders()) {
577 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
579 for (org
.apache
.commons
.httpclient
.Header a
: post
.getResponseHeaders()) {
580 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
582 String resp
= post
.getResponseBodyAsString();
583 Log
.d("share", ""+post
.getURI().toString());
584 Log
.d("share", "returned status " + status
);
585 Log
.d("share", " " +resp
);
587 if(status
!= HttpStatus
.SC_OK
||resp
== null
|| resp
.equals("") || resp
.startsWith("false")) {
591 JSONObject jsonObject
= new JSONObject (resp
);
592 String jsonStatus
= jsonObject
.getString("status");
593 if(!jsonStatus
.equals("success")) throw new Exception("Error while sharing file status != success");
595 String token
= jsonObject
.getString("data");
596 String uri
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + SHARED_PATH
+ token
;
597 Log
.d("Actions:shareFile ok", "url: " + uri
);
599 } catch (HttpException e
) {
600 // TODO Auto-generated catch block
602 } catch (IOException e
) {
603 // TODO Auto-generated catch block
605 } catch (JSONException e
) {
606 // TODO Auto-generated catch block
608 } catch (Exception e
) {
609 // TODO Auto-generated catch block
613 } else if (ocv
.compareTo(new OwnCloudVersion(0x030000)) >= 0) {
619 public void onDismiss(EditNameFragment dialog
) {
620 Log
.e("ASD","ondismiss");
621 if (dialog
instanceof EditNameFragment
) {
622 if (((EditNameFragment
)dialog
).getResult()) {
623 String newFilename
= ((EditNameFragment
)dialog
).getNewFilename();
624 Log
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
625 if (!newFilename
.equals(mFile
.getFileName())) {
626 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
627 if (fdsm
.getFileById(mFile
.getFileId()) != null
) {
628 OCFile newFile
= new OCFile(fdsm
.getFileById(mFile
.getParentId()).getRemotePath()+"/"+newFilename
);
629 newFile
.setCreationTimestamp(mFile
.getCreationTimestamp());
630 newFile
.setFileId(mFile
.getFileId());
631 newFile
.setFileLength(mFile
.getFileLength());
632 newFile
.setKeepInSync(mFile
.keepInSync());
633 newFile
.setLastSyncDate(mFile
.getLastSyncDate());
634 newFile
.setMimetype(mFile
.getMimetype());
635 newFile
.setModificationTimestamp(mFile
.getModificationTimestamp());
636 newFile
.setParentId(mFile
.getParentId());
637 newFile
.setStoragePath(mFile
.getStoragePath());
639 new Thread(new RenameRunnable(mFile
, newFile
, mAccount
, new Handler())).start();
645 Log
.e(TAG
, "Unknown dialog intance passed to onDismissDalog: " + dialog
.getClass().getCanonicalName());
650 private class RenameRunnable
implements Runnable
{
656 public RenameRunnable(OCFile oldFile
, OCFile newFile
, Account account
, Handler handler
) {
664 WebdavClient wc
= new WebdavClient(mAccount
, getSherlockActivity().getApplicationContext());
665 AccountManager am
= AccountManager
.get(getSherlockActivity());
666 String baseUrl
= am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
667 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
668 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
669 Log
.d("ASD", ""+baseUrl
+ webdav_path
+ mOld
.getRemotePath());
671 Log
.e("ASD", Uri
.parse(baseUrl
).getPath() == null ?
"" : Uri
.parse(baseUrl
).getPath() + webdav_path
+ mNew
.getRemotePath());
672 LocalMoveMethod move
= new LocalMoveMethod(baseUrl
+ webdav_path
+ mOld
.getRemotePath(),
673 Uri
.parse(baseUrl
).getPath() == null ?
"" : Uri
.parse(baseUrl
).getPath() + webdav_path
+ mNew
.getRemotePath());
676 int status
= wc
.executeMethod(move
);
677 if (move
.succeeded()) {
678 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
679 fdsm
.removeFile(mOld
);
682 mHandler
.post(new Runnable() {
684 public void run() { updateFileDetails(mFile
, mAccount
); }
687 Log
.e("ASD", ""+move
.getQueryString());
688 Log
.d("move", "returned status " + status
);
689 } catch (HttpException e
) {
690 // TODO Auto-generated catch block
692 } catch (IOException e
) {
693 // TODO Auto-generated catch block
697 private class LocalMoveMethod
extends DavMethodBase
{
699 public LocalMoveMethod(String uri
, String dest
) {
701 addRequestHeader(new org
.apache
.commons
.httpclient
.Header("Destination", dest
));
705 public String
getName() {
710 protected boolean isSuccess(int status
) {
711 return status
== 201 || status
== 204;
717 private static class EditNameFragment
extends SherlockDialogFragment
implements OnClickListener
{
719 private String mNewFilename
;
720 private boolean mResult
;
721 private FileDetailFragment mListener
;
723 static public EditNameFragment
newInstance(String filename
) {
724 EditNameFragment f
= new EditNameFragment();
725 Bundle args
= new Bundle();
726 args
.putString("filename", filename
);
727 f
.setArguments(args
);
732 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
733 View v
= inflater
.inflate(R
.layout
.edit_box_dialog
, container
, false
);
735 String currentName
= getArguments().getString("filename", "");
737 ((Button
)v
.findViewById(R
.id
.cancel
)).setOnClickListener(this);
738 ((Button
)v
.findViewById(R
.id
.ok
)).setOnClickListener(this);
739 ((TextView
)v
.findViewById(R
.id
.user_input
)).setText(currentName
);
740 ((TextView
)v
.findViewById(R
.id
.user_input
)).requestFocus();
741 getDialog().getWindow().setSoftInputMode(LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
748 public void onClick(View view
) {
749 switch (view
.getId()) {
751 mNewFilename
= ((TextView
)getView().findViewById(R
.id
.user_input
)).getText().toString();
754 case R
.id
.cancel
: { // fallthought
756 mListener
.onDismiss(this);
761 void setOnDismissListener(FileDetailFragment listener
) {
762 mListener
= listener
;
765 public String
getNewFilename() {
769 // true if user click ok
770 public boolean getResult() {