FileDetailFragment.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author Bartek Przybylski
  5. * @author David A. Velasco
  6. * Copyright (C) 2011 Bartek Przybylski
  7. * Copyright (C) 2016 ownCloud Inc.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. package com.owncloud.android.ui.fragment;
  23. import android.accounts.Account;
  24. import android.graphics.Bitmap;
  25. import android.os.Bundle;
  26. import android.support.design.widget.Snackbar;
  27. import android.support.design.widget.TabLayout;
  28. import android.support.v4.view.ViewPager;
  29. import android.view.LayoutInflater;
  30. import android.view.Menu;
  31. import android.view.MenuItem;
  32. import android.view.View;
  33. import android.view.View.OnClickListener;
  34. import android.view.ViewGroup;
  35. import android.widget.ImageView;
  36. import android.widget.ListAdapter;
  37. import android.widget.ListView;
  38. import android.widget.PopupMenu;
  39. import android.widget.ProgressBar;
  40. import android.widget.TextView;
  41. import com.owncloud.android.MainApp;
  42. import com.owncloud.android.R;
  43. import com.owncloud.android.datamodel.FileDataStorageManager;
  44. import com.owncloud.android.datamodel.OCFile;
  45. import com.owncloud.android.datamodel.ThumbnailsCacheManager;
  46. import com.owncloud.android.files.FileMenuFilter;
  47. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  48. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  49. import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
  50. import com.owncloud.android.lib.common.utils.Log_OC;
  51. import com.owncloud.android.lib.resources.shares.OCShare;
  52. import com.owncloud.android.ui.activity.FileActivity;
  53. import com.owncloud.android.ui.activity.FileDisplayActivity;
  54. import com.owncloud.android.ui.adapter.FileDetailTabAdapter;
  55. import com.owncloud.android.ui.adapter.UserListAdapter;
  56. import com.owncloud.android.ui.dialog.RemoveFilesDialogFragment;
  57. import com.owncloud.android.ui.dialog.RenameFileDialogFragment;
  58. import com.owncloud.android.utils.AnalyticsUtils;
  59. import com.owncloud.android.utils.DisplayUtils;
  60. import com.owncloud.android.utils.MimeTypeUtil;
  61. import com.owncloud.android.utils.ThemeUtils;
  62. import java.lang.ref.WeakReference;
  63. import java.util.ArrayList;
  64. /**
  65. * This Fragment is used to display the details about a file.
  66. */
  67. public class FileDetailFragment extends FileFragment implements OnClickListener {
  68. private int mLayout;
  69. private View mView;
  70. private Account mAccount;
  71. public ProgressListener mProgressListener;
  72. // to show share with users/groups info
  73. private ArrayList<OCShare> mShares;
  74. private static final String TAG = FileDetailFragment.class.getSimpleName();
  75. public static final String FTAG_CONFIRMATION = "REMOVE_CONFIRMATION_FRAGMENT";
  76. public static final String FTAG_RENAME_FILE = "RENAME_FILE_FRAGMENT";
  77. private static final String ARG_FILE = "FILE";
  78. private static final String ARG_ACCOUNT = "ACCOUNT";
  79. private static final String SCREEN_NAME = "File details";
  80. /**
  81. * Public factory method to create new FileDetailFragment instances.
  82. *
  83. * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
  84. *
  85. * @param fileToDetail An {@link OCFile} to show in the fragment
  86. * @param account An ownCloud account; needed to start downloads
  87. * @return New fragment with arguments set
  88. */
  89. public static FileDetailFragment newInstance(OCFile fileToDetail, Account account) {
  90. FileDetailFragment frag = new FileDetailFragment();
  91. Bundle args = new Bundle();
  92. args.putParcelable(ARG_FILE, fileToDetail);
  93. args.putParcelable(ARG_ACCOUNT, account);
  94. frag.setArguments(args);
  95. return frag;
  96. }
  97. /**
  98. * Creates an empty details fragment.
  99. *
  100. * It's necessary to keep a public constructor without parameters; the system uses it when tries
  101. * to reinstantiate a fragment automatically.
  102. */
  103. public FileDetailFragment() {
  104. super();
  105. mAccount = null;
  106. mLayout = R.layout.file_details_empty;
  107. mProgressListener = null;
  108. }
  109. @Override
  110. public void onResume() {
  111. super.onResume();
  112. if (getActivity() != null) {
  113. AnalyticsUtils.setCurrentScreenName(getActivity(), SCREEN_NAME, TAG);
  114. }
  115. }
  116. @Override
  117. public void onActivityCreated(Bundle savedInstanceState) {
  118. super.onActivityCreated(savedInstanceState);
  119. setHasOptionsMenu(true);
  120. }
  121. @Override
  122. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  123. Bundle savedInstanceState) {
  124. setFile(getArguments().getParcelable(ARG_FILE));
  125. mAccount = getArguments().getParcelable(ARG_ACCOUNT);
  126. if (savedInstanceState != null) {
  127. setFile(savedInstanceState.getParcelable(FileActivity.EXTRA_FILE));
  128. mAccount = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
  129. }
  130. if (getFile() != null && mAccount != null) {
  131. mLayout = R.layout.file_details_fragment;
  132. }
  133. mView = inflater.inflate(mLayout, null);
  134. if (mLayout == R.layout.file_details_fragment) {
  135. ProgressBar progressBar = mView.findViewById(R.id.fdProgressBar);
  136. ThemeUtils.colorHorizontalProgressBar(progressBar, ThemeUtils.primaryAccentColor(getContext()));
  137. mProgressListener = new ProgressListener(progressBar);
  138. mView.findViewById(R.id.fdCancelBtn).setOnClickListener(this);
  139. mView.findViewById(R.id.fdFavorite).setOnClickListener(this);
  140. mView.findViewById(R.id.overflow_menu).setOnClickListener(this);
  141. }
  142. updateFileDetails(false, false);
  143. return mView;
  144. }
  145. public void onOverflowIconClicked(View view) {
  146. PopupMenu popup = new PopupMenu(getActivity(), view);
  147. popup.inflate(R.menu.file_actions_menu);
  148. prepareOptionsMenu(popup.getMenu());
  149. popup.setOnMenuItemClickListener(item -> {
  150. return optionsItemSelected(item);
  151. });
  152. popup.show();
  153. }
  154. private void setupViewPager(View view) {
  155. TabLayout tabLayout = view.findViewById(R.id.tab_layout);
  156. tabLayout.removeAllTabs();
  157. tabLayout.addTab(tabLayout.newTab().setText(R.string.drawer_item_activities));
  158. tabLayout.addTab(tabLayout.newTab().setText(R.string.share_dialog_title));
  159. tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
  160. final ViewPager viewPager = view.findViewById(R.id.pager);
  161. final FileDetailTabAdapter adapter = new FileDetailTabAdapter
  162. (getFragmentManager(), getFile(),mAccount);
  163. viewPager.setAdapter(adapter);
  164. viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
  165. tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
  166. @Override
  167. public void onTabSelected(TabLayout.Tab tab) {
  168. viewPager.setCurrentItem(tab.getPosition());
  169. }
  170. @Override
  171. public void onTabUnselected(TabLayout.Tab tab) {
  172. // unused at the moment
  173. }
  174. @Override
  175. public void onTabReselected(TabLayout.Tab tab) {
  176. // unused at the moment
  177. }
  178. });
  179. }
  180. @Override
  181. public void onSaveInstanceState(Bundle outState) {
  182. super.onSaveInstanceState(outState);
  183. outState.putParcelable(FileActivity.EXTRA_FILE, getFile());
  184. outState.putParcelable(FileActivity.EXTRA_ACCOUNT, mAccount);
  185. }
  186. @Override
  187. public void onStart() {
  188. super.onStart();
  189. listenForTransferProgress();
  190. }
  191. @Override
  192. public void onStop() {
  193. leaveTransferProgress();
  194. super.onStop();
  195. }
  196. @Override
  197. public View getView() {
  198. return super.getView() == null ? mView : super.getView();
  199. }
  200. @Override
  201. public void onPrepareOptionsMenu(Menu menu) {
  202. super.onPrepareOptionsMenu(menu);
  203. MenuItem item;
  204. for (int i = 0; i < menu.size(); i++) {
  205. item = menu.getItem(i);
  206. item.setVisible(false);
  207. item.setEnabled(false);
  208. }
  209. }
  210. private void prepareOptionsMenu(Menu menu) {
  211. super.onPrepareOptionsMenu(menu);
  212. if (mContainerActivity.getStorageManager() != null) {
  213. FileMenuFilter mf = new FileMenuFilter(
  214. getFile(),
  215. mContainerActivity.getStorageManager().getAccount(),
  216. mContainerActivity,
  217. getActivity(),
  218. false
  219. );
  220. mf.filter(menu, true);
  221. }
  222. // restriction for this fragment
  223. hideMenuItem(menu.findItem(R.id.action_see_details));
  224. hideMenuItem(menu.findItem(R.id.action_select_all));
  225. hideMenuItem(menu.findItem(R.id.action_move));
  226. hideMenuItem(menu.findItem(R.id.action_copy));
  227. hideMenuItem(menu.findItem(R.id.action_favorite));
  228. hideMenuItem(menu.findItem(R.id.action_unset_favorite));
  229. hideMenuItem(menu.findItem(R.id.action_search));
  230. // dual pane restrictions
  231. if (!getResources().getBoolean(R.bool.large_land_layout)){
  232. hideMenuItem(menu.findItem(R.id.action_switch_view));
  233. hideMenuItem(menu.findItem(R.id.action_sync_account));
  234. hideMenuItem(menu.findItem(R.id.action_sort));
  235. }
  236. // share restrictions
  237. if (getFile().isSharedWithMe() && !getFile().canReshare()) {
  238. hideMenuItem(menu.findItem(R.id.action_send_share_file));
  239. }
  240. }
  241. private void hideMenuItem(MenuItem item) {
  242. if (item != null) {
  243. item.setVisible(false);
  244. item.setEnabled(false);
  245. }
  246. }
  247. public boolean optionsItemSelected(MenuItem item) {
  248. switch (item.getItemId()) {
  249. case R.id.action_send_share_file: {
  250. if(getFile().isSharedWithMe() && !getFile().canReshare()){
  251. Snackbar.make(getView(),
  252. R.string.resharing_is_not_allowed,
  253. Snackbar.LENGTH_LONG
  254. )
  255. .show();
  256. } else {
  257. mContainerActivity.getFileOperationsHelper().sendShareFile(getFile());
  258. }
  259. return true;
  260. }
  261. case R.id.action_open_file_with: {
  262. mContainerActivity.getFileOperationsHelper().openFile(getFile());
  263. return true;
  264. }
  265. case R.id.action_remove_file: {
  266. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(getFile());
  267. dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
  268. return true;
  269. }
  270. case R.id.action_rename_file: {
  271. RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
  272. dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
  273. return true;
  274. }
  275. case R.id.action_cancel_sync: {
  276. ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
  277. return true;
  278. }
  279. case R.id.action_download_file:
  280. case R.id.action_sync_file: {
  281. mContainerActivity.getFileOperationsHelper().syncFile(getFile());
  282. return true;
  283. }
  284. default:
  285. return super.onOptionsItemSelected(item);
  286. }
  287. }
  288. @Override
  289. public void onClick(View v) {
  290. switch (v.getId()) {
  291. case R.id.fdCancelBtn: {
  292. ((FileDisplayActivity) mContainerActivity).cancelTransference(getFile());
  293. break;
  294. }
  295. case R.id.fdFavorite: {
  296. if (getFile().isAvailableOffline()) {
  297. ((ImageView)getView().findViewById(R.id.fdFavorite)).
  298. setImageDrawable(getResources()
  299. .getDrawable(R.drawable.ic_star_outline));
  300. } else {
  301. ((ImageView)getView().findViewById(R.id.fdFavorite))
  302. .setImageDrawable(getResources()
  303. .getDrawable(R.drawable.ic_star));
  304. }
  305. mContainerActivity.getFileOperationsHelper()
  306. .toggleOfflineFile(getFile(), !getFile().isAvailableOffline());
  307. break;
  308. }
  309. case R.id.overflow_menu: {
  310. onOverflowIconClicked(v);
  311. }
  312. default:
  313. Log_OC.e(TAG, "Incorrect view clicked!");
  314. break;
  315. }
  316. }
  317. /**
  318. * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
  319. *
  320. * @return True when the fragment was created with the empty layout.
  321. */
  322. public boolean isEmpty() {
  323. return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
  324. }
  325. /**
  326. * Use this method to signal this Activity that it shall update its view.
  327. *
  328. * @param file : An {@link OCFile}
  329. */
  330. public void updateFileDetails(OCFile file, Account ocAccount) {
  331. setFile(file);
  332. mAccount = ocAccount;
  333. updateFileDetails(false, false);
  334. }
  335. /**
  336. * Updates the view with all relevant details about that file.
  337. * <p/>
  338. * TODO Remove parameter when the transferring state of files is kept in database.
  339. *
  340. * @param transferring Flag signaling if the file should be considered as downloading or uploading,
  341. * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
  342. * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
  343. * @param refresh If 'true', try to refresh the whole file from the database
  344. */
  345. public void updateFileDetails(boolean transferring, boolean refresh) {
  346. if (readyToShow()) {
  347. FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
  348. if (storageManager == null) {
  349. return;
  350. }
  351. if (refresh) {
  352. setFile(storageManager.getFileByPath(getFile().getRemotePath()));
  353. }
  354. OCFile file = getFile();
  355. // set file details
  356. setFilename(file.getFileName());
  357. setFiletype(file);
  358. setFilesize(file.getFileLength());
  359. setTimeModified(file.getModificationTimestamp());
  360. if (file.isAvailableOffline()) {
  361. ((ImageView)getView().findViewById(R.id.fdFavorite)).setImageDrawable(getResources().getDrawable(R.drawable.ic_star));
  362. } else {
  363. ((ImageView)getView().findViewById(R.id.fdFavorite)).setImageDrawable(getResources().getDrawable(R.drawable.ic_star_outline));
  364. }
  365. // configure UI for depending upon local state of the file
  366. FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
  367. FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
  368. if (transferring ||
  369. (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) ||
  370. (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))
  371. ) {
  372. setButtonsForTransferring();
  373. } else if (file.isDown()) {
  374. setButtonsForDown();
  375. } else {
  376. // TODO load default preview image; when the local file is removed, the preview
  377. // remains there
  378. setButtonsForRemote();
  379. }
  380. }
  381. setupViewPager(getView());
  382. getView().invalidate();
  383. }
  384. /**
  385. * Checks if the fragment is ready to show details of a OCFile
  386. *
  387. * @return 'True' when the fragment is ready to show details of a file
  388. */
  389. private boolean readyToShow() {
  390. return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
  391. }
  392. /**
  393. * Updates the filename in view
  394. *
  395. * @param filename to set
  396. */
  397. private void setFilename(String filename) {
  398. TextView tv = getView().findViewById(R.id.fdFilename);
  399. if (tv != null) {
  400. tv.setText(filename);
  401. }
  402. }
  403. /**
  404. * Updates the MIME type in view
  405. * @param file : An {@link OCFile}
  406. */
  407. private void setFiletype(OCFile file) {
  408. ImageView iv = getView().findViewById(R.id.fdIcon);
  409. if (iv != null) {
  410. iv.setTag(file.getFileId());
  411. // Name of the file, to deduce the icon to use in case the MIME type is not precise enough
  412. iv.setImageDrawable(MimeTypeUtil.getFileTypeIcon(file.getMimetype(), file.getFileName(), mAccount,
  413. getContext()));
  414. Bitmap thumbnail;
  415. if (MimeTypeUtil.isImage(file)) {
  416. String tagId = String.valueOf(file.getRemoteId());
  417. thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(tagId);
  418. if (thumbnail != null && !file.needsUpdateThumbnail()) {
  419. iv.setImageBitmap(thumbnail);
  420. } else {
  421. // generate new Thumbnail
  422. if (ThumbnailsCacheManager.cancelPotentialThumbnailWork(file, iv)) {
  423. final ThumbnailsCacheManager.ThumbnailGenerationTask task =
  424. new ThumbnailsCacheManager.ThumbnailGenerationTask(
  425. iv, mContainerActivity.getStorageManager(), mAccount
  426. );
  427. if (thumbnail == null) {
  428. thumbnail = ThumbnailsCacheManager.mDefaultImg;
  429. }
  430. final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable =
  431. new ThumbnailsCacheManager.AsyncThumbnailDrawable(
  432. MainApp.getAppContext().getResources(),
  433. thumbnail,
  434. task
  435. );
  436. iv.setImageDrawable(asyncDrawable);
  437. task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, file.getRemoteId()));
  438. }
  439. }
  440. } else {
  441. iv.setImageDrawable(MimeTypeUtil.getFileTypeIcon(file.getMimetype(), file.getFileName(), mAccount,
  442. getContext()));
  443. }
  444. }
  445. }
  446. /**
  447. * Updates the file size in view
  448. *
  449. * @param fileSize in bytes to set
  450. */
  451. private void setFilesize(long fileSize) {
  452. TextView tv = getView().findViewById(R.id.fdSize);
  453. if (tv != null) {
  454. tv.setText(DisplayUtils.bytesToHumanReadable(fileSize));
  455. }
  456. }
  457. /**
  458. * Updates the time that the file was last modified
  459. *
  460. * @param milliseconds Unix time to set
  461. */
  462. private void setTimeModified(long milliseconds) {
  463. TextView tv = getView().findViewById(R.id.fdModified);
  464. if (tv != null) {
  465. tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
  466. }
  467. }
  468. /**
  469. * Updates Share by link data
  470. *
  471. * @param isShareByLink flag is share by link is enable
  472. */
  473. private void setShareByLinkInfo(boolean isShareByLink) {
  474. TextView tv = getView().findViewById(R.id.fdSharebyLink);
  475. if (tv != null) {
  476. tv.setText(isShareByLink ? R.string.filedetails_share_link_enable :
  477. R.string.filedetails_share_link_disable);
  478. }
  479. ImageView linkIcon = getView().findViewById(R.id.fdShareLinkIcon);
  480. if (linkIcon != null) {
  481. linkIcon.setVisibility(isShareByLink ? View.VISIBLE : View.GONE);
  482. }
  483. }
  484. /**
  485. * Update Share With data
  486. */
  487. private void setShareWithUserInfo(){
  488. // Get Users and Groups
  489. if (((FileActivity) getActivity()).getStorageManager() != null) {
  490. FileDataStorageManager fileDataStorageManager = ((FileActivity) getActivity()).getStorageManager();
  491. mShares = fileDataStorageManager.getSharesWithForAFile(
  492. getFile().getRemotePath(),mAccount.name
  493. );
  494. // Update list of users/groups
  495. updateListOfUserGroups();
  496. }
  497. }
  498. private void updateListOfUserGroups() {
  499. // Update list of users/groups
  500. // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
  501. UserListAdapter mUserGroupsAdapter = new UserListAdapter(
  502. getActivity().getApplicationContext(),
  503. R.layout.share_user_item, mShares
  504. );
  505. // Show data
  506. ListView usersList = getView().findViewById(R.id.fdshareUsersList);
  507. // No data
  508. TextView noList = getView().findViewById(R.id.fdShareNoUsers);
  509. if (mShares.size() > 0) {
  510. usersList.setVisibility(View.VISIBLE);
  511. usersList.setAdapter(mUserGroupsAdapter);
  512. noList.setVisibility(View.GONE);
  513. setListViewHeightBasedOnChildren(usersList);
  514. } else {
  515. usersList.setVisibility(View.GONE);
  516. noList.setVisibility(View.VISIBLE);
  517. }
  518. }
  519. /**
  520. * Fix scroll in listview when the parent is a ScrollView
  521. */
  522. private static void setListViewHeightBasedOnChildren(ListView listView) {
  523. ListAdapter listAdapter = listView.getAdapter();
  524. if (listAdapter == null) {
  525. return;
  526. }
  527. int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
  528. int totalHeight = 0;
  529. View view = null;
  530. for (int i = 0; i < listAdapter.getCount(); i++) {
  531. view = listAdapter.getView(i, view, listView);
  532. if (i == 0) {
  533. view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
  534. }
  535. view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
  536. totalHeight += view.getMeasuredHeight();
  537. }
  538. ViewGroup.LayoutParams params = listView.getLayoutParams();
  539. params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
  540. listView.setLayoutParams(params);
  541. listView.requestLayout();
  542. }
  543. /**
  544. * Enables or disables buttons for a file being downloaded
  545. */
  546. private void setButtonsForTransferring() {
  547. if (!isEmpty()) {
  548. // show the progress bar for the transfer
  549. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
  550. TextView progressText = getView().findViewById(R.id.fdProgressText);
  551. progressText.setVisibility(View.VISIBLE);
  552. FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
  553. FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
  554. //if (getFile().isDownloading()) {
  555. if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
  556. progressText.setText(R.string.downloader_download_in_progress_ticker);
  557. }
  558. else {
  559. if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
  560. progressText.setText(R.string.uploader_upload_in_progress_ticker);
  561. }
  562. }
  563. }
  564. }
  565. /**
  566. * Enables or disables buttons for a file locally available
  567. */
  568. private void setButtonsForDown() {
  569. if (!isEmpty()) {
  570. // hides the progress bar
  571. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
  572. TextView progressText = getView().findViewById(R.id.fdProgressText);
  573. progressText.setVisibility(View.GONE);
  574. }
  575. }
  576. /**
  577. * Enables or disables buttons for a file not locally available
  578. */
  579. private void setButtonsForRemote() {
  580. if (!isEmpty()) {
  581. // hides the progress bar
  582. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
  583. TextView progressText = getView().findViewById(R.id.fdProgressText);
  584. progressText.setVisibility(View.GONE);
  585. }
  586. }
  587. public void listenForTransferProgress() {
  588. if (mProgressListener != null) {
  589. if (mContainerActivity.getFileDownloaderBinder() != null) {
  590. mContainerActivity.getFileDownloaderBinder().
  591. addDatatransferProgressListener(mProgressListener, mAccount, getFile());
  592. }
  593. if (mContainerActivity.getFileUploaderBinder() != null) {
  594. mContainerActivity.getFileUploaderBinder().
  595. addDatatransferProgressListener(mProgressListener, mAccount, getFile());
  596. }
  597. } else {
  598. Log_OC.d(TAG, "mProgressListener == null");
  599. }
  600. }
  601. public void leaveTransferProgress() {
  602. if (mProgressListener != null) {
  603. if (mContainerActivity.getFileDownloaderBinder() != null) {
  604. mContainerActivity.getFileDownloaderBinder().
  605. removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
  606. }
  607. if (mContainerActivity.getFileUploaderBinder() != null) {
  608. mContainerActivity.getFileUploaderBinder().
  609. removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
  610. }
  611. }
  612. }
  613. /**
  614. * Helper class responsible for updating the progress bar shown for file uploading or
  615. * downloading
  616. */
  617. private class ProgressListener implements OnDatatransferProgressListener {
  618. int mLastPercent = 0;
  619. WeakReference<ProgressBar> mProgressBar = null;
  620. ProgressListener(ProgressBar progressBar) {
  621. mProgressBar = new WeakReference<ProgressBar>(progressBar);
  622. }
  623. @Override
  624. public void onTransferProgress(long progressRate, long totalTransferredSoFar,
  625. long totalToTransfer, String filename) {
  626. int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
  627. if (percent != mLastPercent) {
  628. ProgressBar pb = mProgressBar.get();
  629. if (pb != null) {
  630. pb.setProgress(percent);
  631. pb.postInvalidate();
  632. }
  633. }
  634. mLastPercent = percent;
  635. }
  636. }
  637. }