FileDetailFragment.java 28 KB

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