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 as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
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.
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/>.
19 package com
.owncloud
.android
.ui
.preview
;
21 import java
.lang
.ref
.WeakReference
;
23 import android
.accounts
.Account
;
24 import android
.app
.Activity
;
25 import android
.os
.Bundle
;
26 import android
.support
.v4
.app
.FragmentStatePagerAdapter
;
27 import android
.util
.Log
;
28 import android
.view
.LayoutInflater
;
29 import android
.view
.View
;
30 import android
.view
.View
.OnClickListener
;
31 import android
.view
.ViewGroup
;
32 import android
.widget
.Button
;
33 import android
.widget
.ProgressBar
;
34 import android
.widget
.TextView
;
36 import com
.actionbarsherlock
.app
.SherlockFragment
;
37 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
38 import com
.owncloud
.android
.datamodel
.OCFile
;
39 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
40 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
42 import com
.owncloud
.android
.R
;
44 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
47 * This Fragment is used to monitor the progress of a file downloading.
49 * @author David A. Velasco
51 public class FileDownloadFragment
extends SherlockFragment
implements OnClickListener
, FileFragment
{
53 public static final String EXTRA_FILE
= "FILE";
54 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
55 private static final String EXTRA_ERROR
= "ERROR";
57 private FileFragment
.ContainerActivity mContainerActivity
;
61 private Account mAccount
;
62 private FileDataStorageManager mStorageManager
;
64 public ProgressListener mProgressListener
;
65 private boolean mListening
;
67 private static final String TAG
= FileDownloadFragment
.class.getSimpleName();
69 private boolean mIgnoreFirstSavedState
;
70 private boolean mError
;
74 * Creates an empty details fragment.
76 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
78 public FileDownloadFragment() {
81 mStorageManager
= null
;
82 mProgressListener
= null
;
84 mIgnoreFirstSavedState
= false
;
90 * Creates a details fragment.
92 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
94 * @param fileToDetail An {@link OCFile} to show in the fragment
95 * @param ocAccount An ownCloud account; needed to start downloads
96 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution
98 public FileDownloadFragment(OCFile fileToDetail
, Account ocAccount
, boolean ignoreFirstSavedState
) {
100 mAccount
= ocAccount
;
101 mStorageManager
= null
; // we need a context to init this; the container activity is not available yet at this moment
102 mProgressListener
= null
;
104 mIgnoreFirstSavedState
= ignoreFirstSavedState
;
110 public void onCreate(Bundle savedInstanceState
) {
111 super.onCreate(savedInstanceState
);
116 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
117 Bundle savedInstanceState
) {
118 super.onCreateView(inflater
, container
, savedInstanceState
);
120 if (savedInstanceState
!= null
) {
121 if (!mIgnoreFirstSavedState
) {
122 mFile
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_FILE
);
123 mAccount
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
);
124 mError
= savedInstanceState
.getBoolean(FileDownloadFragment
.EXTRA_ERROR
);
126 mIgnoreFirstSavedState
= false
;
131 view
= inflater
.inflate(R
.layout
.file_download_fragment
, container
, false
);
134 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.progressBar
);
135 mProgressListener
= new ProgressListener(progressBar
);
137 ((Button
)mView
.findViewById(R
.id
.cancelBtn
)).setOnClickListener(this);
140 setButtonsForRemote();
142 setButtonsForTransferring();
153 public void onAttach(Activity activity
) {
154 super.onAttach(activity
);
156 mContainerActivity
= (ContainerActivity
) activity
;
158 } catch (ClassCastException e
) {
159 throw new ClassCastException(activity
.toString() + " must implement " + FileFragment
.ContainerActivity
.class.getSimpleName());
168 public void onActivityCreated(Bundle savedInstanceState
) {
169 super.onActivityCreated(savedInstanceState
);
170 if (mAccount
!= null
) {
171 mStorageManager
= new FileDataStorageManager(mAccount
, getActivity().getApplicationContext().getContentResolver());;
177 public void onSaveInstanceState(Bundle outState
) {
178 super.onSaveInstanceState(outState
);
179 outState
.putParcelable(FileDownloadFragment
.EXTRA_FILE
, mFile
);
180 outState
.putParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
, mAccount
);
181 outState
.putBoolean(FileDownloadFragment
.EXTRA_ERROR
, mError
);
185 public void onStart() {
187 listenForTransferProgress();
191 public void onResume() {
197 public void onPause() {
203 public void onStop() {
205 leaveTransferProgress();
209 public void onDestroy() {
215 public View
getView() {
217 listenForTransferProgress();
219 return super.getView() == null ? mView
: super.getView();
224 public void onClick(View v
) {
226 case R
.id
.cancelBtn
: {
227 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
228 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
)) {
229 downloaderBinder
.cancel(mAccount
, mFile
);
230 getActivity().finish(); // :)
232 leaveTransferProgress();
233 if (mFile.isDown()) {
236 setButtonsForRemote();
243 Log
.e(TAG
, "Incorrect view clicked!");
251 public OCFile
getFile(){
257 * Updates the view depending upon the state of the downloading file.
259 * @param transferring When true, the view must be updated assuming that the holded file is
260 * downloading, no matter what the downloaderBinder says.
262 public void updateView(boolean transferring
) {
263 // configure UI for depending upon local state of the file
264 FileDownloaderBinder downloaderBinder
= (mContainerActivity
== null
) ? null
: mContainerActivity
.getFileDownloaderBinder();
265 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
))) {
266 setButtonsForTransferring();
268 } else if (mFile
.isDown()) {
273 setButtonsForRemote();
275 getView().invalidate();
281 * Enables or disables buttons for a file being downloaded
283 private void setButtonsForTransferring() {
284 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.VISIBLE
);
286 // show the progress bar for the transfer
287 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.VISIBLE
);
288 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
289 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
290 progressText
.setVisibility(View
.VISIBLE
);
292 // hides the error icon
293 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
294 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
299 * Enables or disables buttons for a file locally available
301 private void setButtonsForDown() {
302 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
304 // hides the progress bar
305 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
307 // updates the text message
308 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
309 progressText
.setText(R
.string
.common_loading
);
310 progressText
.setVisibility(View
.VISIBLE
);
312 // hides the error icon
313 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
314 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
319 * Enables or disables buttons for a file not locally available
321 * Currently, this is only used when a download was failed
323 private void setButtonsForRemote() {
324 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
326 // hides the progress bar and message
327 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
328 getView().findViewById(R
.id
.progressText
).setVisibility(View
.GONE
);
330 // shows the error icon and message
331 getView().findViewById(R
.id
.errorText
).setVisibility(View
.VISIBLE
);
332 getView().findViewById(R
.id
.error_image
).setVisibility(View
.VISIBLE
);
336 public void listenForTransferProgress() {
337 if (mProgressListener
!= null
&& !mListening
) {
338 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
339 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, mFile
);
341 setButtonsForTransferring();
347 public void leaveTransferProgress() {
348 if (mProgressListener
!= null
) {
349 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
350 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, mFile
);
358 * Helper class responsible for updating the progress bar shown for file uploading or downloading
360 * @author David A. Velasco
362 private class ProgressListener
implements OnDatatransferProgressListener
{
363 int mLastPercent
= 0;
364 WeakReference
<ProgressBar
> mProgressBar
= null
;
366 ProgressListener(ProgressBar progressBar
) {
367 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
371 public void onTransferProgress(long progressRate
) {
372 // old method, nothing here
376 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
377 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
378 if (percent
!= mLastPercent
) {
379 ProgressBar pb
= mProgressBar
.get();
381 pb
.setProgress(percent
);
385 mLastPercent
= percent
;
391 public void setError(boolean error
) {