2  *   ownCloud Android client application 
   4  *   @author David A. Velasco 
   5  *   Copyright (C) 2015  ownCloud Inc. 
   7  *   This program is free software: you can redistribute it and/or modify 
   8  *   it under the terms of the GNU General Public License version 2, 
   9  *   as published by the Free Software Foundation. 
  11  *   This program is distributed in the hope that it will be useful, 
  12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  14  *   GNU General Public License for more details. 
  16  *   You should have received a copy of the GNU General Public License 
  17  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  20 package com
.owncloud
.android
.ui
.preview
; 
  22 import java
.lang
.ref
.WeakReference
; 
  24 import com
.owncloud
.android
.R
; 
  25 import com
.owncloud
.android
.datamodel
.OCFile
; 
  26 import com
.owncloud
.android
.ui
.fragment
.FileFragment
; 
  28 import android
.accounts
.Account
; 
  29 import android
.os
.Bundle
; 
  30 import android
.support
.v4
.app
.Fragment
; 
  31 import android
.support
.v4
.app
.FragmentStatePagerAdapter
; 
  32 import android
.view
.LayoutInflater
; 
  33 import android
.view
.View
; 
  34 import android
.view
.View
.OnClickListener
; 
  35 import android
.view
.ViewGroup
; 
  36 import android
.widget
.ProgressBar
; 
  37 import android
.widget
.TextView
; 
  39 import com
.owncloud
.android
.lib
.common
.network
.OnDatatransferProgressListener
; 
  40 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
; 
  41 import com
.owncloud
.android
.utils
.DisplayUtils
; 
  45  * This Fragment is used to monitor the progress of a file downloading. 
  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"; 
  53     private static final String ARG_FILE 
= "FILE"; 
  54     private static final String ARG_IGNORE_FIRST 
= "IGNORE_FIRST"; 
  55     private static final String ARG_ACCOUNT 
= "ACCOUNT" ; 
  58     private Account mAccount
; 
  60     public ProgressListener mProgressListener
; 
  61     private boolean mListening
; 
  63     private static final String TAG 
= FileDownloadFragment
.class.getSimpleName(); 
  65     private boolean mIgnoreFirstSavedState
; 
  66     private boolean mError
