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
.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
;
40 import com
.owncloud
.android
.lib
.common
.network
.OnDatatransferProgressListener
;
44 * This Fragment is used to monitor the progress of a file downloading.
46 * @author David A. Velasco
48 public class FileDownloadFragment
extends FileFragment
implements OnClickListener
{
50 public static final String EXTRA_FILE
= "FILE";
51 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
52 private static final String EXTRA_ERROR
= "ERROR";
54 private FileFragment
.ContainerActivity mContainerActivity
;
57 private Account mAccount
;
59 public ProgressListener mProgressListener
;
60 private boolean mListening
;
62 private static final String TAG
= FileDownloadFragment
.class.getSimpleName();
64 private boolean mIgnoreFirstSavedState
;
65 private boolean mError
;
69 * Creates an empty details fragment.
71 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
73 public FileDownloadFragment() {
76 mProgressListener
= null
;
78 mIgnoreFirstSavedState
= false
;
84 * Creates a details fragment.
86 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
88 * @param fileToDetail An {@link OCFile} to show in the fragment
89 * @param ocAccount An ownCloud account; needed to start downloads
90 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution
92 public FileDownloadFragment(OCFile fileToDetail
, Account ocAccount
, boolean ignoreFirstSavedState
) {
95 mProgressListener
= null
;
97 mIgnoreFirstSavedState
= ignoreFirstSavedState
;
103 public void onCreate(Bundle savedInstanceState
) {
104 super.onCreate(savedInstanceState
);
109 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
110 Bundle savedInstanceState
) {
111 super.onCreateView(inflater
, container
, savedInstanceState
);
113 if (savedInstanceState
!= null
) {
114 if (!mIgnoreFirstSavedState
) {
115 setFile((OCFile
)savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_FILE
));
116 mAccount
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
);
117 mError
= savedInstanceState
.getBoolean(FileDownloadFragment
.EXTRA_ERROR
);
119 mIgnoreFirstSavedState
= false
;
124 view
= inflater
.inflate(R
.layout
.file_download_fragment
, container
, false
);
127 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.progressBar
);
128 mProgressListener
= new ProgressListener(progressBar
);
130 ((ImageButton
)mView
.findViewById(R
.id
.cancelBtn
)).setOnClickListener(this);
133 setButtonsForRemote();
135 setButtonsForTransferring();
146 public void onAttach(Activity activity
) {
147 super.onAttach(activity
);
149 mContainerActivity
= (ContainerActivity
) activity
;
151 } catch (ClassCastException e
) {
152 throw new ClassCastException(activity
.toString() + " must implement " + FileFragment
.ContainerActivity
.class.getSimpleName());
158 public void onSaveInstanceState(Bundle outState
) {
159 super.onSaveInstanceState(outState
);
160 outState
.putParcelable(FileDownloadFragment
.EXTRA_FILE
, getFile());
161 outState
.putParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
, mAccount
);
162 outState
.putBoolean(FileDownloadFragment
.EXTRA_ERROR
, mError
);
166 public void onStart() {
168 listenForTransferProgress();
172 public void onResume() {
178 public void onPause() {
184 public void onStop() {
186 leaveTransferProgress();
190 public void onDestroy() {
196 public View
getView() {
198 listenForTransferProgress();
200 return super.getView() == null ? mView
: super.getView();
205 public void onClick(View v
) {
207 case R
.id
.cancelBtn
: {
208 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
209 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile())) {
210 downloaderBinder
.cancel(mAccount
, getFile());
211 getActivity().finish(); // :)
213 leaveTransferProgress();
214 if (mFile.isDown()) {
217 setButtonsForRemote();
224 Log_OC
.e(TAG
, "Incorrect view clicked!");
230 * Updates the view depending upon the state of the downloading file.
232 * @param transferring When true, the view must be updated assuming that the holded file is
233 * downloading, no matter what the downloaderBinder says.
235 public void updateView(boolean transferring
) {
236 // configure UI for depending upon local state of the file
237 FileDownloaderBinder downloaderBinder
= (mContainerActivity
== null
) ? null
: mContainerActivity
.getFileDownloaderBinder();
238 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, getFile()))) {
239 setButtonsForTransferring();
241 } else if (getFile().isDown()) {
246 setButtonsForRemote();
248 getView().invalidate();
254 * Enables or disables buttons for a file being downloaded
256 private void setButtonsForTransferring() {
257 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.VISIBLE
);
259 // show the progress bar for the transfer
260 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.VISIBLE
);
261 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
262 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
263 progressText
.setVisibility(View
.VISIBLE
);
265 // hides the error icon
266 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
267 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
272 * Enables or disables buttons for a file locally available
274 private void setButtonsForDown() {
275 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
277 // hides the progress bar
278 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
280 // updates the text message
281 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
282 progressText
.setText(R
.string
.common_loading
);
283 progressText
.setVisibility(View
.VISIBLE
);
285 // hides the error icon
286 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
287 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
292 * Enables or disables buttons for a file not locally available
294 * Currently, this is only used when a download was failed
296 private void setButtonsForRemote() {
297 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
299 // hides the progress bar and message
300 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
301 getView().findViewById(R
.id
.progressText
).setVisibility(View
.GONE
);
303 // shows the error icon and message
304 getView().findViewById(R
.id
.errorText
).setVisibility(View
.VISIBLE
);
305 getView().findViewById(R
.id
.error_image
).setVisibility(View
.VISIBLE
);
309 public void listenForTransferProgress() {
310 if (mProgressListener
!= null
&& !mListening
) {
311 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
312 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
314 setButtonsForTransferring();
320 public void leaveTransferProgress() {
321 if (mProgressListener
!= null
) {
322 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
323 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
331 * Helper class responsible for updating the progress bar shown for file uploading or downloading
333 * @author David A. Velasco
335 private class ProgressListener
implements OnDatatransferProgressListener
{
336 int mLastPercent
= 0;
337 WeakReference
<ProgressBar
> mProgressBar
= null
;
339 ProgressListener(ProgressBar progressBar
) {
340 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
344 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
345 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
346 if (percent
!= mLastPercent
) {
347 ProgressBar pb
= mProgressBar
.get();
349 pb
.setProgress(percent
);
353 mLastPercent
= percent
;
359 public void setError(boolean error
) {