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
.os
.Bundle
; 
  30 import android
.support
.v4
.app
.FragmentStatePagerAdapter
; 
  31 import android
.view
.LayoutInflater
; 
  32 import android
.view
.View
; 
  33 import android
.view
.View
.OnClickListener
; 
  34 import android
.view
.ViewGroup
; 
  35 import android
.widget
.ImageButton
; 
  36 import android
.widget
.ProgressBar
; 
  37 import android
.widget
.TextView
; 
  39 import com
.owncloud
.android
.lib
.common
.network
.OnDatatransferProgressListener
; 
  43  * This Fragment is used to monitor the progress of a file downloading. 
  45  * @author David A. Velasco 
  47 public class FileDownloadFragment 
extends FileFragment 
implements OnClickListener 
{ 
  49     public static final String EXTRA_FILE 
= "FILE"; 
  50     public static final String EXTRA_ACCOUNT 
= "ACCOUNT"; 
  51     private static final String EXTRA_ERROR 
= "ERROR"; 
  54     private Account mAccount
; 
  56     public ProgressListener mProgressListener
; 
  57     private boolean mListening
; 
  59     private static final String TAG 
= FileDownloadFragment
.class.getSimpleName(); 
  61     private boolean mIgnoreFirstSavedState
; 
  62     private boolean mError
; 
  66      * Creates an empty details fragment. 
  68      * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.  
  70     public FileDownloadFragment() { 
  73         mProgressListener 
= null
; 
  75         mIgnoreFirstSavedState 
= false
; 
  81      * Creates a details fragment. 
  83      * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before). 
  85      * @param fileToDetail      An {@link OCFile} to show in the fragment 
  86      * @param ocAccount         An ownCloud account; needed to start downloads 
  87      * @param ignoreFirstSavedState     Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution  
  89     public FileDownloadFragment(OCFile fileToDetail
, Account ocAccount
, boolean ignoreFirstSavedState
) { 
  92         mProgressListener 
= null
; 
  94         mIgnoreFirstSavedState 
= ignoreFirstSavedState
; 
 100     public void onCreate(Bundle savedInstanceState
) { 
 101         super.onCreate(savedInstanceState
); 
 106     public View 
onCreateView(LayoutInflater inflater
, ViewGroup container
, 
 107             Bundle savedInstanceState
) { 
 108         super.onCreateView(inflater
, container
, savedInstanceState
); 
 110         if (savedInstanceState 
!= null
) { 
 111             if (!mIgnoreFirstSavedState
) { 
 112                 setFile((OCFile
)savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_FILE
)); 
 113                 mAccount 
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
); 
 114                 mError 
= savedInstanceState
.getBoolean(FileDownloadFragment
.EXTRA_ERROR
); 
 116                 mIgnoreFirstSavedState 
= false
; 
 121         view 
= inflater
.inflate(R
.layout
.file_download_fragment
, container
, false
); 
 124         ProgressBar progressBar 
= (ProgressBar
)mView
.findViewById(R
.id
.progressBar
); 
 125         mProgressListener 
= new ProgressListener(progressBar
); 
 127         ((ImageButton
)mView
.findViewById(R
.id
.cancelBtn
)).setOnClickListener(this); 
 130             setButtonsForRemote(); 
 132             setButtonsForTransferring(); 
 140     public void onSaveInstanceState(Bundle outState
) { 
 141         super.onSaveInstanceState(outState
); 
 142         outState
.putParcelable(FileDownloadFragment
.EXTRA_FILE
, getFile()); 
 143         outState
.putParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
, mAccount
); 
 144         outState
.putBoolean(FileDownloadFragment
.EXTRA_ERROR
, mError
); 
 148     public void onStart() { 
 150         listenForTransferProgress(); 
 154     public void onResume() { 
 160     public void onPause() { 
 166     public void onStop() { 
 167         leaveTransferProgress(); 
 172     public void onDestroy() { 
 178     public View 
getView() { 
 180             listenForTransferProgress(); 
 182         return super.getView() == null ? mView 
: super.getView(); 
 187     public void onClick(View v
) { 
 189             case R
.id
.cancelBtn
: { 
 190                 mContainerActivity
.getFileOperationsHelper().cancelTransference(getFile()); 
 191                 getActivity().finish(); 
 195                 Log_OC
.e(TAG
, "Incorrect view clicked!"); 
 201      * Updates the view depending upon the state of the downloading file. 
 203      * @param   transferring    When true, the view must be updated assuming that the holded file is  
 204      *                          downloading, no matter what the downloaderBinder says. 
 206     public void updateView(boolean transferring
) { 
 207         // configure UI for depending upon local state of the file 
 208         FileDownloaderBinder downloaderBinder 
= (mContainerActivity 
== null
) ? null 
: mContainerActivity
.getFileDownloaderBinder(); 
 209         if (transferring 
|| (downloaderBinder 
!= null 
&& downloaderBinder
.isDownloading(mAccount
, getFile()))) { 
 210             setButtonsForTransferring(); 
 212         } else if (getFile().isDown()) { 
 217             setButtonsForRemote(); 
 219         getView().invalidate(); 
 225      * Enables or disables buttons for a file being downloaded 
 227     private void setButtonsForTransferring() { 
 228         getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.VISIBLE
); 
 230         // show the progress bar for the transfer 
 231         getView().findViewById(R
.id
.progressBar
).setVisibility(View
.VISIBLE
); 
 232         TextView progressText 
= (TextView
)getView().findViewById(R
.id
.progressText
); 
 233         progressText
.setText(R
.string
.downloader_download_in_progress_ticker
); 
 234         progressText
.setVisibility(View
.VISIBLE
); 
 236         // hides the error icon 
 237         getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
); 
 238         getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
); 
 243      * Enables or disables buttons for a file locally available  
 245     private void setButtonsForDown() { 
 246         getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
); 
 248         // hides the progress bar 
 249         getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
); 
 251         // updates the text message 
 252         TextView progressText 
= (TextView
)getView().findViewById(R
.id
.progressText
); 
 253         progressText
.setText(R
.string
.common_loading
); 
 254         progressText
.setVisibility(View
.VISIBLE
); 
 256         // hides the error icon 
 257         getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
); 
 258         getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
); 
 263      * Enables or disables buttons for a file not locally available  
 265      * Currently, this is only used when a download was failed 
 267     private void setButtonsForRemote() { 
 268         getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
); 
 270         // hides the progress bar and message 
 271         getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
); 
 272         getView().findViewById(R
.id
.progressText
).setVisibility(View
.GONE
); 
 274         // shows the error icon and message 
 275         getView().findViewById(R
.id
.errorText
).setVisibility(View
.VISIBLE
); 
 276         getView().findViewById(R
.id
.error_image
).setVisibility(View
.VISIBLE
); 
 280     public void listenForTransferProgress() { 
 281         if (mProgressListener 
!= null 
&& !mListening
) { 
 282             if (mContainerActivity
.getFileDownloaderBinder() != null
) { 
 283                 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile()); 
 285                 setButtonsForTransferring(); 
 291     public void leaveTransferProgress() { 
 292         if (mProgressListener 
!= null
) { 
 293             if (mContainerActivity
.getFileDownloaderBinder() != null
) { 
 294                 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile()); 
 302      * Helper class responsible for updating the progress bar shown for file uploading or downloading   
 304      * @author David A. Velasco 
 306     private class ProgressListener 
implements OnDatatransferProgressListener 
{ 
 307         int mLastPercent 
= 0; 
 308         WeakReference
<ProgressBar
> mProgressBar 
= null
; 
 310         ProgressListener(ProgressBar progressBar
) { 
 311             mProgressBar 
= new WeakReference
<ProgressBar
>(progressBar
); 
 315         public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) { 
 316             int percent 
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
)); 
 317             if (percent 
!= mLastPercent
) { 
 318                 ProgressBar pb 
= mProgressBar
.get(); 
 320                     pb
.setProgress(percent
); 
 324             mLastPercent 
= percent
; 
 330     public void setError(boolean error
) {