1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package eu
.alefzero
.owncloud
.ui
.fragment
;
21 import java
.io
.IOException
;
22 import java
.util
.ArrayList
;
23 import java
.util
.List
;
25 import org
.apache
.commons
.httpclient
.HostConfiguration
;
26 import org
.apache
.commons
.httpclient
.HttpException
;
27 import org
.apache
.commons
.httpclient
.cookie
.CookiePolicy
;
28 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
29 import org
.apache
.commons
.httpclient
.methods
.PostMethod
;
30 import org
.apache
.commons
.httpclient
.methods
.StringRequestEntity
;
31 import org
.apache
.commons
.httpclient
.params
.HttpConnectionManagerParams
;
32 import org
.apache
.commons
.httpclient
.params
.HttpMethodParams
;
33 import org
.apache
.http
.HttpStatus
;
34 import org
.apache
.http
.NameValuePair
;
35 import org
.apache
.http
.client
.utils
.URLEncodedUtils
;
36 import org
.apache
.http
.message
.BasicNameValuePair
;
37 import org
.apache
.http
.protocol
.HTTP
;
38 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DavMethodBase
;
39 import org
.apache
.jackrabbit
.webdav
.client
.methods
.DeleteMethod
;
40 import org
.apache
.jackrabbit
.webdav
.client
.methods
.MoveMethod
;
41 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
42 import org
.json
.JSONException
;
43 import org
.json
.JSONObject
;
45 import android
.accounts
.Account
;
46 import android
.accounts
.AccountManager
;
47 import android
.app
.AlertDialog
;
48 import android
.app
.Dialog
;
49 import android
.content
.ActivityNotFoundException
;
50 import android
.content
.BroadcastReceiver
;
51 import android
.content
.Context
;
52 import android
.content
.DialogInterface
;
53 import android
.content
.DialogInterface
.OnDismissListener
;
54 import android
.content
.Intent
;
55 import android
.content
.IntentFilter
;
56 import android
.content
.res
.Resources
.NotFoundException
;
57 import android
.graphics
.Bitmap
;
58 import android
.graphics
.BitmapFactory
;
59 import android
.graphics
.BitmapFactory
.Options
;
60 import android
.graphics
.Point
;
61 import android
.graphics
.drawable
.BitmapDrawable
;
62 import android
.graphics
.drawable
.Drawable
;
63 import android
.net
.Uri
;
64 import android
.os
.AsyncTask
;
65 import android
.os
.Bundle
;
66 import android
.os
.Environment
;
67 import android
.os
.Handler
;
68 import android
.preference
.PreferenceActivity
.Header
;
69 import android
.support
.v4
.app
.FragmentTransaction
;
70 import android
.util
.Log
;
71 import android
.view
.Display
;
72 import android
.view
.LayoutInflater
;
73 import android
.view
.View
;
74 import android
.view
.View
.OnClickListener
;
75 import android
.view
.ViewGroup
;
76 import android
.view
.WindowManager
.LayoutParams
;
77 import android
.webkit
.MimeTypeMap
;
78 import android
.widget
.Button
;
79 import android
.widget
.CheckBox
;
80 import android
.widget
.EditText
;
81 import android
.widget
.ImageView
;
82 import android
.widget
.TextView
;
83 import android
.widget
.Toast
;
85 import com
.actionbarsherlock
.app
.SherlockDialogFragment
;
86 import com
.actionbarsherlock
.app
.SherlockFragment
;
88 import eu
.alefzero
.owncloud
.AccountUtils
;
89 import eu
.alefzero
.owncloud
.DisplayUtils
;
90 import eu
.alefzero
.owncloud
.R
;
91 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
;
92 import eu
.alefzero
.owncloud
.datamodel
.FileDataStorageManager
;
93 import eu
.alefzero
.owncloud
.datamodel
.OCFile
;
94 import eu
.alefzero
.owncloud
.files
.services
.FileDownloader
;
95 import eu
.alefzero
.owncloud
.ui
.activity
.FileDisplayActivity
;
96 import eu
.alefzero
.owncloud
.utils
.OwnCloudVersion
;
97 import eu
.alefzero
.webdav
.WebdavClient
;
98 import eu
.alefzero
.webdav
.WebdavUtils
;
101 * This Fragment is used to display the details about a file.
103 * @author Bartek Przybylski
106 public class FileDetailFragment
extends SherlockFragment
implements
107 OnClickListener
, ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
{
109 public static final String EXTRA_FILE
= "FILE";
110 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
114 private OCFile mFile
;
115 private Account mAccount
;
116 private ImageView mPreview
;
118 private DownloadFinishReceiver mDownloadFinishReceiver
;
120 private static final String TAG
= "FileDetailFragment";
121 public static final String FTAG
= "FileDetails";
122 public static final String FTAG_CONFIRMATION
= "REMOVE_CONFIRMATION_FRAGMENT";
126 * Creates an empty details fragment.
128 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
130 public FileDetailFragment() {
133 mLayout
= R
.layout
.file_details_empty
;
138 * Creates a details fragment.
140 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
142 * @param fileToDetail An {@link OCFile} to show in the fragment
143 * @param ocAccount An ownCloud account; needed to start downloads
145 public FileDetailFragment(OCFile fileToDetail
, Account ocAccount
){
146 mFile
= fileToDetail
;
147 mAccount
= ocAccount
;
148 mLayout
= R
.layout
.file_details_empty
;
150 if(fileToDetail
!= null
&& ocAccount
!= null
) {
151 mLayout
= R
.layout
.file_details_fragment
;
157 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
158 Bundle savedInstanceState
) {
159 super.onCreateView(inflater
, container
, savedInstanceState
);
161 if (savedInstanceState
!= null
) {
162 mFile
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_FILE
);
163 mAccount
= savedInstanceState
.getParcelable(FileDetailFragment
.EXTRA_ACCOUNT
);
167 view
= inflater
.inflate(mLayout
, container
, false
);
170 if (mLayout
== R
.layout
.file_details_fragment
) {
171 mView
.findViewById(R
.id
.fdKeepInSync
).setOnClickListener(this);
172 //mView.findViewById(R.id.fdShareBtn).setOnClickListener(this);
173 mView
.findViewById(R
.id
.fdRenameBtn
).setOnClickListener(this);
174 mView
.findViewById(R
.id
.fdRemoveBtn
).setOnClickListener(this);
175 mPreview
= (ImageView
)mView
.findViewById(R
.id
.fdPreview
);
184 public void onSaveInstanceState(Bundle outState
) {
185 Log
.i(getClass().toString(), "onSaveInstanceState() start");
186 super.onSaveInstanceState(outState
);
187 outState
.putParcelable(FileDetailFragment
.EXTRA_FILE
, mFile
);
188 outState
.putParcelable(FileDetailFragment
.EXTRA_ACCOUNT
, mAccount
);
189 Log
.i(getClass().toString(), "onSaveInstanceState() end");
194 public void onResume() {
196 mDownloadFinishReceiver
= new DownloadFinishReceiver();
197 IntentFilter filter
= new IntentFilter(
198 FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
199 getActivity().registerReceiver(mDownloadFinishReceiver
, filter
);
203 public void onPause() {
205 getActivity().unregisterReceiver(mDownloadFinishReceiver
);
206 mDownloadFinishReceiver
= null
;
207 if (mPreview
!= null
) {
213 public View
getView() {
214 return super.getView() == null ? mView
: super.getView();
220 public void onClick(View v
) {
222 case R
.id
.fdDownloadBtn
: {
223 //Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
224 Intent i
= new Intent(getActivity(), FileDownloader
.class);
225 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
226 i
.putExtra(FileDownloader
.EXTRA_REMOTE_PATH
, mFile
.getRemotePath());
227 i
.putExtra(FileDownloader
.EXTRA_FILE_PATH
, mFile
.getRemotePath());
228 i
.putExtra(FileDownloader
.EXTRA_FILE_SIZE
, mFile
.getFileLength());
230 getActivity().startService(i
);
233 case R
.id
.fdKeepInSync
: {
234 CheckBox cb
= (CheckBox
) getView().findViewById(R
.id
.fdKeepInSync
);
235 mFile
.setKeepInSync(cb
.isChecked());
236 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getApplicationContext().getContentResolver());
237 fdsm
.saveFile(mFile
);
238 if (mFile
.keepInSync() && !mFile
.isDownloaded()) {
239 onClick(getView().findViewById(R
.id
.fdDownloadBtn
));
243 case R
.id
.fdRenameBtn
: {
244 EditNameFragment dialog
= EditNameFragment
.newInstance(mFile
.getFileName());
245 dialog
.show(getFragmentManager(), "nameeditdialog");
246 dialog
.setOnDismissListener(this);
249 case R
.id
.fdRemoveBtn
: {
250 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance("to remove " + mFile
.getFileName());
251 confDialog
.setOnConfirmationListener(this);
252 confDialog
.show(getFragmentManager(), FTAG_CONFIRMATION
);
256 Log
.e(TAG
, "Incorrect view clicked!");
259 /* else if (v.getId() == R.id.fdShareBtn) {
260 Thread t = new Thread(new ShareRunnable(mFile.getRemotePath()));
267 public void onConfirmation(boolean confirmation
, String callerTag
) {
268 if (confirmation
&& callerTag
.equals(FTAG_CONFIRMATION
)) {
269 Log
.e("ASD","onConfirmation");
270 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
271 if (fdsm
.getFileById(mFile
.getFileId()) != null
) {
272 new Thread(new RemoveRunnable(mFile
, mAccount
, new Handler())).start();
274 } else if (!confirmation
) Log
.d(TAG
, "REMOVAL CANCELED");
279 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
281 * @return True when the fragment was created with the empty layout.
283 public boolean isEmpty() {
284 return mLayout
== R
.layout
.file_details_empty
;
289 * Can be used to get the file that is currently being displayed.
290 * @return The file on the screen.
292 public OCFile
getDisplayedFile(){
297 * Use this method to signal this Activity that it shall update its view.
299 * @param file : An {@link OCFile}
301 public void updateFileDetails(OCFile file
, Account ocAccount
) {
303 mAccount
= ocAccount
;
309 * Updates the view with all relevant details about that file.
311 public void updateFileDetails() {
313 if (mFile
!= null
&& mAccount
!= null
&& mLayout
== R
.layout
.file_details_fragment
) {
315 Button downloadButton
= (Button
) getView().findViewById(R
.id
.fdDownloadBtn
);
317 setFilename(mFile
.getFileName());
318 setFiletype(DisplayUtils
.convertMIMEtoPrettyPrint(mFile
320 setFilesize(mFile
.getFileLength());
321 if(ocVersionSupportsTimeCreated()){
322 setTimeCreated(mFile
.getCreationTimestamp());
325 setTimeModified(mFile
.getModificationTimestamp());
327 CheckBox cb
= (CheckBox
)getView().findViewById(R
.id
.fdKeepInSync
);
328 cb
.setChecked(mFile
.keepInSync());
330 if (mFile
.getStoragePath() != null
) {
332 if (mFile
.getMimetype().startsWith("image/")) {
333 BitmapLoader bl
= new BitmapLoader();
334 bl
.execute(new String
[]{mFile
.getStoragePath()});
337 // Change download button to open button
338 downloadButton
.setText(R
.string
.filedetails_open
);
339 downloadButton
.setOnClickListener(new OnClickListener() {
341 public void onClick(View v
) {
342 String storagePath
= mFile
.getStoragePath();
344 Intent i
= new Intent(Intent
.ACTION_VIEW
);
345 i
.setDataAndType(Uri
.parse("file://"+ storagePath
), mFile
.getMimetype());
346 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
349 } catch (Throwable t
) {
350 Log
.e(TAG
, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile
.getMimetype());
351 boolean toastIt
= true
;
352 String mimeType
= "";
354 Intent i
= new Intent(Intent
.ACTION_VIEW
);
355 mimeType
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(storagePath
.substring(storagePath
.lastIndexOf('.') + 1));
356 if (mimeType
!= null
&& !mimeType
.equals(mFile
.getMimetype())) {
357 i
.setDataAndType(Uri
.parse("file://"+mFile
.getStoragePath()), mimeType
);
358 i
.setFlags(Intent
.FLAG_GRANT_READ_URI_PERMISSION
| Intent
.FLAG_GRANT_WRITE_URI_PERMISSION
);
363 } catch (IndexOutOfBoundsException e
) {
364 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + storagePath
);
366 } catch (ActivityNotFoundException e
) {
367 Log
.e(TAG
, "No activity found to handle: " + storagePath
+ " with MIME type " + mimeType
+ " obtained from extension");
369 } catch (Throwable th
) {
370 Log
.e(TAG
, "Unexpected problem when opening: " + storagePath
, th
);
374 Toast
.makeText(getActivity(), "There is no application to handle file " + mFile
.getFileName(), Toast
.LENGTH_SHORT
).show();
382 // Make download button effective
383 downloadButton
.setOnClickListener(this);
390 * Updates the filename in view
391 * @param filename to set
393 private void setFilename(String filename
) {
394 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdFilename
);
396 tv
.setText(filename
);
400 * Updates the MIME type in view
401 * @param mimetype to set
403 private void setFiletype(String mimetype
) {
404 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdType
);
406 tv
.setText(mimetype
);
410 * Updates the file size in view
411 * @param filesize in bytes to set
413 private void setFilesize(long filesize
) {
414 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdSize
);
416 tv
.setText(DisplayUtils
.bytesToHumanReadable(filesize
));
420 * Updates the time that the file was created in view
421 * @param milliseconds Unix time to set
423 private void setTimeCreated(long milliseconds
){
424 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdCreated
);
425 TextView tvLabel
= (TextView
) getView().findViewById(R
.id
.fdCreatedLabel
);
427 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
428 tv
.setVisibility(View
.VISIBLE
);
429 tvLabel
.setVisibility(View
.VISIBLE
);
434 * Updates the time that the file was last modified
435 * @param milliseconds Unix time to set
437 private void setTimeModified(long milliseconds
){
438 TextView tv
= (TextView
) getView().findViewById(R
.id
.fdModified
);
440 tv
.setText(DisplayUtils
.unixTimeToHumanReadable(milliseconds
));
445 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
446 * the time that the file was created. There is a chance that this will
447 * be fixed in future versions. Use this method to check if this version of
448 * ownCloud has this fix.
449 * @return True, if ownCloud the ownCloud version is supporting creation time
451 private boolean ocVersionSupportsTimeCreated(){
452 /*if(mAccount != null){
453 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
454 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
455 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
456 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
464 * Once the file download has finished -> update view
465 * @author Bartek Przybylski
467 private class DownloadFinishReceiver
extends BroadcastReceiver
{
469 public void onReceive(Context context
, Intent intent
) {
470 if (getView()!=null
&& getView().findViewById(R
.id
.fdDownloadBtn
) != null
)
471 getView().findViewById(R
.id
.fdDownloadBtn
).setEnabled(true
);
473 if (intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
)) {
474 mFile
.setStoragePath(intent
.getStringExtra(FileDownloader
.EXTRA_FILE_PATH
));
476 } else if (intent
.getAction().equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
477 Toast
.makeText(context
, R
.string
.downloader_download_failed
, Toast
.LENGTH_SHORT
).show();
483 // this is a temporary class for sharing purposes, it need to be replaced in transfer service
484 private class ShareRunnable
implements Runnable
{
485 private String mPath
;
487 public ShareRunnable(String path
) {
492 AccountManager am
= AccountManager
.get(getActivity());
493 Account account
= AccountUtils
.getCurrentOwnCloudAccount(getActivity());
494 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_VERSION
));
495 String url
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + AccountUtils
.getWebdavPath(ocv
);
497 Log
.d("share", "sharing for version " + ocv
.toString());
499 if (ocv
.compareTo(new OwnCloudVersion(0x040000)) >= 0) {
500 String APPS_PATH
= "/apps/files_sharing/";
501 String SHARE_PATH
= "ajax/share.php";
503 String SHARED_PATH
= "/apps/files_sharing/get.php?token=";
505 final String WEBDAV_SCRIPT
= "webdav.php";
506 final String WEBDAV_FILES_LOCATION
= "/files/";
508 WebdavClient wc
= new WebdavClient();
509 HttpConnectionManagerParams params
= new HttpConnectionManagerParams();
510 params
.setMaxConnectionsPerHost(wc
.getHostConfiguration(), 5);
512 //wc.getParams().setParameter("http.protocol.single-cookie-header", true);
513 //wc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
515 PostMethod post
= new PostMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + APPS_PATH
+ SHARE_PATH
);
517 post
.addRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8" );
518 post
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
519 List
<NameValuePair
> formparams
= new ArrayList
<NameValuePair
>();
520 Log
.d("share", mPath
+"");
521 formparams
.add(new BasicNameValuePair("sources",mPath
));
522 formparams
.add(new BasicNameValuePair("uid_shared_with", "public"));
523 formparams
.add(new BasicNameValuePair("permissions", "0"));
524 post
.setRequestEntity(new StringRequestEntity(URLEncodedUtils
.format(formparams
, HTTP
.UTF_8
)));
528 PropFindMethod find
= new PropFindMethod(url
+"/");
529 find
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
530 Log
.d("sharer", ""+ url
+"/");
531 wc
.setCredentials(account
.name
.substring(0, account
.name
.lastIndexOf('@')), am
.getPassword(account
));
533 for (org
.apache
.commons
.httpclient
.Header a
: find
.getRequestHeaders()) {
534 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
537 int status2
= wc
.executeMethod(find
);
539 Log
.d("sharer", "propstatus "+status2
);
541 GetMethod get
= new GetMethod(am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + "/");
542 get
.addRequestHeader("Referer", am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
));
544 status2
= wc
.executeMethod(get
);
546 Log
.d("sharer", "getstatus "+status2
);
547 Log
.d("sharer", "" + get
.getResponseBodyAsString());
549 for (org
.apache
.commons
.httpclient
.Header a
: get
.getResponseHeaders()) {
550 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
553 status
= wc
.executeMethod(post
);
554 for (org
.apache
.commons
.httpclient
.Header a
: post
.getRequestHeaders()) {
555 Log
.d("sharer-h", a
.getName() + ":"+a
.getValue());
557 for (org
.apache
.commons
.httpclient
.Header a
: post
.getResponseHeaders()) {
558 Log
.d("sharer", a
.getName() + ":"+a
.getValue());
560 String resp
= post
.getResponseBodyAsString();
561 Log
.d("share", ""+post
.getURI().toString());
562 Log
.d("share", "returned status " + status
);
563 Log
.d("share", " " +resp
);
565 if(status
!= HttpStatus
.SC_OK
||resp
== null
|| resp
.equals("") || resp
.startsWith("false")) {
569 JSONObject jsonObject
= new JSONObject (resp
);
570 String jsonStatus
= jsonObject
.getString("status");
571 if(!jsonStatus
.equals("success")) throw new Exception("Error while sharing file status != success");
573 String token
= jsonObject
.getString("data");
574 String uri
= am
.getUserData(account
, AccountAuthenticator
.KEY_OC_BASE_URL
) + SHARED_PATH
+ token
;
575 Log
.d("Actions:shareFile ok", "url: " + uri
);
577 } catch (HttpException e
) {
578 // TODO Auto-generated catch block
580 } catch (IOException e
) {
581 // TODO Auto-generated catch block
583 } catch (JSONException e
) {
584 // TODO Auto-generated catch block
586 } catch (Exception e
) {
587 // TODO Auto-generated catch block
591 } else if (ocv
.compareTo(new OwnCloudVersion(0x030000)) >= 0) {
597 public void onDismiss(EditNameFragment dialog
) {
598 Log
.e("ASD","ondismiss");
599 if (dialog
instanceof EditNameFragment
) {
600 if (((EditNameFragment
)dialog
).getResult()) {
601 String newFilename
= ((EditNameFragment
)dialog
).getNewFilename();
602 Log
.d(TAG
, "name edit dialog dismissed with new name " + newFilename
);
603 if (!newFilename
.equals(mFile
.getFileName())) {
604 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
605 if (fdsm
.getFileById(mFile
.getFileId()) != null
) {
606 OCFile newFile
= new OCFile(fdsm
.getFileById(mFile
.getParentId()).getRemotePath() + OCFile
.PATH_SEPARATOR
+ newFilename
);
607 newFile
.setCreationTimestamp(mFile
.getCreationTimestamp());
608 newFile
.setFileId(mFile
.getFileId());
609 newFile
.setFileLength(mFile
.getFileLength());
610 newFile
.setKeepInSync(mFile
.keepInSync());
611 newFile
.setLastSyncDate(mFile
.getLastSyncDate());
612 newFile
.setMimetype(mFile
.getMimetype());
613 newFile
.setModificationTimestamp(mFile
.getModificationTimestamp());
614 newFile
.setParentId(mFile
.getParentId());
615 newFile
.setStoragePath(mFile
.getStoragePath());
617 new Thread(new RenameRunnable(mFile
, newFile
, mAccount
, new Handler())).start();
623 Log
.e(TAG
, "Unknown dialog intance passed to onDismissDalog: " + dialog
.getClass().getCanonicalName());
628 private class RenameRunnable
implements Runnable
{
634 public RenameRunnable(OCFile oldFile
, OCFile newFile
, Account account
, Handler handler
) {
642 WebdavClient wc
= new WebdavClient(mAccount
, getSherlockActivity().getApplicationContext());
643 AccountManager am
= AccountManager
.get(getSherlockActivity());
644 String baseUrl
= am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
645 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
646 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
647 Log
.d("ASD", ""+baseUrl
+ webdav_path
+ WebdavUtils
.encode(mOld
.getRemotePath()));
649 Log
.e("ASD", Uri
.parse(baseUrl
).getPath() == null ?
"" : Uri
.parse(baseUrl
).getPath() + webdav_path
+ WebdavUtils
.encode(mNew
.getRemotePath()));
650 LocalMoveMethod move
= new LocalMoveMethod(baseUrl
+ webdav_path
+ WebdavUtils
.encode(mOld
.getRemotePath()),
651 Uri
.parse(baseUrl
).getPath() == null ?
"" : Uri
.parse(baseUrl
).getPath() + webdav_path
+ WebdavUtils
.encode(mNew
.getRemotePath()));
654 int status
= wc
.executeMethod(move
);
655 if (move
.succeeded()) {
656 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
657 fdsm
.removeFile(mOld
);
660 mHandler
.post(new Runnable() {
662 public void run() { updateFileDetails(mFile
, mAccount
); }
665 Log
.e("ASD", ""+move
.getQueryString());
666 Log
.d("move", "returned status " + status
);
667 } catch (HttpException e
) {
668 // TODO Auto-generated catch block
670 } catch (IOException e
) {
671 // TODO Auto-generated catch block
675 private class LocalMoveMethod
extends DavMethodBase
{
677 public LocalMoveMethod(String uri
, String dest
) {
679 addRequestHeader(new org
.apache
.commons
.httpclient
.Header("Destination", dest
));
683 public String
getName() {
688 protected boolean isSuccess(int status
) {
689 return status
== 201 || status
== 204;
695 private static class EditNameFragment
extends SherlockDialogFragment
implements OnClickListener
{
697 private String mNewFilename
;
698 private boolean mResult
;
699 private FileDetailFragment mListener
;
701 static public EditNameFragment
newInstance(String filename
) {
702 EditNameFragment f
= new EditNameFragment();
703 Bundle args
= new Bundle();
704 args
.putString("filename", filename
);
705 f
.setArguments(args
);
710 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
, Bundle savedInstanceState
) {
711 View v
= inflater
.inflate(R
.layout
.edit_box_dialog
, container
, false
);
713 String currentName
= getArguments().getString("filename");
714 if (currentName
== null
)
717 ((Button
)v
.findViewById(R
.id
.cancel
)).setOnClickListener(this);
718 ((Button
)v
.findViewById(R
.id
.ok
)).setOnClickListener(this);
719 ((TextView
)v
.findViewById(R
.id
.user_input
)).setText(currentName
);
720 ((TextView
)v
.findViewById(R
.id
.user_input
)).requestFocus();
721 getDialog().getWindow().setSoftInputMode(LayoutParams
.SOFT_INPUT_STATE_VISIBLE
);
728 public void onClick(View view
) {
729 switch (view
.getId()) {
731 mNewFilename
= ((TextView
)getView().findViewById(R
.id
.user_input
)).getText().toString();
734 case R
.id
.cancel
: { // fallthought
736 mListener
.onDismiss(this);
741 void setOnDismissListener(FileDetailFragment listener
) {
742 mListener
= listener
;
745 public String
getNewFilename() {
749 // true if user click ok
750 public boolean getResult() {
757 private class RemoveRunnable
implements Runnable
{
760 OCFile mFileToRemove
;
763 public RemoveRunnable(OCFile fileToRemove
, Account account
, Handler handler
) {
764 mFileToRemove
= fileToRemove
;
770 WebdavClient wc
= new WebdavClient(mAccount
, getSherlockActivity().getApplicationContext());
771 AccountManager am
= AccountManager
.get(getSherlockActivity());
772 String baseUrl
= am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_BASE_URL
);
773 OwnCloudVersion ocv
= new OwnCloudVersion(am
.getUserData(mAccount
, AccountAuthenticator
.KEY_OC_VERSION
));
774 String webdav_path
= AccountUtils
.getWebdavPath(ocv
);
775 Log
.d("ASD", ""+baseUrl
+ webdav_path
+ WebdavUtils
.encode(mFileToRemove
.getRemotePath()));
777 DeleteMethod delete
= new DeleteMethod(baseUrl
+ webdav_path
+ WebdavUtils
.encode(mFileToRemove
.getRemotePath()));
778 HttpMethodParams params
= delete
.getParams();
779 params
.setSoTimeout(1000);
780 delete
.setParams(params
);
782 boolean success
= false
;
784 int status
= wc
.executeMethod(delete
);
785 if (delete
.succeeded()) {
786 FileDataStorageManager fdsm
= new FileDataStorageManager(mAccount
, getActivity().getContentResolver());
787 fdsm
.removeFile(mFileToRemove
);
788 mHandler
.post(new Runnable() {
792 Toast msg
= Toast
.makeText(getActivity().getApplicationContext(), R
.string
.remove_success_msg
, Toast
.LENGTH_LONG
);
794 if (getActivity() instanceof FileDisplayActivity
) {
796 FragmentTransaction transaction
= getActivity().getSupportFragmentManager().beginTransaction();
797 transaction
.replace(R
.id
.file_details_container
, new FileDetailFragment(null
, null
)); // empty FileDetailFragment
798 transaction
.commit();
801 getActivity().finish();
804 } catch (NotFoundException e
) {
811 Log
.e("ASD", ""+ delete
.getQueryString());
812 Log
.d("delete", "returned status " + status
);
814 } catch (HttpException e
) {
817 } catch (IOException e
) {
822 mHandler
.post(new Runnable() {
826 Toast msg
= Toast
.makeText(getActivity(), R
.string
.remove_fail_msg
, Toast
.LENGTH_LONG
);
829 } catch (NotFoundException e
) {
840 class BitmapLoader
extends AsyncTask
<String
, Void
, Bitmap
> {
842 protected Bitmap
doInBackground(String
... params
) {
843 Bitmap result
= null
;
844 if (params
.length
!= 1) return result
;
845 String storagePath
= params
[0];
848 BitmapFactory
.Options options
= new Options();
849 options
.inScaled
= true
;
850 options
.inPurgeable
= true
;
851 options
.inJustDecodeBounds
= true
;
852 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
853 options
.inPreferQualityOverSpeed
= false
;
855 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB
) {
856 options
.inMutable
= false
;
859 result
= BitmapFactory
.decodeFile(storagePath
, options
);
860 options
.inJustDecodeBounds
= false
;
862 int width
= options
.outWidth
;
863 int height
= options
.outHeight
;
865 boolean recycle
= false
;
866 if (width
>= 2048 || height
>= 2048) {
867 scale
= (int) Math
.ceil((Math
.ceil(Math
.max(height
, width
) / 2048.)));
868 options
.inSampleSize
= scale
;
870 Display display
= getActivity().getWindowManager().getDefaultDisplay();
871 Point size
= new Point();
873 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.HONEYCOMB_MR2
) {
874 display
.getSize(size
);
875 screenwidth
= size
.x
;
877 screenwidth
= display
.getWidth();
880 Log
.e("ASD", "W " + width
+ " SW " + screenwidth
);
882 if (width
> screenwidth
) {
883 scale
= (int) Math
.ceil((float)width
/ screenwidth
);
884 options
.inSampleSize
= scale
;
887 result
= BitmapFactory
.decodeFile(storagePath
, options
);
889 Log
.e("ASD", "W " + options
.outWidth
+ " SW " + options
.outHeight
);
891 } catch (OutOfMemoryError e
) {
893 Log
.e(TAG
, "Out of memory occured for file with size " + storagePath
);
895 } catch (NoSuchFieldError e
) {
897 Log
.e(TAG
, "Error from access to unexisting field despite protection " + storagePath
);
899 } catch (Throwable t
) {
901 Log
.e(TAG
, "Unexpected error while creating image preview " + storagePath
, t
);
906 protected void onPostExecute(Bitmap result
) {
907 if (result
!= null
) {
908 mPreview
.setImageBitmap(result
);