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 as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
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.
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/>.
18 package com
.owncloud
.android
.ui
.preview
;
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
;
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
;
52 import com
.owncloud
.android
.AccountUtils
;
53 import com
.owncloud
.android
.R
;
56 * Used as an utility to preview image files contained in an ownCloud account.
58 * @author David A. Velasco
60 public class PreviewImageActivity
extends SherlockFragmentActivity
implements FileFragment
.ContainerActivity
, ViewPager
.OnPageChangeListener
, OnTouchListener
{
62 public static final int DIALOG_SHORT_WAIT
= 0;
64 public static final String TAG
= PreviewImageActivity
.class.getSimpleName();
66 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
67 private static final String KEY_WAITING_FOR_BINDER
= "WAITING_FOR_BINDER";
70 private OCFile mParentFolder
;
71 private Account mAccount
;
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 mFile
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
93 mAccount
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
95 throw new IllegalStateException("Instanced with a NULL OCFile");
97 if (mAccount
== null
) {
98 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
100 if (!mFile
.isImage()) {
101 throw new IllegalArgumentException("Non-image file passed as argument");
103 requestWindowFeature(Window
.FEATURE_ACTION_BAR_OVERLAY
);
104 setContentView(R
.layout
.preview_image_activity
);
106 ActionBar actionBar
= getSupportActionBar();
107 actionBar
.setDisplayHomeAsUpEnabled(true
);
108 actionBar
.setTitle(mFile
.getFileName());
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
);
120 if (savedInstanceState
!= null
) {
121 mRequestWaitingForBinder
= savedInstanceState
.getBoolean(KEY_WAITING_FOR_BINDER
);
123 mRequestWaitingForBinder
= false
;
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 Log
.e(TAG
, "Setting initial position " + position
);
138 mViewPager
.setCurrentItem(position
);
139 if (position
== 0 && !mFile
.isDown()) {
140 // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0)
141 mRequestWaitingForBinder
= true
;
147 public void onStart() {
149 Log
.e(TAG
, "PREVIEW ACTIVITY ON START");
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 Log
.e(TAG
, "PREVIEW_IMAGE Download service connected");
171 mDownloaderBinder
= (FileDownloaderBinder
) service
;
172 if (mRequestWaitingForBinder
) {
173 mRequestWaitingForBinder
= false
;
174 Log
.e(TAG
, "Simulating reselection of current page after connection of download binder");
175 onPageSelected(mViewPager
.getCurrentItem());
178 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
179 Log
.d(TAG
, "Upload service connected");
180 mUploaderBinder
= (FileUploaderBinder
) service
;
188 public void onServiceDisconnected(ComponentName component
) {
189 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
190 Log
.d(TAG
, "Download service suddenly disconnected");
191 mDownloaderBinder
= null
;
192 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
193 Log
.d(TAG
, "Upload service suddenly disconnected");
194 mUploaderBinder
= null
;
201 public void onStop() {
203 if (mDownloadConnection
!= null
) {
204 unbindService(mDownloadConnection
);
205 mDownloadConnection
= null
;
207 if (mUploadConnection
!= null
) {
208 unbindService(mUploadConnection
);
209 mUploadConnection
= null
;
215 public void onDestroy() {
221 public boolean onOptionsItemSelected(MenuItem item
) {
222 boolean returnValue
= false
;
224 switch(item
.getItemId()){
225 case android
.R
.id
.home
:
226 backToDisplayActivity();
230 returnValue
= super.onOptionsItemSelected(item
);
238 protected void onResume() {
240 Log
.e(TAG
, "PREVIEW ACTIVITY ONRESUME");
241 mDownloadFinishReceiver
= new DownloadFinishReceiver();
242 IntentFilter filter
= new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
243 registerReceiver(mDownloadFinishReceiver
, filter
);
248 protected void onPostResume() {
249 super.onPostResume();
250 Log
.e(TAG
, "PREVIEW ACTIVITY ONPOSTRESUME");
254 public void onPause() {
256 unregisterReceiver(mDownloadFinishReceiver
);
257 mDownloadFinishReceiver
= null
;
261 private void backToDisplayActivity() {
263 Intent intent = new Intent(this, FileDisplayActivity.class);
264 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
265 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
266 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
267 startActivity(intent);
274 protected Dialog
onCreateDialog(int id
) {
275 Dialog dialog
= null
;
277 case DIALOG_SHORT_WAIT
: {
278 ProgressDialog working_dialog
= new ProgressDialog(this);
279 working_dialog
.setMessage(getResources().getString(
280 R
.string
.wait_a_moment
));
281 working_dialog
.setIndeterminate(true
);
282 working_dialog
.setCancelable(false
);
283 dialog
= working_dialog
;
297 public void onFileStateChanged() {
298 // nothing to do here!
306 public FileDownloaderBinder
getFileDownloaderBinder() {
307 return mDownloaderBinder
;
312 public FileUploaderBinder
getFileUploaderBinder() {
313 return mUploaderBinder
;
318 public void showFragmentWithDetails(OCFile file
) {
319 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
320 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
321 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
322 showDetailsIntent
.putExtra(FileDetailActivity
.EXTRA_MODE
, FileDetailActivity
.MODE_DETAILS
);
323 startActivity(showDetailsIntent
);
327 private void requestForDownload(OCFile file
) {
328 Log
.e(TAG
, "REQUEST FOR DOWNLOAD : " + file
.getFileName());
329 if (mDownloaderBinder
== null
) {
330 Log
.e(TAG
, "requestForDownload called without binder to download service");
332 } else if (!mDownloaderBinder
.isDownloading(mAccount
, file
)) {
333 Intent i
= new Intent(this, FileDownloader
.class);
334 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
335 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
341 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
343 * @param Position Position index of the new selected page
346 public void onPageSelected(int position
) {
347 Log
.e(TAG
, "onPageSelected " + position
);
348 if (mDownloaderBinder
== null
) {
349 mRequestWaitingForBinder
= true
;
352 OCFile currentFile
= mPreviewImagePagerAdapter
.getFileAt(position
);
353 getSupportActionBar().setTitle(currentFile
.getFileName());
354 if (!currentFile
.isDown()) {
355 requestForDownload(currentFile
);
356 //updateCurrentDownloadFragment(true);
362 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
363 * when the pager is automatically settling to the current page, or when it is fully stopped/idle.
365 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
368 public void onPageScrollStateChanged(int state
) {
372 * This method will be invoked when the current page is scrolled, either as part of a programmatically
373 * initiated smooth scroll or a user initiated touch scroll.
375 * @param position Position index of the first page currently being displayed.
376 * Page position+1 will be visible if positionOffset is nonzero.
378 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
379 * @param positionOffsetPixels Value in pixels indicating the offset from position.
382 public void onPageScrolled(int position
, float positionOffset
, int positionOffsetPixels
) {
386 private void updateCurrentDownloadFragment(boolean transferring
) {
387 FileFragment fragment
= mPreviewImagePagerAdapter
.getFragmentAt(mViewPager
.getCurrentItem());
388 if (fragment
instanceof FileDownloadFragment
) {
389 ((FileDownloadFragment
) fragment
).updateView(transferring
);
390 //mViewPager.invalidate();
396 * Class waiting for broadcast events from the {@link FielDownloader} service.
398 * Updates the UI when a download is started or finished, provided that it is relevant for the
399 * folder displayed in the gallery.
401 private class DownloadFinishReceiver
extends BroadcastReceiver
{
403 public void onReceive(Context context
, Intent intent
) {
404 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
405 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
406 if (mAccount
.name
.equals(accountName
) &&
407 downloadedRemotePath
!= null
) {
409 OCFile file
= mStorageManager
.getFileByPath(downloadedRemotePath
);
410 int position
= mPreviewImagePagerAdapter
.getFilePosition(file
);
411 boolean downloadWasFine
= intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
);
412 boolean isCurrent
= (mViewPager
.getCurrentItem() == position
);
416 Log
.e(TAG
, "downloaded file FOUND in adapter");
417 if (downloadWasFine
) {
418 mPreviewImagePagerAdapter
.updateFile(position
, file
);
419 //Log.e(TAG, "BEFORE NOTIFY DATA SET CHANGED");
420 mPreviewImagePagerAdapter
.notifyDataSetChanged();
421 //Log.e(TAG, "AFTER NOTIFY DATA SET CHANGED");
423 } else if (isCurrent
) {
424 updateCurrentDownloadFragment(false
);
428 Log
.e(TAG
, "DOWNLOADED FILE NOT FOUND IN ADAPTER ");
432 removeStickyBroadcast(intent
);
439 public boolean onTouch(View v
, MotionEvent event
) {
440 Log
.e(TAG
, "TOUCH!!! **********************");
441 if (event
.getAction() == MotionEvent
.ACTION_DOWN
) {
442 Log
.e(TAG
, "TOUCH DOWN!!! **********************");
444 } else if (event
.getAction() == MotionEvent
.ACTION_UP
) {
445 Log
.e(TAG
, "TOUCH UP!!! **********************");
448 } else if (event
.getAction() == MotionEvent
.ACTION_MOVE
) {
449 Log
.e(TAG
, "TOUCH MOVE!!! **********************");
455 private void toggleFullScreen() {
456 ActionBar actionBar
= getSupportActionBar();
464 mFullScreen
= !mFullScreen
;