FileDetailFragment.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author Bartek Przybylski
  5. * @author David A. Velasco
  6. * @author Andy Scherzinger
  7. * @author Chris Narkiewicz
  8. *
  9. * Copyright (C) 2011 Bartek Przybylski
  10. * Copyright (C) 2016 ownCloud Inc.
  11. * Copyright (C) 2018 Andy Scherzinger
  12. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. package com.owncloud.android.ui.fragment;
  27. import android.accounts.Account;
  28. import android.app.Activity;
  29. import android.content.Context;
  30. import android.graphics.Bitmap;
  31. import android.os.Bundle;
  32. import android.view.LayoutInflater;
  33. import android.view.Menu;
  34. import android.view.MenuItem;
  35. import android.view.View;
  36. import android.view.View.OnClickListener;
  37. import android.view.ViewGroup;
  38. import android.widget.ImageButton;
  39. import android.widget.ImageView;
  40. import android.widget.LinearLayout;
  41. import android.widget.PopupMenu;
  42. import android.widget.ProgressBar;
  43. import android.widget.TextView;
  44. import com.google.android.material.tabs.TabLayout;
  45. import com.nextcloud.client.account.User;
  46. import com.nextcloud.client.account.UserAccountManager;
  47. import com.nextcloud.client.device.DeviceInfo;
  48. import com.nextcloud.client.di.Injectable;
  49. import com.nextcloud.client.network.ConnectivityService;
  50. import com.nextcloud.client.preferences.AppPreferences;
  51. import com.owncloud.android.MainApp;
  52. import com.owncloud.android.R;
  53. import com.owncloud.android.datamodel.FileDataStorageManager;
  54. import com.owncloud.android.datamodel.OCFile;
  55. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  56. import com.owncloud.android.files.FileMenuFilter;
  57. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  58. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  59. import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
  60. import com.owncloud.android.lib.common.utils.Log_OC;
  61. import com.owncloud.android.ui.activity.FileDisplayActivity;
  62. import com.owncloud.android.ui.activity.ToolbarActivity;
  63. import com.owncloud.android.ui.adapter.FileDetailTabAdapter;
  64. import com.owncloud.android.ui.dialog.RemoveFilesDialogFragment;
  65. import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
  66. import com.owncloud.android.utils.DisplayUtils;
  67. import com.owncloud.android.utils.MimeTypeUtil;
  68. import com.owncloud.android.utils.ThemeUtils;
  69. import java.lang.ref.WeakReference;
  70. import javax.inject.Inject;
  71. import androidx.annotation.NonNull;
  72. import androidx.annotation.Nullable;
  73. import androidx.core.content.res.ResourcesCompat;
  74. import androidx.viewpager.widget.ViewPager;
  75. import butterknife.BindView;
  76. import butterknife.ButterKnife;
  77. import butterknife.Unbinder;
  78. /**
  79. * This Fragment is used to display the details about a file.
  80. */
  81. public class FileDetailFragment extends FileFragment implements OnClickListener, Injectable {
  82. private static final String TAG = FileDetailFragment.class.getSimpleName();
  83. private static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";
  84. static final String FTAG_RENAME_FILE = "RENAME_FILE_FRAGMENT";
  85. private static final String ARG_FILE = "FILE";
  86. private static final String ARG_USER = "USER";
  87. private static final String ARG_ACTIVE_TAB = "TAB";
  88. @BindView(R.id.detail_container)
  89. LinearLayout detailContainer;
  90. @BindView(R.id.progressBlock)
  91. View downloadProgressContainer;
  92. @BindView(R.id.cancelBtn)
  93. ImageButton cancelButton;
  94. @BindView(R.id.progressBar)
  95. ProgressBar progressBar;
  96. @BindView(R.id.progressText)
  97. TextView progressText;
  98. @BindView(R.id.filename)
  99. TextView fileName;
  100. @BindView(R.id.size)
  101. TextView fileSize;
  102. @BindView(R.id.last_modification_timestamp)
  103. TextView fileModifiedTimestamp;
  104. @BindView(R.id.favorite)
  105. ImageView favoriteIcon;
  106. @BindView(R.id.overflow_menu)
  107. ImageView overflowMenu;
  108. @BindView(R.id.tab_layout)
  109. TabLayout tabLayout;
  110. @BindView(R.id.pager)
  111. ViewPager viewPager;
  112. @BindView(R.id.empty_list_view)
  113. public LinearLayout emptyContentContainer;
  114. @BindView(R.id.empty_list_view_headline)
  115. public TextView emptyContentHeadline;
  116. @BindView(R.id.empty_list_icon)
  117. public ImageView emptyContentIcon;
  118. @BindView(R.id.empty_list_progress)
  119. public ProgressBar emptyContentProgressBar;
  120. private int layout;
  121. private View view;
  122. private User user;
  123. private Unbinder unbinder;
  124. private boolean previewLoaded;
  125. private ProgressListener progressListener;
  126. private ToolbarActivity toolbarActivity;
  127. private int activeTab;
  128. @Inject AppPreferences preferences;
  129. @Inject ConnectivityService connectivityService;
  130. @Inject UserAccountManager accountManager;
  131. @Inject DeviceInfo deviceInfo;
  132. /**
  133. * Public factory method to create new FileDetailFragment instances.
  134. *
  135. * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
  136. *
  137. * @param fileToDetail An {@link OCFile} to show in the fragment
  138. * @param user Currently active user
  139. * @return New fragment with arguments set
  140. */
  141. public static FileDetailFragment newInstance(OCFile fileToDetail, User user) {
  142. FileDetailFragment frag = new FileDetailFragment();
  143. Bundle args = new Bundle();
  144. args.putParcelable(ARG_FILE, fileToDetail);
  145. args.putParcelable(ARG_USER, user);
  146. frag.setArguments(args);
  147. return frag;
  148. }
  149. /**
  150. * Public factory method to create new FileDetailFragment instances.
  151. *
  152. * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
  153. *
  154. * @param fileToDetail An {@link OCFile} to show in the fragment
  155. * @param user Currently active user
  156. * @param activeTab to be active tab
  157. * @return New fragment with arguments set
  158. */
  159. public static FileDetailFragment newInstance(OCFile fileToDetail, User user, int activeTab) {
  160. FileDetailFragment frag = new FileDetailFragment();
  161. Bundle args = new Bundle();
  162. args.putParcelable(ARG_FILE, fileToDetail);
  163. args.putParcelable(ARG_USER, user);
  164. args.putInt(ARG_ACTIVE_TAB, activeTab);
  165. frag.setArguments(args);
  166. return frag;
  167. }
  168. /**
  169. * Creates an empty details fragment.
  170. *
  171. * It's necessary to keep a public constructor without parameters; the system uses it when tries
  172. * to reinstate a fragment automatically.
  173. */
  174. public FileDetailFragment() {
  175. super();
  176. user = null;
  177. layout = R.layout.file_details_fragment;
  178. progressListener = null;
  179. }
  180. /**
  181. * return the reference to the file detail sharing fragment to communicate with it.
  182. *
  183. * @return reference to the {@link FileDetailSharingFragment}
  184. */
  185. public FileDetailSharingFragment getFileDetailSharingFragment() {
  186. return ((FileDetailTabAdapter)viewPager.getAdapter()).getFileDetailSharingFragment();
  187. }
  188. /**
  189. * return the reference to the file detail activity fragment to communicate with it.
  190. *
  191. * @return reference to the {@link FileDetailActivitiesFragment}
  192. */
  193. public FileDetailActivitiesFragment getFileDetailActivitiesFragment() {
  194. return ((FileDetailTabAdapter) viewPager.getAdapter()).getFileDetailActivitiesFragment();
  195. }
  196. @Override
  197. public void onAttach(Activity activity) {
  198. super.onAttach(activity);
  199. }
  200. @Override
  201. public void onActivityCreated(Bundle savedInstanceState) {
  202. super.onActivityCreated(savedInstanceState);
  203. setHasOptionsMenu(true);
  204. }
  205. @Override
  206. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  207. Bundle arguments = getArguments();
  208. if (arguments == null) {
  209. throw new IllegalArgumentException("Arguments may not be null");
  210. }
  211. setFile(arguments.getParcelable(ARG_FILE));
  212. user = arguments.getParcelable(ARG_USER);
  213. activeTab = arguments.getInt(ARG_ACTIVE_TAB, 0);
  214. if (savedInstanceState != null) {
  215. setFile(savedInstanceState.getParcelable(ARG_FILE));
  216. user = savedInstanceState.getParcelable(ARG_USER);
  217. }
  218. view = inflater.inflate(layout, null);
  219. unbinder = ButterKnife.bind(this, view);
  220. if (getFile() == null || user == null) {
  221. showEmptyContent();
  222. } else {
  223. emptyContentContainer.setVisibility(View.GONE);
  224. }
  225. return view;
  226. }
  227. @Override
  228. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  229. if (getFile() != null && user != null) {
  230. ThemeUtils.colorHorizontalProgressBar(progressBar, ThemeUtils.primaryAccentColor(getContext()));
  231. progressListener = new ProgressListener(progressBar);
  232. cancelButton.setOnClickListener(this);
  233. favoriteIcon.setOnClickListener(this);
  234. overflowMenu.setOnClickListener(this);
  235. fileModifiedTimestamp.setOnClickListener(this);
  236. updateFileDetails(false, false);
  237. }
  238. }
  239. private void onOverflowIconClicked(View view) {
  240. PopupMenu popup = new PopupMenu(getActivity(), view);
  241. popup.inflate(R.menu.fragment_file_detail);
  242. prepareOptionsMenu(popup.getMenu());
  243. popup.setOnMenuItemClickListener(this::optionsItemSelected);
  244. popup.show();
  245. }
  246. private void setupViewPager() {
  247. tabLayout.removeAllTabs();
  248. tabLayout.addTab(tabLayout.newTab().setText(R.string.drawer_item_activities));
  249. tabLayout.addTab(tabLayout.newTab().setText(R.string.share_dialog_title));
  250. tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
  251. tabLayout.setSelectedTabIndicatorColor(ThemeUtils.primaryAccentColor(getContext()));
  252. final FileDetailTabAdapter adapter = new FileDetailTabAdapter(getFragmentManager(), getFile(), user);
  253. viewPager.setAdapter(adapter);
  254. viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout) {
  255. @Override
  256. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  257. if (activeTab == 0) {
  258. getFileDetailActivitiesFragment().markCommentsAsRead();
  259. }
  260. super.onPageScrolled(position, positionOffset, positionOffsetPixels);
  261. }
  262. });
  263. tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
  264. @Override
  265. public void onTabSelected(TabLayout.Tab tab) {
  266. viewPager.setCurrentItem(tab.getPosition());
  267. if (tab.getPosition() == 0) {
  268. FileDetailActivitiesFragment fragment = getFileDetailActivitiesFragment();
  269. if (fragment != null) {
  270. fragment.markCommentsAsRead();
  271. }
  272. }
  273. }
  274. @Override
  275. public void onTabUnselected(TabLayout.Tab tab) {
  276. // unused at the moment
  277. }
  278. @Override
  279. public void onTabReselected(TabLayout.Tab tab) {
  280. // unused at the moment
  281. }
  282. });
  283. tabLayout.getTabAt(activeTab).select();
  284. }
  285. @Override
  286. public void onSaveInstanceState(@NonNull Bundle outState) {
  287. super.onSaveInstanceState(outState);
  288. outState.putParcelable(ARG_FILE, getFile());
  289. outState.putParcelable(ARG_USER, user);
  290. }
  291. @Override
  292. public void onStart() {
  293. super.onStart();
  294. listenForTransferProgress();
  295. }
  296. @Override
  297. public void onResume() {
  298. super.onResume();
  299. if (toolbarActivity != null && previewLoaded) {
  300. toolbarActivity.setPreviewImageVisibility(true);
  301. }
  302. }
  303. @Override
  304. public void onPause() {
  305. super.onPause();
  306. }
  307. @Override
  308. public void onStop() {
  309. leaveTransferProgress();
  310. if (toolbarActivity != null) {
  311. toolbarActivity.hidePreviewImage();
  312. }
  313. super.onStop();
  314. }
  315. @Override
  316. public void onAttach(@NonNull Context context) {
  317. super.onAttach(context);
  318. if (context instanceof ToolbarActivity) {
  319. toolbarActivity = (ToolbarActivity) context;
  320. }
  321. }
  322. @Override
  323. public void onDestroy() {
  324. super.onDestroy();
  325. unbinder.unbind();
  326. }
  327. @Override
  328. public View getView() {
  329. return super.getView() == null ? view : super.getView();
  330. }
  331. @Override
  332. public void onPrepareOptionsMenu(Menu menu) {
  333. super.onPrepareOptionsMenu(menu);
  334. FileMenuFilter.hideAll(menu);
  335. }
  336. private void prepareOptionsMenu(Menu menu) {
  337. if (containerActivity.getStorageManager() != null) {
  338. Account currentAccount = containerActivity.getStorageManager().getAccount();
  339. FileMenuFilter mf = new FileMenuFilter(
  340. getFile(),
  341. currentAccount,
  342. containerActivity,
  343. getActivity(),
  344. false,
  345. deviceInfo,
  346. accountManager.getUser()
  347. );
  348. mf.filter(menu,
  349. true,
  350. accountManager.isMediaStreamingSupported(currentAccount));
  351. }
  352. if (getFile().isFolder()) {
  353. FileMenuFilter.hideMenuItems(menu.findItem(R.id.action_send_file));
  354. FileMenuFilter.hideMenuItems(menu.findItem(R.id.action_sync_file));
  355. }
  356. }
  357. private boolean optionsItemSelected(MenuItem item) {
  358. switch (item.getItemId()) {
  359. case R.id.action_send_file: {
  360. containerActivity.getFileOperationsHelper().sendShareFile(getFile(), true);
  361. return true;
  362. }
  363. case R.id.action_open_file_with: {
  364. containerActivity.getFileOperationsHelper().openFile(getFile());
  365. return true;
  366. }
  367. case R.id.action_remove_file: {
  368. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(getFile());
  369. dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
  370. return true;
  371. }
  372. case R.id.action_rename_file: {
  373. RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
  374. dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
  375. return true;
  376. }
  377. case R.id.action_cancel_sync: {
  378. ((FileDisplayActivity) containerActivity).cancelTransference(getFile());
  379. return true;
  380. }
  381. case R.id.action_download_file:
  382. case R.id.action_sync_file: {
  383. containerActivity.getFileOperationsHelper().syncFile(getFile());
  384. return true;
  385. }
  386. case R.id.action_set_as_wallpaper: {
  387. containerActivity.getFileOperationsHelper().setPictureAs(getFile(), getView());
  388. return true;
  389. }
  390. case R.id.action_encrypted: {
  391. // TODO implement or remove
  392. return true;
  393. }
  394. case R.id.action_unset_encrypted: {
  395. // TODO implement or remove
  396. return true;
  397. }
  398. default:
  399. return super.onOptionsItemSelected(item);
  400. }
  401. }
  402. @Override
  403. public void onClick(View v) {
  404. switch (v.getId()) {
  405. case R.id.cancelBtn: {
  406. ((FileDisplayActivity) containerActivity).cancelTransference(getFile());
  407. break;
  408. }
  409. case R.id.favorite: {
  410. if (getFile().isFavorite()) {
  411. containerActivity.getFileOperationsHelper().toggleFavoriteFile(getFile(), false);
  412. } else {
  413. containerActivity.getFileOperationsHelper().toggleFavoriteFile(getFile(), true);
  414. }
  415. setFavoriteIconStatus(!getFile().isFavorite());
  416. break;
  417. }
  418. case R.id.overflow_menu: {
  419. onOverflowIconClicked(v);
  420. break;
  421. }
  422. case R.id.last_modification_timestamp: {
  423. boolean showDetailedTimestamp = !preferences.isShowDetailedTimestampEnabled();
  424. preferences.setShowDetailedTimestampEnabled(showDetailedTimestamp);
  425. setFileModificationTimestamp(getFile(), showDetailedTimestamp);
  426. }
  427. default:
  428. Log_OC.e(TAG, "Incorrect view clicked!");
  429. break;
  430. }
  431. }
  432. /**
  433. * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
  434. *
  435. * @return True when the fragment was created with the empty layout.
  436. */
  437. public boolean isEmpty() {
  438. return layout == R.layout.empty_list || getFile() == null || user == null;
  439. }
  440. /**
  441. * Use this method to signal this Activity that it shall update its view.
  442. *
  443. * @param file : An {@link OCFile}
  444. */
  445. public void updateFileDetails(OCFile file, User user) {
  446. setFile(file);
  447. this.user = user;
  448. updateFileDetails(false, false);
  449. }
  450. /**
  451. * Updates the view with all relevant details about that file.
  452. *
  453. * TODO Remove parameter when the transferring state of files is kept in database.
  454. *
  455. * @param transferring Flag signaling if the file should be considered as downloading or uploading,
  456. * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
  457. * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
  458. * @param refresh If 'true', try to refresh the whole file from the database
  459. */
  460. public void updateFileDetails(boolean transferring, boolean refresh) {
  461. if (readyToShow()) {
  462. FileDataStorageManager storageManager = containerActivity.getStorageManager();
  463. if (storageManager == null) {
  464. return;
  465. }
  466. if (refresh) {
  467. setFile(storageManager.getFileByPath(getFile().getRemotePath()));
  468. }
  469. OCFile file = getFile();
  470. // set file details
  471. if (MimeTypeUtil.isImage(file)) {
  472. fileName.setText(file.getFileName());
  473. } else {
  474. fileName.setVisibility(View.GONE);
  475. }
  476. fileSize.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
  477. boolean showDetailedTimestamp = preferences.isShowDetailedTimestampEnabled();
  478. setFileModificationTimestamp(file, showDetailedTimestamp);
  479. setFilePreview(file);
  480. setFavoriteIconStatus(file.isFavorite());
  481. // configure UI for depending upon local state of the file
  482. FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
  483. FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
  484. if (transferring
  485. || (downloaderBinder != null && downloaderBinder.isDownloading(user.toPlatformAccount(), file))
  486. || (uploaderBinder != null && uploaderBinder.isUploading(user.toPlatformAccount(), file))) {
  487. setButtonsForTransferring();
  488. } else if (file.isDown()) {
  489. setButtonsForDown();
  490. } else {
  491. // TODO load default preview image; when the local file is removed, the preview
  492. // remains there
  493. setButtonsForRemote();
  494. }
  495. }
  496. setupViewPager();
  497. getView().invalidate();
  498. }
  499. private void setFileModificationTimestamp(OCFile file, boolean showDetailedTimestamp) {
  500. if (showDetailedTimestamp) {
  501. fileModifiedTimestamp.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
  502. } else {
  503. fileModifiedTimestamp.setText(DisplayUtils.getRelativeTimestamp(getContext(),
  504. file.getModificationTimestamp()));
  505. }
  506. }
  507. private void setFavoriteIconStatus(boolean isFavorite) {
  508. if (isFavorite) {
  509. favoriteIcon.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_star, null));
  510. } else {
  511. favoriteIcon.setImageDrawable(ResourcesCompat.getDrawable(getResources(),
  512. R.drawable.ic_star_outline,
  513. null));
  514. }
  515. }
  516. /**
  517. * Checks if the fragment is ready to show details of a OCFile
  518. *
  519. * @return 'True' when the fragment is ready to show details of a file
  520. */
  521. private boolean readyToShow() {
  522. return getFile() != null && user != null && layout == R.layout.file_details_fragment;
  523. }
  524. /**
  525. * Updates the file preview if possible
  526. *
  527. * @param file a {@link OCFile} to be previewed
  528. */
  529. private void setFilePreview(OCFile file) {
  530. Bitmap resizedImage;
  531. if (toolbarActivity != null && MimeTypeUtil.isImage(file)) {
  532. String tagId = String.valueOf(ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + getFile().getRemoteId());
  533. resizedImage = ThumbnailsCacheManager.getBitmapFromDiskCache(tagId);
  534. if (resizedImage != null && !file.isUpdateThumbnailNeeded()) {
  535. toolbarActivity.setPreviewImageBitmap(resizedImage);
  536. previewLoaded = true;
  537. } else {
  538. // show thumbnail while loading resized image
  539. Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
  540. String.valueOf(ThumbnailsCacheManager.PREFIX_THUMBNAIL + getFile().getRemoteId()));
  541. if (thumbnail != null) {
  542. toolbarActivity.setPreviewImageBitmap(thumbnail);
  543. } else {
  544. thumbnail = ThumbnailsCacheManager.mDefaultImg;
  545. }
  546. // generate new resized image
  547. if (ThumbnailsCacheManager.cancelPotentialThumbnailWork(getFile(), toolbarActivity.getPreviewImageView()) &&
  548. containerActivity.getStorageManager() != null) {
  549. final ThumbnailsCacheManager.ResizedImageGenerationTask task =
  550. new ThumbnailsCacheManager.ResizedImageGenerationTask(this,
  551. toolbarActivity.getPreviewImageView(),
  552. containerActivity.getStorageManager(),
  553. connectivityService,
  554. containerActivity.getStorageManager().getAccount());
  555. if (resizedImage == null) {
  556. resizedImage = thumbnail;
  557. }
  558. final ThumbnailsCacheManager.AsyncResizedImageDrawable asyncDrawable =
  559. new ThumbnailsCacheManager.AsyncResizedImageDrawable(
  560. MainApp.getAppContext().getResources(),
  561. resizedImage,
  562. task
  563. );
  564. toolbarActivity.setPreviewImageDrawable(asyncDrawable);
  565. previewLoaded = true;
  566. task.execute(getFile());
  567. }
  568. }
  569. } else {
  570. previewLoaded = false;
  571. }
  572. }
  573. /**
  574. * Enables or disables buttons for a file being downloaded
  575. */
  576. private void setButtonsForTransferring() {
  577. if (!isEmpty()) {
  578. // show the progress bar for the transfer
  579. downloadProgressContainer.setVisibility(View.VISIBLE);
  580. progressText.setVisibility(View.VISIBLE);
  581. FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
  582. FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
  583. //if (getFile().isDownloading()) {
  584. if (downloaderBinder != null && downloaderBinder.isDownloading(user.toPlatformAccount(), getFile())) {
  585. progressText.setText(R.string.downloader_download_in_progress_ticker);
  586. }
  587. else {
  588. if (uploaderBinder != null && uploaderBinder.isUploading(user.toPlatformAccount(), getFile())) {
  589. progressText.setText(R.string.uploader_upload_in_progress_ticker);
  590. }
  591. }
  592. }
  593. }
  594. /**
  595. * Enables or disables buttons for a file locally available
  596. */
  597. private void setButtonsForDown() {
  598. if (!isEmpty()) {
  599. // hides the progress bar
  600. downloadProgressContainer.setVisibility(View.GONE);
  601. }
  602. }
  603. /**
  604. * Enables or disables buttons for a file not locally available
  605. */
  606. private void setButtonsForRemote() {
  607. if (!isEmpty()) {
  608. // hides the progress bar
  609. downloadProgressContainer.setVisibility(View.GONE);
  610. }
  611. }
  612. public void listenForTransferProgress() {
  613. if (progressListener != null) {
  614. if (containerActivity.getFileDownloaderBinder() != null) {
  615. containerActivity.getFileDownloaderBinder().
  616. addDatatransferProgressListener(progressListener, getFile());
  617. }
  618. if (containerActivity.getFileUploaderBinder() != null) {
  619. containerActivity.getFileUploaderBinder().
  620. addDatatransferProgressListener(progressListener, user, getFile());
  621. }
  622. } else {
  623. Log_OC.d(TAG, "progressListener == null");
  624. }
  625. }
  626. private void leaveTransferProgress() {
  627. if (progressListener != null) {
  628. if (containerActivity.getFileDownloaderBinder() != null) {
  629. containerActivity.getFileDownloaderBinder().
  630. removeDatatransferProgressListener(progressListener, getFile());
  631. }
  632. if (containerActivity.getFileUploaderBinder() != null) {
  633. containerActivity.getFileUploaderBinder().
  634. removeDatatransferProgressListener(progressListener, user, getFile());
  635. }
  636. }
  637. }
  638. private void showEmptyContent() {
  639. if (emptyContentContainer != null) {
  640. emptyContentContainer.setVisibility(View.VISIBLE);
  641. detailContainer.setVisibility(View.GONE);
  642. emptyContentHeadline.setText(R.string.file_details_no_content);
  643. emptyContentProgressBar.setVisibility(View.GONE);
  644. emptyContentIcon.setImageResource(R.drawable.ic_list_empty_error);
  645. emptyContentIcon.setVisibility(View.VISIBLE);
  646. }
  647. }
  648. /**
  649. * Helper class responsible for updating the progress bar shown for file downloading.
  650. */
  651. private class ProgressListener implements OnDatatransferProgressListener {
  652. private int lastPercent;
  653. private WeakReference<ProgressBar> progressBarReference;
  654. ProgressListener(ProgressBar progressBar) {
  655. progressBarReference = new WeakReference<>(progressBar);
  656. }
  657. @Override
  658. public void onTransferProgress(long progressRate, long totalTransferredSoFar,
  659. long totalToTransfer, String filename) {
  660. int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
  661. if (percent != lastPercent) {
  662. ProgressBar pb = progressBarReference.get();
  663. if (pb != null) {
  664. pb.setProgress(percent);
  665. pb.postInvalidate();
  666. }
  667. }
  668. lastPercent = percent;
  669. }
  670. }
  671. }