741af7455a074cd57121dd009b13f4b707c3bc91
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / FileDownloadFragment.java
1 /* ownCloud Android client application
2 *
3 * @author David A. Velasco
4 * Copyright (C) 2012-2013 ownCloud Inc.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 package com.owncloud.android.ui.preview;
20
21 import java.lang.ref.WeakReference;
22
23 import com.owncloud.android.R;
24 import com.owncloud.android.datamodel.OCFile;
25 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
26 import com.owncloud.android.ui.fragment.FileFragment;
27
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.LinearLayout;
37 import android.widget.ProgressBar;
38 import android.widget.TextView;
39
40 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
41 import com.owncloud.android.lib.common.utils.Log_OC;
42
43
44 /**
45 * This Fragment is used to monitor the progress of a file downloading.
46 */
47 public class FileDownloadFragment extends FileFragment implements OnClickListener {
48
49 public static final String EXTRA_FILE = "FILE";
50 public static final String EXTRA_ACCOUNT = "ACCOUNT";
51 private static final String EXTRA_ERROR = "ERROR";
52
53 private View mView;
54 private Account mAccount;
55
56 public ProgressListener mProgressListener;
57 private boolean mListening;
58
59 private static final String TAG = FileDownloadFragment.class.getSimpleName();
60
61 private boolean mIgnoreFirstSavedState;
62 private boolean mError;
63
64
65 /**
66 * Creates an empty details fragment.
67 *
68 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
69 */
70 public FileDownloadFragment() {
71 super();
72 mAccount = null;
73 mProgressListener = null;
74 mListening = false;
75 mIgnoreFirstSavedState = false;
76 mError = false;
77 }
78
79
80 /**
81 * Creates a details fragment.
82 *
83 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
84 *
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
88 */
89 public FileDownloadFragment(OCFile fileToDetail, Account ocAccount, boolean ignoreFirstSavedState) {
90 super(fileToDetail);
91 mAccount = ocAccount;
92 mProgressListener = null;
93 mListening = false;
94 mIgnoreFirstSavedState = ignoreFirstSavedState;
95 mError = false;
96 }
97
98
99 @Override
100 public void onCreate(Bundle savedInstanceState) {
101 super.onCreate(savedInstanceState);
102 }
103
104
105 @Override
106 public View onCreateView(LayoutInflater inflater, ViewGroup container,
107 Bundle savedInstanceState) {
108 super.onCreateView(inflater, container, savedInstanceState);
109
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);
115 } else {
116 mIgnoreFirstSavedState = false;
117 }
118 }
119
120 View view = null;
121 view = inflater.inflate(R.layout.file_download_fragment, container, false);
122 mView = view;
123
124 ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.progressBar);
125 mProgressListener = new ProgressListener(progressBar);
126
127 ((ImageButton)mView.findViewById(R.id.cancelBtn)).setOnClickListener(this);
128
129 ((LinearLayout)mView.findViewById(R.id.fileDownloadLL)).setOnClickListener(new OnClickListener() {
130 @Override
131 public void onClick(View v) {
132 ((PreviewImageActivity) getActivity()).toggleFullScreen();
133 }
134 });
135
136 if (mError) {
137 setButtonsForRemote();
138 } else {
139 setButtonsForTransferring();
140 }
141
142 return view;
143 }
144
145
146 @Override
147 public void onSaveInstanceState(Bundle outState) {
148 super.onSaveInstanceState(outState);
149 outState.putParcelable(FileDownloadFragment.EXTRA_FILE, getFile());
150 outState.putParcelable(FileDownloadFragment.EXTRA_ACCOUNT, mAccount);
151 outState.putBoolean(FileDownloadFragment.EXTRA_ERROR, mError);
152 }
153
154 @Override
155 public void onStart() {
156 super.onStart();
157 listenForTransferProgress();
158 }
159
160 @Override
161 public void onResume() {
162 super.onResume();
163 }
164
165
166 @Override
167 public void onPause() {
168 super.onPause();
169 }
170
171
172 @Override
173 public void onStop() {
174 leaveTransferProgress();
175 super.onStop();
176 }
177
178 @Override
179 public void onDestroy() {
180 super.onDestroy();
181 }
182
183
184 @Override
185 public View getView() {
186 if (!mListening) {
187 listenForTransferProgress();
188 }
189 return super.getView() == null ? mView : super.getView();
190 }
191
192
193 @Override
194 public void onClick(View v) {
195 switch (v.getId()) {
196 case R.id.cancelBtn: {
197 mContainerActivity.getFileOperationsHelper().cancelTransference(getFile());
198 getActivity().finish();
199 break;
200 }
201 default:
202 Log_OC.e(TAG, "Incorrect view clicked!");
203 }
204 }
205
206
207 /**
208 * Updates the view depending upon the state of the downloading file.
209 *
210 * @param transferring When true, the view must be updated assuming that the holded file is
211 * downloading, no matter what the downloaderBinder says.
212 */
213 /*
214 public void updateView(boolean transferring) {
215 // configure UI for depending upon local state of the file
216 // TODO remove
217 if (transferring || getFile().isDownloading()) {
218 setButtonsForTransferring();
219
220 } else if (getFile().isDown()) {
221
222 setButtonsForDown();
223
224 } else {
225 setButtonsForRemote();
226 }
227 getView().invalidate();
228
229 }
230 */
231
232 /**
233 * Enables or disables buttons for a file being downloaded
234 */
235 private void setButtonsForTransferring() {
236 getView().findViewById(R.id.cancelBtn).setVisibility(View.VISIBLE);
237
238 // show the progress bar for the transfer
239 getView().findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
240 TextView progressText = (TextView)getView().findViewById(R.id.progressText);
241 progressText.setText(R.string.downloader_download_in_progress_ticker);
242 progressText.setVisibility(View.VISIBLE);
243
244 // hides the error icon
245 getView().findViewById(R.id.errorText).setVisibility(View.GONE);
246 getView().findViewById(R.id.error_image).setVisibility(View.GONE);
247 }
248
249
250 /**
251 * Enables or disables buttons for a file locally available
252 */
253 private void setButtonsForDown() {
254 getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
255
256 // hides the progress bar
257 getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
258
259 // updates the text message
260 TextView progressText = (TextView)getView().findViewById(R.id.progressText);
261 progressText.setText(R.string.common_loading);
262 progressText.setVisibility(View.VISIBLE);
263
264 // hides the error icon
265 getView().findViewById(R.id.errorText).setVisibility(View.GONE);
266 getView().findViewById(R.id.error_image).setVisibility(View.GONE);
267 }
268
269
270 /**
271 * Enables or disables buttons for a file not locally available
272 *
273 * Currently, this is only used when a download was failed
274 */
275 private void setButtonsForRemote() {
276 getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
277
278 // hides the progress bar and message
279 getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
280 getView().findViewById(R.id.progressText).setVisibility(View.GONE);
281
282 // shows the error icon and message
283 getView().findViewById(R.id.errorText).setVisibility(View.VISIBLE);
284 getView().findViewById(R.id.error_image).setVisibility(View.VISIBLE);
285 }
286
287
288 public void listenForTransferProgress() {
289 if (mProgressListener != null && !mListening) {
290 if (mContainerActivity.getFileDownloaderBinder() != null) {
291 mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile());
292 mListening = true;
293 setButtonsForTransferring();
294 }
295 }
296 }
297
298
299 public void leaveTransferProgress() {
300 if (mProgressListener != null) {
301 if (mContainerActivity.getFileDownloaderBinder() != null) {
302 mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile());
303 mListening = false;
304 }
305 }
306 }
307
308
309 /**
310 * Helper class responsible for updating the progress bar shown for file uploading or downloading
311 */
312 private class ProgressListener implements OnDatatransferProgressListener {
313 int mLastPercent = 0;
314 WeakReference<ProgressBar> mProgressBar = null;
315
316 ProgressListener(ProgressBar progressBar) {
317 mProgressBar = new WeakReference<ProgressBar>(progressBar);
318 }
319
320 @Override
321 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) {
322 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
323 if (percent != mLastPercent) {
324 ProgressBar pb = mProgressBar.get();
325 if (pb != null) {
326 pb.setProgress(percent);
327 pb.postInvalidate();
328 }
329 }
330 mLastPercent = percent;
331 }
332
333 }
334
335
336 public void setError(boolean error) {
337 mError = error;
338 };
339
340
341
342 }