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 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.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
import android.widget.Toast 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" 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}") Log.d(logTag, "onReceive ${intent.action}")
if (Intent.ACTION_BOOT_COMPLETED == intent.action || DEBUG_BOOT_COMPLETED == 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 { val it = Intent(context, MainService::class.java).apply {
action = ACT_INIT_MEDIA_PROJECTION_AND_SERVICE action = ACT_INIT_MEDIA_PROJECTION_AND_SERVICE
} }

View File

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

View File

@ -409,13 +409,13 @@ class MainService : Service() {
fun checkMediaPermission(): Boolean { fun checkMediaPermission(): Boolean {
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
MainActivity.flutterMethodChannel.invokeMethod( MainActivity.flutterMethodChannel?.invokeMethod(
"on_state_changed", "on_state_changed",
mapOf("name" to "media", "value" to isReady.toString()) mapOf("name" to "media", "value" to isReady.toString())
) )
} }
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
MainActivity.flutterMethodChannel.invokeMethod( MainActivity.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())
) )

View File

@ -59,7 +59,7 @@ fun requestPermission(context: Context, type: String) {
.request { _, all -> .request { _, all ->
if (all) { if (all) {
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
MainActivity.flutterMethodChannel.invokeMethod( MainActivity.flutterMethodChannel?.invokeMethod(
"on_android_permission_result", "on_android_permission_result",
mapOf("type" to type, "result" to all) 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 { class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
final _hasIgnoreBattery = androidVersion >= 26; final _hasIgnoreBattery = androidVersion >= 26;
var _ignoreBatteryOpt = false; var _ignoreBatteryOpt = false;
var _systemAlertWindow = false;
var _enableStartOnBoot = false; var _enableStartOnBoot = false;
var _enableAbr = false; var _enableAbr = false;
var _denyLANDiscovery = false; var _denyLANDiscovery = false;
@ -61,7 +60,7 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
} }
} }
if (await checkAndUpdateSystemAlertWindow()) { if (await checkAndUpdateStartOnBoot()) {
update = true; update = true;
} }
@ -69,7 +68,7 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
var enableStartOnBoot = var enableStartOnBoot =
await gFFI.invokeMethod(AndroidChannel.kGetStartOnBootOpt); await gFFI.invokeMethod(AndroidChannel.kGetStartOnBootOpt);
if (enableStartOnBoot) { if (enableStartOnBoot) {
if (!canStartOnBoot()) { if (!await canStartOnBoot()) {
enableStartOnBoot = false; enableStartOnBoot = false;
gFFI.invokeMethod(AndroidChannel.kSetStartOnBootOpt, false); gFFI.invokeMethod(AndroidChannel.kSetStartOnBootOpt, false);
} }
@ -152,8 +151,9 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
void didChangeAppLifecycleState(AppLifecycleState state) { void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) { if (state == AppLifecycleState.resumed) {
() async { () async {
if (await checkAndUpdateIgnoreBatteryStatus() || final ibs = await checkAndUpdateIgnoreBatteryStatus();
await checkAndUpdateSystemAlertWindow()) { final sob = await checkAndUpdateStartOnBoot();
if (ibs || sob) {
setState(() {}); setState(() {});
} }
}(); }();
@ -171,10 +171,12 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
} }
} }
Future<bool> checkAndUpdateSystemAlertWindow() async { Future<bool> checkAndUpdateStartOnBoot() async {
final res = await AndroidPermissionManager.check(kSystemAlertWindow); if (!await canStartOnBoot() && _enableStartOnBoot) {
if (_systemAlertWindow != res) { _enableStartOnBoot = false;
_systemAlertWindow = res; debugPrint(
"checkAndUpdateStartOnBoot and set _enableStartOnBoot -> false");
gFFI.invokeMethod(AndroidChannel.kSetStartOnBootOpt, false);
return true; return true;
} else { } else {
return false; 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 // start on boot depends on ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS and SYSTEM_ALERT_WINDOW
if (_hasIgnoreBattery && !_ignoreBatteryOpt) { if (_hasIgnoreBattery && !_ignoreBatteryOpt) {
return false; return false;
} }
if (!_systemAlertWindow) { if (!await AndroidPermissionManager.check(kSystemAlertWindow)) {
return false; return false;
} }
return true; return true;