; 
  70      * Public factory method to create a new fragment that shows the progress of a file download. 
  72      * Android strongly recommends keep the empty constructor of fragments as the only public constructor, and 
  73      * use {@link #setArguments(Bundle)} to set the needed arguments. 
  75      * This method hides to client objects the need of doing the construction in two steps. 
  77      * When 'file' is null creates a dummy layout (useful when a file wasn't tapped before). 
  79      * @param file                      An {@link OCFile} to show in the fragment 
  80      * @param account                   An OC account; needed to start downloads 
  81      * @param ignoreFirstSavedState     Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter} 
  82      *                                  TODO better solution 
  84     public static Fragment 
newInstance(OCFile file
, Account account
, boolean ignoreFirstSavedState
) { 
  85         FileDownloadFragment frag 
= new FileDownloadFragment(); 
  86         Bundle args 
= new Bundle(); 
  87         args
.putParcelable(ARG_FILE
, file
); 
  88         args
.putParcelable(ARG_ACCOUNT
, account
); 
  89         args
.putBoolean(ARG_IGNORE_FIRST
, ignoreFirstSavedState
); 
  90         frag
.setArguments(args
); 
  96      * Creates an empty details fragment. 
  98      * It's necessary to keep a public constructor without parameters; the system uses it when tries to 
  99      * reinstantiate a fragment automatically. 
 101     public FileDownloadFragment() { 
 104         mProgressListener 
= null
; 
 106         mIgnoreFirstSavedState 
= false
; 
 112     public void onCreate(Bundle savedInstanceState
) { 
 113         super.onCreate(savedInstanceState
); 
 114         Bundle args 
= getArguments(); 
 115         setFile((OCFile
)args
.getParcelable(ARG_FILE
)); 
 116             // TODO better in super, but needs to check ALL the class extending FileFragment; not right now 
 118         mIgnoreFirstSavedState 
= args
.getBoolean(ARG_IGNORE_FIRST
); 
 119         mAccount 
= args
.getParcelable(ARG_ACCOUNT
); 
 124     public View 
onCreateView(LayoutInflater inflater
, ViewGroup container
, 
 125                              Bundle savedInstanceState
) { 
 126         super.onCreateView(inflater
, container
, savedInstanceState
); 
 128         if (savedInstanceState 
!= null
) { 
 129             if (!mIgnoreFirstSavedState
) { 
 130                 setFile((OCFile
) savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_FILE
)); 
 131                 mAccount 
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
); 
 132                 mError 
= savedInstanceState
.getBoolean(FileDownloadFragment
.EXTRA_ERROR
); 
 135                 mIgnoreFirstSavedState 
= false
; 
 139         mView 
= inflater
.inflate(R
.layout
.file_download_fragment
, container
, false
); 
 141         ProgressBar progressBar 
= (ProgressBar
)mView
.findViewById(R
.id
.progressBar
); 
 142         DisplayUtils
.colorPreLollipopHorizontalProgressBar(progressBar
); 
 143         mProgressListener 
= new ProgressListener(progressBar
); 
 145         (mView
.findViewById(R
.id
.cancelBtn
)).setOnClickListener(this); 
 147         (mView
.findViewById(R
.id
.fileDownloadLL
)).setOnClickListener(new OnClickListener() { 
 149             public void onClick(View v
) { 
 150                 ((PreviewImageActivity
) getActivity()).toggleFullScreen(); 
 155             setButtonsForRemote(); 
 158             setButtonsForTransferring(); 
 166     public void onSaveInstanceState(Bundle outState
) { 
 167         super.onSaveInstanceState(outState
); 
 168         outState
.putParcelable(FileDownloadFragment
.EXTRA_FILE
, getFile()); 
 169         outState
.putParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
, mAccount
); 
 170         outState
.putBoolean(FileDownloadFragment
.EXTRA_ERROR
, mError
); 
 174     public void onStart() { 
 176         listenForTransferProgress(); 
 180     public void onResume() { 
 186     public void onPause() { 
 192     public void onStop() { 
 193         leaveTransferProgress(); 
 198     public void onDestroy() { 
 204     public View 
getView() { 
 206             listenForTransferProgress(); 
 208         return super.getView() == null ? mView 
: super.getView(); 
 213     public void onClick(View v
) { 
 215             case R
.id
.cancelBtn
: { 
 216                 mContainerActivity
.getFileOperationsHelper().cancelTransference(getFile()); 
 217                 getActivity().finish(); 
 221                 Log_OC
.e(TAG
, "Incorrect view clicked!"); 
 227      * Enables or disables buttons for a file being downloaded 
 229     private void setButtonsForTransferring() { 
 230         getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.VISIBLE
); 
 232         // show the progress bar for the transfer 
 233         getView().findViewById(R
.id
.progressBar
).setVisibility(View
.VISIBLE
); 
 234         TextView progressText 
= (TextView
) getView().findViewById(R
.id
.progressText
); 
 235         progressText
.setText(R
.string
.downloader_download_in_progress_ticker
); 
 236         progressText
.setVisibility(View
.VISIBLE
); 
 238         // hides the error icon 
 239         getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
); 
 240         getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
); 
 244      * Enables or disables buttons for a file locally available 
 246     private void setButtonsForDown() { 
 247         getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
); 
 249         // hides the progress bar 
 250         getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
); 
 252         // updates the text message 
 253         TextView progressText 
= (TextView
) getView().findViewById(R
.id
.progressText
); 
 254         progressText
.setText(R
.string
.common_loading
); 
 255         progressText
.setVisibility(View
.VISIBLE
); 
 257         // hides the error icon 
 258         getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
); 
 259         getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
); 
 264      * Enables or disables buttons for a file not locally available 
 266      * Currently, this is only used when a download was failed 
 268     private void setButtonsForRemote() { 
 269         getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
); 
 271         // hides the progress bar and message 
 272         getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
); 
 273         getView().findViewById(R
.id
.progressText
).setVisibility(View
.GONE
); 
 275         // shows the error icon and message 
 276         getView().findViewById(R
.id
.errorText
).setVisibility(View
.VISIBLE
); 
 277         getView().findViewById(R
.id
.error_image
).setVisibility(View
.VISIBLE
); 
 281     public void listenForTransferProgress() { 
 282         if (mProgressListener 
!= null 
&& !mListening
) { 
 283             if (mContainerActivity
.getFileDownloaderBinder() != null
) { 
 284                 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener( 
 285                         mProgressListener
, mAccount
, getFile() 
 288                 setButtonsForTransferring(); 
 294     public void leaveTransferProgress() { 
 295         if (mProgressListener 
!= null
) { 
 296             if (mContainerActivity
.getFileDownloaderBinder() != null
) { 
 297                 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener( 
 298                         mProgressListener
, mAccount
, getFile() 
 307      * Helper class responsible for updating the progress bar shown for file uploading or downloading 
 309     private class ProgressListener 
implements OnDatatransferProgressListener 
{ 
 310         int mLastPercent 
= 0; 
 311         WeakReference
<ProgressBar
> mProgressBar 
= null
; 
 313         ProgressListener(ProgressBar progressBar
) { 
 314             mProgressBar 
= new WeakReference
<ProgressBar
>(progressBar
); 
 318         public void onTransferProgress( 
 319                 long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
 
 321             int percent 
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
)); 
 322             if (percent 
!= mLastPercent
) { 
 323                 ProgressBar pb 
= mProgressBar
.get(); 
 325                     pb
.setProgress(percent
); 
 329             mLastPercent 
= percent
; 
 335     public void setError(boolean error
) {