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