FileDetailFragment.java 19 KB

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