mirror of
https://github.com/M66B/NetGuard.git
synced 2025-01-01 12:54:07 +00:00
Enable/disable traffic logging using a switch in the action bar
This commit is contained in:
parent
b0319263f8
commit
aac945cb17
6 changed files with 73 additions and 26 deletions
|
@ -30,6 +30,7 @@ import android.preference.PreferenceManager;
|
|||
import android.support.v4.view.MenuItemCompat;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.SearchView;
|
||||
import android.support.v7.widget.SwitchCompat;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.Gravity;
|
||||
|
@ -38,9 +39,11 @@ import android.view.MenuInflater;
|
|||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.FilterQueryProvider;
|
||||
import android.widget.ListView;
|
||||
import android.widget.PopupMenu;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -52,7 +55,7 @@ import java.net.UnknownHostException;
|
|||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class ActivityLog extends AppCompatActivity {
|
||||
public class ActivityLog extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
private static final String TAG = "NetGuard.Log";
|
||||
|
||||
private ListView lvLog;
|
||||
|
@ -91,11 +94,35 @@ public class ActivityLog extends AppCompatActivity {
|
|||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.logview);
|
||||
|
||||
// Action bar
|
||||
View actionView = getLayoutInflater().inflate(R.layout.action, null);
|
||||
SwitchCompat swEnabled = (SwitchCompat) actionView.findViewById(R.id.swEnabled);
|
||||
|
||||
getSupportActionBar().setDisplayShowCustomEnabled(true);
|
||||
getSupportActionBar().setCustomView(actionView);
|
||||
|
||||
getSupportActionBar().setTitle(R.string.menu_log);
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
// Get settings
|
||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
resolve = prefs.getBoolean("resolve", false);
|
||||
boolean log = prefs.getBoolean("log", false);
|
||||
|
||||
// Show disabled message
|
||||
TextView tvDisabled = (TextView) findViewById(R.id.tvDisabled);
|
||||
tvDisabled.setVisibility(log ? View.GONE : View.VISIBLE);
|
||||
|
||||
// Set enabled switch
|
||||
swEnabled.setChecked(log);
|
||||
swEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
prefs.edit().putBoolean("log", isChecked).apply();
|
||||
}
|
||||
});
|
||||
|
||||
// Listen for preference changes
|
||||
prefs.registerOnSharedPreferenceChangeListener(this);
|
||||
|
||||
lvLog = (ListView) findViewById(R.id.lvLog);
|
||||
|
||||
|
@ -212,9 +239,32 @@ public class ActivityLog extends AppCompatActivity {
|
|||
@Override
|
||||
protected void onDestroy() {
|
||||
dh.close();
|
||||
|
||||
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
|
||||
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
|
||||
Log.i(TAG, "Preference " + name + "=" + prefs.getAll().get(name));
|
||||
if ("log".equals(name)) {
|
||||
// Get enabled
|
||||
boolean log = prefs.getBoolean(name, false);
|
||||
|
||||
// Display disabled warning
|
||||
TextView tvDisabled = (TextView) findViewById(R.id.tvDisabled);
|
||||
tvDisabled.setVisibility(log ? View.GONE : View.VISIBLE);
|
||||
|
||||
// Check switch state
|
||||
SwitchCompat swEnabled = (SwitchCompat) getSupportActionBar().getCustomView().findViewById(R.id.swEnabled);
|
||||
if (swEnabled.isChecked() != log)
|
||||
swEnabled.setChecked(log);
|
||||
|
||||
SinkholeService.reload(null, "changed " + name, ActivityLog.this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
MenuInflater inflater = getMenuInflater();
|
||||
|
@ -260,10 +310,9 @@ public class ActivityLog extends AppCompatActivity {
|
|||
boolean pcap_enabled = prefs.getBoolean("pcap", false);
|
||||
boolean export = (getPackageManager().resolveActivity(getIntentPCAPDocument(), 0) != null);
|
||||
|
||||
menu.findItem(R.id.menu_log_enabled).setChecked(log);
|
||||
menu.findItem(R.id.menu_log_resolve).setChecked(resolve);
|
||||
menu.findItem(R.id.menu_pcap_enabled).setChecked(pcap_enabled);
|
||||
menu.findItem(R.id.menu_pcap_enabled).setEnabled(log || filter);
|
||||
menu.findItem(R.id.menu_pcap_enabled).setEnabled(filter);
|
||||
menu.findItem(R.id.menu_pcap_export).setEnabled(pcap_file.exists() && export);
|
||||
|
||||
return super.onPrepareOptionsMenu(menu);
|
||||
|
@ -275,12 +324,6 @@ public class ActivityLog extends AppCompatActivity {
|
|||
final File pcap_file = new File(getCacheDir(), "netguard.pcap");
|
||||
|
||||
switch (item.getItemId()) {
|
||||
case R.id.menu_log_enabled:
|
||||
item.setChecked(!item.isChecked());
|
||||
prefs.edit().putBoolean("log", item.isChecked()).apply();
|
||||
SinkholeService.reload(null, "setting changed", this);
|
||||
return true;
|
||||
|
||||
case R.id.menu_log_live:
|
||||
item.setChecked(!item.isChecked());
|
||||
live = item.isChecked();
|
||||
|
|
|
@ -908,10 +908,13 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
|
|||
|
||||
// Called from native code
|
||||
private void logPacket(Packet packet) {
|
||||
Message msg = mServiceHandler.obtainMessage();
|
||||
msg.obj = packet;
|
||||
msg.what = MSG_PACKET;
|
||||
mServiceHandler.sendMessage(msg);
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
if (prefs.getBoolean("log", false)) {
|
||||
Message msg = mServiceHandler.obtainMessage();
|
||||
msg.obj = packet;
|
||||
msg.what = MSG_PACKET;
|
||||
mServiceHandler.sendMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
// Called from native code
|
||||
|
|
|
@ -8,10 +8,22 @@
|
|||
android:paddingStart="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvDisabled"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:padding="8dp"
|
||||
android:text="@string/msg_log_disabled"
|
||||
android:textAppearance="@android:style/TextAppearance.Material.Medium"
|
||||
android:textColor="?attr/colorOff"
|
||||
android:visibility="gone" />
|
||||
|
||||
<ListView
|
||||
android:id="@+id/lvLog"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_below="@id/tvDisabled"
|
||||
android:scrollbarStyle="insideOverlay"
|
||||
android:scrollbars="vertical" />
|
||||
|
||||
|
|
|
@ -7,10 +7,6 @@
|
|||
android:title="@string/menu_search"
|
||||
netguard:actionViewClass="android.support.v7.widget.SearchView"
|
||||
netguard:showAsAction="always|collapseActionView" />
|
||||
<item
|
||||
android:id="@+id/menu_log_enabled"
|
||||
android:checkable="true"
|
||||
android:title="@string/menu_enabled" />
|
||||
<item
|
||||
android:id="@+id/menu_log_live"
|
||||
android:checkable="true"
|
||||
|
|
|
@ -24,7 +24,6 @@ These issues are caused by bugs in Android, or in the software provided by the m
|
|||
<string name="menu_support">Support</string>
|
||||
<string name="menu_about">About</string>
|
||||
|
||||
<string name="menu_enabled">Log enabled</string>
|
||||
<string name="menu_live">Live updates</string>
|
||||
<string name="menu_resolve">Resolve host names</string>
|
||||
<string name="menu_pcap_enabled">PCAP enabled</string>
|
||||
|
@ -55,7 +54,6 @@ These issues are caused by bugs in Android, or in the software provided by the m
|
|||
|
||||
<string name="setting_advanced_options">Advanced options</string>
|
||||
<string name="setting_system">Manage system applications</string>
|
||||
<string name="setting_log">Log traffic</string>
|
||||
<string name="setting_filter">Filter traffic</string>
|
||||
<string name="setting_block_domains">Block domain names</string>
|
||||
<string name="setting_vpn4">VPN IPv4: %s</string>
|
||||
|
@ -90,7 +88,6 @@ These issues are caused by bugs in Android, or in the software provided by the m
|
|||
<string name="summary_national_roaming">Do not apply roaming rules when the SIM and mobile network country are the same</string>
|
||||
|
||||
<string name="summary_system">Define rules for system applications (for experts)</string>
|
||||
<string name="summary_log">Log addresses of IP packets going into the VPN sinkhole. This might result in extra battery usage.</string>
|
||||
<string name="summary_filter">Filter IP packets going out of the VPN sinkhole. This might result in extra battery usage.</string>
|
||||
<string name="summary_block_domains">Redirect blocked domain names to local device. This switch is disabled when no hosts file has been imported.</string>
|
||||
|
||||
|
@ -117,6 +114,7 @@ Since NetGuard has no internet permission, you know your internet traffic is not
|
|||
<string name="msg_kbsec">%7.3f KB/s</string>
|
||||
<string name="msg_mbsec">%7.3f MB/s</string>
|
||||
<string name="msg_filter">Using filtering will cause Android to attribute data and power usage to NetGuard - Android assumes the data and power are being used by NetGuard, rather than the original applications</string>
|
||||
<string name="msg_log_disabled">Traffic logging is disabled, use the switch above to enable logging. Traffic logging might result in extra battery usage.</string>
|
||||
|
||||
<string name="title_screen_wifi">Allow Wi-Fi when screen is on</string>
|
||||
<string name="title_screen_other">Allow mobile when screen is on</string>
|
||||
|
|
|
@ -95,11 +95,6 @@
|
|||
android:key="manage_system"
|
||||
android:summary="@string/summary_system"
|
||||
android:title="@string/setting_system" />
|
||||
<SwitchPreference
|
||||
android:defaultValue="false"
|
||||
android:key="log"
|
||||
android:summary="@string/summary_log"
|
||||
android:title="@string/setting_log" />
|
||||
<SwitchPreference
|
||||
android:defaultValue="false"
|
||||
android:key="filter"
|
||||
|
|
Loading…
Reference in a new issue