Configure white/black list mode

This commit is contained in:
M66B 2015-10-25 23:04:10 +01:00
parent 2f68d91859
commit 7cab1dc832
6 changed files with 45 additions and 53 deletions

View File

@ -186,20 +186,35 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
MenuItem wifiItem = menu.findItem(R.id.menu_whitelist_wifi);
wifiItem.setChecked(prefs.getBoolean("whitelist_wifi", true));
MenuItem otherItem = menu.findItem(R.id.menu_whitelist_other);
otherItem.setChecked(prefs.getBoolean("whitelist_other", true));
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_wifi:
// Toggle Wi-Fi
if (adapter != null)
adapter.toggle("wifi", this);
case R.id.menu_whitelist_wifi:
prefs.edit().putBoolean("whitelist_wifi", !prefs.getBoolean("whitelist_wifi", true)).apply();
fillApplicationList();
BlackHoleService.reload("wifi", this);
return true;
case R.id.menu_other:
// Toggle other
if (adapter != null)
adapter.toggle("other", this);
case R.id.menu_whitelist_other:
prefs.edit().putBoolean("whitelist_other", !prefs.getBoolean("whitelist_other", true)).apply();
fillApplicationList();
BlackHoleService.reload("other", this);
return true;
case R.id.menu_vpn_settings:

View File

@ -156,4 +156,12 @@ public class BlackHoleService extends VpnService {
super.onRevoke();
}
public static void reload(String name, Context context) {
if ("wifi".equals(name) ? Util.isWifiActive(context) : !Util.isWifiActive(context)) {
Intent intent = new Intent(context, BlackHoleService.class);
intent.putExtra(EXTRA_COMMAND, Command.reload);
context.startService(intent);
}
}
}

View File

@ -6,6 +6,7 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import java.util.ArrayList;
import java.util.Collections;
@ -36,6 +37,7 @@ public class Rule implements Comparable<Rule> {
}
public static List<Rule> getRules(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences wifi = context.getSharedPreferences("wifi", Context.MODE_PRIVATE);
SharedPreferences other = context.getSharedPreferences("other", Context.MODE_PRIVATE);
@ -43,8 +45,8 @@ public class Rule implements Comparable<Rule> {
for (PackageInfo info : context.getPackageManager().getInstalledPackages(0))
listRules.add(new Rule(
info,
wifi.getBoolean(info.packageName, true),
other.getBoolean(info.packageName, true),
wifi.getBoolean(info.packageName, prefs.getBoolean("whitelist_wifi", true)),
other.getBoolean(info.packageName, prefs.getBoolean("whitelist_other", true)),
context
));

View File

@ -85,11 +85,7 @@ 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();
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);
}
BlackHoleService.reload(name, context);
}
};
@ -146,33 +142,6 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
};
}
public void toggle(String name, Context context) {
Log.i(TAG, "Toggle " + name);
// Toggle rule set
SharedPreferences.Editor editor = context.getSharedPreferences(name, Context.MODE_PRIVATE).edit();
for (Rule rule : listSelected) {
if ("wifi".equals(name)) {
rule.wifi_blocked = !rule.wifi_blocked;
editor.putBoolean(rule.info.packageName, rule.wifi_blocked);
} else {
rule.other_blocked = !rule.other_blocked;
editor.putBoolean(rule.info.packageName, rule.other_blocked);
}
}
editor.apply();
// Reload rules
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();
}
@Override
public RuleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.rule, parent, false));

View File

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

View File

@ -2,8 +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 applications</string>
<string name="menu_wifi">Toggle Wi-Fi rules</string>
<string name="menu_other">Toggle mobile rules</string>
<string name="menu_whitelist_wifi">Default block Wi-Fi</string>
<string name="menu_whitelist_other">Default block other</string>
<string name="menu_vpn_settings">Android VPN settings</string>
<string name="menu_support">Support</string>
<string name="menu_about">About</string>