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
;
27 import android
.accounts
.Account
;
28 import android
.os
.Bundle
;
29 import android
.support
.v4
.app
.FragmentStatePagerAdapter
;
30 import android
.view
.LayoutInflater
;
31 import android
.view
.View
;
32 import android
.view
.View
.OnClickListener
;
33 import android
.view
.ViewGroup
;
34 import android
.widget
.ImageButton
;
35 import android
.widget
.LinearLayout
;
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 * @author David A. Velasco
48 public class FileDownloadFragment
extends FileFragment
implements OnClickListener
{
50 public static final String EXTRA_FILE
= "FILE";
51 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
52 private static final String EXTRA_ERROR
= "ERROR";
55 private Account mAccount
;
57 public ProgressListener mProgressListener
;
58 private boolean mListening
;
60 private static final String TAG
= FileDownloadFragment
.class.getSimpleName();
62 private boolean mIgnoreFirstSavedState
;
63 private boolean mError
;
67 * Creates an empty details fragment.
69 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
71 public FileDownloadFragment() {
74 mProgressListener
= null
;
76 mIgnoreFirstSavedState
= false
;
82 * Creates a details fragment.
84 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
86 * @param fileToDetail An {@link OCFile} to show in the fragment
87 * @param ocAccount An ownCloud account; needed to start downloads
88 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution
90 public FileDownloadFragment(OCFile fileToDetail
, Account ocAccount
, boolean ignoreFirstSavedState
) {
93 mProgressListener
= null
;
95 mIgnoreFirstSavedState
= ignoreFirstSavedState
;
101 public void onCreate(Bundle savedInstanceState
) {
102 super.onCreate(savedInstanceState
);
107 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
108 Bundle savedInstanceState
) {
109 super.onCreateView(inflater
, container
, savedInstanceState
);
111 if (savedInstanceState
!= null
) {
112 if (!mIgnoreFirstSavedState
) {
113 setFile((OCFile
)savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_FILE
));
114 mAccount
= savedInstanceState
.getParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
);
115 mError
= savedInstanceState
.getBoolean(FileDownloadFragment
.EXTRA_ERROR
);
117 mIgnoreFirstSavedState
= false
;
122 view
= inflater
.inflate(R
.layout
.file_download_fragment
, container
, false
);
125 ProgressBar progressBar
= (ProgressBar
)mView
.findViewById(R
.id
.progressBar
);
126 mProgressListener
= new ProgressListener(progressBar
);
128 ((ImageButton
)mView
.findViewById(R
.id
.cancelBtn
)).setOnClickListener(this);
130 ((LinearLayout
)mView
.findViewById(R
.id
.fileDownloadLL
)).setOnClickListener(new OnClickListener() {
132 public void onClick(View v
) {
133 ((PreviewImageActivity
) getActivity()).toggleFullScreen();
138 setButtonsForRemote();
140 setButtonsForTransferring();
148 public void onSaveInstanceState(Bundle outState
) {
149 super.onSaveInstanceState(outState
);
150 outState
.putParcelable(FileDownloadFragment
.EXTRA_FILE
, getFile());
151 outState
.putParcelable(FileDownloadFragment
.EXTRA_ACCOUNT
, mAccount
);
152 outState
.putBoolean(FileDownloadFragment
.EXTRA_ERROR
, mError
);
156 public void onStart() {
158 listenForTransferProgress();
162 public void onResume() {
168 public void onPause() {
174 public void onStop() {
175 leaveTransferProgress();
180 public void onDestroy() {
186 public View
getView() {
188 listenForTransferProgress();
190 return super.getView() == null ? mView
: super.getView();
195 public void onClick(View v
) {
197 case R
.id
.cancelBtn
: {
198 mContainerActivity
.getFileOperationsHelper().cancelTransference(getFile());
199 getActivity().finish();
203 Log_OC
.e(TAG
, "Incorrect view clicked!");
209 * Updates the view depending upon the state of the downloading file.
211 * @param transferring When true, the view must be updated assuming that the holded file is
212 * downloading, no matter what the downloaderBinder says.
215 public void updateView(boolean transferring) {
216 // configure UI for depending upon local state of the file
218 if (transferring || getFile().isDownloading()) {
219 setButtonsForTransferring();
221 } else if (getFile().isDown()) {
226 setButtonsForRemote();
228 getView().invalidate();
234 * Enables or disables buttons for a file being downloaded
236 private void setButtonsForTransferring() {
237 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.VISIBLE
);
239 // show the progress bar for the transfer
240 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.VISIBLE
);
241 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
242 progressText
.setText(R
.string
.downloader_download_in_progress_ticker
);
243 progressText
.setVisibility(View
.VISIBLE
);
245 // hides the error icon
246 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
247 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
252 * Enables or disables buttons for a file locally available
254 private void setButtonsForDown() {
255 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
257 // hides the progress bar
258 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
260 // updates the text message
261 TextView progressText
= (TextView
)getView().findViewById(R
.id
.progressText
);
262 progressText
.setText(R
.string
.common_loading
);
263 progressText
.setVisibility(View
.VISIBLE
);
265 // hides the error icon
266 getView().findViewById(R
.id
.errorText
).setVisibility(View
.GONE
);
267 getView().findViewById(R
.id
.error_image
).setVisibility(View
.GONE
);
272 * Enables or disables buttons for a file not locally available
274 * Currently, this is only used when a download was failed
276 private void setButtonsForRemote() {
277 getView().findViewById(R
.id
.cancelBtn
).setVisibility(View
.GONE
);
279 // hides the progress bar and message
280 getView().findViewById(R
.id
.progressBar
).setVisibility(View
.GONE
);
281 getView().findViewById(R
.id
.progressText
).setVisibility(View
.GONE
);
283 // shows the error icon and message
284 getView().findViewById(R
.id
.errorText
).setVisibility(View
.VISIBLE
);
285 getView().findViewById(R
.id
.error_image
).setVisibility(View
.VISIBLE
);
289 public void listenForTransferProgress() {
290 if (mProgressListener
!= null
&& !mListening
) {
291 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
292 mContainerActivity
.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
294 setButtonsForTransferring();
300 public void leaveTransferProgress() {
301 if (mProgressListener
!= null
) {
302 if (mContainerActivity
.getFileDownloaderBinder() != null
) {
303 mContainerActivity
.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener
, mAccount
, getFile());
311 * Helper class responsible for updating the progress bar shown for file uploading or downloading
313 * @author David A. Velasco
315 private class ProgressListener
implements OnDatatransferProgressListener
{
316 int mLastPercent
= 0;
317 WeakReference
<ProgressBar
> mProgressBar
= null
;
319 ProgressListener(ProgressBar progressBar
) {
320 mProgressBar
= new WeakReference
<ProgressBar
>(progressBar
);
324 public void onTransferProgress(long progressRate
, long totalTransferredSoFar
, long totalToTransfer
, String filename
) {
325 int percent
= (int)(100.0*((double)totalTransferredSoFar
)/((double)totalToTransfer
));
326 if (percent
!= mLastPercent
) {
327 ProgressBar pb
= mProgressBar
.get();
329 pb
.setProgress(percent
);
333 mLastPercent
= percent
;
339 public void setError(boolean error
) {