FileDetailFragment.java 22 KB

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