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