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 hd 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
.support
.v4
.widget
.DrawerLayout
;
36 import android
.support
.v7
.app
.ActionBar
;
37 import android
.view
.MenuItem
;
38 import android
.view
.View
;
39 import android
.view
.Window
;
41 import com
.ortiz
.touch
.ExtendedViewPager
;
42 import com
.owncloud
.android
.R
;
43 import com
.owncloud
.android
.authentication
.AccountUtils
;
44 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
45 import com
.owncloud
.android
.datamodel
.OCFile
;
46 import com
.owncloud
.android
.files
.services
.FileDownloader
;
47 import com
.owncloud
.android
.files
.services
.FileDownloader
.FileDownloaderBinder
;
48 import com
.owncloud
.android
.files
.services
.FileUploader
;
49 import com
.owncloud
.android
.files
.services
.FileUploader
.FileUploaderBinder
;
50 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
51 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
52 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
53 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
54 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
55 import com
.owncloud
.android
.operations
.CreateShareOperation
;
56 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
57 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
58 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
59 import com
.owncloud
.android
.ui
.activity
.FileDisplayActivity
;
60 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
61 import com
.owncloud
.android
.utils
.DisplayUtils
;
65 * Holds a swiping galley where image files contained in an ownCloud directory are shown
67 public class PreviewImageActivity
extends FileActivity
implements
68 FileFragment
.ContainerActivity
,
69 ViewPager
.OnPageChangeListener
, OnRemoteOperationListener
{
71 public static final int DIALOG_SHORT_WAIT
= 0;
73 public static final String TAG
= PreviewImageActivity
.class.getSimpleName();
75 public static final String KEY_WAITING_TO_PREVIEW
= "WAITING_TO_PREVIEW";
76 private static final String KEY_WAITING_FOR_BINDER
= "WAITING_FOR_BINDER";
78 private static final int INITIAL_HIDE_DELAY
= 0; // immediate hide
80 private ExtendedViewPager mViewPager
;
81 private PreviewImagePagerAdapter mPreviewImagePagerAdapter
;
82 private int mSavedPosition
= 0;
83 private boolean mHasSavedPosition
= false
;
85 private boolean mRequestWaitingForBinder
;
87 private DownloadFinishReceiver mDownloadFinishReceiver
;
89 private View mFullScreenAnchorView
;
93 protected void onCreate(Bundle savedInstanceState
) {
94 super.onCreate(savedInstanceState
);
96 requestWindowFeature(Window
.FEATURE_ACTION_BAR_OVERLAY
);
97 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
116 (new View
.OnSystemUiVisibilityChangeListener() {
117 @SuppressLint("InlinedApi")
119 public void onSystemUiVisibilityChange(int flags
) {
120 boolean visible
= (flags
& View
.SYSTEM_UI_FLAG_HIDE_NAVIGATION
) == 0;
121 ActionBar actionBar
= getSupportActionBar();
124 mDrawerLayout
.setDrawerLockMode(DrawerLayout
.LOCK_MODE_UNLOCKED
);
127 mDrawerLayout
.setDrawerLockMode(DrawerLayout
.LOCK_MODE_LOCKED_CLOSED
);
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,
145 getFile().getRemotePath().lastIndexOf(getFile().getFileName()));
146 OCFile parentFolder
= getStorageManager().getFileByPath(parentPath
);
147 if (parentFolder
== null
) {
148 // should not be necessary
149 parentFolder
= getStorageManager().getFileByPath(OCFile
.ROOT_PATH
);
152 // TODO Enable when "On Device" is recovered ?
153 mPreviewImagePagerAdapter
= new PreviewImagePagerAdapter(getSupportFragmentManager(),
154 parentFolder
, getAccount(), getStorageManager()/*, MainApp.getOnlyOnDevice()*/);
156 mViewPager
= (ExtendedViewPager
) findViewById(R
.id
.fragmentPager
);
157 int position
= mHasSavedPosition ? mSavedPosition
:
158 mPreviewImagePagerAdapter
.getFilePosition(getFile());
159 position
= (position
>= 0) ? position
: 0;
160 mViewPager
.setAdapter(mPreviewImagePagerAdapter
);
161 mViewPager
.setOnPageChangeListener(this);
162 mViewPager
.setCurrentItem(position
);
163 if (position
== 0 && !getFile().isDown()) {
164 // this is necessary because mViewPager.setCurrentItem(0) just after setting the
165 // adapter does not result in a call to #onPageSelected(0)
166 mRequestWaitingForBinder
= true
;
171 protected void onPostCreate(Bundle savedInstanceState
) {
172 super.onPostCreate(savedInstanceState
);
174 // Trigger the initial hide() shortly after the activity has been
175 // created, to briefly hint to the user that UI controls
177 delayedHide(INITIAL_HIDE_DELAY
);
181 Handler mHideSystemUiHandler
= new Handler() {
183 public void handleMessage(Message msg
) {
184 if (isHoneycombOrHigher()) {
185 hideSystemUI(mFullScreenAnchorView
);
187 getSupportActionBar().hide();
191 private void delayedHide(int delayMillis
) {
192 mHideSystemUiHandler
.removeMessages(0);
193 mHideSystemUiHandler
.sendEmptyMessageDelayed(0, delayMillis
);
197 /// handle Window Focus changes
199 public void onWindowFocusChanged(boolean hasFocus
) {
200 super.onWindowFocusChanged(hasFocus
);
202 // When the window loses focus (e.g. the action overflow is shown),
203 // cancel any pending hide action.
205 mHideSystemUiHandler
.removeMessages(0);
212 public void onStart() {
217 protected void onSaveInstanceState(Bundle outState
) {
218 super.onSaveInstanceState(outState
);
219 outState
.putBoolean(KEY_WAITING_FOR_BINDER
, mRequestWaitingForBinder
);
223 public void onRemoteOperationFinish(RemoteOperation operation
, RemoteOperationResult result
) {
224 super.onRemoteOperationFinish(operation
, result
);
226 if (operation
instanceof CreateShareOperation
) {
227 onCreateShareOperationFinish((CreateShareOperation
) operation
, result
);
229 } else if (operation
instanceof UnshareLinkOperation
) {
230 onUnshareLinkOperationFinish((UnshareLinkOperation
) operation
, result
);
232 } else if (operation
instanceof RemoveFileOperation
) {
238 private void onUnshareLinkOperationFinish(UnshareLinkOperation operation
,
239 RemoteOperationResult result
) {
240 if (result
.isSuccess()) {
241 OCFile file
= getStorageManager().getFileByPath(getFile().getRemotePath());
245 invalidateOptionsMenu();
246 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
247 backToDisplayActivity();
252 private void onCreateShareOperationFinish(CreateShareOperation operation
,
253 RemoteOperationResult result
) {
254 if (result
.isSuccess()) {
255 OCFile file
= getStorageManager().getFileByPath(getFile().getRemotePath());
259 invalidateOptionsMenu();
264 protected ServiceConnection
newTransferenceServiceConnection() {
265 return new PreviewImageServiceConnection();
268 /** Defines callbacks for service binding, passed to bindService() */
269 private class PreviewImageServiceConnection
implements ServiceConnection
{
272 public void onServiceConnected(ComponentName component
, IBinder service
) {
274 if (component
.equals(new ComponentName(PreviewImageActivity
.this,
275 FileDownloader
.class))) {
276 mDownloaderBinder
= (FileDownloaderBinder
) service
;
277 if (mRequestWaitingForBinder
) {
278 mRequestWaitingForBinder
= false
;
279 Log_OC
.d(TAG
, "Simulating reselection of current page after connection " +
280 "of download binder");
281 onPageSelected(mViewPager
.getCurrentItem());
284 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this,
285 FileUploader
.class))) {
286 Log_OC
.d(TAG
, "Upload service connected");
287 mUploaderBinder
= (FileUploaderBinder
) service
;
295 public void onServiceDisconnected(ComponentName component
) {
296 if (component
.equals(new ComponentName(PreviewImageActivity
.this,
297 FileDownloader
.class))) {
298 Log_OC
.d(TAG
, "Download service suddenly disconnected");
299 mDownloaderBinder
= null
;
300 } else if (component
.equals(new ComponentName(PreviewImageActivity
.this,
301 FileUploader
.class))) {
302 Log_OC
.d(TAG
, "Upload service suddenly disconnected");
303 mUploaderBinder
= null
;
310 public void onStop() {
316 public void onDestroy() {
321 public boolean onOptionsItemSelected(MenuItem item
) {
322 boolean returnValue
= false
;
324 switch(item
.getItemId()){
325 case android
.R
.id
.home
:
326 backToDisplayActivity();
330 returnValue
= super.onOptionsItemSelected(item
);
338 protected void onResume() {
341 mDownloadFinishReceiver
= new DownloadFinishReceiver();
343 IntentFilter filter
= new IntentFilter(FileDownloader
.getDownloadFinishMessage());
344 filter
.addAction(FileDownloader
.getDownloadAddedMessage());
345 registerReceiver(mDownloadFinishReceiver
, filter
);
349 protected void onPostResume() {
350 super.onPostResume();
354 public void onPause() {
355 if (mDownloadFinishReceiver
!= null
){
356 unregisterReceiver(mDownloadFinishReceiver
);
357 mDownloadFinishReceiver
= null
;
364 private void backToDisplayActivity() {
369 public void showDetails(OCFile file
) {
370 Intent showDetailsIntent
= new Intent(this, FileDisplayActivity
.class);
371 showDetailsIntent
.setAction(FileDisplayActivity
.ACTION_DETAILS
);
372 showDetailsIntent
.putExtra(FileActivity
.EXTRA_FILE
, file
);
373 showDetailsIntent
.putExtra(FileActivity
.EXTRA_ACCOUNT
,
374 AccountUtils
.getCurrentOwnCloudAccount(this));
375 startActivity(showDetailsIntent
);
376 int pos
= mPreviewImagePagerAdapter
.getFilePosition(file
);
377 file
= mPreviewImagePagerAdapter
.getFileAt(pos
);
382 private void requestForDownload(OCFile file
) {
383 if (mDownloaderBinder
== null
) {
384 Log_OC
.d(TAG
, "requestForDownload called without binder to download service");
386 } else if (!mDownloaderBinder
.isDownloading(getAccount(), file
)) {
387 Intent i
= new Intent(this, FileDownloader
.class);
388 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, getAccount());
389 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
395 * This method will be invoked when a new page becomes selected. Animation is not necessarily
398 * @param position Position index of the new selected page
401 public void onPageSelected(int position
) {
402 mSavedPosition
= position
;
403 mHasSavedPosition
= true
;
404 if (mDownloaderBinder
== null
) {
405 mRequestWaitingForBinder
= true
;
408 OCFile currentFile
= mPreviewImagePagerAdapter
.getFileAt(position
);
409 getSupportActionBar().setTitle(currentFile
.getFileName());
410 mDrawerToggle
.setDrawerIndicatorEnabled(false
);
411 if (!currentFile
.isDown()) {
412 if (!mPreviewImagePagerAdapter
.pendingErrorAt(position
)) {
413 requestForDownload(currentFile
);
417 // Call to reset image zoom to initial state
418 ((PreviewImagePagerAdapter
) mViewPager
.getAdapter()).resetZoom();
424 * Called when the scroll state changes. Useful for discovering when the user begins dragging,
425 * when the pager is automatically settling to the current page. when it is fully stopped/idle.
427 * @param state The new scroll state (SCROLL_STATE_IDLE, _DRAGGING, _SETTLING
430 public void onPageScrollStateChanged(int state
) {
434 * This method will be invoked when the current page is scrolled, either as part of a
435 * programmatically initiated smooth scroll or a user initiated touch scroll.
437 * @param position Position index of the first page currently being displayed.
438 * Page position+1 will be visible if positionOffset is
441 * @param positionOffset Value from [0, 1) indicating the offset from the page
443 * @param positionOffsetPixels Value in pixels indicating the offset from position.
446 public void onPageScrolled(int position
, float positionOffset
, int positionOffsetPixels
) {
451 * Class waiting for broadcast events from the {@link FileDownloader} service.
453 * Updates the UI when a download is started or finished, provided that it is relevant for the
454 * folder displayed in the gallery.
456 private class DownloadFinishReceiver
extends BroadcastReceiver
{
458 public void onReceive(Context context
, Intent intent
) {
459 String accountName
= intent
.getStringExtra(FileDownloader
.ACCOUNT_NAME
);
460 String downloadedRemotePath
= intent
.getStringExtra(FileDownloader
.EXTRA_REMOTE_PATH
);
461 if (getAccount().name
.equals(accountName
) &&
462 downloadedRemotePath
!= null
) {
464 OCFile file
= getStorageManager().getFileByPath(downloadedRemotePath
);
465 int position
= mPreviewImagePagerAdapter
.getFilePosition(file
);
466 boolean downloadWasFine
= intent
.getBooleanExtra(
467 FileDownloader
.EXTRA_DOWNLOAD_RESULT
, false
);
468 //boolean isOffscreen = Math.abs((mViewPager.getCurrentItem() - position))
469 // <= mViewPager.getOffscreenPageLimit();
472 intent
.getAction().equals(FileDownloader
.getDownloadFinishMessage())) {
473 if (downloadWasFine
) {
474 mPreviewImagePagerAdapter
.updateFile(position
, file
);
477 mPreviewImagePagerAdapter
.updateWithDownloadError(position
);
479 mPreviewImagePagerAdapter
.notifyDataSetChanged(); // will trigger the creation
483 Log_OC
.d(TAG
, "Download finished, but the fragment is offscreen");
487 removeStickyBroadcast(intent
);
492 @SuppressLint("InlinedApi")
493 public void toggleFullScreen() {
495 if (isHoneycombOrHigher()) {
497 boolean visible
= (mFullScreenAnchorView
.getSystemUiVisibility()
498 & View
.SYSTEM_UI_FLAG_HIDE_NAVIGATION
) == 0;
501 hideSystemUI(mFullScreenAnchorView
);
502 mDrawerLayout
.setDrawerLockMode(DrawerLayout
.LOCK_MODE_LOCKED_CLOSED
);
503 // actionBar.hide(); // propagated through
504 // OnSystemUiVisibilityChangeListener()
506 showSystemUI(mFullScreenAnchorView
);
507 // actionBar.show(); // propagated through
508 // OnSystemUiVisibilityChangeListener()
513 ActionBar actionBar
= getSupportActionBar();
514 if (!actionBar
.isShowing()) {
516 mDrawerLayout
.setDrawerLockMode(DrawerLayout
.LOCK_MODE_UNLOCKED
);
520 mDrawerLayout
.setDrawerLockMode(DrawerLayout
.LOCK_MODE_LOCKED_CLOSED
);
528 protected void onAccountSet(boolean stateWasRecovered
) {
529 super.onAccountSet(stateWasRecovered
);
530 if (getAccount() != null
) {
531 OCFile file
= getFile();
532 /// Validate handled file (first image to preview)
534 throw new IllegalStateException("Instanced with a NULL OCFile");
536 if (!file
.isImage()) {
537 throw new IllegalArgumentException("Non-image file passed as argument");
540 // Update file according to DB file, if it is possible
541 if (file
.getFileId() > FileDataStorageManager
.ROOT_PARENT_ID
)
542 file
= getStorageManager().getFileById(file
.getFileId());
545 /// Refresh the activity according to the Account and OCFile set
546 setFile(file
); // reset after getting it fresh from storageManager
547 getSupportActionBar().setTitle(getFile().getFileName());
548 //if (!stateWasRecovered) {
553 // handled file not in the current Account
560 public void onBrowsedDownTo(OCFile folder
) {
561 // TODO Auto-generated method stub
566 public void onTransferStateChanged(OCFile file
, boolean downloading
, boolean uploading
) {
567 // TODO Auto-generated method stub
572 @SuppressLint("InlinedApi")
573 private void hideSystemUI(View anchorView
) {
574 anchorView
.setSystemUiVisibility(
575 View
.SYSTEM_UI_FLAG_HIDE_NAVIGATION
// hides NAVIGATION BAR; Android >= 4.0
576 | View
.SYSTEM_UI_FLAG_FULLSCREEN
// hides STATUS BAR; Android >= 4.1
577 | View
.SYSTEM_UI_FLAG_IMMERSIVE
// stays interactive; Android >= 4.4
578 | View
.SYSTEM_UI_FLAG_LAYOUT_STABLE
// draw full window; Android >= 4.1
579 | View
.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// draw full window; Android >= 4.1
580 | View
.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
// draw full window; Android >= 4.1
584 @SuppressLint("InlinedApi")
585 private void showSystemUI(View anchorView
) {
586 anchorView
.setSystemUiVisibility(
587 View
.SYSTEM_UI_FLAG_LAYOUT_STABLE
// draw full window; Android >= 4.1
588 | View
.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// draw full window; Android >= 4.1
589 | View
.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
// draw full window; Android >= 4.1
594 * Checks if OS version is Honeycomb one or higher
598 private boolean isHoneycombOrHigher() {
599 if (Build
.VERSION
.SDK_INT
>= Build
.VERSION_CODES
.HONEYCOMB
) {
606 public void allFilesOption(){
607 backToDisplayActivity();
608 super.allFilesOption();