android: Add a preference flag to ignore battery optimizations

This allows users to ignore whether the app is on the device's power
whitelist without a warning.  The flag is currently not set
automatically if the user denies the request.
This commit is contained in:
Tobias Brunner 2020-05-11 15:49:22 +02:00
parent 5d01aaf91d
commit 04f4bef235
10 changed files with 33 additions and 4 deletions

View File

@ -23,6 +23,7 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.net.VpnService; import android.net.VpnService;
import android.os.Build; import android.os.Build;
@ -41,6 +42,7 @@ import org.strongswan.android.data.VpnProfileDataSource;
import org.strongswan.android.data.VpnType.VpnTypeFeature; import org.strongswan.android.data.VpnType.VpnTypeFeature;
import org.strongswan.android.logic.VpnStateService; import org.strongswan.android.logic.VpnStateService;
import org.strongswan.android.logic.VpnStateService.State; import org.strongswan.android.logic.VpnStateService.State;
import org.strongswan.android.utils.Constants;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
@ -49,6 +51,7 @@ import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.preference.PreferenceManager;
public class VpnProfileControlActivity extends AppCompatActivity public class VpnProfileControlActivity extends AppCompatActivity
{ {
@ -198,7 +201,9 @@ public class VpnProfileControlActivity extends AppCompatActivity
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{ {
PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE); PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(this.getPackageName())) SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
if (!pm.isIgnoringBatteryOptimizations(this.getPackageName()) &&
!pref.getBoolean(Constants.PREF_IGNORE_POWER_WHITELIST, false))
{ {
PowerWhitelistRequired whitelist = new PowerWhitelistRequired(); PowerWhitelistRequired whitelist = new PowerWhitelistRequired();
mWaitingForResult = true; mWaitingForResult = true;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2016-2018 Tobias Brunner * Copyright (C) 2016-2020 Tobias Brunner
* HSR Hochschule fuer Technik Rapperswil * HSR Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
@ -58,4 +58,9 @@ public final class Constants
* Preference key to store the most recently used VPN profile * Preference key to store the most recently used VPN profile
*/ */
public static final String PREF_MRU_VPN_PROFILE = "pref_mru_vpn_profile"; public static final String PREF_MRU_VPN_PROFILE = "pref_mru_vpn_profile";
/**
* Preference key to store whether the user permanently dismissed our warning to add the app to the power whitelist
*/
public static final String PREF_IGNORE_POWER_WHITELIST = "pref_ignore_power_whitelist";
} }

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- <!--
Copyright (C) 2012-2019 Tobias Brunner Copyright (C) 2012-2020 Tobias Brunner
Copyright (C) 2012 Giuliano Grassi Copyright (C) 2012 Giuliano Grassi
Copyright (C) 2012 Ralf Sager Copyright (C) 2012 Ralf Sager
HSR Hochschule fuer Technik Rapperswil HSR Hochschule fuer Technik Rapperswil
@ -36,6 +36,8 @@
<string name="pref_title">Einstellungen</string> <string name="pref_title">Einstellungen</string>
<string name="pref_default_vpn_profile">Voreingestelltes VPN Profil</string> <string name="pref_default_vpn_profile">Voreingestelltes VPN Profil</string>
<string name="pref_default_vpn_profile_mru">Verbinden mit zuletzt verwendetem Profil</string> <string name="pref_default_vpn_profile_mru">Verbinden mit zuletzt verwendetem Profil</string>
<string name="pref_power_whitelist_title">Akku-Optimierung ignorieren</string>
<string name="pref_power_whitelist_summary">Keine Warnung anzeigen, falls die App nicht auf der weissen Liste für Akku-Optimierung ist</string>
<!-- Log view --> <!-- Log view -->
<string name="log_title">Log</string> <string name="log_title">Log</string>

View File

@ -36,6 +36,8 @@
<string name="pref_title">Settings</string> <string name="pref_title">Settings</string>
<string name="pref_default_vpn_profile">Default VPN profile</string> <string name="pref_default_vpn_profile">Default VPN profile</string>
<string name="pref_default_vpn_profile_mru">Connect to most recently used profile</string> <string name="pref_default_vpn_profile_mru">Connect to most recently used profile</string>
<string name="pref_power_whitelist_title">Ignore battery optimizations</string>
<string name="pref_power_whitelist_summary">Don\'t show a warning if the app is not on the device\'s power whitelist</string>
<!-- Log view --> <!-- Log view -->
<string name="log_title">Log</string> <string name="log_title">Log</string>

View File

@ -33,6 +33,8 @@
<string name="pref_title">Settings</string> <string name="pref_title">Settings</string>
<string name="pref_default_vpn_profile">Default VPN profile</string> <string name="pref_default_vpn_profile">Default VPN profile</string>
<string name="pref_default_vpn_profile_mru">Connect to most recently used profile</string> <string name="pref_default_vpn_profile_mru">Connect to most recently used profile</string>
<string name="pref_power_whitelist_title">Ignore battery optimizations</string>
<string name="pref_power_whitelist_summary">Don\'t show a warning if the app is not on the device\'s power whitelist</string>
<!-- Log view --> <!-- Log view -->
<string name="log_title">Журнал</string> <string name="log_title">Журнал</string>

View File

@ -34,6 +34,8 @@
<string name="pref_title">Settings</string> <string name="pref_title">Settings</string>
<string name="pref_default_vpn_profile">Default VPN profile</string> <string name="pref_default_vpn_profile">Default VPN profile</string>
<string name="pref_default_vpn_profile_mru">Connect to most recently used profile</string> <string name="pref_default_vpn_profile_mru">Connect to most recently used profile</string>
<string name="pref_power_whitelist_title">Ignore battery optimizations</string>
<string name="pref_power_whitelist_summary">Don\'t show a warning if the app is not on the device\'s power whitelist</string>
<!-- Log view --> <!-- Log view -->
<string name="log_title">Журнал</string> <string name="log_title">Журнал</string>

View File

@ -33,6 +33,8 @@
<string name="pref_title">Settings</string> <string name="pref_title">Settings</string>
<string name="pref_default_vpn_profile">Default VPN profile</string> <string name="pref_default_vpn_profile">Default VPN profile</string>
<string name="pref_default_vpn_profile_mru">Connect to most recently used profile</string> <string name="pref_default_vpn_profile_mru">Connect to most recently used profile</string>
<string name="pref_power_whitelist_title">Ignore battery optimizations</string>
<string name="pref_power_whitelist_summary">Don\'t show a warning if the app is not on the device\'s power whitelist</string>
<!-- Log view --> <!-- Log view -->
<string name="log_title">日志</string> <string name="log_title">日志</string>

View File

@ -33,6 +33,8 @@
<string name="pref_title">Settings</string> <string name="pref_title">Settings</string>
<string name="pref_default_vpn_profile">Default VPN profile</string> <string name="pref_default_vpn_profile">Default VPN profile</string>
<string name="pref_default_vpn_profile_mru">Connect to most recently used profile</string> <string name="pref_default_vpn_profile_mru">Connect to most recently used profile</string>
<string name="pref_power_whitelist_title">Ignore battery optimizations</string>
<string name="pref_power_whitelist_summary">Don\'t show a warning if the app is not on the device\'s power whitelist</string>
<!-- Log view --> <!-- Log view -->
<string name="log_title">日誌</string> <string name="log_title">日誌</string>

View File

@ -36,6 +36,8 @@
<string name="pref_title">Settings</string> <string name="pref_title">Settings</string>
<string name="pref_default_vpn_profile">Default VPN profile</string> <string name="pref_default_vpn_profile">Default VPN profile</string>
<string name="pref_default_vpn_profile_mru">Connect to most recently used profile</string> <string name="pref_default_vpn_profile_mru">Connect to most recently used profile</string>
<string name="pref_power_whitelist_title">Ignore battery optimizations</string>
<string name="pref_power_whitelist_summary">Don\'t show a warning if the app is not on the device\'s power whitelist</string>
<!-- Log view --> <!-- Log view -->
<string name="log_title">Log</string> <string name="log_title">Log</string>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- <!--
Copyright (C) 2018 Tobias Brunner Copyright (C) 2018-2020 Tobias Brunner
HSR Hochschule fuer Technik Rapperswil HSR Hochschule fuer Technik Rapperswil
This program is free software; you can redistribute it and/or modify it This program is free software; you can redistribute it and/or modify it
@ -20,4 +20,9 @@
android:title="@string/pref_default_vpn_profile" android:title="@string/pref_default_vpn_profile"
android:summary="@string/pref_default_vpn_profile_mru" /> android:summary="@string/pref_default_vpn_profile_mru" />
<SwitchPreference
android:key="pref_ignore_power_whitelist"
android:title="@string/pref_power_whitelist_title"
android:summary="@string/pref_power_whitelist_summary" />
</PreferenceScreen> </PreferenceScreen>