mirror of
https://github.com/M66B/NetGuard.git
synced 2025-03-15 16:29:42 +00:00
Improvements
This commit is contained in:
parent
2cbd96bcb8
commit
2f3e8f4e3f
6 changed files with 25 additions and 14 deletions
|
@ -9,7 +9,7 @@ android {
|
|||
minSdkVersion 21
|
||||
targetSdkVersion 23
|
||||
versionCode 1
|
||||
versionName "0.2"
|
||||
versionName "0.1"
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
|
|
|
@ -35,6 +35,7 @@ public class ActivityMain extends AppCompatActivity {
|
|||
|
||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
|
||||
// On/off switch
|
||||
Switch swEnabled = (Switch) view.findViewById(R.id.swEnabled);
|
||||
swEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
|
@ -61,6 +62,7 @@ public class ActivityMain extends AppCompatActivity {
|
|||
});
|
||||
swEnabled.setChecked(prefs.getBoolean("enabled", false));
|
||||
|
||||
// Package list
|
||||
final RecyclerView rvApplication = (RecyclerView) findViewById(R.id.rvApplication);
|
||||
rvApplication.setHasFixedSize(true);
|
||||
rvApplication.setLayoutManager(new LinearLayoutManager(this));
|
||||
|
|
|
@ -10,6 +10,7 @@ import android.net.NetworkInfo;
|
|||
import android.net.VpnService;
|
||||
import android.os.IBinder;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
|
@ -29,12 +30,14 @@ public class BlackHoleService extends VpnService implements Runnable {
|
|||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Log.i(TAG, "Start");
|
||||
Log.i(TAG, "Start intent=" + intent);
|
||||
|
||||
if (thread != null)
|
||||
thread.interrupt();
|
||||
|
||||
boolean enabled = intent.hasExtra(EXTRA_START) && intent.getBooleanExtra(EXTRA_START, false);
|
||||
boolean enabled = (intent != null &&
|
||||
intent.hasExtra(EXTRA_START) &&
|
||||
intent.getBooleanExtra(EXTRA_START, false));
|
||||
|
||||
if (enabled) {
|
||||
Log.i(TAG, "Starting");
|
||||
|
@ -49,6 +52,7 @@ public class BlackHoleService extends VpnService implements Runnable {
|
|||
private BroadcastReceiver connectivityChangedReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("enabled", false))
|
||||
if (intent.hasExtra(ConnectivityManager.EXTRA_NETWORK_TYPE) &&
|
||||
intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_DUMMY) == ConnectivityManager.TYPE_WIFI) {
|
||||
Intent service = new Intent(BlackHoleService.this, BlackHoleService.class);
|
||||
|
@ -62,6 +66,7 @@ public class BlackHoleService extends VpnService implements Runnable {
|
|||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
Log.i(TAG, "Create");
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||
registerReceiver(connectivityChangedReceiver, intentFilter);
|
||||
|
@ -89,11 +94,13 @@ public class BlackHoleService extends VpnService implements Runnable {
|
|||
Log.i(TAG, "Run");
|
||||
ParcelFileDescriptor pfd = null;
|
||||
try {
|
||||
// Check if Wi-Fi connection
|
||||
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo ni = cm.getActiveNetworkInfo();
|
||||
boolean wifi = (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI);
|
||||
Log.i(TAG, "wifi=" + wifi);
|
||||
|
||||
// Build VPN service
|
||||
final Builder builder = new Builder();
|
||||
builder.setSession("BlackHoleService");
|
||||
builder.addAddress("10.1.10.1", 32);
|
||||
|
@ -111,10 +118,12 @@ public class BlackHoleService extends VpnService implements Runnable {
|
|||
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
builder.setConfigureIntent(pi);
|
||||
|
||||
// Start VPN service
|
||||
pfd = builder.establish();
|
||||
FileInputStream in = new FileInputStream(pfd.getFileDescriptor());
|
||||
|
||||
// Drop all packets
|
||||
Log.i(TAG, "Loop start");
|
||||
FileInputStream in = new FileInputStream(pfd.getFileDescriptor());
|
||||
while (!thread.isInterrupted())
|
||||
in.skip(32768);
|
||||
Log.i(TAG, "Loop exit");
|
||||
|
|
|
@ -30,7 +30,7 @@ public class Rule implements Comparable<Rule> {
|
|||
for (PackageInfo info : context.getPackageManager().getInstalledPackages(0))
|
||||
listRules.add(new Rule(
|
||||
info,
|
||||
wifi.getBoolean(info.packageName, true),
|
||||
wifi.getBoolean(info.packageName, false),
|
||||
other.getBoolean(info.packageName, true),
|
||||
context
|
||||
));
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:src="@drawable/ic_network_cell_black_24dp"
|
||||
android:tint="@color/colorAccent" />
|
||||
android:tint="@color/colorPrimary" />
|
|
@ -1,4 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:src="@drawable/ic_network_wifi_black_24dp"
|
||||
android:tint="@color/colorAccent" />
|
||||
android:tint="@color/colorPrimary" />
|
Loading…
Add table
Reference in a new issue