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 version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package com
.owncloud
.android
.ui
.preview
;
19 import android
.content
.BroadcastReceiver
;
20 import android
.content
.ComponentName
;
21 import android
.content
.Context
;
22 import android
.content
.Intent
;
23 import android
.content
.IntentFilter
;
24 import android
.content
.ServiceConnection
;
25 import android
.content
.SharedPreferences
;
26 import android
.os
.Bundle
;
27 import android
.os
.IBinder
;
28 import android
.preference
.PreferenceManager
;
29 import android
.support
.v4
.app
.Fragment
;
30 import android
.support
.v4
.app
.FragmentManager
;
31 import android
.support
.v4
.app
.FragmentTransaction
;
32 import android
.support
.v4
.view
.ViewPager
;
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
.view
.MenuItem
;
39 import com
.actionbarsherlock
.view
.Window
;
40 import com
.owncloud
.android
.R
;
41 import com
.owncloud
.android
.authentication
.AccountUtils
;
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
.FileUploader
;
46 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
47 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
48 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
49 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
50 import com
.owncloud
.android
.ui
.activity
.PinCodeActivity
;
51 import com
.owncloud
.android
.ui
.dialog
.LoadingDialog
;
52 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
53 import com
.owncloud
.android
.utils
.DisplayUtils
;
54 import com
.owncloud
.android
.utils
.Log_OC
;
58 * Holds a swiping galley where image files contained in an ownCloud directory are shown
60 * @author David A. Velasco
62 public class PreviewImageActivity
extends FileActivity
implements FileFragment
.ContainerActivity
, ViewPager
.OnPageChangeListener
, OnTouchListener
{
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";
69 private static final String KEY_WAITING_FOR_BINDER
= "WAITING_FOR_BINDER";
71 private static final String DIALOG_WAIT_TAG
= "DIALOG_WAIT";
73 private FileDataStorageManager 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
;
82 private boolean mRequestWaitingForBinder
;
84 private DownloadFinishReceiver mDownloadFinishReceiver
;
86 private boolean mFullScreen
;
90 protected void onCreate(Bundle savedInstanceState
) {
91 super.onCreate(savedInstanceState
);
93 requestWindowFeature(Window
.FEATURE_ACTION_BAR_OVERLAY
);
94 setContentView(R
.layout
.preview_image_activity
);
96 ActionBar actionBar
= getSupportActionBar();
97 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
98 actionBar
.setDisplayHomeAsUpEnabled(true
);
102 if (getIntent().getExtras() != null
&& savedInstanceState
== null
&& fromNotification()) {
107 if (savedInstanceState
!= null
) {
108 mRequestWaitingForBinder
= savedInstanceState
.getBoolean(KEY_WAITING_FOR_BINDER
);
110 mRequestWaitingForBinder
= false
;
115 private void initViewPager() {
116 // get parent from path
117 String parentPath
= getFile().getRemotePath().substring(0, getFile().getRemotePath().lastIndexOf(getFile().getFileName()));
118 OCFile parentFolder
= mStorageManager
.getFileByPath(parentPath
);
119 //OCFile parentFolder = mStorageManager.getFileById(getFile().getParentId());
120 if (parentFolder
== null
) {
121 // should not be necessary
122 parentFolder
= mStorageManager
.getFileByPath(OCFile
.ROOT_PATH
);
124 mPreviewImagePagerAdapter
= new PreviewImagePagerAdapter(getSupportFragmentManager(), parentFolder
, getAccount(), mStorageManager
);
125 mViewPager
= (ViewPager
) findViewById(R
.id
.fragmentPager
);
126 int position
= mPreviewImagePagerAdapter
.getFilePosition(getFile());
127 position
= (position
>= 0) ? position
: 0;
128 mViewPager
.setAdapter(mPreviewImagePagerAdapter
);
129 mViewPager
.setOnPageChangeListener(this);
130 mViewPager
.setCurrentItem(position
);
131 if (position
== 0 && !getFile().isDown()) {
132 // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0)
133 mRequestWaitingForBinder
= true
;
139 public void onStart() {
141 mDownloadConnection
= new PreviewImageServiceConnection();
142 bindService(new Intent(this, FileDownloader
.class), mDownloadConnection
, Context
.BIND_AUTO_CREATE
);
143 mUploadConnection
= new PreviewImageServiceConnection();
144 bindService(new Intent(this, FileUploader
.class), mUploadConnection
, Context
.BIND_AUTO_CREATE
);
148 protected void onSaveInstanceState(Bundle outState
) {
149 super.onSaveInstanceState(outState
);
150 outState
.putBoolean(KEY_WAITING_FOR_BINDER
, mRequestWaitingForBinder
);
154 /** Defines callbacks for service binding, passed to bindService() */
155 private class PreviewImageServiceConnection
implements ServiceConnection
{
158 public void onServiceConnected(ComponentName component
, IBinder service
) {
160 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
161 mDownloaderBinder
= (FileDownloaderBinder
) service
;
162 if (mRequestWaitingForBinder
) {
163 mRequestWaitingForBinder
= false
;
164 Log_OC
.d(TAG
, "Simulating reselection of current page after connection of download binder");
165 onPageSelected(mViewPager
.getCurrentItem());
168 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
169 Log_OC
.d(TAG
, "Upload service connected");
170 mUploaderBinder
= (FileUploaderBinder
) service
;
178 public void onServiceDisconnected(ComponentName component
) {
179 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
180 Log_OC
.d(TAG
, "Download service suddenly disconnected");
181 mDownloaderBinder
= null
;
182 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
183 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
184 mUploaderBinder
= null
;
191 public void onStop() {
193 if (mDownloadConnection
!= null
) {
194 unbindService(mDownloadConnection
);
195 mDownloadConnection
= null
;
197 if (mUploadConnection
!= null
) {
198 unbindService(mUploadConnection
);
199 mUploadConnection
= null
;
205 public void onDestroy() {
211 public boolean onOptionsItemSelected(MenuItem item
) {
212 boolean returnValue
= false
;
214 switch(item
.getItemId()){
215 case android
.R
.id
.home
:
216 backToDisplayActivity();
220 returnValue
= super.onOptionsItemSelected(item
);
228 protected void onResume() {
230 //Log.e(TAG, "ACTIVITY, ONRESUME");
231 mDownloadFinishReceiver
= new DownloadFinishReceiver();
233 IntentFilter filter
= new IntentFilter(FileDownloader
.getDownloadFinishMessage());
234 filter
.addAction(FileDownloader
.getDownloadAddedMessage());
235 registerReceiver(mDownloadFinishReceiver
, filter
);
239 protected void onPostResume() {
240 //Log.e(TAG, "ACTIVITY, ONPOSTRESUME");
241 super.onPostResume();
245 public void onPause() {
247 unregisterReceiver(mDownloadFinishReceiver
);
248 mDownloadFinishReceiver
= null
;
252 private void backToDisplayActivity() {
257 * Show loading dialog
259 public void showLoadingDialog() {
261 LoadingDialog loading
= new LoadingDialog(getResources().getString(R
.string
.wait_a_moment
));
262 FragmentManager fm
= getSupportFragmentManager();
263 FragmentTransaction ft
= fm
.beginTransaction();
264 loading
.show(ft
, DIALOG_WAIT_TAG
);
269 * Dismiss loading dialog
271 public void dismissLoadingDialog(){
272 Fragment frag
= getSupportFragmentManager().findFragmentByTag(DIALOG_WAIT_TAG
);
274 LoadingDialog loading
= (LoadingDialog
) frag
;
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 showDetails(OCFile file
) {
305 Intent showDetailsIntent
= new Intent(this, FileDisplayActivity
.class);
306 showDetailsIntent
.setAction(FileDisplayActivity
.ACTION_DETAILS
);
307 showDetailsIntent
.putExtra(FileActivity
.EXTRA_FILE
, file
);
308 showDetailsIntent
.putExtra(FileActivity
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
309 startActivity(showDetailsIntent
);
310 int pos
= mPreviewImagePagerAdapter
.getFilePosition(file
);
311 file
= mPreviewImagePagerAdapter
.getFileAt(pos
);
316 private void requestForDownload(OCFile file
) {
317 if (mDownloaderBinder
== null
) {
318 Log_OC
.d(TAG
, "requestForDownload called without binder to download service");
320 } else if (!mDownloaderBinder
.isDownloading(getAccount(), file
)) {
321 Intent i
= new Intent(this, FileDownloader
.class);
322 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, getAccount());
323 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
329 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
331 * @param Position Position index of the new selected page
334 public void onPageSelected(int position
) {
335 if (mDownloaderBinder
== null
) {
336 mRequestWaitingForBinder
= true
;
339 OCFile currentFile
= mPreviewImagePagerAdapter
.getFileAt(position
);
340 getSupportActionBar().setTitle(currentFile
.getFileName());
341 if (!currentFile
.isDown()) {
342 if (!mPreviewImagePagerAdapter
.pendingErrorAt(position
)) {
343 requestForDownload(currentFile
);
350 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
351 * when the pager is automatically settling to the current page. when it is fully stopped/idle.
353 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
356 public void onPageScrollStateChanged(int state
) {
360 * This method will be invoked when the current page is scrolled, either as part of a programmatically
361 * initiated smooth scroll or a user initiated touch scroll.
363 * @param position Position index of the first page currently being displayed.
364 * Page position+1 will be visible if positionOffset is nonzero.
366 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
367 * @param positionOffsetPixels Value in pixels indicating the offset from position.
370 public void onPageScrolled(int position
, float positionOffset
, int positionOffsetPixels
) {
375 * Class waiting for broadcast events from the {@link FielDownloader} service.
377 * Updates the UI when a download is started or finished, provided that it is relevant for the
378 * folder displayed in the gallery.
380 private class DownloadFinishReceiver
extends BroadcastReceiver
{
382 public void onReceive(Context context
, Intent intent
) {
383 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
384 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
385 if (getAccount().name
.equals(accountName
) &&
386 downloadedRemotePath
!= null
) {
388 OCFile file
= mStorageManager
.getFileByPath(downloadedRemotePath
);
389 int position
= mPreviewImagePagerAdapter
.getFilePosition(file
);
390 boolean downloadWasFine
= intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
);
391 //boolean isOffscreen = Math.abs((mViewPager.getCurrentItem() - position)) <= mViewPager.getOffscreenPageLimit();
393 if (position
>= 0 && intent
.getAction().equals(FileDownloader
.getDownloadFinishMessage())) {
394 if (downloadWasFine
) {
395 mPreviewImagePagerAdapter
.updateFile(position
, file
);
398 mPreviewImagePagerAdapter
.updateWithDownloadError(position
);
400 mPreviewImagePagerAdapter
.notifyDataSetChanged(); // will trigger the creation of new fragments
403 Log_OC
.d(TAG
, "Download finished, but the fragment is offscreen");
407 removeStickyBroadcast(intent
);
414 public boolean onTouch(View v
, MotionEvent event
) {
415 if (event
.getAction() == MotionEvent
.ACTION_UP
) {
422 private void toggleFullScreen() {
423 ActionBar actionBar
= getSupportActionBar();
431 mFullScreen
= !mFullScreen
;
435 protected void onAccountSet(boolean stateWasRecovered
) {
436 if (getAccount() != null
) {
437 OCFile file
= getFile();
438 /// Validate handled file (first image to preview)
440 throw new IllegalStateException("Instanced with a NULL OCFile");
442 if (!file
.isImage()) {
443 throw new IllegalArgumentException("Non-image file passed as argument");
445 mStorageManager
= new FileDataStorageManager(getAccount(), getContentResolver());
447 // Update file according to DB file, if it is possible
448 if (file
.getFileId() > FileDataStorageManager
.ROOT_PARENT_ID
)
449 file
= mStorageManager
.getFileById(file
.getFileId());
452 /// Refresh the activity according to the Account and OCFile set
453 setFile(file
); // reset after getting it fresh from mStorageManager
454 getSupportActionBar().setTitle(getFile().getFileName());
455 //if (!stateWasRecovered) {
460 // handled file not in the current Account
465 Log_OC
.wtf(TAG
, "onAccountChanged was called with NULL account associated!");
471 * Launch an intent to request the PIN code to the user before letting him use the app
473 private void requestPinCode() {
474 boolean pinStart
= false
;
475 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
476 pinStart
= appPrefs
.getBoolean("set_pincode", false
);
478 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
479 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "PreviewImageActivity");