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
.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
;
28 import android
.accounts
.Account
;
29 import android
.os
.Bundle
;
30 import android
.support
.v4
.app
.FragmentStatePagerAdapter
;
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
.ImageButton
;
36 import android
.widget
.ProgressBar
;
37 import android
.widget
.TextView
;
39 import com
.owncloud
.android
.lib
.common
.network
.OnDatatransferProgressListener
;
43 * This Fragment is used to monitor the progress of a file downloading.
45 * @author David A. Velasco
47 public class FileDownloadFragment
extends FileFragment
implements OnClickListener
{
49 public static final String EXTRA_FILE
= "FILE";
50 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
51 private static final String EXTRA_ERROR
= "ERROR";
53 private FileFragment
.ContainerActivity mContainerActivity
;
56 private Account mAccount
;
58 public ProgressListener mProgressListener
;
59 private boolean mListening
;
61 private static final String TAG
= FileDownloadFragment
.class.getSimpleName();
63 private boolean mIgnoreFirstSavedState
;
64 private boolean mError
;
68 * Creates an empty details fragment.
70 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
72 public FileDownloadFragment() {
75 mProgressListener
= null
;
77 mIgnoreFirstSavedState
= false
;
83 * Creates a details fragment.
85 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
87 * @param fileToDetail An {@link OCFile} to show in the fragment
88 * @param ocAccount An ownCloud account; needed to start downloads
89 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution
91 public FileDownloadFragment(OCFile fileToDetail
, Account ocAccount
, boolean ignoreFirstSavedState
) {
94 mProgressListener
= null
;
96 mIgnoreFirstSavedState
= ignoreFirstSavedState
;
102 public void onCreate(Bundle savedInstanceState
) {
103 super.onCreate(savedInstanceState
);
108 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
109 Bundle savedInstanceState
) {
110 super.onCreateView(inflater
, container
, savedInstanceState
);
112 if (savedInstanceState
!= null
) {
113 if (!mIgnoreFirstSavedState
) {
114 setFile((OCFile
)savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_FILE
));
115 mAccount
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
);
116 mError
= savedInstanceState
.getBoolean(FileDownloadFragment
.EXTRA_ERROR
);
118 mIgnoreFirstSavedState
= false
;
123 view
= inflater
.inflate(R
.layout
.file_download_fragment
, container
, false
);
126 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.progressBar
);
127 mProgressListener
= new ProgressListener(progressBar
);
129 ((ImageButton
)mView
.findViewById(R
.id
.cancelBtn
)).setOnClickListener(this);
132 setButtonsForRemote();
134 setButtonsForTransferring();
142 public void onSaveInstanceState(Bundle outState
) {
143 super.onSaveInstanceState(outState
);
144 outState
.putParcelable(FileDownloadFragment
.EXTRA_FILE
, getFile());
145 outState
.putParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
, mAccount
);
146 outState
.putBoolean(FileDownloadFragment
.EXTRA_ERROR
, mError
);
150 public void onStart() {
152 listenForTransferProgress();
156 public void onResume() {
162 public void onPause() {
168 public void onStop() {
170 leaveTransferProgress();
174 public void onDestroy() {
180 public View
getView() {
182 listenForTransferProgress();
184 return super.getView() == null ? mView
: super.getView();
189 public void onClick(View v
) {
191 case R
.id
.cancelBtn
: {
192 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
193 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
194 downloaderBinder
.cancel(mAccount
, getFile());
195 getActivity().finish(); // :)
197 leaveTransferProgress();
198 if (mFile.isDown()) {
201 setButtonsForRemote();
208 Log_OC
.e(TAG
, "Incorrect view clicked!");
214 * Updates the view depending upon the state of the downloading file.
216 * @param transferring When true, the view must be updated assuming that the holded file is
217 * downloading, no matter what the downloaderBinder says.
219 public void updateView(boolean transferring
) {
220 // configure UI for depending upon local state of the file
221 FileDownloaderBinder downloaderBinder
= (mContainerActivity
== null
) ? null
: mContainerActivity
.getFileDownloaderBinder();
222 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile()))) {
223 setButtonsForTransferring();
225 } else if (getFile().isDown()) {
230 setButtonsForRemote();
232 getView().invalidate();
238 * Enables or disables buttons for a file being downloaded
240 private void setButtonsForTransferring() {
241 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.VISIBLE
);
243 // show the progress bar for the transfer
244 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.VISIBLE
);
245 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
246 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
247 progressText
.setVisibility(View
.VISIBLE
);
249 // hides the error icon
250 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
251 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
256 * Enables or disables buttons for a file locally available
258 private void setButtonsForDown() {
259 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
261 // hides the progress bar
262 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
264 // updates the text message
265 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
266 progressText
.setText(R
.string
.common_loading
);
267 progressText
.setVisibility(View
.VISIBLE
);
269 // hides the error icon
270 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
271 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
276 * Enables or disables buttons for a file not locally available
278 * Currently, this is only used when a download was failed
280 private void setButtonsForRemote() {
281 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
283 // hides the progress bar and message
284 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
285 getView().findViewById(R
.id
.progressText
).setVisibility(View
.GONE
);
287 // shows the error icon and message
288 getView().findViewById(R
.id
.errorText
).setVisibility(View
.VISIBLE
);
289 getView().findViewById(R
.id
.error_image
).setVisibility(View
.VISIBLE
);
293 public void listenForTransferProgress() {
294 if (mProgressListener
!= null
&& !mListening
) {
295 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
296 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
298 setButtonsForTransferring();
304 public void leaveTransferProgress() {
305 if (mProgressListener
!= null
) {
306 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
307 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
315 * Helper class responsible for updating the progress bar shown for file uploading or downloading
317 * @author David A. Velasco
319 private class ProgressListener
implements OnDatatransferProgressListener
{
320 int mLastPercent
= 0;
321 WeakReference
<ProgressBar
> mProgressBar
= null
;
323 ProgressListener(ProgressBar progressBar
) {
324 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
328 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
329 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
330 if (percent
!= mLastPercent
) {
331 ProgressBar pb
= mProgressBar
.get();
333 pb
.setProgress(percent
);
337 mLastPercent
= percent
;
343 public void setError(boolean error
) {