2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.ui
.preview
;
22 import android
.annotation
.SuppressLint
;
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
.Build
;
30 import android
.os
.Bundle
;
31 import android
.os
.Handler
;
32 import android
.os
.IBinder
;
33 import android
.os
.Message
;
34 import android
.support
.v4
.view
.ViewPager
;
35 import android
.view
.View
;
37 import com
.actionbarsherlock
.app
.ActionBar
;
38 import com
.actionbarsherlock
.view
.MenuItem
;
39 import com
.actionbarsherlock
.view
.Window
;
40 import com
.ortiz
.touch
.ExtendedViewPager
;
41 import com
.owncloud
.android
.MainApp
;
42 import com
.owncloud
.android
.R
;
43 import com
.owncloud
.android
.authentication
.AccountUtils
;
44 import com
.owncloud
.android
.authentication
.PinCheck
;
45 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
46 import com
.owncloud
.android
.datamodel
.OCFile
;
47 import com
.owncloud
.android
.files
.services
.FileDownloader
;
48 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
49 import com
.owncloud
.android
.files
.services
.FileUploader
;
50 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
51 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
52 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
53 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
54 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
55 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
56 import com
.owncloud
.android
.operations
.CreateShareOperation
;
57 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
58 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
59 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
60 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
61 import com
.owncloud
.android
.ui
.activity
.PinCodeActivity
;
62 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
63 import com
.owncloud
.android
.utils
.DisplayUtils
;
67 * Holds a swiping galley where image files contained in an ownCloud directory are shown
69 public class PreviewImageActivity
extends FileActivity
implements
70 FileFragment
.ContainerActivity
,
71 ViewPager
.OnPageChangeListener
, OnRemoteOperationListener
{
73 public static final int DIALOG_SHORT_WAIT
= 0;
75 public static final String TAG
= PreviewImageActivity
.class.getSimpleName();
77 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
78 private static final String KEY_WAITING_FOR_BINDER
= "WAITING_FOR_BINDER";
80 private static final int INITIAL_HIDE_DELAY
= 0; // immediate hide
82 private ExtendedViewPager mViewPager
;
83 private PreviewImagePagerAdapter mPreviewImagePagerAdapter
;
84 private int mSavedPosition
= 0;
85 private boolean mHasSavedPosition
= false
;
87 private boolean mRequestWaitingForBinder
;
89 private DownloadFinishReceiver mDownloadFinishReceiver
;
91 private View mFullScreenAnchorView
;
93 private Boolean mUnlocked
= false
;
97 protected void onCreate(Bundle savedInstanceState
) {
98 super.onCreate(savedInstanceState
);
100 requestWindowFeature(Window
.FEATURE_ACTION_BAR_OVERLAY
);
101 setContentView(R
.layout
.preview_image_activity
);
103 ActionBar actionBar
= getSupportActionBar();
104 actionBar
.setIcon(DisplayUtils
.getSeasonalIconId());
105 actionBar
.setDisplayHomeAsUpEnabled(true
);
108 // Make sure we're running on Honeycomb or higher to use FullScreen and
110 if (isHoneycombOrHigher()) {
112 mFullScreenAnchorView
= getWindow().getDecorView();
113 // to keep our UI controls visibility in line with system bars
115 mFullScreenAnchorView
.setOnSystemUiVisibilityChangeListener(new View
.OnSystemUiVisibilityChangeListener() {
116 @SuppressLint("InlinedApi")
118 public void onSystemUiVisibilityChange(int flags
) {
119 boolean visible
= (flags
& View
.SYSTEM_UI_FLAG_HIDE_NAVIGATION
) == 0;
120 ActionBar actionBar
= getSupportActionBar();
131 if (savedInstanceState
!= null
) {
132 mRequestWaitingForBinder
= savedInstanceState
.getBoolean(KEY_WAITING_FOR_BINDER
);
134 mRequestWaitingForBinder
= false
;
139 private void initViewPager() {
140 // get parent from path
141 String parentPath
= getFile().getRemotePath().substring(0, getFile().getRemotePath().lastIndexOf(getFile().getFileName()));
142 OCFile parentFolder
= getStorageManager().getFileByPath(parentPath
);
143 if (parentFolder
== null
) {
144 // should not be necessary
145 parentFolder
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
);
147 mPreviewImagePagerAdapter
= new PreviewImagePagerAdapter(getSupportFragmentManager(), parentFolder
, getAccount(), getStorageManager());
148 mViewPager
= (ExtendedViewPager
) findViewById(R
.id
.fragmentPager
);
149 int position
= mHasSavedPosition ? mSavedPosition
: mPreviewImagePagerAdapter
.getFilePosition(getFile());
150 position
= (position
>= 0) ? position
: 0;
151 mViewPager
.setAdapter(mPreviewImagePagerAdapter
);
152 mViewPager
.setOnPageChangeListener(this);
153 mViewPager
.setCurrentItem(position
);
154 if (position
== 0 && !getFile().isDown()) {
155 // this is necessary because mViewPager.setCurrentItem(0) just after setting the adapter does not result in a call to #onPageSelected(0)
156 mRequestWaitingForBinder
= true
;
161 protected void onPostCreate(Bundle savedInstanceState
) {
162 super.onPostCreate(savedInstanceState
);
164 // Trigger the initial hide() shortly after the activity has been
165 // created, to briefly hint to the user that UI controls
167 delayedHide(INITIAL_HIDE_DELAY
);
171 Handler mHideSystemUiHandler
= new Handler() {
173 public void handleMessage(Message msg
) {
174 if (isHoneycombOrHigher()) {
175 hideSystemUI(mFullScreenAnchorView
);
177 getSupportActionBar().hide();
181 private void delayedHide(int delayMillis
) {
182 mHideSystemUiHandler
.removeMessages(0);
183 mHideSystemUiHandler
.sendEmptyMessageDelayed(0, delayMillis
);
187 /// handle Window Focus changes
189 public void onWindowFocusChanged(boolean hasFocus
) {
190 super.onWindowFocusChanged(hasFocus
);
192 // When the window loses focus (e.g. the action overflow is shown),
193 // cancel any pending hide action.
195 mHideSystemUiHandler
.removeMessages(0);
202 public void onStart() {
207 protected void onSaveInstanceState(Bundle outState
) {
208 super.onSaveInstanceState(outState
);
209 outState
.putBoolean(KEY_WAITING_FOR_BINDER
, mRequestWaitingForBinder
);
213 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
214 super.onRemoteOperationFinish(operation
, result
);
216 if (operation
instanceof CreateShareOperation
) {
217 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
219 } else if (operation
instanceof UnshareLinkOperation
) {
220 onUnshareLinkOperationFinish((UnshareLinkOperation
) operation
, result
);
222 } else if (operation
instanceof RemoveFileOperation
) {
228 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
, RemoteOperationResult result
) {
229 if (result
.isSuccess()) {
230 OCFile file
= getStorageManager().getFileByPath(getFile().getRemotePath());
234 invalidateOptionsMenu();
235 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
236 backToDisplayActivity();
241 private void onCreateShareOperationFinish(CreateShareOperation operation
, RemoteOperationResult result
) {
242 if (result
.isSuccess()) {
243 OCFile file
= getStorageManager().getFileByPath(getFile().getRemotePath());
247 invalidateOptionsMenu();
252 protected ServiceConnection
newTransferenceServiceConnection() {
253 return new PreviewImageServiceConnection();
256 /** Defines callbacks for service binding, passed to bindService() */
257 private class PreviewImageServiceConnection
implements ServiceConnection
{
260 public void onServiceConnected(ComponentName component
, IBinder service
) {
262 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
263 mDownloaderBinder
= (FileDownloaderBinder
) service
;
264 if (mRequestWaitingForBinder
) {
265 mRequestWaitingForBinder
= false
;
266 Log_OC
.d(TAG
, "Simulating reselection of current page after connection of download binder");
267 onPageSelected(mViewPager
.getCurrentItem());
270 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
271 Log_OC
.d(TAG
, "Upload service connected");
272 mUploaderBinder
= (FileUploaderBinder
) service
;
280 public void onServiceDisconnected(ComponentName component
) {
281 if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileDownloader
.class))) {
282 Log_OC
.d(TAG
, "Download service suddenly disconnected");
283 mDownloaderBinder
= null
;
284 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this, FileUploader
.class))) {
285 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
286 mUploaderBinder
= null
;
293 public void onStop() {
299 public void onDestroy() {
304 public boolean onOptionsItemSelected(MenuItem item
) {
305 boolean returnValue
= false
;
307 switch(item
.getItemId()){
308 case android
.R
.id
.home
:
309 backToDisplayActivity();
313 returnValue
= super.onOptionsItemSelected(item
);
321 protected void onResume() {
324 mDownloadFinishReceiver
= new DownloadFinishReceiver();
326 IntentFilter filter
= new IntentFilter(FileDownloader
.getDownloadFinishMessage());
327 filter
.addAction(FileDownloader
.getDownloadAddedMessage());
328 registerReceiver(mDownloadFinishReceiver
, filter
);
332 protected void onPostResume() {
333 super.onPostResume();
337 public void onPause() {
338 if (mDownloadFinishReceiver
!= null
){
339 unregisterReceiver(mDownloadFinishReceiver
);
340 mDownloadFinishReceiver
= null
;
343 PinCheck
.setUnlockTimestamp();
348 private void backToDisplayActivity() {
353 public void showDetails(OCFile file
) {
354 Intent showDetailsIntent
= new Intent(this, FileDisplayActivity
.class);
355 showDetailsIntent
.setAction(FileDisplayActivity
.ACTION_DETAILS
);
356 showDetailsIntent
.putExtra(FileActivity
.EXTRA_FILE
, file
);
357 showDetailsIntent
.putExtra(FileActivity
.EXTRA_ACCOUNT
, AccountUtils
.getCurrentOwnCloudAccount(this));
358 startActivity(showDetailsIntent
);
359 int pos
= mPreviewImagePagerAdapter
.getFilePosition(file
);
360 file
= mPreviewImagePagerAdapter
.getFileAt(pos
);
365 private void requestForDownload(OCFile file
) {
366 if (mDownloaderBinder
== null
) {
367 Log_OC
.d(TAG
, "requestForDownload called without binder to download service");
369 } else if (!mDownloaderBinder
.isDownloading(getAccount(), file
)) {
370 Intent i
= new Intent(this, FileDownloader
.class);
371 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, getAccount());
372 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
378 * This method will be invoked when a new page becomes selected. Animation is not necessarily complete.
380 * @param Position Position index of the new selected page
383 public void onPageSelected(int position
) {
384 mSavedPosition
= position
;
385 mHasSavedPosition
= true
;
386 if (mDownloaderBinder
== null
) {
387 mRequestWaitingForBinder
= true
;
390 OCFile currentFile
= mPreviewImagePagerAdapter
.getFileAt(position
);
391 getSupportActionBar().setTitle(currentFile
.getFileName());
392 if (!currentFile
.isDown()) {
393 if (!mPreviewImagePagerAdapter
.pendingErrorAt(position
)) {
394 requestForDownload(currentFile
);
398 // Call to reset image zoom to initial state
399 ((PreviewImagePagerAdapter
) mViewPager
.getAdapter()).resetZoom();
405 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
406 * when the pager is automatically settling to the current page. when it is fully stopped/idle.
408 * @param State The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
411 public void onPageScrollStateChanged(int state
) {
415 * This method will be invoked when the current page is scrolled, either as part of a programmatically
416 * initiated smooth scroll or a user initiated touch scroll.
418 * @param position Position index of the first page currently being displayed.
419 * Page position+1 will be visible if positionOffset is nonzero.
421 * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
422 * @param positionOffsetPixels Value in pixels indicating the offset from position.
425 public void onPageScrolled(int position
, float positionOffset
, int positionOffsetPixels
) {
430 * Class waiting for broadcast events from the {@link FileDownloader} service.
432 * Updates the UI when a download is started or finished, provided that it is relevant for the
433 * folder displayed in the gallery.
435 private class DownloadFinishReceiver
extends BroadcastReceiver
{
437 public void onReceive(Context context
, Intent intent
) {
438 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
439 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
440 if (getAccount().name
.equals(accountName
) &&
441 downloadedRemotePath
!= null
) {
443 OCFile file
= getStorageManager().getFileByPath(downloadedRemotePath
);
444 int position
= mPreviewImagePagerAdapter
.getFilePosition(file
);
445 boolean downloadWasFine
= intent
.getBooleanExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
);
446 //boolean isOffscreen = Math.abs((mViewPager.getCurrentItem() - position)) <= mViewPager.getOffscreenPageLimit();
448 if (position
>= 0 && intent
.getAction().equals(FileDownloader
.getDownloadFinishMessage())) {
449 if (downloadWasFine
) {
450 mPreviewImagePagerAdapter
.updateFile(position
, file
);
453 mPreviewImagePagerAdapter
.updateWithDownloadError(position
);
455 mPreviewImagePagerAdapter
.notifyDataSetChanged(); // will trigger the creation of new fragments
458 Log_OC
.d(TAG
, "Download finished, but the fragment is offscreen");
462 removeStickyBroadcast(intent
);
467 @SuppressLint("InlinedApi")
468 public void toggleFullScreen() {
470 if (isHoneycombOrHigher()) {
472 boolean visible
= (mFullScreenAnchorView
.getSystemUiVisibility()
473 & View
.SYSTEM_UI_FLAG_HIDE_NAVIGATION
) == 0;
476 hideSystemUI(mFullScreenAnchorView
);
477 // actionBar.hide(); // propagated through
478 // OnSystemUiVisibilityChangeListener()
480 showSystemUI(mFullScreenAnchorView
);
481 // actionBar.show(); // propagated through
482 // OnSystemUiVisibilityChangeListener()
487 ActionBar actionBar
= getSupportActionBar();
488 if (!actionBar
.isShowing()) {
500 protected void onAccountSet(boolean stateWasRecovered
) {
501 super.onAccountSet(stateWasRecovered
);
502 if (getAccount() != null
) {
503 OCFile file
= getFile();
504 /// Validate handled file (first image to preview)
506 throw new IllegalStateException("Instanced with a NULL OCFile");
508 if (!file
.isImage()) {
509 throw new IllegalArgumentException("Non-image file passed as argument");
512 // Update file according to DB file, if it is possible
513 if (file
.getFileId() > FileDataStorageManager
.ROOT_PARENT_ID
)
514 file
= getStorageManager().getFileById(file
.getFileId());
517 /// Refresh the activity according to the Account and OCFile set
518 setFile(file
); // reset after getting it fresh from storageManager
519 getSupportActionBar().setTitle(getFile().getFileName());
520 //if (!stateWasRecovered) {
525 // handled file not in the current Account
532 public void onBrowsedDownTo(OCFile folder
) {
533 // TODO Auto-generated method stub
538 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
539 // TODO Auto-generated method stub
544 @SuppressLint("InlinedApi")
545 private void hideSystemUI(View anchorView
) {
546 anchorView
.setSystemUiVisibility(
547 View
.SYSTEM_UI_FLAG_HIDE_NAVIGATION
// hides NAVIGATION BAR; Android >= 4.0
548 | View
.SYSTEM_UI_FLAG_FULLSCREEN
// hides STATUS BAR; Android >= 4.1
549 | View
.SYSTEM_UI_FLAG_IMMERSIVE
// stays interactive; Android >= 4.4
550 | View
.SYSTEM_UI_FLAG_LAYOUT_STABLE
// draw full window; Android >= 4.1
551 | View
.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// draw full window; Android >= 4.1
552 | View
.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
// draw full window; Android >= 4.1
556 @SuppressLint("InlinedApi")
557 private void showSystemUI(View anchorView
) {
558 anchorView
.setSystemUiVisibility(
559 View
.SYSTEM_UI_FLAG_LAYOUT_STABLE
// draw full window; Android >= 4.1
560 | View
.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// draw full window; Android >= 4.1
561 | View
.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
// draw full window; Android >= 4.1
566 * Checks if OS version is Honeycomb one or higher
570 private boolean isHoneycombOrHigher() {
571 if (Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.HONEYCOMB
) {