Remove unused imports and split long lines modified
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / FileDownloadFragment.java
1 /* ownCloud Android client application
2 *
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18 package com.owncloud.android.ui.preview;
19
20 import java.lang.ref.WeakReference;
21
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;
26
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;
38
39 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
40 import com.owncloud.android.lib.common.utils.Log_OC;
41
42
43 /**
44 * This Fragment is used to monitor the progress of a file downloading.
45 *
46 * @author David A. Velasco
47 */
48 public class FileDownloadFragment extends FileFragment implements OnClickListener {
49
50 public static final String EXTRA_FILE = "FILE";
51 public static final String EXTRA_ACCOUNT = "ACCOUNT";
52 private static final String EXTRA_ERROR = "ERROR";
53
54 private View mView;
55 private Account mAccount;
56
57 public ProgressListener mProgressListener;
58 private boolean mListening;
59
60 private static final String TAG = FileDownloadFragment.class.getSimpleName();
61
62 private boolean mIgnoreFirstSavedState;
63 private boolean mError;
64
65
66 /**
67 * Creates an empty details fragment.
68 *
69 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
70 */
71 public FileDownloadFragment() {
72 super();
73 mAccount = null;
74 mProgressListener = null;
75 mListening = false;
76 mIgnoreFirstSavedState = false;
77 mError = false;
78 }
79
80
81 /**
82 * Creates a details fragment.
83 *
84 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
85 *
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
89 */
90 public FileDownloadFragment(OCFile fileToDetail, Account ocAccount, boolean ignoreFirstSavedState) {
91 super(fileToDetail);
92 mAccount = ocAccount;
93 mProgressListener = null;
94 mListening = false;
95 mIgnoreFirstSavedState = ignoreFirstSavedState;
96 mError = false;
97 }
98
99
100 @Override
101 public void onCreate(Bundle savedInstanceState) {
102 super.onCreate(savedInstanceState);
103 }
104
105
106 @Override
107 public View onCreateView(LayoutInflater inflater, ViewGroup container,
108 Bundle savedInstanceState) {
109 super.onCreateView(inflater, container, savedInstanceState);
110
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);
116 } else {
117 mIgnoreFirstSavedState = false;
118 }
119 }
120
121 View view = null;
122 view = inflater.inflate(R.layout.file_download_fragment, container, false);
123 mView = view;
124
125 ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.progressBar);
126 mProgressListener = new ProgressListener(progressBar);
127
128 ((ImageButton)mView.findViewById(R.id.cancelBtn)).setOnClickListener(this);
129
130 ((LinearLayout)mView.findViewById(R.id.fileDownloadLL)).setOnClickListener(new OnClickListener() {
131 @Override
132 public void onClick(View v) {
133 ((PreviewImageActivity) getActivity()).toggleFullScreen();
134 }
135 });
136
137 if (mError) {
138 setButtonsForRemote();
139 } else {
140 setButtonsForTransferring();
141 }
142
143 return view;
144 }
145
146
147 @Override
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);
153 }
154
155 @Override
156 public void onStart() {
157 super.onStart();
158 listenForTransferProgress();
159 }
160
161 @Override
162 public void onResume() {
163 super.onResume();
164 }
165
166
167 @Override
168 public void onPause() {
169 super.onPause();
170 }
171
172
173 @Override
174 public void onStop() {
175 leaveTransferProgress();
176 super.onStop();
177 }
178
179 @Override
180 public void onDestroy() {
181 super.onDestroy();
182 }
183
184
185 @Override
186 public View getView() {
187 if (!mListening) {
188 listenForTransferProgress();
189 }
190 return super.getView() == null ? mView : super.getView();
191 }
192
193
194 @Override
195 public void onClick(View v) {
196 switch (v.getId()) {
197 case R.id.cancelBtn: {
198 mContainerActivity.getFileOperationsHelper().cancelTransference(getFile());
199 getActivity().finish();
200 break;
201 }
202 default:
203 Log_OC.e(TAG, "Incorrect view clicked!");
204 }
205 }
206
207
208 /**
209 * Updates the view depending upon the state of the downloading file.
210 *
211 * @param transferring When true, the view must be updated assuming that the holded file is
212 * downloading, no matter what the downloaderBinder says.
213 */
214 public void updateView(boolean transferring) {
215 // configure UI for depending upon local state of the file
216 FileDownloaderBinder downloaderBinder = (mContainerActivity == null) ? null : mContainerActivity.getFileDownloaderBinder();
217 if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, getFile()))) {
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 * @author David A. Velasco
313 */
314 private class ProgressListener implements OnDatatransferProgressListener {
315 int mLastPercent = 0;
316 WeakReference<ProgressBar> mProgressBar = null;
317
318 ProgressListener(ProgressBar progressBar) {
319 mProgressBar = new WeakReference<ProgressBar>(progressBar);
320 }
321
322 @Override
323 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) {
324 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
325 if (percent != mLastPercent) {
326 ProgressBar pb = mProgressBar.get();
327 if (pb != null) {
328 pb.setProgress(percent);
329 pb.postInvalidate();
330 }
331 }
332 mLastPercent = percent;
333 }
334
335 }
336
337
338 public void setError(boolean error) {
339 mError = error;
340 };
341
342
343
344 }