From 07585ce8ae1939f424ac9a15ba16eb8e2d65d5f6 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 1 Jul 2018 17:50:42 +0200 Subject: [PATCH] make Lock Orientation more advanced, toggle between portrait/landscape/auto --- .../gallery/activities/PhotoVideoActivity.kt | 2 +- .../gallery/activities/ViewPagerActivity.kt | 43 +++++++++++-------- app/src/main/res/layout/bottom_actions.xml | 6 +-- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt index c6ccf3a6c..17201aee8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt @@ -178,7 +178,7 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList } private fun initBottomActionButtons() { - arrayListOf(bottom_favorite, bottom_delete, bottom_rotate, bottom_properties, bottom_lock_orientation, bottom_slideshow, bottom_show_on_map, bottom_toggle_file_visibility).forEach { + arrayListOf(bottom_favorite, bottom_delete, bottom_rotate, bottom_properties, bottom_change_orientation, bottom_slideshow, bottom_show_on_map, bottom_toggle_file_visibility).forEach { it.beGone() } 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 36a35fe4f..32a672fc8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -13,7 +13,6 @@ import android.graphics.BitmapFactory import android.graphics.Color import android.graphics.Matrix import android.graphics.drawable.ColorDrawable -import android.graphics.drawable.Drawable import android.media.ExifInterface import android.net.Uri import android.os.Build @@ -268,14 +267,12 @@ 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 + if (!mIsOrientationLocked) { + if (config.screenRotation == ROTATE_BY_DEVICE_ROTATION) { + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR + } else if (config.screenRotation == ROTATE_BY_SYSTEM_SETTING) { + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED } - } else if (config.screenRotation == ROTATE_BY_DEVICE_ROTATION) { - requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR - } else if (config.screenRotation == ROTATE_BY_SYSTEM_SETTING) { - requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED } } @@ -301,7 +298,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View findItem(R.id.menu_remove_from_favorites).isVisible = currentMedium.isFavorite && visibleBottomActions and BOTTOM_ACTION_TOGGLE_FAVORITE == 0 findItem(R.id.menu_restore_file).isVisible = currentMedium.path.startsWith(filesDir.toString()) findItem(R.id.menu_change_orientation).isVisible = mRotationDegrees == 0 && visibleBottomActions and BOTTOM_ACTION_CHANGE_ORIENTATION == 0 - findItem(R.id.menu_change_orientation).icon = getChangeOrientationIcon() + findItem(R.id.menu_change_orientation).icon = resources.getDrawable(getChangeOrientationIcon()) findItem(R.id.menu_rotate).setShowAsAction( if (mRotationDegrees != 0) { MenuItem.SHOW_AS_ACTION_ALWAYS @@ -549,12 +546,12 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun toggleOrientation(orientation: Int) { requestedOrientation = orientation - mIsOrientationLocked = orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + mIsOrientationLocked = orientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED invalidateOptionsMenu() } - private fun getChangeOrientationIcon(): Drawable { - val drawable = if (mIsOrientationLocked) { + private fun getChangeOrientationIcon(): Int { + return if (mIsOrientationLocked) { if (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { R.drawable.ic_orientation_portrait } else { @@ -563,7 +560,6 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } else { R.drawable.ic_orientation_auto } - return resources.getDrawable(drawable) } private fun saveImageAs() { @@ -819,10 +815,16 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } } - bottom_lock_orientation.beVisibleIf(visibleBottomActions and BOTTOM_ACTION_CHANGE_ORIENTATION != 0) - bottom_lock_orientation.setOnClickListener { + bottom_change_orientation.beVisibleIf(visibleBottomActions and BOTTOM_ACTION_CHANGE_ORIENTATION != 0) + bottom_change_orientation.setOnClickListener { if (bottom_actions.alpha == 1f) { - + requestedOrientation = when (requestedOrientation) { + ActivityInfo.SCREEN_ORIENTATION_PORTRAIT -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE -> ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED + else -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT + } + mIsOrientationLocked = requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED + updateBottomActionIcons(getCurrentMedium()) } } @@ -845,7 +847,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View if (bottom_actions.alpha == 1f) { getCurrentMedium()?.apply { toggleFileVisibility(!isHidden()) { - updateBottomActionIcons(getCurrentMedium()!!) + updateBottomActionIcons(getCurrentMedium()) } } } @@ -859,7 +861,11 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } } - private fun updateBottomActionIcons(medium: Medium) { + private fun updateBottomActionIcons(medium: Medium?) { + if (medium == null) { + return + } + val favoriteIcon = if (medium.isFavorite) R.drawable.ic_star_on else R.drawable.ic_star_off bottom_favorite.setImageResource(favoriteIcon) @@ -867,6 +873,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View bottom_toggle_file_visibility.setImageResource(hideIcon) bottom_rotate.beVisibleIf(config.visibleBottomActions and BOTTOM_ACTION_ROTATE != 0 && getCurrentMedium()?.isImage() == true) + bottom_change_orientation.setImageResource(getChangeOrientationIcon()) } private fun toggleFavorite() { diff --git a/app/src/main/res/layout/bottom_actions.xml b/app/src/main/res/layout/bottom_actions.xml index 0e69d8aeb..b2c1e79bd 100644 --- a/app/src/main/res/layout/bottom_actions.xml +++ b/app/src/main/res/layout/bottom_actions.xml @@ -71,12 +71,12 @@ android:layout_height="wrap_content" android:padding="@dimen/medium_margin" android:src="@drawable/ic_properties" - app:layout_constraintEnd_toStartOf="@+id/bottom_lock_orientation" + app:layout_constraintEnd_toStartOf="@+id/bottom_change_orientation" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toEndOf="@+id/bottom_rotate"/> + app:layout_constraintStart_toEndOf="@+id/bottom_change_orientation"/>