FileDetailFragment.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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.di.Injectable;
  46. import com.nextcloud.client.network.ConnectivityService;
  47. import com.nextcloud.client.preferences.AppPreferences;
  48. import com.owncloud.android.MainApp;
  49. import com.owncloud.android.R;
  50. import com.owncloud.android.datamodel.FileDataStorageManager;
  51. import com.owncloud.android.datamodel.OCFile;
  52. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  53. import com.owncloud.android.files.FileMenuFilter;
  54. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  55. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  56. import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
  57. import com.owncloud.android.lib.common.utils.Log_OC;
  58. import com.owncloud.android.ui.activity.FileActivity;
  59. import com.owncloud.android.ui.activity.FileDisplayActivity;
  60. import com.owncloud.android.ui.activity.ToolbarActivity;
  61. import com.owncloud.android.ui.adapter.FileDetailTabAdapter;
  62. import com.owncloud.android.ui.dialog.RemoveFilesDialogFragment;
  63. import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
  64. import com.owncloud.android.utils.DisplayUtils;
  65. import com.owncloud.android.utils.MimeTypeUtil;
  66. import com.owncloud.android.utils.ThemeUtils;
  67. import java.lang.ref.WeakReference;
  68. import javax.inject.Inject;
  69. import androidx.annotation.NonNull;
  70. import androidx.annotation.Nullable;
  71. import androidx.viewpager.widget.ViewPager;
  72. import butterknife.BindView;
  73. import butterknife.ButterKnife;
  74. import butterknife.Unbinder;
  75. /**
  76. * This Fragment is used to display the details about a file.
  77. */
  78. public class FileDetailFragment extends FileFragment implements OnClickListener, Injectable {
  79. private static final String TAG = FileDetailFragment.class.getSimpleName();
  80. private static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";
  81. static final String FTAG_RENAME_FILE = "RENAME_FILE_FRAGMENT";
  82. private static final String ARG_FILE = "FILE";
  83. private static final String ARG_ACCOUNT = "ACCOUNT";
  84. private static final String ARG_ACTIVE_TAB = "TAB";
  85. @BindView(R.id.detail_container)
  86. LinearLayout detailContainer;
  87. @BindView(R.id.progressBlock)
  88. View downloadProgressContainer;
  89. @BindView(R.id.cancelBtn)
  90. ImageButton cancelButton;
  91. @BindView(R.id.progressBar)
  92. ProgressBar progressBar;
  93. @BindView(R.id.progressText)
  94. TextView progressText;
  95. @BindView(R.id.filename)
  96. TextView fileName;
  97. @BindView(R.id.size)
  98. TextView fileSize;
  99. @BindView(R.id.last_modification_timestamp)
  100. TextView fileModifiedTimestamp;
  101. @BindView(R.id.favorite)
  102. ImageView favoriteIcon;
  103. @BindView(R.id.overflow_menu)
  104. ImageView overflowMenu;
  105. @BindView(R.id.tab_layout)
  106. TabLayout tabLayout;
  107. @BindView(R.id.pager)
  108. ViewPager viewPager;
  109. @BindView(R.id.empty_list_view)
  110. public LinearLayout emptyContentContainer;
  111. @BindView(R.id.empty_list_view_headline)
  112. public TextView emptyContentHeadline;
  113. @BindView(R.id.empty_list_icon)
  114. public ImageView emptyContentIcon;
  115. @BindView(R.id.empty_list_progress)
  116. public ProgressBar emptyContentProgressBar;
  117. private int layout;
  118. private View view;
  119. private boolean previewLoaded;
  120. private Account account;
  121. private Unbinder unbinder;
  122. private ProgressListener progressListener;
  123. private ToolbarActivity activity;
  124. private int activeTab;
  125. @Inject
  126. AppPreferences preferences;
  127. @Inject
  128. ConnectivityService connectivityService;
  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.black));
  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. FileMenuFilter mf = new FileMenuFilter(
  347. getFile(),
  348. containerActivity.getStorageManager().getAccount(),
  349. containerActivity,
  350. getActivity(),
  351. false
  352. );
  353. mf.filter(menu, true);
  354. }
  355. if (getFile().isFolder()) {
  356. FileMenuFilter.hideMenuItems(menu.findItem(R.id.action_send_file));
  357. FileMenuFilter.hideMenuItems(menu.findItem(R.id.action_sync_file));
  358. }
  359. // dual pane restrictions
  360. if (!getResources().getBoolean(R.bool.large_land_layout)){
  361. FileMenuFilter.hideMenuItems(menu.findItem(R.id.action_sync_account));
  362. }
  363. }
  364. private boolean optionsItemSelected(MenuItem item) {
  365. switch (item.getItemId()) {
  366. case R.id.action_send_file: {
  367. containerActivity.getFileOperationsHelper().sendShareFile(getFile(), true);
  368. return true;
  369. }
  370. case R.id.action_open_file_with: {
  371. containerActivity.getFileOperationsHelper().openFile(getFile());
  372. return true;
  373. }
  374. case R.id.action_remove_file: {
  375. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(getFile());
  376. dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
  377. return true;
  378. }
  379. case R.id.action_rename_file: {
  380. RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
  381. dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
  382. return true;
  383. }
  384. case R.id.action_cancel_sync: {
  385. ((FileDisplayActivity) containerActivity).cancelTransference(getFile());
  386. return true;
  387. }
  388. case R.id.action_download_file:
  389. case R.id.action_sync_file: {
  390. containerActivity.getFileOperationsHelper().syncFile(getFile());
  391. return true;
  392. }
  393. case R.id.action_encrypted: {
  394. // TODO implement or remove
  395. return true;
  396. }
  397. case R.id.action_unset_encrypted: {
  398. // TODO implement or remove
  399. return true;
  400. }
  401. default:
  402. return super.onOptionsItemSelected(item);
  403. }
  404. }
  405. @Override
  406. public void onClick(View v) {
  407. switch (v.getId()) {
  408. case R.id.cancelBtn: {
  409. ((FileDisplayActivity) containerActivity).cancelTransference(getFile());
  410. break;
  411. }
  412. case R.id.favorite: {
  413. if (getFile().isFavorite()) {
  414. containerActivity.getFileOperationsHelper().toggleFavoriteFile(getFile(), false);
  415. } else {
  416. containerActivity.getFileOperationsHelper().toggleFavoriteFile(getFile(), true);
  417. }
  418. setFavoriteIconStatus(!getFile().isFavorite());
  419. break;
  420. }
  421. case R.id.overflow_menu: {
  422. onOverflowIconClicked(v);
  423. break;
  424. }
  425. case R.id.last_modification_timestamp: {
  426. boolean showDetailedTimestamp = !preferences.isShowDetailedTimestampEnabled();
  427. preferences.setShowDetailedTimestampEnabled(showDetailedTimestamp);
  428. setFileModificationTimestamp(getFile(), showDetailedTimestamp);
  429. }
  430. default:
  431. Log_OC.e(TAG, "Incorrect view clicked!");
  432. break;
  433. }
  434. }
  435. /**
  436. * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
  437. *
  438. * @return True when the fragment was created with the empty layout.
  439. */
  440. public boolean isEmpty() {
  441. return layout == R.layout.empty_list || getFile() == null || account == null;
  442. }
  443. /**
  444. * Use this method to signal this Activity that it shall update its view.
  445. *
  446. * @param file : An {@link OCFile}
  447. */
  448. public void updateFileDetails(OCFile file, Account ocAccount) {
  449. setFile(file);
  450. account = ocAccount;
  451. updateFileDetails(false, false);
  452. }
  453. /**
  454. * Updates the view with all relevant details about that file.
  455. *
  456. * TODO Remove parameter when the transferring state of files is kept in database.
  457. *
  458. * @param transferring Flag signaling if the file should be considered as downloading or uploading,
  459. * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
  460. * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
  461. * @param refresh If 'true', try to refresh the whole file from the database
  462. */
  463. public void updateFileDetails(boolean transferring, boolean refresh) {
  464. if (readyToShow()) {
  465. FileDataStorageManager storageManager = containerActivity.getStorageManager();
  466. if (storageManager == null) {
  467. return;
  468. }
  469. if (refresh) {
  470. setFile(storageManager.getFileByPath(getFile().getRemotePath()));
  471. }
  472. OCFile file = getFile();
  473. // set file details
  474. if (MimeTypeUtil.isImage(file)) {
  475. fileName.setText(file.getFileName());
  476. } else {
  477. fileName.setVisibility(View.GONE);
  478. }
  479. fileSize.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
  480. boolean showDetailedTimestamp = preferences.isShowDetailedTimestampEnabled();
  481. setFileModificationTimestamp(file, showDetailedTimestamp);
  482. setFilePreview(file);
  483. setFavoriteIconStatus(file.isFavorite());
  484. // configure UI for depending upon local state of the file
  485. FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
  486. FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
  487. if (transferring
  488. || (downloaderBinder != null && downloaderBinder.isDownloading(account, file))
  489. || (uploaderBinder != null && uploaderBinder.isUploading(account, file))) {
  490. setButtonsForTransferring();
  491. } else if (file.isDown()) {
  492. setButtonsForDown();
  493. } else {
  494. // TODO load default preview image; when the local file is removed, the preview
  495. // remains there
  496. setButtonsForRemote();
  497. }
  498. }
  499. setupViewPager();
  500. getView().invalidate();
  501. }
  502. private void setFileModificationTimestamp(OCFile file, boolean showDetailedTimestamp) {
  503. if (showDetailedTimestamp) {
  504. fileModifiedTimestamp.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
  505. } else {
  506. fileModifiedTimestamp.setText(DisplayUtils.getRelativeTimestamp(getContext(),
  507. file.getModificationTimestamp()));
  508. }
  509. }
  510. private void setFavoriteIconStatus(boolean isFavorite) {
  511. if (isFavorite) {
  512. favoriteIcon.setImageDrawable(getResources().getDrawable(R.drawable.ic_star));
  513. } else {
  514. favoriteIcon.setImageDrawable(getResources().getDrawable(R.drawable.ic_star_outline));
  515. }
  516. }
  517. /**
  518. * Checks if the fragment is ready to show details of a OCFile
  519. *
  520. * @return 'True' when the fragment is ready to show details of a file
  521. */
  522. private boolean readyToShow() {
  523. return getFile() != null && account != null && layout == R.layout.file_details_fragment;
  524. }
  525. /**
  526. * Updates the file preview if possible
  527. *
  528. * @param file a {@link OCFile} to be previewed
  529. */
  530. private void setFilePreview(OCFile file) {
  531. Bitmap resizedImage;
  532. if (activity != null && MimeTypeUtil.isImage(file)) {
  533. String tagId = String.valueOf(ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + getFile().getRemoteId());
  534. resizedImage = ThumbnailsCacheManager.getBitmapFromDiskCache(tagId);
  535. if (resizedImage != null && !file.isUpdateThumbnailNeeded()) {
  536. activity.setPreviewImageBitmap(resizedImage);
  537. activatePreviewImage();
  538. previewLoaded = true;
  539. } else {
  540. // show thumbnail while loading resized image
  541. Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
  542. String.valueOf(ThumbnailsCacheManager.PREFIX_THUMBNAIL + getFile().getRemoteId()));
  543. if (thumbnail != null) {
  544. activity.setPreviewImageBitmap(thumbnail);
  545. } else {
  546. thumbnail = ThumbnailsCacheManager.mDefaultImg;
  547. }
  548. // generate new resized image
  549. if (ThumbnailsCacheManager.cancelPotentialThumbnailWork(getFile(), activity.getPreviewImageView()) &&
  550. containerActivity.getStorageManager() != null) {
  551. final ThumbnailsCacheManager.ResizedImageGenerationTask task =
  552. new ThumbnailsCacheManager.ResizedImageGenerationTask(this,
  553. activity.getPreviewImageView(),
  554. containerActivity.getStorageManager(),
  555. connectivityService,
  556. containerActivity.getStorageManager().getAccount());
  557. if (resizedImage == null) {
  558. resizedImage = thumbnail;
  559. }
  560. final ThumbnailsCacheManager.AsyncResizedImageDrawable asyncDrawable =
  561. new ThumbnailsCacheManager.AsyncResizedImageDrawable(
  562. MainApp.getAppContext().getResources(),
  563. resizedImage,
  564. task
  565. );
  566. activity.setPreviewImageDrawable(asyncDrawable);
  567. activatePreviewImage();
  568. previewLoaded = true;
  569. task.execute(getFile());
  570. }
  571. }
  572. }
  573. }
  574. /**
  575. * Enables or disables buttons for a file being downloaded
  576. */
  577. private void setButtonsForTransferring() {
  578. if (!isEmpty()) {
  579. // show the progress bar for the transfer
  580. downloadProgressContainer.setVisibility(View.VISIBLE);
  581. progressText.setVisibility(View.VISIBLE);
  582. FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
  583. FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
  584. //if (getFile().isDownloading()) {
  585. if (downloaderBinder != null && downloaderBinder.isDownloading(account, getFile())) {
  586. progressText.setText(R.string.downloader_download_in_progress_ticker);
  587. }
  588. else {
  589. if (uploaderBinder != null && uploaderBinder.isUploading(account, getFile())) {
  590. progressText.setText(R.string.uploader_upload_in_progress_ticker);
  591. }
  592. }
  593. }
  594. }
  595. /**
  596. * Enables or disables buttons for a file locally available
  597. */
  598. private void setButtonsForDown() {
  599. if (!isEmpty()) {
  600. // hides the progress bar
  601. downloadProgressContainer.setVisibility(View.GONE);
  602. }
  603. }
  604. /**
  605. * Enables or disables buttons for a file not locally available
  606. */
  607. private void setButtonsForRemote() {
  608. if (!isEmpty()) {
  609. // hides the progress bar
  610. downloadProgressContainer.setVisibility(View.GONE);
  611. }
  612. }
  613. public void listenForTransferProgress() {
  614. if (progressListener != null) {
  615. if (containerActivity.getFileDownloaderBinder() != null) {
  616. containerActivity.getFileDownloaderBinder().
  617. addDatatransferProgressListener(progressListener, account, getFile());
  618. }
  619. if (containerActivity.getFileUploaderBinder() != null) {
  620. containerActivity.getFileUploaderBinder().
  621. addDatatransferProgressListener(progressListener, account, getFile());
  622. }
  623. } else {
  624. Log_OC.d(TAG, "progressListener == null");
  625. }
  626. }
  627. private void leaveTransferProgress() {
  628. if (progressListener != null) {
  629. if (containerActivity.getFileDownloaderBinder() != null) {
  630. containerActivity.getFileDownloaderBinder().
  631. removeDatatransferProgressListener(progressListener, account, getFile());
  632. }
  633. if (containerActivity.getFileUploaderBinder() != null) {
  634. containerActivity.getFileUploaderBinder().
  635. removeDatatransferProgressListener(progressListener, account, getFile());
  636. }
  637. }
  638. }
  639. private void showEmptyContent() {
  640. if (emptyContentContainer != null) {
  641. emptyContentContainer.setVisibility(View.VISIBLE);
  642. detailContainer.setVisibility(View.GONE);
  643. emptyContentHeadline.setText(R.string.file_details_no_content);
  644. emptyContentProgressBar.setVisibility(View.GONE);
  645. emptyContentIcon.setImageResource(R.drawable.ic_list_empty_error);
  646. emptyContentIcon.setVisibility(View.VISIBLE);
  647. }
  648. }
  649. /**
  650. * Helper class responsible for updating the progress bar shown for file downloading.
  651. */
  652. private class ProgressListener implements OnDatatransferProgressListener {
  653. private int lastPercent;
  654. private WeakReference<ProgressBar> progressBarReference;
  655. ProgressListener(ProgressBar progressBar) {
  656. progressBarReference = new WeakReference<>(progressBar);
  657. }
  658. @Override
  659. public void onTransferProgress(long progressRate, long totalTransferredSoFar,
  660. long totalToTransfer, String filename) {
  661. int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
  662. if (percent != lastPercent) {
  663. ProgressBar pb = progressBarReference.get();
  664. if (pb != null) {
  665. pb.setProgress(percent);
  666. pb.postInvalidate();
  667. }
  668. }
  669. lastPercent = percent;
  670. }
  671. }
  672. }