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