FileDownloadFragment.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.ui.preview;
  21. import android.accounts.Account;
  22. import android.os.Bundle;
  23. import android.view.LayoutInflater;
  24. import android.view.View;
  25. import android.view.View.OnClickListener;
  26. import android.view.ViewGroup;
  27. import android.widget.ProgressBar;
  28. import android.widget.TextView;
  29. import com.nextcloud.client.account.User;
  30. import com.owncloud.android.R;
  31. import com.owncloud.android.datamodel.OCFile;
  32. import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
  33. import com.owncloud.android.lib.common.utils.Log_OC;
  34. import com.owncloud.android.ui.fragment.FileFragment;
  35. import com.owncloud.android.utils.ThemeUtils;
  36. import java.lang.ref.WeakReference;
  37. import androidx.annotation.NonNull;
  38. import androidx.fragment.app.Fragment;
  39. import androidx.fragment.app.FragmentStatePagerAdapter;
  40. /**
  41. * This Fragment is used to monitor the progress of a file downloading.
  42. */
  43. public class FileDownloadFragment extends FileFragment implements OnClickListener {
  44. public static final String EXTRA_FILE = "FILE";
  45. public static final String EXTRA_USER = "USER";
  46. private static final String EXTRA_ERROR = "ERROR";
  47. private static final String ARG_FILE = "FILE";
  48. private static final String ARG_IGNORE_FIRST = "IGNORE_FIRST";
  49. private static final String ARG_USER = "USER" ;
  50. private View mView;
  51. private User user;
  52. public ProgressListener mProgressListener;
  53. private boolean mListening;
  54. private static final String TAG = FileDownloadFragment.class.getSimpleName();
  55. private boolean mIgnoreFirstSavedState;
  56. private boolean mError;
  57. /**
  58. * Public factory method to create a new fragment that shows the progress of a file download.
  59. *
  60. * Android strongly recommends keep the empty constructor of fragments as the only public constructor, and
  61. * use {@link #setArguments(Bundle)} to set the needed arguments.
  62. *
  63. * This method hides to client objects the need of doing the construction in two steps.
  64. *
  65. * When 'file' is null creates a dummy layout (useful when a file wasn't tapped before).
  66. *
  67. * @param file An {@link OCFile} to show in the fragment
  68. * @param user Nextcloud user; needed to start downloads
  69. * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}
  70. * TODO better solution
  71. */
  72. public static Fragment newInstance(OCFile file, User user, boolean ignoreFirstSavedState) {
  73. FileDownloadFragment frag = new FileDownloadFragment();
  74. Bundle args = new Bundle();
  75. args.putParcelable(ARG_FILE, file);
  76. args.putParcelable(ARG_USER, user);
  77. args.putBoolean(ARG_IGNORE_FIRST, ignoreFirstSavedState);
  78. frag.setArguments(args);
  79. return frag;
  80. }
  81. /**
  82. * Creates an empty details fragment.
  83. *
  84. * It's necessary to keep a public constructor without parameters; the system uses it when tries to
  85. * reinstantiate a fragment automatically.
  86. */
  87. public FileDownloadFragment() {
  88. super();
  89. mProgressListener = null;
  90. mListening = false;
  91. mIgnoreFirstSavedState = false;
  92. mError = false;
  93. }
  94. @Override
  95. public void onCreate(Bundle savedInstanceState) {
  96. super.onCreate(savedInstanceState);
  97. Bundle args = getArguments();
  98. setFile(args.getParcelable(ARG_FILE));
  99. // TODO better in super, but needs to check ALL the class extending FileFragment; not right now
  100. mIgnoreFirstSavedState = args.getBoolean(ARG_IGNORE_FIRST);
  101. user = args.getParcelable(ARG_USER);
  102. }
  103. @Override
  104. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  105. Bundle savedInstanceState) {
  106. super.onCreateView(inflater, container, savedInstanceState);
  107. if (savedInstanceState != null) {
  108. if (!mIgnoreFirstSavedState) {
  109. setFile(savedInstanceState.getParcelable(EXTRA_FILE));
  110. user = savedInstanceState.getParcelable(EXTRA_USER);
  111. mError = savedInstanceState.getBoolean(EXTRA_ERROR);
  112. }
  113. else {
  114. mIgnoreFirstSavedState = false;
  115. }
  116. }
  117. mView = inflater.inflate(R.layout.file_download_fragment, container, false);
  118. ProgressBar progressBar = mView.findViewById(R.id.progressBar);
  119. ThemeUtils.colorHorizontalProgressBar(progressBar, ThemeUtils.primaryAccentColor(getContext()));
  120. mProgressListener = new ProgressListener(progressBar);
  121. (mView.findViewById(R.id.cancelBtn)).setOnClickListener(this);
  122. (mView.findViewById(R.id.fileDownloadLL)).setOnClickListener(new OnClickListener() {
  123. @Override
  124. public void onClick(View v) {
  125. ((PreviewImageActivity) getActivity()).toggleFullScreen();
  126. }
  127. });
  128. if (mError) {
  129. setButtonsForRemote();
  130. }
  131. else {
  132. setButtonsForTransferring();
  133. }
  134. return mView;
  135. }
  136. @Override
  137. public void onSaveInstanceState(@NonNull Bundle outState) {
  138. super.onSaveInstanceState(outState);
  139. outState.putParcelable(FileDownloadFragment.EXTRA_FILE, getFile());
  140. outState.putParcelable(FileDownloadFragment.EXTRA_USER, user);
  141. outState.putBoolean(FileDownloadFragment.EXTRA_ERROR, mError);
  142. }
  143. @Override
  144. public void onStart() {
  145. super.onStart();
  146. listenForTransferProgress();
  147. }
  148. @Override
  149. public void onResume() {
  150. super.onResume();
  151. }
  152. @Override
  153. public void onPause() {
  154. super.onPause();
  155. }
  156. @Override
  157. public void onStop() {
  158. leaveTransferProgress();
  159. super.onStop();
  160. }
  161. @Override
  162. public void onDestroy() {
  163. super.onDestroy();
  164. }
  165. @Override
  166. public View getView() {
  167. if (!mListening) {
  168. listenForTransferProgress();
  169. }
  170. return super.getView() == null ? mView : super.getView();
  171. }
  172. @Override
  173. public void onClick(View v) {
  174. switch (v.getId()) {
  175. case R.id.cancelBtn: {
  176. containerActivity.getFileOperationsHelper().cancelTransference(getFile());
  177. getActivity().finish();
  178. break;
  179. }
  180. default:
  181. Log_OC.e(TAG, "Incorrect view clicked!");
  182. break;
  183. }
  184. }
  185. /**
  186. * Enables or disables buttons for a file being downloaded
  187. */
  188. private void setButtonsForTransferring() {
  189. getView().findViewById(R.id.cancelBtn).setVisibility(View.VISIBLE);
  190. // show the progress bar for the transfer
  191. getView().findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
  192. TextView progressText = getView().findViewById(R.id.progressText);
  193. progressText.setText(R.string.downloader_download_in_progress_ticker);
  194. progressText.setVisibility(View.VISIBLE);
  195. // hides the error icon
  196. getView().findViewById(R.id.errorText).setVisibility(View.GONE);
  197. getView().findViewById(R.id.error_image).setVisibility(View.GONE);
  198. }
  199. /**
  200. * Enables or disables buttons for a file not locally available
  201. *
  202. * Currently, this is only used when a download was failed
  203. */
  204. private void setButtonsForRemote() {
  205. getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
  206. // hides the progress bar and message
  207. getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
  208. getView().findViewById(R.id.progressText).setVisibility(View.GONE);
  209. // shows the error icon and message
  210. getView().findViewById(R.id.errorText).setVisibility(View.VISIBLE);
  211. getView().findViewById(R.id.error_image).setVisibility(View.VISIBLE);
  212. }
  213. public void listenForTransferProgress() {
  214. if (mProgressListener != null && !mListening && containerActivity.getFileDownloaderBinder() != null) {
  215. containerActivity.getFileDownloaderBinder().addDatatransferProgressListener(
  216. mProgressListener, user.toPlatformAccount(), getFile()
  217. );
  218. mListening = true;
  219. setButtonsForTransferring();
  220. }
  221. }
  222. public void leaveTransferProgress() {
  223. if (mProgressListener != null && containerActivity.getFileDownloaderBinder() != null) {
  224. containerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(
  225. mProgressListener, user.toPlatformAccount(), getFile()
  226. );
  227. mListening = false;
  228. }
  229. }
  230. /**
  231. * Helper class responsible for updating the progress bar shown for file uploading or downloading
  232. */
  233. private class ProgressListener implements OnDatatransferProgressListener {
  234. int mLastPercent;
  235. WeakReference<ProgressBar> mProgressBar;
  236. ProgressListener(ProgressBar progressBar) {
  237. mProgressBar = new WeakReference<>(progressBar);
  238. }
  239. @Override
  240. public void onTransferProgress(
  241. long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename
  242. ) {
  243. int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
  244. if (percent != mLastPercent) {
  245. ProgressBar pb = mProgressBar.get();
  246. if (pb != null) {
  247. pb.setProgress(percent);
  248. pb.postInvalidate();
  249. }
  250. }
  251. mLastPercent = percent;
  252. }
  253. }
  254. public void setError(boolean error) {
  255. mError = error;
  256. }
  257. }