Skip to content

Commit

Permalink
Fix & enable HW rotation on notched devices on P+ (#288)
Browse files Browse the repository at this point in the history
By updating the current handling to be rotation-aware.
  • Loading branch information
NiLuJe authored Jan 13, 2021
1 parent 1884a15 commit e42e173
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
29 changes: 25 additions & 4 deletions app/src/main/java/org/koreader/launcher/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,24 @@ class MainActivity : NativeActivity(), JNILuaInterface,
return ScreenUtils.getScreenAvailableHeight(this)
}

override fun getScreenAvailableWidth(): Int {
return ScreenUtils.getScreenAvailableWidth(this)
}

override fun getScreenBrightness(): Int {
return device.getScreenBrightness(this)
}

override fun getScreenHeight(): Int {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ScreenUtils.getScreenHeight(this) - topInsetHeight
// We need to handle the notch in Portrait
// NOTE: getScreenAvailableHeight does it automatically, but it also excludes the nav bar, when there's one :/
if (device.getScreenOrientation(this).and(1) == 0) {
// getScreenOrientation returns LinuxFB rotation constants, Portrait rotations are always even
ScreenUtils.getScreenHeight(this) - topInsetHeight
} else {
ScreenUtils.getScreenHeight(this)
}
} else {
ScreenUtils.getScreenHeight(this)
}
Expand Down Expand Up @@ -355,7 +366,18 @@ class MainActivity : NativeActivity(), JNILuaInterface,
}

override fun getScreenWidth(): Int {
return ScreenUtils.getScreenWidth(this)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// We need to handle the notch in Landscape
// NOTE: getScreenAvailableWidth does it automatically, but it also excludes the nav bar, when there's one :/
if (device.getScreenOrientation(this).and(1) == 1) {
// getScreenOrientation returns LinuxFB rotation constants, Landscape rotations are always odd
ScreenUtils.getScreenWidth(this) - topInsetHeight
} else {
ScreenUtils.getScreenWidth(this)
}
} else {
ScreenUtils.getScreenWidth(this)
}
}

override fun getStatusBarHeight(): Int {
Expand All @@ -377,8 +399,7 @@ class MainActivity : NativeActivity(), JNILuaInterface,
override fun hasNativeRotation(): Boolean {
return if (device.platform == "android") {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// FIXME: hw rotation is disabled in devices with a Notch.
!((topInsetHeight > 0) || (device.bugRotation))
!(device.bugRotation)
} else false
} else false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface JNILuaInterface {
fun getPlatformName(): String
fun getProduct(): String
fun getScreenAvailableHeight(): Int
fun getScreenAvailableWidth(): Int
fun getScreenBrightness(): Int
fun getScreenHeight(): Int
fun getScreenMaxBrightness(): Int
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/org/koreader/launcher/utils/ScreenUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import java.util.concurrent.CountDownLatch
object ScreenUtils {
private const val TAG = "ScreenUtils"

fun getScreenAvailableWidth(activity: Activity): Int {
return getScreenSizeWithConstraints(activity).x
}

fun getScreenAvailableHeight(activity: Activity): Int {
return getScreenSizeWithConstraints(activity).y
}
Expand Down
10 changes: 10 additions & 0 deletions assets/android.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,16 @@ local function run(android_app_state)
end)
end

android.getScreenAvailableWidth = function()
return JNI:context(android.app.activity.vm, function(jni)
return jni:callIntMethod(
android.app.activity.clazz,
"getScreenAvailableWidth",
"()I"
)
end)
end

android.getScreenAvailableHeight = function()
return JNI:context(android.app.activity.vm, function(jni)
return jni:callIntMethod(
Expand Down

0 comments on commit e42e173

Please sign in to comment.