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