1
0
Fork 0
mirror of https://github.com/M66B/NetGuard.git synced 2024-12-26 09:48:46 +00:00

Refactoring

This commit is contained in:
M66B 2016-02-21 12:04:13 +01:00
parent 7544f3cdc7
commit 6f51464651
3 changed files with 120 additions and 77 deletions

View file

@ -150,6 +150,8 @@ public class ActivityLog extends AppCompatActivity implements SharedPreferences.
PackageManager pm = getPackageManager();
Cursor cursor = (Cursor) adapter.getItem(position);
long time = cursor.getLong(cursor.getColumnIndex("time"));
int version = cursor.getInt(cursor.getColumnIndex("version"));
int protocol = cursor.getInt(cursor.getColumnIndex("protocol"));
final String daddr = cursor.getString(cursor.getColumnIndex("daddr"));
final int dport = (cursor.isNull(cursor.getColumnIndex("dport")) ? -1 : cursor.getInt(cursor.getColumnIndex("dport")));
final String saddr = cursor.getString(cursor.getColumnIndex("saddr"));
@ -176,37 +178,57 @@ public class ActivityLog extends AppCompatActivity implements SharedPreferences.
// Build popup menu
PopupMenu popup = new PopupMenu(ActivityLog.this, findViewById(R.id.vwPopupAnchor));
popup.inflate(R.menu.log);
// Application name
if (uid >= 0)
popup.getMenu().add(Menu.NONE, 1, 1, TextUtils.join(", ", Util.getApplicationNames(uid, ActivityLog.this)));
popup.getMenu().findItem(R.id.menu_application).setTitle(TextUtils.join(", ", Util.getApplicationNames(uid, ActivityLog.this)));
else
popup.getMenu().removeItem(R.id.menu_application);
// Destination IP
popup.getMenu().findItem(R.id.menu_protocol).setTitle(Util.getProtocolName(protocol, version, false));
// Whois
final Intent lookupIP = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.tcpiputils.com/whois-lookup/" + ip));
popup.getMenu().add(Menu.NONE, 2, 2, getString(R.string.title_log_whois, ip))
.setEnabled(pm.resolveActivity(lookupIP, 0) != null);
if (pm.resolveActivity(lookupIP, 0) == null)
popup.getMenu().removeItem(R.id.menu_whois);
else
popup.getMenu().findItem(R.id.menu_whois).setTitle(getString(R.string.title_log_whois, ip));
// Lookup port
final Intent lookupPort = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.speedguide.net/port.php?port=" + port));
if (port > 0)
popup.getMenu().add(Menu.NONE, 3, 3, getString(R.string.title_log_port, dport))
.setEnabled(pm.resolveActivity(lookupPort, 0) != null);
if (port <= 0 || pm.resolveActivity(lookupPort, 0) == null)
popup.getMenu().removeItem(R.id.menu_port);
else
popup.getMenu().findItem(R.id.menu_port).setTitle(getString(R.string.title_log_port, dport));
popup.getMenu().add(Menu.NONE, 4, 4, SimpleDateFormat.getDateTimeInstance().format(time))
.setEnabled(false);
// Time
popup.getMenu().findItem(R.id.menu_time).setTitle(SimpleDateFormat.getDateTimeInstance().format(time));
// Handle click
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
if (menuItem.getItemId() == 1) {
Intent main = new Intent(ActivityLog.this, ActivityMain.class);
main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(uid));
startActivity(main);
} else if (menuItem.getItemId() == 2)
startActivity(lookupIP);
else if (menuItem.getItemId() == 3)
startActivity(lookupPort);
return false;
switch (menuItem.getItemId()) {
case R.id.menu_application:
Intent main = new Intent(ActivityLog.this, ActivityMain.class);
main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(uid));
startActivity(main);
return true;
case R.id.menu_whois:
startActivity(lookupIP);
return true;
case R.id.menu_port:
startActivity(lookupPort);
return true;
default:
return false;
}
}
});
// Show
popup.show();
}
});
@ -260,7 +282,7 @@ public class ActivityLog extends AppCompatActivity implements SharedPreferences.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.log, menu);
inflater.inflate(R.menu.logging, menu);
menuSearch = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuSearch);

