FileDetailFragment.java 29 KB

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