1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package com
.owncloud
.android
.ui
.preview
;
19 import org
.apache
.commons
.httpclient
.methods
.PostMethod
;
21 import android
.accounts
.Account
;
22 import android
.app
.Dialog
;
23 import android
.app
.ProgressDialog
;
24 import android
.content
.BroadcastReceiver
;
25 import android
.content
.ComponentName
;
26 import android
.content
.Context
;
27 import android
.content
.Intent
;
28 import android
.content
.IntentFilter
;
29 import android
.content
.ServiceConnection
;
30 import android
.os
.Bundle
;
31 import android
.os
.IBinder
;
32 import android
.support
.v4
.app
.Fragment
;
33 import android
.support
.v4
.view
.ViewPager
;
34 import android
.util
.Log
;
35 import android
.view
.MotionEvent
;
36 import android
.view
.View
;
37 import android
.view
.View
.OnTouchListener
;
39 import com
.actionbarsherlock
.app
.ActionBar
;
40 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
41 import com
.actionbarsherlock
.view
.MenuItem
;
42 import com
.actionbarsherlock
.view
.Window
;
43 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
44 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
45 import com
.owncloud
.android
.datamodel
.OCFile
;
46 import com
.owncloud
.android
.files
.services
.FileDownloader
;
47 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
48 import com
.owncloud
.android
.files
.services
.FileUploader
;
49 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
50 import com
.owncloud
.android
.ui
.activity
.FileDetailActivity
;
51 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
52 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
54 import com
.owncloud
.android
.AccountUtils
;
55 import com
.owncloud
.android
.R
;
58 * Used as an utility to preview image files contained in an ownCloud account.
60 * @author David A. Velasco
62 public class PreviewImageActivity
extends SherlockFragmentActivity
implements FileFragment
.ContainerActivity
, ViewPager
.OnPageChangeListener
, OnTouchListener
{
64 public static final int DIALOG_SHORT_WAIT
= 0;
66 public static final String TAG
= PreviewImageActivity
.class.getSimpleName();
68 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
69 private static final String KEY_WAITING_FOR_BINDER
= "WAITING_FOR_BINDER";
72 private OCFile mParentFolder
;
73 private Account mAccount
;
74 private DataStorageManager mStorageManager
;
76 private ViewPager mViewPager
;
77 private PreviewImagePagerAdapter mPreviewImagePagerAdapter
;
79 private FileDownloaderBinder mDownloaderBinder
= null
;
80 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
81 private FileUploaderBinder mUploaderBinder
= null
;
83 private boolean mRequestWaitingForBinder
;
85 private DownloadFinishReceiver mDownloadFinishReceiver
;
87 private boolean mFullScreen
;
91 protected void onCreate(Bundle savedInstanceState
) {
92 super.onCreate(savedInstanceState
);
94 mFile
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
95 mAccount
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
97 throw new IllegalStateException("Instanced with a NULL OCFile");
99 if (mAccount
== null
) {
100 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
102 if (!mFile
.isImage()) {
103 throw new IllegalArgumentException("Non-image file passed as argument");
105 requestWindowFeature(Window
.FEATURE_ACTION_BAR_OVERLAY
);
106 setContentView(R
.layout
.preview_image_activity
);
108 ActionBar actionBar
= getSupportActionBar();
109 actionBar
.setDisplayHomeAsUpEnabled(true
);
110 actionBar
.setTitle(mFile
.getFileName());
115 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
116 mParentFolder
= mStorageManager
.getFileById(mFile
.getParentId());
117 if (mParentFolder
== null
) {
118 // should not be necessary
119 mParentFolder
= mStorageManager
.getFileByPath(OCFile
.PATH_SEPARATOR
);
122 if (savedInstanceState
!= null
) {
123 mRequestWaitingForBinder
= savedInstanceState
.getBoolean(KEY_WAITING_FOR_BINDER
);
125 mRequestWaitingForBinder
= false
;
132 private void createViewPager() {
133 mPreviewImagePagerAdapter
= new PreviewImagePagerAdapter(getSupportFragmentManager(), mParentFolder
, mAccount
, mStorageManager
);
134 mViewPager
= (ViewPager
) findViewById(R
.id
.fragmentPager
);
135 int position
= mPreviewImagePagerAdapter
.getFilePosition(mFile
);
136 position
= (position
>= 0) ? position
: 0;
137 mViewPager
.setAdapter(mPreviewImagePagerAdapter
);
138 mViewPager
.setOnPageChangeListener(this);
139 mViewPager
.setCurrentItem(position
);
140 if (position
== 0 && !mFile
.isDown()) {
141 // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0)
142 mRequestWaitingForBinder
= true
;
148 public void onStart() {
150 mDownloadConnection
= new PreviewImageServiceConnection();
151 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
152 mUploadConnection
= new PreviewImageServiceConnection();
153 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
157 protected void onSaveInstanceState(Bundle outState
) {
158 super.onSaveInstanceState(outState
);
159 outState
.putBoolean(KEY_WAITING_FOR_BINDER
, mRequestWaitingForBinder
);
163 /** Defines callbacks for service binding, passed to bindService() */
164 private class PreviewImageServiceConnection
implements ServiceConnection
{
167 public void onServiceConnected(ComponentName component
, IBinder service
) {
169 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
170 mDownloaderBinder
= (FileDownloaderBinder
) service
;
171 if (mRequestWaitingForBinder
) {
172 mRequestWaitingForBinder
= false
;
173 Log
.d(TAG
, "Simulating reselection of current page after connection of download binder");
174 onPageSelected(mViewPager
.getCurrentItem());
177 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
178 Log
.d(TAG
, "Upload service connected");
179 mUploaderBinder
= (FileUploaderBinder
) service
;
187 public void onServiceDisconnected(ComponentName component
) {
188 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
189 Log
.d(TAG
, "Download service suddenly disconnected");
190 mDownloaderBinder
= null
;
191 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
192 Log
.d(TAG
, "Upload service suddenly disconnected");
193 mUploaderBinder
= null
;
200 public void onStop() {
202 if (mDownloadConnection
!= null
) {
203 unbindService(mDownloadConnection
);
204 mDownloadConnection
= null
;
206 if (mUploadConnection
!= null
) {
207 unbindService(mUploadConnection
);
208 mUploadConnection
= null
;
214 public void onDestroy() {
220 public boolean onOptionsItemSelected(MenuItem item
) {
221 boolean returnValue
= false
;
223 switch(item
.getItemId()){
224 case android
.R
.id
.home
:
225 backToDisplayActivity();
229 returnValue
= super.onOptionsItemSelected(item
);
237 protected void onResume() {
239 //Log.e(TAG, "ACTIVITY, ONRESUME");
240 mDownloadFinishReceiver
= new DownloadFinishReceiver();
241 IntentFilter filter
= new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
242 filter
.addAction(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
);
243 registerReceiver(mDownloadFinishReceiver
, filter
);
247 protected void onPostResume() {
248 //Log.e(TAG, "ACTIVITY, ONPOSTRESUME");
249 super.onPostResume();
253 public void onPause() {
255 unregisterReceiver(mDownloadFinishReceiver
);
256 mDownloadFinishReceiver
= null
;
260 private void backToDisplayActivity() {
262 Intent intent = new Intent(this, FileDisplayActivity.class);
263 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
264 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
265 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
266 startActivity(intent);
273 protected Dialog
onCreateDialog(int id
) {
274 Dialog dialog
= null
;
276 case DIALOG_SHORT_WAIT
: {
277 ProgressDialog working_dialog
= new ProgressDialog(this);
278 working_dialog
.setMessage(getResources().getString(
279 R
.string
.wait_a_moment
));
280 working_dialog
.setIndeterminate(true
);
281 working_dialog
.setCancelable(false
);
282 dialog
= working_dialog
;
296 public void onFileStateChanged() {
297 // nothing to do here!
305 public FileDownloaderBinder
getFileDownloaderBinder() {
306 return mDownloaderBinder
;
311 public FileUploaderBinder
getFileUploaderBinder() {
312 return mUploaderBinder
;
317 public void showFragmentWithDetails(OCFile file
) {
318 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
319 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
320 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
321 showDetailsIntent
.putExtra(FileDetailActivity
.EXTRA_MODE
, FileDetailActivity
.MODE_DETAILS
);
322 startActivity(showDetailsIntent
);
323 int pos
= mPreviewImagePagerAdapter
.getFilePosition(file
);
324 file
= mPreviewImagePagerAdapter
.getFileAt(pos
);
329 private void requestForDownload(OCFile file
) {
330 if (mDownloaderBinder
== null
) {
331 Log
.d(TAG
, "requestForDownload called without binder to download service");
333 } else if (!mDownloaderBinder
.isDownloading(mAccount
, file
)) {
334 Intent i
= new Intent(this, FileDownloader
.class);
335 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
336 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
342 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
344 * @param Position Position index of the new selected page
347 public void onPageSelected(int position
) {
348 if (mDownloaderBinder
== null
) {
349 mRequestWaitingForBinder
= true
;
352 OCFile currentFile
= mPreviewImagePagerAdapter
.getFileAt(position
);
353 getSupportActionBar().setTitle(currentFile
.getFileName());
354 if (!currentFile
.isDown()) {
355 if (!mPreviewImagePagerAdapter
.pendingErrorAt(position
)) {
356 requestForDownload(currentFile
);
363 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
364 * when the pager is automatically settling to the current page. when it is fully stopped/idle.
366 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
369 public void onPageScrollStateChanged(int state
) {
373 * This method will be invoked when the current page is scrolled, either as part of a programmatically
374 * initiated smooth scroll or a user initiated touch scroll.
376 * @param position Position index of the first page currently being displayed.
377 * Page position+1 will be visible if positionOffset is nonzero.
379 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
380 * @param positionOffsetPixels Value in pixels indicating the offset from position.
383 public void onPageScrolled(int position
, float positionOffset
, int positionOffsetPixels
) {
388 * Class waiting for broadcast events from the {@link FielDownloader} service.
390 * Updates the UI when a download is started or finished, provided that it is relevant for the
391 * folder displayed in the gallery.
393 private class DownloadFinishReceiver
extends BroadcastReceiver
{
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
) {
401 OCFile file
= mStorageManager
.getFileByPath(downloadedRemotePath
);
402 int position
= mPreviewImagePagerAdapter
.getFilePosition(file
);
403 boolean downloadWasFine
= intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
);
404 //boolean isOffscreen = Math.abs((mViewPager.getCurrentItem() - position)) <= mViewPager.getOffscreenPageLimit();
406 if (position
>= 0 && intent
.getAction().equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
407 if (downloadWasFine
) {
408 mPreviewImagePagerAdapter
.updateFile(position
, file
);
411 mPreviewImagePagerAdapter
.updateWithDownloadError(position
);
413 mPreviewImagePagerAdapter
.notifyDataSetChanged(); // will trigger the creation of new fragments
416 Log
.d(TAG
, "Download finished, but the fragment is offscreen");
420 removeStickyBroadcast(intent
);
427 public boolean onTouch(View v
, MotionEvent event
) {
428 if (event
.getAction() == MotionEvent
.ACTION_UP
) {
435 private void toggleFullScreen() {
436 ActionBar actionBar
= getSupportActionBar();
444 mFullScreen
= !mFullScreen
;