add setting that reports / triggers android power optimization exemption

This commit is contained in:
Dan Ballard 2022-04-12 18:06:48 -07:00
parent d261fbd4c0
commit c377a09748
17 changed files with 129 additions and 30 deletions

View File

@ -37,6 +37,9 @@ class MainActivity: FlutterActivity() {
// Channel to get app info
private val CHANNEL_APP_INFO = "test.flutter.dev/applicationInfo"
private val CALL_APP_INFO = "getNativeLibDir"
private val ANDROID_SETTINGS_CHANNEL_NAME = "androidSettings"
private val ANDROID_SETTINGS_CHANGE_NAME= "androidSettingsChanged"
private var andoidSettingsChangeChannel: MethodChannel? = null
private val CALL_ASK_BATTERY_EXEMPTION = "requestBatteryExemption"
private val CALL_IS_BATTERY_EXEMPT = "isBatteryExempt"
@ -94,6 +97,14 @@ class MainActivity: FlutterActivity() {
override fun onActivityResult(requestCode: Int, result: Int, intent: Intent?) {
super.onActivityResult(requestCode, result, intent);
// has null intent and data
if (requestCode == REQUEST_DOZE_WHITELISTING_CODE) {
// 0 == "battery optimized" (still)
// -1 == "no battery optimization" (exempt!)
andoidSettingsChangeChannel!!.invokeMethod("powerExemptionChange", result == -1)
return;
}
if (intent == null || intent!!.getData() == null) {
Log.i(TAG, "user canceled activity");
return;
@ -135,8 +146,6 @@ class MainActivity: FlutterActivity() {
os?.close();
//Files.delete(sourcePath);
}
} else if (requestCode == REQUEST_DOZE_WHITELISTING_CODE) {
checkIgnoreBatteryOpt()
}
}
@ -147,8 +156,10 @@ class MainActivity: FlutterActivity() {
requestWindowFeature(Window.FEATURE_NO_TITLE)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL_APP_INFO).setMethodCallHandler { call, result -> handleAppInfo(call, result) }
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL_CWTCH).setMethodCallHandler { call, result -> handleCwtch(call, result) }
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, ANDROID_SETTINGS_CHANNEL_NAME).setMethodCallHandler { call, result -> handleAndroidSettings(call, result) }
notificationClickChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL_NOTIF_CLICK)
shutdownClickChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL_SHUTDOWN_CLICK)
andoidSettingsChangeChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, ANDROID_SETTINGS_CHANGE_NAME)
}
// MethodChannel CHANNEL_APP_INFO handler (Flutter Channel for requests for Android environment info)
@ -156,8 +167,16 @@ class MainActivity: FlutterActivity() {
when (call.method) {
CALL_APP_INFO -> result.success(getNativeLibDir())
?: result.error("Unavailable", "nativeLibDir not available", null);
CALL_ASK_BATTERY_EXEMPTION -> result.success(checkIgnoreBatteryOpt()) ?: false;
CALL_IS_BATTERY_EXEMPT -> result.success(requestBatteryExemption());
else -> result.notImplemented()
}
}
// MethodChannel ANDROID_SETTINGS_CHANNEL_NAME handler (Flutter Channel for requests for Android settings)
// Called from lib/view/globalsettingsview.dart
private fun handleAndroidSettings(@NonNull call: MethodCall, @NonNull result: Result) {
when (call.method) {
CALL_IS_BATTERY_EXEMPT -> result.success(checkIgnoreBatteryOpt() ?: false);
CALL_ASK_BATTERY_EXEMPTION -> { requestBatteryExemption(); result.success(null); }
else -> result.notImplemented()
}
}

View File

