FileDownloadFragment.java 12 KB

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