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)
)

View File

@ -36,7 +36,6 @@ const url = 'https://rustdesk.com/';
class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
final _hasIgnoreBattery = androidVersion >= 26;
var _ignoreBatteryOpt = false;
var _systemAlertWindow = false;
var _enableStartOnBoot = false;
var _enableAbr = false;
var _denyLANDiscovery = false;
@ -61,7 +60,7 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
}
}
if (await checkAndUpdateSystemAlertWindow()) {
if (await checkAndUpdateStartOnBoot()) {
update = true;
}
@ -69,7 +68,7 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
var enableStartOnBoot =
await gFFI.invokeMethod(AndroidChannel.kGetStartOnBootOpt);
if (enableStartOnBoot) {
if (!canStartOnBoot()) {
if (!await canStartOnBoot()) {
enableStartOnBoot = false;
gFFI.invokeMethod(AndroidChannel.kSetStartOnBootOpt, false);
}
@ -152,8 +151,9 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
() async {
if (await checkAndUpdateIgnoreBatteryStatus() ||
await checkAndUpdateSystemAlertWindow()) {
final ibs = await checkAndUpdateIgnoreBatteryStatus();
final sob = await checkAndUpdateStartOnBoot();
if (ibs || sob) {
setState(() {});
}
}();
@ -171,10 +171,12 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
}
}
Future<bool> checkAndUpdateSystemAlertWindow() async {
final res = await AndroidPermissionManager.check(kSystemAlertWindow);
if (_systemAlertWindow != res) {
_systemAlertWindow = res;
Future<bool> checkAndUpdateStartOnBoot() async {
if (!await canStartOnBoot() && _enableStartOnBoot) {
_enableStartOnBoot = false;
debugPrint(
"checkAndUpdateStartOnBoot and set _enableStartOnBoot -> false");
gFFI.invokeMethod(AndroidChannel.kSetStartOnBootOpt, false);
return true;
} else {
return false;
@ -461,12 +463,12 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
);
}
bool canStartOnBoot() {
Future<bool> canStartOnBoot() async {
// start on boot depends on ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS and SYSTEM_ALERT_WINDOW
if (_hasIgnoreBattery && !_ignoreBatteryOpt) {
return false;
}
if (!_systemAlertWindow) {
if (!await AndroidPermissionManager.check(kSystemAlertWindow)) {
return false;
}
return true;