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
;
44 * This Fragment is used to monitor the progress of a file downloading.
46 public class FileDownloadFragment
extends FileFragment
implements OnClickListener
{
48 public static final String EXTRA_FILE
= "FILE";
49 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
50 private static final String EXTRA_ERROR
= "ERROR";
52 private static final String ARG_FILE
= "FILE";
53 private static final String ARG_IGNORE_FIRST
= "IGNORE_FIRST";
54 private static final String ARG_ACCOUNT
= "ACCOUNT" ;
57 private Account mAccount
;
59 public ProgressListener mProgressListener
;
60 private boolean mListening
;
62 private static final String TAG
= FileDownloadFragment
.class.getSimpleName();
64 private boolean mIgnoreFirstSavedState
;
65 private boolean mError
;
69 * Public factory method to create a new fragment that shows the progress of a file download.
71 * Android strongly recommends keep the empty constructor of fragments as the only public constructor, and
72 * use {@link #setArguments(Bundle)} to set the needed arguments.
74 * This method hides to client objects the need of doing the construction in two steps.
76 * When 'file' is null creates a dummy layout (useful when a file wasn't tapped before).
78 * @param file An {@link OCFile} to show in the fragment
79 * @param account An OC account; needed to start downloads
80 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}
81 * TODO better solution
83 public static Fragment
newInstance(OCFile file
, Account account
, boolean ignoreFirstSavedState
) {
84 FileDownloadFragment frag
= new FileDownloadFragment();
85 Bundle args
= new Bundle();
86 args
.putParcelable(ARG_FILE
, file
);
87 args
.putParcelable(ARG_ACCOUNT
, account
);
88 args
.putBoolean(ARG_IGNORE_FIRST
, ignoreFirstSavedState
);
89 frag
.setArguments(args
);
95 * Creates an empty details fragment.
97 * It's necessary to keep a public constructor without parameters; the system uses it when tries to
98 * reinstantiate a fragment automatically.
100 public FileDownloadFragment() {
103 mProgressListener
= null
;
105 mIgnoreFirstSavedState
= false
;
111 public void onCreate(Bundle savedInstanceState
) {
112 super.onCreate(savedInstanceState
);
113 Bundle args
= getArguments();
114 setFile((OCFile
)args
.getParcelable(ARG_FILE
));
115 // TODO better in super, but needs to check ALL the class extending FileFragment; not right now
117 mIgnoreFirstSavedState
= args
.getBoolean(ARG_IGNORE_FIRST
);
118 mAccount
= args
.getParcelable(ARG_ACCOUNT
);
123 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
124 Bundle savedInstanceState
) {
125 super.onCreateView(inflater
, container
, savedInstanceState
);
127 if (savedInstanceState
!= null
) {
128 if (!mIgnoreFirstSavedState
) {
129 setFile((OCFile
)savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_FILE
));
130 mAccount
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
);
131 mError
= savedInstanceState
.getBoolean(FileDownloadFragment
.EXTRA_ERROR
);
133 mIgnoreFirstSavedState
= false
;
138 view
= inflater
.inflate(R
.layout
.file_download_fragment
, container
, false
);
141 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.progressBar
);
142 mProgressListener
= new ProgressListener(progressBar
);
144 (mView
.findViewById(R
.id
.cancelBtn
)).setOnClickListener(this);
146 (mView
.findViewById(R
.id
.fileDownloadLL
)).setOnClickListener(new OnClickListener() {
148 public void onClick(View v
) {
149 ((PreviewImageActivity
) getActivity()).toggleFullScreen();
154 setButtonsForRemote();
156 setButtonsForTransferring();
164 public void onSaveInstanceState(Bundle outState
) {
165 super.onSaveInstanceState(outState
);
166 outState
.putParcelable(FileDownloadFragment
.EXTRA_FILE
, getFile());
167 outState
.putParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
, mAccount
);
168 outState
.putBoolean(FileDownloadFragment
.EXTRA_ERROR
, mError
);
172 public void onStart() {
174 listenForTransferProgress();
178 public void onResume() {
184 public void onPause() {
190 public void onStop() {
191 leaveTransferProgress();
196 public void onDestroy() {
202 public View
getView() {
204 listenForTransferProgress();
206 return super.getView() == null ? mView
: super.getView();
211 public void onClick(View v
) {
213 case R
.id
.cancelBtn
: {
214 mContainerActivity
.getFileOperationsHelper().cancelTransference(getFile());
215 getActivity().finish();
219 Log_OC
.e(TAG
, "Incorrect view clicked!");
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(
284 mProgressListener
, mAccount
, getFile()
287 setButtonsForTransferring();
293 public void leaveTransferProgress() {
294 if (mProgressListener
!= null
) {
295 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
296 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(
297 mProgressListener
, mAccount
, getFile()
306 * Helper class responsible for updating the progress bar shown for file uploading or downloading
308 private class ProgressListener
implements OnDatatransferProgressListener
{
309 int mLastPercent
= 0;
310 WeakReference
<ProgressBar
> mProgressBar
= null
;
312 ProgressListener(ProgressBar progressBar
) {
313 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
317 public void onTransferProgress(
318 long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
320 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
321 if (percent
!= mLastPercent
) {
322 ProgressBar pb
= mProgressBar
.get();
324 pb
.setProgress(percent
);
328 mLastPercent
= percent
;
334 public void setError(boolean error
) {