Switch to blocking, add action toggle all

This commit is contained in:
M66B 2015-10-25 12:52:54 +01:00
parent c2c5bd2004
commit c3a7a5a844
51 changed files with 77 additions and 20 deletions

View File

@ -120,25 +120,35 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
MenuItem wifiItem = menu.findItem(R.id.menu_wifi);
MenuItem otherItem = menu.findItem(R.id.menu_other);
wifiItem.setIcon(prefs.getBoolean("wifi", true) ? R.drawable.ic_network_wifi_white_24dp : R.drawable.ic_signal_wifi_off_white_24dp);
otherItem.setIcon(prefs.getBoolean("other", true) ? R.drawable.ic_network_cell_white_24dp : R.drawable.ic_signal_cellular_off_white_24dp);
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
applyFilter(query);
if (adapter != null)
adapter.getFilter().filter(query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
applyFilter(newText);
if (adapter != null)
adapter.getFilter().filter(newText);
return true;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
applyFilter(null);
if (adapter != null)
adapter.getFilter().filter(null);
return true;
}
});
@ -146,15 +156,27 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
return true;
}
private void applyFilter(String query) {
if (adapter != null)
adapter.getFilter().filter(query);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
switch (item.getItemId()) {
case R.id.menu_wifi:
boolean wifi = !prefs.getBoolean("wifi", true);
prefs.edit().putBoolean("wifi", wifi).apply();
if (adapter != null)
adapter.set("wifi", wifi, this);
invalidateOptionsMenu();
return true;
case R.id.menu_other:
boolean other = !prefs.getBoolean("other", true);
prefs.edit().putBoolean("other", other).apply();
if (adapter != null)
adapter.set("other", other, this);
invalidateOptionsMenu();
return true;
case R.id.menu_vpn_settings:
Intent intent = new Intent("android.net.vpn.SETTINGS");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

View File

@ -115,7 +115,7 @@ public class BlackHoleService extends VpnService implements Runnable {
builder.setSession("BlackHoleService");
builder.addAddress("10.1.10.1", 32);
builder.addRoute("0.0.0.0", 0);
builder.setBlocking(true);
builder.setBlocking(false);
// Add list of allowed applications
for (Rule rule : Rule.getRules(this))
@ -135,7 +135,11 @@ public class BlackHoleService extends VpnService implements Runnable {
Log.i(TAG, "Loop start thread=" + Thread.currentThread());
FileInputStream in = new FileInputStream(pfd.getFileDescriptor());
while (!Thread.currentThread().isInterrupted() && pfd.getFileDescriptor().valid())
in.skip(32768);
if (in.skip(32768) < 0)
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
Log.i(TAG, "Loop exit thread=" + Thread.currentThread());
} catch (Throwable ex) {

View File

@ -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, false),
wifi.getBoolean(info.packageName, true),
other.getBoolean(info.packageName, true),
context
));

View File

@ -124,6 +124,24 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
};
}
public void set(String name, boolean blocked, Context context) {
SharedPreferences.Editor editor = context.getSharedPreferences(name, Context.MODE_PRIVATE).edit();
for (Rule rule : listSelected) {
if ("wifi".equals(name))
rule.wifi_blocked = blocked;
else
rule.other_blocked = blocked;
editor.putBoolean(rule.info.packageName, blocked);
}
editor.apply();
Intent intent = new Intent(context, BlackHoleService.class);
intent.putExtra(BlackHoleService.EXTRA_COMMAND, BlackHoleService.Command.reload);
context.startService(intent);
notifyDataSetChanged();
}
@Override
public RuleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.rule, parent, false));

Binary file not shown.

Before

Width:  |  Height:  |  Size: 161 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 394 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 427 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 261 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 421 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 273 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 299 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 195 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 201 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 291 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 308 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 193 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 491 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 538 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 289 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 309 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 481 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 528 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 244 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 263 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 715 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 786 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 388 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 435 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 727 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 795 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 278 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 293 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 945 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 463 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 524 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 899 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 994 B

View File

@ -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_signal_cellular_off_black_24dp"
android:src="@drawable/ic_signal_cellular_off_white_24dp"
android:tint="@color/colorAccent" />

View File

@ -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:src="@drawable/ic_network_cell_white_24dp"
android:tint="@color/colorPrimary" />

View File

@ -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_signal_wifi_off_black_24dp"
android:src="@drawable/ic_signal_wifi_off_white_24dp"
android:tint="@color/colorAccent" />

View File

@ -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:src="@drawable/ic_network_wifi_white_24dp"
android:tint="@color/colorPrimary" />

View File

@ -1,11 +1,12 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
android:background="@color/colorPrimary"
android:orientation="horizontal">
<Switch
android:id="@+id/swEnabled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp" />
</RelativeLayout>
android:layout_marginLeft="12dp" />
</LinearLayout>

View File

@ -6,7 +6,17 @@
android:icon="@drawable/ic_search_white_24dp"
android:title="@string/menu_search"
netguard:actionViewClass="android.support.v7.widget.SearchView"
netguard:showAsAction="ifRoom|collapseActionView" />
netguard:showAsAction="always|collapseActionView" />
<item
android:id="@+id/menu_wifi"
android:icon="@drawable/ic_network_wifi_white_24dp"
android:title="@string/menu_wifi"
netguard:showAsAction="always" />
<item
android:id="@+id/menu_other"
android:icon="@drawable/ic_network_cell_white_24dp"
android:title="@string/menu_other"
netguard:showAsAction="always" />
<item
android:id="@+id/menu_vpn_settings"
android:title="@string/menu_vpn_settings" />

View File

@ -2,6 +2,8 @@
<string name="app_name">NetGuard</string>
<string name="app_copyright">Copyright \u00A9 2015 by M. Bokhorst (M66B)</string>
<string name="menu_search">Search</string>
<string name="menu_wifi">Toggle Wi-Fi</string>
<string name="menu_other">Toggle other</string>
<string name="menu_vpn_settings">VPN settings</string>
<string name="menu_about">About</string>
</resources>