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";
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
;
72 * Creates an empty details fragment.
74 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
76 public FileDownloadFragment() {
79 mStorageManager
= null
;
80 mProgressListener
= null
;
82 mIgnoreFirstSavedState
= false
;
87 * Creates a details fragment.
89 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
91 * @param fileToDetail An {@link OCFile} to show in the fragment
92 * @param ocAccount An ownCloud account; needed to start downloads
93 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution
95 public FileDownloadFragment(OCFile fileToDetail
, Account ocAccount
, boolean ignoreFirstSavedState
) {
98 mStorageManager
= null
; // we need a context to init this; the container activity is not available yet at this moment
99 mProgressListener
= null
;
101 mIgnoreFirstSavedState
= ignoreFirstSavedState
;
106 public void onCreate(Bundle savedInstanceState
) {
107 super.onCreate(savedInstanceState
);
108 Log
.e(TAG
, "PREVIEW_DOWNLOAD_FRAGMENT ONCREATE " + ((mFile
== null
)?
"(NULL)" : mFile
.getFileName()));
113 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
114 Bundle savedInstanceState
) {
115 super.onCreateView(inflater
, container
, savedInstanceState
);
117 if (savedInstanceState
!= null
) {
118 if (!mIgnoreFirstSavedState
) {
119 mFile
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_FILE
);
120 mAccount
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
);
122 mIgnoreFirstSavedState
= false
;
127 view
= inflater
.inflate(R
.layout
.file_download_fragment
, container
, false
);
130 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.progressBar
);
131 mProgressListener
= new ProgressListener(progressBar
);
133 ((Button
)mView
.findViewById(R
.id
.cancelBtn
)).setOnClickListener(this);
143 public void onAttach(Activity activity
) {
144 super.onAttach(activity
);
145 Log
.e(TAG
, "PREVIEW_DOWNLOAD_FRAGMENT ONATTACH " + ((mFile
== null
)?
" (NULL)":mFile
.getFileName()));
147 mContainerActivity
= (ContainerActivity
) activity
;
149 } catch (ClassCastException e
) {
150 throw new ClassCastException(activity
.toString() + " must implement " + FileFragment
.ContainerActivity
.class.getSimpleName());
159 public void onActivityCreated(Bundle savedInstanceState
) {
160 super.onActivityCreated(savedInstanceState
);
161 Log
.e(TAG
, "PREVIEW_DOWNLOAD_FRAGMENT ONACTIVITYCREATED " + ((mFile
== null
)?
" (NULL)":mFile
.getFileName()));
162 if (mAccount
!= null
) {
163 mStorageManager
= new FileDataStorageManager(mAccount
, getActivity().getApplicationContext().getContentResolver());;
169 public void onSaveInstanceState(Bundle outState
) {
170 super.onSaveInstanceState(outState
);
171 outState
.putParcelable(FileDownloadFragment
.EXTRA_FILE
, mFile
);
172 outState
.putParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
, mAccount
);
176 public void onStart() {
178 Log
.e(TAG
, "FILE_DOWNLOAD_FRAGMENT ONSTART " + mFile
.getFileName());
179 listenForTransferProgress();
183 public void onResume() {
184 Log
.e(TAG
, "PREVIEW_DOWNLOAD_FRAGMENT ONRESUME " + mFile
.getFileName());
190 public void onPause() {
192 Log
.e(TAG
, "PREVIEW_DOWNLOAD_FRAGMENT ONPAUSE " + mFile
.getFileName());
197 public void onStop() {
199 Log
.e(TAG
, "FILE_DOWNLOAD_FRAGMENT ONSTOP " + mFile
.getFileName());
200 leaveTransferProgress();
204 public void onDestroy() {
206 Log
.e(TAG
, "FILE_DOWNLOAD_FRAGMENT ONDESTROY " + mFile
.getFileName());
211 public View
getView() {
213 listenForTransferProgress();
215 return super.getView() == null ? mView
: super.getView();
220 public void onClick(View v
) {
222 case R
.id
.cancelBtn
: {
223 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
224 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
)) {
225 downloaderBinder
.cancel(mAccount
, mFile
);
226 leaveTransferProgress();
227 if (mFile
.isDown()) {
230 setButtonsForRemote();
236 Log
.e(TAG
, "Incorrect view clicked!");
244 public OCFile
getFile(){
250 * Updates the view depending upon the state of the downloading file.
252 * @param transferring When true, the view must be updated assuming that the holded file is
253 * downloading, no matter what the downloaderBinder says.
255 public void updateView(boolean transferring
) {
256 // configure UI for depending upon local state of the file
257 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
258 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
))) {
259 setButtonsForTransferring();
261 } else if (mFile
.isDown()) {
266 setButtonsForRemote();
268 getView().invalidate();
273 * Enables or disables buttons for a file being downloaded
275 private void setButtonsForTransferring() {
276 Button downloadButton
= (Button
) getView().findViewById(R
.id
.cancelBtn
);
277 downloadButton
.setText(R
.string
.common_cancel
);
279 // show the progress bar for the transfer
280 ProgressBar progressBar
= (ProgressBar
)getView().findViewById(R
.id
.progressBar
);
281 progressBar
.setVisibility(View
.VISIBLE
);
282 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
283 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
288 * Enables or disables buttons for a file locally available
290 private void setButtonsForDown() {
291 Button downloadButton
= (Button
) getView().findViewById(R
.id
.cancelBtn
);
292 downloadButton
.setVisibility(View
.GONE
);
294 // hides the progress bar
295 ProgressBar progressBar
= (ProgressBar
)getView().findViewById(R
.id
.progressBar
);
296 progressBar
.setVisibility(View
.GONE
);
298 // updates the text message
299 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
300 progressText
.setText(R
.string
.common_loading
);
305 * Enables or disables buttons for a file not locally available
307 private void setButtonsForRemote() {
308 Button downloadButton
= (Button
) getView().findViewById(R
.id
.cancelBtn
);
309 downloadButton
.setVisibility(View
.GONE
);
310 //downloadButton.setText(R.string.filedetails_download);
312 // hides the progress bar
313 ProgressBar progressBar
= (ProgressBar
)getView().findViewById(R
.id
.progressBar
);
314 progressBar
.setVisibility(View
.GONE
);
316 // updates the text message
317 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
318 progressText
.setText(R
.string
.downloader_not_downloaded_yet
);
322 public void listenForTransferProgress() {
323 if (mProgressListener
!= null
&& !mListening
) {
324 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
325 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, mFile
);
327 setButtonsForTransferring();
333 public void leaveTransferProgress() {
334 if (mProgressListener
!= null
) {
335 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
336 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, mFile
);
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
;