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
.content
.BroadcastReceiver
;
20 import android
.content
.ComponentName
;
21 import android
.content
.Context
;
22 import android
.content
.Intent
;
23 import android
.content
.IntentFilter
;
24 import android
.content
.ServiceConnection
;
25 import android
.os
.Bundle
;
26 import android
.os
.IBinder
;
27 import android
.support
.v4
.app
.Fragment
;
28 import android
.support
.v4
.app
.FragmentManager
;
29 import android
.support
.v4
.app
.FragmentTransaction
;
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
.view
.MenuItem
;
37 import com
.actionbarsherlock
.view
.Window
;
38 import com
.owncloud
.android
.Log_OC
;
39 import com
.owncloud
.android
.R
;
40 import com
.owncloud
.android
.authentication
.AccountUtils
;
41 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
42 import com
.owncloud
.android
.datamodel
.OCFile
;
43 import com
.owncloud
.android
.files
.services
.FileDownloader
;
44 import com
.owncloud
.android
.files
.services
.FileUploader
;
45 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
46 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
47 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
48 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
49 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
50 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
54 * Holds a swiping galley where image files contained in an ownCloud directory are shown
56 * @author David A. Velasco
58 public class PreviewImageActivity
extends FileActivity
implements FileFragment
.ContainerActivity
, ViewPager
.OnPageChangeListener
, OnTouchListener
{
60 public static final int DIALOG_SHORT_WAIT
= 0;
62 public static final String TAG
= PreviewImageActivity
.class.getSimpleName();
64 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
65 private static final String KEY_WAITING_FOR_BINDER
= "WAITING_FOR_BINDER";
67 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
69 private FileDataStorageManager mStorageManager
;
71 private ViewPager mViewPager
;
72 private PreviewImagePagerAdapter mPreviewImagePagerAdapter
;
74 private FileDownloaderBinder mDownloaderBinder
= null
;
75 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
76 private FileUploaderBinder mUploaderBinder
= null
;
78 private boolean mRequestWaitingForBinder
;
80 private DownloadFinishReceiver mDownloadFinishReceiver
;
82 private boolean mFullScreen
;
86 protected void onCreate(Bundle savedInstanceState
) {
87 super.onCreate(savedInstanceState
);
89 requestWindowFeature(Window
.FEATURE_ACTION_BAR_OVERLAY
);
90 setContentView(R
.layout
.preview_image_activity
);
92 ActionBar actionBar
= getSupportActionBar();
93 actionBar
.setDisplayHomeAsUpEnabled(true
);
97 if (savedInstanceState
!= null
) {
98 mRequestWaitingForBinder
= savedInstanceState
.getBoolean(KEY_WAITING_FOR_BINDER
);
100 mRequestWaitingForBinder
= false
;
105 private void initViewPager() {
106 // get parent from path
107 String parentPath
= getFile().getRemotePath().substring(0, getFile().getRemotePath().lastIndexOf(getFile().getFileName()));
108 OCFile parentFolder
= mStorageManager
.getFileByPath(parentPath
);
109 //OCFile parentFolder = mStorageManager.getFileById(getFile().getParentId());
110 if (parentFolder
== null
) {
111 // should not be necessary
112 parentFolder
= mStorageManager
.getFileByPath(OCFile
.ROOT_PATH
);
114 mPreviewImagePagerAdapter
= new PreviewImagePagerAdapter(getSupportFragmentManager(), parentFolder
, getAccount(), mStorageManager
);
115 mViewPager
= (ViewPager
) findViewById(R
.id
.fragmentPager
);
116 int position
= mPreviewImagePagerAdapter
.getFilePosition(getFile());
117 position
= (position
>= 0) ? position
: 0;
118 mViewPager
.setAdapter(mPreviewImagePagerAdapter
);
119 mViewPager
.setOnPageChangeListener(this);
120 mViewPager
.setCurrentItem(position
);
121 if (position
== 0 && !getFile().isDown()) {
122 // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0)
123 mRequestWaitingForBinder
= true
;
129 public void onStart() {
131 mDownloadConnection
= new PreviewImageServiceConnection();
132 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
133 mUploadConnection
= new PreviewImageServiceConnection();
134 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
138 protected void onSaveInstanceState(Bundle outState
) {
139 super.onSaveInstanceState(outState
);
140 outState
.putBoolean(KEY_WAITING_FOR_BINDER
, mRequestWaitingForBinder
);
144 /** Defines callbacks for service binding, passed to bindService() */
145 private class PreviewImageServiceConnection
implements ServiceConnection
{
148 public void onServiceConnected(ComponentName component
, IBinder service
) {
150 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
151 mDownloaderBinder
= (FileDownloaderBinder
) service
;
152 if (mRequestWaitingForBinder
) {
153 mRequestWaitingForBinder
= false
;
154 Log_OC
.d(TAG
, "Simulating reselection of current page after connection of download binder");
155 onPageSelected(mViewPager
.getCurrentItem());
158 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
159 Log_OC
.d(TAG
, "Upload service connected");
160 mUploaderBinder
= (FileUploaderBinder
) service
;
168 public void onServiceDisconnected(ComponentName component
) {
169 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
170 Log_OC
.d(TAG
, "Download service suddenly disconnected");
171 mDownloaderBinder
= null
;
172 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
173 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
174 mUploaderBinder
= null
;
181 public void onStop() {
183 if (mDownloadConnection
!= null
) {
184 unbindService(mDownloadConnection
);
185 mDownloadConnection
= null
;
187 if (mUploadConnection
!= null
) {
188 unbindService(mUploadConnection
);
189 mUploadConnection
= null
;
195 public void onDestroy() {
201 public boolean onOptionsItemSelected(MenuItem item
) {
202 boolean returnValue
= false
;
204 switch(item
.getItemId()){
205 case android
.R
.id
.home
:
206 backToDisplayActivity();
210 returnValue
= super.onOptionsItemSelected(item
);
218 protected void onResume() {
220 //Log.e(TAG, "ACTIVITY, ONRESUME");
221 mDownloadFinishReceiver
= new DownloadFinishReceiver();
223 IntentFilter filter
= new IntentFilter(FileDownloader
.getDownloadFinishMessage());
224 filter
.addAction(FileDownloader
.getDownloadAddedMessage());
225 registerReceiver(mDownloadFinishReceiver
, filter
);
229 protected void onPostResume() {
230 //Log.e(TAG, "ACTIVITY, ONPOSTRESUME");
231 super.onPostResume();
235 public void onPause() {
237 unregisterReceiver(mDownloadFinishReceiver
);
238 mDownloadFinishReceiver
= null
;
242 private void backToDisplayActivity() {
247 * Show loading dialog
249 public void showLoadingDialog() {
251 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
252 FragmentManager fm
= getSupportFragmentManager();
253 FragmentTransaction ft
= fm
.beginTransaction();
254 loading
.show(ft
, DIALOG_WAIT_TAG
);
259 * Dismiss loading dialog
261 public void dismissLoadingDialog(){
262 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
264 LoadingDialog loading
= (LoadingDialog
) frag
;
273 public void onFileStateChanged() {
274 // nothing to do here!
282 public FileDownloaderBinder
getFileDownloaderBinder() {
283 return mDownloaderBinder
;
288 public FileUploaderBinder
getFileUploaderBinder() {
289 return mUploaderBinder
;
294 public void showDetails(OCFile file
) {
295 Intent showDetailsIntent
= new Intent(this, FileDisplayActivity
.class);
296 showDetailsIntent
.setAction(FileDisplayActivity
.ACTION_DETAILS
);
297 showDetailsIntent
.putExtra(FileActivity
.EXTRA_FILE
, file
);
298 showDetailsIntent
.putExtra(FileActivity
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
299 startActivity(showDetailsIntent
);
300 int pos
= mPreviewImagePagerAdapter
.getFilePosition(file
);
301 file
= mPreviewImagePagerAdapter
.getFileAt(pos
);
306 private void requestForDownload(OCFile file
) {
307 if (mDownloaderBinder
== null
) {
308 Log_OC
.d(TAG
, "requestForDownload called without binder to download service");
310 } else if (!mDownloaderBinder
.isDownloading(getAccount(), file
)) {
311 Intent i
= new Intent(this, FileDownloader
.class);
312 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, getAccount());
313 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
319 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
321 * @param Position Position index of the new selected page
324 public void onPageSelected(int position
) {
325 if (mDownloaderBinder
== null
) {
326 mRequestWaitingForBinder
= true
;
329 OCFile currentFile
= mPreviewImagePagerAdapter
.getFileAt(position
);
330 getSupportActionBar().setTitle(currentFile
.getFileName());
331 if (!currentFile
.isDown()) {
332 if (!mPreviewImagePagerAdapter
.pendingErrorAt(position
)) {
333 requestForDownload(currentFile
);
340 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
341 * when the pager is automatically settling to the current page. when it is fully stopped/idle.
343 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
346 public void onPageScrollStateChanged(int state
) {
350 * This method will be invoked when the current page is scrolled, either as part of a programmatically
351 * initiated smooth scroll or a user initiated touch scroll.
353 * @param position Position index of the first page currently being displayed.
354 * Page position+1 will be visible if positionOffset is nonzero.
356 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
357 * @param positionOffsetPixels Value in pixels indicating the offset from position.
360 public void onPageScrolled(int position
, float positionOffset
, int positionOffsetPixels
) {
365 * Class waiting for broadcast events from the {@link FielDownloader} service.
367 * Updates the UI when a download is started or finished, provided that it is relevant for the
368 * folder displayed in the gallery.
370 private class DownloadFinishReceiver
extends BroadcastReceiver
{
372 public void onReceive(Context context
, Intent intent
) {
373 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
374 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
375 if (getAccount().name
.equals(accountName
) &&
376 downloadedRemotePath
!= null
) {
378 OCFile file
= mStorageManager
.getFileByPath(downloadedRemotePath
);
379 int position
= mPreviewImagePagerAdapter
.getFilePosition(file
);
380 boolean downloadWasFine
= intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
);
381 //boolean isOffscreen = Math.abs((mViewPager.getCurrentItem() - position)) <= mViewPager.getOffscreenPageLimit();
383 if (position
>= 0 && intent
.getAction().equals(FileDownloader
.getDownloadFinishMessage())) {
384 if (downloadWasFine
) {
385 mPreviewImagePagerAdapter
.updateFile(position
, file
);
388 mPreviewImagePagerAdapter
.updateWithDownloadError(position
);
390 mPreviewImagePagerAdapter
.notifyDataSetChanged(); // will trigger the creation of new fragments
393 Log_OC
.d(TAG
, "Download finished, but the fragment is offscreen");
397 removeStickyBroadcast(intent
);
404 public boolean onTouch(View v
, MotionEvent event
) {
405 if (event
.getAction() == MotionEvent
.ACTION_UP
) {
412 private void toggleFullScreen() {
413 ActionBar actionBar
= getSupportActionBar();
421 mFullScreen
= !mFullScreen
;
425 protected void onAccountSet(boolean stateWasRecovered
) {
426 if (getAccount() != null
) {
427 OCFile file
= getFile();
428 /// Validate handled file (first image to preview)
430 throw new IllegalStateException("Instanced with a NULL OCFile");
432 if (!file
.isImage()) {
433 throw new IllegalArgumentException("Non-image file passed as argument");
435 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
437 // Update file according to DB file, if it is possible
438 if (file
.getFileId() > FileDataStorageManager
.ROOT_PARENT_ID
)
439 file
= mStorageManager
.getFileById(file
.getFileId());
442 /// Refresh the activity according to the Account and OCFile set
443 setFile(file
); // reset after getting it fresh from mStorageManager
444 getSupportActionBar().setTitle(getFile().getFileName());
445 //if (!stateWasRecovered) {
450 // handled file not in the current Account
455 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");