7101dd0634a65dea067ddf31c8f19370d49b1891
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / ui / fragment / FileDetailFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package eu.alefzero.owncloud.ui.fragment;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.commons.httpclient.HttpException;
26 import org.apache.commons.httpclient.methods.GetMethod;
27 import org.apache.commons.httpclient.methods.PostMethod;
28 import org.apache.commons.httpclient.methods.StringRequestEntity;
29 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
30 import org.apache.http.HttpStatus;
31 import org.apache.http.NameValuePair;
32 import org.apache.http.client.utils.URLEncodedUtils;
33 import org.apache.http.message.BasicNameValuePair;
34 import org.apache.http.protocol.HTTP;
35 import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
36 import org.apache.jackrabbit.webdav.client.methods.DeleteMethod;
37 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
38 import org.json.JSONException;
39 import org.json.JSONObject;
40
41 import android.accounts.Account;
42 import android.accounts.AccountManager;
43 import android.content.ActivityNotFoundException;
44 import android.content.BroadcastReceiver;
45 import android.content.Context;
46 import android.content.Intent;
47 import android.content.IntentFilter;
48 import android.content.res.Resources.NotFoundException;
49 import android.graphics.Bitmap;
50 import android.graphics.BitmapFactory;
51 import android.graphics.BitmapFactory.Options;
52 import android.graphics.Point;
53 import android.net.Uri;
54 import android.os.AsyncTask;
55 import android.os.Bundle;
56 import android.os.Handler;
57 import android.support.v4.app.FragmentTransaction;
58 import android.util.Log;
59 import android.view.Display;
60 import android.view.LayoutInflater;
61 import android.view.View;
62 import android.view.View.OnClickListener;
63 import android.view.ViewGroup;
64 import android.view.WindowManager.LayoutParams;
65 import android.webkit.MimeTypeMap;
66 import android.widget.Button;
67 import android.widget.CheckBox;
68 import android.widget.ImageView;
69 import android.widget.TextView;
70 import android.widget.Toast;
71
72 import com.actionbarsherlock.app.SherlockDialogFragment;
73 import com.actionbarsherlock.app.SherlockFragment;
74
75 import eu.alefzero.owncloud.AccountUtils;
76 import eu.alefzero.owncloud.DisplayUtils;
77 import eu.alefzero.owncloud.R;
78 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
79 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;
80 import eu.alefzero.owncloud.datamodel.OCFile;
81 import eu.alefzero.owncloud.files.services.FileDownloader;
82 import eu.alefzero.owncloud.files.services.FileUploader;
83 import eu.alefzero.owncloud.ui.activity.FileDisplayActivity;
84 import eu.alefzero.owncloud.utils.OwnCloudVersion;
85 import eu.alefzero.webdav.WebdavClient;
86 import eu.alefzero.webdav.WebdavUtils;
87
88 /**
89 * This Fragment is used to display the details about a file.
90 *
91 * @author Bartek Przybylski
92 *
93 */
94 public class FileDetailFragment extends SherlockFragment implements
95 OnClickListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener {
96
97 public static final String EXTRA_FILE = "FILE";
98 public static final String EXTRA_ACCOUNT = "ACCOUNT";
99
100 private int mLayout;
101 private View mView;
102 private OCFile mFile;
103 private Account mAccount;
104 private ImageView mPreview;
105
106 private DownloadFinishReceiver mDownloadFinishReceiver;
107
108 private static final String TAG = "FileDetailFragment";
109 public static final String FTAG = "FileDetails";
110 public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";
111
112
113 /**
114 * Creates an empty details fragment.
115 *
116 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
117 */
118 public FileDetailFragment() {
119 mFile = null;
120 mAccount = null;
121 mLayout = R.layout.file_details_empty;
122 }
123
124
125 /**
126 * Creates a details fragment.
127 *
128 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
129 *
130 * @param fileToDetail An {@link OCFile} to show in the fragment
131 * @param ocAccount An ownCloud account; needed to start downloads
132 */
133 public FileDetailFragment(OCFile fileToDetail, Account ocAccount){
134 mFile = fileToDetail;
135 mAccount = ocAccount;
136 mLayout = R.layout.file_details_empty;
137
138 if(fileToDetail != null && ocAccount != null) {
139 mLayout = R.layout.file_details_fragment;
140 }
141 }
142
143
144 @Override
145 public View onCreateView(LayoutInflater inflater, ViewGroup container,
146 Bundle savedInstanceState) {
147 super.onCreateView(inflater, container, savedInstanceState);
148
149 if (savedInstanceState != null) {
150 mFile = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE);
151 mAccount = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_ACCOUNT);
152 }
153
154 View view = null;
155 view = inflater.inflate(mLayout, container, false);
156 mView = view;
157
158 if (mLayout == R.layout.file_details_fragment) {
159 mView.findViewById(R.id.fdKeepInSync).setOnClickListener(this);
160 mView.findViewById(R.id.fdRenameBtn).setOnClickListener(this);
161 mView.findViewById(R.id.fdDownloadBtn).setOnClickListener(this);
162 mView.findViewById(R.id.fdOpenBtn).setOnClickListener(this);
163 mView.findViewById(R.id.fdRemoveBtn).setOnClickListener(this);
164 //mView.findViewById(R.id.fdShareBtn).setOnClickListener(this);
165 mPreview = (ImageView)mView.findViewById(R.id.fdPreview);
166 }
167
168 updateFileDetails();
169 return view;
170 }
171
172
173 @Override
174 public void onSaveInstanceState(Bundle outState) {
175 Log.i(getClass().toString(), "onSaveInstanceState() start");
176 super.onSaveInstanceState(outState);
177 outState.putParcelable(FileDetailFragment.EXTRA_FILE, mFile);
178 outState.putParcelable(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
179 Log.i(getClass().toString(), "onSaveInstanceState() end");
180 }
181
182
183 @Override
184 public void onResume() {
185 super.onResume();
186 mDownloadFinishReceiver = new DownloadFinishReceiver();
187 IntentFilter filter = new IntentFilter(
188 FileDownloader.DOWNLOAD_FINISH_MESSAGE);
189 getActivity().registerReceiver(mDownloadFinishReceiver, filter);
190 mPreview = (ImageView)mView.findViewById(R.id.fdPreview);
191 }
192
193 @Override
194 public void onPause() {
195 super.onPause();
196 getActivity().unregisterReceiver(mDownloadFinishReceiver);
197 mDownloadFinishReceiver = null;
198 if (mPreview != null) {
199 mPreview = null;
200 }
201 }
202
203 @Override
204 public View getView() {
205 return super.getView() == null ? mView : super.getView();
206 }
207
208
209
210 @Override
211 public void onClick(View v) {
212 switch (v.getId()) {
213 case R.id.fdDownloadBtn: {
214 Intent i = new Intent(getActivity(), FileDownloader.class);
215 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
216 i.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mFile.getRemotePath());
217 i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getRemotePath());
218 i.putExtra(FileDownloader.EXTRA_FILE_SIZE, mFile.getFileLength());
219
220 // update ui
221 Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();
222 setButtonsForDownloading();
223
224 getActivity().startService(i);
225 break;
226 }
227 case R.id.fdKeepInSync: {
228 CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);
229 mFile.setKeepInSync(cb.isChecked());
230 FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());
231 fdsm.saveFile(mFile);
232 if (mFile.keepInSync()) {
233 onClick(getView().findViewById(R.id.fdDownloadBtn));
234 }
235 break;
236 }
237 case R.id.fdRenameBtn: {
238 EditNameFragment dialog = EditNameFragment.newInstance(mFile.getFileName());
239 dialog.show(getFragmentManager(), "nameeditdialog");
240 dialog.setOnDismissListener(this);
241 break;
242 }
243 case R.id.fdRemoveBtn: {
244 ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(R.string.confirmation_remove_alert, new String[]{mFile.getFileName()});
245 confDialog.setOnConfirmationListener(this);
246 confDialog.show(getFragmentManager(), FTAG_CONFIRMATION);
247 break;
248 }
249 case R.id.fdOpenBtn: {
250 String storagePath = mFile.getStoragePath();
251 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
252 try {
253 Intent i = new Intent(Intent.ACTION_VIEW);
254 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype());
255 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
256 startActivity(i);
257
258 } catch (Throwable t) {
259 Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype());
260 boolean toastIt = true;
261 String mimeType = "";
262 try {
263 Intent i = new Intent(Intent.ACTION_VIEW);
264 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
265 if (mimeType != null && !mimeType.equals(mFile.getMimetype())) {
266 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);
267 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
268 startActivity(i);
269 toastIt = false;
270 }
271
272 } catch (IndexOutOfBoundsException e) {
273 Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);
274
275 } catch (ActivityNotFoundException e) {
276 Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");
277
278 } catch (Throwable th) {
279 Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);
280
281 } finally {
282 if (toastIt) {
283 Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();
284 }
285 }
286
287 }
288 break;
289 }
290 default:
291 Log.e(TAG, "Incorrect view clicked!");
292 }
293
294 /* else if (v.getId() == R.id.fdShareBtn) {
295 Thread t = new Thread(new ShareRunnable(mFile.getRemotePath()));
296 t.start();
297 }*/
298 }
299
300
301 @Override
302 public void onConfirmation(boolean confirmation, String callerTag) {
303 if (confirmation && callerTag.equals(FTAG_CONFIRMATION)) {
304 Log.e("ASD","onConfirmation");
305 FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
306 if (fdsm.getFileById(mFile.getFileId()) != null) {
307 new Thread(new RemoveRunnable(mFile, mAccount, new Handler())).start();
308 }
309 } else if (!confirmation) Log.d(TAG, "REMOVAL CANCELED");
310 }
311
312
313 /**
314 * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
315 *
316 * @return True when the fragment was created with the empty layout.
317 */
318 public boolean isEmpty() {
319 return mLayout == R.layout.file_details_empty;
320 }
321
322
323 /**
324 * Can be used to get the file that is currently being displayed.
325 * @return The file on the screen.
326 */
327 public OCFile getDisplayedFile(){
328 return mFile;
329 }
330
331 /**
332 * Use this method to signal this Activity that it shall update its view.
333 *
334 * @param file : An {@link OCFile}
335 */
336 public void updateFileDetails(OCFile file, Account ocAccount) {
337 mFile = file;
338 mAccount = ocAccount;
339 updateFileDetails();
340 }
341
342
343 /**
344 * Updates the view with all relevant details about that file.
345 */
346 public void updateFileDetails() {
347
348 if (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment) {
349
350 // set file details
351 setFilename(mFile.getFileName());
352 setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile
353 .getMimetype()));
354 setFilesize(mFile.getFileLength());
355 if(ocVersionSupportsTimeCreated()){
356 setTimeCreated(mFile.getCreationTimestamp());
357 }
358
359 setTimeModified(mFile.getModificationTimestamp());
360
361 CheckBox cb = (CheckBox)getView().findViewById(R.id.fdKeepInSync);
362 cb.setChecked(mFile.keepInSync());
363
364 // configure UI for depending upon local state of the file
365 if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath())) {
366 setButtonsForDownloading();
367
368 } else if (mFile.isDown()) {
369 // Update preview
370 if (mFile.getMimetype().startsWith("image/")) {
371 BitmapLoader bl = new BitmapLoader();
372 bl.execute(new String[]{mFile.getStoragePath()});
373 }
374
375 setButtonsForDown();
376
377 // Change download button to open button
378 /*downloadButton.setText(R.string.filedetails_open);
379 downloadButton.setOnClickListener(new OnClickListener() {
380 @Override
381 public void onClick(View v) {
382 String storagePath = mFile.getStoragePath();
383 String encodedStoragePath = WebdavUtils.encodePath(storagePath);
384 try {
385 Intent i = new Intent(Intent.ACTION_VIEW);
386 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype());
387 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
388 startActivity(i);
389
390 } catch (Throwable t) {
391 Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype());
392 boolean toastIt = true;
393 String mimeType = "";
394 try {
395 Intent i = new Intent(Intent.ACTION_VIEW);
396 mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
397 if (mimeType != null && !mimeType.equals(mFile.getMimetype())) {
398 i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);
399 i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
400 startActivity(i);
401 toastIt = false;
402 }
403
404 } catch (IndexOutOfBoundsException e) {
405 Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);
406
407 } catch (ActivityNotFoundException e) {
408 Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");
409
410 } catch (Throwable th) {
411 Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);
412
413 } finally {
414 if (toastIt) {
415 Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();
416 }
417 }
418
419 }
420 }
421 });*/
422 } else {
423 setButtonsForRemote();
424 }
425 }
426 }
427
428
429 /**
430 * Updates the filename in view
431 * @param filename to set
432 */
433 private void setFilename(String filename) {
434 TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
435 if (tv != null)
436 tv.setText(filename);
437 }
438
439 /**
440 * Updates the MIME type in view
441 * @param mimetype to set
442 */
443 private void setFiletype(String mimetype) {
444 TextView tv = (TextView) getView().findViewById(R.id.fdType);
445 if (tv != null)
446 tv.setText(mimetype);
447 }
448
449 /**
450 * Updates the file size in view
451 * @param filesize in bytes to set
452 */
453 private void setFilesize(long filesize) {
454 TextView tv = (TextView) getView().findViewById(R.id.fdSize);
455 if (tv != null)
456 tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
457 }
458
459 /**
460 * Updates the time that the file was created in view
461 * @param milliseconds Unix time to set
462 */
463 private void setTimeCreated(long milliseconds){
464 TextView tv = (TextView) getView().findViewById(R.id.fdCreated);
465 TextView tvLabel = (TextView) getView().findViewById(R.id.fdCreatedLabel);
466 if(tv != null){
467 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
468 tv.setVisibility(View.VISIBLE);
469 tvLabel.setVisibility(View.VISIBLE);
470 }
471 }
472
473 /**
474 * Updates the time that the file was last modified
475 * @param milliseconds Unix time to set
476 */
477 private void setTimeModified(long milliseconds){
478 TextView tv = (TextView) getView().findViewById(R.id.fdModified);
479 if(tv != null){
480 tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
481 }
482 }
483
484 /**
485 * Enables or disables buttons for a file being downloaded
486 */
487 private void setButtonsForDownloading() {
488 if (!isEmpty()) {
489 Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
490 downloadButton.setText(R.string.filedetails_download_in_progress);
491 downloadButton.setEnabled(false); // TODO replace it with a 'cancel download' button
492
493 // let's protect the user from himself ;)
494 ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(false);
495 ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(false);
496 ((Button) getView().findViewById(R.id.fdRemoveBtn)).setEnabled(false);
497 }
498 }
499
500 /**
501 * Enables or disables buttons for a file locally available
502 */
503 private void setButtonsForDown() {
504 if (!isEmpty()) {
505 Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
506 downloadButton.setText(R.string.filedetails_redownload);
507 downloadButton.setEnabled(true);
508
509 ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(true);
510 ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(true);
511 ((Button) getView().findViewById(R.id.fdRemoveBtn)).setEnabled(true);
512 }
513 }
514
515 /**
516 * Enables or disables buttons for a file not locally available
517 */
518 private void setButtonsForRemote() {
519 if (!isEmpty()) {
520 Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
521 downloadButton.setText(R.string.filedetails_download);
522 downloadButton.setEnabled(true);
523
524 ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(false);
525 ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(true);
526 ((Button) getView().findViewById(R.id.fdRemoveBtn)).setEnabled(true);
527 }
528 }
529
530
531 /**
532 * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
533 * the time that the file was created. There is a chance that this will
534 * be fixed in future versions. Use this method to check if this version of
535 * ownCloud has this fix.
536 * @return True, if ownCloud the ownCloud version is supporting creation time
537 */
538 private boolean ocVersionSupportsTimeCreated(){
539 /*if(mAccount != null){
540 AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
541 OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
542 .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
543 if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
544 return true;
545 }
546 }*/
547 return false;
548 }
549
550 /**
551 * Once the file download has finished -> update view
552 * @author Bartek Przybylski
553 */
554 private class DownloadFinishReceiver extends BroadcastReceiver {
555 @Override
556 public void onReceive(Context context, Intent intent) {
557 String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
558
559 if (accountName.equals(mAccount.name) && mFile != null) {
560 boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);
561 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
562 if (mFile.getRemotePath().equals(downloadedRemotePath)) {
563 if (downloadWasFine) {
564 mFile.setStoragePath(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH));
565 }
566 updateFileDetails(); // it updates the buttons; must be called although !downloadWasFine
567 }
568 }
569 }
570 }
571
572 // this is a temporary class for sharing purposes, it need to be replaced in transfer service
573 private class ShareRunnable implements Runnable {
574 private String mPath;
575
576 public ShareRunnable(String path) {
577 mPath = path;
578 }
579
580 public void run() {
581 AccountManager am = AccountManager.get(getActivity());
582 Account account = AccountUtils.getCurrentOwnCloudAccount(getActivity());
583 OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(account, AccountAuthenticator.KEY_OC_VERSION));
584 String url = am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + AccountUtils.getWebdavPath(ocv);
585
586 Log.d("share", "sharing for version " + ocv.toString());
587
588 if (ocv.compareTo(new OwnCloudVersion(0x040000)) >= 0) {
589 String APPS_PATH = "/apps/files_sharing/";
590 String SHARE_PATH = "ajax/share.php";
591
592 String SHARED_PATH = "/apps/files_sharing/get.php?token=";
593
594 final String WEBDAV_SCRIPT = "webdav.php";
595 final String WEBDAV_FILES_LOCATION = "/files/";
596
597 WebdavClient wc = new WebdavClient();
598 HttpConnectionManagerParams params = new HttpConnectionManagerParams();
599 params.setMaxConnectionsPerHost(wc.getHostConfiguration(), 5);
600
601 //wc.getParams().setParameter("http.protocol.single-cookie-header", true);
602 //wc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
603
604 PostMethod post = new PostMethod(am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + APPS_PATH + SHARE_PATH);
605
606 post.addRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8" );
607 post.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL));
608 List<NameValuePair> formparams = new ArrayList<NameValuePair>();
609 Log.d("share", mPath+"");
610 formparams.add(new BasicNameValuePair("sources",mPath));
611 formparams.add(new BasicNameValuePair("uid_shared_with", "public"));
612 formparams.add(new BasicNameValuePair("permissions", "0"));
613 post.setRequestEntity(new StringRequestEntity(URLEncodedUtils.format(formparams, HTTP.UTF_8)));
614
615 int status;
616 try {
617 PropFindMethod find = new PropFindMethod(url+"/");
618 find.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL));
619 Log.d("sharer", ""+ url+"/");
620 wc.setCredentials(account.name.substring(0, account.name.lastIndexOf('@')), am.getPassword(account));
621
622 for (org.apache.commons.httpclient.Header a : find.getRequestHeaders()) {
623 Log.d("sharer-h", a.getName() + ":"+a.getValue());
624 }
625
626 int status2 = wc.executeMethod(find);
627
628 Log.d("sharer", "propstatus "+status2);
629
630 GetMethod get = new GetMethod(am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + "/");
631 get.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL));
632
633 status2 = wc.executeMethod(get);
634
635 Log.d("sharer", "getstatus "+status2);
636 Log.d("sharer", "" + get.getResponseBodyAsString());
637
638 for (org.apache.commons.httpclient.Header a : get.getResponseHeaders()) {
639 Log.d("sharer", a.getName() + ":"+a.getValue());
640 }
641
642 status = wc.executeMethod(post);
643 for (org.apache.commons.httpclient.Header a : post.getRequestHeaders()) {
644 Log.d("sharer-h", a.getName() + ":"+a.getValue());
645 }
646 for (org.apache.commons.httpclient.Header a : post.getResponseHeaders()) {
647 Log.d("sharer", a.getName() + ":"+a.getValue());
648 }
649 String resp = post.getResponseBodyAsString();
650 Log.d("share", ""+post.getURI().toString());
651 Log.d("share", "returned status " + status);
652 Log.d("share", " " +resp);
653
654 if(status != HttpStatus.SC_OK ||resp == null || resp.equals("") || resp.startsWith("false")) {
655 return;
656 }
657
658 JSONObject jsonObject = new JSONObject (resp);
659 String jsonStatus = jsonObject.getString("status");
660 if(!jsonStatus.equals("success")) throw new Exception("Error while sharing file status != success");
661
662 String token = jsonObject.getString("data");
663 String uri = am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + SHARED_PATH + token;
664 Log.d("Actions:shareFile ok", "url: " + uri);
665
666 } catch (HttpException e) {
667 // TODO Auto-generated catch block
668 e.printStackTrace();
669 } catch (IOException e) {
670 // TODO Auto-generated catch block
671 e.printStackTrace();
672 } catch (JSONException e) {
673 // TODO Auto-generated catch block
674 e.printStackTrace();
675 } catch (Exception e) {
676 // TODO Auto-generated catch block
677 e.printStackTrace();
678 }
679
680 } else if (ocv.compareTo(new OwnCloudVersion(0x030000)) >= 0) {
681
682 }
683 }
684 }
685
686 public void onDismiss(EditNameFragment dialog) {
687 if (dialog instanceof EditNameFragment) {
688 if (((EditNameFragment)dialog).getResult()) {
689 String newFilename = ((EditNameFragment)dialog).getNewFilename();
690 Log.d(TAG, "name edit dialog dismissed with new name " + newFilename);
691 if (!newFilename.equals(mFile.getFileName())) {
692 FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
693 if (fdsm.getFileById(mFile.getFileId()) != null) {
694 OCFile newFile = new OCFile(fdsm.getFileById(mFile.getParentId()).getRemotePath() + newFilename);
695 newFile.setCreationTimestamp(mFile.getCreationTimestamp());
696 newFile.setFileId(mFile.getFileId());
697 newFile.setFileLength(mFile.getFileLength());
698 newFile.setKeepInSync(mFile.keepInSync());
699 newFile.setLastSyncDate(mFile.getLastSyncDate());
700 newFile.setMimetype(mFile.getMimetype());
701 newFile.setModificationTimestamp(mFile.getModificationTimestamp());
702 newFile.setParentId(mFile.getParentId());
703 if (mFile.isDown()) {
704 File f = new File(mFile.getStoragePath());
705 Log.e(TAG, f.getAbsolutePath());
706 f.renameTo(new File(f.getParent() + File.separator + newFilename)); // TODO check if fails
707 Log.e(TAG, f.getParent() + File.separator + newFilename);
708 newFile.setStoragePath(f.getParent() + File.separator + newFilename);
709 }
710
711 new Thread(new RenameRunnable(mFile, newFile, mAccount, new Handler())).start();
712
713 }
714 }
715 }
716 } else {
717 Log.e(TAG, "Unknown dialog instance passed to onDismissDalog: " + dialog.getClass().getCanonicalName());
718 }
719
720 }
721
722 private class RenameRunnable implements Runnable {
723
724 Account mAccount;
725 OCFile mOld, mNew;
726 Handler mHandler;
727
728 public RenameRunnable(OCFile oldFile, OCFile newFile, Account account, Handler handler) {
729 mOld = oldFile;
730 mNew = newFile;
731 mAccount = account;
732 mHandler = handler;
733 }
734
735 public void run() {
736 WebdavClient wc = new WebdavClient(mAccount, getSherlockActivity().getApplicationContext());
737 AccountManager am = AccountManager.get(getSherlockActivity());
738 String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);
739 OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
740 String webdav_path = AccountUtils.getWebdavPath(ocv);
741 Log.d("ASD", ""+baseUrl + webdav_path + WebdavUtils.encodePath(mOld.getRemotePath()));
742
743 Log.e("ASD", Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + WebdavUtils.encodePath(mNew.getRemotePath()));
744 LocalMoveMethod move = new LocalMoveMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mOld.getRemotePath()),
745 Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + WebdavUtils.encodePath(mNew.getRemotePath()));
746
747 try {
748 int status = wc.executeMethod(move);
749 if (move.succeeded()) {
750 FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
751 fdsm.removeFile(mOld);
752 fdsm.saveFile(mNew);
753 mFile = mNew;
754 mHandler.post(new Runnable() {
755 @Override
756 public void run() { updateFileDetails(mFile, mAccount); }
757 });
758 }
759 Log.e("ASD", ""+move.getQueryString());
760 Log.d("move", "returned status " + status);
761 } catch (HttpException e) {
762 // TODO Auto-generated catch block
763 e.printStackTrace();
764 } catch (IOException e) {
765 // TODO Auto-generated catch block
766 e.printStackTrace();
767 }
768 }
769 private class LocalMoveMethod extends DavMethodBase {
770
771 public LocalMoveMethod(String uri, String dest) {
772 super(uri);
773 addRequestHeader(new org.apache.commons.httpclient.Header("Destination", dest));
774 }
775
776 @Override
777 public String getName() {
778 return "MOVE";
779 }
780
781 @Override
782 protected boolean isSuccess(int status) {
783 return status == 201 || status == 204;
784 }
785
786 }
787 }
788
789 private static class EditNameFragment extends SherlockDialogFragment implements OnClickListener {
790
791 private String mNewFilename;
792 private boolean mResult;
793 private FileDetailFragment mListener;
794
795 static public EditNameFragment newInstance(String filename) {
796 EditNameFragment f = new EditNameFragment();
797 Bundle args = new Bundle();
798 args.putString("filename", filename);
799 f.setArguments(args);
800 return f;
801 }
802
803 @Override
804 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
805 View v = inflater.inflate(R.layout.edit_box_dialog, container, false);
806
807 String currentName = getArguments().getString("filename");
808 if (currentName == null)
809 currentName = "";
810
811 ((Button)v.findViewById(R.id.cancel)).setOnClickListener(this);
812 ((Button)v.findViewById(R.id.ok)).setOnClickListener(this);
813 ((TextView)v.findViewById(R.id.user_input)).setText(currentName);
814 ((TextView)v.findViewById(R.id.user_input)).requestFocus();
815 getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
816
817 mResult = false;
818 return v;
819 }
820
821 @Override
822 public void onClick(View view) {
823 switch (view.getId()) {
824 case R.id.ok: {
825 mNewFilename = ((TextView)getView().findViewById(R.id.user_input)).getText().toString();
826 mResult = true;
827 }
828 case R.id.cancel: { // fallthought
829 dismiss();
830 mListener.onDismiss(this);
831 }
832 }
833 }
834
835 void setOnDismissListener(FileDetailFragment listener) {
836 mListener = listener;
837 }
838
839 public String getNewFilename() {
840 return mNewFilename;
841 }
842
843 // true if user click ok
844 public boolean getResult() {
845 return mResult;
846 }
847
848 }
849
850 private class RemoveRunnable implements Runnable {
851
852 /** Arbitrary timeout for deletion */
853 public final static int DELETION_TIMEOUT = 5000;
854
855 Account mAccount;
856 OCFile mFileToRemove;
857 Handler mHandler;
858
859 public RemoveRunnable(OCFile fileToRemove, Account account, Handler handler) {
860 mFileToRemove = fileToRemove;
861 mAccount = account;
862 mHandler = handler;
863 }
864
865 public void run() {
866 WebdavClient wc = new WebdavClient(mAccount, getSherlockActivity().getApplicationContext());
867 AccountManager am = AccountManager.get(getSherlockActivity());
868 String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);
869 OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
870 String webdav_path = AccountUtils.getWebdavPath(ocv);
871 Log.d("ASD", ""+baseUrl + webdav_path + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));
872
873 DeleteMethod delete = new DeleteMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mFileToRemove.getRemotePath()));
874
875 boolean success = false;
876 try {
877 int status = wc.executeMethod(delete, DELETION_TIMEOUT);
878 if (delete.succeeded()) {
879 FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
880 fdsm.removeFile(mFileToRemove);
881 mHandler.post(new Runnable() {
882 @Override
883 public void run() {
884 try {
885 Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);
886 msg.show();
887 if (getActivity() instanceof FileDisplayActivity) {
888 // double pane
889 FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
890 transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment
891 transaction.commit();
892
893 } else {
894 getActivity().finish();
895 }
896
897 } catch (NotFoundException e) {
898 e.printStackTrace();
899 }
900 }
901 });
902 success = true;
903 }
904 Log.e("ASD", ""+ delete.getQueryString());
905 Log.d("delete", "returned status " + status);
906
907 } catch (HttpException e) {
908 e.printStackTrace();
909
910 } catch (IOException e) {
911 e.printStackTrace();
912
913 } finally {
914 if (!success) {
915 mHandler.post(new Runnable() {
916 @Override
917 public void run() {
918 try {
919 Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG);
920 msg.show();
921
922 } catch (NotFoundException e) {
923 e.printStackTrace();
924 }
925 }
926 });
927 }
928 }
929 }
930
931 }
932
933 class BitmapLoader extends AsyncTask<String, Void, Bitmap> {
934 @Override
935 protected Bitmap doInBackground(String... params) {
936 Bitmap result = null;
937 if (params.length != 1) return result;
938 String storagePath = params[0];
939 try {
940
941 BitmapFactory.Options options = new Options();
942 options.inScaled = true;
943 options.inPurgeable = true;
944 options.inJustDecodeBounds = true;
945 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
946 options.inPreferQualityOverSpeed = false;
947 }
948 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
949 options.inMutable = false;
950 }
951
952 result = BitmapFactory.decodeFile(storagePath, options);
953 options.inJustDecodeBounds = false;
954
955 int width = options.outWidth;
956 int height = options.outHeight;
957 int scale = 1;
958 boolean recycle = false;
959 if (width >= 2048 || height >= 2048) {
960 scale = (int) Math.ceil((Math.ceil(Math.max(height, width) / 2048.)));
961 options.inSampleSize = scale;
962 }
963 Display display = getActivity().getWindowManager().getDefaultDisplay();
964 Point size = new Point();
965 int screenwidth;
966 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
967 display.getSize(size);
968 screenwidth = size.x;
969 } else {
970 screenwidth = display.getWidth();
971 }
972
973 Log.e("ASD", "W " + width + " SW " + screenwidth);
974
975 if (width > screenwidth) {
976 scale = (int) Math.ceil((float)width / screenwidth);
977 options.inSampleSize = scale;
978 }
979
980 result = BitmapFactory.decodeFile(storagePath, options);
981
982 Log.e("ASD", "W " + options.outWidth + " SW " + options.outHeight);
983
984 } catch (OutOfMemoryError e) {
985 result = null;
986 Log.e(TAG, "Out of memory occured for file with size " + storagePath);
987
988 } catch (NoSuchFieldError e) {
989 result = null;
990 Log.e(TAG, "Error from access to unexisting field despite protection " + storagePath);
991
992 } catch (Throwable t) {
993 result = null;
994 Log.e(TAG, "Unexpected error while creating image preview " + storagePath, t);
995 }
996 return result;
997 }
998 @Override
999 protected void onPostExecute(Bitmap result) {
1000 if (result != null && mPreview != null) {
1001 mPreview.setImageBitmap(result);
1002 }
1003 }
1004
1005 }
1006
1007
1008 }