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
.app
.Dialog
;
20 import android
.app
.ProgressDialog
;
21 import android
.content
.BroadcastReceiver
;
22 import android
.content
.ComponentName
;
23 import android
.content
.Context
;
24 import android
.content
.Intent
;
25 import android
.content
.IntentFilter
;
26 import android
.content
.ServiceConnection
;
27 import android
.os
.Bundle
;
28 import android
.os
.IBinder
;
29 import android
.support
.v4
.app
.Fragment
;
30 import android
.support
.v4
.app
.FragmentManager
;
31 import android
.support
.v4
.app
.FragmentTransaction
;
32 import android
.support
.v4
.view
.ViewPager
;
33 import android
.view
.MotionEvent
;
34 import android
.view
.View
;
35 import android
.view
.View
.OnTouchListener
;
37 import com
.actionbarsherlock
.app
.ActionBar
;
38 import com
.actionbarsherlock
.view
.MenuItem
;
39 import com
.actionbarsherlock
.view
.Window
;
40 import com
.owncloud
.android
.authentication
.AccountUtils
;
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
.FileActivity
;
49 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
50 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
51 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
53 import com
.owncloud
.android
.Log_OC
;
54 import com
.owncloud
.android
.R
;
57 * Holds a swiping galley where image files contained in an ownCloud directory are shown
59 * @author David A. Velasco
61 public class PreviewImageActivity
extends FileActivity
implements FileFragment
.ContainerActivity
, ViewPager
.OnPageChangeListener
, OnTouchListener
{
63 public static final int DIALOG_SHORT_WAIT
= 0;
65 public static final String TAG
= PreviewImageActivity
.class.getSimpleName();
67 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
68 private static final String KEY_WAITING_FOR_BINDER
= "WAITING_FOR_BINDER";
70 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
72 private DataStorageManager mStorageManager
;
74 private ViewPager mViewPager
;
75 private PreviewImagePagerAdapter mPreviewImagePagerAdapter
;
77 private FileDownloaderBinder mDownloaderBinder
= null
;
78 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
79 private FileUploaderBinder mUploaderBinder
= null
;
81 private boolean mRequestWaitingForBinder
;
83 private DownloadFinishReceiver mDownloadFinishReceiver
;
85 private boolean mFullScreen
;
89 protected void onCreate(Bundle savedInstanceState
) {
90 super.onCreate(savedInstanceState
);
92 requestWindowFeature(Window
.FEATURE_ACTION_BAR_OVERLAY
);
93 setContentView(R
.layout
.preview_image_activity
);
95 ActionBar actionBar
= getSupportActionBar();
96 actionBar
.setDisplayHomeAsUpEnabled(true
);
100 if (savedInstanceState
!= null
) {
101 mRequestWaitingForBinder
= savedInstanceState
.getBoolean(KEY_WAITING_FOR_BINDER
);
103 mRequestWaitingForBinder
= false
;
107 private void initViewPager() {
108 OCFile parentFolder
= mStorageManager
.getFileById(getFile().getParentId());
109 if (parentFolder
== null
) {
110 // should not be necessary
111 parentFolder
= mStorageManager
.getFileByPath(OCFile
.PATH_SEPARATOR
);
113 mPreviewImagePagerAdapter
= new PreviewImagePagerAdapter(getSupportFragmentManager(), parentFolder
, getAccount(), mStorageManager
);
114 mViewPager
= (ViewPager
) findViewById(R
.id
.fragmentPager
);
115 int position
= mPreviewImagePagerAdapter
.getFilePosition(getFile());
116 position
= (position
>= 0) ? position
: 0;
117 mViewPager
.setAdapter(mPreviewImagePagerAdapter
);
118 mViewPager
.setOnPageChangeListener(this);
119 mViewPager
.setCurrentItem(position
);
120 if (position
== 0 && !getFile().isDown()) {
121 // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0)
122 mRequestWaitingForBinder
= true
;
128 public void onStart() {
130 mDownloadConnection
= new PreviewImageServiceConnection();
131 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
132 mUploadConnection
= new PreviewImageServiceConnection();
133 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
137 protected void onSaveInstanceState(Bundle outState
) {
138 super.onSaveInstanceState(outState
);
139 outState
.putBoolean(KEY_WAITING_FOR_BINDER
, mRequestWaitingForBinder
);
143 /** Defines callbacks for service binding, passed to bindService() */
144 private class PreviewImageServiceConnection
implements ServiceConnection
{
147 public void onServiceConnected(ComponentName component
, IBinder service
) {
149 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
150 mDownloaderBinder
= (FileDownloaderBinder
) service
;
151 if (mRequestWaitingForBinder
) {
152 mRequestWaitingForBinder
= false
;
153 Log_OC
.d(TAG
, "Simulating reselection of current page after connection of download binder");
154 onPageSelected(mViewPager
.getCurrentItem());
157 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
158 Log_OC
.d(TAG
, "Upload service connected");
159 mUploaderBinder
= (FileUploaderBinder
) service
;
167 public void onServiceDisconnected(ComponentName component
) {
168 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
169 Log_OC
.d(TAG
, "Download service suddenly disconnected");
170 mDownloaderBinder
= null
;
171 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
172 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
173 mUploaderBinder
= null
;
180 public void onStop() {
182 if (mDownloadConnection
!= null
) {
183 unbindService(mDownloadConnection
);
184 mDownloadConnection
= null
;
186 if (mUploadConnection
!= null
) {
187 unbindService(mUploadConnection
);
188 mUploadConnection
= null
;
194 public void onDestroy() {
200 public boolean onOptionsItemSelected(MenuItem item
) {
201 boolean returnValue
= false
;
203 switch(item
.getItemId()){
204 case android
.R
.id
.home
:
205 backToDisplayActivity();
209 returnValue
= super.onOptionsItemSelected(item
);
217 protected void onResume() {
219 //Log.e(TAG, "ACTIVITY, ONRESUME");
220 mDownloadFinishReceiver
= new DownloadFinishReceiver();
221 IntentFilter filter
= new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
222 filter
.addAction(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
);
223 registerReceiver(mDownloadFinishReceiver
, filter
);
227 protected void onPostResume() {
228 //Log.e(TAG, "ACTIVITY, ONPOSTRESUME");
229 super.onPostResume();
233 public void onPause() {
235 unregisterReceiver(mDownloadFinishReceiver
);
236 mDownloadFinishReceiver
= null
;
240 private void backToDisplayActivity() {
246 // protected Dialog onCreateDialog(int id) {
247 // Dialog dialog = null;
249 // case DIALOG_SHORT_WAIT: {
250 // ProgressDialog working_dialog = new ProgressDialog(this);
251 // working_dialog.setMessage(getResources().getString(
252 // R.string.wait_a_moment));
253 // working_dialog.setIndeterminate(true);
254 // working_dialog.setCancelable(false);
255 // dialog = working_dialog;
266 * Show loading dialog
268 public void showLoadingDialog() {
270 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
271 FragmentManager fm
= getSupportFragmentManager();
272 FragmentTransaction ft
= fm
.beginTransaction();
273 loading
.show(ft
, DIALOG_WAIT_TAG
);
278 * Dismiss loading dialog
280 public void dismissLoadingDialog(){
281 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
283 LoadingDialog loading
= (LoadingDialog
) frag
;
292 public void onFileStateChanged() {
293 // nothing to do here!
301 public FileDownloaderBinder
getFileDownloaderBinder() {
302 return mDownloaderBinder
;
307 public FileUploaderBinder
getFileUploaderBinder() {
308 return mUploaderBinder
;
313 public void showDetails(OCFile file
) {
314 Intent showDetailsIntent
= new Intent(this, FileDisplayActivity
.class);
315 showDetailsIntent
.setAction(FileDisplayActivity
.ACTION_DETAILS
);
316 showDetailsIntent
.putExtra(FileActivity
.EXTRA_FILE
, file
);
317 showDetailsIntent
.putExtra(FileActivity
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
318 startActivity(showDetailsIntent
);
319 int pos
= mPreviewImagePagerAdapter
.getFilePosition(file
);
320 file
= mPreviewImagePagerAdapter
.getFileAt(pos
);
325 private void requestForDownload(OCFile file
) {
326 if (mDownloaderBinder
== null
) {
327 Log_OC
.d(TAG
, "requestForDownload called without binder to download service");
329 } else if (!mDownloaderBinder
.isDownloading(getAccount(), file
)) {
330 Intent i
= new Intent(this, FileDownloader
.class);
331 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, getAccount());
332 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
338 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
340 * @param Position Position index of the new selected page
343 public void onPageSelected(int position
) {
344 if (mDownloaderBinder
== null
) {
345 mRequestWaitingForBinder
= true
;
348 OCFile currentFile
= mPreviewImagePagerAdapter
.getFileAt(position
);
349 getSupportActionBar().setTitle(currentFile
.getFileName());
350 if (!currentFile
.isDown()) {
351 if (!mPreviewImagePagerAdapter
.pendingErrorAt(position
)) {
352 requestForDownload(currentFile
);
359 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
360 * when the pager is automatically settling to the current page. when it is fully stopped/idle.
362 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
365 public void onPageScrollStateChanged(int state
) {
369 * This method will be invoked when the current page is scrolled, either as part of a programmatically
370 * initiated smooth scroll or a user initiated touch scroll.
372 * @param position Position index of the first page currently being displayed.
373 * Page position+1 will be visible if positionOffset is nonzero.
375 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
376 * @param positionOffsetPixels Value in pixels indicating the offset from position.
379 public void onPageScrolled(int position
, float positionOffset
, int positionOffsetPixels
) {
384 * Class waiting for broadcast events from the {@link FielDownloader} service.
386 * Updates the UI when a download is started or finished, provided that it is relevant for the
387 * folder displayed in the gallery.
389 private class DownloadFinishReceiver
extends BroadcastReceiver
{
391 public void onReceive(Context context
, Intent intent
) {
392 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
393 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
394 if (getAccount().name
.equals(accountName
) &&
395 downloadedRemotePath
!= null
) {
397 OCFile file
= mStorageManager
.getFileByPath(downloadedRemotePath
);
398 int position
= mPreviewImagePagerAdapter
.getFilePosition(file
);
399 boolean downloadWasFine
= intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
);
400 //boolean isOffscreen = Math.abs((mViewPager.getCurrentItem() - position)) <= mViewPager.getOffscreenPageLimit();
402 if (position
>= 0 && intent
.getAction().equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
)) {
403 if (downloadWasFine
) {
404 mPreviewImagePagerAdapter
.updateFile(position
, file
);
407 mPreviewImagePagerAdapter
.updateWithDownloadError(position
);
409 mPreviewImagePagerAdapter
.notifyDataSetChanged(); // will trigger the creation of new fragments
412 Log_OC
.d(TAG
, "Download finished, but the fragment is offscreen");
416 removeStickyBroadcast(intent
);
423 public boolean onTouch(View v
, MotionEvent event
) {
424 if (event
.getAction() == MotionEvent
.ACTION_UP
) {
431 private void toggleFullScreen() {
432 ActionBar actionBar
= getSupportActionBar();
440 mFullScreen
= !mFullScreen
;
444 protected void onAccountSet(boolean stateWasRecovered
) {
445 if (getAccount() != null
) {
446 OCFile file
= getFile();
447 /// Validate handled file (first image to preview)
449 throw new IllegalStateException("Instanced with a NULL OCFile");
451 if (!file
.isImage()) {
452 throw new IllegalArgumentException("Non-image file passed as argument");
454 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
455 file
= mStorageManager
.getFileById(file
.getFileId());
457 /// Refresh the activity according to the Account and OCFile set
458 setFile(file
); // reset after getting it fresh from mStorageManager
459 getSupportActionBar().setTitle(getFile().getFileName());
460 //if (!stateWasRecovered) {
465 // handled file not in the current Account
470 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");