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