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
);
115 if (savedInstanceState
== null
) {
116 mWaitingToPreview
= false
;
118 mWaitingToPreview
= savedInstanceState
.getBoolean(KEY_WAITING_TO_PREVIEW
);
121 mDownloadConnection
= new DetailsServiceConnection();
122 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
123 mUploadConnection
= new DetailsServiceConnection();
124 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
128 private void createViewPager() {
129 mPreviewImagePagerAdapter
= new PreviewImagePagerAdapter(getSupportFragmentManager(), mParentFolder
);
130 mViewPager
= (ViewPager
) findViewById(R
.id
.fragmentPager
);
131 mViewPager
.setAdapter(mPreviewImagePagerAdapter
);
132 mViewPager
.setOnPageChangeListener(this);
137 * Adapter class that provides Fragment instances
139 * @author David A. Velasco
141 public class PreviewImagePagerAdapter
extends FragmentStatePagerAdapter
{
143 Vector
<OCFile
> mImageFiles
;
145 public PreviewImagePagerAdapter(FragmentManager fm
, OCFile parentFolder
) {
147 mImageFiles
= mStorageManager
.getDirectoryImages(parentFolder
);
151 public Fragment
getItem(int i
) {
152 Log
.e(TAG
, "GETTING PAGE " + i
);
153 OCFile file
= mImageFiles
.get(i
);
154 Fragment fragment
= null
;
156 fragment
= new PreviewImageFragment(file
, mAccount
);
157 mWaitingToPreview
= false
;
159 fragment
= new FileDownloadFragment(file
, mAccount
); // TODO
160 //mWaitingToPreview = true;
166 public int getCount() {
167 return mImageFiles
.size();
171 public CharSequence
getPageTitle(int position
) {
172 return mImageFiles
.get(position
).getFileName();
178 protected void onSaveInstanceState(Bundle outState
) {
179 super.onSaveInstanceState(outState
);
180 outState
.putBoolean(KEY_WAITING_TO_PREVIEW
, mWaitingToPreview
);
184 /** Defines callbacks for service binding, passed to bindService() */
185 private class DetailsServiceConnection
implements ServiceConnection
{
188 public void onServiceConnected(ComponentName component
, IBinder service
) {
190 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
191 Log
.d(TAG
, "Download service connected");
192 mDownloaderBinder
= (FileDownloaderBinder
) service
;
193 if (mWaitingToPreview
) {
194 requestForDownload();
197 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
198 Log
.d(TAG
, "Upload service connected");
199 mUploaderBinder
= (FileUploaderBinder
) service
;
204 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
205 FileDetailFragment detailsFragment
= (fragment
instanceof FileDetailFragment
) ?
(FileDetailFragment
) fragment
: null
;
206 if (detailsFragment
!= null
) {
207 detailsFragment
.listenForTransferProgress();
208 detailsFragment
.updateFileDetails(mWaitingToPreview
); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
213 public void onServiceDisconnected(ComponentName component
) {
214 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
215 Log
.d(TAG
, "Download service disconnected");
216 mDownloaderBinder
= null
;
217 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
218 Log
.d(TAG
, "Upload service disconnected");
219 mUploaderBinder
= null
;
226 public void onDestroy() {
228 if (mDownloadConnection
!= null
) {
229 unbindService(mDownloadConnection
);
230 mDownloadConnection
= null
;
232 if (mUploadConnection
!= null
) {
233 unbindService(mUploadConnection
);
234 mUploadConnection
= null
;
240 public boolean onOptionsItemSelected(MenuItem item
) {
241 boolean returnValue
= false
;
243 switch(item
.getItemId()){
244 case android
.R
.id
.home
:
245 backToDisplayActivity();
249 returnValue
= super.onOptionsItemSelected(item
);
257 protected void onResume() {
259 Fragment fragment
= getSupportFragmentManager().findFragmentByTag(FileDetailFragment
.FTAG
);
260 if (fragment
!= null
&& fragment
instanceof FileDetailFragment
) {
261 ((FileDetailFragment
) fragment
).updateFileDetails(false
);
266 private void backToDisplayActivity() {
268 Intent intent = new Intent(this, FileDisplayActivity.class);
269 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
270 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
271 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
272 startActivity(intent);
279 protected Dialog
onCreateDialog(int id
) {
280 Dialog dialog
= null
;
282 case DIALOG_SHORT_WAIT
: {
283 ProgressDialog working_dialog
= new ProgressDialog(this);
284 working_dialog
.setMessage(getResources().getString(
285 R
.string
.wait_a_moment
));
286 working_dialog
.setIndeterminate(true
);
287 working_dialog
.setCancelable(false
);
288 dialog
= working_dialog
;
302 public void onFileStateChanged() {
303 // nothing to do here!
311 public FileDownloaderBinder
getFileDownloaderBinder() {
312 return mDownloaderBinder
;
317 public FileUploaderBinder
getFileUploaderBinder() {
318 return mUploaderBinder
;
323 public void showFragmentWithDetails(OCFile file
) {
324 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
325 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
326 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
327 showDetailsIntent
.putExtra(FileDetailActivity
.EXTRA_MODE
, FileDetailActivity
.MODE_DETAILS
);
328 startActivity(showDetailsIntent
);
332 private void requestForDownload() {
333 if (!mDownloaderBinder
.isDownloading(mAccount
, mFile
)) {
334 Intent i
= new Intent(this, FileDownloader
.class);
335 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
336 i
.putExtra(FileDownloader
.EXTRA_FILE
, mFile
);
342 public void notifySuccessfulDownload(OCFile file
, Intent intent
, boolean success
) {
344 if (mWaitingToPreview
) {
345 FragmentTransaction transaction
= getSupportFragmentManager().beginTransaction();
346 transaction
.replace(R
.id
.fragment
, new FilePreviewFragment(file
, mAccount
), FileDetailFragment
.FTAG
);
347 transaction
.commit();
348 mWaitingToPreview
= false
;
355 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
357 * @param Position Position index of the new selected page
360 public void onPageSelected(int position
) {
361 OCFile currentFile
= ((FileFragment
)mPreviewImagePagerAdapter
.getItem(position
)).getFile();
362 getSupportActionBar().setTitle(currentFile
.getFileName());
366 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
367 * when the pager is automatically settling to the current page, or when it is fully stopped/idle.
369 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
372 public void onPageScrollStateChanged(int state
) {
376 * This method will be invoked when the current page is scrolled, either as part of a programmatically
377 * initiated smooth scroll or a user initiated touch scroll.
379 * @param position Position index of the first page currently being displayed.
380 * Page position+1 will be visible if positionOffset is nonzero.
382 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
383 * @param positionOffsetPixels Value in pixels indicating the offset from position.
386 public void onPageScrolled(int position
, float positionOffset
, int positionOffsetPixels
) {