-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRNASModule.kt
61 lines (49 loc) · 1.9 KB
/
RNASModule.kt
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
55
56
57
58
59
60
61
package tech.bam.rnas
import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition
import android.content.Context
import android.view.inputmethod.InputMethodManager
import android.provider.Settings
class RNASModule : Module() {
override fun definition() = ModuleDefinition {
Name("RNAS")
Function("showInputMethodPicker") {
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showInputMethodPicker()
}
Function("getCurrentInputMethodInfo") {
val currentKeyboardId =
Settings.Secure.getString(context.contentResolver, Settings.Secure.DEFAULT_INPUT_METHOD)
val isInDefaultSafeList = isKeyboardSafe(currentKeyboardId)
return@Function mapOf(
"isInDefaultSafeList" to isInDefaultSafeList,
"inputMethodId" to currentKeyboardId.substringBefore('/')
)
}
}
private val context
get() = requireNotNull(appContext.reactContext)
}
val defaultAllowedKeyboardPackagesList = arrayOf("com.touchtype.swiftkey", "com.samsung.android", "com.google.android", "com.sec.android.inputmethod")
/**
* Check whether the package name provided as `input` matches any of the allowed packages
* Examples:
* Input: com.myPackage.android.latin/id
* Array: ['com.myPackage.android']
* Output: true
*
* Input: android.com.myPackage.android.latin/id
* Array: ['com.myPackage.android']
* Output: false
*
* Input: com.randomPackage/com.myPackage.android.latin
* Array: ['com.myPackage.android']
* Output: false
*/
fun doesPackageNameMatch(input: String, allowedPackagesList: Array<String>): Boolean {
val packageName = input.substringBefore('/')
return allowedPackagesList.any { packageName.matches("${Regex.escape(it)}.*".toRegex()) }
}
fun isKeyboardSafe(keyboardID: String): Boolean {
return doesPackageNameMatch(keyboardID, defaultAllowedKeyboardPackagesList)
}