Minimize new fragment to monitor the progress of file download
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / fragment / 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.fragment;
20
21 import java.lang.ref.WeakReference;
22
23 import android.accounts.Account;
24 import android.app.Activity;
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.IntentFilter;
29 import android.os.Bundle;
30 import android.util.Log;
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.Button;
36 import android.widget.ProgressBar;
37 import android.widget.TextView;
38
39 import com.actionbarsherlock.app.SherlockFragment;
40 import com.owncloud.android.datamodel.FileDataStorageManager;
41 import com.owncloud.android.datamodel.OCFile;
42 import com.owncloud.android.files.services.FileDownloader;
43 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
44
45 import com.owncloud.android.R;
46
47 import eu.alefzero.webdav.OnDatatransferProgressListener;
48
49 /**
50 * This Fragment is used to monitor the progress of a file downloading.
51 *
52 * @author David A. Velasco
53 */
54 public class FileDownloadFragment extends SherlockFragment implements OnClickListener,FileFragment {
55
56 public static final String EXTRA_FILE = "FILE";
57 public static final String EXTRA_ACCOUNT = "ACCOUNT";
58
59 private FileFragment.ContainerActivity mContainerActivity;
60
61 private View mView;
62 private OCFile mFile;
63 private Account mAccount;
64 private FileDataStorageManager mStorageManager;
65
66 private DownloadFinishReceiver mDownloadFinishReceiver;
67 public ProgressListener mProgressListener;
68
69 private static final String TAG = FileDownloadFragment.class.getSimpleName();
70
71
72 /**
73 * Creates an empty details fragment.
74 *
75 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
76 */
77 public FileDownloadFragment() {
78 mFile = null;
79 mAccount = null;
80 mStorageManager = null;
81 mProgressListener = null;
82 }
83
84
85 /**
86 * Creates a details fragment.
87 *
88 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
89 *
90 * @param fileToDetail An {@link OCFile} to show in the fragment
91 * @param ocAccount An ownCloud account; needed to start downloads
92 */
93 public FileDownloadFragment(OCFile fileToDetail, Account ocAccount) {
94 mFile = fileToDetail;
95 mAccount = ocAccount;
96 mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment
97 mProgressListener = null;
98 }
99
100
101 @Override
102 public void onCreate(Bundle savedInstanceState) {
103 super.onCreate(savedInstanceState);
104 Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONCREATE " + ((mFile == null)? "(NULL)" : mFile.getFileName()));
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 mFile = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE);
115 mAccount = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_ACCOUNT);
116 }
117
118 if(mFile != null && mAccount != null) {
119 //mLayout = R.layout.file_details_fragment;
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.fdProgressBar);
127 mProgressListener = new ProgressListener(progressBar);
128
129 return view;
130 }
131
132
133 /**
134 * {@inheritDoc}
135 */
136 @Override
137 public void onAttach(Activity activity) {
138 super.onAttach(activity);
139 try {
140 mContainerActivity = (ContainerActivity) activity;
141
142 } catch (ClassCastException e) {
143 throw new ClassCastException(activity.toString() + " must implement " + FileFragment.ContainerActivity.class.getSimpleName());
144 }
145 }
146
147
148 /**
149 * {@inheritDoc}
150 */
151 @Override
152 public void onActivityCreated(Bundle savedInstanceState) {
153 super.onActivityCreated(savedInstanceState);
154 if (mAccount != null) {
155 mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());;
156 }
157 }
158
159
160 @Override
161 public void onSaveInstanceState(Bundle outState) {
162 super.onSaveInstanceState(outState);
163 outState.putParcelable(FileDownloadFragment.EXTRA_FILE, mFile);
164 outState.putParcelable(FileDownloadFragment.EXTRA_ACCOUNT, mAccount);
165 }
166
167 @Override
168 public void onStart() {
169 super.onStart();
170 Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONSTART " + mFile.getFileName());
171 listenForTransferProgress();
172 }
173
174 @Override
175 public void onResume() {
176 Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONRESUME " + mFile.getFileName());
177 super.onResume();
178
179 mDownloadFinishReceiver = new DownloadFinishReceiver();
180 IntentFilter filter = new IntentFilter(
181 FileDownloader.DOWNLOAD_FINISH_MESSAGE);
182 getActivity().registerReceiver(mDownloadFinishReceiver, filter);
183 }
184
185
186 @Override
187 public void onPause() {
188 super.onPause();
189 Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONPAUSE " + mFile.getFileName());
190
191 getActivity().unregisterReceiver(mDownloadFinishReceiver);
192 mDownloadFinishReceiver = null;
193 }
194
195
196 @Override
197 public void onStop() {
198 super.onStop();
199 Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONSTOP " + mFile.getFileName());
200 leaveTransferProgress();
201 }
202
203 @Override
204 public void onDestroy() {
205 super.onDestroy();
206 Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONDESTROY " + mFile.getFileName());
207 }
208
209
210 @Override
211 public View getView() {
212 return super.getView() == null ? mView : super.getView();
213 }
214
215
216 @Override
217 public void onClick(View v) {
218 switch (v.getId()) {
219 case R.id.cancelBtn: {
220 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
221 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) {
222 downloaderBinder.cancel(mAccount, mFile);
223 if (mFile.isDown()) {
224 setButtonsForDown();
225 } else {
226 setButtonsForRemote();
227 }
228 }
229 break;
230 }
231 default:
232 Log.e(TAG, "Incorrect view clicked!");
233 }
234 }
235
236
237 /**
238 * {@inheritDoc}
239 */
240 public OCFile getFile(){
241 return mFile;
242 }
243
244
245 /**
246 * Updates the view depending upon the state of the downloading file.
247 */
248 public void updateView() {
249 // configure UI for depending upon local state of the file
250 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
251 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) {
252 setButtonsForTransferring();
253
254 } else if (mFile.isDown()) {
255
256 setButtonsForDown();
257
258 } else {
259 setButtonsForRemote();
260 }
261 getView().invalidate();
262 }
263
264
265 /**
266 * Enables or disables buttons for a file being downloaded
267 */
268 private void setButtonsForTransferring() {
269 Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
270 downloadButton.setText(R.string.common_cancel);
271
272 // show the progress bar for the transfer
273 ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.fdProgressBar);
274 progressBar.setVisibility(View.VISIBLE);
275 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
276 progressText.setText(R.string.downloader_download_in_progress_ticker);
277 }
278
279
280 /**
281 * Enables or disables buttons for a file locally available
282 */
283 private void setButtonsForDown() {
284 Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
285 downloadButton.setVisibility(View.GONE);
286
287 // hides the progress bar
288 ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.fdProgressBar);
289 progressBar.setVisibility(View.GONE);
290
291 // updates the text message
292 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
293 progressText.setText(R.string.common_loading);
294 }
295
296
297 /**
298 * Enables or disables buttons for a file not locally available
299 */
300 private void setButtonsForRemote() {
301 Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
302 downloadButton.setVisibility(View.GONE);
303 //downloadButton.setText(R.string.filedetails_download);
304
305 // hides the progress bar
306 ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.fdProgressBar);
307 progressBar.setVisibility(View.GONE);
308
309 // updates the text message
310 TextView progressText = (TextView)getView().findViewById(R.id.fdProgressText);
311 progressText.setText(R.string.downloader_not_downloaded_yet);
312 }
313
314
315 /**
316 * Once the file download has finished -> update view
317 */
318 private class DownloadFinishReceiver extends BroadcastReceiver {
319 @Override
320 public void onReceive(Context context, Intent intent) {
321 String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
322 if (accountName.equals(mAccount.name)) {
323 boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);
324 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
325 if (mFile.getRemotePath().equals(downloadedRemotePath)) {
326 if (downloadWasFine) {
327 mFile = mStorageManager.getFileByPath(downloadedRemotePath);
328 }
329 mContainerActivity.notifySuccessfulDownload(mFile, intent, downloadWasFine);
330 getActivity().removeStickyBroadcast(intent);
331 updateView();
332 }
333 }
334 }
335 }
336
337
338 public void listenForTransferProgress() {
339 if (mProgressListener != null) {
340 if (mContainerActivity.getFileDownloaderBinder() != null) {
341 mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, mFile);
342 }
343 if (mContainerActivity.getFileUploaderBinder() != null) {
344 mContainerActivity.getFileUploaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, mFile);
345 }
346 }
347 }
348
349
350 public void leaveTransferProgress() {
351 if (mProgressListener != null) {
352 if (mContainerActivity.getFileDownloaderBinder() != null) {
353 mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, mFile);
354 }
355 if (mContainerActivity.getFileUploaderBinder() != null) {
356 mContainerActivity.getFileUploaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, mFile);
357 }
358 }
359 }
360
361
362 /**
363 * Helper class responsible for updating the progress bar shown for file uploading or downloading
364 *
365 * @author David A. Velasco
366 */
367 private class ProgressListener implements OnDatatransferProgressListener {
368 int mLastPercent = 0;
369 WeakReference<ProgressBar> mProgressBar = null;
370
371 ProgressListener(ProgressBar progressBar) {
372 mProgressBar = new WeakReference<ProgressBar>(progressBar);
373 }
374
375 @Override
376 public void onTransferProgress(long progressRate) {
377 // old method, nothing here
378 };
379
380 @Override
381 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) {
382 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
383 if (percent != mLastPercent) {
384 ProgressBar pb = mProgressBar.get();
385 if (pb != null) {
386 pb.setProgress(percent);
387 pb.postInvalidate();
388 }
389 }
390 mLastPercent = percent;
391 }
392
393 };
394
395
396
397 }