mirror of https://github.com/M66B/NetGuard.git
Reload rules when needed only
This commit is contained in:
parent
d3abc323b5
commit
a5a60e20b9
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue