Fixes the last reported bugs
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / FileDownloadFragment.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20 package com.owncloud.android.ui.preview;
21
22 import java.lang.ref.WeakReference;
23
24 import com.owncloud.android.R;
25 import com.owncloud.android.datamodel.OCFile;
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.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;
38
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;
44
45 import java.lang.ref.WeakReference;
46
47
48 /**
49 * This Fragment is used to monitor the progress of a file downloading.
50 */
51 public class FileDownloadFragment extends FileFragment implements OnClickListener {
52
53 public static final String EXTRA_FILE = "FILE";
54 public static final String EXTRA_ACCOUNT = "ACCOUNT";
55 private static final String EXTRA_ERROR = "ERROR";
56
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" ;
60
61 private View mView;
62 private Account mAccount;
63
64 public ProgressListener mProgressListener;
65 private boolean mListening;
66
67 private static final String TAG = FileDownloadFragment.class.getSimpleName();
68
69 private boolean mIgnoreFirstSavedState;
70 private boolean mError;
71
72
73 /**
74 * Public factory method to create a new fragment that shows the progress of a file download.
75 *
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.
78 *
79 * This method hides to client objects the need of doing the construction in two steps.
80 *
81 * When 'file' is null creates a dummy layout (useful when a file wasn't tapped before).
82 *
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
87 */
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);
95 return frag;
96 }
97
98
99 /**
100 * Creates an empty details fragment.
101 *
102 * It's necessary to keep a public constructor without parameters; the system uses it when tries to
103 * reinstantiate a fragment automatically.
104 */
105 public FileDownloadFragment() {
106 super();
107 mAccount = null;
108 mProgressListener = null;
109 mListening = false;
110 mIgnoreFirstSavedState = false;
111 mError = false;
112 }
113
114
115 @Override
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
121
122 mIgnoreFirstSavedState = args.getBoolean(ARG_IGNORE_FIRST);
123 mAccount = args.getParcelable(ARG_ACCOUNT);
124 }
125
126
127 @Override
128 public View onCreateView(LayoutInflater inflater, ViewGroup container,
129 Bundle savedInstanceState) {
130 super.onCreateView(inflater, container, savedInstanceState);
131
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);
137 }
138 else {
139 mIgnoreFirstSavedState = false;
140 }
141 }
142
143 View view = null;
144 view = inflater.inflate(R.layout.file_download_fragment, container, false);
145 mView = view;
146
147 ProgressBar progressBar = (ProgressBar) mView.findViewById(R.id.progressBar);
148 mProgressListener = new ProgressListener(progressBar);
149
150 (mView.findViewById(R.id.cancelBtn)).setOnClickListener(this);
151
152 (mView.findViewById(R.id.fileDownloadLL)).setOnClickListener(new OnClickListener() {
153 @Override
154 public void onClick(View v) {
155 ((PreviewImageActivity) getActivity()).toggleFullScreen();
156 }
157 });
158
159 if (mError) {
160 setButtonsForRemote();
161 }
162 else {
163 setButtonsForTransferring();
164 }
165
166 return view;
167 }
168
169
170 @Override
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);
176 }
177
178 @Override
179 public void onStart() {
180 super.onStart();
181 listenForTransferProgress();
182 }
183
184 @Override
185 public void onResume() {
186 super.onResume();
187 }
188
189
190 @Override
191 public void onPause() {
192 super.onPause();
193 }
194
195
196 @Override
197 public void onStop() {
198 leaveTransferProgress();
199 super.onStop();
200 }
201
202 @Override
203 public void onDestroy() {
204 super.onDestroy();
205 }
206
207
208 @Override
209 public View getView() {
210 if (!mListening) {
211 listenForTransferProgress();
212 }
213 return super.getView() == null ? mView : super.getView();
214 }
215
216
217 @Override
218 public void onClick(View v) {
219 switch (v.getId()) {
220 case R.id.cancelBtn: {
221 mContainerActivity.getFileOperationsHelper().cancelTransference(getFile());
222 getActivity().finish();
223 break;
224 }
225 default:
226 Log_OC.e(TAG, "Incorrect view clicked!");
227 }
228 }
229
230
231 /**
232 * Enables or disables buttons for a file being downloaded
233 */
234 private void setButtonsForTransferring() {
235 getView().findViewById(R.id.cancelBtn).setVisibility(View.VISIBLE);
236
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);
242
243 // hides the error icon
244 getView().findViewById(R.id.errorText).setVisibility(View.GONE);
245 getView().findViewById(R.id.error_image).setVisibility(View.GONE);
246 }
247
248 /**
249 * Enables or disables buttons for a file locally available
250 */
251 private void setButtonsForDown() {
252 getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
253
254 // hides the progress bar
255 getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
256
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);
261
262 // hides the error icon
263 getView().findViewById(R.id.errorText).setVisibility(View.GONE);
264 getView().findViewById(R.id.error_image).setVisibility(View.GONE);
265 }
266
267
268 /**
269 * Enables or disables buttons for a file not locally available
270 * <p/>
271 * Currently, this is only used when a download was failed
272 */
273 private void setButtonsForRemote() {
274 getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
275
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);
279
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);
283 }
284
285
286 public void listenForTransferProgress() {
287 if (mProgressListener != null && !mListening) {
288 if (mContainerActivity.getFileDownloaderBinder() != null) {
289 mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(
290 mProgressListener, mAccount, getFile()
291 );
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(
303 mProgressListener, mAccount, getFile()
304 );
305 mListening = false;
306 }
307 }
308 }
309
310
311 /**
312 * Helper class responsible for updating the progress bar shown for file uploading or downloading
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(
324 long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename
325 ) {
326 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
327 if (percent != mLastPercent) {
328 ProgressBar pb = mProgressBar.get();
329 if (pb != null) {
330 pb.setProgress(percent);
331 pb.postInvalidate();
332 }
333 }
334 mLastPercent = percent;
335 }
336
337 }
338
339
340 public void setError(boolean error) {
341 mError = error;
342 }
343
344 ;
345
346
347 }