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
.annotation
.SuppressLint
;
20 import android
.content
.BroadcastReceiver
;
21 import android
.content
.ComponentName
;
22 import android
.content
.Context
;
23 import android
.content
.Intent
;
24 import android
.content
.IntentFilter
;
25 import android
.content
.ServiceConnection
;
26 import android
.content
.SharedPreferences
;
27 import android
.os
.Build
;
28 import android
.os
.Bundle
;
29 import android
.os
.Handler
;
30 import android
.os
.IBinder
;
31 import android
.os
.Message
;
32 import android
.preference
.PreferenceManager
;
33 import android
.support
.v4
.view
.ViewPager
;
34 import android
.view
.View
;
36 import com
.actionbarsherlock
.app
.ActionBar
;
37 import com
.actionbarsherlock
.view
.MenuItem
;
38 import com
.actionbarsherlock
.view
.Window
;
39 import com
.ortiz
.touch
.ExtendedViewPager
;
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
.FileDownloader
.FileDownloaderBinder
;
46 import com
.owncloud
.android
.files
.services
.FileUploader
;
47 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
48 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
49 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
50 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
51 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
52 import com
.owncloud
.android
.operations
.CreateShareOperation
;
53 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
54 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
55 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
56 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
57 import com
.owncloud
.android
.ui
.activity
.PinCodeActivity
;
58 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
59 import com
.owncloud
.android
.utils
.DisplayUtils
;
60 import com
.owncloud
.android
.utils
.Log_OC
;
64 * Holds a swiping galley where image files contained in an ownCloud directory are shown
66 * @author David A. Velasco
68 public class PreviewImageActivity
extends FileActivity
implements
69 FileFragment
.ContainerActivity
,
70 ViewPager
.OnPageChangeListener
, OnRemoteOperationListener
{
72 public static final int DIALOG_SHORT_WAIT
= 0;
74 public static final String TAG
= PreviewImageActivity
.class.getSimpleName();
76 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
77 private static final String KEY_WAITING_FOR_BINDER
= "WAITING_FOR_BINDER";
79 private static final int INITIAL_HIDE_DELAY
= 0; // immediate hide
81 private ExtendedViewPager mViewPager
;
82 private PreviewImagePagerAdapter mPreviewImagePagerAdapter
;
84 private boolean mRequestWaitingForBinder
;
86 private DownloadFinishReceiver mDownloadFinishReceiver
;
88 //private boolean mFullScreen;
90 private View mFullScreenAnchorView
;
94 protected void onCreate(Bundle savedInstanceState
) {
95 super.onCreate(savedInstanceState
);
97 requestWindowFeature(Window
.FEATURE_ACTION_BAR_OVERLAY
);
98 setContentView(R
.layout
.preview_image_activity
);
100 ActionBar actionBar
= getSupportActionBar();
101 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
102 actionBar
.setDisplayHomeAsUpEnabled(true
);
106 if (getIntent().getExtras() != null
&& savedInstanceState
== null
&& fromNotification()) {
110 // Make sure we're running on Honeycomb or higher to use FullScreen and
112 if (isHoneycombOrHigher()) {
114 // mFullScreen = true;
115 mFullScreenAnchorView
= getWindow().getDecorView();
116 // to keep our UI controls visibility in line with system bars
118 mFullScreenAnchorView
.setOnSystemUiVisibilityChangeListener(new View
.OnSystemUiVisibilityChangeListener() {
119 @SuppressLint("InlinedApi")
121 public void onSystemUiVisibilityChange(int flags
) {
122 boolean visible
= (flags
& View
.SYSTEM_UI_FLAG_HIDE_NAVIGATION
) == 0;
123 ActionBar actionBar
= getSupportActionBar();
134 if (savedInstanceState
!= null
) {
135 mRequestWaitingForBinder
= savedInstanceState
.getBoolean(KEY_WAITING_FOR_BINDER
);
137 mRequestWaitingForBinder
= false
;
142 private void initViewPager() {
143 // get parent from path
144 String parentPath
= getFile().getRemotePath().substring(0, getFile().getRemotePath().lastIndexOf(getFile().getFileName()));
145 OCFile parentFolder
= getStorageManager().getFileByPath(parentPath
);
146 if (parentFolder
== null
) {
147 // should not be necessary
148 parentFolder
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
);
150 mPreviewImagePagerAdapter
= new PreviewImagePagerAdapter(getSupportFragmentManager(), parentFolder
, getAccount(), getStorageManager());
151 mViewPager
= (ExtendedViewPager
) findViewById(R
.id
.fragmentPager
);
152 int position
= mPreviewImagePagerAdapter
.getFilePosition(getFile());
153 position
= (position
>= 0) ? position
: 0;
154 mViewPager
.setAdapter(mPreviewImagePagerAdapter
);
155 mViewPager
.setOnPageChangeListener(this);
156 mViewPager
.setCurrentItem(position
);
157 if (position
== 0 && !getFile().isDown()) {
158 // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0)
159 mRequestWaitingForBinder
= true
;
164 protected void onPostCreate(Bundle savedInstanceState
) {
165 super.onPostCreate(savedInstanceState
);
167 // Trigger the initial hide() shortly after the activity has been
168 // created, to briefly hint to the user that UI controls
170 delayedHide(INITIAL_HIDE_DELAY
);
174 Handler mHideSystemUiHandler
= new Handler() {
176 public void handleMessage(Message msg
) {
177 if (isHoneycombOrHigher()) {
178 hideSystemUI(mFullScreenAnchorView
);
180 getSupportActionBar().hide();
184 private void delayedHide(int delayMillis
) {
185 mHideSystemUiHandler
.removeMessages(0);
186 mHideSystemUiHandler
.sendEmptyMessageDelayed(0, delayMillis
);
190 /// handle Window Focus changes
192 public void onWindowFocusChanged(boolean hasFocus
) {
193 super.onWindowFocusChanged(hasFocus
);
195 // When the window loses focus (e.g. the action overflow is shown),
196 // cancel any pending hide action. When the window gains focus,
197 // hide the system UI.
199 mHideSystemUiHandler
.removeMessages(0);
206 public void onStart() {
211 protected void onSaveInstanceState(Bundle outState
) {
212 super.onSaveInstanceState(outState
);
213 outState
.putBoolean(KEY_WAITING_FOR_BINDER
, mRequestWaitingForBinder
);
217 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
218 super.onRemoteOperationFinish(operation
, result
);
220 if (operation
instanceof CreateShareOperation
) {
221 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
223 } else if (operation
instanceof UnshareLinkOperation
) {
224 onUnshareLinkOperationFinish((UnshareLinkOperation
) operation
, result
);
226 } else if (operation
instanceof RemoveFileOperation
) {
232 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
, RemoteOperationResult result
) {
233 if (result
.isSuccess()) {
234 OCFile file
= getStorageManager().getFileByPath(getFile().getRemotePath());
238 invalidateOptionsMenu();
239 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
240 backToDisplayActivity();
245 private void onCreateShareOperationFinish(CreateShareOperation operation
, RemoteOperationResult result
) {
246 if (result
.isSuccess()) {
247 OCFile file
= getStorageManager().getFileByPath(getFile().getRemotePath());
251 invalidateOptionsMenu();
256 protected ServiceConnection
newTransferenceServiceConnection() {
257 return new PreviewImageServiceConnection();
260 /** Defines callbacks for service binding, passed to bindService() */
261 private class PreviewImageServiceConnection
implements ServiceConnection
{
264 public void onServiceConnected(ComponentName component
, IBinder service
) {
266 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
267 mDownloaderBinder
= (FileDownloaderBinder
) service
;
268 if (mRequestWaitingForBinder
) {
269 mRequestWaitingForBinder
= false
;
270 Log_OC
.d(TAG
, "Simulating reselection of current page after connection of download binder");
271 onPageSelected(mViewPager
.getCurrentItem());
274 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
275 Log_OC
.d(TAG
, "Upload service connected");
276 mUploaderBinder
= (FileUploaderBinder
) service
;
284 public void onServiceDisconnected(ComponentName component
) {
285 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
286 Log_OC
.d(TAG
, "Download service suddenly disconnected");
287 mDownloaderBinder
= null
;
288 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
289 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
290 mUploaderBinder
= null
;
297 public void onStop() {
303 public void onDestroy() {
308 public boolean onOptionsItemSelected(MenuItem item
) {
309 boolean returnValue
= false
;
311 switch(item
.getItemId()){
312 case android
.R
.id
.home
:
313 backToDisplayActivity();
317 returnValue
= super.onOptionsItemSelected(item
);
325 protected void onResume() {
327 //Log.e(TAG, "ACTIVITY, ONRESUME");
328 mDownloadFinishReceiver
= new DownloadFinishReceiver();
330 IntentFilter filter
= new IntentFilter(FileDownloader
.getDownloadFinishMessage());
331 filter
.addAction(FileDownloader
.getDownloadAddedMessage());
332 registerReceiver(mDownloadFinishReceiver
, filter
);
336 protected void onPostResume() {
337 //Log.e(TAG, "ACTIVITY, ONPOSTRESUME");
338 super.onPostResume();
342 public void onPause() {
343 unregisterReceiver(mDownloadFinishReceiver
);
344 mDownloadFinishReceiver
= null
;
349 private void backToDisplayActivity() {
354 public void showDetails(OCFile file
) {
355 Intent showDetailsIntent
= new Intent(this, FileDisplayActivity
.class);
356 showDetailsIntent
.setAction(FileDisplayActivity
.ACTION_DETAILS
);
357 showDetailsIntent
.putExtra(FileActivity
.EXTRA_FILE
, file
);
358 showDetailsIntent
.putExtra(FileActivity
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
359 startActivity(showDetailsIntent
);
360 int pos
= mPreviewImagePagerAdapter
.getFilePosition(file
);
361 file
= mPreviewImagePagerAdapter
.getFileAt(pos
);
366 private void requestForDownload(OCFile file
) {
367 if (mDownloaderBinder
== null
) {
368 Log_OC
.d(TAG
, "requestForDownload called without binder to download service");
370 } else if (!mDownloaderBinder
.isDownloading(getAccount(), file
)) {
371 Intent i
= new Intent(this, FileDownloader
.class);
372 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, getAccount());
373 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
379 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
381 * @param Position Position index of the new selected page
384 public void onPageSelected(int position
) {
385 if (mDownloaderBinder
== null
) {
386 mRequestWaitingForBinder
= true
;
389 OCFile currentFile
= mPreviewImagePagerAdapter
.getFileAt(position
);
390 getSupportActionBar().setTitle(currentFile
.getFileName());
391 if (!currentFile
.isDown()) {
392 if (!mPreviewImagePagerAdapter
.pendingErrorAt(position
)) {
393 requestForDownload(currentFile
);
397 // Call to reset image zoom to initial state
398 ((PreviewImagePagerAdapter
) mViewPager
.getAdapter()).resetZoom();
404 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
405 * when the pager is automatically settling to the current page. when it is fully stopped/idle.
407 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
410 public void onPageScrollStateChanged(int state
) {
414 * This method will be invoked when the current page is scrolled, either as part of a programmatically
415 * initiated smooth scroll or a user initiated touch scroll.
417 * @param position Position index of the first page currently being displayed.
418 * Page position+1 will be visible if positionOffset is nonzero.
420 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
421 * @param positionOffsetPixels Value in pixels indicating the offset from position.
424 public void onPageScrolled(int position
, float positionOffset
, int positionOffsetPixels
) {
429 * Class waiting for broadcast events from the {@link FielDownloader} service.
431 * Updates the UI when a download is started or finished, provided that it is relevant for the
432 * folder displayed in the gallery.
434 private class DownloadFinishReceiver
extends BroadcastReceiver
{
436 public void onReceive(Context context
, Intent intent
) {
437 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
438 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
439 if (getAccount().name
.equals(accountName
) &&
440 downloadedRemotePath
!= null
) {
442 OCFile file
= getStorageManager().getFileByPath(downloadedRemotePath
);
443 int position
= mPreviewImagePagerAdapter
.getFilePosition(file
);
444 boolean downloadWasFine
= intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
);
445 //boolean isOffscreen = Math.abs((mViewPager.getCurrentItem() - position)) <= mViewPager.getOffscreenPageLimit();
447 if (position
>= 0 && intent
.getAction().equals(FileDownloader
.getDownloadFinishMessage())) {
448 if (downloadWasFine
) {
449 mPreviewImagePagerAdapter
.updateFile(position
, file
);
452 mPreviewImagePagerAdapter
.updateWithDownloadError(position
);
454 mPreviewImagePagerAdapter
.notifyDataSetChanged(); // will trigger the creation of new fragments
457 Log_OC
.d(TAG
, "Download finished, but the fragment is offscreen");
461 removeStickyBroadcast(intent
);
466 @SuppressLint("InlinedApi")
467 public void toggleFullScreen() {
469 if (isHoneycombOrHigher()) {
471 boolean visible
= (mFullScreenAnchorView
.getSystemUiVisibility() & View
.SYSTEM_UI_FLAG_HIDE_NAVIGATION
) == 0;
474 hideSystemUI(mFullScreenAnchorView
);
475 // actionBar.hide(); // propagated through
476 // OnSystemUiVisibilityChangeListener()
478 showSystemUI(mFullScreenAnchorView
);
479 // actionBar.show(); // propagated through
480 // OnSystemUiVisibilityChangeListener()
485 ActionBar actionBar
= getSupportActionBar();
486 if (!actionBar
.isShowing()) {
498 protected void onAccountSet(boolean stateWasRecovered
) {
499 super.onAccountSet(stateWasRecovered
);
500 if (getAccount() != null
) {
501 OCFile file
= getFile();
502 /// Validate handled file (first image to preview)
504 throw new IllegalStateException("Instanced with a NULL OCFile");
506 if (!file
.isImage()) {
507 throw new IllegalArgumentException("Non-image file passed as argument");
510 // Update file according to DB file, if it is possible
511 if (file
.getFileId() > FileDataStorageManager
.ROOT_PARENT_ID
)
512 file
= getStorageManager().getFileById(file
.getFileId());
515 /// Refresh the activity according to the Account and OCFile set
516 setFile(file
); // reset after getting it fresh from storageManager
517 getSupportActionBar().setTitle(getFile().getFileName());
518 //if (!stateWasRecovered) {
523 // handled file not in the current Account
531 * Launch an intent to request the PIN code to the user before letting him use the app
533 private void requestPinCode() {
534 boolean pinStart
= false
;
535 SharedPreferences appPrefs
= PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
536 pinStart
= appPrefs
.getBoolean("set_pincode", false
);
538 Intent i
= new Intent(getApplicationContext(), PinCodeActivity
.class);
539 i
.putExtra(PinCodeActivity
.EXTRA_ACTIVITY
, "PreviewImageActivity");
545 public void onBrowsedDownTo(OCFile folder
) {
546 // TODO Auto-generated method stub
551 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
552 // TODO Auto-generated method stub
557 @SuppressLint("InlinedApi")
558 private void hideSystemUI(View anchorView
) {
559 anchorView
.setSystemUiVisibility(
560 View
.SYSTEM_UI_FLAG_HIDE_NAVIGATION
// hides NAVIGATION BAR; Android >= 4.0
561 | View
.SYSTEM_UI_FLAG_FULLSCREEN
// hides STATUS BAR; Android >= 4.1
562 | View
.SYSTEM_UI_FLAG_IMMERSIVE
// stays interactive; Android >= 4.4
563 | View
.SYSTEM_UI_FLAG_LAYOUT_STABLE
// draw full window; Android >= 4.1
564 | View
.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// draw full window; Android >= 4.1
565 | View
.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
// draw full window; Android >= 4.1
569 @SuppressLint("InlinedApi")
570 private void showSystemUI(View anchorView
) {
571 anchorView
.setSystemUiVisibility(
572 View
.SYSTEM_UI_FLAG_LAYOUT_STABLE
// draw full window; Android >= 4.1
573 | View
.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// draw full window; Android >= 4.1
574 | View
.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
// draw full window; Android >= 4.1
579 * Checks if OS version is Honeycomb one or higher
583 private boolean isHoneycombOrHigher() {
584 if (Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.HONEYCOMB
) {