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
.preference
.PreferenceActivity
.Header
;
61 import android
.util
.Log
;
62 import android
.view
.Display
;
63 import android
.view
.LayoutInflater
;
64 import android
.view
.View
;
65 import android
.view
.View
.OnClickListener
;
66 import android
.view
.ViewGroup
;
67 import android
.view
.WindowManager
.LayoutParams
;
68 import android
.webkit
.MimeTypeMap
;
69 import android
.widget
.Button
;
70 import android
.widget
.CheckBox
;
71 import android
.widget
.EditText
;
72 import android
.widget
.ImageView
;
73 import android
.widget
.TextView
;
74 import android
.widget
.Toast
;
76 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
77 import com
.actionbarsherlock
.app
.SherlockFragment
;
79 import eu
.alefzero
.owncloud
.AccountUtils
;
80 import eu
.alefzero
.owncloud
.DisplayUtils
;
81 import eu
.alefzero
.owncloud
.R
;
82 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
83 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
84 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
85 import eu
.alefzero
.owncloud
.files
.services
.FileDownloader
;
86 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
87 import eu
.alefzero
.webdav
.WebdavClient
;
90 * This Fragment is used to display the details about a file.
92 * @author Bartek Przybylski
95 public class FileDetailFragment
extends SherlockFragment
implements
98 public static final String EXTRA_FILE
= "FILE";
99 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
103 private OCFile mFile
;
104 private Account mAccount
;
106 private DownloadFinishReceiver mDownloadFinishReceiver
;
108 private static final String TAG
= "FileDetailFragment";
109 public static final String FTAG
= "FileDetails";
113 * Creates an empty details fragment.
115 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
117 public FileDetailFragment() {
120 mLayout
= R
.layout
.file_details_empty
;
125 * Creates a details fragment.
127 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
129 * @param fileToDetail An {@link OCFile} to show in the fragment
130 * @param ocAccount An ownCloud account; needed to start downloads
132 public FileDetailFragment(OCFile fileToDetail
, Account ocAccount
){
133 mFile
= fileToDetail
;
134 mAccount
= ocAccount
;
135 mLayout
= R
.layout
.file_details_empty
;
137 if(fileToDetail
!= null
&& ocAccount
!= null
) {
138 mLayout
= R
.layout
.file_details_fragment
;
144 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
145 Bundle savedInstanceState
) {
146 super.onCreateView(inflater
, container
, savedInstanceState
);
148 if (savedInstanceState
!= null
) {
149 mFile
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_FILE
);
150 mAccount
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_ACCOUNT
);
154 view
= inflater
.inflate(mLayout
, container
, false
);
163 public void onSaveInstanceState(Bundle outState
) {
164 Log
.i(getClass().toString(), "onSaveInstanceState() start");
165 super.onSaveInstanceState(outState
);
166 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, mFile
);
167 outState
.putParcelable(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
168 Log
.i(getClass().toString(), "onSaveInstanceState() end");
173 public void onResume() {
175 mDownloadFinishReceiver
= new DownloadFinishReceiver();
176 IntentFilter filter
= new IntentFilter(
177 FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
178 getActivity().registerReceiver(mDownloadFinishReceiver
, filter
);
182 public void onPause() {
184 getActivity().unregisterReceiver(mDownloadFinishReceiver
);
185 mDownloadFinishReceiver
= null
;
189 public View
getView() {
190 return super.getView() == null ? mView
: super.getView();
196 public void onClick(View v
) {
198 case R
.id
.fdDownloadBtn
: {
199 //Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
200 Intent i
= new Intent(getActivity(), FileDownloader
.class);
201 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
202 i
.putExtra(FileDownloader
.EXTRA_REMOTE_PATH
, mFile
.getRemotePath());
203 i
.putExtra(FileDownloader
.EXTRA_FILE_PATH
, mFile
.getURLDecodedRemotePath());
204 i
.putExtra(FileDownloader
.EXTRA_FILE_SIZE
, mFile
.getFileLength());
206 getActivity().startService(i
);
209 case R
.id
.fdKeepInSync
: {
210 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
211 mFile
.setKeepInSync(cb
.isChecked());
212 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getApplicationContext().getContentResolver());
213 fdsm
.saveFile(mFile
);
214 if (mFile
.keepInSync() && !mFile
.isDownloaded()) {
215 onClick(getView().findViewById(R
.id
.fdDownloadBtn
));
219 case R
.id
.fdRenameBtn
: {
220 EditNameFragment dialog
= EditNameFragment
.newInstance(mFile
.getFileName());
221 dialog
.show(getFragmentManager(), "nameeditdialog");
222 dialog
.setOnDismissListener(this);
226 Log
.e(TAG
, "Incorrect view clicked!");
229 /* else if (v.getId() == R.id.fdShareBtn) {
230 Thread t = new Thread(new ShareRunnable(mFile.getRemotePath()));
237 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
239 * @return True when the fragment was created with the empty layout.
241 public boolean isEmpty() {
242 return mLayout
== R
.layout
.file_details_empty
;
247 * Can be used to get the file that is currently being displayed.
248 * @return The file on the screen.
250 public OCFile
getDisplayedFile(){
255 * Use this method to signal this Activity that it shall update its view.
257 * @param file : An {@link OCFile}
259 public void updateFileDetails(OCFile file
, Account ocAccount
) {
261 mAccount
= ocAccount
;
267 * Updates the view with all relevant details about that file.
269 public void updateFileDetails() {
271 if (mFile
!= null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
) {
273 Button downloadButton
= (Button
) getView().findViewById(R
.id
.fdDownloadBtn
);
275 setFilename(mFile
.getFileName());
276 setFiletype(DisplayUtils
.convertMIMEtoPrettyPrint(mFile
278 setFilesize(mFile
.getFileLength());
279 if(ocVersionSupportsTimeCreated()){
280 setTimeCreated(mFile
.getCreationTimestamp());
283 setTimeModified(mFile
.getModificationTimestamp());
285 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
286 cb
.setChecked(mFile
.keepInSync());
287 cb
.setOnClickListener(this);
288 //getView().findViewById(R.id.fdShareBtn).setOnClickListener(this);
289 getView().findViewById(R
.id
.fdRenameBtn
).setOnClickListener(this);
291 if (mFile
.getStoragePath() != null
) {
293 ImageView preview
= (ImageView
) getView().findViewById(R
.id
.fdPreview
);
295 if (mFile
.getMimetype().startsWith("image/")) {
296 BitmapFactory
.Options options
= new Options();
297 options
.inScaled
= true
;
298 options
.inPurgeable
= true
;
299 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
300 options
.inPreferQualityOverSpeed
= false
;
302 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
303 options
.inMutable
= false
;
306 Bitmap bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath(), options
);
309 int width
= options
.outWidth
;
310 int height
= options
.outHeight
;
312 boolean recycle
= false
;
313 if (width
>= 2048 || height
>= 2048) {
314 scale
= (int) (Math
.ceil(Math
.max(height
, width
)/2048.));
315 options
.inSampleSize
= scale
;
318 Display display
= getActivity().getWindowManager().getDefaultDisplay();
319 Point size
= new Point();
321 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB_MR2
) {
322 display
.getSize(size
);
323 screenwidth
= size
.x
;
325 screenwidth
= display
.getWidth();
328 Log
.e("ASD", "W " + width
+ " SW " + screenwidth
);
330 if (width
> screenwidth
) {
331 scale
= (int) (Math
.ceil(Math
.max(height
, width
)/screenwidth
));
332 options
.inSampleSize
= scale
;
337 if (recycle
) bmp
.recycle();
338 bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath(), options
);
342 preview
.setImageBitmap(bmp
);
345 } catch (OutOfMemoryError e
) {
346 preview
.setVisibility(View
.INVISIBLE
);
347 Log
.e(TAG
, "Out of memory occured for file with size " + mFile
.getFileLength());
349 } catch (NoSuchFieldError e
) {
350 preview
.setVisibility(View
.INVISIBLE
);
351 Log
.e(TAG
, "Error from access to unexisting field despite protection " + mFile
.getFileLength());
353 } catch (Throwable t
) {
354 preview
.setVisibility(View
.INVISIBLE
);
355 Log
.e(TAG
, "Unexpected error while creating image preview " + mFile
.getFileLength(), t
);
358 // Change download button to open button
359 downloadButton
.setText(R
.string
.filedetails_open
);
360 downloadButton
.setOnClickListener(new OnClickListener() {
362 public void onClick(View v
) {
363 String storagePath
= mFile
.getStoragePath();
365 Intent i
= new Intent(Intent
.ACTION_VIEW
);
366 i
.setDataAndType(Uri
.parse("file://"+ storagePath
), mFile
.getMimetype());
367 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
370 } catch (Throwable t
) {
371 Log
.e(TAG
, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile
.getMimetype());
372 boolean toastIt
= true
;
373 String mimeType
= "";
375 Intent i
= new Intent(Intent
.ACTION_VIEW
);
376 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
377 if (mimeType
!= null
&& !mimeType
.equals(mFile
.getMimetype())) {
378 i
.setDataAndType(Uri
.parse("file://"+mFile
.getStoragePath()), mimeType
);
379 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
384 } catch (IndexOutOfBoundsException e
) {
385 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + storagePath
);
387 } catch (ActivityNotFoundException e
) {
388 Log
.e(TAG
, "No activity found to handle: " + storagePath
+ " with MIME type " + mimeType
+ " obtained from extension");
390 } catch (Throwable th
) {
391 Log
.e(TAG
, "Unexpected problem when opening: " + storagePath
, th
);
395 Toast
.makeText(getActivity(), "There is no application to handle file " + mFile
.getFileName(), Toast
.LENGTH_SHORT
).show();
403 // Make download button effective
404 downloadButton
.setOnClickListener(this);
411 * Updates the filename in view
412 * @param filename to set
414 private void setFilename(String filename
) {
415 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
417 tv
.setText(filename
);
421 * Updates the MIME type in view
422 * @param mimetype to set
424 private void setFiletype(String mimetype
) {
425 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
427 tv
.setText(mimetype
);
431 * Updates the file size in view
432 * @param filesize in bytes to set
434 private void setFilesize(long filesize
) {
435 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
437 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
441 * Updates the time that the file was created in view
442 * @param milliseconds Unix time to set
444 private void setTimeCreated(long milliseconds
){
445 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
446 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
448 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
449 tv
.setVisibility(View
.VISIBLE
);
450 tvLabel
.setVisibility(View
.VISIBLE
);
455 * Updates the time that the file was last modified
456 * @param milliseconds Unix time to set
458 private void setTimeModified(long milliseconds
){
459 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
461 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
466 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
467 * the time that the file was created. There is a chance that this will
468 * be fixed in future versions. Use this method to check if this version of
469 * ownCloud has this fix.
470 * @return True, if ownCloud the ownCloud version is supporting creation time
472 private boolean ocVersionSupportsTimeCreated(){
473 /*if(mAccount != null){
474 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
475 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
476 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
477 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
485 * Once the file download has finished -> update view
486 * @author Bartek Przybylski
488 private class DownloadFinishReceiver
extends BroadcastReceiver
{
490 public void onReceive(Context context
, Intent intent
) {
491 if (getView()!=null
&& getView().findViewById(R
.id
.fdDownloadBtn
) != null
)
492 getView().findViewById(R
.id
.fdDownloadBtn
).setEnabled(true
);
494 if (intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
)) {
495 mFile
.setStoragePath(intent
.getStringExtra(FileDownloader
.EXTRA_FILE_PATH
));
497 } else if (intent
.getAction().equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
498 Toast
.makeText(context
, R
.string
.downloader_download_failed
, Toast
.LENGTH_SHORT
).show();
504 // this is a temporary class for sharing purposes, it need to be replacead in transfer service
505 private class ShareRunnable
implements Runnable
{
506 private String mPath
;
508 public ShareRunnable(String path
) {
513 AccountManager am
= AccountManager
.get(getActivity());
514 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
515 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
));
516 String url
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + AccountUtils
.getWebdavPath(ocv
);
518 Log
.d("share", "sharing for version " + ocv
.toString());
520 if (ocv
.compareTo(new OwnCloudVersion(0x040000)) >= 0) {
521 String APPS_PATH
= "/apps/files_sharing/";
522 String SHARE_PATH
= "ajax/share.php";
524 String SHARED_PATH
= "/apps/files_sharing/get.php?token=";
526 final String WEBDAV_SCRIPT
= "webdav.php";
527 final String WEBDAV_FILES_LOCATION
= "/files/";
529 WebdavClient wc
= new WebdavClient();
530 HttpConnectionManagerParams params
= new HttpConnectionManagerParams();
531 params
.setMaxConnectionsPerHost(wc
.getHostConfiguration(), 5);
533 //wc.getParams().setParameter("http.protocol.single-cookie-header", true);
534 //wc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
536 PostMethod post
= new PostMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + APPS_PATH
+ SHARE_PATH
);
538 post
.addRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8" );
539 post
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
540 List
<NameValuePair
> formparams
= new ArrayList
<NameValuePair
>();
541 Log
.d("share", mPath
+"");
542 formparams
.add(new BasicNameValuePair("sources",mPath
));
543 formparams
.add(new BasicNameValuePair("uid_shared_with", "public"));
544 formparams
.add(new BasicNameValuePair("permissions", "0"));
545 post
.setRequestEntity(new StringRequestEntity(URLEncodedUtils
.format(formparams
, HTTP
.UTF_8
)));
549 PropFindMethod find
= new PropFindMethod(url
+"/");
550 find
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
551 Log
.d("sharer", ""+ url
+"/");
552 wc
.setCredentials(account
.name
.substring(0, account
.name
.lastIndexOf('@')), am
.getPassword(account
));
554 for (org
.apache
.commons
.httpclient
.Header a
: find
.getRequestHeaders()) {
555 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
558 int status2
= wc
.executeMethod(find
);
560 Log
.d("sharer", "propstatus "+status2
);
562 GetMethod get
= new GetMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + "/");
563 get
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
565 status2
= wc
.executeMethod(get
);
567 Log
.d("sharer", "getstatus "+status2
);
568 Log
.d("sharer", "" + get
.getResponseBodyAsString());
570 for (org
.apache
.commons
.httpclient
.Header a
: get
.getResponseHeaders()) {
571 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
574 status
= wc
.executeMethod(post
);
575 for (org
.apache
.commons
.httpclient
.Header a
: post
.getRequestHeaders()) {
576 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
578 for (org
.apache
.commons
.httpclient
.Header a
: post
.getResponseHeaders()) {
579 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
581 String resp
= post
.getResponseBodyAsString();
582 Log
.d("share", ""+post
.getURI().toString());
583 Log
.d("share", "returned status " + status
);
584 Log
.d("share", " " +resp
);
586 if(status
!= HttpStatus
.SC_OK
||resp
== null
|| resp
.equals("") || resp
.startsWith("false")) {
590 JSONObject jsonObject
= new JSONObject (resp
);
591 String jsonStatus
= jsonObject
.getString("status");
592 if(!jsonStatus
.equals("success")) throw new Exception("Error while sharing file status != success");
594 String token
= jsonObject
.getString("data");
595 String uri
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + SHARED_PATH
+ token
;
596 Log
.d("Actions:shareFile ok", "url: " + uri
);
598 } catch (HttpException e
) {
599 // TODO Auto-generated catch block
601 } catch (IOException e
) {
602 // TODO Auto-generated catch block
604 } catch (JSONException e
) {
605 // TODO Auto-generated catch block
607 } catch (Exception e
) {
608 // TODO Auto-generated catch block
612 } else if (ocv
.compareTo(new OwnCloudVersion(0x030000)) >= 0) {
618 public void onDismiss(EditNameFragment dialog
) {
619 Log
.e("ASD","ondismiss");
620 if (dialog
instanceof EditNameFragment
) {
621 if (((EditNameFragment
)dialog
).getResult()) {
622 String newFilename
= ((EditNameFragment
)dialog
).getNewFilename();
623 Log
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
624 if (!newFilename
.equals(mFile
.getFileName())) {
625 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
626 if (fdsm
.getFileById(mFile
.getFileId()) != null
) {
627 OCFile newFile
= new OCFile(fdsm
.getFileById(mFile
.getParentId()).getRemotePath()+"/"+newFilename
);
628 newFile
.setCreationTimestamp(mFile
.getCreationTimestamp());
629 newFile
.setFileId(mFile
.getFileId());
630 newFile
.setFileLength(mFile
.getFileLength());
631 newFile
.setKeepInSync(mFile
.keepInSync());
632 newFile
.setLastSyncDate(mFile
.getLastSyncDate());
633 newFile
.setMimetype(mFile
.getMimetype());
634 newFile
.setModificationTimestamp(mFile
.getModificationTimestamp());
635 newFile
.setParentId(mFile
.getParentId());
636 newFile
.setStoragePath(mFile
.getStoragePath());
637 fdsm
.removeFile(mFile
);
638 fdsm
.saveFile(newFile
);
639 new Thread(new RenameRunnable(mFile
, newFile
, mAccount
)).start();
641 updateFileDetails(mFile
, mAccount
);
646 Log
.e(TAG
, "Unknown dialog intance passed to onDismissDalog: " + dialog
.getClass().getCanonicalName());
651 private class RenameRunnable
implements Runnable
{
656 public RenameRunnable(OCFile oldFile
, OCFile newFile
, Account account
) {
663 WebdavClient wc
= new WebdavClient(mAccount
, getSherlockActivity().getApplicationContext());
664 AccountManager am
= AccountManager
.get(getSherlockActivity());
665 String baseUrl
= am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
666 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
667 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
668 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 Log
.e("ASD", ""+move
.getQueryString());
678 Log
.d("move", "returned status " + status
);
679 } catch (HttpException e
) {
680 // TODO Auto-generated catch block
682 } catch (IOException e
) {
683 // TODO Auto-generated catch block
687 private class LocalMoveMethod
extends DavMethodBase
{
689 public LocalMoveMethod(String uri
, String dest
) {
691 addRequestHeader(new org
.apache
.commons
.httpclient
.Header("Destination", dest
));
695 public String
getName() {
700 protected boolean isSuccess(int status
) {
701 return status
== 201 || status
== 204;
707 private static class EditNameFragment
extends SherlockDialogFragment
implements OnClickListener
{
709 private String mNewFilename
;
710 private boolean mResult
;
711 private FileDetailFragment mListener
;
713 static public EditNameFragment
newInstance(String filename
) {
714 EditNameFragment f
= new EditNameFragment();
715 Bundle args
= new Bundle();
716 args
.putString("filename", filename
);
717 f
.setArguments(args
);
722 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
723 View v
= inflater
.inflate(R
.layout
.edit_box_dialog
, container
, false
);
725 String currentName
= getArguments().getString("filename", "");
727 ((Button
)v
.findViewById(R
.id
.cancel
)).setOnClickListener(this);
728 ((Button
)v
.findViewById(R
.id
.ok
)).setOnClickListener(this);
729 ((TextView
)v
.findViewById(R
.id
.user_input
)).setText(currentName
);
730 ((TextView
)v
.findViewById(R
.id
.user_input
)).requestFocus();
731 getDialog().getWindow().setSoftInputMode(LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
738 public void onClick(View view
) {
739 switch (view
.getId()) {
741 mNewFilename
= ((TextView
)getView().findViewById(R
.id
.user_input
)).getText().toString();
744 case R
.id
.cancel
: { // fallthought
746 mListener
.onDismiss(this);
751 void setOnDismissListener(FileDetailFragment listener
) {
752 mListener
= listener
;
755 public String
getNewFilename() {
759 // true if user click ok
760 public boolean getResult() {