Merge pull request #1119 from owncloud/strip-index-php-from-server-url
[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 }
134 else {
135 mIgnoreFirstSavedState = false;
136 }
137 }
138
139 mView = inflater.inflate(R.layout.file_download_fragment, container, false);
140
141 ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.progressBar);
142 DisplayUtils.colorPreLollipopHorizontalProgressBar(progressBar);
143 mProgressListener = new ProgressListener(progressBar);
144
145 (mView.findViewById(R.id.cancelBtn)).setOnClickListener(this);
146
147 (mView.findViewById(R.id.fileDownloadLL)).setOnClickListener(new OnClickListener() {
148 @Override
149 public void onClick(View v) {
150 ((PreviewImageActivity) getActivity()).toggleFullScreen();
151 }
152 });
153
154 if (mError) {
155 setButtonsForRemote();
156 }
157 else {
158 setButtonsForTransferring();
159 }
160
161 return mView;
162 }
163
164
165 @Override
166 public void onSaveInstanceState(Bundle outState) {
167 super.onSaveInstanceState(outState);
168 outState.putParcelable(FileDownloadFragment.EXTRA_FILE, getFile());
169 outState.putParcelable(FileDownloadFragment.EXTRA_ACCOUNT, mAccount);
170 outState.putBoolean(FileDownloadFragment.EXTRA_ERROR, mError);
171 }
172
173 @Override
174 public void onStart() {
175 super.onStart();
176 listenForTransferProgress();
177 }
178
179 @Override
180 public void onResume() {
181 super.onResume();
182 }
183
184
185 @Override
186 public void onPause() {
187 super.onPause();
188 }
189
190
191 @Override
192 public void onStop() {
193 leaveTransferProgress();
194 super.onStop();
195 }
196
197 @Override
198 public void onDestroy() {
199 super.onDestroy();
200 }
201
202
203 @Override
204 public View getView() {
205 if (!mListening) {
206 listenForTransferProgress();
207 }
208 return super.getView() == null ? mView : super.getView();
209 }
210
211
212 @Override
213 public void onClick(View v) {
214 switch (v.getId()) {
215 case R.id.cancelBtn: {
216 mContainerActivity.getFileOperationsHelper().cancelTransference(getFile());
217 getActivity().finish();
218 break;
219 }
220 default:
221 Log_OC.e(TAG, "Incorrect view clicked!");
222 }
223 }
224
225
226 /**
227 * Enables or disables buttons for a file being downloaded
228 */
229 private void setButtonsForTransferring() {
230 getView().findViewById(R.id.cancelBtn).setVisibility(View.VISIBLE);
231
232 // show the progress bar for the transfer
233 getView().findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
234 TextView progressText = (TextView) getView().findViewById(R.id.progressText);
235 progressText.setText(R.string.downloader_download_in_progress_ticker);
236 progressText.setVisibility(View.VISIBLE);
237
238 // hides the error icon
239 getView().findViewById(R.id.errorText).setVisibility(View.GONE);
240 getView().findViewById(R.id.error_image).setVisibility(View.GONE);
241 }
242
243 /**
244 * Enables or disables buttons for a file locally available
245 */
246 private void setButtonsForDown() {
247 getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
248
249 // hides the progress bar
250 getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
251
252 // updates the text message
253 TextView progressText = (TextView) getView().findViewById(R.id.progressText);
254 progressText.setText(R.string.common_loading);
255 progressText.setVisibility(View.VISIBLE);
256
257 // hides the error icon
258 getView().findViewById(R.id.errorText).setVisibility(View.GONE);
259 getView().findViewById(R.id.error_image).setVisibility(View.GONE);
260 }
261
262
263 /**
264 * Enables or disables buttons for a file not locally available
265 * <p/>
266 * Currently, this is only used when a download was failed
267 */
268 private void setButtonsForRemote() {
269 getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE);
270
271 // hides the progress bar and message
272 getView().findViewById(R.id.progressBar).setVisibility(View.GONE);
273 getView().findViewById(R.id.progressText).setVisibility(View.GONE);
274
275 // shows the error icon and message
276 getView().findViewById(R.id.errorText).setVisibility(View.VISIBLE);
277 getView().findViewById(R.id.error_image).setVisibility(View.VISIBLE);
278 }
279
280
281 public void listenForTransferProgress() {
282 if (mProgressListener != null && !mListening) {
283 if (mContainerActivity.getFileDownloaderBinder() != null) {
284 mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(
285 mProgressListener, mAccount, getFile()
286 );
287 mListening = true;
288 setButtonsForTransferring();
289 }
290 }
291 }
292
293
294 public void leaveTransferProgress() {
295 if (mProgressListener != null) {
296 if (mContainerActivity.getFileDownloaderBinder() != null) {
297 mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(
298 mProgressListener, mAccount, getFile()
299 );
300 mListening = false;
301 }
302 }
303 }
304
305
306 /**
307 * Helper class responsible for updating the progress bar shown for file uploading or downloading
308 */
309 private class ProgressListener implements OnDatatransferProgressListener {
310 int mLastPercent = 0;
311 WeakReference<ProgressBar> mProgressBar = null;
312
313 ProgressListener(ProgressBar progressBar) {
314 mProgressBar = new WeakReference<ProgressBar>(progressBar);
315 }
316
317 @Override
318 public void onTransferProgress(
319 long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename
320 ) {
321 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
322 if (percent != mLastPercent) {
323 ProgressBar pb = mProgressBar.get();
324 if (pb != null) {
325 pb.setProgress(percent);
326 pb.postInvalidate();
327 }
328 }
329 mLastPercent = percent;
330 }
331
332 }
333
334
335 public void setError(boolean error) {
336 mError = error;
337 }
338
339 ;
340
341
342 }