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 android
.accounts
.Account
;
23 import android
.app
.Activity
;
24 import android
.os
.Bundle
;
25 import android
.support
.v4
.app
.FragmentStatePagerAdapter
;
26 import android
.view
.LayoutInflater
;
27 import android
.view
.View
;
28 import android
.view
.View
.OnClickListener
;
29 import android
.view
.ViewGroup
;
30 import android
.widget
.Button
;
31 import android
.widget
.ProgressBar
;
32 import android
.widget
.TextView
;
34 import com
.actionbarsherlock
.app
.SherlockFragment
;
35 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
36 import com
.owncloud
.android
.datamodel
.OCFile
;
37 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
38 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
40 import com
.owncloud
.android
.Log_OC
;
41 import com
.owncloud
.android
.R
;
43 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
46 * This Fragment is used to monitor the progress of a file downloading.
48 * @author David A. Velasco
50 public class FileDownloadFragment
extends SherlockFragment
implements OnClickListener
, FileFragment
{
52 public static final String EXTRA_FILE
= "FILE";
53 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
54 private static final String EXTRA_ERROR
= "ERROR";
56 private FileFragment
.ContainerActivity mContainerActivity
;
60 private Account mAccount
;
61 private FileDataStorageManager mStorageManager
;
63 public ProgressListener mProgressListener
;
64 private boolean mListening
;
66 private static final String TAG
= FileDownloadFragment
.class.getSimpleName();
68 private boolean mIgnoreFirstSavedState
;
69 private boolean mError
;
73 * Creates an empty details fragment.
75 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
77 public FileDownloadFragment() {
80 mStorageManager
= null
;
81 mProgressListener
= null
;
83 mIgnoreFirstSavedState
= false
;
89 * Creates a details fragment.
91 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
93 * @param fileToDetail An {@link OCFile} to show in the fragment
94 * @param ocAccount An ownCloud account; needed to start downloads
95 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution
97 public FileDownloadFragment(OCFile fileToDetail
, Account ocAccount
, boolean ignoreFirstSavedState
) {
100 mStorageManager
= null
; // we need a context to init this; the container activity is not available yet at this moment
101 mProgressListener
= null
;
103 mIgnoreFirstSavedState
= ignoreFirstSavedState
;
109 public void onCreate(Bundle savedInstanceState
) {
110 super.onCreate(savedInstanceState
);
115 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
116 Bundle savedInstanceState
) {
117 super.onCreateView(inflater
, container
, savedInstanceState
);
119 if (savedInstanceState
!= null
) {
120 if (!mIgnoreFirstSavedState
) {
121 mFile
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_FILE
);
122 mAccount
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
);
123 mError
= savedInstanceState
.getBoolean(FileDownloadFragment
.EXTRA_ERROR
);
125 mIgnoreFirstSavedState
= false
;
130 view
= inflater
.inflate(R
.layout
.file_download_fragment
, container
, false
);
133 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.progressBar
);
134 mProgressListener
= new ProgressListener(progressBar
);
136 ((Button
)mView
.findViewById(R
.id
.cancelBtn
)).setOnClickListener(this);
139 setButtonsForRemote();
141 setButtonsForTransferring();
152 public void onAttach(Activity activity
) {
153 super.onAttach(activity
);
155 mContainerActivity
= (ContainerActivity
) activity
;
157 } catch (ClassCastException e
) {
158 throw new ClassCastException(activity
.toString() + " must implement " + FileFragment
.ContainerActivity
.class.getSimpleName());
167 public void onActivityCreated(Bundle savedInstanceState
) {
168 super.onActivityCreated(savedInstanceState
);
169 if (mAccount
!= null
) {
170 mStorageManager
= new FileDataStorageManager(mAccount
, getActivity().getApplicationContext().getContentResolver());;
176 public void onSaveInstanceState(Bundle outState
) {
177 super.onSaveInstanceState(outState
);
178 outState
.putParcelable(FileDownloadFragment
.EXTRA_FILE
, mFile
);
179 outState
.putParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
, mAccount
);
180 outState
.putBoolean(FileDownloadFragment
.EXTRA_ERROR
, mError
);
184 public void onStart() {
186 listenForTransferProgress();
190 public void onResume() {
196 public void onPause() {
202 public void onStop() {
204 leaveTransferProgress();
208 public void onDestroy() {
214 public View
getView() {
216 listenForTransferProgress();
218 return super.getView() == null ? mView
: super.getView();
223 public void onClick(View v
) {
225 case R
.id
.cancelBtn
: {
226 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
227 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
)) {
228 downloaderBinder
.cancel(mAccount
, mFile
);
229 getActivity().finish(); // :)
231 leaveTransferProgress();
232 if (mFile.isDown()) {
235 setButtonsForRemote();
242 Log_OC
.e(TAG
, "Incorrect view clicked!");
250 public OCFile
getFile(){
256 * Updates the view depending upon the state of the downloading file.
258 * @param transferring When true, the view must be updated assuming that the holded file is
259 * downloading, no matter what the downloaderBinder says.
261 public void updateView(boolean transferring
) {
262 // configure UI for depending upon local state of the file
263 FileDownloaderBinder downloaderBinder
= (mContainerActivity
== null
) ? null
: mContainerActivity
.getFileDownloaderBinder();
264 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
))) {
265 setButtonsForTransferring();
267 } else if (mFile
.isDown()) {
272 setButtonsForRemote();
274 getView().invalidate();
280 * Enables or disables buttons for a file being downloaded
282 private void setButtonsForTransferring() {
283 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.VISIBLE
);
285 // show the progress bar for the transfer
286 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.VISIBLE
);
287 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
288 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
289 progressText
.setVisibility(View
.VISIBLE
);
291 // hides the error icon
292 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
293 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
298 * Enables or disables buttons for a file locally available
300 private void setButtonsForDown() {
301 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
303 // hides the progress bar
304 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
306 // updates the text message
307 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
308 progressText
.setText(R
.string
.common_loading
);
309 progressText
.setVisibility(View
.VISIBLE
);
311 // hides the error icon
312 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
313 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
318 * Enables or disables buttons for a file not locally available
320 * Currently, this is only used when a download was failed
322 private void setButtonsForRemote() {
323 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
325 // hides the progress bar and message
326 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
327 getView().findViewById(R
.id
.progressText
).setVisibility(View
.GONE
);
329 // shows the error icon and message
330 getView().findViewById(R
.id
.errorText
).setVisibility(View
.VISIBLE
);
331 getView().findViewById(R
.id
.error_image
).setVisibility(View
.VISIBLE
);
335 public void listenForTransferProgress() {
336 if (mProgressListener
!= null
&& !mListening
) {
337 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
338 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, mFile
);
340 setButtonsForTransferring();
346 public void leaveTransferProgress() {
347 if (mProgressListener
!= null
) {
348 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
349 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, mFile
);
357 * Helper class responsible for updating the progress bar shown for file uploading or downloading
359 * @author David A. Velasco
361 private class ProgressListener
implements OnDatatransferProgressListener
{
362 int mLastPercent
= 0;
363 WeakReference
<ProgressBar
> mProgressBar
= null
;
365 ProgressListener(ProgressBar progressBar
) {
366 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
370 public void onTransferProgress(long progressRate
) {
371 // old method, nothing here
375 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
376 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
377 if (percent
!= mLastPercent
) {
378 ProgressBar pb
= mProgressBar
.get();
380 pb
.setProgress(percent
);
384 mLastPercent
= percent
;
390 public void setError(boolean error
) {