Fixed automatic download for a not downloaded image file when previewing it
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewImageActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18 package com.owncloud.android.ui.preview;
19
20 import android.accounts.Account;
21 import android.app.Dialog;
22 import android.app.ProgressDialog;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.ServiceConnection;
27 import android.os.Bundle;
28 import android.os.IBinder;
29 import android.support.v4.view.ViewPager;
30 import android.util.Log;
31
32 import com.actionbarsherlock.app.ActionBar;
33 import com.actionbarsherlock.app.SherlockFragmentActivity;
34 import com.actionbarsherlock.view.MenuItem;
35 import com.owncloud.android.datamodel.DataStorageManager;
36 import com.owncloud.android.datamodel.FileDataStorageManager;
37 import com.owncloud.android.datamodel.OCFile;
38 import com.owncloud.android.files.services.FileDownloader;
39 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
40 import com.owncloud.android.files.services.FileUploader;
41 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
42 import com.owncloud.android.ui.activity.FileDetailActivity;
43 import com.owncloud.android.ui.fragment.FileDetailFragment;
44 import com.owncloud.android.ui.fragment.FileFragment;
45
46 import com.owncloud.android.AccountUtils;
47 import com.owncloud.android.R;
48
49 /**
50 * Used as an utility to preview image files contained in an ownCloud account.
51 *
52 * @author David A. Velasco
53 */
54 public class PreviewImageActivity extends SherlockFragmentActivity implements FileFragment.ContainerActivity, ViewPager.OnPageChangeListener {
55
56 public static final int DIALOG_SHORT_WAIT = 0;
57
58 public static final String TAG = PreviewImageActivity.class.getSimpleName();
59
60 public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
61 private static final String KEY_WAITING_FOR_BINDER = "WAITING_FOR_BINDER";
62
63 private OCFile mFile;
64 private OCFile mParentFolder;
65 private Account mAccount;
66 private DataStorageManager mStorageManager;
67
68 private ViewPager mViewPager;
69 private PreviewImagePagerAdapter mPreviewImagePagerAdapter;
70
71 private FileDownloaderBinder mDownloaderBinder = null;
72 private ServiceConnection mDownloadConnection, mUploadConnection = null;
73 private FileUploaderBinder mUploaderBinder = null;
74 private OCFile mWaitingToPreview = null;
75
76 private boolean mRequestWaitingForBinder;
77
78
79 @Override
80 protected void onCreate(Bundle savedInstanceState) {
81 super.onCreate(savedInstanceState);
82
83 mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
84 mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
85 if (mFile == null) {
86 throw new IllegalStateException("Instanced with a NULL OCFile");
87 }
88 if (mAccount == null) {
89 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
90 }
91 if (!mFile.isImage()) {
92 throw new IllegalArgumentException("Non-image file passed as argument");
93 }
94
95 setContentView(R.layout.preview_image_activity);
96
97 ActionBar actionBar = getSupportActionBar();
98 actionBar.setDisplayHomeAsUpEnabled(true);
99 actionBar.setTitle(mFile.getFileName());
100
101 mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
102 mParentFolder = mStorageManager.getFileById(mFile.getParentId());
103 if (mParentFolder == null) {
104 // should not be necessary
105 mParentFolder = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);
106 }
107
108 if (savedInstanceState != null) {
109 mWaitingToPreview = (OCFile) savedInstanceState.getParcelable(KEY_WAITING_TO_PREVIEW);
110 mRequestWaitingForBinder = savedInstanceState.getBoolean(KEY_WAITING_FOR_BINDER);
111 } else {
112 mWaitingToPreview = null;
113 mRequestWaitingForBinder = false;
114 }
115
116 createViewPager();
117
118 mDownloadConnection = new PreviewImageServiceConnection();
119 bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
120 mUploadConnection = new PreviewImageServiceConnection();
121 bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
122
123 }
124
125 private void createViewPager() {
126 mPreviewImagePagerAdapter = new PreviewImagePagerAdapter(getSupportFragmentManager(), mParentFolder, mAccount, mStorageManager);
127 mViewPager = (ViewPager) findViewById(R.id.fragmentPager);
128 int position = mPreviewImagePagerAdapter.getFilePosition(mFile);
129 Log.e(TAG, "Setting initial position " + position);
130 mViewPager.setAdapter(mPreviewImagePagerAdapter);
131 mViewPager.setOnPageChangeListener(this);
132 mViewPager.setCurrentItem((position >= 0) ? position : 0);
133 }
134
135
136 @Override
137 protected void onSaveInstanceState(Bundle outState) {
138 super.onSaveInstanceState(outState);
139 outState.putParcelable(KEY_WAITING_TO_PREVIEW, mWaitingToPreview);
140 outState.putBoolean(KEY_WAITING_FOR_BINDER, mRequestWaitingForBinder);
141 }
142
143
144 /** Defines callbacks for service binding, passed to bindService() */
145 private class PreviewImageServiceConnection implements ServiceConnection {
146
147 @Override
148 public void onServiceConnected(ComponentName component, IBinder service) {
149
150 if (component.equals(new ComponentName(PreviewImageActivity.this, FileDownloader.class))) {
151 Log.d(TAG, "Download service connected");
152 mDownloaderBinder = (FileDownloaderBinder) service;
153 if (mRequestWaitingForBinder) {
154 if (mWaitingToPreview != null) {
155 requestForDownload();
156 }
157 mRequestWaitingForBinder = false;
158 }
159
160 } else if (component.equals(new ComponentName(PreviewImageActivity.this, FileUploader.class))) {
161 Log.d(TAG, "Upload service connected");
162 mUploaderBinder = (FileUploaderBinder) service;
163 } else {
164 return;
165 }
166
167 }
168
169 @Override
170 public void onServiceDisconnected(ComponentName component) {
171 if (component.equals(new ComponentName(PreviewImageActivity.this, FileDownloader.class))) {
172 Log.d(TAG, "Download service suddenly disconnected");
173 mDownloaderBinder = null;
174 } else if (component.equals(new ComponentName(PreviewImageActivity.this, FileUploader.class))) {
175 Log.d(TAG, "Upload service suddenly disconnected");
176 mUploaderBinder = null;
177 }
178 }
179 };
180
181
182 @Override
183 public void onDestroy() {
184 super.onDestroy();
185 if (mDownloadConnection != null) {
186 unbindService(mDownloadConnection);
187 mDownloadConnection = null;
188 }
189 if (mUploadConnection != null) {
190 unbindService(mUploadConnection);
191 mUploadConnection = null;
192 }
193 }
194
195
196 @Override
197 public boolean onOptionsItemSelected(MenuItem item) {
198 boolean returnValue = false;
199
200 switch(item.getItemId()){
201 case android.R.id.home:
202 backToDisplayActivity();
203 returnValue = true;
204 break;
205 default:
206 returnValue = super.onOptionsItemSelected(item);
207 }
208
209 return returnValue;
210 }
211
212
213 @Override
214 protected void onResume() {
215 super.onResume();
216 }
217
218
219 private void backToDisplayActivity() {
220 /*
221 Intent intent = new Intent(this, FileDisplayActivity.class);
222 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
223 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
224 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
225 startActivity(intent);
226 */
227 finish();
228 }
229
230
231 @Override
232 protected Dialog onCreateDialog(int id) {
233 Dialog dialog = null;
234 switch (id) {
235 case DIALOG_SHORT_WAIT: {
236 ProgressDialog working_dialog = new ProgressDialog(this);
237 working_dialog.setMessage(getResources().getString(
238 R.string.wait_a_moment));
239 working_dialog.setIndeterminate(true);
240 working_dialog.setCancelable(false);
241 dialog = working_dialog;
242 break;
243 }
244 default:
245 dialog = null;
246 }
247 return dialog;
248 }
249
250
251 /**
252 * {@inheritDoc}
253 */
254 @Override
255 public void onFileStateChanged() {
256 // nothing to do here!
257 }
258
259
260 /**
261 * {@inheritDoc}
262 */
263 @Override
264 public FileDownloaderBinder getFileDownloaderBinder() {
265 return mDownloaderBinder;
266 }
267
268
269 @Override
270 public FileUploaderBinder getFileUploaderBinder() {
271 return mUploaderBinder;
272 }
273
274
275 @Override
276 public void showFragmentWithDetails(OCFile file) {
277 Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
278 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
279 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
280 showDetailsIntent.putExtra(FileDetailActivity.EXTRA_MODE, FileDetailActivity.MODE_DETAILS);
281 startActivity(showDetailsIntent);
282 }
283
284
285 private void requestForDownload() {
286 Log.e(TAG, "REQUEST FOR DOWNLOAD : " + mWaitingToPreview.getFileName());
287 if (mDownloaderBinder == null) {
288 mRequestWaitingForBinder = true;
289
290 } else if (!mDownloaderBinder.isDownloading(mAccount, mWaitingToPreview)) {
291 Intent i = new Intent(this, FileDownloader.class);
292 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
293 i.putExtra(FileDownloader.EXTRA_FILE, mWaitingToPreview);
294 startService(i);
295 }
296 mViewPager.invalidate();
297 }
298
299 @Override
300 public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {
301 if (success) {
302 if (mWaitingToPreview != null && mWaitingToPreview.equals(file)) {
303 mWaitingToPreview = null;
304 int position = mViewPager.getCurrentItem();
305 mPreviewImagePagerAdapter.updateFile(position, file);
306 Log.e(TAG, "BEFORE NOTIFY DATA SET CHANGED");
307 mPreviewImagePagerAdapter.notifyDataSetChanged();
308 Log.e(TAG, "AFTER NOTIFY DATA SET CHANGED");
309 }
310 }
311 }
312
313
314 /**
315 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
316 *
317 * @param Position Position index of the new selected page
318 */
319 @Override
320 public void onPageSelected(int position) {
321 Log.e(TAG, "onPageSelected " + position);
322 OCFile currentFile = mPreviewImagePagerAdapter.getFileAt(position);
323 getSupportActionBar().setTitle(currentFile.getFileName());
324 if (currentFile.isDown()) {
325 mWaitingToPreview = null;
326 } else {
327 mWaitingToPreview = currentFile;
328 requestForDownload();
329 }
330 }
331
332 /**
333 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
334 * when the pager is automatically settling to the current page, or when it is fully stopped/idle.
335 *
336 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
337 */
338 @Override
339 public void onPageScrollStateChanged(int state) {
340 }
341
342 /**
343 * This method will be invoked when the current page is scrolled, either as part of a programmatically
344 * initiated smooth scroll or a user initiated touch scroll.
345 *
346 * @param position Position index of the first page currently being displayed.
347 * Page position+1 will be visible if positionOffset is nonzero.
348 *
349 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
350 * @param positionOffsetPixels Value in pixels indicating the offset from position.
351 */
352 @Override
353 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
354 }
355
356 }