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