FileDownloadFragment.java 10 KB

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