Fixed update of download progress bar when the gallery is left with button HOME,...
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / FileDownloadFragment.java
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 as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 package com.owncloud.android.ui.preview;
20
21 import java.lang.ref.WeakReference;
22
23 import android.accounts.Account;
24 import android.app.Activity;
25 import android.os.Bundle;
26 import android.support.v4.app.FragmentStatePagerAdapter;
27 import android.util.Log;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.View.OnClickListener;
31 import android.view.ViewGroup;
32 import android.widget.Button;
33 import android.widget.ProgressBar;
34 import android.widget.TextView;
35
36 import com.actionbarsherlock.app.SherlockFragment;
37 import com.owncloud.android.datamodel.FileDataStorageManager;
38 import com.owncloud.android.datamodel.OCFile;
39 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
40 import com.owncloud.android.ui.fragment.FileFragment;
41
42 import com.owncloud.android.R;
43
44 import eu.alefzero.webdav.OnDatatransferProgressListener;
45
46 /**
47 * This Fragment is used to monitor the progress of a file downloading.
48 *
49 * @author David A. Velasco
50 */
51 public class FileDownloadFragment extends SherlockFragment implements OnClickListener, FileFragment {
52
53 public static final String EXTRA_FILE = "FILE";
54 public static final String EXTRA_ACCOUNT = "ACCOUNT";
55
56 private FileFragment.ContainerActivity mContainerActivity;
57
58 private View mView;
59 private OCFile mFile;
60 private Account mAccount;
61 private FileDataStorageManager mStorageManager;
62
63 public ProgressListener mProgressListener;
64 private boolean mListening;
65
66 private static final String TAG = FileDownloadFragment.class.getSimpleName();
67
68 private boolean mIgnoreFirstSavedState;
69
70
71 /**
72 * Creates an empty details fragment.
73 *
74 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
75 */
76 public FileDownloadFragment() {
77 mFile = null;
78 mAccount = null;
79 mStorageManager = null;
80 mProgressListener = null;
81 mListening = false;
82 mIgnoreFirstSavedState = false;
83 }
84
85
86 /**
87 * Creates a details fragment.
88 *
89 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
90 *
91 * @param fileToDetail An {@link OCFile} to show in the fragment
92 * @param ocAccount An ownCloud account; needed to start downloads
93 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution
94 */
95 public FileDownloadFragment(OCFile fileToDetail, Account ocAccount, boolean ignoreFirstSavedState) {
96 mFile = fileToDetail;
97 mAccount = ocAccount;
98 mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment
99 mProgressListener = null;
100 mListening = false;
101 mIgnoreFirstSavedState = ignoreFirstSavedState;
102 }
103
104
105 @Override
106 public void onCreate(Bundle savedInstanceState) {
107 super.onCreate(savedInstanceState);
108 Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONCREATE " + ((mFile == null)? "(NULL)" : mFile.getFileName()));
109 }
110
111
112 @Override
113 public View onCreateView(LayoutInflater inflater, ViewGroup container,
114 Bundle savedInstanceState) {
115 super.onCreateView(inflater, container, savedInstanceState);
116
117 if (savedInstanceState != null) {
118 if (!mIgnoreFirstSavedState) {
119 mFile = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE);
120 mAccount = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_ACCOUNT);
121 } else {
122 mIgnoreFirstSavedState = false;
123 }
124 }
125
126 View view = null;
127 view = inflater.inflate(R.layout.file_download_fragment, container, false);
128 mView = view;
129
130 ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.progressBar);
131 mProgressListener = new ProgressListener(progressBar);
132
133 ((Button)mView.findViewById(R.id.cancelBtn)).setOnClickListener(this);
134
135 return view;
136 }
137
138
139 /**
140 * {@inheritDoc}
141 */
142 @Override
143 public void onAttach(Activity activity) {
144 super.onAttach(activity);
145 Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONATTACH " + ((mFile == null)?" (NULL)":mFile.getFileName()));
146 try {
147 mContainerActivity = (ContainerActivity) activity;
148
149 } catch (ClassCastException e) {
150 throw new ClassCastException(activity.toString() + " must implement " + FileFragment.ContainerActivity.class.getSimpleName());
151 }
152 }
153
154
155 /**
156 * {@inheritDoc}
157 */
158 @Override
159 public void onActivityCreated(Bundle savedInstanceState) {
160 super.onActivityCreated(savedInstanceState);
161 Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONACTIVITYCREATED " + ((mFile == null)?" (NULL)":mFile.getFileName()));
162 if (mAccount != null) {
163 mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());;
164 }
165 }
166
167
168 @Override
169 public void onSaveInstanceState(Bundle outState) {
170 super.onSaveInstanceState(outState);
171 outState.putParcelable(FileDownloadFragment.EXTRA_FILE, mFile);
172 outState.putParcelable(FileDownloadFragment.EXTRA_ACCOUNT, mAccount);
173 }
174
175 @Override
176 public void onStart() {
177 super.onStart();
178 Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONSTART " + mFile.getFileName());
179 listenForTransferProgress();
180 }
181
182 @Override
183 public void onResume() {
184 Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONRESUME " + mFile.getFileName());
185 super.onResume();
186 }
187
188
189 @Override
190 public void onPause() {
191 super.onPause();
192 Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONPAUSE " + mFile.getFileName());
193 }
194
195
196 @Override
197 public void onStop() {
198 super.onStop();
199 Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONSTOP " + mFile.getFileName());
200 leaveTransferProgress();
201 }
202
203 @Override
204 public void onDestroy() {
205 super.onDestroy();
206 Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONDESTROY " + mFile.getFileName());
207 }
208
209
210 @Override
211 public View getView() {
212 if (!mListening) {
213 listenForTransferProgress();
214 }
215 return super.getView() == null ? mView : super.getView();
216 }
217
218
219 @Override
220 public void onClick(View v) {
221 switch (v.getId()) {
222 case R.id.cancelBtn: {
223 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
224 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) {
225 downloaderBinder.cancel(mAccount, mFile);
226 leaveTransferProgress();
227 if (mFile.isDown()) {
228 setButtonsForDown();
229 } else {
230 setButtonsForRemote();
231 }
232 }
233 break;
234 }
235 default:
236 Log.e(TAG, "Incorrect view clicked!");
237 }
238 }
239
240
241 /**
242 * {@inheritDoc}
243 */
244 public OCFile getFile(){
245 return mFile;
246 }
247
248
249 /**
250 * Updates the view depending upon the state of the downloading file.
251 *
252 * @param transferring When true, the view must be updated assuming that the holded file is
253 * downloading, no matter what the downloaderBinder says.
254 */
255 public void updateView(boolean transferring) {
256 // configure UI for depending upon local state of the file
257 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
258 if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile))) {
259 setButtonsForTransferring();
260
261 } else if (mFile.isDown()) {
262
263 setButtonsForDown();
264
265 } else {
266 setButtonsForRemote();
267 }
268 getView().invalidate();
269 }
270
271
272 /**
273 * Enables or disables buttons for a file being downloaded
274 */
275 private void setButtonsForTransferring() {
276 Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
277 downloadButton.setText(R.string.common_cancel);
278
279 // show the progress bar for the transfer
280 ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
281 progressBar.setVisibility(View.VISIBLE);
282 TextView progressText = (TextView)getView().findViewById(R.id.progressText);
283 progressText.setText(R.string.downloader_download_in_progress_ticker);
284 }
285
286
287 /**
288 * Enables or disables buttons for a file locally available
289 */
290 private void setButtonsForDown() {
291 Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
292 downloadButton.setVisibility(View.GONE);
293
294 // hides the progress bar
295 ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
296 progressBar.setVisibility(View.GONE);
297
298 // updates the text message
299 TextView progressText = (TextView)getView().findViewById(R.id.progressText);
300 progressText.setText(R.string.common_loading);
301 }
302
303
304 /**
305 * Enables or disables buttons for a file not locally available
306 */
307 private void setButtonsForRemote() {
308 Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
309 downloadButton.setVisibility(View.GONE);
310 //downloadButton.setText(R.string.filedetails_download);
311
312 // hides the progress bar
313 ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
314 progressBar.setVisibility(View.GONE);
315
316 // updates the text message
317 TextView progressText = (TextView)getView().findViewById(R.id.progressText);
318 progressText.setText(R.string.downloader_not_downloaded_yet);
319 }
320
321
322 public void listenForTransferProgress() {
323 if (mProgressListener != null && !mListening) {
324 if (mContainerActivity.getFileDownloaderBinder() != null) {
325 mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, mFile);
326 mListening = true;
327 setButtonsForTransferring();
328 }
329 }
330 }
331
332
333 public void leaveTransferProgress() {
334 if (mProgressListener != null) {
335 if (mContainerActivity.getFileDownloaderBinder() != null) {
336 mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, mFile);
337 mListening = false;
338 }
339 }
340 }
341
342
343 /**
344 * Helper class responsible for updating the progress bar shown for file uploading or downloading
345 *
346 * @author David A. Velasco
347 */
348 private class ProgressListener implements OnDatatransferProgressListener {
349 int mLastPercent = 0;
350 WeakReference<ProgressBar> mProgressBar = null;
351
352 ProgressListener(ProgressBar progressBar) {
353 mProgressBar = new WeakReference<ProgressBar>(progressBar);
354 }
355
356 @Override
357 public void onTransferProgress(long progressRate) {
358 // old method, nothing here
359 };
360
361 @Override
362 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) {
363 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
364 if (percent != mLastPercent) {
365 ProgressBar pb = mProgressBar.get();
366 if (pb != null) {
367 pb.setProgress(percent);
368 pb.postInvalidate();
369 }
370 }
371 mLastPercent = percent;
372 }
373
374 };
375
376
377
378 }