FileDetailFragment.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. activity.showProgressBar(false);
  210. ThemeUtils.setStatusBarColor(activity, activity.getResources().getColor(R.color.background_color_inverse));
  211. if (activity.getSupportActionBar() != null) {
  212. activity.getSupportActionBar().setTitle(null);
  213. activity.getSupportActionBar().setBackgroundDrawable(null);
  214. }
  215. }
  216. }
  217. @Override
  218. public void onActivityCreated(Bundle savedInstanceState) {
  219. super.onActivityCreated(savedInstanceState);
  220. setHasOptionsMenu(true);
  221. }
  222. @Override
  223. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  224. Bundle arguments = getArguments();
  225. if (arguments == null) {
  226. throw new IllegalArgumentException("Arguments may not be null");
  227. }
  228. setFile(arguments.getParcelable(ARG_FILE));
  229. account = arguments.getParcelable(ARG_ACCOUNT);
  230. activeTab = arguments.getInt(ARG_ACTIVE_TAB, 0);
  231. if (savedInstanceState != null) {
  232. setFile(savedInstanceState.getParcelable(FileActivity.EXTRA_FILE));
  233. account = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
  234. }
  235. view = inflater.inflate(layout, null);
  236. unbinder = ButterKnife.bind(this, view);
  237. if (getFile() == null || account == null) {
  238. showEmptyContent();
  239. } else {
  240. emptyContentContainer.setVisibility(View.GONE);
  241. }
  242. return view;
  243. }
  244. @Override
  245. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  246. if (getFile() != null && account != null) {
  247. ThemeUtils.colorHorizontalProgressBar(progressBar, ThemeUtils.primaryAccentColor(getContext()));
  248. progressListener = new ProgressListener(progressBar);
  249. cancelButton.setOnClickListener(this);
  250. favoriteIcon.setOnClickListener(this);
  251. overflowMenu.setOnClickListener(this);
  252. fileModifiedTimestamp.setOnClickListener(this);
  253. updateFileDetails(false, false);
  254. }
  255. }
  256. private void onOverflowIconClicked(View view) {
  257. PopupMenu popup = new PopupMenu(getActivity(), view);
  258. popup.inflate(R.menu.fragment_file_detail);
  259. prepareOptionsMenu(popup.getMenu());
  260. popup.setOnMenuItemClickListener(this::optionsItemSelected);
  261. popup.show();
  262. }
  263. private void setupViewPager() {
  264. tabLayout.removeAllTabs();
  265. tabLayout.addTab(tabLayout.newTab().setText(R.string.drawer_item_activities));
  266. tabLayout.addTab(tabLayout.newTab().setText(R.string.share_dialog_title));
  267. tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
  268. tabLayout.setSelectedTabIndicatorColor(ThemeUtils.primaryAccentColor(getContext()));
  269. final FileDetailTabAdapter adapter = new FileDetailTabAdapter(getFragmentManager(), getFile(), account);
  270. viewPager.setAdapter(adapter);
  271. viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout) {
  272. @Override
  273. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  274. if (activeTab == 0) {
  275. getFileDetailActivitiesFragment().markCommentsAsRead();
  276. }
  277. super.onPageScrolled(position, positionOffset, positionOffsetPixels);
  278. }
  279. });
  280. tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
  281. @Override
  282. public void onTabSelected(TabLayout.Tab tab) {
  283. viewPager.setCurrentItem(tab.getPosition());
  284. if (tab.getPosition() == 0) {
  285. FileDetailActivitiesFragment fragment = getFileDetailActivitiesFragment();
  286. if (fragment != null) {
  287. fragment.markCommentsAsRead();
  288. }
  289. }
  290. }
  291. @Override
  292. public void onTabUnselected(TabLayout.Tab tab) {
  293. // unused at the moment
  294. }
  295. @Override
  296. public void onTabReselected(TabLayout.Tab tab) {
  297. // unused at the moment
  298. }
  299. });
  300. tabLayout.getTabAt(activeTab).select();
  301. }
  302. @Override
  303. public void onSaveInstanceState(@NonNull Bundle outState) {
  304. super.onSaveInstanceState(outState);
  305. outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
  306. outState.putParcelable(FileActivity.EXTRA_ACCOUNT, account);
  307. }
  308. @Override
  309. public void onStart() {
  310. super.onStart();
  311. listenForTransferProgress();
  312. }
  313. @Override
  314. public void onStop() {
  315. leaveTransferProgress();
  316. if(activity != null) {
  317. activity.setupToolbar();
  318. activity.setPreviewImageVisibility(View.GONE);
  319. activity.showProgressBar(true);
  320. }
  321. super.onStop();
  322. }
  323. @Override
  324. public void onAttach(Context context) {
  325. super.onAttach(context);
  326. if (context instanceof ToolbarActivity) {
  327. activity = (ToolbarActivity) context;
  328. } else {
  329. activity = null;
  330. }
  331. }
  332. @Override
  333. public void onDestroy() {
  334. super.onDestroy();
  335. unbinder.unbind();
  336. }
  337. @Override
  338. public View getView() {
  339. return super.getView() == null ? view : super.getView();
  340. }
  341. @Override
  342. public void onPrepareOptionsMenu(Menu menu) {
  343. super.onPrepareOptionsMenu(menu);
  344. FileMenuFilter.hideAll(menu);
  345. }
  346. private void prepareOptionsMenu(Menu menu) {
  347. if (containerActivity.getStorageManager() != null) {
  348. Account currentAccount = containerActivity.getStorageManager().getAccount();
  349. FileMenuFilter mf = new FileMenuFilter(
  350. getFile(),
  351. currentAccount,
  352. containerActivity,
  353. getActivity(),
  354. false,
  355. deviceInfo,
  356. accountManager.getUser()
  357. );
  358. mf.filter(menu,
  359. true,
  360. accountManager.isMediaStreamingSupported(currentAccount));
  361. }
  362. if (getFile().isFolder()) {
  363. FileMenuFilter.hideMenuItems(menu.findItem(R.id.action_send_file));
  364. FileMenuFilter.hideMenuItems(menu.findItem(R.id.action_sync_file));
  365. }
  366. // dual pane restrictions
  367. if (!getResources().getBoolean(R.bool.large_land_layout)){
  368. FileMenuFilter.hideMenuItems(menu.findItem(R.id.action_sync_account));
  369. }
  370. }
  371. private boolean optionsItemSelected(MenuItem item) {
  372. switch (item.getItemId()) {
  373. case R.id.action_send_file: {
  374. containerActivity.getFileOperationsHelper().sendShareFile(getFile(), true);
  375. return true;
  376. }
  377. case R.id.action_open_file_with: {
  378. containerActivity.getFileOperationsHelper().openFile(getFile());
  379. return true;
  380. }
  381. case R.id.action_remove_file: {
  382. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(getFile());
  383. dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
  384. return true;
  385. }
  386. case R.id.action_rename_file: {
  387. RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
  388. dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
  389. return true;
  390. }
  391. case R.id.action_cancel_sync: {
  392. ((FileDisplayActivity) containerActivity).cancelTransference(getFile());
  393. return true;
  394. }
  395. case R.id.action_download_file:
  396. case R.id.action_sync_file: {
  397. containerActivity.getFileOperationsHelper().syncFile(getFile());
  398. return true;
  399. }
  400. case R.id.action_set_as_wallpaper: {
  401. containerActivity.getFileOperationsHelper().setPictureAs(getFile(), getView());
  402. return true;
  403. }
  404. case R.id.action_encrypted: {
  405. // TODO implement or remove
  406. return true;
  407. }
  408. case R.id.action_unset_encrypted: {
  409. // TODO implement or remove
  410. return true;
  411. }
  412. default:
  413. return super.onOptionsItemSelected(item);
  414. }
  415. }
  416. @Override
  417. public void onClick(View v) {
  418. switch (v.getId()) {
  419. case R.id.cancelBtn: {
  420. ((FileDisplayActivity) containerActivity).cancelTransference(getFile());
  421. break;
  422. }
  423. case R.id.favorite: {
  424. if (getFile().isFavorite()) {
  425. containerActivity.getFileOperationsHelper().toggleFavoriteFile(getFile(), false);
  426. } else {
  427. containerActivity.getFileOperationsHelper().toggleFavoriteFile(getFile(), true);
  428. }
  429. setFavoriteIconStatus(!getFile().isFavorite());
  430. break;
  431. }
  432. case R.id.overflow_menu: {
  433. onOverflowIconClicked(v);
  434. break;
  435. }
  436. case R.id.last_modification_timestamp: {
  437. boolean showDetailedTimestamp = !preferences.isShowDetailedTimestampEnabled();
  438. preferences.setShowDetailedTimestampEnabled(showDetailedTimestamp);
  439. setFileModificationTimestamp(getFile(), showDetailedTimestamp);
  440. }
  441. default:
  442. Log_OC.e(TAG, "Incorrect view clicked!");
  443. break;
  444. }
  445. }
  446. /**
  447. * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
  448. *
  449. * @return True when the fragment was created with the empty layout.
  450. */
  451. public boolean isEmpty() {
  452. return layout == R.layout.empty_list || getFile() == null || account == null;
  453. }
  454. /**
  455. * Use this method to signal this Activity that it shall update its view.
  456. *
  457. * @param file : An {@link OCFile}
  458. */
  459. public void updateFileDetails(OCFile file, Account ocAccount) {
  460. setFile(file);
  461. account = ocAccount;
  462. updateFileDetails(false, false);
  463. }
  464. /**
  465. * Updates the view with all relevant details about that file.
  466. *
  467. * TODO Remove parameter when the transferring state of files is kept in database.
  468. *
  469. * @param transferring Flag signaling if the file should be considered as downloading or uploading,
  470. * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
  471. * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
  472. * @param refresh If 'true', try to refresh the whole file from the database
  473. */
  474. public void updateFileDetails(boolean transferring, boolean refresh) {
  475. if (readyToShow()) {
  476. FileDataStorageManager storageManager = containerActivity.getStorageManager();
  477. if (storageManager == null) {
  478. return;
  479. }
  480. if (refresh) {
  481. setFile(storageManager.getFileByPath(getFile().getRemotePath()));
  482. }
  483. OCFile file = getFile();
  484. // set file details
  485. if (MimeTypeUtil.isImage(file)) {
  486. fileName.setText(file.getFileName());
  487. } else {
  488. fileName.setVisibility(View.GONE);
  489. }
  490. fileSize.setText(DisplayUtils.bytesToHumanReadable(file.getFileLength()));
  491. boolean showDetailedTimestamp = preferences.isShowDetailedTimestampEnabled();
  492. setFileModificationTimestamp(file, showDetailedTimestamp);
  493. setFilePreview(file);
  494. setFavoriteIconStatus(file.isFavorite());
  495. // configure UI for depending upon local state of the file
  496. FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
  497. FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
  498. if (transferring
  499. || (downloaderBinder != null && downloaderBinder.isDownloading(account, file))
  500. || (uploaderBinder != null && uploaderBinder.isUploading(account, file))) {
  501. setButtonsForTransferring();
  502. } else if (file.isDown()) {
  503. setButtonsForDown();
  504. } else {
  505. // TODO load default preview image; when the local file is removed, the preview
  506. // remains there
  507. setButtonsForRemote();
  508. }
  509. }
  510. setupViewPager();
  511. getView().invalidate();
  512. }
  513. private void setFileModificationTimestamp(OCFile file, boolean showDetailedTimestamp) {
  514. if (showDetailedTimestamp) {
  515. fileModifiedTimestamp.setText(DisplayUtils.unixTimeToHumanReadable(file.getModificationTimestamp()));
  516. } else {
  517. fileModifiedTimestamp.setText(DisplayUtils.getRelativeTimestamp(getContext(),
  518. file.getModificationTimestamp()));
  519. }
  520. }
  521. private void setFavoriteIconStatus(boolean isFavorite) {
  522. if (isFavorite) {
  523. favoriteIcon.setImageDrawable(getResources().getDrawable(R.drawable.ic_star));
  524. } else {
  525. favoriteIcon.setImageDrawable(getResources().getDrawable(R.drawable.ic_star_outline));
  526. }
  527. }
  528. /**
  529. * Checks if the fragment is ready to show details of a OCFile
  530. *
  531. * @return 'True' when the fragment is ready to show details of a file
  532. */
  533. private boolean readyToShow() {
  534. return getFile() != null && account != null && layout == R.layout.file_details_fragment;
  535. }
  536. /**
  537. * Updates the file preview if possible
  538. *
  539. * @param file a {@link OCFile} to be previewed
  540. */
  541. private void setFilePreview(OCFile file) {
  542. Bitmap resizedImage;
  543. if (activity != null && MimeTypeUtil.isImage(file)) {
  544. String tagId = String.valueOf(ThumbnailsCacheManager.PREFIX_RESIZED_IMAGE + getFile().getRemoteId());
  545. resizedImage = ThumbnailsCacheManager.getBitmapFromDiskCache(tagId);
  546. if (resizedImage != null && !file.isUpdateThumbnailNeeded()) {
  547. activity.setPreviewImageBitmap(resizedImage);
  548. activatePreviewImage();
  549. previewLoaded = true;
  550. } else {
  551. // show thumbnail while loading resized image
  552. Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
  553. String.valueOf(ThumbnailsCacheManager.PREFIX_THUMBNAIL + getFile().getRemoteId()));
  554. if (thumbnail != null) {
  555. activity.setPreviewImageBitmap(thumbnail);
  556. } else {
  557. thumbnail = ThumbnailsCacheManager.mDefaultImg;
  558. }
  559. // generate new resized image
  560. if (ThumbnailsCacheManager.cancelPotentialThumbnailWork(getFile(), activity.getPreviewImageView()) &&
  561. containerActivity.getStorageManager() != null) {
  562. final ThumbnailsCacheManager.ResizedImageGenerationTask task =
  563. new ThumbnailsCacheManager.ResizedImageGenerationTask(this,
  564. activity.getPreviewImageView(),
  565. containerActivity.getStorageManager(),
  566. connectivityService,
  567. containerActivity.getStorageManager().getAccount());
  568. if (resizedImage == null) {
  569. resizedImage = thumbnail;
  570. }
  571. final ThumbnailsCacheManager.AsyncResizedImageDrawable asyncDrawable =
  572. new ThumbnailsCacheManager.AsyncResizedImageDrawable(
  573. MainApp.getAppContext().getResources(),
  574. resizedImage,
  575. task
  576. );
  577. activity.setPreviewImageDrawable(asyncDrawable);
  578. activatePreviewImage();
  579. previewLoaded = true;
  580. task.execute(getFile());
  581. }
  582. }
  583. }
  584. }
  585. /**
  586. * Enables or disables buttons for a file being downloaded
  587. */
  588. private void setButtonsForTransferring() {
  589. if (!isEmpty()) {
  590. // show the progress bar for the transfer
  591. downloadProgressContainer.setVisibility(View.VISIBLE);
  592. progressText.setVisibility(View.VISIBLE);
  593. FileDownloaderBinder downloaderBinder = containerActivity.getFileDownloaderBinder();
  594. FileUploaderBinder uploaderBinder = containerActivity.getFileUploaderBinder();
  595. //if (getFile().isDownloading()) {
  596. if (downloaderBinder != null && downloaderBinder.isDownloading(account, getFile())) {
  597. progressText.setText(R.string.downloader_download_in_progress_ticker);
  598. }
  599. else {
  600. if (uploaderBinder != null && uploaderBinder.isUploading(account, getFile())) {
  601. progressText.setText(R.string.uploader_upload_in_progress_ticker);
  602. }
  603. }
  604. }
  605. }
  606. /**
  607. * Enables or disables buttons for a file locally available
  608. */
  609. private void setButtonsForDown() {
  610. if (!isEmpty()) {
  611. // hides the progress bar
  612. downloadProgressContainer.setVisibility(View.GONE);
  613. }
  614. }
  615. /**
  616. * Enables or disables buttons for a file not locally available
  617. */
  618. private void setButtonsForRemote() {
  619. if (!isEmpty()) {
  620. // hides the progress bar
  621. downloadProgressContainer.setVisibility(View.GONE);
  622. }
  623. }
  624. public void listenForTransferProgress() {
  625. if (progressListener != null) {
  626. if (containerActivity.getFileDownloaderBinder() != null) {
  627. containerActivity.getFileDownloaderBinder().
  628. addDatatransferProgressListener(progressListener, account, getFile());
  629. }
  630. if (containerActivity.getFileUploaderBinder() != null) {
  631. containerActivity.getFileUploaderBinder().
  632. addDatatransferProgressListener(progressListener, account, getFile());
  633. }
  634. } else {
  635. Log_OC.d(TAG, "progressListener == null");
  636. }
  637. }
  638. private void leaveTransferProgress() {
  639. if (progressListener != null) {
  640. if (containerActivity.getFileDownloaderBinder() != null) {
  641. containerActivity.getFileDownloaderBinder().
  642. removeDatatransferProgressListener(progressListener, account, getFile());
  643. }
  644. if (containerActivity.getFileUploaderBinder() != null) {
  645. containerActivity.getFileUploaderBinder().
  646. removeDatatransferProgressListener(progressListener, account, getFile());
  647. }
  648. }
  649. }
  650. private void showEmptyContent() {
  651. if (emptyContentContainer != null) {
  652. emptyContentContainer.setVisibility(View.VISIBLE);
  653. detailContainer.setVisibility(View.GONE);
  654. emptyContentHeadline.setText(R.string.file_details_no_content);
  655. emptyContentProgressBar.setVisibility(View.GONE);
  656. emptyContentIcon.setImageResource(R.drawable.ic_list_empty_error);
  657. emptyContentIcon.setVisibility(View.VISIBLE);
  658. }
  659. }
  660. /**
  661. * Helper class responsible for updating the progress bar shown for file downloading.
  662. */
  663. private class ProgressListener implements OnDatatransferProgressListener {
  664. private int lastPercent;
  665. private WeakReference<ProgressBar> progressBarReference;
  666. ProgressListener(ProgressBar progressBar) {
  667. progressBarReference = new WeakReference<>(progressBar);
  668. }
  669. @Override
  670. public void onTransferProgress(long progressRate, long totalTransferredSoFar,
  671. long totalToTransfer, String filename) {
  672. int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
  673. if (percent != lastPercent) {
  674. ProgressBar pb = progressBarReference.get();
  675. if (pb != null) {
  676. pb.setProgress(percent);
  677. pb.postInvalidate();
  678. }
  679. }
  680. lastPercent = percent;
  681. }
  682. }
  683. }