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