1 /* ownCloud Android client application
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.ui
.preview
;
20 import java
.lang
.ref
.WeakReference
;
22 import com
.owncloud
.android
.Log_OC
;
23 import com
.owncloud
.android
.R
;
24 import com
.owncloud
.android
.datamodel
.OCFile
;
25 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
26 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
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
;
42 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
45 * This Fragment is used to monitor the progress of a file downloading.
47 * @author David A. Velasco
49 public class FileDownloadFragment
extends FileFragment
implements OnClickListener
{
51 public static final String EXTRA_FILE
= "FILE";
52 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
53 private static final String EXTRA_ERROR
= "ERROR";
55 private FileFragment
.ContainerActivity mContainerActivity
;
58 private Account mAccount
;
60 public ProgressListener mProgressListener
;
61 private boolean mListening
;
63 private static final String TAG
= FileDownloadFragment
.class.getSimpleName();
65 private boolean mIgnoreFirstSavedState
;
66 private boolean mError
;
70 * Creates an empty details fragment.
72 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
74 public FileDownloadFragment() {
77 mProgressListener
= null
;
79 mIgnoreFirstSavedState
= false
;
85 * Creates a details fragment.
87 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
89 * @param fileToDetail An {@link OCFile} to show in the fragment
90 * @param ocAccount An ownCloud account; needed to start downloads
91 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution
93 public FileDownloadFragment(OCFile fileToDetail
, Account ocAccount
, boolean ignoreFirstSavedState
) {
96 mProgressListener
= null
;
98 mIgnoreFirstSavedState
= ignoreFirstSavedState
;
104 public void onCreate(Bundle savedInstanceState
) {
105 super.onCreate(savedInstanceState
);
110 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
111 Bundle savedInstanceState
) {
112 super.onCreateView(inflater
, container
, savedInstanceState
);
114 if (savedInstanceState
!= null
) {
115 if (!mIgnoreFirstSavedState
) {
116 setFile((OCFile
)savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_FILE
));
117 mAccount
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
);
118 mError
= savedInstanceState
.getBoolean(FileDownloadFragment
.EXTRA_ERROR
);
120 mIgnoreFirstSavedState
= false
;
125 view
= inflater
.inflate(R
.layout
.file_download_fragment
, container
, false
);
128 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.progressBar
);
129 mProgressListener
= new ProgressListener(progressBar
);
131 ((ImageButton
)mView
.findViewById(R
.id
.cancelBtn
)).setOnClickListener(this);
134 setButtonsForRemote();
136 setButtonsForTransferring();
147 public void onAttach(Activity activity
) {
148 super.onAttach(activity
);
150 mContainerActivity
= (ContainerActivity
) activity
;
152 } catch (ClassCastException e
) {
153 throw new ClassCastException(activity
.toString() + " must implement " + FileFragment
.ContainerActivity
.class.getSimpleName());
162 public void onActivityCreated(Bundle savedInstanceState
) {
163 super.onActivityCreated(savedInstanceState
);
164 if (mAccount
!= null
) {
165 //mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());;
171 public void onSaveInstanceState(Bundle outState
) {
172 super.onSaveInstanceState(outState
);
173 outState
.putParcelable(FileDownloadFragment
.EXTRA_FILE
, getFile());
174 outState
.putParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
, mAccount
);
175 outState
.putBoolean(FileDownloadFragment
.EXTRA_ERROR
, mError
);
179 public void onStart() {
181 listenForTransferProgress();
185 public void onResume() {
191 public void onPause() {
197 public void onStop() {
199 leaveTransferProgress();
203 public void onDestroy() {
209 public View
getView() {
211 listenForTransferProgress();
213 return super.getView() == null ? mView
: super.getView();
218 public void onClick(View v
) {
220 case R
.id
.cancelBtn
: {
221 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
222 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
223 downloaderBinder
.cancel(mAccount
, getFile());
224 getActivity().finish(); // :)
226 leaveTransferProgress();
227 if (mFile.isDown()) {
230 setButtonsForRemote();
237 Log_OC
.e(TAG
, "Incorrect view clicked!");
243 * Updates the view depending upon the state of the downloading file.
245 * @param transferring When true, the view must be updated assuming that the holded file is
246 * downloading, no matter what the downloaderBinder says.
248 public void updateView(boolean transferring
) {
249 // configure UI for depending upon local state of the file
250 FileDownloaderBinder downloaderBinder
= (mContainerActivity
== null
) ? null
: mContainerActivity
.getFileDownloaderBinder();
251 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile()))) {
252 setButtonsForTransferring();
254 } else if (getFile().isDown()) {
259 setButtonsForRemote();
261 getView().invalidate();
267 * Enables or disables buttons for a file being downloaded
269 private void setButtonsForTransferring() {
270 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.VISIBLE
);
272 // show the progress bar for the transfer
273 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.VISIBLE
);
274 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
275 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
276 progressText
.setVisibility(View
.VISIBLE
);
278 // hides the error icon
279 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
280 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
285 * Enables or disables buttons for a file locally available
287 private void setButtonsForDown() {
288 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
290 // hides the progress bar
291 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
293 // updates the text message
294 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
295 progressText
.setText(R
.string
.common_loading
);
296 progressText
.setVisibility(View
.VISIBLE
);
298 // hides the error icon
299 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
300 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
305 * Enables or disables buttons for a file not locally available
307 * Currently, this is only used when a download was failed
309 private void setButtonsForRemote() {
310 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
312 // hides the progress bar and message
313 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
314 getView().findViewById(R
.id
.progressText
).setVisibility(View
.GONE
);
316 // shows the error icon and message
317 getView().findViewById(R
.id
.errorText
).setVisibility(View
.VISIBLE
);
318 getView().findViewById(R
.id
.error_image
).setVisibility(View
.VISIBLE
);
322 public void listenForTransferProgress() {
323 if (mProgressListener
!= null
&& !mListening
) {
324 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
325 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
327 setButtonsForTransferring();
333 public void leaveTransferProgress() {
334 if (mProgressListener
!= null
) {
335 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
336 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
344 * Helper class responsible for updating the progress bar shown for file uploading or downloading
346 * @author David A. Velasco
348 private class ProgressListener
implements OnDatatransferProgressListener
{
349 int mLastPercent
= 0;
350 WeakReference
<ProgressBar
> mProgressBar
= null
;
352 ProgressListener(ProgressBar progressBar
) {
353 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
357 public void onTransferProgress(long progressRate
) {
358 // old method, nothing here
362 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
363 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
364 if (percent
!= mLastPercent
) {
365 ProgressBar pb
= mProgressBar
.get();
367 pb
.setProgress(percent
);
371 mLastPercent
= percent
;
377 public void setError(boolean error
) {