Reload rules when needed only

This commit is contained in:
M66B 2015-10-25 17:12:25 +01:00
parent d3abc323b5
commit a5a60e20b9
3 changed files with 20 additions and 10 deletions

View File

@ -8,7 +8,6 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.VpnService;
import android.os.ParcelFileDescriptor;
import android.preference.PreferenceManager;
@ -55,9 +54,7 @@ public class BlackHoleService extends VpnService {
Log.i(TAG, "Starting");
// Check if Wi-Fi
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
boolean wifi = (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI);
boolean wifi = Util.isWifiActive(this);
Log.i(TAG, "wifi=" + wifi);
// Build VPN service

View File

@ -73,9 +73,11 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
SharedPreferences prefs = context.getSharedPreferences(name, Context.MODE_PRIVATE);
prefs.edit().putBoolean(rule.info.packageName, isChecked).apply();
Intent intent = new Intent(context, BlackHoleService.class);
intent.putExtra(BlackHoleService.EXTRA_COMMAND, BlackHoleService.Command.reload);
context.startService(intent);
if ("wifi".equals(name) ? Util.isWifiActive(context) : !Util.isWifiActive(context)) {
Intent intent = new Intent(context, BlackHoleService.class);
intent.putExtra(BlackHoleService.EXTRA_COMMAND, BlackHoleService.Command.reload);
context.startService(intent);
}
}
};
@ -141,9 +143,11 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
editor.apply();
// Reload rules
Intent intent = new Intent(context, BlackHoleService.class);
intent.putExtra(BlackHoleService.EXTRA_COMMAND, BlackHoleService.Command.reload);
context.startService(intent);
if ("wifi".equals(name) ? Util.isWifiActive(context) : !Util.isWifiActive(context)) {
Intent intent = new Intent(context, BlackHoleService.class);
intent.putExtra(BlackHoleService.EXTRA_COMMAND, BlackHoleService.Command.reload);
context.startService(intent);
}
// Update UI
notifyDataSetChanged();

View File

@ -4,6 +4,8 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
@ -19,6 +21,13 @@ public class Util {
}
}
public static boolean isWifiActive(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
return (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI);
}
public static void logExtras(String tag, Intent intent) {
logBundle(tag, intent.getExtras());
}