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 android
.accounts
.Account
;
20 import android
.app
.Dialog
;
21 import android
.app
.ProgressDialog
;
22 import android
.content
.BroadcastReceiver
;
23 import android
.content
.ComponentName
;
24 import android
.content
.Context
;
25 import android
.content
.Intent
;
26 import android
.content
.IntentFilter
;
27 import android
.content
.ServiceConnection
;
28 import android
.os
.Bundle
;
29 import android
.os
.IBinder
;
30 import android
.support
.v4
.view
.ViewPager
;
31 import android
.view
.MotionEvent
;
32 import android
.view
.View
;
33 import android
.view
.View
.OnTouchListener
;
35 import com
.actionbarsherlock
.app
.ActionBar
;
36 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
37 import com
.actionbarsherlock
.view
.MenuItem
;
38 import com
.actionbarsherlock
.view
.Window
;
39 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
40 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
41 import com
.owncloud
.android
.datamodel
.OCFile
;
42 import com
.owncloud
.android
.files
.services
.FileDownloader
;
43 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
44 import com
.owncloud
.android
.files
.services
.FileUploader
;
45 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
46 import com
.owncloud
.android
.ui
.activity
.FileDetailActivity
;
47 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
48 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
50 import com
.owncloud
.android
.AccountUtils
;
51 import com
.owncloud
.android
.Log_OC
;
52 import com
.owncloud
.android
.R
;
55 * Used as an utility to preview image files contained in an ownCloud account.
57 * @author David A. Velasco
59 public class PreviewImageActivity
extends SherlockFragmentActivity
implements FileFragment
.ContainerActivity
, ViewPager
.OnPageChangeListener
, OnTouchListener
{
61 public static final int DIALOG_SHORT_WAIT
= 0;
63 public static final String TAG
= PreviewImageActivity
.class.getSimpleName();
65 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
66 private static final String KEY_WAITING_FOR_BINDER
= "WAITING_FOR_BINDER";
69 private OCFile mParentFolder
;
70 private Account mAccount
;
71 private DataStorageManager mStorageManager
;
73 private ViewPager mViewPager
;
74 private PreviewImagePagerAdapter mPreviewImagePagerAdapter
;
76 private FileDownloaderBinder mDownloaderBinder
= null
;
77 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
78 private FileUploaderBinder mUploaderBinder
= null
;
80 private boolean mRequestWaitingForBinder
;
82 private DownloadFinishReceiver mDownloadFinishReceiver
;
84 private boolean mFullScreen
;
88 protected void onCreate(Bundle savedInstanceState
) {
89 super.onCreate(savedInstanceState
);
91 mFile
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
92 mAccount
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
94 throw new IllegalStateException("Instanced with a NULL OCFile");
96 if (mAccount
== null
) {
97 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
99 if (!mFile
.isImage()) {
100 throw new IllegalArgumentException("Non-image file passed as argument");
102 requestWindowFeature(Window
.FEATURE_ACTION_BAR_OVERLAY
);
103 setContentView(R
.layout
.preview_image_activity
);
105 ActionBar actionBar
= getSupportActionBar();
106 actionBar
.setDisplayHomeAsUpEnabled(true
);
107 actionBar
.setTitle(mFile
.getFileName());
112 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
113 mParentFolder
= mStorageManager
.getFileById(mFile
.getParentId());
114 if (mParentFolder
== null
) {
115 // should not be necessary
116 mParentFolder
= mStorageManager
.getFileByPath(OCFile
.PATH_SEPARATOR
);
119 if (savedInstanceState
!= null
) {
120 mRequestWaitingForBinder
= savedInstanceState
.getBoolean(KEY_WAITING_FOR_BINDER
);
122 mRequestWaitingForBinder
= false
;
129 private void createViewPager() {
130 mPreviewImagePagerAdapter
= new PreviewImagePagerAdapter(getSupportFragmentManager(), mParentFolder
, mAccount
, mStorageManager
);
131 mViewPager
= (ViewPager
) findViewById(R
.id
.fragmentPager
);
132 int position
= mPreviewImagePagerAdapter
.getFilePosition(mFile
);
133 position
= (position
>= 0) ? position
: 0;
134 mViewPager
.setAdapter(mPreviewImagePagerAdapter
);
135 mViewPager
.setOnPageChangeListener(this);
136 mViewPager
.setCurrentItem(position
);
137 if (position
== 0 && !mFile
.isDown()) {
138 // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0)
139 mRequestWaitingForBinder
= true
;
145 public void onStart() {
147 mDownloadConnection
= new PreviewImageServiceConnection();
148 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
149 mUploadConnection
= new PreviewImageServiceConnection();
150 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
154 protected void onSaveInstanceState(Bundle outState
) {
155 super.onSaveInstanceState(outState
);
156 outState
.putBoolean(KEY_WAITING_FOR_BINDER
, mRequestWaitingForBinder
);
160 /** Defines callbacks for service binding, passed to bindService() */
161 private class PreviewImageServiceConnection
implements ServiceConnection
{
164 public void onServiceConnected(ComponentName component
, IBinder service
) {
166 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
167 mDownloaderBinder
= (FileDownloaderBinder
) service
;
168 if (mRequestWaitingForBinder
) {
169 mRequestWaitingForBinder
= false
;
170 Log_OC
.d(TAG
, "Simulating reselection of current page after connection of download binder");
171 onPageSelected(mViewPager
.getCurrentItem());
174 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
175 Log_OC
.d(TAG
, "Upload service connected");
176 mUploaderBinder
= (FileUploaderBinder
) service
;
184 public void onServiceDisconnected(ComponentName component
) {
185 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
186 Log_OC
.d(TAG
, "Download service suddenly disconnected");
187 mDownloaderBinder
= null
;
188 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
189 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
190 mUploaderBinder
= null
;
197 public void onStop() {
199 if (mDownloadConnection
!= null
) {
200 unbindService(mDownloadConnection
);
201 mDownloadConnection
= null
;
203 if (mUploadConnection
!= null
) {
204 unbindService(mUploadConnection
);
205 mUploadConnection
= null
;
211 public void onDestroy() {
217 public boolean onOptionsItemSelected(MenuItem item
) {
218 boolean returnValue
= false
;
220 switch(item
.getItemId()){
221 case android
.R
.id
.home
:
222 backToDisplayActivity();
226 returnValue
= super.onOptionsItemSelected(item
);
234 protected void onResume() {
236 //Log.e(TAG, "ACTIVITY, ONRESUME");
237 mDownloadFinishReceiver
= new DownloadFinishReceiver();
238 IntentFilter filter
= new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
239 filter
.addAction(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
);
240 registerReceiver(mDownloadFinishReceiver
, filter
);
244 protected void onPostResume() {
245 //Log.e(TAG, "ACTIVITY, ONPOSTRESUME");
246 super.onPostResume();
250 public void onPause() {
252 unregisterReceiver(mDownloadFinishReceiver
);
253 mDownloadFinishReceiver
= null
;
257 private void backToDisplayActivity() {
259 Intent intent = new Intent(this, FileDisplayActivity.class);
260 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
261 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
262 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
263 startActivity(intent);
270 protected Dialog
onCreateDialog(int id
) {
271 Dialog dialog
= null
;
273 case DIALOG_SHORT_WAIT
: {
274 ProgressDialog working_dialog
= new ProgressDialog(this);
275 working_dialog
.setMessage(getResources().getString(
276 R
.string
.wait_a_moment
));
277 working_dialog
.setIndeterminate(true
);
278 working_dialog
.setCancelable(false
);
279 dialog
= working_dialog
;
293 public void onFileStateChanged() {
294 // nothing to do here!
302 public FileDownloaderBinder
getFileDownloaderBinder() {
303 return mDownloaderBinder
;
308 public FileUploaderBinder
getFileUploaderBinder() {
309 return mUploaderBinder
;
314 public void showFragmentWithDetails(OCFile file
) {
315 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
316 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
317 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
318 showDetailsIntent
.putExtra(FileDetailActivity
.EXTRA_MODE
, FileDetailActivity
.MODE_DETAILS
);
319 startActivity(showDetailsIntent
);
320 int pos
= mPreviewImagePagerAdapter
.getFilePosition(file
);
321 file
= mPreviewImagePagerAdapter
.getFileAt(pos
);
326 private void requestForDownload(OCFile file
) {
327 if (mDownloaderBinder
== null
) {
328 Log_OC
.d(TAG
, "requestForDownload called without binder to download service");
330 } else if (!mDownloaderBinder
.isDownloading(mAccount
, file
)) {
331 Intent i
= new Intent(this, FileDownloader
.class);
332 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
333 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
339 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
341 * @param Position Position index of the new selected page
344 public void onPageSelected(int position
) {
345 if (mDownloaderBinder
== null
) {
346 mRequestWaitingForBinder
= true
;
349 OCFile currentFile
= mPreviewImagePagerAdapter
.getFileAt(position
);
350 getSupportActionBar().setTitle(currentFile
.getFileName());
351 if (!currentFile
.isDown()) {
352 if (!mPreviewImagePagerAdapter
.pendingErrorAt(position
)) {
353 requestForDownload(currentFile
);
360 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
361 * when the pager is automatically settling to the current page. when it is fully stopped/idle.
363 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
366 public void onPageScrollStateChanged(int state
) {
370 * This method will be invoked when the current page is scrolled, either as part of a programmatically
371 * initiated smooth scroll or a user initiated touch scroll.
373 * @param position Position index of the first page currently being displayed.
374 * Page position+1 will be visible if positionOffset is nonzero.
376 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
377 * @param positionOffsetPixels Value in pixels indicating the offset from position.
380 public void onPageScrolled(int position
, float positionOffset
, int positionOffsetPixels
) {
385 * Class waiting for broadcast events from the {@link FielDownloader} service.
387 * Updates the UI when a download is started or finished, provided that it is relevant for the
388 * folder displayed in the gallery.
390 private class DownloadFinishReceiver
extends BroadcastReceiver
{
392 public void onReceive(Context context
, Intent intent
) {
393 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
394 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
395 if (mAccount
.name
.equals(accountName
) &&
396 downloadedRemotePath
!= null
) {
398 OCFile file
= mStorageManager
.getFileByPath(downloadedRemotePath
);
399 int position
= mPreviewImagePagerAdapter
.getFilePosition(file
);
400 boolean downloadWasFine
= intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
);
401 //boolean isOffscreen = Math.abs((mViewPager.getCurrentItem() - position)) <= mViewPager.getOffscreenPageLimit();
403 if (position
>= 0 && intent
.getAction().equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
404 if (downloadWasFine
) {
405 mPreviewImagePagerAdapter
.updateFile(position
, file
);
408 mPreviewImagePagerAdapter
.updateWithDownloadError(position
);
410 mPreviewImagePagerAdapter
.notifyDataSetChanged(); // will trigger the creation of new fragments
413 Log_OC
.d(TAG
, "Download finished, but the fragment is offscreen");
417 removeStickyBroadcast(intent
);
424 public boolean onTouch(View v
, MotionEvent event
) {
425 if (event
.getAction() == MotionEvent
.ACTION_UP
) {
432 private void toggleFullScreen() {
433 ActionBar actionBar
= getSupportActionBar();
441 mFullScreen
= !mFullScreen
;