FileDetailFragment.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. if(getFile().isSharedWithMe() && !getFile().canReshare()){
  251. // additional restriction for this fragment
  252. item = menu.findItem(R.id.action_share_file);
  253. if(item != null){
  254. item.setVisible(false);
  255. item.setEnabled(false);
  256. }
  257. }
  258. }
  259. /**
  260. * {@inheritDoc}
  261. */
  262. @Override
  263. public boolean onOptionsItemSelected(MenuItem item) {
  264. switch (item.getItemId()) {
  265. case R.id.action_share_file: {
  266. mContainerActivity.getFileOperationsHelper().showShareFile(getFile());
  267. return true;
  268. }
  269. case R.id.action_open_file_with: {
  270. mContainerActivity.getFileOperationsHelper().openFile(getFile());
  271. return true;
  272. }
  273. case R.id.action_remove_file: {
  274. RemoveFilesDialogFragment dialog = RemoveFilesDialogFragment.newInstance(getFile());
  275. dialog.show(getFragmentManager(), FTAG_CONFIRMATION);
  276. return true;
  277. }
  278. case R.id.action_rename_file: {
  279. RenameFileDialogFragment dialog = RenameFileDialogFragment.newInstance(getFile());
  280. dialog.show(getFragmentManager(), FTAG_RENAME_FILE);
  281. return true;
  282. }
  283. case R.id.action_cancel_sync: {
  284. ((FileDisplayActivity)mContainerActivity).cancelTransference(getFile());
  285. return true;
  286. }
  287. case R.id.action_download_file:
  288. case R.id.action_sync_file: {
  289. mContainerActivity.getFileOperationsHelper().syncFile(getFile());
  290. return true;
  291. }
  292. case R.id.action_send_file: {
  293. // Obtain the file
  294. if (!getFile().isDown()) { // Download the file
  295. Log_OC.d(TAG, getFile().getRemotePath() + " : File must be downloaded");
  296. ((FileDisplayActivity) mContainerActivity).startDownloadForSending(getFile(),
  297. OCFileListFragment.DOWNLOAD_SEND);
  298. }
  299. else {
  300. mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
  301. }
  302. return true;
  303. }
  304. default:
  305. return super.onOptionsItemSelected(item);
  306. }
  307. }
  308. @Override
  309. public void onClick(View v) {
  310. switch (v.getId()) {
  311. case R.id.fdCancelBtn: {
  312. ((FileDisplayActivity) mContainerActivity).cancelTransference(getFile());
  313. break;
  314. }
  315. default:
  316. Log_OC.e(TAG, "Incorrect view clicked!");
  317. break;
  318. }
  319. }
  320. @Override
  321. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  322. SwitchCompat favSwitch = (SwitchCompat) getView().findViewById(R.id.fdFavorite);
  323. mContainerActivity.getFileOperationsHelper().toggleOfflineFile(getFile(), favSwitch.isChecked());
  324. }
  325. /**
  326. * Check if the fragment was created with an empty layout. An empty fragment can't show file details, must be replaced.
  327. *
  328. * @return True when the fragment was created with the empty layout.
  329. */
  330. public boolean isEmpty() {
  331. return (mLayout == R.layout.file_details_empty || getFile() == null || mAccount == null);
  332. }
  333. /**
  334. * Use this method to signal this Activity that it shall update its view.
  335. *
  336. * @param file : An {@link OCFile}
  337. */
  338. public void updateFileDetails(OCFile file, Account ocAccount) {
  339. setFile(file);
  340. mAccount = ocAccount;
  341. updateFileDetails(false, false);
  342. }
  343. /**
  344. * Updates the view with all relevant details about that file.
  345. * <p/>
  346. * TODO Remove parameter when the transferring state of files is kept in database.
  347. *
  348. * @param transferring Flag signaling if the file should be considered as downloading or uploading,
  349. * although {@link FileDownloaderBinder#isDownloading(Account, OCFile)} and
  350. * {@link FileUploaderBinder#isUploading(Account, OCFile)} return false.
  351. * @param refresh If 'true', try to refresh the whole file from the database
  352. */
  353. public void updateFileDetails(boolean transferring, boolean refresh) {
  354. if (readyToShow()) {
  355. FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
  356. if (refresh && storageManager != null) {
  357. setFile(storageManager.getFileByPath(getFile().getRemotePath()));
  358. }
  359. OCFile file = getFile();
  360. // set file details
  361. setFilename(file.getFileName());
  362. setFiletype(file);
  363. setFilesize(file.getFileLength());
  364. setTimeModified(file.getModificationTimestamp());
  365. SwitchCompat favSwitch = (SwitchCompat) getView().findViewById(R.id.fdFavorite);
  366. favSwitch.setChecked(file.isAvailableOffline());
  367. setShareByLinkInfo(file.isSharedViaLink());
  368. setShareWithUserInfo();
  369. // configure UI for depending upon local state of the file
  370. FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
  371. FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
  372. if (transferring ||
  373. (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, file)) ||
  374. (uploaderBinder != null && uploaderBinder.isUploading(mAccount, file))
  375. ) {
  376. setButtonsForTransferring();
  377. } else if (file.isDown()) {
  378. setButtonsForDown();
  379. } else {
  380. // TODO load default preview image; when the local file is removed, the preview
  381. // remains there
  382. setButtonsForRemote();
  383. }
  384. }
  385. getView().invalidate();
  386. }
  387. /**
  388. * Checks if the fragment is ready to show details of a OCFile
  389. *
  390. * @return 'True' when the fragment is ready to show details of a file
  391. */
  392. private boolean readyToShow() {
  393. return (getFile() != null && mAccount != null && mLayout == R.layout.file_details_fragment);
  394. }
  395. /**
  396. * Updates the filename in view
  397. *
  398. * @param filename to set
  399. */
  400. private void setFilename(String filename) {
  401. TextView tv = (TextView) getView().findViewById(R.id.fdFilename);
  402. if (tv != null) {
  403. tv.setText(filename);
  404. }
  405. }
  406. /**
  407. * Updates the MIME type in view
  408. * @param file : An {@link OCFile}
  409. */
  410. private void setFiletype(OCFile file) {
  411. ImageView iv = (ImageView) getView().findViewById(R.id.fdIcon);
  412. if (iv != null) {
  413. iv.setTag(file.getFileId());
  414. // Name of the file, to deduce the icon to use in case the MIME type is not precise enough
  415. iv.setImageDrawable(MimeTypeUtil.getFileTypeIcon(file.getMimetype(), file.getFileName(), mAccount));
  416. Bitmap thumbnail;
  417. if (MimeTypeUtil.isImage(file)) {
  418. String tagId = String.valueOf(file.getRemoteId());
  419. thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(tagId);
  420. if (thumbnail != null && !file.needsUpdateThumbnail()) {
  421. iv.setImageBitmap(thumbnail);
  422. } else {
  423. // generate new Thumbnail
  424. if (ThumbnailsCacheManager.cancelPotentialThumbnailWork(file, iv)) {
  425. final ThumbnailsCacheManager.ThumbnailGenerationTask task =
  426. new ThumbnailsCacheManager.ThumbnailGenerationTask(
  427. iv, mContainerActivity.getStorageManager(), mAccount
  428. );
  429. if (thumbnail == null) {
  430. thumbnail = ThumbnailsCacheManager.mDefaultImg;
  431. }
  432. final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable =
  433. new ThumbnailsCacheManager.AsyncThumbnailDrawable(
  434. MainApp.getAppContext().getResources(),
  435. thumbnail,
  436. task
  437. );
  438. iv.setImageDrawable(asyncDrawable);
  439. task.execute(file);
  440. }
  441. }
  442. } else {
  443. iv.setImageDrawable(MimeTypeUtil.getFileTypeIcon(file.getMimetype(), file.getFileName(), mAccount));
  444. }
  445. }
  446. }
  447. /**
  448. * Updates the file size in view
  449. *
  450. * @param fileSize in bytes to set
  451. */
  452. private void setFilesize(long fileSize) {
  453. TextView tv = (TextView) getView().findViewById(R.id.fdSize);
  454. if (tv != null) {
  455. tv.setText(DisplayUtils.bytesToHumanReadable(fileSize));
  456. }
  457. }
  458. /**
  459. * Updates the time that the file was last modified
  460. *
  461. * @param milliseconds Unix time to set
  462. */
  463. private void setTimeModified(long milliseconds) {
  464. TextView tv = (TextView) getView().findViewById(R.id.fdModified);
  465. if (tv != null) {
  466. tv.setText(DisplayUtils.unixTimeToHumanReadable(milliseconds));
  467. }
  468. }
  469. /**
  470. * Updates Share by link data
  471. *
  472. * @param isShareByLink flag is share by link is enable
  473. */
  474. private void setShareByLinkInfo(boolean isShareByLink) {
  475. TextView tv = (TextView) getView().findViewById(R.id.fdSharebyLink);
  476. if (tv != null) {
  477. tv.setText(isShareByLink ? R.string.filedetails_share_link_enable :
  478. R.string.filedetails_share_link_disable);
  479. }
  480. ImageView linkIcon = (ImageView) getView().findViewById(R.id.fdShareLinkIcon);
  481. if (linkIcon != null) {
  482. linkIcon.setVisibility(isShareByLink ? View.VISIBLE : View.GONE);
  483. }
  484. }
  485. /**
  486. * Update Share With data
  487. */
  488. private void setShareWithUserInfo(){
  489. // Get Users and Groups
  490. if (((FileActivity) getActivity()).getStorageManager() != null) {
  491. FileDataStorageManager fileDataStorageManager = ((FileActivity) getActivity()).getStorageManager();
  492. mShares = fileDataStorageManager.getSharesWithForAFile(
  493. getFile().getRemotePath(),mAccount.name
  494. );
  495. // Update list of users/groups
  496. updateListOfUserGroups();
  497. }
  498. }
  499. private void updateListOfUserGroups() {
  500. // Update list of users/groups
  501. // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
  502. UserListAdapter mUserGroupsAdapter = new UserListAdapter(
  503. getActivity().getApplicationContext(),
  504. R.layout.share_user_item, mShares
  505. );
  506. // Show data
  507. ListView usersList = (ListView) getView().findViewById(R.id.fdshareUsersList);
  508. // No data
  509. TextView noList = (TextView) getView().findViewById(R.id.fdShareNoUsers);
  510. if (mShares.size() > 0) {
  511. usersList.setVisibility(View.VISIBLE);
  512. usersList.setAdapter(mUserGroupsAdapter);
  513. noList.setVisibility(View.GONE);
  514. setListViewHeightBasedOnChildren(usersList);
  515. } else {
  516. usersList.setVisibility(View.GONE);
  517. noList.setVisibility(View.VISIBLE);
  518. }
  519. }
  520. /**
  521. * Fix scroll in listview when the parent is a ScrollView
  522. */
  523. private static void setListViewHeightBasedOnChildren(ListView listView) {
  524. ListAdapter listAdapter = listView.getAdapter();
  525. if (listAdapter == null) {
  526. return;
  527. }
  528. int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
  529. int totalHeight = 0;
  530. View view = null;
  531. for (int i = 0; i < listAdapter.getCount(); i++) {
  532. view = listAdapter.getView(i, view, listView);
  533. if (i == 0) {
  534. view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
  535. }
  536. view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
  537. totalHeight += view.getMeasuredHeight();
  538. }
  539. ViewGroup.LayoutParams params = listView.getLayoutParams();
  540. params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
  541. listView.setLayoutParams(params);
  542. listView.requestLayout();
  543. }
  544. /**
  545. * Enables or disables buttons for a file being downloaded
  546. */
  547. private void setButtonsForTransferring() {
  548. if (!isEmpty()) {
  549. // let's protect the user from himself ;)
  550. getView().findViewById(R.id.fdFavorite).setEnabled(false);
  551. // show the progress bar for the transfer
  552. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.VISIBLE);
  553. TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
  554. progressText.setVisibility(View.VISIBLE);
  555. FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
  556. FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder();
  557. //if (getFile().isDownloading()) {
  558. if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
  559. progressText.setText(R.string.downloader_download_in_progress_ticker);
  560. }
  561. else {
  562. if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, getFile())) {
  563. progressText.setText(R.string.uploader_upload_in_progress_ticker);
  564. }
  565. }
  566. }
  567. }
  568. /**
  569. * Enables or disables buttons for a file locally available
  570. */
  571. private void setButtonsForDown() {
  572. if (!isEmpty()) {
  573. getView().findViewById(R.id.fdFavorite).setEnabled(true);
  574. // hides the progress bar
  575. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
  576. TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
  577. progressText.setVisibility(View.GONE);
  578. }
  579. }
  580. /**
  581. * Enables or disables buttons for a file not locally available
  582. */
  583. private void setButtonsForRemote() {
  584. if (!isEmpty()) {
  585. getView().findViewById(R.id.fdFavorite).setEnabled(true);
  586. // hides the progress bar
  587. getView().findViewById(R.id.fdProgressBlock).setVisibility(View.GONE);
  588. TextView progressText = (TextView) getView().findViewById(R.id.fdProgressText);
  589. progressText.setVisibility(View.GONE);
  590. }
  591. }
  592. public void listenForTransferProgress() {
  593. if (mProgressListener != null) {
  594. if (mContainerActivity.getFileDownloaderBinder() != null) {
  595. mContainerActivity.getFileDownloaderBinder().
  596. addDatatransferProgressListener(mProgressListener, mAccount, getFile());
  597. }
  598. if (mContainerActivity.getFileUploaderBinder() != null) {
  599. mContainerActivity.getFileUploaderBinder().
  600. addDatatransferProgressListener(mProgressListener, mAccount, getFile());
  601. }
  602. } else {
  603. Log_OC.d(TAG, "mProgressListener == null");
  604. }
  605. }
  606. public void leaveTransferProgress() {
  607. if (mProgressListener != null) {
  608. if (mContainerActivity.getFileDownloaderBinder() != null) {
  609. mContainerActivity.getFileDownloaderBinder().
  610. removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
  611. }
  612. if (mContainerActivity.getFileUploaderBinder() != null) {
  613. mContainerActivity.getFileUploaderBinder().
  614. removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
  615. }
  616. }
  617. }
  618. /**
  619. * Helper class responsible for updating the progress bar shown for file uploading or
  620. * downloading
  621. */
  622. private class ProgressListener implements OnDatatransferProgressListener {
  623. int mLastPercent = 0;
  624. WeakReference<ProgressBar> mProgressBar = null;
  625. ProgressListener(ProgressBar progressBar) {
  626. mProgressBar = new WeakReference<ProgressBar>(progressBar);
  627. }
  628. @Override
  629. public void onTransferProgress(long progressRate, long totalTransferredSoFar,
  630. long totalToTransfer, String filename) {
  631. int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
  632. if (percent != mLastPercent) {
  633. ProgressBar pb = mProgressBar.get();
  634. if (pb != null) {
  635. pb.setProgress(percent);
  636. pb.postInvalidate();
  637. }
  638. }
  639. mLastPercent = percent;
  640. }
  641. }
  642. }