Used metered network instead of wifi active indication

This commit is contained in:
M66B 2015-11-04 13:04:27 +01:00
parent b7e6000393
commit b03b40bab3
3 changed files with 17 additions and 16 deletions

View File

@ -326,12 +326,13 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
ivOther.setVisibility(View.GONE);
ivRoaming.setVisibility(View.GONE);
if (Util.isWifiActive(context))
ivWifi.setVisibility(View.VISIBLE);
else if (Util.isRoaming(context))
if (Util.isMetered(context)) {
if (Util.isRoaming(context))
ivRoaming.setVisibility(View.VISIBLE);
else
ivOther.setVisibility(View.VISIBLE);
} else
ivWifi.setVisibility(View.VISIBLE);
actionView.postInvalidate();
}

View File

@ -110,17 +110,12 @@ public class SinkholeService extends VpnService {
private ParcelFileDescriptor startVPN() {
Log.i(TAG, "Starting");
// Check if Wi-Fi
// Check state
boolean metered = Util.isMetered(this);
boolean wifi = Util.isWifiActive(this);
Log.i(TAG, "wifi=" + wifi);
// Check if Wi-Fi
boolean roaming = Util.isRoaming(this);
Log.i(TAG, "roaming=" + roaming);
// Check if interactive
boolean interactive = Util.isInteractive(this);
Log.i(TAG, "interactive=" + interactive);
Log.i(TAG, "metered=" + metered + " wifi=" + wifi + " roaming=" + roaming + " interactive=" + interactive);
// Build VPN service
final Builder builder = new Builder();
@ -132,8 +127,8 @@ public class SinkholeService extends VpnService {
// Add list of allowed applications
for (Rule rule : Rule.getRules(true, TAG, this)) {
boolean blocked = (wifi ? rule.wifi_blocked : rule.other_blocked);
if ((!blocked || (rule.unused && interactive)) && (wifi || !(rule.roaming && roaming))) {
boolean blocked = (metered ? rule.other_blocked : rule.wifi_blocked);
if ((!blocked || (rule.unused && interactive)) && (!metered || !(rule.roaming && roaming))) {
Log.i(TAG, "Allowing " + rule.info.packageName);
try {
builder.addDisallowedApplication(rule.info.packageName);
@ -374,7 +369,7 @@ public class SinkholeService extends VpnService {
}
public static void reload(String network, Context context) {
if (network == null || ("wifi".equals(network) ? Util.isWifiActive(context) : !Util.isWifiActive(context))) {
if (network == null || ("wifi".equals(network) ? !Util.isMetered(context) : Util.isMetered(context))) {
Intent intent = new Intent(context, SinkholeService.class);
intent.putExtra(EXTRA_COMMAND, Command.reload);
context.startService(intent);

View File

@ -69,6 +69,11 @@ public class Util {
}
public static boolean isMetered(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.isActiveNetworkMetered();
}
public static boolean isInteractive(Context context) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
return pm.isInteractive();