View file

@ -1,67 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:netguard="http://schemas.android.com/apk/res-auto">
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_search"
android:icon="@drawable/ic_search_white_24dp"
android:title="@string/menu_search"
netguard:actionViewClass="android.support.v7.widget.SearchView"
netguard:showAsAction="always|collapseActionView" />
<item
android:id="@+id/menu_filter"
android:icon="@drawable/ic_filter_list_white_24dp"
android:title="@string/menu_filter"
netguard:showAsAction="always|collapseActionView">
<menu>
<item
android:id="@+id/menu_protocol_udp"
android:checkable="true"
android:title="@string/menu_protocol_udp" />
<item
android:id="@+id/menu_protocol_tcp"
android:checkable="true"
android:title="@string/menu_protocol_tcp" />
<item
android:id="@+id/menu_protocol_other"
android:checkable="true"
android:title="@string/menu_protocol_other" />
<item
android:id="@+id/menu_traffic_allowed"
android:checkable="true"
android:title="@string/menu_traffic_allowed" />
<item
android:id="@+id/menu_traffic_blocked"
android:checkable="true"
android:title="@string/menu_traffic_blocked" />
</menu>
</item>
<item
android:id="@+id/menu_log_live"
android:checkable="true"
android:checked="true"
android:title="@string/menu_live" />
<item
android:id="@+id/menu_refresh"
android:id="@+id/menu_application"
android:enabled="false"
android:title="@string/menu_refresh" />
android:title="" />
<item
android:id="@+id/menu_log_resolve"
android:checkable="true"
android:checked="false"
android:title="@string/menu_resolve" />
android:id="@+id/menu_protocol"
android:enabled="false"
android:title="" />
<item
android:id="@+id/menu_pcap_enabled"
android:checkable="true"
android:title="@string/menu_pcap_enabled" />
android:id="@+id/menu_whois"
android:title="" />
<item
android:id="@+id/menu_pcap_export"
android:title="@string/menu_pcap_export" />
android:id="@+id/menu_port"
android:title="" />
<item
android:id="@+id/menu_log_clear"
android:title="@string/menu_clear" />
<item
android:id="@+id/menu_log_support"
android:title="@string/menu_support" />
android:id="@+id/menu_time"
android:enabled="false"
android:title="" />
</menu>

View file

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:netguard="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_search"
android:icon="@drawable/ic_search_white_24dp"
android:title="@string/menu_search"
netguard:actionViewClass="android.support.v7.widget.SearchView"
netguard:showAsAction="always|collapseActionView" />
<item
android:id="@+id/menu_filter"
android:icon="@drawable/ic_filter_list_white_24dp"
android:title="@string/menu_filter"
netguard:showAsAction="always|collapseActionView">
<menu>
<item
android:id="@+id/menu_protocol_udp"
android:checkable="true"
android:title="@string/menu_protocol_udp" />
<item
android:id="@+id/menu_protocol_tcp"
android:checkable="true"
android:title="@string/menu_protocol_tcp" />
<item
android:id="@+id/menu_protocol_other"
android:checkable="true"
android:title="@string/menu_protocol_other" />
<item
android:id="@+id/menu_traffic_allowed"
android:checkable="true"
android:title="@string/menu_traffic_allowed" />
<item
android:id="@+id/menu_traffic_blocked"
android:checkable="true"
android:title="@string/menu_traffic_blocked" />
</menu>
</item>
<item
android:id="@+id/menu_log_live"
android:checkable="true"
android:checked="true"
android:title="@string/menu_live" />
<item
android:id="@+id/menu_refresh"
android:enabled="false"
android:title="@string/menu_refresh" />
<item
android:id="@+id/menu_log_resolve"
android:checkable="true"
android:checked="false"
android:title="@string/menu_resolve" />
<item
android:id="@+id/menu_pcap_enabled"
android:checkable="true"
android:title="@string/menu_pcap_enabled" />
<item
android:id="@+id/menu_pcap_export"
android:title="@string/menu_pcap_export" />
<item
android:id="@+id/menu_log_clear"
android:title="@string/menu_clear" />
<item
android:id="@+id/menu_log_support"
android:title="@string/menu_support" />
</menu>