FileDetailFragment.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. * Copyright (C) 2012-2013 ownCloud Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2,
  7. * as published by the Free Software Foundation.
  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.lang.ref.WeakReference;
  20. import android.accounts.Account;
  21. import android.content.Intent;
  22. import android.os.Bundle;
  23. import android.view.LayoutInflater;
  24. import android.view.View;
  25. import android.view.View.OnClickListener;
  26. import android.view.ViewGroup;
  27. import android.widget.CheckBox;
  28. import android.widget.ImageView;
  29. import android.widget.ProgressBar;
  30. import android.widget.TextView;
  31. import com.actionbarsherlock.view.Menu;
  32. import com.actionbarsherlock.view.MenuInflater;
  33. import com.actionbarsherlock.view.MenuItem;
  34. import com.owncloud.android.R;
  35. import com.owncloud.android.datamodel.FileDataStorageManager;
  36. import com.owncloud.android.datamodel.OCFile;
  37. import com.owncloud.android.files.FileMenuFilter;
  38. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  39. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  40. import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
  41. import com.owncloud.android.lib.common.utils.Log_OC;
  42. import com.owncloud.android.services.observer.FileObserverService;
  43. import com.owncloud.android.ui.activity.FileActivity;
  44. import com.owncloud.android.ui.activity.FileDisplayActivity;
  45. import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
  46. import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
  47. import com.owncloud.android.utils.DisplayUtils;
  48. /**
  49. * This Fragment is used to display the details about a file.
  50. *
  51. * @author Bartek Przybylski
  52. * @author David A. Velasco
  53. */
  54. public class FileDetailFragment extends FileFragment implements OnClickListener {
  55. private int mLayout;
  56. private View mView;
  57. private Account mAccount;
  58. public ProgressListener mProgressListener;
  59. private static final String TAG = FileDetailFragment.class.getSimpleName();
  60. public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";
  61. public static final String FTAG_RENAME_FILE = "RENAME_FILE_FRAGMENT";
  62. /**
  63. * Creates an empty details fragment.
  64. *
  65. * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
  66. */
  67. public FileDetailFragment() {
  68. super();
  69. mAccount = null;
  70. mLayout = R.layout.file_details_empty;
  71. mProgressListener = null;
  72. }
  73. /**
  74. * Creates a details fragment.
  75. *
  76. * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
  77. *
  78. * @param fileToDetail An {@link OCFile} to show in the fragment
  79. * @param ocAccount An ownCloud account; needed to start downloads
  80. */
  81. public FileDetailFragment(OCFile fileToDetail, Account ocAccount) {
  82. super(fileToDetail);
  83. mAccount = ocAccount;
  84. mLayout = R.layout.file_details_empty;
  85. mProgressListener = null;
  86. }
  87. @Override
  88. public void onActivityCreated(Bundle savedInstanceState) {
  89. super.onCreate(savedInstanceState);
  90. setHasOptionsMenu(true);
  91. }
  92. @Override
  93. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  94. Bundle savedInstanceState) {
  95. if (savedInstanceState != null) {
  96. setFile((OCFile)savedInstanceState.getParcelable(FileActivity.EXTRA_FILE));
  97. mAccount = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
  98. }
  99. if(getFile() != null && mAccount != null) {
  100. mLayout = R.layout.file_details_fragment;
  101. }
  102. View view = null;
  103. view = inflater.inflate(mLayout, null);
  104. mView = view;
  105. if (mLayout == R.layout.file_details_fragment) {
  106. mView.findViewById(R.id.fdKeepInSync).setOnClickListener(this);
  107. ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.fdProgressBar);
  108. mProgressListener = new ProgressListener(progressBar);
  109. mView.findViewById(R.id.fdCancelBtn).setOnClickListener(this);
  110. }
  111. updateFileDetails(false, false);
  112. return view;
  113. }
  114. @Override
  115. public void onSaveInstanceState(Bundle outState) {
  116. super.onSaveInstanceState(outState);
  117. outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
  118. outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
  119. }
  120. @Override
  121. public void onStart() {
  122. super.onStart();
  123. listenForTransferProgress();
  124. }
  125. @Override
  126. public void onStop() {
  127. leaveTransferProgress();
  128. super.onStop();
  129. }
  130. @Override
  131. public View getView() {
  132. return super.getView() == null ? mView : super.getView();
  133. }
  134. /**
  135. * {@inheritDoc}
  136. */
  137. @Override
  138. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  139. super.onCreateOptionsMenu(menu, inflater);
  140. inflater.inflate(R.menu.file_actions_menu, menu);
  141. }
  142. /**
  143. * {@inheritDoc}
  144. */
  145. @Override
  146. public void onPrepareOptionsMenu (Menu menu) {
  147. super.onPrepareOptionsMenu(menu);
  148. if (mContainerActivity.getStorageManager() != null) {
  149. FileMenuFilter mf = new FileMenuFilter(
  150. getFile(),
  151. mContainerActivity.getStorageManager().getAccount(),
  152. mContainerActivity,
  153. getSherlockActivity()
  154. );
  155. mf.filter(menu);
  156. }
  157. // additional restriction for this fragment
  158. MenuItem item = menu.findItem(R.id.action_see_details);
  159. if (item != null) {
  160. item.setVisible(false);
  161. item.setEnabled(false);
  162. }
  163. // additional restriction for this fragment
  164. item = menu.findItem(R.id.action_move);
  165. if (item != null) {
  166. item.setVisible(false);
  167. item.setEnabled(false);
  168. }
  169. }
  170. /**
  171. * {@inheritDoc}
  172. */
  173. @Override
  174. public boolean onOptionsItemSelected(MenuItem item) {
  175. switch (item.getItemId()) {
  176. case R.id.action_share_file: {
  177. mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
  178. return true;
  179. }
  180. case R.id.action_unshare_file: {
  181. mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
  182. return true;
  183. }
  184. case R.id.action_open_file_with: {
  185. mContainerActivity.getFileOperationsHelper().openFile(getFile());
  186. return true;
  187. }
  188. case R.id.action_remove_file: {
  189. RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
  190. dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
  191. return true;
  192. }
  193. case R.id.action_rename_file: {
  194. RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
  195. dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
  196. return true;
  197. }
  198. case R.id.action_cancel_download:
  199. case R.id.action_cancel_upload: {
  200. ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
  201. return true;
  202. }
  203. case R.id.action_download_file:
  204. case R.id.action_sync_file: {
  205. mContainerActivity.getFileOperationsHelper().syncFile(getFile());
  206. return true;
  207. }
  208. case R.id.action_send_file: {
  209. // Obtain the file
  210. if (!getFile().isDown()) { // Download the file
  211. Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
  212. ((FileDisplayActivity)mContainerActivity).startDownloadForSending(getFile());
  213. } else {
  214. mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
  215. }
  216. return true;
  217. }
  218. default:
  219. return false;
  220. }
  221. }
  222. @Override
  223. public void onClick(View v) {
  224. switch (v.getId()) {
  225. case R.id.fdKeepInSync: {
  226. toggleKeepInSync();
  227. break;
  228. }
  229. case R.id.fdCancelBtn: {
  230. ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
  231. break;
  232. }
  233. default:
  234. Log_OC.e(TAG, "Incorrect view clicked!");
  235. }
  236. }
  237. private void toggleKeepInSync() {
  238. CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync);
  239. OCFile file = getFile();
  240. file.setKeepInSync(cb.isChecked());
  241. mContainerActivity.getStorageManager().saveFile(file);
  242. /// register the OCFile instance in the observer service to monitor local updates
  243. Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
  244. getActivity(),
  245. file,
  246. mAccount,
  247. cb.isChecked());
  248. getActivity().startService(observedFileIntent);
  249. /// immediate content synchronization
  250. if (file.keepInSync()) {
  251. mContainerActivity.getFileOperationsHelper().syncFile(getFile());
  252. }
  253. }
  254. /**
  255. * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
  256. *
  257. * @return True when the fragment was created with the empty layout.
  258. */
  259. public boolean isEmpty() {
  260. return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
  261. }
  262. /**
  263. * Use this method to signal this Activity that it shall update its view.
  264. *
  265. * @param file : An {@link OCFile}
  266. */
  267. public void updateFileDetails(OCFile file, Account ocAccount) {
  268. setFile(file);
  269. mAccount = ocAccount;
  270. updateFileDetails(false, false);
  271. }
  272. /**
  273. * Updates the view with all relevant details about that file.
  274. *
  275. * TODO Remove parameter when the transferring state of files is kept in database.
  276. *
  277. * @param transferring Flag signaling if the file should be considered as downloading or uploading,
  278. * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
  279. * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
  280. *
  281. * @param refresh If 'true', try to refresh the whole file from the database
  282. */
  283. public void updateFileDetails(boolean transferring, boolean refresh) {
  284. if (readyToShow()) {
  285. FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
  286. if (refresh && storageManager != null) {
  287. setFile(storageManager.getFileByPath(getFile().getRemotePath()));
  288. }
  289. OCFile file = getFile();
  290. // set file details
  291. setFilename(file.getFileName());
  292. setFiletype(file.getMimetype(), file.getFileName());
  293. setFilesize(file.getFileLength());
  294. if(ocVersionSupportsTimeCreated()){
  295. setTimeCreated(file.getCreationTimestamp());
  296. }
  297. setTimeModified(file.getModificationTimestamp());
  298. CheckBox cb = (CheckBox)getView().findViewById(R.id.fdKeepInSync);
  299. cb.setChecked(file.keepInSync());
  300. // configure UI for depending upon local state of the file
  301. FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
  302. FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
  303. if (transferring ||
  304. (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) ||
  305. (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))
  306. ) {
  307. setButtonsForTransferring();
  308. } else if (file.isDown()) {
  309. setButtonsForDown();
  310. } else {
  311. // TODO load default preview image; when the local file is removed, the preview remains there
  312. setButtonsForRemote();
  313. }
  314. }
  315. getView().invalidate();
  316. }
  317. /**
  318. * Checks if the fragment is ready to show details of a OCFile
  319. *
  320. * @return 'True' when the fragment is ready to show details of a file
  321. */
  322. private boolean readyToShow() {
  323. return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
  324. }
  325. /**
  326. * Updates the filename in view
  327. * @param filename to set
  328. */
  329. private void setFilename(String filename) {
  330. TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
  331. if (tv != null)
  332. tv.setText(filename);
  333. }
  334. /**
  335. * Updates the MIME type in view
  336. * @param mimetype to set
  337. * @param filename
  338. */
  339. private void setFiletype(String mimetype, String filename) {
  340. TextView tv = (TextView) getView().findViewById(R.id.fdType);
  341. if (tv != null) {
  342. String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);;
  343. tv.setText(printableMimetype);
  344. }
  345. ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
  346. if (iv != null) {
  347. iv.setImageResource(DisplayUtils.getFileTypeIconId(mimetype, filename));
  348. }
  349. }
  350. /**
  351. * Updates the file size in view
  352. * @param filesize in bytes to set
  353. */
  354. private void setFilesize(long filesize) {
  355. TextView tv = (TextView) getView().findViewById(R.id.fdSize);
  356. if (tv != null)
  357. tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
  358. }
  359. /**
  360. * Updates the time that the file was created in view
  361. * @param milliseconds Unix time to set
  362. */
  363. private void setTimeCreated(long milliseconds){
  364. TextView tv = (TextView) getView().findViewById(R.id.fdCreated);
  365. TextView tvLabel = (TextView) getView().findViewById(R.id.fdCreatedLabel);
  366. if(tv != null){
  367. tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
  368. tv.setVisibility(View.VISIBLE);
  369. tvLabel.setVisibility(View.VISIBLE);
  370. }
  371. }
  372. /**
  373. * Updates the time that the file was last modified
  374. * @param milliseconds Unix time to set
  375. */
  376. private void setTimeModified(long milliseconds){
  377. TextView tv = (TextView) getView().findViewById(R.id.fdModified);
  378. if(tv != null){
  379. tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
  380. }
  381. }
  382. /**
  383. * Enables or disables buttons for a file being downloaded
  384. */
  385. private void setButtonsForTransferring() {
  386. if (!isEmpty()) {
  387. // let's protect the user from himself ;)
  388. getView().findViewById(R.id.fdKeepInSync).setEnabled(false);
  389. // show the progress bar for the transfer
  390. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
  391. TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
  392. progressText.setVisibility(View.VISIBLE);
  393. FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
  394. FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
  395. //if (getFile().isDownloading()) {
  396. if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
  397. progressText.setText(R.string.downloader_download_in_progress_ticker);
  398. } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
  399. progressText.setText(R.string.uploader_upload_in_progress_ticker);
  400. }
  401. }
  402. }
  403. /**
  404. * Enables or disables buttons for a file locally available
  405. */
  406. private void setButtonsForDown() {
  407. if (!isEmpty()) {
  408. getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
  409. // hides the progress bar
  410. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
  411. TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
  412. progressText.setVisibility(View.GONE);
  413. }
  414. }
  415. /**
  416. * Enables or disables buttons for a file not locally available
  417. */
  418. private void setButtonsForRemote() {
  419. if (!isEmpty()) {
  420. getView().findViewById(R.id.fdKeepInSync).setEnabled(true);
  421. // hides the progress bar
  422. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
  423. TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
  424. progressText.setVisibility(View.GONE);
  425. }
  426. }
  427. /**
  428. * In ownCloud 3.X.X and 4.X.X there is a bug that SabreDAV does not return
  429. * the time that the file was created. There is a chance that this will
  430. * be fixed in future versions. Use this method to check if this version of
  431. * ownCloud has this fix.
  432. * @return True, if ownCloud the ownCloud version is supporting creation time
  433. */
  434. private boolean ocVersionSupportsTimeCreated(){
  435. /*if(mAccount != null){
  436. AccountManager accManager = (AccountManager) getActivity().getSystemService(Context.ACCOUNT_SERVICE);
  437. OwnCloudVersion ocVersion = new OwnCloudVersion(accManager
  438. .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
  439. if(ocVersion.compareTo(new OwnCloudVersion(0x030000)) < 0) {
  440. return true;
  441. }
  442. }*/
  443. return false;
  444. }
  445. public void listenForTransferProgress() {
  446. if (mProgressListener != null) {
  447. if (mContainerActivity.getFileDownloaderBinder() != null) {
  448. mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile());
  449. }
  450. if (mContainerActivity.getFileUploaderBinder() != null) {
  451. mContainerActivity.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile());
  452. }
  453. }
  454. }
  455. public void leaveTransferProgress() {
  456. if (mProgressListener != null) {
  457. if (mContainerActivity.getFileDownloaderBinder() != null) {
  458. mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
  459. }
  460. if (mContainerActivity.getFileUploaderBinder() != null) {
  461. mContainerActivity.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
  462. }
  463. }
  464. }
  465. /**
  466. * Helper class responsible for updating the progress bar shown for file uploading or downloading
  467. *
  468. * @author David A. Velasco
  469. */
  470. private class ProgressListener implements OnDatatransferProgressListener {
  471. int mLastPercent = 0;
  472. WeakReference<ProgressBar> mProgressBar = null;
  473. ProgressListener(ProgressBar progressBar) {
  474. mProgressBar = new WeakReference<ProgressBar>(progressBar);
  475. }
  476. @Override
  477. public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) {
  478. int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
  479. if (percent != mLastPercent) {
  480. ProgressBar pb = mProgressBar.get();
  481. if (pb != null) {
  482. pb.setProgress(percent);
  483. pb.postInvalidate();
  484. }
  485. }
  486. mLastPercent = percent;
  487. }
  488. };
  489. }