Check for Doze mode

This commit is contained in:
M66B 2016-03-15 08:05:48 +01:00
parent 3a56c6d216
commit acff5f6994
4 changed files with 114 additions and 5 deletions

8
FAQ.md
View File

@ -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).
<a name="FAQ22"></a>
**(22) Can I tether / use Wi-Fi calling while using NetGuard?**

View File

@ -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)

View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="@string/app_name"
android:textAppearance="@style/TextLarge"
android:textStyle="bold" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/msg_doze"
android:textAppearance="@style/TextMedium" />
<CheckBox
android:id="@+id/cbDontAsk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/title_dontask"
android:textAppearance="@style/TextMedium" />
</LinearLayout>
</ScrollView>
</LinearLayout>

View File

@ -159,6 +159,8 @@ Your internet traffic is not being sent to a remote VPN server.</string>
<string name="msg_mb">%1$7.3f&#9650; %2$7.3f&#9660; MB</string>
<string name="msg_gb">%1$7.3f&#9650; %2$7.3f&#9660; GB</string>
<string name="msg_count">%dx</string>
<string name="msg_doze">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\".</string>
<string name="msg_filter">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</string>
<string name="msg_log_disabled">Traffic logging is disabled, use the switch above to enable logging. Traffic logging might result in extra battery usage.</string>
<string name="msg_clear_rules">This will reset the rules and conditions to their default values</string>
@ -202,6 +204,7 @@ Your internet traffic is not being sent to a remote VPN server.</string>
<string name="title_mediaserver">mediaserver</string>
<string name="title_nobody">nobody</string>
<string name="title_notify">Notify internet access attempts</string>
<string name="title_dontask">Don\'t ask again</string>
<string name="title_log_whois">Whois %1$s</string>
<string name="title_log_port">Port %1$d</string>