Merge remote-tracking branch 'upstream/develop' into develop
[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.lib.common.network.OnDatatransferProgressListener;
40 import com.owncloud.android.lib.common.utils.Log_OC;
41 import com.owncloud.android.utils.DisplayUtils;
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 static final String ARG_FILE = "FILE";
54 private static final String ARG_IGNORE_FIRST = "IGNORE_FIRST";
55 private static final String ARG_ACCOUNT = "ACCOUNT" ;
56
57 private View mView;
58 private Account mAccount;
59
60 public ProgressListener mProgressListener;
61 private boolean mListening;
62
63 private static final String TAG = FileDownloadFragment.class.getSimpleName();
64
65 private boolean mIgnoreFirstSavedState;
66 private boolean mError;
67
68
69 /**
70 * Public factory method to create a new fragment that shows the progress of a file download.
71 *
72 * Android strongly recommends keep the empty constructor of fragments as the only public constructor, and
73 * use {@link #setArguments(Bundle)} to set the needed arguments.
74 *
75 * This method hides to client objects the need of doing the construction in two steps.
76 *
77 * When 'file' is null creates a dummy layout (useful when a file wasn't tapped before).
78 *
79 * @param file An {@link OCFile} to show in the fragment
80 * @param account An OC account; needed to start downloads
81 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}
82 * TODO better solution
83 */
84 public static Fragment newInstance(OCFile file, Account account, boolean ignoreFirstSavedState) {
85 FileDownloadFragment frag = new FileDownloadFragment();
86 Bundle args = new Bundle();
87 args.putParcelable(ARG_FILE, file);
88 args.putParcelable(ARG_ACCOUNT, account);
89 args.putBoolean(ARG_IGNORE_FIRST, ignoreFirstSavedState);
90 frag.setArguments(args);
91 return frag;
92 }
93
94
95 /**
96 * Creates an empty details fragment.
97 *
98 * It's necessary to keep a public constructor without parameters; the system uses it when tries to
99 * reinstantiate a fragment automatically.
100 */
101 public FileDownloadFragment() {
102 super();
103 mAccount = null;
104 mProgressListener = null;
105 mListening = false;
106 mIgnoreFirstSavedState = false;
107 mError = false;
108 }
109
110
111 @Override
112 public void onCreate(Bundle savedInstanceState) {
113 super.onCreate(savedInstanceState);
114 Bundle args = getArguments();
115 setFile((OCFile)args.getParcelable(ARG_FILE));
116 // TODO better in super, but needs to check ALL the class extending FileFragment; not right now
117
118 mIgnoreFirstSavedState = args.getBoolean(ARG_IGNORE_FIRST);
119 mAccount = args.getParcelable(ARG_ACCOUNT);
120 }
121
122
123 @Override
124 public View onCreateView(LayoutInflater inflater, ViewGroup container,
125 Bundle savedInstanceState) {
126 super.onCreateView(inflater, container, savedInstanceState);
127
128 if (savedInstanceState != null) {
129 if (!mIgnoreFirstSavedState) {
130 setFile((OCFile)savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE));
131 mAccount = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_ACCOUNT);
132 mError = savedInstanceState.getBoolean(FileDownloadFragment.EXTRA_ERROR);
133 } else {
134 mIgnoreFirstSavedState = false;
135 }
136 }
137
138 mView = inflater.inflate(R.layout.file_download_fragment, container, false);
139
140 ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.progressBar);
141 DisplayUtils.colorPreLollipopHorizontalProgressBar(progressBar);
142 mProgressListener = new ProgressListener(progressBar);
143
144 (mView.findViewById(R.id.cancelBtn)).setOnClickListener(this);
145
146 (mView.findViewById(R.id.fileDownloadLL)).setOnClickListener(new OnClickListener() {
147 @Override
148 public void onClick(View v) {
149 ((PreviewImageActivity) getActivity()).toggleFullScreen();
150 }
151 });
152
153 if (mError) {
154 setButtonsForRemote();
155 } else {
156 setButtonsForTransferring();
157 }
158
159 return mView;
160 }
161
162
163 @Override
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);
169 }
170
171 @Override
172 public void onStart() {
173 super.onStart();
174 listenForTransferProgress();
175 }
176
177 @Override
178 public void onResume() {
179 super.onResume();
180 }
181
182
183 @Override
184 public void onPause() {
185 super.onPause();
186 }
187
188
189 @Override
190 public void onStop() {
191 leaveTransferProgress();
192 super.onStop();
193 }
194
195 @Override
196 public void onDestroy() {
197 super.onDestroy();
198 }
199
200
201 @Override
202 public View getView() {
203 if (!mListening) {
204 listenForTransferProgress();
205 }
206 return super.getView() == null ? mView : super.getView();
207 }
208
209
210 @Override
211 public void onClick(View v) {
212 switch (v.getId()) {
213 case R.id.cancelBtn: {
214 mContainerActivity.getFileOperationsHelper().cancelTransference(getFile());
215 getActivity().finish();
216 break;
217 }
218 default:
219 Log_OC.e(TAG, "Incorrect view clicked!");
220 }
221 }
222
223
224 /**
225 * Enables or disables buttons for a file being downloaded
226 */
227 private void setButtonsForTransferring() {
228 getView().findViewById(R.id.cancelBtn).setVisibility(View.VISIBLE);
229
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);
235
236 // hides the error icon
237 getView().findViewById(R.id.errorText).setVisibility(View.GONE);
238 getView().findViewById(R.id.error_image).setVisibility(View.GONE);
239 }
240
241
242 /**
243 * Enables or disables buttons for a file locally available
244 */
245 private void setButtonsForDown() {
246 getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
247
248 // hides the progress bar
249 getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
250
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);
255
256 // hides the error icon
257 getView().findViewById(R.id.errorText).setVisibility(View.GONE);
258 getView().findViewById(R.id.error_image).setVisibility(View.GONE);
259 }
260
261
262 /**
263 * Enables or disables buttons for a file not locally available
264 *
265 * Currently, this is only used when a download was failed
266 */
267 private void setButtonsForRemote() {
268 getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
269
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);
273
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);
277 }
278
279
280 public void listenForTransferProgress() {
281 if (mProgressListener != null && !mListening) {
282 if (mContainerActivity.getFileDownloaderBinder() != null) {
283 mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(
284 mProgressListener, mAccount, getFile()
285 );
286 mListening = true;
287 setButtonsForTransferring();
288 }
289 }
290 }
291
292
293 public void leaveTransferProgress() {
294 if (mProgressListener != null) {
295 if (mContainerActivity.getFileDownloaderBinder() != null) {
296 mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(
297 mProgressListener, mAccount, getFile()
298 );
299 mListening = false;
300 }
301 }
302 }
303
304
305 /**
306 * Helper class responsible for updating the progress bar shown for file uploading or downloading
307 */
308 private class ProgressListener implements OnDatatransferProgressListener {
309 int mLastPercent = 0;
310 WeakReference<ProgressBar> mProgressBar = null;
311
312 ProgressListener(ProgressBar progressBar) {
313 mProgressBar = new WeakReference<ProgressBar>(progressBar);
314 }
315
316 @Override
317 public void onTransferProgress(
318 long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename
319 ) {
320 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
321 if (percent != mLastPercent) {
322 ProgressBar pb = mProgressBar.get();
323 if (pb != null) {
324 pb.setProgress(percent);
325 pb.postInvalidate();
326 }
327 }
328 mLastPercent = percent;
329 }
330
331 }
332
333
334 public void setError(boolean error) {
335 mError = error;
336 };
337
338
339
340 }