FileDetailFragment.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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) 2016 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 android.accounts.Account;
  24. import android.graphics.Bitmap;
  25. import android.os.Bundle;
  26. import android.view.LayoutInflater;
  27. import android.view.Menu;
  28. import android.view.MenuInflater;
  29. import android.view.MenuItem;
  30. import android.view.View;
  31. import android.view.View.OnClickListener;
  32. import android.view.ViewGroup;
  33. import android.widget.CheckBox;
  34. import android.widget.ImageView;
  35. import android.widget.ProgressBar;
  36. import android.widget.TextView;
  37. import com.owncloud.android.MainApp;
  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.datamodel.ThumbnailsCacheManager;
  42. import com.owncloud.android.files.FileMenuFilter;
  43. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  44. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  45. import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
  46. import com.owncloud.android.lib.common.utils.Log_OC;
  47. import com.owncloud.android.ui.activity.FileActivity;
  48. import com.owncloud.android.ui.activity.FileDisplayActivity;
  49. import com.owncloud.android.ui.dialog.RemoveFilesDialogFragment;
  50. import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
  51. import com.owncloud.android.utils.AnalyticsUtils;
  52. import com.owncloud.android.utils.DisplayUtils;
  53. import com.owncloud.android.utils.MimeTypeUtil;
  54. import java.lang.ref.WeakReference;
  55. /**
  56. * This Fragment is used to display the details about a file.
  57. */
  58. public class FileDetailFragment extends FileFragment implements OnClickListener {
  59. private int mLayout;
  60. private View mView;
  61. private Account mAccount;
  62. public ProgressListener mProgressListener;
  63. private static final String TAG = FileDetailFragment.class.getSimpleName();
  64. public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";
  65. public static final String FTAG_RENAME_FILE = "RENAME_FILE_FRAGMENT";
  66. private static final String ARG_FILE = "FILE";
  67. private static final String ARG_ACCOUNT = "ACCOUNT";
  68. private static final String SCREEN_NAME = "File details";
  69. /**
  70. * Public factory method to create new FileDetailFragment instances.
  71. *
  72. * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
  73. *
  74. * @param fileToDetail An {@link OCFile} to show in the fragment
  75. * @param account An ownCloud account; needed to start downloads
  76. * @return New fragment with arguments set
  77. */
  78. public static FileDetailFragment newInstance(OCFile fileToDetail, Account account) {
  79. FileDetailFragment frag = new FileDetailFragment();
  80. Bundle args = new Bundle();
  81. args.putParcelable(ARG_FILE, fileToDetail);
  82. args.putParcelable(ARG_ACCOUNT, account);
  83. frag.setArguments(args);
  84. return frag;
  85. }
  86. /**
  87. * Creates an empty details fragment.
  88. *
  89. * It's necessary to keep a public constructor without parameters; the system uses it when tries
  90. * to reinstantiate a fragment automatically.
  91. */
  92. public FileDetailFragment() {
  93. super();
  94. mAccount = null;
  95. mLayout = R.layout.file_details_empty;
  96. mProgressListener = null;
  97. }
  98. @Override
  99. public void onResume() {
  100. super.onResume();
  101. if (getActivity() != null) {
  102. AnalyticsUtils.setCurrentScreenName(getActivity(), SCREEN_NAME, TAG);
  103. }
  104. }
  105. @Override
  106. public void onActivityCreated(Bundle savedInstanceState) {
  107. super.onCreate(savedInstanceState);
  108. setHasOptionsMenu(true);
  109. }
  110. @Override
  111. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  112. Bundle savedInstanceState) {
  113. setFile((OCFile) getArguments().getParcelable(ARG_FILE));
  114. mAccount = getArguments().getParcelable(ARG_ACCOUNT);
  115. if (savedInstanceState != null) {
  116. setFile((OCFile) savedInstanceState.getParcelable(FileActivity.EXTRA_FILE));
  117. mAccount = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
  118. }
  119. if (getFile() != null && mAccount != null) {
  120. mLayout = R.layout.file_details_fragment;
  121. }
  122. mView = inflater.inflate(mLayout, null);
  123. if (mLayout == R.layout.file_details_fragment) {
  124. mView.findViewById(R.id.fdFavorite).setOnClickListener(this);
  125. ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.fdProgressBar);
  126. DisplayUtils.colorPreLollipopHorizontalProgressBar(progressBar);
  127. mProgressListener = new ProgressListener(progressBar);
  128. mView.findViewById(R.id.fdCancelBtn).setOnClickListener(this);
  129. }
  130. updateFileDetails(false, false);
  131. return mView;
  132. }
  133. @Override
  134. public void onSaveInstanceState(Bundle outState) {
  135. super.onSaveInstanceState(outState);
  136. outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
  137. outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
  138. }
  139. @Override
  140. public void onStart() {
  141. super.onStart();
  142. listenForTransferProgress();
  143. }
  144. @Override
  145. public void onStop() {
  146. leaveTransferProgress();
  147. super.onStop();
  148. }
  149. @Override
  150. public View getView() {
  151. return super.getView() == null ? mView : super.getView();
  152. }
  153. /**
  154. * {@inheritDoc}
  155. */
  156. @Override
  157. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  158. super.onCreateOptionsMenu(menu, inflater);
  159. inflater.inflate(R.menu.file_actions_menu, menu);
  160. }
  161. /**
  162. * {@inheritDoc}
  163. */
  164. @Override
  165. public void onPrepareOptionsMenu(Menu menu) {
  166. super.onPrepareOptionsMenu(menu);
  167. if (mContainerActivity.getStorageManager() != null) {
  168. FileMenuFilter mf = new FileMenuFilter(
  169. getFile(),
  170. mContainerActivity.getStorageManager().getAccount(),
  171. mContainerActivity,
  172. getActivity()
  173. );
  174. mf.filter(menu);
  175. }
  176. // additional restriction for this fragment
  177. MenuItem item = menu.findItem(R.id.action_see_details);
  178. if (item != null) {
  179. item.setVisible(false);
  180. item.setEnabled(false);
  181. }
  182. // additional restriction for this fragment
  183. item = menu.findItem(R.id.action_move);
  184. if (item != null) {
  185. item.setVisible(false);
  186. item.setEnabled(false);
  187. }
  188. // additional restriction for this fragment
  189. item = menu.findItem(R.id.action_copy);
  190. if (item != null) {
  191. item.setVisible(false);
  192. item.setEnabled(false);
  193. }
  194. // additional restriction for this fragment
  195. item = menu.findItem(R.id.action_favorite);
  196. if (item != null) {
  197. item.setVisible(false);
  198. item.setEnabled(false);
  199. }
  200. // additional restriction for this fragment
  201. item = menu.findItem(R.id.action_unset_favorite);
  202. if (item != null) {
  203. item.setVisible(false);
  204. item.setEnabled(false);
  205. }
  206. // additional restriction for this fragment
  207. item = menu.findItem(R.id.action_search);
  208. if (item != null) {
  209. item.setVisible(false);
  210. item.setEnabled(false);
  211. }
  212. Boolean dualPane = getResources().getBoolean(R.bool.large_land_layout);
  213. item = menu.findItem(R.id.action_switch_view);
  214. if (item != null && !dualPane){
  215. item.setVisible(false);
  216. item.setEnabled(false);
  217. }
  218. item = menu.findItem(R.id.action_sync_account);
  219. if (item != null && !dualPane) {
  220. item.setVisible(false);
  221. item.setEnabled(false);
  222. }
  223. item = menu.findItem(R.id.action_sort);
  224. if (item != null && !dualPane) {
  225. item.setVisible(false);
  226. item.setEnabled(false);
  227. }
  228. }
  229. /**
  230. * {@inheritDoc}
  231. */
  232. @Override
  233. public boolean onOptionsItemSelected(MenuItem item) {
  234. switch (item.getItemId()) {
  235. case R.id.action_share_file: {
  236. mContainerActivity.getFileOperationsHelper().showShareFile(getFile());
  237. return true;
  238. }
  239. case R.id.action_open_file_with: {
  240. mContainerActivity.getFileOperationsHelper().openFile(getFile());
  241. return true;
  242. }
  243. case R.id.action_remove_file: {
  244. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(getFile());
  245. dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
  246. return true;
  247. }
  248. case R.id.action_rename_file: {
  249. RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
  250. dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
  251. return true;
  252. }
  253. case R.id.action_cancel_sync: {
  254. ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
  255. return true;
  256. }
  257. case R.id.action_download_file:
  258. case R.id.action_sync_file: {
  259. mContainerActivity.getFileOperationsHelper().syncFile(getFile());
  260. return true;
  261. }
  262. case R.id.action_send_file: {
  263. // Obtain the file
  264. if (!getFile().isDown()) { // Download the file
  265. Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
  266. ((FileDisplayActivity) mContainerActivity).startDownloadForSending(getFile());
  267. }
  268. else {
  269. mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
  270. }
  271. return true;
  272. }
  273. default:
  274. return super.onOptionsItemSelected(item);
  275. }
  276. }
  277. @Override
  278. public void onClick(View v) {
  279. switch (v.getId()) {
  280. case R.id.fdFavorite: {
  281. CheckBox cb = (CheckBox) getView().findViewById(R.id.fdFavorite);
  282. mContainerActivity.getFileOperationsHelper().toggleOfflineFile(getFile(),cb.isChecked());
  283. break;
  284. }
  285. case R.id.fdCancelBtn: {
  286. ((FileDisplayActivity) mContainerActivity).cancelTransference(getFile());
  287. break;
  288. }
  289. default:
  290. Log_OC.e(TAG, "Incorrect view clicked!");
  291. }
  292. }
  293. /**
  294. * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
  295. *
  296. * @return True when the fragment was created with the empty layout.
  297. */
  298. public boolean isEmpty() {
  299. return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
  300. }
  301. /**
  302. * Use this method to signal this Activity that it shall update its view.
  303. *
  304. * @param file : An {@link OCFile}
  305. */
  306. public void updateFileDetails(OCFile file, Account ocAccount) {
  307. setFile(file);
  308. mAccount = ocAccount;
  309. updateFileDetails(false, false);
  310. }
  311. /**
  312. * Updates the view with all relevant details about that file.
  313. * <p/>
  314. * TODO Remove parameter when the transferring state of files is kept in database.
  315. *
  316. * @param transferring Flag signaling if the file should be considered as downloading or uploading,
  317. * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
  318. * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
  319. * @param refresh If 'true', try to refresh the whole file from the database
  320. */
  321. public void updateFileDetails(boolean transferring, boolean refresh) {
  322. if (readyToShow()) {
  323. FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
  324. if (refresh && storageManager != null) {
  325. setFile(storageManager.getFileByPath(getFile().getRemotePath()));
  326. }
  327. OCFile file = getFile();
  328. // set file details
  329. setFilename(file.getFileName());
  330. setFiletype(file);
  331. setFilesize(file.getFileLength());
  332. setTimeModified(file.getModificationTimestamp());
  333. CheckBox cb = (CheckBox)getView().findViewById(R.id.fdFavorite);
  334. cb.setChecked(file.isAvailableOffline());
  335. // configure UI for depending upon local state of the file
  336. FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
  337. FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
  338. if (transferring ||
  339. (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) ||
  340. (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))
  341. ) {
  342. setButtonsForTransferring();
  343. } else if (file.isDown()) {
  344. setButtonsForDown();
  345. } else {
  346. // TODO load default preview image; when the local file is removed, the preview
  347. // remains there
  348. setButtonsForRemote();
  349. }
  350. }
  351. getView().invalidate();
  352. }
  353. /**
  354. * Checks if the fragment is ready to show details of a OCFile
  355. *
  356. * @return 'True' when the fragment is ready to show details of a file
  357. */
  358. private boolean readyToShow() {
  359. return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
  360. }
  361. /**
  362. * Updates the filename in view
  363. *
  364. * @param filename to set
  365. */
  366. private void setFilename(String filename) {
  367. TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
  368. if (tv != null) {
  369. tv.setText(filename);
  370. }
  371. }
  372. /**
  373. * Updates the MIME type in view
  374. * @param file : An {@link OCFile}
  375. */
  376. private void setFiletype(OCFile file) {
  377. String mimetype = file.getMimetype();
  378. TextView tv = (TextView) getView().findViewById(R.id.fdType);
  379. if (tv != null) {
  380. // mimetype MIME type to set
  381. String printableMimetype = DisplayUtils.convertMIMEtoPrettyPrint(mimetype);
  382. tv.setText(printableMimetype);
  383. }
  384. ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
  385. if (iv != null) {
  386. Bitmap thumbnail;
  387. iv.setTag(file.getFileId());
  388. if (MimeTypeUtil.isImage(file)) {
  389. String tagId = String.valueOf(file.getRemoteId());
  390. thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(tagId);
  391. if (thumbnail != null && !file.needsUpdateThumbnail()) {
  392. iv.setImageBitmap(thumbnail);
  393. } else {
  394. // generate new Thumbnail
  395. if (ThumbnailsCacheManager.cancelPotentialThumbnailWork(file, iv)) {
  396. final ThumbnailsCacheManager.ThumbnailGenerationTask task =
  397. new ThumbnailsCacheManager.ThumbnailGenerationTask(
  398. iv, mContainerActivity.getStorageManager(), mAccount
  399. );
  400. if (thumbnail == null) {
  401. thumbnail = ThumbnailsCacheManager.mDefaultImg;
  402. }
  403. final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable =
  404. new ThumbnailsCacheManager.AsyncThumbnailDrawable(
  405. MainApp.getAppContext().getResources(),
  406. thumbnail,
  407. task
  408. );
  409. iv.setImageDrawable(asyncDrawable);
  410. task.execute(file);
  411. }
  412. }
  413. } else {
  414. // Name of the file, to deduce the icon to use in case the MIME type is not precise enough
  415. String filename = file.getFileName();
  416. iv.setImageResource(MimeTypeUtil.getFileTypeIconId(mimetype, filename));
  417. }
  418. }
  419. }
  420. /**
  421. * Updates the file size in view
  422. *
  423. * @param filesize in bytes to set
  424. */
  425. private void setFilesize(long filesize) {
  426. TextView tv = (TextView) getView().findViewById(R.id.fdSize);
  427. if (tv != null) {
  428. tv.setText(DisplayUtils.bytesToHumanReadable(filesize));
  429. }
  430. }
  431. /**
  432. * Updates the time that the file was last modified
  433. *
  434. * @param milliseconds Unix time to set
  435. */
  436. private void setTimeModified(long milliseconds) {
  437. TextView tv = (TextView) getView().findViewById(R.id.fdModified);
  438. if (tv != null) {
  439. tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
  440. }
  441. }
  442. /**
  443. * Enables or disables buttons for a file being downloaded
  444. */
  445. private void setButtonsForTransferring() {
  446. if (!isEmpty()) {
  447. // let's protect the user from himself ;)
  448. getView().findViewById(R.id.fdFavorite).setEnabled(false);
  449. // show the progress bar for the transfer
  450. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
  451. TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
  452. progressText.setVisibility(View.VISIBLE);
  453. FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
  454. FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
  455. //if (getFile().isDownloading()) {
  456. if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
  457. progressText.setText(R.string.downloader_download_in_progress_ticker);
  458. }
  459. else {
  460. if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
  461. progressText.setText(R.string.uploader_upload_in_progress_ticker);
  462. }
  463. }
  464. }
  465. }
  466. /**
  467. * Enables or disables buttons for a file locally available
  468. */
  469. private void setButtonsForDown() {
  470. if (!isEmpty()) {
  471. getView().findViewById(R.id.fdFavorite).setEnabled(true);
  472. // hides the progress bar
  473. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
  474. TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
  475. progressText.setVisibility(View.GONE);
  476. }
  477. }
  478. /**
  479. * Enables or disables buttons for a file not locally available
  480. */
  481. private void setButtonsForRemote() {
  482. if (!isEmpty()) {
  483. getView().findViewById(R.id.fdFavorite).setEnabled(true);
  484. // hides the progress bar
  485. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
  486. TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
  487. progressText.setVisibility(View.GONE);
  488. }
  489. }
  490. public void listenForTransferProgress() {
  491. if (mProgressListener != null) {
  492. if (mContainerActivity.getFileDownloaderBinder() != null) {
  493. mContainerActivity.getFileDownloaderBinder().
  494. addDatatransferProgressListener(mProgressListener, mAccount, getFile());
  495. }
  496. if (mContainerActivity.getFileUploaderBinder() != null) {
  497. mContainerActivity.getFileUploaderBinder().
  498. addDatatransferProgressListener(mProgressListener, mAccount, getFile());
  499. }
  500. } else {
  501. Log_OC.d(TAG, "mProgressListener == null");
  502. }
  503. }
  504. public void leaveTransferProgress() {
  505. if (mProgressListener != null) {
  506. if (mContainerActivity.getFileDownloaderBinder() != null) {
  507. mContainerActivity.getFileDownloaderBinder().
  508. removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
  509. }
  510. if (mContainerActivity.getFileUploaderBinder() != null) {
  511. mContainerActivity.getFileUploaderBinder().
  512. removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
  513. }
  514. }
  515. }
  516. /**
  517. * Helper class responsible for updating the progress bar shown for file uploading or
  518. * downloading
  519. */
  520. private class ProgressListener implements OnDatatransferProgressListener {
  521. int mLastPercent = 0;
  522. WeakReference<ProgressBar> mProgressBar = null;
  523. ProgressListener(ProgressBar progressBar) {
  524. mProgressBar = new WeakReference<ProgressBar>(progressBar);
  525. }
  526. @Override
  527. public void onTransferProgress(long progressRate, long totalTransferredSoFar,
  528. long totalToTransfer, String filename) {
  529. int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
  530. if (percent != mLastPercent) {
  531. ProgressBar pb = mProgressBar.get();
  532. if (pb != null) {
  533. pb.setProgress(percent);
  534. pb.postInvalidate();
  535. }
  536. }
  537. mLastPercent = percent;
  538. }
  539. }
  540. }