From b41f1e97e64d48cc103aeb3557669fde7e779fcf Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 8 Nov 2017 21:10:43 +0100 Subject: [PATCH] add a button at fullscreen media for locking screen orientation --- .../gallery/activities/ViewPagerActivity.kt | 44 ++++++++++++++----- app/src/main/res/menu/menu_viewpager.xml | 10 +++-- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index 3d6f319e0..6abee7975 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -66,6 +66,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private var mSlideshowMoveBackwards = false private var mSlideshowMedia = mutableListOf() private var mAreSlideShowMediaVisible = false + private var mIsOrientationLocked = false private var mStoredUseEnglish = false @@ -110,12 +111,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View window.attributes = attributes } - if (config.screenRotation == ROTATE_BY_DEVICE_ROTATION && mOrientationEventListener?.canDetectOrientation() == true) { - mOrientationEventListener?.enable() - } else if (config.screenRotation == ROTATE_BY_SYSTEM_SETTING) { - requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED - } - + setupRotation() invalidateOptionsMenu() } @@ -214,6 +210,18 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } } + private fun setupRotation() { + if (mIsOrientationLocked) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED + } + } else if (config.screenRotation == ROTATE_BY_DEVICE_ROTATION && mOrientationEventListener?.canDetectOrientation() == true) { + mOrientationEventListener?.enable() + } else if (config.screenRotation == ROTATE_BY_SYSTEM_SETTING) { + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED + } + } + private fun setupOrientationEventListener() { mOrientationEventListener = object : OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { override fun onOrientationChanged(orientation: Int) { @@ -223,7 +231,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View else -> ORIENT_PORTRAIT } - if (mLastHandledOrientation != currOrient) { + if (!mIsOrientationLocked && mLastHandledOrientation != currOrient) { mLastHandledOrientation = currOrient requestedOrientation = when (currOrient) { @@ -247,6 +255,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View findItem(R.id.menu_save_as).isVisible = mRotationDegrees != 0f findItem(R.id.menu_hide).isVisible = !currentMedium.name.startsWith('.') findItem(R.id.menu_unhide).isVisible = currentMedium.name.startsWith('.') + findItem(R.id.menu_lock_orientation).title = getString(if (mIsOrientationLocked) R.string.unlock_orientation else R.string.lock_orientation) findItem(R.id.menu_rotate).setShowAsAction( if (mRotationDegrees != 0f) { MenuItem.SHOW_AS_ACTION_ALWAYS @@ -264,7 +273,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View when (item.itemId) { R.id.menu_set_as -> setAs(Uri.fromFile(getCurrentFile())) - R.id.slideshow -> initSlideshow() + R.id.menu_slideshow -> initSlideshow() R.id.menu_copy_to -> copyMoveTo(true) R.id.menu_move_to -> copyMoveTo(false) R.id.menu_open_with -> openFile(Uri.fromFile(getCurrentFile()), true) @@ -276,10 +285,11 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View R.id.menu_rename -> renameFile() R.id.menu_edit -> openEditor(Uri.fromFile(getCurrentFile())) R.id.menu_properties -> showProperties() - R.id.show_on_map -> showOnMap() + R.id.menu_show_on_map -> showOnMap() R.id.menu_rotate -> rotateImage() + R.id.menu_lock_orientation -> toggleLockOrientation() R.id.menu_save_as -> saveImageAs() - R.id.settings -> launchSettings() + R.id.menu_settings -> launchSettings() else -> return super.onOptionsItemSelected(item) } return true @@ -467,6 +477,18 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View supportInvalidateOptionsMenu() } + private fun toggleLockOrientation() { + mIsOrientationLocked = !mIsOrientationLocked + if (mIsOrientationLocked) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED + } + } else { + setupRotation() + } + invalidateOptionsMenu() + } + private fun saveImageAs() { val currPath = getCurrentPath() SaveAsDialog(this, currPath, false) { @@ -756,7 +778,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun checkOrientation() { - if (config.screenRotation == ROTATE_BY_ASPECT_RATIO) { + if (!mIsOrientationLocked && config.screenRotation == ROTATE_BY_ASPECT_RATIO) { val res = getCurrentFile().getResolution() if (res.x > res.y) { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE diff --git a/app/src/main/res/menu/menu_viewpager.xml b/app/src/main/res/menu/menu_viewpager.xml index 192458087..f22a0afb3 100644 --- a/app/src/main/res/menu/menu_viewpager.xml +++ b/app/src/main/res/menu/menu_viewpager.xml @@ -29,7 +29,11 @@ android:visible="false" app:showAsAction="ifRoom"/> +