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
.app
.AlertDialog
;
46 import android
.app
.Dialog
;
47 import android
.content
.ActivityNotFoundException
;
48 import android
.content
.BroadcastReceiver
;
49 import android
.content
.Context
;
50 import android
.content
.DialogInterface
;
51 import android
.content
.DialogInterface
.OnDismissListener
;
52 import android
.content
.Intent
;
53 import android
.content
.IntentFilter
;
54 import android
.graphics
.Bitmap
;
55 import android
.graphics
.BitmapFactory
;
56 import android
.graphics
.BitmapFactory
.Options
;
57 import android
.graphics
.Point
;
58 import android
.graphics
.drawable
.BitmapDrawable
;
59 import android
.graphics
.drawable
.Drawable
;
60 import android
.net
.Uri
;
61 import android
.os
.Bundle
;
62 import android
.os
.Handler
;
63 import android
.preference
.PreferenceActivity
.Header
;
64 import android
.util
.Log
;
65 import android
.view
.Display
;
66 import android
.view
.LayoutInflater
;
67 import android
.view
.View
;
68 import android
.view
.View
.OnClickListener
;
69 import android
.view
.ViewGroup
;
70 import android
.view
.WindowManager
.LayoutParams
;
71 import android
.webkit
.MimeTypeMap
;
72 import android
.widget
.Button
;
73 import android
.widget
.CheckBox
;
74 import android
.widget
.EditText
;
75 import android
.widget
.ImageView
;
76 import android
.widget
.TextView
;
77 import android
.widget
.Toast
;
79 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
80 import com
.actionbarsherlock
.app
.SherlockFragment
;
82 import eu
.alefzero
.owncloud
.AccountUtils
;
83 import eu
.alefzero
.owncloud
.DisplayUtils
;
84 import eu
.alefzero
.owncloud
.R
;
85 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
86 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
87 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
88 import eu
.alefzero
.owncloud
.files
.services
.FileDownloader
;
89 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
90 import eu
.alefzero
.webdav
.WebdavClient
;
93 * This Fragment is used to display the details about a file.
95 * @author Bartek Przybylski
98 public class FileDetailFragment
extends SherlockFragment
implements
99 OnClickListener
, ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
{
101 public static final String EXTRA_FILE
= "FILE";
102 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
106 private OCFile mFile
;
107 private Account mAccount
;
109 private DownloadFinishReceiver mDownloadFinishReceiver
;
111 private static final String TAG
= "FileDetailFragment";
112 public static final String FTAG
= "FileDetails";
116 * Creates an empty details fragment.
118 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
120 public FileDetailFragment() {
123 mLayout
= R
.layout
.file_details_empty
;
128 * Creates a details fragment.
130 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
132 * @param fileToDetail An {@link OCFile} to show in the fragment
133 * @param ocAccount An ownCloud account; needed to start downloads
135 public FileDetailFragment(OCFile fileToDetail
, Account ocAccount
){
136 mFile
= fileToDetail
;
137 mAccount
= ocAccount
;
138 mLayout
= R
.layout
.file_details_empty
;
140 if(fileToDetail
!= null
&& ocAccount
!= null
) {
141 mLayout
= R
.layout
.file_details_fragment
;
147 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
148 Bundle savedInstanceState
) {
149 super.onCreateView(inflater
, container
, savedInstanceState
);
151 if (savedInstanceState
!= null
) {
152 mFile
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_FILE
);
153 mAccount
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_ACCOUNT
);
157 view
= inflater
.inflate(mLayout
, container
, false
);
166 public void onSaveInstanceState(Bundle outState
) {
167 Log
.i(getClass().toString(), "onSaveInstanceState() start");
168 super.onSaveInstanceState(outState
);
169 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, mFile
);
170 outState
.putParcelable(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
171 Log
.i(getClass().toString(), "onSaveInstanceState() end");
176 public void onResume() {
178 mDownloadFinishReceiver
= new DownloadFinishReceiver();
179 IntentFilter filter
= new IntentFilter(
180 FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
181 getActivity().registerReceiver(mDownloadFinishReceiver
, filter
);
185 public void onPause() {
187 getActivity().unregisterReceiver(mDownloadFinishReceiver
);
188 mDownloadFinishReceiver
= null
;
192 public View
getView() {
193 return super.getView() == null ? mView
: super.getView();
199 public void onClick(View v
) {
201 case R
.id
.fdDownloadBtn
: {
202 //Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
203 Intent i
= new Intent(getActivity(), FileDownloader
.class);
204 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
205 i
.putExtra(FileDownloader
.EXTRA_REMOTE_PATH
, mFile
.getRemotePath());
206 i
.putExtra(FileDownloader
.EXTRA_FILE_PATH
, mFile
.getURLDecodedRemotePath());
207 i
.putExtra(FileDownloader
.EXTRA_FILE_SIZE
, mFile
.getFileLength());
209 getActivity().startService(i
);
212 case R
.id
.fdKeepInSync
: {
213 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
214 mFile
.setKeepInSync(cb
.isChecked());
215 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getApplicationContext().getContentResolver());
216 fdsm
.saveFile(mFile
);
217 if (mFile
.keepInSync() && !mFile
.isDownloaded()) {
218 onClick(getView().findViewById(R
.id
.fdDownloadBtn
));
222 case R
.id
.fdRenameBtn
: {
223 EditNameFragment dialog
= EditNameFragment
.newInstance(mFile
.getFileName());
224 dialog
.show(getFragmentManager(), "nameeditdialog");
225 dialog
.setOnDismissListener(this);
228 case R
.id
.fdRemoveBtn
: {
229 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance("remove " + mFile
.getFileName());
230 confDialog
.setOnConfirmationListener(this);
231 confDialog
.show(getFragmentManager(), "REMOVE_CONFIRMATION_FRAGMENT");
235 Log
.e(TAG
, "Incorrect view clicked!");
238 /* else if (v.getId() == R.id.fdShareBtn) {
239 Thread t = new Thread(new ShareRunnable(mFile.getRemotePath()));
246 public void onConfirmation(boolean confirmation
, String callerTag
) {
247 if (confirmation
&& callerTag
.equals("REMOVE_CONFIRMATION_FRAGMENT")) {
248 // TODO remove in a separated thread
254 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
256 * @return True when the fragment was created with the empty layout.
258 public boolean isEmpty() {
259 return mLayout
== R
.layout
.file_details_empty
;
264 * Can be used to get the file that is currently being displayed.
265 * @return The file on the screen.
267 public OCFile
getDisplayedFile(){
272 * Use this method to signal this Activity that it shall update its view.
274 * @param file : An {@link OCFile}
276 public void updateFileDetails(OCFile file
, Account ocAccount
) {
278 mAccount
= ocAccount
;
284 * Updates the view with all relevant details about that file.
286 public void updateFileDetails() {
288 if (mFile
!= null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
) {
290 Button downloadButton
= (Button
) getView().findViewById(R
.id
.fdDownloadBtn
);
292 setFilename(mFile
.getFileName());
293 setFiletype(DisplayUtils
.convertMIMEtoPrettyPrint(mFile
295 setFilesize(mFile
.getFileLength());
296 if(ocVersionSupportsTimeCreated()){
297 setTimeCreated(mFile
.getCreationTimestamp());
300 setTimeModified(mFile
.getModificationTimestamp());
302 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
303 cb
.setChecked(mFile
.keepInSync());
304 cb
.setOnClickListener(this);
305 //getView().findViewById(R.id.fdShareBtn).setOnClickListener(this);
306 getView().findViewById(R
.id
.fdRenameBtn
).setOnClickListener(this);
308 if (mFile
.getStoragePath() != null
) {
310 ImageView preview
= (ImageView
) getView().findViewById(R
.id
.fdPreview
);
312 if (mFile
.getMimetype().startsWith("image/")) {
313 BitmapFactory
.Options options
= new Options();
314 options
.inScaled
= true
;
315 options
.inPurgeable
= true
;
316 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
317 options
.inPreferQualityOverSpeed
= false
;
319 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
320 options
.inMutable
= false
;
323 Bitmap bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath(), options
);
326 int width
= options
.outWidth
;
327 int height
= options
.outHeight
;
329 boolean recycle
= false
;
330 if (width
>= 2048 || height
>= 2048) {
331 scale
= (int) (Math
.ceil(Math
.max(height
, width
)/2048.));
332 options
.inSampleSize
= scale
;
335 Display display
= getActivity().getWindowManager().getDefaultDisplay();
336 Point size
= new Point();
338 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB_MR2
) {
339 display
.getSize(size
);
340 screenwidth
= size
.x
;
342 screenwidth
= display
.getWidth();
345 Log
.e("ASD", "W " + width
+ " SW " + screenwidth
);
347 if (width
> screenwidth
) {
348 scale
= (int) (Math
.ceil(Math
.max(height
, width
)/screenwidth
));
349 options
.inSampleSize
= scale
;
354 if (recycle
) bmp
.recycle();
355 bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath(), options
);
359 preview
.setImageBitmap(bmp
);
362 } catch (OutOfMemoryError e
) {
363 preview
.setVisibility(View
.INVISIBLE
);
364 Log
.e(TAG
, "Out of memory occured for file with size " + mFile
.getFileLength());
366 } catch (NoSuchFieldError e
) {
367 preview
.setVisibility(View
.INVISIBLE
);
368 Log
.e(TAG
, "Error from access to unexisting field despite protection " + mFile
.getFileLength());
370 } catch (Throwable t
) {
371 preview
.setVisibility(View
.INVISIBLE
);
372 Log
.e(TAG
, "Unexpected error while creating image preview " + mFile
.getFileLength(), t
);
375 // Change download button to open button
376 downloadButton
.setText(R
.string
.filedetails_open
);
377 downloadButton
.setOnClickListener(new OnClickListener() {
379 public void onClick(View v
) {
380 String storagePath
= mFile
.getStoragePath();
382 Intent i
= new Intent(Intent
.ACTION_VIEW
);
383 i
.setDataAndType(Uri
.parse("file://"+ storagePath
), mFile
.getMimetype());
384 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
387 } catch (Throwable t
) {
388 Log
.e(TAG
, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile
.getMimetype());
389 boolean toastIt
= true
;
390 String mimeType
= "";
392 Intent i
= new Intent(Intent
.ACTION_VIEW
);
393 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
394 if (mimeType
!= null
&& !mimeType
.equals(mFile
.getMimetype())) {
395 i
.setDataAndType(Uri
.parse("file://"+mFile
.getStoragePath()), mimeType
);
396 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
401 } catch (IndexOutOfBoundsException e
) {
402 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + storagePath
);
404 } catch (ActivityNotFoundException e
) {
405 Log
.e(TAG
, "No activity found to handle: " + storagePath
+ " with MIME type " + mimeType
+ " obtained from extension");
407 } catch (Throwable th
) {
408 Log
.e(TAG
, "Unexpected problem when opening: " + storagePath
, th
);
412 Toast
.makeText(getActivity(), "There is no application to handle file " + mFile
.getFileName(), Toast
.LENGTH_SHORT
).show();
420 // Make download button effective
421 downloadButton
.setOnClickListener(this);
428 * Updates the filename in view
429 * @param filename to set
431 private void setFilename(String filename
) {
432 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
434 tv
.setText(filename
);
438 * Updates the MIME type in view
439 * @param mimetype to set
441 private void setFiletype(String mimetype
) {
442 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
444 tv
.setText(mimetype
);
448 * Updates the file size in view
449 * @param filesize in bytes to set
451 private void setFilesize(long filesize
) {
452 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
454 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
458 * Updates the time that the file was created in view
459 * @param milliseconds Unix time to set
461 private void setTimeCreated(long milliseconds
){
462 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
463 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
465 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
466 tv
.setVisibility(View
.VISIBLE
);
467 tvLabel
.setVisibility(View
.VISIBLE
);
472 * Updates the time that the file was last modified
473 * @param milliseconds Unix time to set
475 private void setTimeModified(long milliseconds
){
476 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
478 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
483 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
484 * the time that the file was created. There is a chance that this will
485 * be fixed in future versions. Use this method to check if this version of
486 * ownCloud has this fix.
487 * @return True, if ownCloud the ownCloud version is supporting creation time
489 private boolean ocVersionSupportsTimeCreated(){
490 /*if(mAccount != null){
491 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
492 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
493 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
494 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
502 * Once the file download has finished -> update view
503 * @author Bartek Przybylski
505 private class DownloadFinishReceiver
extends BroadcastReceiver
{
507 public void onReceive(Context context
, Intent intent
) {
508 if (getView()!=null
&& getView().findViewById(R
.id
.fdDownloadBtn
) != null
)
509 getView().findViewById(R
.id
.fdDownloadBtn
).setEnabled(true
);
511 if (intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
)) {
512 mFile
.setStoragePath(intent
.getStringExtra(FileDownloader
.EXTRA_FILE_PATH
));
514 } else if (intent
.getAction().equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
515 Toast
.makeText(context
, R
.string
.downloader_download_failed
, Toast
.LENGTH_SHORT
).show();
521 // this is a temporary class for sharing purposes, it need to be replaced in transfer service
522 private class ShareRunnable
implements Runnable
{
523 private String mPath
;
525 public ShareRunnable(String path
) {
530 AccountManager am
= AccountManager
.get(getActivity());
531 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
532 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
));
533 String url
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + AccountUtils
.getWebdavPath(ocv
);
535 Log
.d("share", "sharing for version " + ocv
.toString());
537 if (ocv
.compareTo(new OwnCloudVersion(0x040000)) >= 0) {
538 String APPS_PATH
= "/apps/files_sharing/";
539 String SHARE_PATH
= "ajax/share.php";
541 String SHARED_PATH
= "/apps/files_sharing/get.php?token=";
543 final String WEBDAV_SCRIPT
= "webdav.php";
544 final String WEBDAV_FILES_LOCATION
= "/files/";
546 WebdavClient wc
= new WebdavClient();
547 HttpConnectionManagerParams params
= new HttpConnectionManagerParams();
548 params
.setMaxConnectionsPerHost(wc
.getHostConfiguration(), 5);
550 //wc.getParams().setParameter("http.protocol.single-cookie-header", true);
551 //wc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
553 PostMethod post
= new PostMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + APPS_PATH
+ SHARE_PATH
);
555 post
.addRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8" );
556 post
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
557 List
<NameValuePair
> formparams
= new ArrayList
<NameValuePair
>();
558 Log
.d("share", mPath
+"");
559 formparams
.add(new BasicNameValuePair("sources",mPath
));
560 formparams
.add(new BasicNameValuePair("uid_shared_with", "public"));
561 formparams
.add(new BasicNameValuePair("permissions", "0"));
562 post
.setRequestEntity(new StringRequestEntity(URLEncodedUtils
.format(formparams
, HTTP
.UTF_8
)));
566 PropFindMethod find
= new PropFindMethod(url
+"/");
567 find
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
568 Log
.d("sharer", ""+ url
+"/");
569 wc
.setCredentials(account
.name
.substring(0, account
.name
.lastIndexOf('@')), am
.getPassword(account
));
571 for (org
.apache
.commons
.httpclient
.Header a
: find
.getRequestHeaders()) {
572 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
575 int status2
= wc
.executeMethod(find
);
577 Log
.d("sharer", "propstatus "+status2
);
579 GetMethod get
= new GetMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + "/");
580 get
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
582 status2
= wc
.executeMethod(get
);
584 Log
.d("sharer", "getstatus "+status2
);
585 Log
.d("sharer", "" + get
.getResponseBodyAsString());
587 for (org
.apache
.commons
.httpclient
.Header a
: get
.getResponseHeaders()) {
588 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
591 status
= wc
.executeMethod(post
);
592 for (org
.apache
.commons
.httpclient
.Header a
: post
.getRequestHeaders()) {
593 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
595 for (org
.apache
.commons
.httpclient
.Header a
: post
.getResponseHeaders()) {
596 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
598 String resp
= post
.getResponseBodyAsString();
599 Log
.d("share", ""+post
.getURI().toString());
600 Log
.d("share", "returned status " + status
);
601 Log
.d("share", " " +resp
);
603 if(status
!= HttpStatus
.SC_OK
||resp
== null
|| resp
.equals("") || resp
.startsWith("false")) {
607 JSONObject jsonObject
= new JSONObject (resp
);
608 String jsonStatus
= jsonObject
.getString("status");
609 if(!jsonStatus
.equals("success")) throw new Exception("Error while sharing file status != success");
611 String token
= jsonObject
.getString("data");
612 String uri
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + SHARED_PATH
+ token
;
613 Log
.d("Actions:shareFile ok", "url: " + uri
);
615 } catch (HttpException e
) {
616 // TODO Auto-generated catch block
618 } catch (IOException e
) {
619 // TODO Auto-generated catch block
621 } catch (JSONException e
) {
622 // TODO Auto-generated catch block
624 } catch (Exception e
) {
625 // TODO Auto-generated catch block
629 } else if (ocv
.compareTo(new OwnCloudVersion(0x030000)) >= 0) {
635 public void onDismiss(EditNameFragment dialog
) {
636 Log
.e("ASD","ondismiss");
637 if (dialog
instanceof EditNameFragment
) {
638 if (((EditNameFragment
)dialog
).getResult()) {
639 String newFilename
= ((EditNameFragment
)dialog
).getNewFilename();
640 Log
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
641 if (!newFilename
.equals(mFile
.getFileName())) {
642 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
643 if (fdsm
.getFileById(mFile
.getFileId()) != null
) {
644 OCFile newFile
= new OCFile(fdsm
.getFileById(mFile
.getParentId()).getRemotePath()+"/"+newFilename
);
645 newFile
.setCreationTimestamp(mFile
.getCreationTimestamp());
646 newFile
.setFileId(mFile
.getFileId());
647 newFile
.setFileLength(mFile
.getFileLength());
648 newFile
.setKeepInSync(mFile
.keepInSync());
649 newFile
.setLastSyncDate(mFile
.getLastSyncDate());
650 newFile
.setMimetype(mFile
.getMimetype());
651 newFile
.setModificationTimestamp(mFile
.getModificationTimestamp());
652 newFile
.setParentId(mFile
.getParentId());
653 newFile
.setStoragePath(mFile
.getStoragePath());
655 new Thread(new RenameRunnable(mFile
, newFile
, mAccount
, new Handler())).start();
661 Log
.e(TAG
, "Unknown dialog intance passed to onDismissDalog: " + dialog
.getClass().getCanonicalName());
666 private class RenameRunnable
implements Runnable
{
672 public RenameRunnable(OCFile oldFile
, OCFile newFile
, Account account
, Handler handler
) {
680 WebdavClient wc
= new WebdavClient(mAccount
, getSherlockActivity().getApplicationContext());
681 AccountManager am
= AccountManager
.get(getSherlockActivity());
682 String baseUrl
= am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
683 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
684 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
685 Log
.d("ASD", ""+baseUrl
+ webdav_path
+ mOld
.getRemotePath());
687 Log
.e("ASD", Uri
.parse(baseUrl
).getPath() == null ?
"" : Uri
.parse(baseUrl
).getPath() + webdav_path
+ mNew
.getRemotePath());
688 LocalMoveMethod move
= new LocalMoveMethod(baseUrl
+ webdav_path
+ mOld
.getRemotePath(),
689 Uri
.parse(baseUrl
).getPath() == null ?
"" : Uri
.parse(baseUrl
).getPath() + webdav_path
+ mNew
.getRemotePath());
692 int status
= wc
.executeMethod(move
);
693 if (move
.succeeded()) {
694 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
695 fdsm
.removeFile(mOld
);
698 mHandler
.post(new Runnable() {
700 public void run() { updateFileDetails(mFile
, mAccount
); }
703 Log
.e("ASD", ""+move
.getQueryString());
704 Log
.d("move", "returned status " + status
);
705 } catch (HttpException e
) {
706 // TODO Auto-generated catch block
708 } catch (IOException e
) {
709 // TODO Auto-generated catch block
713 private class LocalMoveMethod
extends DavMethodBase
{
715 public LocalMoveMethod(String uri
, String dest
) {
717 addRequestHeader(new org
.apache
.commons
.httpclient
.Header("Destination", dest
));
721 public String
getName() {
726 protected boolean isSuccess(int status
) {
727 return status
== 201 || status
== 204;
733 private static class EditNameFragment
extends SherlockDialogFragment
implements OnClickListener
{
735 private String mNewFilename
;
736 private boolean mResult
;
737 private FileDetailFragment mListener
;
739 static public EditNameFragment
newInstance(String filename
) {
740 EditNameFragment f
= new EditNameFragment();
741 Bundle args
= new Bundle();
742 args
.putString("filename", filename
);
743 f
.setArguments(args
);
748 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
749 View v
= inflater
.inflate(R
.layout
.edit_box_dialog
, container
, false
);
751 String currentName
= getArguments().getString("filename", "");
753 ((Button
)v
.findViewById(R
.id
.cancel
)).setOnClickListener(this);
754 ((Button
)v
.findViewById(R
.id
.ok
)).setOnClickListener(this);
755 ((TextView
)v
.findViewById(R
.id
.user_input
)).setText(currentName
);
756 ((TextView
)v
.findViewById(R
.id
.user_input
)).requestFocus();
757 getDialog().getWindow().setSoftInputMode(LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
764 public void onClick(View view
) {
765 switch (view
.getId()) {
767 mNewFilename
= ((TextView
)getView().findViewById(R
.id
.user_input
)).getText().toString();
770 case R
.id
.cancel
: { // fallthought
772 mListener
.onDismiss(this);
777 void setOnDismissListener(FileDetailFragment listener
) {
778 mListener
= listener
;
781 public String
getNewFilename() {
785 // true if user click ok
786 public boolean getResult() {