FileDownloadFragment.java 11 KB

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