FileDetailFragment.java 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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 com.owncloud.android.ui.fragment;
  19. import java.io.File;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.apache.commons.httpclient.methods.GetMethod;
  23. import org.apache.commons.httpclient.methods.PostMethod;
  24. import org.apache.commons.httpclient.methods.StringRequestEntity;
  25. import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
  26. import org.apache.http.HttpStatus;
  27. import org.apache.http.NameValuePair;
  28. import org.apache.http.client.utils.URLEncodedUtils;
  29. import org.apache.http.message.BasicNameValuePair;
  30. import org.apache.http.protocol.HTTP;
  31. import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
  32. import org.json.JSONObject;
  33. import android.accounts.Account;
  34. import android.accounts.AccountManager;
  35. import android.annotation.SuppressLint;
  36. import android.app.Activity;
  37. import android.content.ActivityNotFoundException;
  38. import android.content.BroadcastReceiver;
  39. import android.content.Context;
  40. import android.content.Intent;
  41. import android.content.IntentFilter;
  42. import android.graphics.Bitmap;
  43. import android.graphics.BitmapFactory;
  44. import android.graphics.BitmapFactory.Options;
  45. import android.graphics.Point;
  46. import android.net.Uri;
  47. import android.os.AsyncTask;
  48. import android.os.Bundle;
  49. import android.os.Handler;
  50. import android.support.v4.app.FragmentTransaction;
  51. import android.util.Log;
  52. import android.view.Display;
  53. import android.view.LayoutInflater;
  54. import android.view.View;
  55. import android.view.View.OnClickListener;
  56. import android.view.ViewGroup;
  57. import android.webkit.MimeTypeMap;
  58. import android.widget.Button;
  59. import android.widget.CheckBox;
  60. import android.widget.ImageView;
  61. import android.widget.TextView;
  62. import android.widget.Toast;
  63. import com.actionbarsherlock.app.SherlockFragment;
  64. import com.owncloud.android.AccountUtils;
  65. import com.owncloud.android.DisplayUtils;
  66. import com.owncloud.android.authenticator.AccountAuthenticator;
  67. import com.owncloud.android.datamodel.FileDataStorageManager;
  68. import com.owncloud.android.datamodel.OCFile;
  69. import com.owncloud.android.files.services.FileDownloader;
  70. import com.owncloud.android.files.services.FileObserverService;
  71. import com.owncloud.android.files.services.FileUploader;
  72. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  73. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  74. import com.owncloud.android.network.OwnCloudClientUtils;
  75. import com.owncloud.android.operations.OnRemoteOperationListener;
  76. import com.owncloud.android.operations.RemoteOperation;
  77. import com.owncloud.android.operations.RemoteOperationResult;
  78. import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
  79. import com.owncloud.android.operations.RemoveFileOperation;
  80. import com.owncloud.android.operations.RenameFileOperation;
  81. import com.owncloud.android.operations.SynchronizeFileOperation;
  82. import com.owncloud.android.ui.activity.ConflictsResolveActivity;
  83. import com.owncloud.android.ui.activity.FileDetailActivity;
  84. import com.owncloud.android.ui.activity.FileDisplayActivity;
  85. import com.owncloud.android.ui.activity.TransferServiceGetter;
  86. import com.owncloud.android.ui.dialog.EditNameDialog;
  87. import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
  88. import com.owncloud.android.utils.OwnCloudVersion;
  89. import com.owncloud.android.R;
  90. import eu.alefzero.webdav.WebdavClient;
  91. import eu.alefzero.webdav.WebdavUtils;
  92. /**
  93. * This Fragment is used to display the details about a file.
  94. *
  95. * @author Bartek Przybylski
  96. *
  97. */
  98. public class FileDetailFragment extends SherlockFragment implements
  99. OnClickListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener, EditNameDialogListener {
  100. public static final String EXTRA_FILE = "FILE";
  101. public static final String EXTRA_ACCOUNT = "ACCOUNT";
  102. private FileDetailFragment.ContainerActivity mContainerActivity;
  103. private int mLayout;
  104. private View mView;
  105. private OCFile mFile;
  106. private Account mAccount;
  107. private FileDataStorageManager mStorageManager;
  108. private ImageView mPreview;
  109. private DownloadFinishReceiver mDownloadFinishReceiver;
  110. private UploadFinishReceiver mUploadFinishReceiver;
  111. private Handler mHandler;
  112. private RemoteOperation mLastRemoteOperation;
  113. private static final String TAG = FileDetailFragment.class.getSimpleName();
  114. public static final String FTAG = "FileDetails";
  115. public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";
  116. /**
  117. * Creates an empty details fragment.
  118. *
  119. * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
  120. */
  121. public FileDetailFragment() {
  122. mFile = null;
  123. mAccount = null;
  124. mStorageManager = null;
  125. mLayout = R.layout.file_details_empty;
  126. }
  127. /**
  128. * Creates a details fragment.
  129. *
  130. * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
  131. *
  132. * @param fileToDetail An {@link OCFile} to show in the fragment
  133. * @param ocAccount An ownCloud account; needed to start downloads
  134. */
  135. public FileDetailFragment(OCFile fileToDetail, Account ocAccount) {
  136. mFile = fileToDetail;
  137. mAccount = ocAccount;
  138. mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment
  139. mLayout = R.layout.file_details_empty;
  140. if(fileToDetail != null && ocAccount != null) {
  141. mLayout = R.layout.file_details_fragment;
  142. }
  143. }
  144. @Override
  145. public void onCreate(Bundle savedInstanceState) {
  146. super.onCreate(savedInstanceState);
  147. mHandler = new Handler();
  148. }
  149. @Override
  150. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  151. Bundle savedInstanceState) {
  152. super.onCreateView(inflater, container, savedInstanceState);
  153. if (savedInstanceState != null) {
  154. mFile = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_FILE);
  155. mAccount = savedInstanceState.getParcelable(FileDetailFragment.EXTRA_ACCOUNT);
  156. }
  157. View view = null;
  158. view = inflater.inflate(mLayout, container, false);
  159. mView = view;
  160. if (mLayout == R.layout.file_details_fragment) {
  161. mView.findViewById(R.id.fdKeepInSync).setOnClickListener(this);
  162. mView.findViewById(R.id.fdRenameBtn).setOnClickListener(this);
  163. mView.findViewById(R.id.fdDownloadBtn).setOnClickListener(this);
  164. mView.findViewById(R.id.fdOpenBtn).setOnClickListener(this);
  165. mView.findViewById(R.id.fdRemoveBtn).setOnClickListener(this);
  166. //mView.findViewById(R.id.fdShareBtn).setOnClickListener(this);
  167. mPreview = (ImageView)mView.findViewById(R.id.fdPreview);
  168. }
  169. updateFileDetails();
  170. return view;
  171. }
  172. /**
  173. * {@inheritDoc}
  174. */
  175. @Override
  176. public void onAttach(Activity activity) {
  177. super.onAttach(activity);
  178. try {
  179. mContainerActivity = (ContainerActivity) activity;
  180. } catch (ClassCastException e) {
  181. throw new ClassCastException(activity.toString() + " must implement " + FileDetailFragment.ContainerActivity.class.getSimpleName());
  182. }
  183. }
  184. /**
  185. * {@inheritDoc}
  186. */
  187. @Override
  188. public void onActivityCreated(Bundle savedInstanceState) {
  189. super.onActivityCreated(savedInstanceState);
  190. if (mAccount != null) {
  191. mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());;
  192. }
  193. }
  194. @Override
  195. public void onSaveInstanceState(Bundle outState) {
  196. Log.i(getClass().toString(), "onSaveInstanceState() start");
  197. super.onSaveInstanceState(outState);
  198. outState.putParcelable(FileDetailFragment.EXTRA_FILE, mFile);
  199. outState.putParcelable(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
  200. Log.i(getClass().toString(), "onSaveInstanceState() end");
  201. }
  202. @Override
  203. public void onResume() {
  204. super.onResume();
  205. mDownloadFinishReceiver = new DownloadFinishReceiver();
  206. IntentFilter filter = new IntentFilter(
  207. FileDownloader.DOWNLOAD_FINISH_MESSAGE);
  208. getActivity().registerReceiver(mDownloadFinishReceiver, filter);
  209. mUploadFinishReceiver = new UploadFinishReceiver();
  210. filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
  211. getActivity().registerReceiver(mUploadFinishReceiver, filter);
  212. mPreview = (ImageView)mView.findViewById(R.id.fdPreview);
  213. }
  214. @Override
  215. public void onPause() {
  216. super.onPause();
  217. getActivity().unregisterReceiver(mDownloadFinishReceiver);
  218. mDownloadFinishReceiver = null;
  219. getActivity().unregisterReceiver(mUploadFinishReceiver);
  220. mUploadFinishReceiver = null;
  221. if (mPreview != null) {
  222. mPreview = null;
  223. }
  224. }
  225. @Override
  226. public View getView() {
  227. return super.getView() == null ? mView : super.getView();
  228. }
  229. @Override
  230. public void onClick(View v) {
  231. switch (v.getId()) {
  232. case R.id.fdDownloadBtn: {
  233. //if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath())) {
  234. FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
  235. FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
  236. if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) {
  237. downloaderBinder.cancel(mAccount, mFile);
  238. if (mFile.isDown()) {
  239. setButtonsForDown();
  240. } else {
  241. setButtonsForRemote();
  242. }
  243. } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile)) {
  244. uploaderBinder.cancel(mAccount, mFile);
  245. if (!mFile.fileExists()) {
  246. // TODO make something better
  247. if (getActivity() instanceof FileDisplayActivity) {
  248. // double pane
  249. FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
  250. transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null), FTAG); // empty FileDetailFragment
  251. transaction.commit();
  252. mContainerActivity.onFileStateChanged();
  253. } else {
  254. getActivity().finish();
  255. }
  256. } else if (mFile.isDown()) {
  257. setButtonsForDown();
  258. } else {
  259. setButtonsForRemote();
  260. }
  261. } else {
  262. mLastRemoteOperation = new SynchronizeFileOperation(mFile, null, mStorageManager, mAccount, true, false, getActivity());
  263. WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());
  264. mLastRemoteOperation.execute(wc, this, mHandler);
  265. // update ui
  266. boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
  267. getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
  268. setButtonsForTransferring(); // disable button immediately, although the synchronization does not result in a file transference
  269. }
  270. break;
  271. }
  272. case R.id.fdKeepInSync: {
  273. CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);
  274. mFile.setKeepInSync(cb.isChecked());
  275. mStorageManager.saveFile(mFile);
  276. /// register the OCFile instance in the observer service to monitor local updates;
  277. /// if necessary, the file is download
  278. Intent intent = new Intent(getActivity().getApplicationContext(),
  279. FileObserverService.class);
  280. intent.putExtra(FileObserverService.KEY_FILE_CMD,
  281. (cb.isChecked()?
  282. FileObserverService.CMD_ADD_OBSERVED_FILE:
  283. FileObserverService.CMD_DEL_OBSERVED_FILE));
  284. intent.putExtra(FileObserverService.KEY_CMD_ARG_FILE, mFile);
  285. intent.putExtra(FileObserverService.KEY_CMD_ARG_ACCOUNT, mAccount);
  286. Log.e(TAG, "starting observer service");
  287. getActivity().startService(intent);
  288. if (mFile.keepInSync()) {
  289. onClick(getView().findViewById(R.id.fdDownloadBtn)); // force an immediate synchronization
  290. }
  291. break;
  292. }
  293. case R.id.fdRenameBtn: {
  294. EditNameDialog dialog = EditNameDialog.newInstance(mFile.getFileName());
  295. dialog.setOnDismissListener(this);
  296. dialog.show(getFragmentManager(), "nameeditdialog");
  297. break;
  298. }
  299. case R.id.fdRemoveBtn: {
  300. ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance(
  301. R.string.confirmation_remove_alert,
  302. new String[]{mFile.getFileName()},
  303. mFile.isDown() ? R.string.confirmation_remove_remote_and_local : R.string.confirmation_remove_remote,
  304. mFile.isDown() ? R.string.confirmation_remove_local : -1,
  305. R.string.common_cancel);
  306. confDialog.setOnConfirmationListener(this);
  307. confDialog.show(getFragmentManager(), FTAG_CONFIRMATION);
  308. break;
  309. }
  310. case R.id.fdOpenBtn: {
  311. String storagePath = mFile.getStoragePath();
  312. String encodedStoragePath = WebdavUtils.encodePath(storagePath);
  313. try {
  314. Intent i = new Intent(Intent.ACTION_VIEW);
  315. i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mFile.getMimetype());
  316. i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  317. startActivity(i);
  318. } catch (Throwable t) {
  319. Log.e(TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype());
  320. boolean toastIt = true;
  321. String mimeType = "";
  322. try {
  323. Intent i = new Intent(Intent.ACTION_VIEW);
  324. mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
  325. if (mimeType != null && !mimeType.equals(mFile.getMimetype())) {
  326. i.setDataAndType(Uri.parse("file://"+ encodedStoragePath), mimeType);
  327. i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  328. startActivity(i);
  329. toastIt = false;
  330. }
  331. } catch (IndexOutOfBoundsException e) {
  332. Log.e(TAG, "Trying to find out MIME type of a file without extension: " + storagePath);
  333. } catch (ActivityNotFoundException e) {
  334. Log.e(TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension");
  335. } catch (Throwable th) {
  336. Log.e(TAG, "Unexpected problem when opening: " + storagePath, th);
  337. } finally {
  338. if (toastIt) {
  339. Toast.makeText(getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT).show();
  340. }
  341. }
  342. }
  343. break;
  344. }
  345. default:
  346. Log.e(TAG, "Incorrect view clicked!");
  347. }
  348. /* else if (v.getId() == R.id.fdShareBtn) {
  349. Thread t = new Thread(new ShareRunnable(mFile.getRemotePath()));
  350. t.start();
  351. }*/
  352. }
  353. @Override
  354. public void onConfirmation(String callerTag) {
  355. if (callerTag.equals(FTAG_CONFIRMATION)) {
  356. if (mStorageManager.getFileById(mFile.getFileId()) != null) {
  357. mLastRemoteOperation = new RemoveFileOperation( mFile,
  358. true,
  359. mStorageManager);
  360. WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());
  361. mLastRemoteOperation.execute(wc, this, mHandler);
  362. boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
  363. getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
  364. }
  365. }
  366. }
  367. @Override
  368. public void onNeutral(String callerTag) {
  369. File f = null;
  370. if (mFile.isDown() && (f = new File(mFile.getStoragePath())).exists()) {
  371. f.delete();
  372. mFile.setStoragePath(null);
  373. mStorageManager.saveFile(mFile);
  374. updateFileDetails(mFile, mAccount);
  375. }
  376. }
  377. @Override
  378. public void onCancel(String callerTag) {
  379. Log.d(TAG, "REMOVAL CANCELED");
  380. }
  381. /**
  382. * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
  383. *
  384. * @return True when the fragment was created with the empty layout.
  385. */
  386. public boolean isEmpty() {
  387. return mLayout == R.layout.file_details_empty;
  388. }
  389. /**
  390. * Can be used to get the file that is currently being displayed.
  391. * @return The file on the screen.
  392. */
  393. public OCFile getDisplayedFile(){
  394. return mFile;
  395. }
  396. /**
  397. * Use this method to signal this Activity that it shall update its view.
  398. *
  399. * @param file : An {@link OCFile}
  400. */
  401. public void updateFileDetails(OCFile file, Account ocAccount) {
  402. mFile = file;
  403. if (ocAccount != null && (
  404. mStorageManager == null ||
  405. (mAccount != null && !mAccount.equals(ocAccount))
  406. )) {
  407. mStorageManager = new FileDataStorageManager(ocAccount, getActivity().getApplicationContext().getContentResolver());
  408. }
  409. mAccount = ocAccount;
  410. updateFileDetails();
  411. }
  412. /**
  413. * Updates the view with all relevant details about that file.
  414. */
  415. public void updateFileDetails() {
  416. if (mFile != null && mAccount != null && mLayout == R.layout.file_details_fragment) {
  417. // set file details
  418. setFilename(mFile.getFileName());
  419. setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile
  420. .getMimetype()));
  421. setFilesize(mFile.getFileLength());
  422. if(ocVersionSupportsTimeCreated()){
  423. setTimeCreated(mFile.getCreationTimestamp());
  424. }
  425. setTimeModified(mFile.getModificationTimestamp());
  426. CheckBox cb = (CheckBox)getView().findViewById(R.id.fdKeepInSync);
  427. cb.setChecked(mFile.keepInSync());
  428. // configure UI for depending upon local state of the file
  429. //if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath()) || FileUploader.isUploading(mAccount, mFile.getRemotePath())) {
  430. FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
  431. FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
  432. if ((downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) || (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile))) {
  433. setButtonsForTransferring();
  434. } else if (mFile.isDown()) {
  435. // Update preview
  436. if (mFile.getMimetype().startsWith("image/")) {
  437. BitmapLoader bl = new BitmapLoader();
  438. bl.execute(new String[]{mFile.getStoragePath()});
  439. }
  440. setButtonsForDown();
  441. } else {
  442. setButtonsForRemote();
  443. }
  444. }
  445. }
  446. /**
  447. * Updates the filename in view
  448. * @param filename to set
  449. */
  450. private void setFilename(String filename) {
  451. TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
  452. if (tv != null)
  453. tv.setText(filename);
  454. }
  455. /**
  456. * Updates the MIME type in view
  457. * @param mimetype to set
  458. */
  459. private void setFiletype(String mimetype) {
  460. TextView tv = (TextView) getView().findViewById(R.id.fdType);
  461. if (tv != null)
  462. tv.setText(mimetype);
  463. }
  464. /**
  465. * Updates the file size in view
  466. * @param filesize in bytes to set
  467. */
  468. private void setFilesize(long filesize) {
  469. TextView tv = (TextView) getView().findViewById(R.id.fdSize);
  470. if (tv != null)
  471. tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
  472. }
  473. /**
  474. * Updates the time that the file was created in view
  475. * @param milliseconds Unix time to set
  476. */
  477. private void setTimeCreated(long milliseconds){
  478. TextView tv = (TextView) getView().findViewById(R.id.fdCreated);
  479. TextView tvLabel = (TextView) getView().findViewById(R.id.fdCreatedLabel);
  480. if(tv != null){
  481. tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
  482. tv.setVisibility(View.VISIBLE);
  483. tvLabel.setVisibility(View.VISIBLE);
  484. }
  485. }
  486. /**
  487. * Updates the time that the file was last modified
  488. * @param milliseconds Unix time to set
  489. */
  490. private void setTimeModified(long milliseconds){
  491. TextView tv = (TextView) getView().findViewById(R.id.fdModified);
  492. if(tv != null){
  493. tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
  494. }
  495. }
  496. /**
  497. * Enables or disables buttons for a file being downloaded
  498. */
  499. private void setButtonsForTransferring() {
  500. if (!isEmpty()) {
  501. Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
  502. downloadButton.setText(R.string.common_cancel);
  503. //downloadButton.setEnabled(false);
  504. // let's protect the user from himself ;)
  505. ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(false);
  506. ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(false);
  507. ((Button) getView().findViewById(R.id.fdRemoveBtn)).setEnabled(false);
  508. getView().findViewById(R.id.fdKeepInSync).setEnabled(false);
  509. }
  510. }
  511. /**
  512. * Enables or disables buttons for a file locally available
  513. */
  514. private void setButtonsForDown() {
  515. if (!isEmpty()) {
  516. Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
  517. downloadButton.setText(R.string.filedetails_sync_file);
  518. //downloadButton.setEnabled(true);
  519. ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(true);
  520. ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(true);
  521. ((Button) getView().findViewById(R.id.fdRemoveBtn)).setEnabled(true);
  522. getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
  523. }
  524. }
  525. /**
  526. * Enables or disables buttons for a file not locally available
  527. */
  528. private void setButtonsForRemote() {
  529. if (!isEmpty()) {
  530. Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);
  531. downloadButton.setText(R.string.filedetails_download);
  532. ((Button) getView().findViewById(R.id.fdOpenBtn)).setEnabled(false);
  533. ((Button) getView().findViewById(R.id.fdRenameBtn)).setEnabled(true);
  534. ((Button) getView().findViewById(R.id.fdRemoveBtn)).setEnabled(true);
  535. getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
  536. }
  537. }
  538. /**
  539. * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
  540. * the time that the file was created. There is a chance that this will
  541. * be fixed in future versions. Use this method to check if this version of
  542. * ownCloud has this fix.
  543. * @return True, if ownCloud the ownCloud version is supporting creation time
  544. */
  545. private boolean ocVersionSupportsTimeCreated(){
  546. /*if(mAccount != null){
  547. AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
  548. OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
  549. .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
  550. if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
  551. return true;
  552. }
  553. }*/
  554. return false;
  555. }
  556. /**
  557. * Interface to implement by any Activity that includes some instance of FileDetailFragment
  558. *
  559. * @author David A. Velasco
  560. */
  561. public interface ContainerActivity extends TransferServiceGetter {
  562. /**
  563. * Callback method invoked when the detail fragment wants to notice its container
  564. * activity about a relevant state the file shown by the fragment.
  565. *
  566. * Added to notify to FileDisplayActivity about the need of refresh the files list.
  567. *
  568. * Currently called when:
  569. * - a download is started;
  570. * - a rename is completed;
  571. * - a deletion is completed;
  572. * - the 'inSync' flag is changed;
  573. */
  574. public void onFileStateChanged();
  575. }
  576. /**
  577. * Once the file download has finished -> update view
  578. * @author Bartek Przybylski
  579. */
  580. private class DownloadFinishReceiver extends BroadcastReceiver {
  581. @Override
  582. public void onReceive(Context context, Intent intent) {
  583. String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
  584. if (!isEmpty() && accountName.equals(mAccount.name)) {
  585. boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);
  586. String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
  587. if (mFile.getRemotePath().equals(downloadedRemotePath)) {
  588. if (downloadWasFine) {
  589. mFile = mStorageManager.getFileByPath(downloadedRemotePath);
  590. }
  591. updateFileDetails(); // it updates the buttons; must be called although !downloadWasFine
  592. }
  593. }
  594. }
  595. }
  596. /**
  597. * Once the file upload has finished -> update view
  598. *
  599. * Being notified about the finish of an upload is necessary for the next sequence:
  600. * 1. Upload a big file.
  601. * 2. Force a synchronization; if it finished before the upload, the file in transfer will be included in the local database and in the file list
  602. * of its containing folder; the the server includes it in the PROPFIND requests although it's not fully upload.
  603. * 3. Click the file in the list to see its details.
  604. * 4. Wait for the upload finishes; at this moment, the details view must be refreshed to enable the action buttons.
  605. */
  606. private class UploadFinishReceiver extends BroadcastReceiver {
  607. @Override
  608. public void onReceive(Context context, Intent intent) {
  609. String accountName = intent.getStringExtra(FileUploader.ACCOUNT_NAME);
  610. if (!isEmpty() && accountName.equals(mAccount.name)) {
  611. boolean uploadWasFine = intent.getBooleanExtra(FileUploader.EXTRA_UPLOAD_RESULT, false);
  612. String uploadRemotePath = intent.getStringExtra(FileUploader.EXTRA_REMOTE_PATH);
  613. if (mFile.getRemotePath().equals(uploadRemotePath)) {
  614. if (uploadWasFine) {
  615. FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());
  616. mFile = fdsm.getFileByPath(mFile.getRemotePath());
  617. }
  618. updateFileDetails(); // it updates the buttons; must be called although !uploadWasFine; interrupted uploads still leave an incomplete file in the server
  619. }
  620. }
  621. }
  622. }
  623. // this is a temporary class for sharing purposes, it need to be replaced in transfer service
  624. @SuppressWarnings("unused")
  625. private class ShareRunnable implements Runnable {
  626. private String mPath;
  627. public ShareRunnable(String path) {
  628. mPath = path;
  629. }
  630. public void run() {
  631. AccountManager am = AccountManager.get(getActivity());
  632. Account account = AccountUtils.getCurrentOwnCloudAccount(getActivity());
  633. OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(account, AccountAuthenticator.KEY_OC_VERSION));
  634. String url = am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + AccountUtils.getWebdavPath(ocv);
  635. Log.d("share", "sharing for version " + ocv.toString());
  636. if (ocv.compareTo(new OwnCloudVersion(0x040000)) >= 0) {
  637. String APPS_PATH = "/apps/files_sharing/";
  638. String SHARE_PATH = "ajax/share.php";
  639. String SHARED_PATH = "/apps/files_sharing/get.php?token=";
  640. final String WEBDAV_SCRIPT = "webdav.php";
  641. final String WEBDAV_FILES_LOCATION = "/files/";
  642. WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(account, getActivity().getApplicationContext());
  643. HttpConnectionManagerParams params = new HttpConnectionManagerParams();
  644. params.setMaxConnectionsPerHost(wc.getHostConfiguration(), 5);
  645. //wc.getParams().setParameter("http.protocol.single-cookie-header", true);
  646. //wc.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
  647. PostMethod post = new PostMethod(am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + APPS_PATH + SHARE_PATH);
  648. post.addRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8" );
  649. post.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL));
  650. List<NameValuePair> formparams = new ArrayList<NameValuePair>();
  651. Log.d("share", mPath+"");
  652. formparams.add(new BasicNameValuePair("sources",mPath));
  653. formparams.add(new BasicNameValuePair("uid_shared_with", "public"));
  654. formparams.add(new BasicNameValuePair("permissions", "0"));
  655. post.setRequestEntity(new StringRequestEntity(URLEncodedUtils.format(formparams, HTTP.UTF_8)));
  656. int status;
  657. try {
  658. PropFindMethod find = new PropFindMethod(url+"/");
  659. find.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL));
  660. Log.d("sharer", ""+ url+"/");
  661. for (org.apache.commons.httpclient.Header a : find.getRequestHeaders()) {
  662. Log.d("sharer-h", a.getName() + ":"+a.getValue());
  663. }
  664. int status2 = wc.executeMethod(find);
  665. Log.d("sharer", "propstatus "+status2);
  666. GetMethod get = new GetMethod(am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + "/");
  667. get.addRequestHeader("Referer", am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL));
  668. status2 = wc.executeMethod(get);
  669. Log.d("sharer", "getstatus "+status2);
  670. Log.d("sharer", "" + get.getResponseBodyAsString());
  671. for (org.apache.commons.httpclient.Header a : get.getResponseHeaders()) {
  672. Log.d("sharer", a.getName() + ":"+a.getValue());
  673. }
  674. status = wc.executeMethod(post);
  675. for (org.apache.commons.httpclient.Header a : post.getRequestHeaders()) {
  676. Log.d("sharer-h", a.getName() + ":"+a.getValue());
  677. }
  678. for (org.apache.commons.httpclient.Header a : post.getResponseHeaders()) {
  679. Log.d("sharer", a.getName() + ":"+a.getValue());
  680. }
  681. String resp = post.getResponseBodyAsString();
  682. Log.d("share", ""+post.getURI().toString());
  683. Log.d("share", "returned status " + status);
  684. Log.d("share", " " +resp);
  685. if(status != HttpStatus.SC_OK ||resp == null || resp.equals("") || resp.startsWith("false")) {
  686. return;
  687. }
  688. JSONObject jsonObject = new JSONObject (resp);
  689. String jsonStatus = jsonObject.getString("status");
  690. if(!jsonStatus.equals("success")) throw new Exception("Error while sharing file status != success");
  691. String token = jsonObject.getString("data");
  692. String uri = am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL) + SHARED_PATH + token;
  693. Log.d("Actions:shareFile ok", "url: " + uri);
  694. } catch (Exception e) {
  695. e.printStackTrace();
  696. }
  697. } else if (ocv.compareTo(new OwnCloudVersion(0x030000)) >= 0) {
  698. }
  699. }
  700. }
  701. public void onDismiss(EditNameDialog dialog) {
  702. if (dialog.getResult()) {
  703. String newFilename = dialog.getNewFilename();
  704. Log.d(TAG, "name edit dialog dismissed with new name " + newFilename);
  705. mLastRemoteOperation = new RenameFileOperation( mFile,
  706. newFilename,
  707. new FileDataStorageManager(mAccount, getActivity().getContentResolver()));
  708. WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());
  709. mLastRemoteOperation.execute(wc, this, mHandler);
  710. boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
  711. getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
  712. }
  713. }
  714. class BitmapLoader extends AsyncTask<String, Void, Bitmap> {
  715. @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20
  716. @Override
  717. protected Bitmap doInBackground(String... params) {
  718. Bitmap result = null;
  719. if (params.length != 1) return result;
  720. String storagePath = params[0];
  721. try {
  722. BitmapFactory.Options options = new Options();
  723. options.inScaled = true;
  724. options.inPurgeable = true;
  725. options.inJustDecodeBounds = true;
  726. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
  727. options.inPreferQualityOverSpeed = false;
  728. }
  729. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
  730. options.inMutable = false;
  731. }
  732. result = BitmapFactory.decodeFile(storagePath, options);
  733. options.inJustDecodeBounds = false;
  734. int width = options.outWidth;
  735. int height = options.outHeight;
  736. int scale = 1;
  737. if (width >= 2048 || height >= 2048) {
  738. scale = (int) Math.ceil((Math.ceil(Math.max(height, width) / 2048.)));
  739. options.inSampleSize = scale;
  740. }
  741. Display display = getActivity().getWindowManager().getDefaultDisplay();
  742. Point size = new Point();
  743. int screenwidth;
  744. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
  745. display.getSize(size);
  746. screenwidth = size.x;
  747. } else {
  748. screenwidth = display.getWidth();
  749. }
  750. Log.e("ASD", "W " + width + " SW " + screenwidth);
  751. if (width > screenwidth) {
  752. scale = (int) Math.ceil((float)width / screenwidth);
  753. options.inSampleSize = scale;
  754. }
  755. result = BitmapFactory.decodeFile(storagePath, options);
  756. Log.e("ASD", "W " + options.outWidth + " SW " + options.outHeight);
  757. } catch (OutOfMemoryError e) {
  758. result = null;
  759. Log.e(TAG, "Out of memory occured for file with size " + storagePath);
  760. } catch (NoSuchFieldError e) {
  761. result = null;
  762. Log.e(TAG, "Error from access to unexisting field despite protection " + storagePath);
  763. } catch (Throwable t) {
  764. result = null;
  765. Log.e(TAG, "Unexpected error while creating image preview " + storagePath, t);
  766. }
  767. return result;
  768. }
  769. @Override
  770. protected void onPostExecute(Bitmap result) {
  771. if (result != null && mPreview != null) {
  772. mPreview.setImageBitmap(result);
  773. }
  774. }
  775. }
  776. /**
  777. * {@inheritDoc}
  778. */
  779. @Override
  780. public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
  781. if (operation.equals(mLastRemoteOperation)) {
  782. if (operation instanceof RemoveFileOperation) {
  783. onRemoveFileOperationFinish((RemoveFileOperation)operation, result);
  784. } else if (operation instanceof RenameFileOperation) {
  785. onRenameFileOperationFinish((RenameFileOperation)operation, result);
  786. } else if (operation instanceof SynchronizeFileOperation) {
  787. onSynchronizeFileOperationFinish((SynchronizeFileOperation)operation, result);
  788. }
  789. }
  790. }
  791. private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) {
  792. boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
  793. getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
  794. if (result.isSuccess()) {
  795. Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);
  796. msg.show();
  797. if (inDisplayActivity) {
  798. // double pane
  799. FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
  800. transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment
  801. transaction.commit();
  802. mContainerActivity.onFileStateChanged();
  803. } else {
  804. getActivity().finish();
  805. }
  806. } else {
  807. Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG);
  808. msg.show();
  809. if (result.isSslRecoverableException()) {
  810. // TODO show the SSL warning dialog
  811. }
  812. }
  813. }
  814. private void onRenameFileOperationFinish(RenameFileOperation operation, RemoteOperationResult result) {
  815. boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
  816. getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
  817. if (result.isSuccess()) {
  818. updateFileDetails(((RenameFileOperation)operation).getFile(), mAccount);
  819. mContainerActivity.onFileStateChanged();
  820. } else {
  821. if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
  822. Toast msg = Toast.makeText(getActivity(), R.string.rename_local_fail_msg, Toast.LENGTH_LONG);
  823. msg.show();
  824. // TODO throw again the new rename dialog
  825. } else {
  826. Toast msg = Toast.makeText(getActivity(), R.string.rename_server_fail_msg, Toast.LENGTH_LONG);
  827. msg.show();
  828. if (result.isSslRecoverableException()) {
  829. // TODO show the SSL warning dialog
  830. }
  831. }
  832. }
  833. }
  834. private void onSynchronizeFileOperationFinish(SynchronizeFileOperation operation, RemoteOperationResult result) {
  835. boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
  836. getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
  837. if (!result.isSuccess()) {
  838. if (result.getCode() == ResultCode.SYNC_CONFLICT) {
  839. Intent i = new Intent(getActivity(), ConflictsResolveActivity.class);
  840. i.putExtra(ConflictsResolveActivity.EXTRA_FILE, mFile);
  841. i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, mAccount);
  842. startActivity(i);
  843. } else {
  844. Toast msg = Toast.makeText(getActivity(), R.string.sync_file_fail_msg, Toast.LENGTH_LONG);
  845. msg.show();
  846. }
  847. if (mFile.isDown()) {
  848. setButtonsForDown();
  849. } else {
  850. setButtonsForRemote();
  851. }
  852. } else {
  853. if (operation.transferWasRequested()) {
  854. mContainerActivity.onFileStateChanged(); // this is not working; FileDownloader won't do NOTHING at all until this method finishes, so
  855. // checking the service to see if the file is downloading results in FALSE
  856. } else {
  857. Toast msg = Toast.makeText(getActivity(), R.string.sync_file_nothing_to_do_msg, Toast.LENGTH_LONG);
  858. msg.show();
  859. if (mFile.isDown()) {
  860. setButtonsForDown();
  861. } else {
  862. setButtonsForRemote();
  863. }
  864. }
  865. }
  866. }
  867. }