android opt:add disable ignore_battery_optimizations

This commit is contained in:
csf 2022-07-16 22:31:44 +08:00
parent d3fc6ccd9c
commit 88fef77980
7 changed files with 93 additions and 36 deletions

View File

@ -192,7 +192,6 @@ class MainActivity : FlutterActivity() {
override fun onResume() {
super.onResume()
val inputPer = InputService.isOpen
Log.d(logTag, "onResume inputPer:$inputPer")
activity.runOnUiThread {
flutterMethodChannel.invokeMethod(
"on_state_changed",

View File

@ -57,6 +57,18 @@ fun requestPermission(context: Context, type: String) {
}
return
}
"application_details_settings" -> {
try {
context.startActivity(Intent().apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
action = "android.settings.APPLICATION_DETAILS_SETTINGS"
data = Uri.parse("package:" + context.packageName)
})
} catch (e:Exception) {
e.printStackTrace()
}
return
}
"audio" -> {
Permission.RECORD_AUDIO
}

View File

@ -260,7 +260,12 @@ class PermissionManager {
static Timer? _timer;
static var _current = "";
static final permissions = ["audio", "file", "ignore_battery_optimizations"];
static final permissions = [
"audio",
"file",
"ignore_battery_optimizations",
"application_details_settings"
];
static bool isWaitingFile() {
if (_completer != null) {

View File

@ -26,23 +26,42 @@ class SettingsPage extends StatefulWidget implements PageShape {
_SettingsState createState() => _SettingsState();
}
class _SettingsState extends State<SettingsPage> {
class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
static const url = 'https://rustdesk.com/';
var _showIgnoreBattery = false;
final _hasIgnoreBattery = androidVersion >= 26;
var _ignoreBatteryOpt = false;
@override
void initState() {
super.initState();
if (androidVersion >= 26) {
() async {
final res =
await PermissionManager.check("ignore_battery_optimizations");
if (_showIgnoreBattery != !res) {
setState(() {
_showIgnoreBattery = !res;
});
}
}();
WidgetsBinding.instance.addObserver(this);
if (_hasIgnoreBattery) {
updateIgnoreBatteryStatus();
}
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
updateIgnoreBatteryStatus();
}
}
Future<bool> updateIgnoreBatteryStatus() async {
final res = await PermissionManager.check("ignore_battery_optimizations");
if (_ignoreBatteryOpt != res) {
setState(() {
_ignoreBatteryOpt = res;
});
return true;
} else {
return false;
}
}
@ -53,7 +72,6 @@ class _SettingsState extends State<SettingsPage> {
final enableAbr = FFI.getByName("option", "enable-abr") != 'N';
final enhancementsTiles = [
SettingsTile.switchTile(
leading: Icon(Icons.more_horiz),
title: Text(translate('Adaptive Bitrate') + '(beta)'),
initialValue: enableAbr,
onToggle: (v) {
@ -68,32 +86,37 @@ class _SettingsState extends State<SettingsPage> {
},
)
];
if (_showIgnoreBattery) {
if (_hasIgnoreBattery) {
enhancementsTiles.insert(
0,
SettingsTile.navigation(
SettingsTile.switchTile(
initialValue: _ignoreBatteryOpt,
title: Text(translate('Keep RustDesk background service')),
description:
Text('* ${translate('Ignore Battery Optimizations')}'),
leading: Icon(Icons.battery_saver),
onPressed: (context) {
PermissionManager.request("ignore_battery_optimizations");
var count = 0;
Timer.periodic(Duration(seconds: 1), (timer) async {
if (count > 5) {
count = 0;
timer.cancel();
onToggle: (v) async {
if (v) {
PermissionManager.request("ignore_battery_optimizations");
} else {
final res = await DialogManager.show<bool>(
(setState, close) => CustomAlertDialog(
title: Text(translate("Open System Setting")),
content: Text(translate(
"android_open_battery_optimizations_tip")),
actions: [
TextButton(
onPressed: () => close(),
child: Text(translate("Cancel"))),
ElevatedButton(
onPressed: () => close(true),
child:
Text(translate("Open System Setting"))),
],
));
if (res == true) {
PermissionManager.request("application_details_settings");
}
if (await PermissionManager.check(
"ignore_battery_optimizations")) {
count = 0;
timer.cancel();
setState(() {
_showIgnoreBattery = false;
});
}
count++;
});
}
}));
}
@ -123,7 +146,13 @@ class _SettingsState extends State<SettingsPage> {
leading: Icon(Icons.cloud),
onPressed: (context) {
showServerSettings();
})
}),
SettingsTile.navigation(
title: Text(translate('Language')),
leading: Icon(Icons.translate),
onPressed: (context) {
showLanguageSettings();
}),
]),
SettingsSection(
title: Text(translate("Enhancements")),
@ -162,6 +191,13 @@ void showServerSettings() {
showServerSettingsWithValue(id, relay, key, api);
}
void showLanguageSettings() {
try {
final langs = json.decode(FFI.getByName('langs')) as Map<String, String>;
debugPrint("langs:$langs");
} catch (e) {}
}
void showAbout() {
DialogManager.show((setState, close) {
return CustomAlertDialog(

View File

@ -286,5 +286,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("Language", "语言"),
("Keep RustDesk background service", "保持RustDesk后台服务"),
("Ignore Battery Optimizations", "忽略电池优化"),
("android_open_battery_optimizations_tip", "如需关闭此功能请在接下来的RustDesk应用设置页面中找到并进入 [电源] 页面,取消勾选 [不受限制]"),
].iter().cloned().collect();
}

View File

@ -27,5 +27,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> =
("doc_mac_permission", "https://rustdesk.com/docs/en/manual/mac/#enable-permissions"),
("doc_fix_wayland", "https://rustdesk.com/docs/en/manual/linux/#x11-required"),
("server_not_support", "Not yet supported by the server"),
("android_open_battery_optimizations_tip", "If you want to disable this feature, please go to the next RustDesk application settings page, find and enter [Battery] ,Uncheck [Unrestricted]"),
].iter().cloned().collect();
}

View File

@ -130,6 +130,9 @@ unsafe extern "C" fn get_by_name(name: *const c_char, arg: *const c_char) -> *co
.clone()
.to_string();
}
"langs" => {
res = crate::lang::LANGS.to_string();
}
// File Action
"get_home_dir" => {
res = fs::get_home_as_string();