refactor initFlutterChannel
This commit is contained in:
parent
60ab29ad6e
commit
836249d34c
@ -13,8 +13,6 @@ import android.content.Intent
|
|||||||
import android.content.ServiceConnection
|
import android.content.ServiceConnection
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.preference.PreferenceManager
|
|
||||||
import android.provider.Settings
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
@ -44,9 +42,59 @@ class MainActivity : FlutterActivity() {
|
|||||||
flutterMethodChannel = MethodChannel(
|
flutterMethodChannel = MethodChannel(
|
||||||
flutterEngine.dartExecutor.binaryMessenger,
|
flutterEngine.dartExecutor.binaryMessenger,
|
||||||
channelTag
|
channelTag
|
||||||
).apply {
|
)
|
||||||
// make sure result is set, otherwise flutter will await forever
|
initFlutterChannel(flutterMethodChannel)
|
||||||
setMethodCallHandler { call, result ->
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
val inputPer = InputService.isOpen
|
||||||
|
activity.runOnUiThread {
|
||||||
|
flutterMethodChannel.invokeMethod(
|
||||||
|
"on_state_changed",
|
||||||
|
mapOf("name" to "input", "value" to inputPer.toString())
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun requestMediaProjection() {
|
||||||
|
val intent = Intent(this, PermissionRequestTransparentActivity::class.java).apply {
|
||||||
|
action = ACT_REQUEST_MEDIA_PROJECTION
|
||||||
|
}
|
||||||
|
startActivityForResult(intent, REQ_INVOKE_PERMISSION_ACTIVITY_MEDIA_PROJECTION)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
|
if (requestCode == REQ_INVOKE_PERMISSION_ACTIVITY_MEDIA_PROJECTION && resultCode == RES_FAILED) {
|
||||||
|
flutterMethodChannel.invokeMethod("on_media_projection_canceled", null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
Log.e(logTag, "onDestroy")
|
||||||
|
mainService?.let {
|
||||||
|
unbindService(serviceConnection)
|
||||||
|
}
|
||||||
|
super.onDestroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
private val serviceConnection = object : ServiceConnection {
|
||||||
|
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
||||||
|
Log.d(logTag, "onServiceConnected")
|
||||||
|
val binder = service as MainService.LocalBinder
|
||||||
|
mainService = binder.getService()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onServiceDisconnected(name: ComponentName?) {
|
||||||
|
Log.d(logTag, "onServiceDisconnected")
|
||||||
|
mainService = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun initFlutterChannel(flutterMethodChannel: MethodChannel) {
|
||||||
|
flutterMethodChannel.setMethodCallHandler { call, result ->
|
||||||
|
// make sure result will be invoked, otherwise flutter will await forever
|
||||||
when (call.method) {
|
when (call.method) {
|
||||||
"init_service" -> {
|
"init_service" -> {
|
||||||
Intent(activity, MainService::class.java).also {
|
Intent(activity, MainService::class.java).also {
|
||||||
@ -106,11 +154,11 @@ class MainActivity : FlutterActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
"check_service" -> {
|
"check_service" -> {
|
||||||
flutterMethodChannel.invokeMethod(
|
Companion.flutterMethodChannel.invokeMethod(
|
||||||
"on_state_changed",
|
"on_state_changed",
|
||||||
mapOf("name" to "input", "value" to InputService.isOpen.toString())
|
mapOf("name" to "input", "value" to InputService.isOpen.toString())
|
||||||
)
|
)
|
||||||
flutterMethodChannel.invokeMethod(
|
Companion.flutterMethodChannel.invokeMethod(
|
||||||
"on_state_changed",
|
"on_state_changed",
|
||||||
mapOf("name" to "media", "value" to MainService.isReady.toString())
|
mapOf("name" to "media", "value" to MainService.isReady.toString())
|
||||||
)
|
)
|
||||||
@ -121,38 +169,35 @@ class MainActivity : FlutterActivity() {
|
|||||||
InputService.ctx?.disableSelf()
|
InputService.ctx?.disableSelf()
|
||||||
}
|
}
|
||||||
InputService.ctx = null
|
InputService.ctx = null
|
||||||
flutterMethodChannel.invokeMethod(
|
Companion.flutterMethodChannel.invokeMethod(
|
||||||
"on_state_changed",
|
"on_state_changed",
|
||||||
mapOf("name" to "input", "value" to InputService.isOpen.toString())
|
mapOf("name" to "input", "value" to InputService.isOpen.toString())
|
||||||
)
|
)
|
||||||
result.success(true)
|
result.success(true)
|
||||||
}
|
}
|
||||||
"cancel_notification" -> {
|
"cancel_notification" -> {
|
||||||
try {
|
if (call.arguments is Int) {
|
||||||
val id = call.arguments as Int
|
val id = call.arguments as Int
|
||||||
mainService?.cancelNotification(id)
|
mainService?.cancelNotification(id)
|
||||||
} finally {
|
} else {
|
||||||
result.success(true)
|
result.success(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"enable_soft_keyboard" -> {
|
"enable_soft_keyboard" -> {
|
||||||
// https://blog.csdn.net/hanye2020/article/details/105553780
|
// https://blog.csdn.net/hanye2020/article/details/105553780
|
||||||
try {
|
|
||||||
if (call.arguments as Boolean) {
|
if (call.arguments as Boolean) {
|
||||||
window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
|
window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
|
||||||
} else {
|
} else {
|
||||||
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
|
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
result.success(true)
|
result.success(true)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
GET_START_ON_BOOT_OPT -> {
|
GET_START_ON_BOOT_OPT -> {
|
||||||
val prefs = getSharedPreferences(KEY_SHARED_PREFERENCES, MODE_PRIVATE)
|
val prefs = getSharedPreferences(KEY_SHARED_PREFERENCES, MODE_PRIVATE)
|
||||||
result.success(prefs.getBoolean(KEY_START_ON_BOOT_OPT, false))
|
result.success(prefs.getBoolean(KEY_START_ON_BOOT_OPT, false))
|
||||||
}
|
}
|
||||||
SET_START_ON_BOOT_OPT -> {
|
SET_START_ON_BOOT_OPT -> {
|
||||||
try {
|
|
||||||
if (call.arguments is Boolean) {
|
if (call.arguments is Boolean) {
|
||||||
val prefs = getSharedPreferences(KEY_SHARED_PREFERENCES, MODE_PRIVATE)
|
val prefs = getSharedPreferences(KEY_SHARED_PREFERENCES, MODE_PRIVATE)
|
||||||
val edit = prefs.edit()
|
val edit = prefs.edit()
|
||||||
@ -162,9 +207,6 @@ class MainActivity : FlutterActivity() {
|
|||||||
} else {
|
} else {
|
||||||
result.success(false)
|
result.success(false)
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
result.success(false)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
result.error("-1", "No such method", null)
|
result.error("-1", "No such method", null)
|
||||||
@ -173,50 +215,3 @@ class MainActivity : FlutterActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
|
||||||
super.onResume()
|
|
||||||
val inputPer = InputService.isOpen
|
|
||||||
activity.runOnUiThread {
|
|
||||||
flutterMethodChannel.invokeMethod(
|
|
||||||
"on_state_changed",
|
|
||||||
mapOf("name" to "input", "value" to inputPer.toString())
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun requestMediaProjection() {
|
|
||||||
val intent = Intent(this, PermissionRequestTransparentActivity::class.java).apply {
|
|
||||||
action = ACT_REQUEST_MEDIA_PROJECTION
|
|
||||||
}
|
|
||||||
startActivityForResult(intent, REQ_INVOKE_PERMISSION_ACTIVITY_MEDIA_PROJECTION)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
||||||
super.onActivityResult(requestCode, resultCode, data)
|
|
||||||
if (requestCode == REQ_INVOKE_PERMISSION_ACTIVITY_MEDIA_PROJECTION && resultCode == RES_FAILED) {
|
|
||||||
flutterMethodChannel.invokeMethod("on_media_projection_canceled", null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDestroy() {
|
|
||||||
Log.e(logTag, "onDestroy")
|
|
||||||
mainService?.let {
|
|
||||||
unbindService(serviceConnection)
|
|
||||||
}
|
|
||||||
super.onDestroy()
|
|
||||||
}
|
|
||||||
|
|
||||||
private val serviceConnection = object : ServiceConnection {
|
|
||||||
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
|
||||||
Log.d(logTag, "onServiceConnected")
|
|
||||||
val binder = service as MainService.LocalBinder
|
|
||||||
mainService = binder.getService()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onServiceDisconnected(name: ComponentName?) {
|
|
||||||
Log.d(logTag, "onServiceDisconnected")
|
|
||||||
mainService = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -53,7 +53,6 @@ data class Info(
|
|||||||
var width: Int, var height: Int, var scale: Int, var dpi: Int
|
var width: Int, var height: Int, var scale: Int, var dpi: Int
|
||||||
)
|
)
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.M)
|
|
||||||
fun requestPermission(context: Context, type: String) {
|
fun requestPermission(context: Context, type: String) {
|
||||||
XXPermissions.with(context)
|
XXPermissions.with(context)
|
||||||
.permission(type)
|
.permission(type)
|
||||||
@ -69,7 +68,6 @@ fun requestPermission(context: Context, type: String) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.M)
|
|
||||||
fun startAction(context: Context, action: String) {
|
fun startAction(context: Context, action: String) {
|
||||||
try {
|
try {
|
||||||
context.startActivity(Intent(action).apply {
|
context.startActivity(Intent(action).apply {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user