FileDetailFragment.java 28 KB

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