[android] fix build; ignore battery optimizations

This commit is contained in:
csf 2022-07-14 17:44:37 +08:00
parent bb8257b7f2
commit fb02fc1197
22 changed files with 128 additions and 15 deletions

View File

@ -3,6 +3,7 @@
package="com.carriez.flutter_hbb">
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

View File

@ -2,20 +2,26 @@ package com.carriez.flutter_hbb
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.media.AudioRecord
import android.media.AudioRecord.READ_BLOCKING
import android.media.MediaCodecList
import android.media.MediaFormat
import android.net.Uri
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.os.PowerManager
import android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS
import android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat.getSystemService
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import java.nio.ByteBuffer
import java.util.*
@SuppressLint("ConstantLocale")
val LOCAL_NAME = Locale.getDefault().toString()
val SCREEN_INFO = Info(0, 0, 1, 200)
@ -38,8 +44,19 @@ fun testVP9Support(): Boolean {
return res != null
}
@RequiresApi(Build.VERSION_CODES.M)
fun requestPermission(context: Context, type: String) {
val permission = when (type) {
"ignore_battery_optimizations" -> {
try {
context.startActivity(Intent(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply {
data = Uri.parse("package:" + context.packageName)
})
} catch (e:Exception) {
e.printStackTrace()
}
return
}
"audio" -> {
Permission.RECORD_AUDIO
}
@ -52,7 +69,7 @@ fun requestPermission(context: Context, type: String) {
}
XXPermissions.with(context)
.permission(permission)
.request { permissions, all ->
.request { _, all ->
if (all) {
Handler(Looper.getMainLooper()).post {
MainActivity.flutterMethodChannel.invokeMethod(
@ -64,8 +81,13 @@ fun requestPermission(context: Context, type: String) {
}
}
@RequiresApi(Build.VERSION_CODES.M)
fun checkPermission(context: Context, type: String): Boolean {
val permission = when (type) {
"ignore_battery_optimizations" -> {
val pw = context.getSystemService(Context.POWER_SERVICE) as PowerManager
return pw.isIgnoringBatteryOptimizations(context.packageName)
}
"audio" -> {
Permission.RECORD_AUDIO
}

View File

@ -260,7 +260,7 @@ class PermissionManager {
static Timer? _timer;
static var _current = "";
static final permissions = ["audio", "file"];
static final permissions = ["audio", "file", "ignore_battery_optimizations"];
static bool isWaitingFile() {
if (_completer != null) {
@ -279,9 +279,12 @@ class PermissionManager {
if (!permissions.contains(type))
return Future.error("Wrong permission!$type");
FFI.invokeMethod("request_permission", type);
if (type == "ignore_battery_optimizations") {
return Future.value(false);
}
_current = type;
_completer = Completer<bool>();
FFI.invokeMethod("request_permission", type);
// timeout
_timer?.cancel();

View File

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:settings_ui/settings_ui.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
@ -26,11 +28,69 @@ class SettingsPage extends StatefulWidget implements PageShape {
class _SettingsState extends State<SettingsPage> {
static const url = 'https://rustdesk.com/';
var _showIgnoreBattery = false;
@override
void initState() {
super.initState();
if (androidVersion >= 26) {
() async {
final res =
await PermissionManager.check("ignore_battery_optimizations");
if (_showIgnoreBattery != !res) {
setState(() {
_showIgnoreBattery = !res;
});
}
}();
}
}
@override
Widget build(BuildContext context) {
Provider.of<FfiModel>(context);
final username = getUsername();
final settingsTiles = [
SettingsTile.navigation(
title: Text(translate('ID/Relay Server')),
leading: Icon(Icons.cloud),
onPressed: (context) {
showServerSettings();
},
),
SettingsTile.navigation(
title: Text(translate('Enhancements')),
leading: Icon(Icons.tune),
onPressed: (context) {},
),
];
if (_showIgnoreBattery) {
settingsTiles.add(SettingsTile.navigation(
title: Text(translate('Keep RustDesk background service')),
description: Text('* ${translate('Ignore Battery Optimizations')}'),
leading: Icon(Icons.settings_backup_restore),
onPressed: (context) {
PermissionManager.request("ignore_battery_optimizations");
var count = 0;
Timer.periodic(Duration(seconds: 1), (timer) async {
debugPrint("BatteryOpt Timer, count:$count");
if (count > 5) {
count = 0;
timer.cancel();
}
if (await PermissionManager.check(
"ignore_battery_optimizations")) {
count = 0;
timer.cancel();
setState(() {
_showIgnoreBattery = false;
});
}
count++;
});
}));
}
return SettingsList(
sections: [
SettingsSection(
@ -53,15 +113,7 @@ class _SettingsState extends State<SettingsPage> {
),
SettingsSection(
title: Text(translate("Settings")),
tiles: [
SettingsTile.navigation(
title: Text(translate('ID/Relay Server')),
leading: Icon(Icons.cloud),
onPressed: (context) {
showServerSettings();
},
),
],
tiles: settingsTiles,
),
SettingsSection(
title: Text(translate("About")),

View File

@ -316,6 +316,9 @@ async fn test_nat_type_() -> ResultType<bool> {
}
pub async fn get_rendezvous_server(ms_timeout: u64) -> (String, Vec<String>, bool) {
#[cfg(any(target_os = "android", target_os = "ios"))]
let (mut a, mut b) = get_rendezvous_server_(ms_timeout);
#[cfg(not(any(target_os = "android", target_os = "ios")))]
let (mut a, mut b) = get_rendezvous_server_(ms_timeout).await;
let mut b: Vec<String> = b
.drain(..)

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "进入隐私模式"),
("Out privacy mode", "退出隐私模式"),
("Language", "语言"),
("Keep RustDesk background service", "保持RustDesk后台服务"),
("Ignore Battery Optimizations", "忽略电池优化"),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "v režimu soukromí"),
("Out privacy mode", "mimo režim soukromí"),
("Language", ""),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "I databeskyttelsestilstand"),
("Out privacy mode", "Databeskyttelsestilstand fra"),
("Language", ""),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "im Datenschutzmodus"),
("Out privacy mode", "Datenschutzmodus aus"),
("Language", "Sprache"),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", ""),
("Out privacy mode", ""),
("Language", ""),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "En modo de privacidad"),
("Out privacy mode", "Fuera del modo de privacidad"),
("Language", ""),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "en mode privé"),
("Out privacy mode", "hors mode de confidentialité"),
("Language", "Langue"),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -270,7 +270,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("Overwrite", "Felülírás"),
("This file exists, skip or overwrite this file?", "Ez a fájl már létezik, skippeljünk, vagy felülírjuk ezt a fájlt?"),
("Quit", "Kilépés"),
("doc_mac_permission", "https://rustdesk.com/docs/hu/manual/mac/#enable-permissions"),
("doc_mac_permission", "https://rustdesk.com/docs/hu/manual/mac/#enable-permissions"),
("Help", "Segítség"),
("Failed", "Sikertelen"),
("Succeeded", "Sikeres"),
@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "Belépés a privát módba"),
("Out privacy mode", "Kilépés a privát módból"),
("Language", "Nyelv"),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "Dalam mode privasi"),
("Out privacy mode", "Keluar dari mode privasi"),
("Language", ""),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "In modalità privacy"),
("Out privacy mode", "Fuori modalità privacy"),
("Language", "Linguaggio"),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "No modo de privacidade"),
("Out privacy mode", "Fora do modo de privacidade"),
("Language", ""),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "В режиме конфиденциальности"),
("Out privacy mode", "Выход из режима конфиденциальности"),
("Language", "Язык"),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "V režime súkromia"),
("Out privacy mode", "Mimo režimu súkromia"),
("Language", ""),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", ""),
("Out privacy mode", ""),
("Language", ""),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "Gizlilik modunda"),
("Out privacy mode", "Gizlilik modu dışında"),
("Language", ""),
("Keep RustDesk background service", ""),
("Ignore Battery Optimizations", ""),
].iter().cloned().collect();
}

View File

@ -284,5 +284,7 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("In privacy mode", "開啟隱私模式"),
("Out privacy mode", "退出隱私模式"),
("Language", "語言"),
("Keep RustDesk background service", "保持RustDesk後台服務"),
("Ignore Battery Optimizations", "忽略電池優化"),
].iter().cloned().collect();
}

View File

@ -171,7 +171,7 @@ impl VideoQoS {
#[cfg(target_os = "android")]
{
// fix when andorid screen shrinks
let fix = Display::fix_quality() as u32;
let fix = scrap::Display::fix_quality() as u32;
log::debug!("Android screen, fix quality:{}", fix);
let base_bitrate = base_bitrate * fix;
self.target_bitrate = base_bitrate * self.current_image_quality / 100;