@ -57,18 +57,6 @@ class CwtchGomobile implements Cwtch {
return cwtchPlatform.invokeMethod("Start", {"appDir": cwtchDir, "torPath": torPath});
}
//* Android Only Requests
Future<bool> IsBatteryExempt() async {
return await appInfoPlatform?.invokeMethod('isBatteryExempt') ?? false;
}
Future<void> RequestBatteryExemption() async {
await appInfoPlatform?.invokeMethod('requestBatteryExemption');
}
//* End Android Only Requests
@override
// ignore: non_constant_identifier_names
Future<void> ReconnectCwtchForeground() async {

View File

@ -1,6 +1,8 @@
{
"@@locale": "cy",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"@@last_modified": "2022-04-13T02:54:41+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Enable rich text formatting in displayed messages e.g. **bold** and *italic*",
"formattingExperiment": "Message Formatting",

View File

@ -1,6 +1,8 @@
{
"@@locale": "da",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"@@last_modified": "2022-04-13T02:54:41+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Enable rich text formatting in displayed messages e.g. **bold** and *italic*",
"formattingExperiment": "Message Formatting",

View File

@ -1,6 +1,8 @@
{
"@@locale": "de",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"@@last_modified": "2022-04-13T02:54:41+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Enable rich text formatting in displayed messages e.g. **bold** and *italic*",
"formattingExperiment": "Message Formatting",

View File

@ -1,6 +1,8 @@
{
"@@locale": "el",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"@@last_modified": "2022-04-13T02:54:41+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Enable rich text formatting in displayed messages e.g. **bold** and *italic*",
"formattingExperiment": "Message Formatting",

View File

@ -1,6 +1,8 @@
{
"@@locale": "en",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"@@last_modified": "2022-04-13T02:54:41+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Enable rich text formatting in displayed messages e.g. **bold** and *italic*",
"formattingExperiment": "Message Formatting",

View File

@ -1,6 +1,8 @@
{
"@@locale": "es",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"@@last_modified": "2022-04-13T02:54:41+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Enable rich text formatting in displayed messages e.g. **bold** and *italic*",
"formattingExperiment": "Message Formatting",

View File

@ -1,6 +1,8 @@
{
"@@locale": "fr",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"@@last_modified": "2022-04-13T02:54:41+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Activer la mise en forme de texte enrichi dans les messages affichés, par exemple **gras** et *italique*",
"formattingExperiment": "Mise en forme des messages",

View File

@ -1,6 +1,8 @@
{
"@@locale": "it",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"@@last_modified": "2022-04-13T02:54:41+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Enable rich text formatting in displayed messages e.g. **bold** and *italic*",
"formattingExperiment": "Message Formatting",

View File

@ -1,6 +1,8 @@
{
"@@locale": "lb",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"@@last_modified": "2022-04-13T02:54:41+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Enable rich text formatting in displayed messages e.g. **bold** and *italic*",
"formattingExperiment": "Message Formatting",

View File

@ -1,6 +1,8 @@
{
"@@locale": "no",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"@@last_modified": "2022-04-13T02:54:41+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Enable rich text formatting in displayed messages e.g. **bold** and *italic*",
"formattingExperiment": "Message Formatting",

View File

@ -1,6 +1,8 @@
{
"@@locale": "pl",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"@@last_modified": "2022-04-13T02:54:41+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Enable rich text formatting in displayed messages e.g. **bold** and *italic*",
"formattingExperiment": "Message Formatting",

View File

@ -1,6 +1,8 @@
{
"@@locale": "pt",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"@@last_modified": "2022-04-13T02:54:41+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Enable rich text formatting in displayed messages e.g. **bold** and *italic*",
"formattingExperiment": "Message Formatting",

View File

@ -1,6 +1,8 @@
{
"@@locale": "ro",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"@@last_modified": "2022-04-13T02:54:41+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Enable rich text formatting in displayed messages e.g. **bold** and *italic*",
"formattingExperiment": "Message Formatting",

View File

@ -1,6 +1,7 @@
{
"@@locale": "ru",
"@@last_modified": "2022-04-12T20:56:53+02:00",
"settingAndroidPowerExemptionDescription": "Android by default applies an \"optimized\" power management profile to apps which can result in them being stopped or killed. Request Android to exempt Cwtch from this for better stability but more power use.",
"settingAndroidPowerExemption": "Android Ignore Battery Optimizations",
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
"messageFormattingDescription": "Enable rich text formatting in displayed messages e.g. **bold** and *italic*",
"formattingExperiment": "Message Formatting",

View File

@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'package:cwtch/cwtch_icons_icons.dart';
import 'package:cwtch/models/servers.dart';
import 'package:cwtch/widgets/folderpicker.dart';
@ -13,6 +14,7 @@ import 'package:cwtch/themes/opaque.dart';
import 'package:cwtch/themes/pumpkin.dart';
import 'package:cwtch/themes/vampire.dart';
import 'package:cwtch/themes/witch.dart';
import 'package:flutter/services.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:cwtch/settings.dart';
@ -29,11 +31,52 @@ class GlobalSettingsView extends StatefulWidget {
}
class _GlobalSettingsViewState extends State<GlobalSettingsView> {
static const androidSettingsChannel = const MethodChannel('androidSettings');
static const androidSettingsChangeChannel = const MethodChannel('androidSettingsChanged');
bool powerExempt = false;
@override
void dispose() {
super.dispose();
}
@override
void initState() {
super.initState();
androidSettingsChangeChannel.setMethodCallHandler(handleSettingsChanged);
if (Platform.isAndroid) {
isBatteryExempt().then((value) => setState(() { powerExempt = value; }) );
} else {
powerExempt = false;
}
}
// Handler on method channel for MainActivity/onActivityResult to report the user choice when we ask for power exemption
Future<void> handleSettingsChanged(MethodCall call) async {
if (call.method == "powerExemptionChange") {
if (call.arguments) {
setState(() {
powerExempt = true;
});
}
}
}
//* Android Only Requests
Future<bool> isBatteryExempt() async {
return await androidSettingsChannel.invokeMethod('isBatteryExempt', {}) ?? false;
}
Future<void> requestBatteryExemption() async {
await androidSettingsChannel.invokeMethod('requestBatteryExemption', {});
return Future.value();
}
//* End Android Only Requests
@override
Widget build(BuildContext context) {
return Scaffold(
@ -172,6 +215,28 @@ class _GlobalSettingsViewState extends State<GlobalSettingsView> {
height: 40,
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [Text(AppLocalizations.of(context)!.settingGroupBehaviour, style: TextStyle(fontWeight: FontWeight.bold))]),
Visibility(
visible: Platform.isAndroid,
child: SwitchListTile(
title: Text(AppLocalizations.of(context)!.settingAndroidPowerExemption, style: TextStyle(color: settings
.current()
.mainTextColor)),
subtitle: Text(AppLocalizations.of(context)!.settingAndroidPowerExemptionDescription),
value: powerExempt,
onChanged: (bool value) {
if (value) {
requestBatteryExemption();
} else {
// We don't ask for it to be turned off, user has to manually in android settings, so this is a NOP
}
},
activeTrackColor: settings.theme.defaultButtonColor,
inactiveTrackColor: settings.theme.defaultButtonDisabledColor,
secondary: Icon(Icons.power, color: settings
.current()
.mainTextColor),
),
),
ListTile(
title: Text(AppLocalizations.of(context)!.notificationPolicySettingLabel),
subtitle: Text(AppLocalizations.of(context)!.notificationPolicySettingDescription),