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
.R
;
40 import com
.owncloud
.android
.datamodel
.OCFile
;
41 import com
.owncloud
.android
.lib
.common
.network
.OnDatatransferProgressListener
;
42 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
43 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
45 import java
.lang
.ref
.WeakReference
;
49 * This Fragment is used to monitor the progress of a file downloading.
51 public class FileDownloadFragment
extends FileFragment
implements OnClickListener
{
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 static final String ARG_FILE
= "FILE";
58 private static final String ARG_IGNORE_FIRST
= "IGNORE_FIRST";
59 private static final String ARG_ACCOUNT
= "ACCOUNT" ;
62 private Account mAccount
;
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 * Public factory method to create a new fragment that shows the progress of a file download.
76 * Android strongly recommends keep the empty constructor of fragments as the only public constructor, and
77 * use {@link #setArguments(Bundle)} to set the needed arguments.
79 * This method hides to client objects the need of doing the construction in two steps.
81 * When 'file' is null creates a dummy layout (useful when a file wasn't tapped before).
83 * @param file An {@link OCFile} to show in the fragment
84 * @param account An OC account; needed to start downloads
85 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}
86 * TODO better solution
88 public static Fragment
newInstance(OCFile file
, Account account
, boolean ignoreFirstSavedState
) {
89 FileDownloadFragment frag
= new FileDownloadFragment();
90 Bundle args
= new Bundle();
91 args
.putParcelable(ARG_FILE
, file
);
92 args
.putParcelable(ARG_ACCOUNT
, account
);
93 args
.putBoolean(ARG_IGNORE_FIRST
, ignoreFirstSavedState
);
94 frag
.setArguments(args
);
100 * Creates an empty details fragment.
102 * It's necessary to keep a public constructor without parameters; the system uses it when tries to
103 * reinstantiate a fragment automatically.
105 public FileDownloadFragment() {
108 mProgressListener
= null
;
110 mIgnoreFirstSavedState
= false
;
116 public void onCreate(Bundle savedInstanceState
) {
117 super.onCreate(savedInstanceState
);
118 Bundle args
= getArguments();
119 setFile((OCFile
)args
.getParcelable(ARG_FILE
));
120 // TODO better in super, but needs to check ALL the class extending FileFragment; not right now
122 mIgnoreFirstSavedState
= args
.getBoolean(ARG_IGNORE_FIRST
);
123 mAccount
= args
.getParcelable(ARG_ACCOUNT
);
128 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
129 Bundle savedInstanceState
) {
130 super.onCreateView(inflater
, container
, savedInstanceState
);
132 if (savedInstanceState
!= null
) {
133 if (!mIgnoreFirstSavedState
) {
134 setFile((OCFile
) savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_FILE
));
135 mAccount
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
);
136 mError
= savedInstanceState
.getBoolean(FileDownloadFragment
.EXTRA_ERROR
);
139 mIgnoreFirstSavedState
= false
;
144 view
= inflater
.inflate(R
.layout
.file_download_fragment
, container
, false
);
147 ProgressBar progressBar
= (ProgressBar
) mView
.findViewById(R
.id
.progressBar
);
148 mProgressListener
= new ProgressListener(progressBar
);
150 (mView
.findViewById(R
.id
.cancelBtn
)).setOnClickListener(this);
152 (mView
.findViewById(R
.id
.fileDownloadLL
)).setOnClickListener(new OnClickListener() {
154 public void onClick(View v
) {
155 ((PreviewImageActivity
) getActivity()).toggleFullScreen();
160 setButtonsForRemote();
163 setButtonsForTransferring();
171 public void onSaveInstanceState(Bundle outState
) {
172 super.onSaveInstanceState(outState
);
173 outState
.putParcelable(FileDownloadFragment
.EXTRA_FILE
, getFile());
174 outState
.putParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
, mAccount
);
175 outState
.putBoolean(FileDownloadFragment
.EXTRA_ERROR
, mError
);
179 public void onStart() {
181 listenForTransferProgress();
185 public void onResume() {
191 public void onPause() {
197 public void onStop() {
198 leaveTransferProgress();
203 public void onDestroy() {
209 public View
getView() {
211 listenForTransferProgress();
213 return super.getView() == null ? mView
: super.getView();
218 public void onClick(View v
) {
220 case R
.id
.cancelBtn
: {
221 mContainerActivity
.getFileOperationsHelper().cancelTransference(getFile());
222 getActivity().finish();
226 Log_OC
.e(TAG
, "Incorrect view clicked!");
232 * Enables or disables buttons for a file being downloaded
234 private void setButtonsForTransferring() {
235 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.VISIBLE
);
237 // show the progress bar for the transfer
238 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.VISIBLE
);
239 TextView progressText
= (TextView
) getView().findViewById(R
.id
.progressText
);
240 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
241 progressText
.setVisibility(View
.VISIBLE
);
243 // hides the error icon
244 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
245 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
249 * Enables or disables buttons for a file locally available
251 private void setButtonsForDown() {
252 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
254 // hides the progress bar
255 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
257 // updates the text message
258 TextView progressText
= (TextView
) getView().findViewById(R
.id
.progressText
);
259 progressText
.setText(R
.string
.common_loading
);
260 progressText
.setVisibility(View
.VISIBLE
);
262 // hides the error icon
263 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
264 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
269 * Enables or disables buttons for a file not locally available
271 * Currently, this is only used when a download was failed
273 private void setButtonsForRemote() {
274 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
276 // hides the progress bar and message
277 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
278 getView().findViewById(R
.id
.progressText
).setVisibility(View
.GONE
);
280 // shows the error icon and message
281 getView().findViewById(R
.id
.errorText
).setVisibility(View
.VISIBLE
);
282 getView().findViewById(R
.id
.error_image
).setVisibility(View
.VISIBLE
);
286 public void listenForTransferProgress() {
287 if (mProgressListener
!= null
&& !mListening
) {
288 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
289 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(
290 mProgressListener
, mAccount
, getFile()
293 setButtonsForTransferring();
299 public void leaveTransferProgress() {
300 if (mProgressListener
!= null
) {
301 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
302 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(
303 mProgressListener
, mAccount
, getFile()
312 * Helper class responsible for updating the progress bar shown for file uploading or downloading
314 private class ProgressListener
implements OnDatatransferProgressListener
{
315 int mLastPercent
= 0;
316 WeakReference
<ProgressBar
> mProgressBar
= null
;
318 ProgressListener(ProgressBar progressBar
) {
319 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
323 public void onTransferProgress(
324 long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
326 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
327 if (percent
!= mLastPercent
) {
328 ProgressBar pb
= mProgressBar
.get();
330 pb
.setProgress(percent
);
334 mLastPercent
= percent
;
340 public void setError(boolean error
) {