Merge branch 'master' 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
42
43 /**
44 * This Fragment is used to monitor the progress of a file downloading.
45 */
46 public class FileDownloadFragment extends FileFragment implements OnClickListener {
47
48 public static final String EXTRA_FILE = "FILE";
49 public static final String EXTRA_ACCOUNT = "ACCOUNT";
50 private static final String EXTRA_ERROR = "ERROR";
51
52 private static final String ARG_FILE = "FILE";
53 private static final String ARG_IGNORE_FIRST = "IGNORE_FIRST";
54 private static final String ARG_ACCOUNT = "ACCOUNT" ;
55
56 private View mView;
57 private Account mAccount;
58
59 public ProgressListener mProgressListener;
60 private boolean mListening;
61
62 private static final String TAG = FileDownloadFragment.class.getSimpleName();
63
64 private boolean mIgnoreFirstSavedState;
65 private boolean mError;
66
67
68 /**
69 * Public factory method to create a new fragment that shows the progress of a file download.
70 *
71 * Android strongly recommends keep the empty constructor of fragments as the only public constructor, and
72 * use {@link #setArguments(Bundle)} to set the needed arguments.
73 *
74 * This method hides to client objects the need of doing the construction in two steps.
75 *
76 * When 'file' is null creates a dummy layout (useful when a file wasn't tapped before).
77 *
78 * @param file An {@link OCFile} to show in the fragment
79 * @param account An OC account; needed to start downloads
80 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}
81 * TODO better solution
82 */
83 public static Fragment newInstance(OCFile file, Account account, boolean ignoreFirstSavedState) {
84 FileDownloadFragment frag = new FileDownloadFragment();
85 Bundle args = new Bundle();
86 args.putParcelable(ARG_FILE, file);
87 args.putParcelable(ARG_ACCOUNT, account);
88 args.putBoolean(ARG_IGNORE_FIRST, ignoreFirstSavedState);
89 frag.setArguments(args);
90 return frag;
91 }
92
93
94 /**
95 * Creates an empty details fragment.
96 *
97 * It's necessary to keep a public constructor without parameters; the system uses it when tries to
98 * reinstantiate a fragment automatically.
99 */
100 public FileDownloadFragment() {
101 super();
102 mAccount = null;
103 mProgressListener = null;
104 mListening = false;
105 mIgnoreFirstSavedState = false;
106 mError = false;
107 }
108
109
110 @Override
111 public void onCreate(Bundle savedInstanceState) {
112 super.onCreate(savedInstanceState);
113 Bundle args = getArguments();
114 setFile((OCFile)args.getParcelable(ARG_FILE));
115 // TODO better in super, but needs to check ALL the class extending FileFragment; not right now
116
117 mIgnoreFirstSavedState = args.getBoolean(ARG_IGNORE_FIRST);
118 mAccount = args.getParcelable(ARG_ACCOUNT);
119 }
120
121
122 @Override
123 public View onCreateView(LayoutInflater inflater, ViewGroup container,
124 Bundle savedInstanceState) {
125 super.onCreateView(inflater, container, savedInstanceState);
126
127 if (savedInstanceState != null) {
128 if (!mIgnoreFirstSavedState) {
129 setFile((OCFile)savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE));
130 mAccount = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_ACCOUNT);
131 mError = savedInstanceState.getBoolean(FileDownloadFragment.EXTRA_ERROR);
132 } else {
133 mIgnoreFirstSavedState = false;
134 }
135 }
136
137 View view = null;
138 view = inflater.inflate(R.layout.file_download_fragment, container, false);
139 mView = view;
140
141 ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.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 view;
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 }