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 mViewPager
.setCurrentItem(position
);
138 if (position
== 0 && !mFile
.isDown()) {
139 // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0)
140 mRequestWaitingForBinder
= true
;
146 public void onStart() {
148 mDownloadConnection
= new PreviewImageServiceConnection();
149 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
150 mUploadConnection
= new PreviewImageServiceConnection();
151 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
155 protected void onSaveInstanceState(Bundle outState
) {
156 super.onSaveInstanceState(outState
);
157 outState
.putBoolean(KEY_WAITING_FOR_BINDER
, mRequestWaitingForBinder
);
161 /** Defines callbacks for service binding, passed to bindService() */
162 private class PreviewImageServiceConnection
implements ServiceConnection
{
165 public void onServiceConnected(ComponentName component
, IBinder service
) {
167 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
168 mDownloaderBinder
= (FileDownloaderBinder
) service
;
169 if (mRequestWaitingForBinder
) {
170 mRequestWaitingForBinder
= false
;
171 Log
.d(TAG
, "Simulating reselection of current page after connection of download binder");
172 onPageSelected(mViewPager
.getCurrentItem());
175 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
176 Log
.d(TAG
, "Upload service connected");
177 mUploaderBinder
= (FileUploaderBinder
) service
;
185 public void onServiceDisconnected(ComponentName component
) {
186 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
187 Log
.d(TAG
, "Download service suddenly disconnected");
188 mDownloaderBinder
= null
;
189 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
190 Log
.d(TAG
, "Upload service suddenly disconnected");
191 mUploaderBinder
= null
;
198 public void onStop() {
200 if (mDownloadConnection
!= null
) {
201 unbindService(mDownloadConnection
);
202 mDownloadConnection
= null
;
204 if (mUploadConnection
!= null
) {
205 unbindService(mUploadConnection
);
206 mUploadConnection
= null
;
212 public void onDestroy() {
218 public boolean onOptionsItemSelected(MenuItem item
) {
219 boolean returnValue
= false
;
221 switch(item
.getItemId()){
222 case android
.R
.id
.home
:
223 backToDisplayActivity();
227 returnValue
= super.onOptionsItemSelected(item
);
235 protected void onResume() {
237 mDownloadFinishReceiver
= new DownloadFinishReceiver();
238 IntentFilter filter
= new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
239 registerReceiver(mDownloadFinishReceiver
, filter
);
244 protected void onPostResume() {
245 super.onPostResume();
249 public void onPause() {
251 unregisterReceiver(mDownloadFinishReceiver
);
252 mDownloadFinishReceiver
= null
;
256 private void backToDisplayActivity() {
258 Intent intent = new Intent(this, FileDisplayActivity.class);
259 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
260 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
261 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
262 startActivity(intent);
269 protected Dialog
onCreateDialog(int id
) {
270 Dialog dialog
= null
;
272 case DIALOG_SHORT_WAIT
: {
273 ProgressDialog working_dialog
= new ProgressDialog(this);
274 working_dialog
.setMessage(getResources().getString(
275 R
.string
.wait_a_moment
));
276 working_dialog
.setIndeterminate(true
);
277 working_dialog
.setCancelable(false
);
278 dialog
= working_dialog
;
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 showFragmentWithDetails(OCFile file
) {
314 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
315 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
316 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
317 showDetailsIntent
.putExtra(FileDetailActivity
.EXTRA_MODE
, FileDetailActivity
.MODE_DETAILS
);
318 startActivity(showDetailsIntent
);
322 private void requestForDownload(OCFile file
) {
323 if (mDownloaderBinder
== null
) {
324 Log
.d(TAG
, "requestForDownload called without binder to download service");
326 } else if (!mDownloaderBinder
.isDownloading(mAccount
, file
)) {
327 Intent i
= new Intent(this, FileDownloader
.class);
328 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
329 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
335 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
337 * @param Position Position index of the new selected page
340 public void onPageSelected(int position
) {
341 if (mDownloaderBinder
== null
) {
342 mRequestWaitingForBinder
= true
;
345 OCFile currentFile
= mPreviewImagePagerAdapter
.getFileAt(position
);
346 getSupportActionBar().setTitle(currentFile
.getFileName());
347 if (!currentFile
.isDown()) {
348 requestForDownload(currentFile
);
354 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
355 * when the pager is automatically settling to the current page, or when it is fully stopped/idle.
357 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
360 public void onPageScrollStateChanged(int state
) {
364 * This method will be invoked when the current page is scrolled, either as part of a programmatically
365 * initiated smooth scroll or a user initiated touch scroll.
367 * @param position Position index of the first page currently being displayed.
368 * Page position+1 will be visible if positionOffset is nonzero.
370 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
371 * @param positionOffsetPixels Value in pixels indicating the offset from position.
374 public void onPageScrolled(int position
, float positionOffset
, int positionOffsetPixels
) {
378 private void updateCurrentDownloadFragment(boolean transferring
) {
379 FileFragment fragment
= mPreviewImagePagerAdapter
.getFragmentAt(mViewPager
.getCurrentItem());
380 if (fragment
instanceof FileDownloadFragment
) {
381 ((FileDownloadFragment
) fragment
).updateView(transferring
);
382 //mViewPager.invalidate();
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 isCurrent
= (mViewPager
.getCurrentItem() == position
);
408 if (downloadWasFine
) {
409 mPreviewImagePagerAdapter
.updateFile(position
, file
);
410 mPreviewImagePagerAdapter
.notifyDataSetChanged();
412 } else if (isCurrent
) {
413 updateCurrentDownloadFragment(false
);
417 Log
.e(TAG
, "DOWNLOADED FILE NOT FOUND IN ADAPTER ");
421 removeStickyBroadcast(intent
);
428 public boolean onTouch(View v
, MotionEvent event
) {
429 if (event
.getAction() == MotionEvent
.ACTION_UP
) {
436 private void toggleFullScreen() {
437 ActionBar actionBar
= getSupportActionBar();
445 mFullScreen
= !mFullScreen
;