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