Cancelling a download in gallery finishes the gallery
[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 as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 package com.owncloud.android.ui.preview;
20
21 import java.lang.ref.WeakReference;
22
23 import android.accounts.Account;
24 import android.app.Activity;
25 import android.os.Bundle;
26 import android.support.v4.app.FragmentStatePagerAdapter;
27 import android.util.Log;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.View.OnClickListener;
31 import android.view.ViewGroup;
32 import android.widget.Button;
33 import android.widget.ProgressBar;
34 import android.widget.TextView;
35
36 import com.actionbarsherlock.app.SherlockFragment;
37 import com.owncloud.android.datamodel.FileDataStorageManager;
38 import com.owncloud.android.datamodel.OCFile;
39 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
40 import com.owncloud.android.ui.fragment.FileFragment;
41
42 import com.owncloud.android.R;
43
44 import eu.alefzero.webdav.OnDatatransferProgressListener;
45
46 /**
47 * This Fragment is used to monitor the progress of a file downloading.
48 *
49 * @author David A. Velasco
50 */
51 public class FileDownloadFragment extends SherlockFragment implements OnClickListener, FileFragment {
52
53 public static final String EXTRA_FILE = "FILE";
54 public static final String EXTRA_ACCOUNT = "ACCOUNT";
55
56 private FileFragment.ContainerActivity mContainerActivity;
57
58 private View mView;
59 private OCFile mFile;
60 private Account mAccount;
61 private FileDataStorageManager mStorageManager;
62
63 public ProgressListener mProgressListener;
64 private boolean mListening;
65
66 private static final String TAG = FileDownloadFragment.class.getSimpleName();
67
68 private boolean mIgnoreFirstSavedState;
69
70
71 /**
72 * Creates an empty details fragment.
73 *
74 * It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
75 */
76 public FileDownloadFragment() {
77 mFile = null;
78 mAccount = null;
79 mStorageManager = null;
80 mProgressListener = null;
81 mListening = false;
82 mIgnoreFirstSavedState = false;
83 }
84
85
86 /**
87 * Creates a details fragment.
88 *
89 * When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
90 *
91 * @param fileToDetail An {@link OCFile} to show in the fragment
92 * @param ocAccount An ownCloud account; needed to start downloads
93 * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution
94 */
95 public FileDownloadFragment(OCFile fileToDetail, Account ocAccount, boolean ignoreFirstSavedState) {
96 mFile = fileToDetail;
97 mAccount = ocAccount;
98 mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment
99 mProgressListener = null;
100 mListening = false;
101 mIgnoreFirstSavedState = ignoreFirstSavedState;
102 }
103
104
105 @Override
106 public void onCreate(Bundle savedInstanceState) {
107 super.onCreate(savedInstanceState);
108 Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONCREATE " + ((mFile == null)? "(NULL)" : mFile.getFileName()));
109 }
110
111
112 @Override
113 public View onCreateView(LayoutInflater inflater, ViewGroup container,
114 Bundle savedInstanceState) {
115 super.onCreateView(inflater, container, savedInstanceState);
116
117 if (savedInstanceState != null) {
118 if (!mIgnoreFirstSavedState) {
119 mFile = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE);
120 mAccount = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_ACCOUNT);
121 } else {
122 mIgnoreFirstSavedState = false;
123 }
124 }
125
126 View view = null;
127 view = inflater.inflate(R.layout.file_download_fragment, container, false);
128 mView = view;
129
130 ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.progressBar);
131 mProgressListener = new ProgressListener(progressBar);
132
133 ((Button)mView.findViewById(R.id.cancelBtn)).setOnClickListener(this);
134
135 return view;
136 }
137
138
139 /**
140 * {@inheritDoc}
141 */
142 @Override
143 public void onAttach(Activity activity) {
144 super.onAttach(activity);
145 Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONATTACH " + ((mFile == null)?" (NULL)":mFile.getFileName()));
146 try {
147 mContainerActivity = (ContainerActivity) activity;
148
149 } catch (ClassCastException e) {
150 throw new ClassCastException(activity.toString() + " must implement " + FileFragment.ContainerActivity.class.getSimpleName());
151 }
152 }
153
154
155 /**
156 * {@inheritDoc}
157 */
158 @Override
159 public void onActivityCreated(Bundle savedInstanceState) {
160 super.onActivityCreated(savedInstanceState);
161 Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONACTIVITYCREATED " + ((mFile == null)?" (NULL)":mFile.getFileName()));
162 if (mAccount != null) {
163 mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());;
164 }
165 }
166
167
168 @Override
169 public void onSaveInstanceState(Bundle outState) {
170 super.onSaveInstanceState(outState);
171 outState.putParcelable(FileDownloadFragment.EXTRA_FILE, mFile);
172 outState.putParcelable(FileDownloadFragment.EXTRA_ACCOUNT, mAccount);
173 }
174
175 @Override
176 public void onStart() {
177 super.onStart();
178 Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONSTART " + mFile.getFileName());
179 listenForTransferProgress();
180 }
181
182 @Override
183 public void onResume() {
184 Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONRESUME " + mFile.getFileName());
185 super.onResume();
186 }
187
188
189 @Override
190 public void onPause() {
191 super.onPause();
192 Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONPAUSE " + mFile.getFileName());
193 }
194
195
196 @Override
197 public void onStop() {
198 super.onStop();
199 Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONSTOP " + mFile.getFileName());
200 leaveTransferProgress();
201 }
202
203 @Override
204 public void onDestroy() {
205 super.onDestroy();
206 Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONDESTROY " + mFile.getFileName());
207 }
208
209
210 @Override
211 public View getView() {
212 if (!mListening) {
213 listenForTransferProgress();
214 }
215 return super.getView() == null ? mView : super.getView();
216 }
217
218
219 @Override
220 public void onClick(View v) {
221 switch (v.getId()) {
222 case R.id.cancelBtn: {
223 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
224 if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) {
225 downloaderBinder.cancel(mAccount, mFile);
226 getActivity().finish(); // :)
227 /*
228 leaveTransferProgress();
229 if (mFile.isDown()) {
230 setButtonsForDown();
231 } else {
232 setButtonsForRemote();
233 }
234 */
235 }
236 break;
237 }
238 default:
239 Log.e(TAG, "Incorrect view clicked!");
240 }
241 }
242
243
244 /**
245 * {@inheritDoc}
246 */
247 public OCFile getFile(){
248 return mFile;
249 }
250
251
252 /**
253 * Updates the view depending upon the state of the downloading file.
254 *
255 * @param transferring When true, the view must be updated assuming that the holded file is
256 * downloading, no matter what the downloaderBinder says.
257 */
258 public void updateView(boolean transferring) {
259 // configure UI for depending upon local state of the file
260 FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
261 if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile))) {
262 setButtonsForTransferring();
263
264 } else if (mFile.isDown()) {
265
266 setButtonsForDown();
267
268 } else {
269 setButtonsForRemote();
270 }
271 getView().invalidate();
272 }
273
274
275 /**
276 * Enables or disables buttons for a file being downloaded
277 */
278 private void setButtonsForTransferring() {
279 Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
280 downloadButton.setText(R.string.common_cancel);
281
282 // show the progress bar for the transfer
283 ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
284 progressBar.setVisibility(View.VISIBLE);
285 TextView progressText = (TextView)getView().findViewById(R.id.progressText);
286 progressText.setText(R.string.downloader_download_in_progress_ticker);
287 }
288
289
290 /**
291 * Enables or disables buttons for a file locally available
292 */
293 private void setButtonsForDown() {
294 Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
295 downloadButton.setVisibility(View.GONE);
296
297 // hides the progress bar
298 ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
299 progressBar.setVisibility(View.GONE);
300
301 // updates the text message
302 TextView progressText = (TextView)getView().findViewById(R.id.progressText);
303 progressText.setText(R.string.common_loading);
304 }
305
306
307 /**
308 * Enables or disables buttons for a file not locally available
309 */
310 private void setButtonsForRemote() {
311 Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
312 downloadButton.setVisibility(View.GONE);
313 //downloadButton.setText(R.string.filedetails_download);
314
315 // hides the progress bar
316 ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
317 progressBar.setVisibility(View.GONE);
318
319 // updates the text message
320 TextView progressText = (TextView)getView().findViewById(R.id.progressText);
321 progressText.setText(R.string.downloader_not_downloaded_yet);
322 }
323
324
325 public void listenForTransferProgress() {
326 if (mProgressListener != null && !mListening) {
327 if (mContainerActivity.getFileDownloaderBinder() != null) {
328 mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, mFile);
329 mListening = true;
330 setButtonsForTransferring();
331 }
332 }
333 }
334
335
336 public void leaveTransferProgress() {
337 if (mProgressListener != null) {
338 if (mContainerActivity.getFileDownloaderBinder() != null) {
339 mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, mFile);
340 mListening = false;
341 }
342 }
343 }
344
345
346 /**
347 * Helper class responsible for updating the progress bar shown for file uploading or downloading
348 *
349 * @author David A. Velasco
350 */
351 private class ProgressListener implements OnDatatransferProgressListener {
352 int mLastPercent = 0;
353 WeakReference<ProgressBar> mProgressBar = null;
354
355 ProgressListener(ProgressBar progressBar) {
356 mProgressBar = new WeakReference<ProgressBar>(progressBar);
357 }
358
359 @Override
360 public void onTransferProgress(long progressRate) {
361 // old method, nothing here
362 };
363
364 @Override
365 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) {
366 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
367 if (percent != mLastPercent) {
368 ProgressBar pb = mProgressBar.get();
369 if (pb != null) {
370 pb.setProgress(percent);
371 pb.postInvalidate();
372 }
373 }
374 mLastPercent = percent;
375 }
376
377 };
378
379
380
381 }