-
-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathContext.kt
More file actions
54 lines (45 loc) · 1.61 KB
/
Context.kt
File metadata and controls
54 lines (45 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.reactnativekeyboardcontroller.extensions
import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.content.res.Configuration
import android.graphics.Point
import android.os.Build
import android.provider.Settings
import android.view.Display
import android.view.WindowManager
import android.view.WindowMetrics
@SuppressLint("ObsoleteSdkInt")
@Suppress("DEPRECATION")
fun Context.getDisplaySize(): Point {
val size = Point()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// For API level 30 (Android 11) and above
val windowManager = getSystemService(WindowManager::class.java)
val windowMetrics: WindowMetrics = windowManager.currentWindowMetrics
val bounds = windowMetrics.bounds
size.x = bounds.width()
size.y = bounds.height()
} else {
val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
val display: Display = windowManager.defaultDisplay
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
display.getRealSize(size) // API level 17 and above
} else {
// Fallback for API level < 17
size.x = display.width
size.y = display.height
}
}
return size
}
fun Context.isSystemDarkMode(): Boolean =
resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
fun Context.currentImePackage(): String? {
val id =
Settings.Secure.getString(
contentResolver,
Settings.Secure.DEFAULT_INPUT_METHOD,
) ?: return null
return ComponentName.unflattenFromString(id)?.packageName
}