b090316fb4e1de17ebf00ebf87b4729bb7c28a04
[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.BroadcastReceiver;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.content.ServiceConnection;
29 import android.os.Bundle;
30 import android.os.IBinder;
31 import android.support.v4.view.ViewPager;
32 import android.util.Log;
33
34 import com.actionbarsherlock.app.ActionBar;
35 import com.actionbarsherlock.app.SherlockFragmentActivity;
36 import com.actionbarsherlock.view.MenuItem;
37 import com.owncloud.android.datamodel.DataStorageManager;
38 import com.owncloud.android.datamodel.FileDataStorageManager;
39 import com.owncloud.android.datamodel.OCFile;
40 import com.owncloud.android.files.services.FileDownloader;
41 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
42 import com.owncloud.android.files.services.FileUploader;
43 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
44 import com.owncloud.android.ui.activity.FileDetailActivity;
45 import com.owncloud.android.ui.fragment.FileDetailFragment;
46 import com.owncloud.android.ui.fragment.FileFragment;
47
48 import com.owncloud.android.AccountUtils;
49 import com.owncloud.android.R;
50
51 /**
52 * Used as an utility to preview image files contained in an ownCloud account.
53 *
54 * @author David A. Velasco
55 */
56 public class PreviewImageActivity extends SherlockFragmentActivity implements FileFragment.ContainerActivity, ViewPager.OnPageChangeListener {
57
58 public static final int DIALOG_SHORT_WAIT = 0;
59
60 public static final String TAG = PreviewImageActivity.class.getSimpleName();
61
62 public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
63 private static final String KEY_WAITING_FOR_BINDER = "WAITING_FOR_BINDER";
64
65 private OCFile mFile;
66 private OCFile mParentFolder;
67 private Account mAccount;
68 private DataStorageManager mStorageManager;
69
70 private ViewPager mViewPager;
71 private PreviewImagePagerAdapter mPreviewImagePagerAdapter;
72
73 private FileDownloaderBinder mDownloaderBinder = null;
74 private ServiceConnection mDownloadConnection, mUploadConnection = null;
75 private FileUploaderBinder mUploaderBinder = null;
76
77 private boolean mRequestWaitingForBinder;
78
79 private DownloadFinishReceiver mDownloadFinishReceiver;
80
81
82 @Override
83 protected void onCreate(Bundle savedInstanceState) {
84 super.onCreate(savedInstanceState);
85
86 mFile = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
87 mAccount = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
88 if (mFile == null) {
89 throw new IllegalStateException("Instanced with a NULL OCFile");
90 }
91 if (mAccount == null) {
92 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
93 }
94 if (!mFile.isImage()) {
95 throw new IllegalArgumentException("Non-image file passed as argument");
96 }
97
98 setContentView(R.layout.preview_image_activity);
99
100 ActionBar actionBar = getSupportActionBar();
101 actionBar.setDisplayHomeAsUpEnabled(true);
102 actionBar.setTitle(mFile.getFileName());
103
104 mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
105 mParentFolder = mStorageManager.getFileById(mFile.getParentId());
106 if (mParentFolder == null) {
107 // should not be necessary
108 mParentFolder = mStorageManager.getFileByPath(OCFile.PATH_SEPARATOR);
109 }
110
111 if (savedInstanceState != null) {
112 mRequestWaitingForBinder = savedInstanceState.getBoolean(KEY_WAITING_FOR_BINDER);
113 } else {
114 mRequestWaitingForBinder = false;
115 }
116
117 createViewPager();
118
119 }
120
121 private void createViewPager() {
122 mPreviewImagePagerAdapter = new PreviewImagePagerAdapter(getSupportFragmentManager(), mParentFolder, mAccount, mStorageManager);
123 mViewPager = (ViewPager) findViewById(R.id.fragmentPager);
124 int position = mPreviewImagePagerAdapter.getFilePosition(mFile);
125 position = (position >= 0) ? position : 0;
126 mViewPager.setAdapter(mPreviewImagePagerAdapter);
127 mViewPager.setOnPageChangeListener(this);
128 Log.e(TAG, "Setting initial position " + position);
129 mViewPager.setCurrentItem(position);
130 if (position == 0 && !mFile.isDown()) {
131 // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0)
132 mRequestWaitingForBinder = true;
133 }
134 }
135
136
137 @Override
138 public void onStart() {
139 super.onStart();
140 Log.e(TAG, "PREVIEW ACTIVITY ON START");
141 mDownloadConnection = new PreviewImageServiceConnection();
142 bindService(new Intent(this, FileDownloader.class), mDownloadConnection, Context.BIND_AUTO_CREATE);
143 mUploadConnection = new PreviewImageServiceConnection();
144 bindService(new Intent(this, FileUploader.class), mUploadConnection, Context.BIND_AUTO_CREATE);
145 }
146
147 @Override
148 protected void onSaveInstanceState(Bundle outState) {
149 super.onSaveInstanceState(outState);
150 outState.putBoolean(KEY_WAITING_FOR_BINDER, mRequestWaitingForBinder);
151 }
152
153
154 /** Defines callbacks for service binding, passed to bindService() */
155 private class PreviewImageServiceConnection implements ServiceConnection {
156
157 @Override
158 public void onServiceConnected(ComponentName component, IBinder service) {
159
160 if (component.equals(new ComponentName(PreviewImageActivity.this, FileDownloader.class))) {
161 Log.e(TAG, "PREVIEW_IMAGE Download service connected");
162 mDownloaderBinder = (FileDownloaderBinder) service;
163 if (mRequestWaitingForBinder) {
164 mRequestWaitingForBinder = false;
165 Log.e(TAG, "Simulating reselection of current page after connection of download binder");
166 onPageSelected(mViewPager.getCurrentItem());
167 }
168
169 } else if (component.equals(new ComponentName(PreviewImageActivity.this, FileUploader.class))) {
170 Log.d(TAG, "Upload service connected");
171 mUploaderBinder = (FileUploaderBinder) service;
172 } else {
173 return;
174 }
175
176 }
177
178 @Override
179 public void onServiceDisconnected(ComponentName component) {
180 if (component.equals(new ComponentName(PreviewImageActivity.this, FileDownloader.class))) {
181 Log.d(TAG, "Download service suddenly disconnected");
182 mDownloaderBinder = null;
183 } else if (component.equals(new ComponentName(PreviewImageActivity.this, FileUploader.class))) {
184 Log.d(TAG, "Upload service suddenly disconnected");
185 mUploaderBinder = null;
186 }
187 }
188 };
189
190
191 @Override
192 public void onStop() {
193 super.onStop();
194 if (mDownloadConnection != null) {
195 unbindService(mDownloadConnection);
196 mDownloadConnection = null;
197 }
198 if (mUploadConnection != null) {
199 unbindService(mUploadConnection);
200 mUploadConnection = null;
201 }
202 }
203
204
205 @Override
206 public void onDestroy() {
207 super.onDestroy();
208 }
209
210
211 @Override
212 public boolean onOptionsItemSelected(MenuItem item) {
213 boolean returnValue = false;
214
215 switch(item.getItemId()){
216 case android.R.id.home:
217 backToDisplayActivity();
218 returnValue = true;
219 break;
220 default:
221 returnValue = super.onOptionsItemSelected(item);
222 }
223
224 return returnValue;
225 }
226
227
228 @Override
229 protected void onResume() {
230 super.onResume();
231 Log.e(TAG, "PREVIEW ACTIVITY ONRESUME");
232 mDownloadFinishReceiver = new DownloadFinishReceiver();
233 IntentFilter filter = new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
234 registerReceiver(mDownloadFinishReceiver, filter);
235 }
236
237
238 @Override
239 protected void onPostResume() {
240 super.onPostResume();
241 Log.e(TAG, "PREVIEW ACTIVITY ONPOSTRESUME");
242 }
243
244 @Override
245 public void onPause() {
246 super.onPause();
247 unregisterReceiver(mDownloadFinishReceiver);
248 mDownloadFinishReceiver = null;
249 }
250
251
252 private void backToDisplayActivity() {
253 /*
254 Intent intent = new Intent(this, FileDisplayActivity.class);
255 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
256 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
257 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
258 startActivity(intent);
259 */
260 finish();
261 }
262
263
264 @Override
265 protected Dialog onCreateDialog(int id) {
266 Dialog dialog = null;
267 switch (id) {
268 case DIALOG_SHORT_WAIT: {
269 ProgressDialog working_dialog = new ProgressDialog(this);
270 working_dialog.setMessage(getResources().getString(
271 R.string.wait_a_moment));
272 working_dialog.setIndeterminate(true);
273 working_dialog.setCancelable(false);
274 dialog = working_dialog;
275 break;
276 }
277 default:
278 dialog = null;
279 }
280 return dialog;
281 }
282
283
284 /**
285 * {@inheritDoc}
286 */
287 @Override
288 public void onFileStateChanged() {
289 // nothing to do here!
290 }
291
292
293 /**
294 * {@inheritDoc}
295 */
296 @Override
297 public FileDownloaderBinder getFileDownloaderBinder() {
298 return mDownloaderBinder;
299 }
300
301
302 @Override
303 public FileUploaderBinder getFileUploaderBinder() {
304 return mUploaderBinder;
305 }
306
307
308 @Override
309 public void showFragmentWithDetails(OCFile file) {
310 Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
311 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, file);
312 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, AccountUtils.getCurrentOwnCloudAccount(this));
313 showDetailsIntent.putExtra(FileDetailActivity.EXTRA_MODE, FileDetailActivity.MODE_DETAILS);
314 startActivity(showDetailsIntent);
315 }
316
317
318 private void requestForDownload(OCFile file) {
319 Log.e(TAG, "REQUEST FOR DOWNLOAD : " + file.getFileName());
320 if (mDownloaderBinder == null) {
321 Log.e(TAG, "requestForDownload called without binder to download service");
322
323 } else if (!mDownloaderBinder.isDownloading(mAccount, file)) {
324 Intent i = new Intent(this, FileDownloader.class);
325 i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount);
326 i.putExtra(FileDownloader.EXTRA_FILE, file);
327 startService(i);
328 }
329 }
330
331 @Override
332 public void notifySuccessfulDownload(OCFile file, Intent intent, boolean success) {
333 /*
334 if (success) {
335 if (mWaitingToPreview != null && mWaitingToPreview.equals(file)) {
336 mWaitingToPreview = null;
337 int position = mViewPager.getCurrentItem();
338 mPreviewImagePagerAdapter.updateFile(position, file);
339 Log.e(TAG, "BEFORE NOTIFY DATA SET CHANGED");
340 mPreviewImagePagerAdapter.notifyDataSetChanged();
341 Log.e(TAG, "AFTER NOTIFY DATA SET CHANGED");
342 }
343 }
344 */
345 }
346
347
348 /**
349 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
350 *
351 * @param Position Position index of the new selected page
352 */
353 @Override
354 public void onPageSelected(int position) {
355 Log.e(TAG, "onPageSelected " + position);
356 if (mDownloaderBinder == null) {
357 mRequestWaitingForBinder = true;
358
359 } else {
360 OCFile currentFile = mPreviewImagePagerAdapter.getFileAt(position);
361 getSupportActionBar().setTitle(currentFile.getFileName());
362 if (!currentFile.isDown()) {
363 requestForDownload(currentFile);
364 //updateCurrentDownloadFragment(true);
365 }
366 }
367 }
368
369 /**
370 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
371 * when the pager is automatically settling to the current page, or when it is fully stopped/idle.
372 *
373 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
374 */
375 @Override
376 public void onPageScrollStateChanged(int state) {
377 }
378
379 /**
380 * This method will be invoked when the current page is scrolled, either as part of a programmatically
381 * initiated smooth scroll or a user initiated touch scroll.
382 *
383 * @param position Position index of the first page currently being displayed.
384 * Page position+1 will be visible if positionOffset is nonzero.
385 *
386 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
387 * @param positionOffsetPixels Value in pixels indicating the offset from position.
388 */
389 @Override
390 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
391 }
392
393
394 private void updateCurrentDownloadFragment(boolean transferring) {
395 FileFragment fragment = mPreviewImagePagerAdapter.getFragmentAt(mViewPager.getCurrentItem());
396 if (fragment instanceof FileDownloadFragment) {
397 ((FileDownloadFragment) fragment).updateView(transferring);
398 //mViewPager.invalidate();
399 }
400 }
401
402
403 /**
404 * Class waiting for broadcast events from the {@link FielDownloader} service.
405 *
406 * Updates the UI when a download is started or finished, provided that it is relevant for the
407 * folder displayed in the gallery.
408 */
409 private class DownloadFinishReceiver extends BroadcastReceiver {
410 @Override
411 public void onReceive(Context context, Intent intent) {
412 String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
413 String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
414 if (mAccount.name.equals(accountName) &&
415 downloadedRemotePath != null) {
416
417 OCFile file = mStorageManager.getFileByPath(downloadedRemotePath);
418 int position = mPreviewImagePagerAdapter.getFilePosition(file);
419 boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);
420 boolean isCurrent = (mViewPager.getCurrentItem() == position);
421
422 if (position >= 0) {
423 /// ITS MY BUSSINESS
424 Log.e(TAG, "downloaded file FOUND in adapter");
425 if (downloadWasFine) {
426 mPreviewImagePagerAdapter.updateFile(position, file);
427 //Log.e(TAG, "BEFORE NOTIFY DATA SET CHANGED");
428 mPreviewImagePagerAdapter.notifyDataSetChanged();
429 //Log.e(TAG, "AFTER NOTIFY DATA SET CHANGED");
430
431 } else if (isCurrent) {
432 updateCurrentDownloadFragment(false);
433 }
434
435 } else {
436 Log.e(TAG, "DOWNLOADED FILE NOT FOUND IN ADAPTER ");
437 }
438
439 }
440 removeStickyBroadcast(intent);
441 }
442
443 }
444
445
446 }