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