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
);
112 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
113 Bundle savedInstanceState
) {
114 super.onCreateView(inflater
, container
, savedInstanceState
);
116 if (savedInstanceState
!= null
) {
117 if (!mIgnoreFirstSavedState
) {
118 mFile
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_FILE
);
119 mAccount
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
);
121 mIgnoreFirstSavedState
= false
;
126 view
= inflater
.inflate(R
.layout
.file_download_fragment
, container
, false
);
129 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.progressBar
);
130 mProgressListener
= new ProgressListener(progressBar
);
132 ((Button
)mView
.findViewById(R
.id
.cancelBtn
)).setOnClickListener(this);
142 public void onAttach(Activity activity
) {
143 super.onAttach(activity
);
145 mContainerActivity
= (ContainerActivity
) activity
;
147 } catch (ClassCastException e
) {
148 throw new ClassCastException(activity
.toString() + " must implement " + FileFragment
.ContainerActivity
.class.getSimpleName());
157 public void onActivityCreated(Bundle savedInstanceState
) {
158 super.onActivityCreated(savedInstanceState
);
159 if (mAccount
!= null
) {
160 mStorageManager
= new FileDataStorageManager(mAccount
, getActivity().getApplicationContext().getContentResolver());;
166 public void onSaveInstanceState(Bundle outState
) {
167 super.onSaveInstanceState(outState
);
168 outState
.putParcelable(FileDownloadFragment
.EXTRA_FILE
, mFile
);
169 outState
.putParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
, mAccount
);
173 public void onStart() {
175 listenForTransferProgress();
179 public void onResume() {
185 public void onPause() {
191 public void onStop() {
193 leaveTransferProgress();
197 public void onDestroy() {
203 public View
getView() {
205 listenForTransferProgress();
207 return super.getView() == null ? mView
: super.getView();
212 public void onClick(View v
) {
214 case R
.id
.cancelBtn
: {
215 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
216 if (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
)) {
217 downloaderBinder
.cancel(mAccount
, mFile
);
218 getActivity().finish(); // :)
220 leaveTransferProgress();
221 if (mFile.isDown()) {
224 setButtonsForRemote();
231 Log
.e(TAG
, "Incorrect view clicked!");
239 public OCFile
getFile(){
245 * Updates the view depending upon the state of the downloading file.
247 * @param transferring When true, the view must be updated assuming that the holded file is
248 * downloading, no matter what the downloaderBinder says.
250 public void updateView(boolean transferring
) {
251 // configure UI for depending upon local state of the file
252 FileDownloaderBinder downloaderBinder
= mContainerActivity
.getFileDownloaderBinder();
253 if (transferring
|| (downloaderBinder
!= null
&& downloaderBinder
.isDownloading(mAccount
, mFile
))) {
254 setButtonsForTransferring();
256 } else if (mFile
.isDown()) {
261 setButtonsForRemote();
263 getView().invalidate();
268 * Enables or disables buttons for a file being downloaded
270 private void setButtonsForTransferring() {
271 Button downloadButton
= (Button
) getView().findViewById(R
.id
.cancelBtn
);
272 downloadButton
.setText(R
.string
.common_cancel
);
274 // show the progress bar for the transfer
275 ProgressBar progressBar
= (ProgressBar
)getView().findViewById(R
.id
.progressBar
);
276 progressBar
.setVisibility(View
.VISIBLE
);
277 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
278 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
283 * Enables or disables buttons for a file locally available
285 private void setButtonsForDown() {
286 Button downloadButton
= (Button
) getView().findViewById(R
.id
.cancelBtn
);
287 downloadButton
.setVisibility(View
.GONE
);
289 // hides the progress bar
290 ProgressBar progressBar
= (ProgressBar
)getView().findViewById(R
.id
.progressBar
);
291 progressBar
.setVisibility(View
.GONE
);
293 // updates the text message
294 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
295 progressText
.setText(R
.string
.common_loading
);
300 * Enables or disables buttons for a file not locally available
302 private void setButtonsForRemote() {
303 Button downloadButton
= (Button
) getView().findViewById(R
.id
.cancelBtn
);
304 downloadButton
.setVisibility(View
.GONE
);
305 //downloadButton.setText(R.string.filedetails_download);
307 // hides the progress bar
308 ProgressBar progressBar
= (ProgressBar
)getView().findViewById(R
.id
.progressBar
);
309 progressBar
.setVisibility(View
.GONE
);
311 // updates the text message
312 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
313 progressText
.setText(R
.string
.downloader_not_downloaded_yet
);
317 public void listenForTransferProgress() {
318 if (mProgressListener
!= null
&& !mListening
) {
319 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
320 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, mFile
);
322 setButtonsForTransferring();
328 public void leaveTransferProgress() {
329 if (mProgressListener
!= null
) {
330 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
331 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, mFile
);
339 * Helper class responsible for updating the progress bar shown for file uploading or downloading
341 * @author David A. Velasco
343 private class ProgressListener
implements OnDatatransferProgressListener
{
344 int mLastPercent
= 0;
345 WeakReference
<ProgressBar
> mProgressBar
= null
;
347 ProgressListener(ProgressBar progressBar
) {
348 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
352 public void onTransferProgress(long progressRate
) {
353 // old method, nothing here
357 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
358 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
359 if (percent
!= mLastPercent
) {
360 ProgressBar pb
= mProgressBar
.get();
362 pb
.setProgress(percent
);
366 mLastPercent
= percent
;