FileDownloadFragment.java 11 KB

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