forked from cwtch.im/cwtch-ui
Merge pull request 'andoird settings / request for power optimization exemption' (#426) from power into trunk
Reviewed-on: cwtch.im/cwtch-ui#426 Reviewed-by: Sarah Jamie Lewis <sarah@openprivacy.ca>
This commit is contained in:
commit
2a712565e9
|
@ -46,6 +46,10 @@
|
|||
<!--Needed to run in background (lol)-->
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
|
||||
<!-- Ability to ask user to exempt app from power management (which can kill it more frequently especially on some devices.
|
||||
Allows app to use ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS -->
|
||||
<uses-permission-sdk-23 android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
|
||||
|
||||
<!--Needed to check if activity is foregrounded or if messages from the service should be queued-->
|
||||
<uses-permission android:name="android.permission.GET_TASKS" />
|
||||
|
||||
|
|
|
@ -1,49 +1,47 @@
|
|||
package im.cwtch.flwtch
|
||||
|
||||
import SplashView
|
||||
import android.annotation.TargetApi
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import androidx.annotation.NonNull
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.os.PowerManager
|
||||
import android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
||||
import android.util.Log
|
||||
import android.view.Window
|
||||
import androidx.annotation.NonNull
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import androidx.work.*
|
||||
import io.flutter.embedding.android.SplashScreen
|
||||
import cwtch.Cwtch
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
import io.flutter.embedding.android.SplashScreen
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import io.flutter.plugin.common.MethodChannel.Result
|
||||
import io.flutter.plugin.common.ErrorLogResult
|
||||
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.plugin.common.MethodChannel.Result
|
||||
import org.json.JSONObject
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import java.nio.file.StandardCopyOption
|
||||
|
||||
import android.net.Uri
|
||||
import android.provider.DocumentsContract
|
||||
import android.content.ContentUris
|
||||
import android.os.Build
|
||||
import android.os.Environment
|
||||
import android.database.Cursor
|
||||
import android.provider.MediaStore
|
||||
|
||||
import cwtch.Cwtch
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
|
||||
class MainActivity: FlutterActivity() {
|
||||
override fun provideSplashScreen(): SplashScreen? = SplashView()
|
||||
|
||||
|
||||
// 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"
|
||||
|
||||
// Channel to get cwtch api calls on
|
||||
private val CHANNEL_CWTCH = "cwtch"
|
||||
|
@ -67,6 +65,7 @@ class MainActivity: FlutterActivity() {
|
|||
private val FILEPICKER_REQUEST_CODE = 234
|
||||
private val PREVIEW_EXPORT_REQUEST_CODE = 235
|
||||
private val PROFILE_EXPORT_REQUEST_CODE = 236
|
||||
private val REQUEST_DOZE_WHITELISTING_CODE:Int = 9
|
||||
private var dlToProfile = ""
|
||||
private var dlToHandle = ""
|
||||
private var dlToFileKey = ""
|
||||
|
@ -98,8 +97,16 @@ 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("MainActivity:onActivityResult", "user canceled activity");
|
||||
Log.i(TAG, "user canceled activity");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -149,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)
|
||||
|
@ -162,6 +171,30 @@ class MainActivity: FlutterActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(23)
|
||||
private fun checkIgnoreBatteryOpt(): Boolean {
|
||||
val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||
return powerManager.isIgnoringBatteryOptimizations(this.packageName) ?: false;
|
||||
}
|
||||
|
||||
@TargetApi(23)
|
||||
private fun requestBatteryExemption() {
|
||||
val i = Intent()
|
||||
i.action = ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
||||
i.data = Uri.parse("package:" + this.packageName)
|
||||
startActivityForResult(i, REQUEST_DOZE_WHITELISTING_CODE);
|
||||
}
|
||||
|
||||
private fun getNativeLibDir(): String {
|
||||
val ainfo = this.applicationContext.packageManager.getApplicationInfo(
|
||||
"im.cwtch.flwtch", // Must be app name
|
||||
|
@ -489,9 +522,7 @@ class MainActivity: FlutterActivity() {
|
|||
// We need to do this here because after a "pause" flutter is still running
|
||||
// but we might have lost sync with the background process...
|
||||
Log.i("MainActivity.kt", "Call ReconnectCwtchForeground")
|
||||
val data: Data = Data.Builder().putString(FlwtchWorker.KEY_METHOD, "ReconnectCwtchForeground").putString(FlwtchWorker.KEY_ARGS, "{}").build()
|
||||
val workRequest = OneTimeWorkRequestBuilder<FlwtchWorker>().setInputData(data).build()
|
||||
WorkManager.getInstance(applicationContext).enqueue(workRequest)
|
||||
Cwtch.reconnectCwtchForeground()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"@@locale": "cy",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"settingAndroidPowerExemptionDescription": "Optional: Request Android to exempt Cwtch from optimized power management. This will result in better stability at the cost of greater battery 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",
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"@@locale": "da",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"settingAndroidPowerExemptionDescription": "Optional: Request Android to exempt Cwtch from optimized power management. This will result in better stability at the cost of greater battery 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",
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"@@locale": "de",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"settingAndroidPowerExemptionDescription": "Optional: Request Android to exempt Cwtch from optimized power management. This will result in better stability at the cost of greater battery 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",
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"@@locale": "el",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"settingAndroidPowerExemptionDescription": "Optional: Request Android to exempt Cwtch from optimized power management. This will result in better stability at the cost of greater battery 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",
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"@@locale": "en",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"settingAndroidPowerExemptionDescription": "Optional: Request Android to exempt Cwtch from optimized power management. This will result in better stability at the cost of greater battery 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",
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"@@locale": "es",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"settingAndroidPowerExemptionDescription": "Optional: Request Android to exempt Cwtch from optimized power management. This will result in better stability at the cost of greater battery 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",
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
{
|
||||
"@@locale": "fr",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"thisFeatureRequiresGroupExpermientsToBeEnabled": "This feature requires the Groups Experiment to be enabled in Settings",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"thisFeatureRequiresGroupExpermientsToBeEnabled": "Cette fonctionnalité nécessite que l’expérience Groupes soit activée dans Paramètres",
|
||||
"settingAndroidPowerExemptionDescription": "Android applique par défaut un profil de gestion de l'énergie \"optimisé\" aux applications, ce qui peut entraîner leur arrêt ou leur suppression. Demandez à Android d'exempter Cwtch de ce profil pour une meilleure stabilité mais une plus grande consommation d'énergie.",
|
||||
"settingAndroidPowerExemption": "Android ignore les optimisations de la batterie",
|
||||
"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",
|
||||
"clickableLinksWarning": "L'ouverture de cette URL lancera une application en dehors de Cwtch et peut révéler des métadonnées ou compromettre la sécurité de Cwtch. N'ouvrez que les URLs de personnes en qui vous avez confiance. Êtes-vous sûr de vouloir continuer ?",
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"@@locale": "it",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"settingAndroidPowerExemptionDescription": "Optional: Request Android to exempt Cwtch from optimized power management. This will result in better stability at the cost of greater battery 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",
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"@@locale": "lb",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"settingAndroidPowerExemptionDescription": "Optional: Request Android to exempt Cwtch from optimized power management. This will result in better stability at the cost of greater battery 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",
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"@@locale": "no",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"settingAndroidPowerExemptionDescription": "Optional: Request Android to exempt Cwtch from optimized power management. This will result in better stability at the cost of greater battery 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",
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"@@locale": "pl",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"settingAndroidPowerExemptionDescription": "Optional: Request Android to exempt Cwtch from optimized power management. This will result in better stability at the cost of greater battery 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",
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"@@locale": "pt",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"settingAndroidPowerExemptionDescription": "Optional: Request Android to exempt Cwtch from optimized power management. This will result in better stability at the cost of greater battery 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",
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"@@locale": "ro",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"settingAndroidPowerExemptionDescription": "Optional: Request Android to exempt Cwtch from optimized power management. This will result in better stability at the cost of greater battery 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",
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
{
|
||||
"@@locale": "ru",
|
||||
"@@last_modified": "2022-04-12T20:56:53+02:00",
|
||||
"@@last_modified": "2022-04-13T22:56:39+02:00",
|
||||
"okButton": "OK",
|
||||
"settingsAndroidPowerReenablePopup": "Cannot re-enable Battery Optimization from within Cwtch. Please go to Android \/ Settings \/ Apps \/ Cwtch \/ Battery and set Usage to 'Optimized'",
|
||||
"settingAndroidPowerExemptionDescription": "Optional: Request Android to exempt Cwtch from optimized power management. This will result in better stability at the cost of greater battery 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",
|
||||
|
|
|
@ -109,8 +109,8 @@ class Settings extends ChangeNotifier {
|
|||
// single pane vs dual pane preferences
|
||||
_uiColumnModePortrait = uiColumnModeFromString(settings["UIColumnModePortrait"]);
|
||||
_uiColumnModeLandscape = uiColumnModeFromString(settings["UIColumnModeLandscape"]);
|
||||
|
||||
_notificationPolicy = notificationPolicyFromString(settings["NotificationPolicy"]);
|
||||
|
||||
_notificationContent = notificationContentFromString(settings["NotificationContent"]);
|
||||
|
||||
// auto-download folder
|
||||
|
@ -275,7 +275,7 @@ class Settings extends ChangeNotifier {
|
|||
|
||||
static NotificationPolicy notificationPolicyFromString(String? np) {
|
||||
switch (np) {
|
||||
case "NotificationPolicy.None":
|
||||
case "NotificationPolicy.Mute":
|
||||
return NotificationPolicy.Mute;
|
||||
case "NotificationPolicy.OptIn":
|
||||
return NotificationPolicy.OptIn;
|
||||
|
|
|
@ -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,29 @@ 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 can't ask for it to be turned off, show an informational popup
|
||||
showBatteryDialog(context);
|
||||
}
|
||||
},
|
||||
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),
|
||||
|
@ -402,6 +468,32 @@ class _GlobalSettingsViewState extends State<GlobalSettingsView> {
|
|||
});
|
||||
});
|
||||
}
|
||||
|
||||
showBatteryDialog(BuildContext context) {
|
||||
Widget okButton = ElevatedButton(
|
||||
child: Text(AppLocalizations.of(context)!.okButton),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
// set up the AlertDialog
|
||||
AlertDialog alert = AlertDialog(
|
||||
title: Text(AppLocalizations.of(context)!.settingsAndroidPowerReenablePopup),
|
||||
actions: [
|
||||
okButton,
|
||||
],
|
||||
);
|
||||
|
||||
// show the dialog
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext context) {
|
||||
return alert;
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct a version string from Package Info
|
||||
|
|
Loading…
Reference in New Issue