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
.ComponentName
;
24 import android
.content
.Context
;
25 import android
.content
.Intent
;
26 import android
.content
.ServiceConnection
;
27 import android
.os
.Bundle
;
28 import android
.os
.IBinder
;
29 import android
.support
.v4
.view
.ViewPager
;
30 import android
.util
.Log
;
32 import com
.actionbarsherlock
.app
.ActionBar
;
33 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
34 import com
.actionbarsherlock
.view
.MenuItem
;
35 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
36 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
37 import com
.owncloud
.android
.datamodel
.OCFile
;
38 import com
.owncloud
.android
.files
.services
.FileDownloader
;
39 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
40 import com
.owncloud
.android
.files
.services
.FileUploader
;
41 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
42 import com
.owncloud
.android
.ui
.activity
.FileDetailActivity
;
43 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
44 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
46 import com
.owncloud
.android
.AccountUtils
;
47 import com
.owncloud
.android
.R
;
50 * Used as an utility to preview image files contained in an ownCloud account.
52 * @author David A. Velasco
54 public class PreviewImageActivity
extends SherlockFragmentActivity
implements FileFragment
.ContainerActivity
, ViewPager
.OnPageChangeListener
{
56 public static final int DIALOG_SHORT_WAIT
= 0;
58 public static final String TAG
= PreviewImageActivity
.class.getSimpleName();
60 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
63 private OCFile mParentFolder
;
64 private Account mAccount
;
65 private DataStorageManager mStorageManager
;
67 private ViewPager mViewPager
;
68 private PreviewImagePagerAdapter mPreviewImagePagerAdapter
;
70 private FileDownloaderBinder mDownloaderBinder
= null
;
71 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
72 private FileUploaderBinder mUploaderBinder
= null
;
73 private OCFile mWaitingToPreview
= null
;
77 protected void onCreate(Bundle savedInstanceState
) {
78 super.onCreate(savedInstanceState
);
80 mFile
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
81 mAccount
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
83 throw new IllegalStateException("Instanced with a NULL OCFile");
85 if (mAccount
== null
) {
86 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
88 if (!mFile
.isImage()) {
89 throw new IllegalArgumentException("Non-image file passed as argument");
92 setContentView(R
.layout
.preview_image_activity
);
94 ActionBar actionBar
= getSupportActionBar();
95 actionBar
.setDisplayHomeAsUpEnabled(true
);
96 actionBar
.setTitle(mFile
.getFileName());
98 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
99 mParentFolder
= mStorageManager
.getFileById(mFile
.getParentId());
100 if (mParentFolder
== null
) {
101 // should not be necessary
102 mParentFolder
= mStorageManager
.getFileByPath(OCFile
.PATH_SEPARATOR
);
107 if (savedInstanceState
== null
) {
108 mWaitingToPreview
= (mFile
.isDown())?null
:mFile
;
110 mWaitingToPreview
= (OCFile
) savedInstanceState
.getParcelable(KEY_WAITING_TO_PREVIEW
);
113 mDownloadConnection
= new PreviewImageServiceConnection();
114 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
115 mUploadConnection
= new PreviewImageServiceConnection();
116 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
120 private void createViewPager() {
121 mPreviewImagePagerAdapter
= new PreviewImagePagerAdapter(getSupportFragmentManager(), mParentFolder
, mAccount
, mStorageManager
);
122 mViewPager
= (ViewPager
) findViewById(R
.id
.fragmentPager
);
123 mViewPager
.setAdapter(mPreviewImagePagerAdapter
);
124 mViewPager
.setOnPageChangeListener(this);
129 protected void onSaveInstanceState(Bundle outState
) {
130 super.onSaveInstanceState(outState
);
131 outState
.putParcelable(KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
135 /** Defines callbacks for service binding, passed to bindService() */
136 private class PreviewImageServiceConnection
implements ServiceConnection
{
139 public void onServiceConnected(ComponentName component
, IBinder service
) {
141 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
142 Log
.d(TAG
, "Download service connected");
143 mDownloaderBinder
= (FileDownloaderBinder
) service
;
145 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
146 Log
.d(TAG
, "Upload service connected");
147 mUploaderBinder
= (FileUploaderBinder
) service
;
155 public void onServiceDisconnected(ComponentName component
) {
156 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
157 Log
.d(TAG
, "Download service suddenly disconnected");
158 mDownloaderBinder
= null
;
159 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
160 Log
.d(TAG
, "Upload service suddenly disconnected");
161 mUploaderBinder
= null
;
168 public void onDestroy() {
170 if (mDownloadConnection
!= null
) {
171 unbindService(mDownloadConnection
);
172 mDownloadConnection
= null
;
174 if (mUploadConnection
!= null
) {
175 unbindService(mUploadConnection
);
176 mUploadConnection
= null
;
182 public boolean onOptionsItemSelected(MenuItem item
) {
183 boolean returnValue
= false
;
185 switch(item
.getItemId()){
186 case android
.R
.id
.home
:
187 backToDisplayActivity();
191 returnValue
= super.onOptionsItemSelected(item
);
199 protected void onResume() {
204 private void backToDisplayActivity() {
206 Intent intent = new Intent(this, FileDisplayActivity.class);
207 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
208 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
209 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
210 startActivity(intent);
217 protected Dialog
onCreateDialog(int id
) {
218 Dialog dialog
= null
;
220 case DIALOG_SHORT_WAIT
: {
221 ProgressDialog working_dialog
= new ProgressDialog(this);
222 working_dialog
.setMessage(getResources().getString(
223 R
.string
.wait_a_moment
));
224 working_dialog
.setIndeterminate(true
);
225 working_dialog
.setCancelable(false
);
226 dialog
= working_dialog
;
240 public void onFileStateChanged() {
241 // nothing to do here!
249 public FileDownloaderBinder
getFileDownloaderBinder() {
250 return mDownloaderBinder
;
255 public FileUploaderBinder
getFileUploaderBinder() {
256 return mUploaderBinder
;
261 public void showFragmentWithDetails(OCFile file
) {
262 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
263 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
264 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
265 showDetailsIntent
.putExtra(FileDetailActivity
.EXTRA_MODE
, FileDetailActivity
.MODE_DETAILS
);
266 startActivity(showDetailsIntent
);
270 private void requestForDownload() {
271 Log
.e(TAG
, "REQUEST FOR DOWNLOAD : " + mWaitingToPreview
.getFileName());
272 if (!mDownloaderBinder
.isDownloading(mAccount
, mWaitingToPreview
)) {
273 Intent i
= new Intent(this, FileDownloader
.class);
274 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
275 i
.putExtra(FileDownloader
.EXTRA_FILE
, mWaitingToPreview
);
281 public void notifySuccessfulDownload(OCFile file
, Intent intent
, boolean success
) {
283 if (mWaitingToPreview
!= null
&& mWaitingToPreview
.equals(file
)) {
284 mWaitingToPreview
= null
;
285 int position
= mViewPager
.getCurrentItem();
286 mPreviewImagePagerAdapter
.updateFile(position
, file
);
287 Log
.e(TAG
, "BEFORE NOTIFY DATA SET CHANGED");
288 mPreviewImagePagerAdapter
.notifyDataSetChanged();
289 Log
.e(TAG
, "AFTER NOTIFY DATA SET CHANGED");
290 //Log.e(TAG, "BEFORE INVALIDATE");
291 //mViewPager.postInvalidate();
292 //Log.e(TAG, "AFTER INVALIDATE");
299 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
301 * @param Position Position index of the new selected page
304 public void onPageSelected(int position
) {
305 OCFile currentFile
= mPreviewImagePagerAdapter
.getFileAt(position
);
306 getSupportActionBar().setTitle(currentFile
.getFileName());
307 if (currentFile
.isDown()) {
308 mWaitingToPreview
= null
;
310 mWaitingToPreview
= currentFile
;
311 requestForDownload();
312 mViewPager
.invalidate();
317 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
318 * when the pager is automatically settling to the current page, or when it is fully stopped/idle.
320 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
323 public void onPageScrollStateChanged(int state
) {
327 * This method will be invoked when the current page is scrolled, either as part of a programmatically
328 * initiated smooth scroll or a user initiated touch scroll.
330 * @param position Position index of the first page currently being displayed.
331 * Page position+1 will be visible if positionOffset is nonzero.
333 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
334 * @param positionOffsetPixels Value in pixels indicating the offset from position.
337 public void onPageScrolled(int position
, float positionOffset
, int positionOffsetPixels
) {