diff --git a/FAQ.md b/FAQ.md index 729d732c..a3b58f4c 100644 --- a/FAQ.md +++ b/FAQ.md @@ -166,16 +166,16 @@ when connectivity changes from Wi-Fi/mobile, screen on/off and roaming/not roami **(21) Does doze mode affect NetGuard?** I am not sure, because the [doze mode documentation](http://developer.android.com/training/monitoring-device-state/doze-standby.html) -is not clear if broadcast receivers will be disabled in doze mode. -If broadcast receivers are being disabled, then the rules might not be reloaded at the correct time or not at all -when connectivity changes from Wi-Fi to mobile or the other way around. +is not clear if the [Android VPN service](http://developer.android.com/reference/android/net/VpnService.html) will be affected. + To be sure you can disable battery optimizations for NetGuard manually like this: ``` Android settings > Battery > three dot menu > Battery optimizations > Dropdown > All apps > NetGuard > Don't optimize > Done ``` -This cannot be done from the application, because NetGuard is not an application type allowed to do this. +This cannot be done from the application, +because according to Google NetGuard is [not an application type allowed to do this](http://developer.android.com/training/monitoring-device-state/doze-standby.html#whitelisting-cases). **(22) Can I tether / use Wi-Fi calling while using NetGuard?** diff --git a/app/src/main/java/eu/faircode/netguard/ActivityMain.java b/app/src/main/java/eu/faircode/netguard/ActivityMain.java index 55fae6ef..8a577c92 100644 --- a/app/src/main/java/eu/faircode/netguard/ActivityMain.java +++ b/app/src/main/java/eu/faircode/netguard/ActivityMain.java @@ -32,7 +32,9 @@ import android.net.Uri; import android.net.VpnService; import android.os.AsyncTask; import android.os.Build; +import android.os.PowerManager; import android.preference.PreferenceManager; +import android.provider.Settings; import android.support.annotation.NonNull; import android.support.v4.content.LocalBroadcastManager; import android.support.v4.graphics.drawable.DrawableCompat; @@ -55,6 +57,7 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.Button; +import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.TextView; @@ -75,6 +78,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences private MenuItem menuSearch = null; private AlertDialog dialogFirst = null; private AlertDialog dialogVpn = null; + private AlertDialog dialogDoze = null; private AlertDialog dialogLegend = null; private AlertDialog dialogAbout = null; @@ -219,6 +223,8 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences ServiceSinkhole.stop("switch off", ActivityMain.this); } }); + if (enabled) + checkDoze(); // Network is metered ivMetered.setOnLongClickListener(new View.OnLongClickListener() { @@ -422,6 +428,10 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences dialogVpn.dismiss(); dialogVpn = null; } + if (dialogDoze != null) { + dialogDoze.dismiss(); + dialogDoze = null; + } if (dialogLegend != null) { dialogLegend.dismiss(); dialogLegend = null; @@ -448,8 +458,10 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences // Handle VPN approval SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); prefs.edit().putBoolean("enabled", resultCode == RESULT_OK).apply(); - if (resultCode == RESULT_OK) + if (resultCode == RESULT_OK) { ServiceSinkhole.start("prepared", this); + checkDoze(); + } } else if (requestCode == REQUEST_INVITE) { // Do nothing @@ -622,6 +634,44 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences } } + private void checkDoze() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); + if (!pm.isIgnoringBatteryOptimizations(getPackageName())) { + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + if (!prefs.getBoolean("nodoze", false)) { + LayoutInflater inflater = LayoutInflater.from(this); + View view = inflater.inflate(R.layout.doze, null, false); + final CheckBox cbDontAsk = (CheckBox) view.findViewById(R.id.cbDontAsk); + dialogDoze = new AlertDialog.Builder(this) + .setView(view) + .setCancelable(true) + .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + prefs.edit().putBoolean("nodoze", cbDontAsk.isChecked()).apply(); + startActivity(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)); + } + }) + .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + prefs.edit().putBoolean("nodoze", cbDontAsk.isChecked()).apply(); + } + }) + .setOnDismissListener(new DialogInterface.OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialogInterface) { + dialogDoze = null; + } + }) + .create(); + dialogDoze.show(); + } + } + } + } + @Override public boolean onCreateOptionsMenu(Menu menu) { if (Build.VERSION.SDK_INT < MIN_SDK) diff --git a/app/src/main/res/layout/doze.xml b/app/src/main/res/layout/doze.xml new file mode 100644 index 00000000..9d032102 --- /dev/null +++ b/app/src/main/res/layout/doze.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b874e71a..bfcd819f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -159,6 +159,8 @@ Your internet traffic is not being sent to a remote VPN server. %1$7.3f▲ %2$7.3f▼ MB %1$7.3f▲ %2$7.3f▼ GB %dx + For consistent results, the Android battery optimizations should be disabled for NetGuard. +\n\nIn the next dialog, select \"All apps\" at the top, tap on NetGuard in the list and select and confirm \"Don\'t optimize\". Using filtering will cause Android to attribute data and power usage to NetGuard - Android assumes the data and power are being used by NetGuard, rather than the original applications Traffic logging is disabled, use the switch above to enable logging. Traffic logging might result in extra battery usage. This will reset the rules and conditions to their default values @@ -202,6 +204,7 @@ Your internet traffic is not being sent to a remote VPN server. mediaserver nobody Notify internet access attempts + Don\'t ask again Whois %1$s Port %1$d