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 3 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
.activity
;
20 import java
.util
.Vector
;
22 import android
.accounts
.Account
;
23 import android
.app
.Dialog
;
24 import android
.app
.ProgressDialog
;
25 import android
.content
.ComponentName
;
26 import android
.content
.Context
;
27 import android
.content
.Intent
;
28 import android
.content
.ServiceConnection
;
29 import android
.os
.Bundle
;
30 import android
.os
.IBinder
;
31 import android
.support
.v4
.app
.Fragment
;
32 import android
.support
.v4
.app
.FragmentManager
;
33 import android
.support
.v4
.app
.FragmentStatePagerAdapter
;
34 import android
.support
.v4
.app
.FragmentTransaction
;
35 import android
.support
.v4
.view
.ViewPager
;
36 import android
.util
.Log
;
38 import com
.actionbarsherlock
.app
.ActionBar
;
39 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
40 import com
.actionbarsherlock
.view
.MenuItem
;
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
.fragment
.FileDetailFragment
;
49 import com
.owncloud
.android
.ui
.fragment
.FileDownloadFragment
;
50 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
51 import com
.owncloud
.android
.ui
.fragment
.FilePreviewFragment
;
52 import com
.owncloud
.android
.ui
.fragment
.PreviewImageFragment
;
54 import com
.owncloud
.android
.AccountUtils
;
55 import com
.owncloud
.android
.R
;
58 * Used as an utility to preview image files contained in an ownCloud account.
60 * @author David A. Velasco
62 public class PreviewImageActivity
extends SherlockFragmentActivity
implements FileFragment
.ContainerActivity
, ViewPager
.OnPageChangeListener
{
64 public static final int DIALOG_SHORT_WAIT
= 0;
66 public static final String TAG
= PreviewImageActivity
.class.getSimpleName();
68 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
71 private OCFile mParentFolder
;
72 private Account mAccount
;
73 private DataStorageManager mStorageManager
;
75 private ViewPager mViewPager
;
76 private PreviewImagePagerAdapter mPreviewImagePagerAdapter
;
78 private FileDownloaderBinder mDownloaderBinder
= null
;
79 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
80 private FileUploaderBinder mUploaderBinder
= null
;
81 private boolean mWaitingToPreview
;
85 protected void onCreate(Bundle savedInstanceState
) {
86 super.onCreate(savedInstanceState
);
88 mFile
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
89 mAccount
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
91 throw new IllegalStateException("Instanced with a NULL OCFile");
93 if (mAccount
== null
) {
94 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
96 if (!mFile
.isImage()) {
97 throw new IllegalArgumentException("Non-image file passed as argument");
100 setContentView(R
.layout
.preview_image_activity
);
102 ActionBar actionBar
= getSupportActionBar();
103 actionBar
.setDisplayHomeAsUpEnabled(true
);
104 actionBar
.setTitle(mFile
.getFileName());
106 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
107 mParentFolder
= mStorageManager
.getFileById(mFile
.getParentId());
108 if (mParentFolder
== null
) {
109 // should not be necessary
110 mParentFolder
= mStorageManager
.getFileByPath(OCFile
.PATH_SEPARATOR
);
113 if (savedInstanceState
== null
) {
114 mWaitingToPreview
= false
;
117 mWaitingToPreview
= savedInstanceState
.getBoolean(KEY_WAITING_TO_PREVIEW
);
120 mDownloadConnection
= new DetailsServiceConnection();
121 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
122 mUploadConnection
= new DetailsServiceConnection();
123 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
127 private void createViewPager() {
128 mPreviewImagePagerAdapter
= new PreviewImagePagerAdapter(getSupportFragmentManager(), mParentFolder
);
129 mViewPager
= (ViewPager
) findViewById(R
.id
.fragmentPager
);
130 mViewPager
.setAdapter(mPreviewImagePagerAdapter
);
131 mViewPager
.setOnPageChangeListener(this);
136 * Adapter class that provides Fragment instances
138 * @author David A. Velasco
140 public class PreviewImagePagerAdapter
extends FragmentStatePagerAdapter
{
142 Vector
<OCFile
> mImageFiles
;
144 public PreviewImagePagerAdapter(FragmentManager fm
, OCFile parentFolder
) {
146 mImageFiles
= mStorageManager
.getDirectoryImages(parentFolder
);
150 public Fragment
getItem(int i
) {
151 Log
.e(TAG
, "GETTING PAGE " + i
);
152 OCFile file
= mImageFiles
.get(i
);
153 Fragment fragment
= null
;
155 fragment
= new PreviewImageFragment(file
, mAccount
);
156 mWaitingToPreview
= false
;
158 fragment
= new FileDownloadFragment(file
, mAccount
); // TODO
159 //mWaitingToPreview = true;
165 public int getCount() {
166 return mImageFiles
.size();
170 public CharSequence
getPageTitle(int position
) {
171 return mImageFiles
.get(position
).getFileName();
177 protected void onSaveInstanceState(Bundle outState
) {
178 super.onSaveInstanceState(outState
);
179 outState
.putBoolean(KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
183 /** Defines callbacks for service binding, passed to bindService() */
184 private class DetailsServiceConnection
implements ServiceConnection
{
187 public void onServiceConnected(ComponentName component
, IBinder service
) {
189 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
190 Log
.d(TAG
, "Download service connected");
191 mDownloaderBinder
= (FileDownloaderBinder
) service
;
192 if (mWaitingToPreview
) {
193 requestForDownload();
196 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
197 Log
.d(TAG
, "Upload service connected");
198 mUploaderBinder
= (FileUploaderBinder
) service
;
203 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
204 FileDetailFragment detailsFragment
= (fragment
instanceof FileDetailFragment
) ?
(FileDetailFragment
) fragment
: null
;
205 if (detailsFragment
!= null
) {
206 detailsFragment
.listenForTransferProgress();
207 detailsFragment
.updateFileDetails(mWaitingToPreview
); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
212 public void onServiceDisconnected(ComponentName component
) {
213 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
214 Log
.d(TAG
, "Download service disconnected");
215 mDownloaderBinder
= null
;
216 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
217 Log
.d(TAG
, "Upload service disconnected");
218 mUploaderBinder
= null
;
225 public void onDestroy() {
227 if (mDownloadConnection
!= null
) {
228 unbindService(mDownloadConnection
);
229 mDownloadConnection
= null
;
231 if (mUploadConnection
!= null
) {
232 unbindService(mUploadConnection
);
233 mUploadConnection
= null
;
239 public boolean onOptionsItemSelected(MenuItem item
) {
240 boolean returnValue
= false
;
242 switch(item
.getItemId()){
243 case android
.R
.id
.home
:
244 backToDisplayActivity();
248 returnValue
= super.onOptionsItemSelected(item
);
256 protected void onResume() {
258 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
259 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
260 ((FileDetailFragment
) fragment
).updateFileDetails(false
);
265 private void backToDisplayActivity() {
267 Intent intent = new Intent(this, FileDisplayActivity.class);
268 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
269 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
270 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
271 startActivity(intent);
278 protected Dialog
onCreateDialog(int id
) {
279 Dialog dialog
= null
;
281 case DIALOG_SHORT_WAIT
: {
282 ProgressDialog working_dialog
= new ProgressDialog(this);
283 working_dialog
.setMessage(getResources().getString(
284 R
.string
.wait_a_moment
));
285 working_dialog
.setIndeterminate(true
);
286 working_dialog
.setCancelable(false
);
287 dialog
= working_dialog
;
301 public void onFileStateChanged() {
302 // nothing to do here!
310 public FileDownloaderBinder
getFileDownloaderBinder() {
311 return mDownloaderBinder
;
316 public FileUploaderBinder
getFileUploaderBinder() {
317 return mUploaderBinder
;
322 public void showFragmentWithDetails(OCFile file
) {
323 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
324 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
325 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
326 showDetailsIntent
.putExtra(FileDetailActivity
.EXTRA_MODE
, FileDetailActivity
.MODE_DETAILS
);
327 startActivity(showDetailsIntent
);
331 private void requestForDownload() {
332 if (!mDownloaderBinder
.isDownloading(mAccount
, mFile
)) {
333 Intent i
= new Intent(this, FileDownloader
.class);
334 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
335 i
.putExtra(FileDownloader
.EXTRA_FILE
, mFile
);
341 public void notifySuccessfulDownload(OCFile file
, Intent intent
, boolean success
) {
343 if (mWaitingToPreview
) {
344 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
345 transaction
.replace(R
.id
.fragment
, new FilePreviewFragment(file
, mAccount
), FileDetailFragment
.FTAG
);
346 transaction
.commit();
347 mWaitingToPreview
= false
;
354 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
356 * @param Position Position index of the new selected page
359 public void onPageSelected(int position
) {
360 OCFile currentFile
= ((FileFragment
)mPreviewImagePagerAdapter
.getItem(position
)).getFile();
361 getSupportActionBar().setTitle(currentFile
.getFileName());
365 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
366 * when the pager is automatically settling to the current page, or when it is fully stopped/idle.
368 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
371 public void onPageScrollStateChanged(int state
) {
375 * This method will be invoked when the current page is scrolled, either as part of a programmatically
376 * initiated smooth scroll or a user initiated touch scroll.
378 * @param position Position index of the first page currently being displayed.
379 * Page position+1 will be visible if positionOffset is nonzero.
381 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
382 * @param positionOffsetPixels Value in pixels indicating the offset from position.
385 public void onPageScrolled(int position
, float positionOffset
, int positionOffsetPixels
) {