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
);
160 if (mLayout
== R
.layout
.file_details_fragment
) {
161 getView().findViewById(R
.id
.fdKeepInSync
).setOnClickListener(this);
162 //getView().findViewById(R.id.fdShareBtn).setOnClickListener(this);
163 getView().findViewById(R
.id
.fdRenameBtn
).setOnClickListener(this);
164 getView().findViewById(R
.id
.fdRemoveBtn
).setOnClickListener(this);
173 public void onSaveInstanceState(Bundle outState
) {
174 Log
.i(getClass().toString(), "onSaveInstanceState() start");
175 super.onSaveInstanceState(outState
);
176 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, mFile
);
177 outState
.putParcelable(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
178 Log
.i(getClass().toString(), "onSaveInstanceState() end");
183 public void onResume() {
185 mDownloadFinishReceiver
= new DownloadFinishReceiver();
186 IntentFilter filter
= new IntentFilter(
187 FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
188 getActivity().registerReceiver(mDownloadFinishReceiver
, filter
);
192 public void onPause() {
194 getActivity().unregisterReceiver(mDownloadFinishReceiver
);
195 mDownloadFinishReceiver
= null
;
199 public View
getView() {
200 return super.getView() == null ? mView
: super.getView();
206 public void onClick(View v
) {
208 case R
.id
.fdDownloadBtn
: {
209 //Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
210 Intent i
= new Intent(getActivity(), FileDownloader
.class);
211 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
212 i
.putExtra(FileDownloader
.EXTRA_REMOTE_PATH
, mFile
.getRemotePath());
213 i
.putExtra(FileDownloader
.EXTRA_FILE_PATH
, mFile
.getURLDecodedRemotePath());
214 i
.putExtra(FileDownloader
.EXTRA_FILE_SIZE
, mFile
.getFileLength());
216 getActivity().startService(i
);
219 case R
.id
.fdKeepInSync
: {
220 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
221 mFile
.setKeepInSync(cb
.isChecked());
222 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getApplicationContext().getContentResolver());
223 fdsm
.saveFile(mFile
);
224 if (mFile
.keepInSync() && !mFile
.isDownloaded()) {
225 onClick(getView().findViewById(R
.id
.fdDownloadBtn
));
229 case R
.id
.fdRenameBtn
: {
230 EditNameFragment dialog
= EditNameFragment
.newInstance(mFile
.getFileName());
231 dialog
.show(getFragmentManager(), "nameeditdialog");
232 dialog
.setOnDismissListener(this);
235 case R
.id
.fdRemoveBtn
: {
236 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance("to remove " + mFile
.getFileName());
237 confDialog
.setOnConfirmationListener(this);
238 confDialog
.show(getFragmentManager(), "REMOVE_CONFIRMATION_FRAGMENT");
242 Log
.e(TAG
, "Incorrect view clicked!");
245 /* else if (v.getId() == R.id.fdShareBtn) {
246 Thread t = new Thread(new ShareRunnable(mFile.getRemotePath()));
253 public void onConfirmation(boolean confirmation
, String callerTag
) {
254 if (confirmation
&& callerTag
.equals("REMOVE_CONFIRMATION_FRAGMENT")) {
255 // TODO remove in a separated thread
261 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
263 * @return True when the fragment was created with the empty layout.
265 public boolean isEmpty() {
266 return mLayout
== R
.layout
.file_details_empty
;
271 * Can be used to get the file that is currently being displayed.
272 * @return The file on the screen.
274 public OCFile
getDisplayedFile(){
279 * Use this method to signal this Activity that it shall update its view.
281 * @param file : An {@link OCFile}
283 public void updateFileDetails(OCFile file
, Account ocAccount
) {
285 mAccount
= ocAccount
;
291 * Updates the view with all relevant details about that file.
293 public void updateFileDetails() {
295 if (mFile
!= null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
) {
297 Button downloadButton
= (Button
) getView().findViewById(R
.id
.fdDownloadBtn
);
299 setFilename(mFile
.getFileName());
300 setFiletype(DisplayUtils
.convertMIMEtoPrettyPrint(mFile
302 setFilesize(mFile
.getFileLength());
303 if(ocVersionSupportsTimeCreated()){
304 setTimeCreated(mFile
.getCreationTimestamp());
307 setTimeModified(mFile
.getModificationTimestamp());
309 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
310 cb
.setChecked(mFile
.keepInSync());
312 if (mFile
.getStoragePath() != null
) {
314 ImageView preview
= (ImageView
) getView().findViewById(R
.id
.fdPreview
);
316 if (mFile
.getMimetype().startsWith("image/")) {
317 BitmapFactory
.Options options
= new Options();
318 options
.inScaled
= true
;
319 options
.inPurgeable
= true
;
320 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
321 options
.inPreferQualityOverSpeed
= false
;
323 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
324 options
.inMutable
= false
;
327 Bitmap bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath(), options
);
330 int width
= options
.outWidth
;
331 int height
= options
.outHeight
;
333 boolean recycle
= false
;
334 if (width
>= 2048 || height
>= 2048) {
335 scale
= (int) (Math
.ceil(Math
.max(height
, width
)/2048.));
336 options
.inSampleSize
= scale
;
339 Display display
= getActivity().getWindowManager().getDefaultDisplay();
340 Point size
= new Point();
342 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB_MR2
) {
343 display
.getSize(size
);
344 screenwidth
= size
.x
;
346 screenwidth
= display
.getWidth();
349 Log
.e("ASD", "W " + width
+ " SW " + screenwidth
);
351 if (width
> screenwidth
) {
352 scale
= (int) (Math
.ceil(Math
.max(height
, width
)/screenwidth
));
353 options
.inSampleSize
= scale
;
358 if (recycle
) bmp
.recycle();
359 bmp
= BitmapFactory
.decodeFile(mFile
.getStoragePath(), options
);
363 preview
.setImageBitmap(bmp
);
366 } catch (OutOfMemoryError e
) {
367 preview
.setVisibility(View
.INVISIBLE
);
368 Log
.e(TAG
, "Out of memory occured for file with size " + mFile
.getFileLength());
370 } catch (NoSuchFieldError e
) {
371 preview
.setVisibility(View
.INVISIBLE
);
372 Log
.e(TAG
, "Error from access to unexisting field despite protection " + mFile
.getFileLength());
374 } catch (Throwable t
) {
375 preview
.setVisibility(View
.INVISIBLE
);
376 Log
.e(TAG
, "Unexpected error while creating image preview " + mFile
.getFileLength(), t
);
379 // Change download button to open button
380 downloadButton
.setText(R
.string
.filedetails_open
);
381 downloadButton
.setOnClickListener(new OnClickListener() {
383 public void onClick(View v
) {
384 String storagePath
= mFile
.getStoragePath();
386 Intent i
= new Intent(Intent
.ACTION_VIEW
);
387 i
.setDataAndType(Uri
.parse("file://"+ storagePath
), mFile
.getMimetype());
388 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
391 } catch (Throwable t
) {
392 Log
.e(TAG
, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile
.getMimetype());
393 boolean toastIt
= true
;
394 String mimeType
= "";
396 Intent i
= new Intent(Intent
.ACTION_VIEW
);
397 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
398 if (mimeType
!= null
&& !mimeType
.equals(mFile
.getMimetype())) {
399 i
.setDataAndType(Uri
.parse("file://"+mFile
.getStoragePath()), mimeType
);
400 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
405 } catch (IndexOutOfBoundsException e
) {
406 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + storagePath
);
408 } catch (ActivityNotFoundException e
) {
409 Log
.e(TAG
, "No activity found to handle: " + storagePath
+ " with MIME type " + mimeType
+ " obtained from extension");
411 } catch (Throwable th
) {
412 Log
.e(TAG
, "Unexpected problem when opening: " + storagePath
, th
);
416 Toast
.makeText(getActivity(), "There is no application to handle file " + mFile
.getFileName(), Toast
.LENGTH_SHORT
).show();
424 // Make download button effective
425 downloadButton
.setOnClickListener(this);
432 * Updates the filename in view
433 * @param filename to set
435 private void setFilename(String filename
) {
436 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
438 tv
.setText(filename
);
442 * Updates the MIME type in view
443 * @param mimetype to set
445 private void setFiletype(String mimetype
) {
446 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
448 tv
.setText(mimetype
);
452 * Updates the file size in view
453 * @param filesize in bytes to set
455 private void setFilesize(long filesize
) {
456 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
458 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
462 * Updates the time that the file was created in view
463 * @param milliseconds Unix time to set
465 private void setTimeCreated(long milliseconds
){
466 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
467 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
469 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
470 tv
.setVisibility(View
.VISIBLE
);
471 tvLabel
.setVisibility(View
.VISIBLE
);
476 * Updates the time that the file was last modified
477 * @param milliseconds Unix time to set
479 private void setTimeModified(long milliseconds
){
480 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
482 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
487 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
488 * the time that the file was created. There is a chance that this will
489 * be fixed in future versions. Use this method to check if this version of
490 * ownCloud has this fix.
491 * @return True, if ownCloud the ownCloud version is supporting creation time
493 private boolean ocVersionSupportsTimeCreated(){
494 /*if(mAccount != null){
495 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
496 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
497 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
498 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
506 * Once the file download has finished -> update view
507 * @author Bartek Przybylski
509 private class DownloadFinishReceiver
extends BroadcastReceiver
{
511 public void onReceive(Context context
, Intent intent
) {
512 if (getView()!=null
&& getView().findViewById(R
.id
.fdDownloadBtn
) != null
)
513 getView().findViewById(R
.id
.fdDownloadBtn
).setEnabled(true
);
515 if (intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
)) {
516 mFile
.setStoragePath(intent
.getStringExtra(FileDownloader
.EXTRA_FILE_PATH
));
518 } else if (intent
.getAction().equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
519 Toast
.makeText(context
, R
.string
.downloader_download_failed
, Toast
.LENGTH_SHORT
).show();
525 // this is a temporary class for sharing purposes, it need to be replaced in transfer service
526 private class ShareRunnable
implements Runnable
{
527 private String mPath
;
529 public ShareRunnable(String path
) {
534 AccountManager am
= AccountManager
.get(getActivity());
535 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
536 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
));
537 String url
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + AccountUtils
.getWebdavPath(ocv
);
539 Log
.d("share", "sharing for version " + ocv
.toString());
541 if (ocv
.compareTo(new OwnCloudVersion(0x040000)) >= 0) {
542 String APPS_PATH
= "/apps/files_sharing/";
543 String SHARE_PATH
= "ajax/share.php";
545 String SHARED_PATH
= "/apps/files_sharing/get.php?token=";
547 final String WEBDAV_SCRIPT
= "webdav.php";
548 final String WEBDAV_FILES_LOCATION
= "/files/";
550 WebdavClient wc
= new WebdavClient();
551 HttpConnectionManagerParams params
= new HttpConnectionManagerParams();
552 params
.setMaxConnectionsPerHost(wc
.getHostConfiguration(), 5);
554 //wc.getParams().setParameter("http.protocol.single-cookie-header", true);
555 //wc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
557 PostMethod post
= new PostMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + APPS_PATH
+ SHARE_PATH
);
559 post
.addRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8" );
560 post
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
561 List
<NameValuePair
> formparams
= new ArrayList
<NameValuePair
>();
562 Log
.d("share", mPath
+"");
563 formparams
.add(new BasicNameValuePair("sources",mPath
));
564 formparams
.add(new BasicNameValuePair("uid_shared_with", "public"));
565 formparams
.add(new BasicNameValuePair("permissions", "0"));
566 post
.setRequestEntity(new StringRequestEntity(URLEncodedUtils
.format(formparams
, HTTP
.UTF_8
)));
570 PropFindMethod find
= new PropFindMethod(url
+"/");
571 find
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
572 Log
.d("sharer", ""+ url
+"/");
573 wc
.setCredentials(account
.name
.substring(0, account
.name
.lastIndexOf('@')), am
.getPassword(account
));
575 for (org
.apache
.commons
.httpclient
.Header a
: find
.getRequestHeaders()) {
576 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
579 int status2
= wc
.executeMethod(find
);
581 Log
.d("sharer", "propstatus "+status2
);
583 GetMethod get
= new GetMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + "/");
584 get
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
586 status2
= wc
.executeMethod(get
);
588 Log
.d("sharer", "getstatus "+status2
);
589 Log
.d("sharer", "" + get
.getResponseBodyAsString());
591 for (org
.apache
.commons
.httpclient
.Header a
: get
.getResponseHeaders()) {
592 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
595 status
= wc
.executeMethod(post
);
596 for (org
.apache
.commons
.httpclient
.Header a
: post
.getRequestHeaders()) {
597 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
599 for (org
.apache
.commons
.httpclient
.Header a
: post
.getResponseHeaders()) {
600 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
602 String resp
= post
.getResponseBodyAsString();
603 Log
.d("share", ""+post
.getURI().toString());
604 Log
.d("share", "returned status " + status
);
605 Log
.d("share", " " +resp
);
607 if(status
!= HttpStatus
.SC_OK
||resp
== null
|| resp
.equals("") || resp
.startsWith("false")) {
611 JSONObject jsonObject
= new JSONObject (resp
);
612 String jsonStatus
= jsonObject
.getString("status");
613 if(!jsonStatus
.equals("success")) throw new Exception("Error while sharing file status != success");
615 String token
= jsonObject
.getString("data");
616 String uri
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + SHARED_PATH
+ token
;
617 Log
.d("Actions:shareFile ok", "url: " + uri
);
619 } catch (HttpException e
) {
620 // TODO Auto-generated catch block
622 } catch (IOException e
) {
623 // TODO Auto-generated catch block
625 } catch (JSONException e
) {
626 // TODO Auto-generated catch block
628 } catch (Exception e
) {
629 // TODO Auto-generated catch block
633 } else if (ocv
.compareTo(new OwnCloudVersion(0x030000)) >= 0) {
639 public void onDismiss(EditNameFragment dialog
) {
640 Log
.e("ASD","ondismiss");
641 if (dialog
instanceof EditNameFragment
) {
642 if (((EditNameFragment
)dialog
).getResult()) {
643 String newFilename
= ((EditNameFragment
)dialog
).getNewFilename();
644 Log
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
645 if (!newFilename
.equals(mFile
.getFileName())) {
646 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
647 if (fdsm
.getFileById(mFile
.getFileId()) != null
) {
648 OCFile newFile
= new OCFile(fdsm
.getFileById(mFile
.getParentId()).getRemotePath()+"/"+newFilename
);
649 newFile
.setCreationTimestamp(mFile
.getCreationTimestamp());
650 newFile
.setFileId(mFile
.getFileId());
651 newFile
.setFileLength(mFile
.getFileLength());
652 newFile
.setKeepInSync(mFile
.keepInSync());
653 newFile
.setLastSyncDate(mFile
.getLastSyncDate());
654 newFile
.setMimetype(mFile
.getMimetype());
655 newFile
.setModificationTimestamp(mFile
.getModificationTimestamp());
656 newFile
.setParentId(mFile
.getParentId());
657 newFile
.setStoragePath(mFile
.getStoragePath());
659 new Thread(new RenameRunnable(mFile
, newFile
, mAccount
, new Handler())).start();
665 Log
.e(TAG
, "Unknown dialog intance passed to onDismissDalog: " + dialog
.getClass().getCanonicalName());
670 private class RenameRunnable
implements Runnable
{
676 public RenameRunnable(OCFile oldFile
, OCFile newFile
, Account account
, Handler handler
) {
684 WebdavClient wc
= new WebdavClient(mAccount
, getSherlockActivity().getApplicationContext());
685 AccountManager am
= AccountManager
.get(getSherlockActivity());
686 String baseUrl
= am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
687 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
688 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
689 Log
.d("ASD", ""+baseUrl
+ webdav_path
+ mOld
.getRemotePath());
691 Log
.e("ASD", Uri
.parse(baseUrl
).getPath() == null ?
"" : Uri
.parse(baseUrl
).getPath() + webdav_path
+ mNew
.getRemotePath());
692 LocalMoveMethod move
= new LocalMoveMethod(baseUrl
+ webdav_path
+ mOld
.getRemotePath(),
693 Uri
.parse(baseUrl
).getPath() == null ?
"" : Uri
.parse(baseUrl
).getPath() + webdav_path
+ mNew
.getRemotePath());
696 int status
= wc
.executeMethod(move
);
697 if (move
.succeeded()) {
698 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
699 fdsm
.removeFile(mOld
);
702 mHandler
.post(new Runnable() {
704 public void run() { updateFileDetails(mFile
, mAccount
); }
707 Log
.e("ASD", ""+move
.getQueryString());
708 Log
.d("move", "returned status " + status
);
709 } catch (HttpException e
) {
710 // TODO Auto-generated catch block
712 } catch (IOException e
) {
713 // TODO Auto-generated catch block
717 private class LocalMoveMethod
extends DavMethodBase
{
719 public LocalMoveMethod(String uri
, String dest
) {
721 addRequestHeader(new org
.apache
.commons
.httpclient
.Header("Destination", dest
));
725 public String
getName() {
730 protected boolean isSuccess(int status
) {
731 return status
== 201 || status
== 204;
737 private static class EditNameFragment
extends SherlockDialogFragment
implements OnClickListener
{
739 private String mNewFilename
;
740 private boolean mResult
;
741 private FileDetailFragment mListener
;
743 static public EditNameFragment
newInstance(String filename
) {
744 EditNameFragment f
= new EditNameFragment();
745 Bundle args
= new Bundle();
746 args
.putString("filename", filename
);
747 f
.setArguments(args
);
752 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
753 View v
= inflater
.inflate(R
.layout
.edit_box_dialog
, container
, false
);
755 String currentName
= getArguments().getString("filename");
756 if (currentName
== null
)
759 ((Button
)v
.findViewById(R
.id
.cancel
)).setOnClickListener(this);
760 ((Button
)v
.findViewById(R
.id
.ok
)).setOnClickListener(this);
761 ((TextView
)v
.findViewById(R
.id
.user_input
)).setText(currentName
);
762 ((TextView
)v
.findViewById(R
.id
.user_input
)).requestFocus();
763 getDialog().getWindow().setSoftInputMode(LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
770 public void onClick(View view
) {
771 switch (view
.getId()) {
773 mNewFilename
= ((TextView
)getView().findViewById(R
.id
.user_input
)).getText().toString();
776 case R
.id
.cancel
: { // fallthought
778 mListener
.onDismiss(this);
783 void setOnDismissListener(FileDetailFragment listener
) {
784 mListener
= listener
;
787 public String
getNewFilename() {
791 // true if user click ok
792 public boolean getResult() {