Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: use windowInsets for input method detection
  • Loading branch information
jinliu9508 committed Apr 24, 2025
commit 3c7325076e6e9c1f2a079c91a88134c2d59d15e5
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
import android.content.Context
import android.graphics.Rect
import android.net.ConnectivityManager
import android.os.Build
import android.telephony.TelephonyManager
import android.util.DisplayMetrics
import android.view.View
import androidx.core.view.WindowInsetsCompat
import java.lang.ref.WeakReference

object DeviceUtils {
private val MARGIN_ERROR_PX_SIZE = ViewUtils.dpToPx(24)

/**
* Check if the keyboard is currently being shown.
* Does not work for cases when keyboard is full screen.
*/
fun isKeyboardUp(activityWeakReference: WeakReference<Activity?>): Boolean {
val metrics = DisplayMetrics()
Expand All @@ -25,11 +26,18 @@
val window = activityWeakReference.get()!!.window
view = window.decorView
view.getWindowVisibleDisplayFrame(visibleBounds)
window.windowManager.defaultDisplay.getMetrics(metrics)

Check warning on line 29 in OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/DeviceUtils.kt

View workflow job for this annotation

GitHub Actions / build

'getter for defaultDisplay: Display!' is deprecated. Deprecated in Java

Check warning on line 29 in OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/DeviceUtils.kt

View workflow job for this annotation

GitHub Actions / build

'getMetrics(DisplayMetrics!): Unit' is deprecated. Deprecated in Java
}
if (view != null) {
val heightDiff = metrics.heightPixels - visibleBounds.bottom
isOpen = heightDiff > MARGIN_ERROR_PX_SIZE
isOpen =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val imeInsets = view.rootWindowInsets
imeInsets.isVisible(WindowInsetsCompat.Type.ime())
} else {
// Does not work for cases when keyboard is full screen for Android 9 and below
val heightDiff = metrics.heightPixels - visibleBounds.bottom
heightDiff > MARGIN_ERROR_PX_SIZE
}
}
return isOpen
}
Expand All @@ -37,7 +45,7 @@
fun getNetType(appContext: Context): Int? {
val cm =
appContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val netInfo = cm.activeNetworkInfo

Check warning on line 48 in OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/DeviceUtils.kt

View workflow job for this annotation

GitHub Actions / build

'getter for activeNetworkInfo: NetworkInfo?' is deprecated. Deprecated in Java
if (netInfo != null) {
val networkType = netInfo.type
return if (networkType == ConnectivityManager.TYPE_WIFI || networkType == ConnectivityManager.TYPE_ETHERNET) 0 else 1
Expand Down
Loading