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
;
34 import com
.actionbarsherlock
.app
.ActionBar
;
35 import com
.actionbarsherlock
.app
.SherlockFragmentActivity
;
36 import com
.actionbarsherlock
.view
.MenuItem
;
37 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
38 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
39 import com
.owncloud
.android
.datamodel
.OCFile
;
40 import com
.owncloud
.android
.files
.services
.FileDownloader
;
41 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
42 import com
.owncloud
.android
.files
.services
.FileUploader
;
43 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
44 import com
.owncloud
.android
.ui
.activity
.FileDetailActivity
;
45 import com
.owncloud
.android
.ui
.fragment
.FileDetailFragment
;
46 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
48 import com
.owncloud
.android
.AccountUtils
;
49 import com
.owncloud
.android
.R
;
52 * Used as an utility to preview image files contained in an ownCloud account.
54 * @author David A. Velasco
56 public class PreviewImageActivity
extends SherlockFragmentActivity
implements FileFragment
.ContainerActivity
, ViewPager
.OnPageChangeListener
{
58 public static final int DIALOG_SHORT_WAIT
= 0;
60 public static final String TAG
= PreviewImageActivity
.class.getSimpleName();
62 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
63 private static final String KEY_WAITING_FOR_BINDER
= "WAITING_FOR_BINDER";
66 private OCFile mParentFolder
;
67 private Account mAccount
;
68 private DataStorageManager mStorageManager
;
70 private ViewPager mViewPager
;
71 private PreviewImagePagerAdapter mPreviewImagePagerAdapter
;
73 private FileDownloaderBinder mDownloaderBinder
= null
;
74 private ServiceConnection mDownloadConnection
, mUploadConnection
= null
;
75 private FileUploaderBinder mUploaderBinder
= null
;
77 private boolean mRequestWaitingForBinder
;
79 private DownloadFinishReceiver mDownloadFinishReceiver
;
83 protected void onCreate(Bundle savedInstanceState
) {
84 super.onCreate(savedInstanceState
);
86 mFile
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_FILE
);
87 mAccount
= getIntent().getParcelableExtra(FileDetailFragment
.EXTRA_ACCOUNT
);
89 throw new IllegalStateException("Instanced with a NULL OCFile");
91 if (mAccount
== null
) {
92 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
94 if (!mFile
.isImage()) {
95 throw new IllegalArgumentException("Non-image file passed as argument");
98 setContentView(R
.layout
.preview_image_activity
);
100 ActionBar actionBar
= getSupportActionBar();
101 actionBar
.setDisplayHomeAsUpEnabled(true
);
102 actionBar
.setTitle(mFile
.getFileName());
104 mStorageManager
= new FileDataStorageManager(mAccount
, getContentResolver());
105 mParentFolder
= mStorageManager
.getFileById(mFile
.getParentId());
106 if (mParentFolder
== null
) {
107 // should not be necessary
108 mParentFolder
= mStorageManager
.getFileByPath(OCFile
.PATH_SEPARATOR
);
111 if (savedInstanceState
!= null
) {
112 mRequestWaitingForBinder
= savedInstanceState
.getBoolean(KEY_WAITING_FOR_BINDER
);
114 mRequestWaitingForBinder
= false
;
119 mDownloadConnection
= new PreviewImageServiceConnection();
120 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
121 mUploadConnection
= new PreviewImageServiceConnection();
122 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
126 private void createViewPager() {
127 mPreviewImagePagerAdapter
= new PreviewImagePagerAdapter(getSupportFragmentManager(), mParentFolder
, mAccount
, mStorageManager
);
128 mViewPager
= (ViewPager
) findViewById(R
.id
.fragmentPager
);
129 int position
= mPreviewImagePagerAdapter
.getFilePosition(mFile
);
130 position
= (position
>= 0) ? position
: 0;
131 mViewPager
.setAdapter(mPreviewImagePagerAdapter
);
132 mViewPager
.setOnPageChangeListener(this);
133 Log
.e(TAG
, "Setting initial position " + position
);
134 mViewPager
.setCurrentItem(position
);
135 if (position
== 0 && !mFile
.isDown()) {
136 // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0)
137 mRequestWaitingForBinder
= true
;
143 public void onStart() {
145 Log
.e(TAG
, "PREVIEW ACTIVITY ON START");
149 protected void onSaveInstanceState(Bundle outState
) {
150 super.onSaveInstanceState(outState
);
151 outState
.putBoolean(KEY_WAITING_FOR_BINDER
, mRequestWaitingForBinder
);
155 /** Defines callbacks for service binding, passed to bindService() */
156 private class PreviewImageServiceConnection
implements ServiceConnection
{
159 public void onServiceConnected(ComponentName component
, IBinder service
) {
161 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
162 Log
.e(TAG
, "PREVIEW_IMAGE Download service connected");
163 mDownloaderBinder
= (FileDownloaderBinder
) service
;
164 if (mRequestWaitingForBinder
) {
165 mRequestWaitingForBinder
= false
;
166 Log
.e(TAG
, "Simulating reselection of current page after connection of download binder");
167 onPageSelected(mViewPager
.getCurrentItem());
170 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
171 Log
.d(TAG
, "Upload service connected");
172 mUploaderBinder
= (FileUploaderBinder
) service
;
180 public void onServiceDisconnected(ComponentName component
) {
181 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
182 Log
.d(TAG
, "Download service suddenly disconnected");
183 mDownloaderBinder
= null
;
184 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
185 Log
.d(TAG
, "Upload service suddenly disconnected");
186 mUploaderBinder
= null
;
193 public void onDestroy() {
195 if (mDownloadConnection
!= null
) {
196 unbindService(mDownloadConnection
);
197 mDownloadConnection
= null
;
199 if (mUploadConnection
!= null
) {
200 unbindService(mUploadConnection
);
201 mUploadConnection
= null
;
207 public boolean onOptionsItemSelected(MenuItem item
) {
208 boolean returnValue
= false
;
210 switch(item
.getItemId()){
211 case android
.R
.id
.home
:
212 backToDisplayActivity();
216 returnValue
= super.onOptionsItemSelected(item
);
224 protected void onResume() {
226 Log
.e(TAG
, "PREVIEW ACTIVITY ONRESUME");
227 mDownloadFinishReceiver
= new DownloadFinishReceiver();
228 IntentFilter filter
= new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
229 registerReceiver(mDownloadFinishReceiver
, filter
);
234 protected void onPostResume() {
235 super.onPostResume();
236 Log
.e(TAG
, "PREVIEW ACTIVITY ONPOSTRESUME");
240 public void onPause() {
242 unregisterReceiver(mDownloadFinishReceiver
);
243 mDownloadFinishReceiver
= null
;
247 private void backToDisplayActivity() {
249 Intent intent = new Intent(this, FileDisplayActivity.class);
250 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
251 intent.putExtra(FileDetailFragment.EXTRA_FILE, mFile);
252 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, mAccount);
253 startActivity(intent);
260 protected Dialog
onCreateDialog(int id
) {
261 Dialog dialog
= null
;
263 case DIALOG_SHORT_WAIT
: {
264 ProgressDialog working_dialog
= new ProgressDialog(this);
265 working_dialog
.setMessage(getResources().getString(
266 R
.string
.wait_a_moment
));
267 working_dialog
.setIndeterminate(true
);
268 working_dialog
.setCancelable(false
);
269 dialog
= working_dialog
;
283 public void onFileStateChanged() {
284 // nothing to do here!
292 public FileDownloaderBinder
getFileDownloaderBinder() {
293 return mDownloaderBinder
;
298 public FileUploaderBinder
getFileUploaderBinder() {
299 return mUploaderBinder
;
304 public void showFragmentWithDetails(OCFile file
) {
305 Intent showDetailsIntent
= new Intent(this, FileDetailActivity
.class);
306 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_FILE
, file
);
307 showDetailsIntent
.putExtra(FileDetailFragment
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
308 showDetailsIntent
.putExtra(FileDetailActivity
.EXTRA_MODE
, FileDetailActivity
.MODE_DETAILS
);
309 startActivity(showDetailsIntent
);
313 private void requestForDownload(OCFile file
) {
314 Log
.e(TAG
, "REQUEST FOR DOWNLOAD : " + file
.getFileName());
315 if (mDownloaderBinder
== null
) {
316 Log
.e(TAG
, "requestForDownload called without binder to download service");
318 } else if (!mDownloaderBinder
.isDownloading(mAccount
, file
)) {
319 Intent i
= new Intent(this, FileDownloader
.class);
320 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
321 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
327 public void notifySuccessfulDownload(OCFile file
, Intent intent
, boolean success
) {
330 if (mWaitingToPreview != null && mWaitingToPreview.equals(file)) {
331 mWaitingToPreview = null;
332 int position = mViewPager.getCurrentItem();
333 mPreviewImagePagerAdapter.updateFile(position, file);
334 Log.e(TAG, "BEFORE NOTIFY DATA SET CHANGED");
335 mPreviewImagePagerAdapter.notifyDataSetChanged();
336 Log.e(TAG, "AFTER NOTIFY DATA SET CHANGED");
344 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
346 * @param Position Position index of the new selected page
349 public void onPageSelected(int position
) {
350 Log
.e(TAG
, "onPageSelected " + position
);
351 if (mDownloaderBinder
== null
) {
352 mRequestWaitingForBinder
= true
;
355 OCFile currentFile
= mPreviewImagePagerAdapter
.getFileAt(position
);
356 getSupportActionBar().setTitle(currentFile
.getFileName());
357 if (!currentFile
.isDown()) {
358 requestForDownload(currentFile
);
359 //updateCurrentDownloadFragment(true);
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
) {
389 private void updateCurrentDownloadFragment(boolean transferring
) {
390 FileFragment fragment
= mPreviewImagePagerAdapter
.getFragmentAt(mViewPager
.getCurrentItem());
391 if (fragment
instanceof FileDownloadFragment
) {
392 ((FileDownloadFragment
) fragment
).updateView(transferring
);
393 //mViewPager.invalidate();
399 * Class waiting for broadcast events from the {@link FielDownloader} service.
401 * Updates the UI when a download is started or finished, provided that it is relevant for the
402 * folder displayed in the gallery.
404 private class DownloadFinishReceiver
extends BroadcastReceiver
{
406 public void onReceive(Context context
, Intent intent
) {
407 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
408 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
409 if (mAccount
.name
.equals(accountName
) &&
410 downloadedRemotePath
!= null
) {
412 OCFile file
= mStorageManager
.getFileByPath(downloadedRemotePath
);
413 int position
= mPreviewImagePagerAdapter
.getFilePosition(file
);
414 boolean downloadWasFine
= intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
);
415 boolean isCurrent
= (mViewPager
.getCurrentItem() == position
);
419 Log
.e(TAG
, "downloaded file FOUND in adapter");
420 if (downloadWasFine
) {
421 mPreviewImagePagerAdapter
.updateFile(position
, file
);
422 //Log.e(TAG, "BEFORE NOTIFY DATA SET CHANGED");
423 mPreviewImagePagerAdapter
.notifyDataSetChanged();
424 //Log.e(TAG, "AFTER NOTIFY DATA SET CHANGED");
426 } else if (isCurrent
) {
427 updateCurrentDownloadFragment(false
);
431 Log
.e(TAG
, "DOWNLOADED FILE NOT FOUND IN ADAPTER ");
435 removeStickyBroadcast(intent
);