FileDetailFragment.java 28 KB

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