1. fix check boot on start opt.

2. fix late var flutterMethodChannel
This commit is contained in:
csf
2023-02-28 22:26:47 +09:00
parent 836249d34c
commit 660d6ff230
5 changed files with 39 additions and 21 deletions

View File

@@ -1,11 +1,15 @@
package com.carriez.flutter_hbb
import android.Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
import android.Manifest.permission.SYSTEM_ALERT_WINDOW
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import android.widget.Toast
import com.hjq.permissions.XXPermissions
import io.flutter.embedding.android.FlutterActivity
const val DEBUG_BOOT_COMPLETED = "com.carriez.flutter_hbb.DEBUG_BOOT_COMPLETED"
@@ -16,6 +20,18 @@ class BootReceiver : BroadcastReceiver() {
Log.d(logTag, "onReceive ${intent.action}")
if (Intent.ACTION_BOOT_COMPLETED == intent.action || DEBUG_BOOT_COMPLETED == intent.action) {
// check SharedPreferences config
val prefs = context.getSharedPreferences(KEY_SHARED_PREFERENCES, FlutterActivity.MODE_PRIVATE)
if (!prefs.getBoolean(KEY_START_ON_BOOT_OPT, false)) {
Log.d(logTag, "KEY_START_ON_BOOT_OPT is false")
return
}
// check pre-permission
if (!XXPermissions.isGranted(context, REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, SYSTEM_ALERT_WINDOW)){
Log.d(logTag, "REQUEST_IGNORE_BATTERY_OPTIMIZATIONS or SYSTEM_ALERT_WINDOW is not granted")
return
}
val it = Intent(context, MainService::class.java).apply {
action = ACT_INIT_MEDIA_PROJECTION_AND_SERVICE
}

View File

@@ -24,7 +24,7 @@ import io.flutter.plugin.common.MethodChannel
class MainActivity : FlutterActivity() {
companion object {
lateinit var flutterMethodChannel: MethodChannel
var flutterMethodChannel: MethodChannel? = null
}
private val channelTag = "mChannel"
@@ -43,14 +43,14 @@ class MainActivity : FlutterActivity() {
flutterEngine.dartExecutor.binaryMessenger,
channelTag
)
initFlutterChannel(flutterMethodChannel)
initFlutterChannel(flutterMethodChannel!!)
}
override fun onResume() {
super.onResume()
val inputPer = InputService.isOpen
activity.runOnUiThread {
flutterMethodChannel.invokeMethod(
flutterMethodChannel?.invokeMethod(
"on_state_changed",
mapOf("name" to "input", "value" to inputPer.toString())
)
@@ -67,7 +67,7 @@ class MainActivity : FlutterActivity() {
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)
flutterMethodChannel?.invokeMethod("on_media_projection_canceled", null)
}
}
@@ -154,11 +154,11 @@ class MainActivity : FlutterActivity() {
}
}
"check_service" -> {
Companion.flutterMethodChannel.invokeMethod(
Companion.flutterMethodChannel?.invokeMethod(
"on_state_changed",
mapOf("name" to "input", "value" to InputService.isOpen.toString())
)
Companion.flutterMethodChannel.invokeMethod(
Companion.flutterMethodChannel?.invokeMethod(
"on_state_changed",
mapOf("name" to "media", "value" to MainService.isReady.toString())
)
@@ -169,7 +169,7 @@ class MainActivity : FlutterActivity() {
InputService.ctx?.disableSelf()
}
InputService.ctx = null
Companion.flutterMethodChannel.invokeMethod(
Companion.flutterMethodChannel?.invokeMethod(
"on_state_changed",
mapOf("name" to "input", "value" to InputService.isOpen.toString())
)

View File

@@ -409,13 +409,13 @@ class MainService : Service() {
fun checkMediaPermission(): Boolean {
Handler(Looper.getMainLooper()).post {
MainActivity.flutterMethodChannel.invokeMethod(
MainActivity.flutterMethodChannel?.invokeMethod(
"on_state_changed",
mapOf("name" to "media", "value" to isReady.toString())
)
}
Handler(Looper.getMainLooper()).post {
MainActivity.flutterMethodChannel.invokeMethod(
MainActivity.flutterMethodChannel?.invokeMethod(
"on_state_changed",
mapOf("name" to "input", "value" to InputService.isOpen.toString())
)

View File

@@ -59,7 +59,7 @@ fun requestPermission(context: Context, type: String) {
.request { _, all ->
if (all) {
Handler(Looper.getMainLooper()).post {
MainActivity.flutterMethodChannel.invokeMethod(
MainActivity.flutterMethodChannel?.invokeMethod(
"on_android_permission_result",
mapOf("type" to type, "result" to all)
)