FileDownloadFragment.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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.support.v4.app.Fragment;
  24. import android.support.v4.app.FragmentStatePagerAdapter;
  25. import android.view.LayoutInflater;
  26. import android.view.View;
  27. import android.view.View.OnClickListener;
  28. import android.view.ViewGroup;
  29. import android.widget.ProgressBar;
  30. import android.widget.TextView;
  31. import com.owncloud.android.R;
  32. import com.owncloud.android.datamodel.OCFile;
  33. import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
  34. import com.owncloud.android.lib.common.utils.Log_OC;
  35. import com.owncloud.android.ui.fragment.FileFragment;
  36. import com.owncloud.android.utils.DisplayUtils;
  37. import java.lang.ref.WeakReference;
  38. /**
  39. * This Fragment is used to monitor the progress of a file downloading.
  40. */
  41. public class FileDownloadFragment extends FileFragment implements OnClickListener {
  42. public static final String EXTRA_FILE = "FILE";
  43. public static final String EXTRA_ACCOUNT = "ACCOUNT";
  44. private static final String EXTRA_ERROR = "ERROR";
  45. private static final String ARG_FILE = "FILE";
  46. private static final String ARG_IGNORE_FIRST = "IGNORE_FIRST";
  47. private static final String ARG_ACCOUNT = "ACCOUNT" ;
  48. private View mView;
  49. private Account mAccount;
  50. public ProgressListener mProgressListener;
  51. private boolean mListening;
  52. private static final String TAG = FileDownloadFragment.class.getSimpleName();
  53. private boolean mIgnoreFirstSavedState;
  54. private boolean mError;
  55. /**
  56. * Public factory method to create a new fragment that shows the progress of a file download.
  57. *
  58. * Android strongly recommends keep the empty constructor of fragments as the only public constructor, and
  59. * use {@link #setArguments(Bundle)} to set the needed arguments.
  60. *
  61. * This method hides to client objects the need of doing the construction in two steps.
  62. *
  63. * When 'file' is null creates a dummy layout (useful when a file wasn't tapped before).
  64. *
  65. * @param file An {@link OCFile} to show in the fragment
  66. * @param account An OC account; needed to start downloads
  67. * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}
  68. * TODO better solution
  69. */
  70. public static Fragment newInstance(OCFile file, Account account, boolean ignoreFirstSavedState) {
  71. FileDownloadFragment frag = new FileDownloadFragment();
  72. Bundle args = new Bundle();
  73. args.putParcelable(ARG_FILE, file);
  74. args.putParcelable(ARG_ACCOUNT, account);
  75. args.putBoolean(ARG_IGNORE_FIRST, ignoreFirstSavedState);
  76. frag.setArguments(args);
  77. return frag;
  78. }
  79. /**
  80. * Creates an empty details fragment.
  81. *
  82. * It's necessary to keep a public constructor without parameters; the system uses it when tries to
  83. * reinstantiate a fragment automatically.
  84. */
  85. public FileDownloadFragment() {
  86. super();
  87. mAccount = null;
  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((OCFile)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. mAccount = args.getParcelable(ARG_ACCOUNT);
  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((OCFile) savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE));
  109. mAccount = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_ACCOUNT);
  110. mError = savedInstanceState.getBoolean(FileDownloadFragment.EXTRA_ERROR);
  111. }
  112. else {
  113. mIgnoreFirstSavedState = false;
  114. }
  115. }
  116. mView = inflater.inflate(R.layout.file_download_fragment, container, false);
  117. ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.progressBar);
  118. DisplayUtils.colorPreLollipopHorizontalProgressBar(progressBar);
  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(Bundle outState) {
  137. super.onSaveInstanceState(outState);
  138. outState.putParcelable(FileDownloadFragment.EXTRA_FILE, getFile());
  139. outState.putParcelable(FileDownloadFragment.EXTRA_ACCOUNT, mAccount);
  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. mContainerActivity.getFileOperationsHelper().cancelTransference(getFile());
  176. getActivity().finish();
  177. break;
  178. }
  179. default:
  180. Log_OC.e(TAG, "Incorrect view clicked!");
  181. }
  182. }
  183. /**
  184. * Enables or disables buttons for a file being downloaded
  185. */
  186. private void setButtonsForTransferring() {
  187. getView().findViewById(R.id.cancelBtn).setVisibility(View.VISIBLE);
  188. // show the progress bar for the transfer
  189. getView().findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
  190. TextView progressText = (TextView) getView().findViewById(R.id.progressText);
  191. progressText.setText(R.string.downloader_download_in_progress_ticker);
  192. progressText.setVisibility(View.VISIBLE);
  193. // hides the error icon
  194. getView().findViewById(R.id.errorText).setVisibility(View.GONE);
  195. getView().findViewById(R.id.error_image).setVisibility(View.GONE);
  196. }
  197. /**
  198. * Enables or disables buttons for a file locally available
  199. */
  200. private void setButtonsForDown() {
  201. getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
  202. // hides the progress bar
  203. getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
  204. // updates the text message
  205. TextView progressText = (TextView) getView().findViewById(R.id.progressText);
  206. progressText.setText(R.string.common_loading);
  207. progressText.setVisibility(View.VISIBLE);
  208. // hides the error icon
  209. getView().findViewById(R.id.errorText).setVisibility(View.GONE);
  210. getView().findViewById(R.id.error_image).setVisibility(View.GONE);
  211. }
  212. /**
  213. * Enables or disables buttons for a file not locally available
  214. * <p/>
  215. * Currently, this is only used when a download was failed
  216. */
  217. private void setButtonsForRemote() {
  218. getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
  219. // hides the progress bar and message
  220. getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
  221. getView().findViewById(R.id.progressText).setVisibility(View.GONE);
  222. // shows the error icon and message
  223. getView().findViewById(R.id.errorText).setVisibility(View.VISIBLE);
  224. getView().findViewById(R.id.error_image).setVisibility(View.VISIBLE);
  225. }
  226. public void listenForTransferProgress() {
  227. if (mProgressListener != null && !mListening && mContainerActivity.getFileDownloaderBinder() != null) {
  228. mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(
  229. mProgressListener, mAccount, getFile()
  230. );
  231. mListening = true;
  232. setButtonsForTransferring();
  233. }
  234. }
  235. public void leaveTransferProgress() {
  236. if (mProgressListener != null && mContainerActivity.getFileDownloaderBinder() != null) {
  237. mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(
  238. mProgressListener, mAccount, getFile()
  239. );
  240. mListening = false;
  241. }
  242. }
  243. /**
  244. * Helper class responsible for updating the progress bar shown for file uploading or downloading
  245. */
  246. private class ProgressListener implements OnDatatransferProgressListener {
  247. int mLastPercent = 0;
  248. WeakReference<ProgressBar> mProgressBar = null;
  249. ProgressListener(ProgressBar progressBar) {
  250. mProgressBar = new WeakReference<ProgressBar>(progressBar);
  251. }
  252. @Override
  253. public void onTransferProgress(
  254. long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename
  255. ) {
  256. int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
  257. if (percent != mLastPercent) {
  258. ProgressBar pb = mProgressBar.get();
  259. if (pb != null) {
  260. pb.setProgress(percent);
  261. pb.postInvalidate();
  262. }
  263. }
  264. mLastPercent = percent;
  265. }
  266. }
  267. public void setError(boolean error) {
  268. mError = error;
  269. }
  270. }