FileDownloadFragment.java 11 KB

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