FileDetailFragment.java 27 KB

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