Refactoring to reduce dependencies and duplicated code (WIP)
[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.app.Activity;
30 import android.os.Bundle;
31 import android.support.v4.app.FragmentStatePagerAdapter;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.View.OnClickListener;
35 import android.view.ViewGroup;
36 import android.widget.ImageButton;
37 import android.widget.ProgressBar;
38 import android.widget.TextView;
39
40 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
41
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 @Override
158 public void onSaveInstanceState(Bundle outState) {
159 super.onSaveInstanceState(outState);
160 outState.putParcelable(FileDownloadFragment.EXTRA_FILE, getFile());
161 outState.putParcelable(FileDownloadFragment.EXTRA_ACCOUNT, mAccount);
162 outState.putBoolean(FileDownloadFragment.EXTRA_ERROR, mError);
163 }
164
165 @Override
166 public void onStart() {
167 super.onStart();
168 listenForTransferProgress();
169 }
170
171 @Override
172 public void onResume() {
173 super.onResume();
174 }
175
176
177 @Override
178 public void onPause() {
179 super.onPause();
180 }
181
182
183 @Override
184 public void onStop() {
185 super.onStop();
186 leaveTransferProgress();
187 }
188
189 @Override
190 public void onDestroy() {
191 super.onDestroy();
192 }
193
194
195 @Override
196 public View getView() {
197 if (!mListening) {
198 listenForTransferProgress();
199 }
200 return super.getView() == null ? mView : super.getView();
201 }
202
203
204 @Override
205 public void onClick(View v) {
206 switch (v.getId()) {
207 case R.id.cancelBtn: {
208 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
209 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile())) {
210 downloaderBinder.cancel(mAccount, getFile());
211 getActivity().finish(); // :)
212 /*
213 leaveTransferProgress();
214 if (mFile.isDown()) {
215 setButtonsForDown();
216 } else {
217 setButtonsForRemote();
218 }
219 */
220 }
221 break;
222 }
223 default:
224 Log_OC.e(TAG, "Incorrect view clicked!");
225 }
226 }
227
228
229 /**
230 * Updates the view depending upon the state of the downloading file.
231 *
232 * @param transferring When true, the view must be updated assuming that the holded file is
233 * downloading, no matter what the downloaderBinder says.
234 */
235 public void updateView(boolean transferring) {
236 // configure UI for depending upon local state of the file
237 FileDownloaderBinder downloaderBinder = (mContainerActivity == null) ? null : mContainerActivity.getFileDownloaderBinder();
238 if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile()))) {
239 setButtonsForTransferring();
240
241 } else if (getFile().isDown()) {
242
243 setButtonsForDown();
244
245 } else {
246 setButtonsForRemote();
247 }
248 getView().invalidate();
249
250 }
251
252
253 /**
254 * Enables or disables buttons for a file being downloaded
255 */
256 private void setButtonsForTransferring() {
257 getView().findViewById(R.id.cancelBtn).setVisibility(View.VISIBLE);
258
259 // show the progress bar for the transfer
260 getView().findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
261 TextView progressText = (TextView)getView().findViewById(R.id.progressText);
262 progressText.setText(R.string.downloader_download_in_progress_ticker);
263 progressText.setVisibility(View.VISIBLE);
264
265 // hides the error icon
266 getView().findViewById(R.id.errorText).setVisibility(View.GONE);
267 getView().findViewById(R.id.error_image).setVisibility(View.GONE);
268 }
269
270
271 /**
272 * Enables or disables buttons for a file locally available
273 */
274 private void setButtonsForDown() {
275 getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
276
277 // hides the progress bar
278 getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
279
280 // updates the text message
281 TextView progressText = (TextView)getView().findViewById(R.id.progressText);
282 progressText.setText(R.string.common_loading);
283 progressText.setVisibility(View.VISIBLE);
284
285 // hides the error icon
286 getView().findViewById(R.id.errorText).setVisibility(View.GONE);
287 getView().findViewById(R.id.error_image).setVisibility(View.GONE);
288 }
289
290
291 /**
292 * Enables or disables buttons for a file not locally available
293 *
294 * Currently, this is only used when a download was failed
295 */
296 private void setButtonsForRemote() {
297 getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
298
299 // hides the progress bar and message
300 getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
301 getView().findViewById(R.id.progressText).setVisibility(View.GONE);
302
303 // shows the error icon and message
304 getView().findViewById(R.id.errorText).setVisibility(View.VISIBLE);
305 getView().findViewById(R.id.error_image).setVisibility(View.VISIBLE);
306 }
307
308
309 public void listenForTransferProgress() {
310 if (mProgressListener != null && !mListening) {
311 if (mContainerActivity.getFileDownloaderBinder() != null) {
312 mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile());
313 mListening = true;
314 setButtonsForTransferring();
315 }
316 }
317 }
318
319
320 public void leaveTransferProgress() {
321 if (mProgressListener != null) {
322 if (mContainerActivity.getFileDownloaderBinder() != null) {
323 mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
324 mListening = false;
325 }
326 }
327 }
328
329
330 /**
331 * Helper class responsible for updating the progress bar shown for file uploading or downloading
332 *
333 * @author David A. Velasco
334 */
335 private class ProgressListener implements OnDatatransferProgressListener {
336 int mLastPercent = 0;
337 WeakReference<ProgressBar> mProgressBar = null;
338
339 ProgressListener(ProgressBar progressBar) {
340 mProgressBar = new WeakReference<ProgressBar>(progressBar);
341 }
342
343 @Override
344 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) {
345 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
346 if (percent != mLastPercent) {
347 ProgressBar pb = mProgressBar.get();
348 if (pb != null) {
349 pb.setProgress(percent);
350 pb.postInvalidate();
351 }
352 }
353 mLastPercent = percent;
354 }
355
356 }
357
358
359 public void setError(boolean error) {
360 mError = error;
361 };
362
363
364
365 }