Color code block allowed names

This commit is contained in:
M66B 2016-01-31 09:04:54 +01:00
parent a65e56e218
commit 58cfe19c9c
5 changed files with 86 additions and 19 deletions

View File

@ -20,7 +20,10 @@ package eu.faircode.netguard;
*/
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -36,14 +39,33 @@ public class AccessAdapter extends CursorAdapter {
private int colDaddr;
private int colDPort;
private int colTime;
private int colAllowed;
private int colBlock;
private int colorText;
private int colorPrimary;
private int colorAccent;
public AccessAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
colDaddr = cursor.getColumnIndex("daddr");
colDPort = cursor.getColumnIndex("dport");
colTime = cursor.getColumnIndex("time");
colAllowed = cursor.getColumnIndex("allowed");
colBlock = cursor.getColumnIndex("block");
TypedArray ta = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorSecondary});
try {
colorText = ta.getColor(0, 0);
} finally {
ta.recycle();
}
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
colorPrimary = tv.data;
context.getTheme().resolveAttribute(R.attr.colorAccent, tv, true);
colorAccent = tv.data;
}
@Override
@ -57,6 +79,7 @@ public class AccessAdapter extends CursorAdapter {
String daddr = cursor.getString(colDaddr);
int dport = (cursor.isNull(colDPort) ? -1 : cursor.getInt(colDPort));
long time = cursor.getLong(colTime);
int allowed = cursor.getInt(colAllowed);
int block = cursor.getInt(colBlock);
// Get views
@ -71,5 +94,12 @@ public class AccessAdapter extends CursorAdapter {
else
ivBlock.setImageResource(block > 0 ? R.drawable.host_blocked : R.drawable.host_allowed);
tvDest.setText(daddr + (dport > 0 ? ":" + dport : ""));
if (allowed < 0)
tvDest.setTextColor(colorText);
else if (allowed > 0)
tvDest.setTextColor(colorPrimary);
else
tvDest.setTextColor(colorAccent);
}
}

View File

@ -34,7 +34,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "NetGuard.Database";
private static final String DB_NAME = "Netguard";
private static final int DB_VERSION = 11;
private static final int DB_VERSION = 12;
private static boolean once = true;
private static List<LogChangedListener> logChangedListeners = new ArrayList<>();
@ -108,6 +108,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
", daddr TEXT NOT NULL" +
", dport INTEGER NULL" +
", time INTEGER NOT NULL" +
", allowed INTEGER NULL" +
", block INTEGER NOT NULL" +
");");
db.execSQL("CREATE UNIQUE INDEX idx_access ON access(uid, daddr, dport)");
@ -167,10 +168,10 @@ public class DatabaseHelper extends SQLiteOpenHelper {
createTableAccess(db);
oldVersion = 10;
}
if (oldVersion < 11) {
if (oldVersion < 12) {
db.execSQL("DROP TABLE access");
createTableAccess(db);
oldVersion = 11;
oldVersion = 12;
}
if (oldVersion == DB_VERSION) {
@ -290,6 +291,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
ContentValues cv = new ContentValues();
cv.put("time", packet.time);
cv.put("allowed", packet.allowed ? 1 : 0);
rows = db.update("access", cv, "uid = ? AND daddr = ? AND dport = ?",
new String[]{
@ -325,6 +327,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
ContentValues cv = new ContentValues();
cv.put("block", block);
cv.put("allowed", block < 0 ? -1 : 1 - block);
if (db.update("access", cv, "ID = ?", new String[]{Long.toString(id)}) != 1)
Log.e(TAG, "Set access failed");
@ -371,7 +374,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
public Cursor getAccessUnset(int uid) {
SQLiteDatabase db = this.getReadableDatabase();
return db.query("access", new String[]{"time", "daddr", "dport"},
return db.query("access", new String[]{"daddr", "dport", "time", "allowed"},
"uid = ? AND block < 0", new String[]{Integer.toString(uid)},
null, null, "time DESC");
}

View File

@ -477,8 +477,9 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
public void onItemClick(AdapterView<?> parent, View view, final int bposition, long bid) {
Cursor cursor = (Cursor) badapter.getItem(bposition);
final long id = cursor.getLong(cursor.getColumnIndex("ID"));
final String daddr = cursor.getString(cursor.getColumnIndex("daddr"));
final int dport = (cursor.isNull(cursor.getColumnIndex("dport")) ? -1 : cursor.getInt(cursor.getColumnIndex("dport")));
String daddr = cursor.getString(cursor.getColumnIndex("daddr"));
int dport = (cursor.isNull(cursor.getColumnIndex("dport")) ? -1 : cursor.getInt(cursor.getColumnIndex("dport")));
int block = cursor.getInt(cursor.getColumnIndex("block"));
PopupMenu popup = new PopupMenu(context, view);
popup.inflate(R.menu.access);
@ -505,6 +506,11 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
}
});
if (block == 0)
popup.getMenu().removeItem(R.id.menu_allow);
else if (block == 1)
popup.getMenu().removeItem(R.id.menu_block);
popup.show();
}
});

View File

@ -62,6 +62,7 @@ import android.telephony.TelephonyManager;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.util.TypedValue;
@ -648,19 +649,37 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
}
private void log(Packet packet) {
// Get settings
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this);
boolean log = prefs.getBoolean("log", false);
boolean filter = prefs.getBoolean("filter", false);
boolean notify = prefs.getBoolean("notify_access", false);
boolean system = prefs.getBoolean("manage_system", false);
ResourceRecord rr = resolveDNS(packet.daddr);
// Get name
String dname = null;
if (filter) {
ResourceRecord rr = resolveDNS(packet.daddr);
dname = (rr == null ? null : rr.QName);
} else {
try {
dname = InetAddress.getByName(packet.daddr).toString();
if (dname.startsWith("/"))
dname = dname.substring(1);
} catch (UnknownHostException ex) {
Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
}
// Store
DatabaseHelper dh = new DatabaseHelper(SinkholeService.this);
if (prefs.getBoolean("log", false))
dh.insertLog(packet, rr == null ? null : rr.QName, (last_connected ? last_metered ? 2 : 1 : 0), last_interactive);
if (log)
dh.insertLog(packet, dname, (last_connected ? last_metered ? 2 : 1 : 0), last_interactive);
if (packet.uid > 0) {
if (dh.updateAccess(packet, rr == null ? null : rr.QName))
if (prefs.getBoolean("notify_access", false) &&
(prefs.getBoolean("manage_system", false) ||
!Util.isSystem(packet.uid, SinkholeService.this)))
if (dh.updateAccess(packet, dname))
if (notify && (system || !Util.isSystem(packet.uid, SinkholeService.this)))
showAccessNotification(packet.uid);
} else if (packet.dport != 53)
Log.w(TAG, "Unknown application packet=" + packet);
@ -1485,7 +1504,11 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
PendingIntent pi = PendingIntent.getActivity(SinkholeService.this, uid + 10000, main, PendingIntent.FLAG_UPDATE_CURRENT);
TypedValue tv = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
int colorPrimary = tv.data;
getTheme().resolveAttribute(R.attr.colorAccent, tv, true);
int colorAccent = tv.data;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_cloud_upload_white_24dp)
.setContentTitle(getString(R.string.app_name))
@ -1493,7 +1516,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
.setContentIntent(pi)
.setCategory(Notification.CATEGORY_REMINDER)
.setVisibility(Notification.VISIBILITY_SECRET)
.setColor(tv.data)
.setColor(colorAccent)
.setOngoing(false)
.setAutoCancel(true);
@ -1508,9 +1531,10 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
DatabaseHelper dh = new DatabaseHelper(SinkholeService.this);
Cursor cursor = dh.getAccessUnset(uid);
int colTime = cursor.getColumnIndex("time");
int colDAddr = cursor.getColumnIndex("daddr");
int colDPort = cursor.getColumnIndex("dport");
int colTime = cursor.getColumnIndex("time");
int colAllowed = cursor.getColumnIndex("allowed");
while (cursor.moveToNext()) {
StringBuilder sb = new StringBuilder();
sb.append(df.format(cursor.getLong(colTime))).append(' ');
@ -1520,9 +1544,13 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS
if (dport > 0)
sb.append(':').append(dport);
pos = sb.indexOf(daddr);
sp = new SpannableString(sb);
sp.setSpan(new StyleSpan(Typeface.ITALIC), pos, pos + daddr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
int allowed = cursor.getInt(colAllowed);
if (allowed >= 0) {
pos = sb.indexOf(daddr);
sp = new SpannableString(sb);
ForegroundColorSpan fgsp = new ForegroundColorSpan(allowed > 0 ? colorPrimary : colorAccent);
sp.setSpan(fgsp, pos, pos + daddr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
notification.addLine(sp);
}
cursor.close();

View File

@ -103,7 +103,7 @@ however it is impossible to guarantee NetGuard will work correctly on every devi
<string name="msg_revoked">NetGuard has been disabled, likely by using another VPN based application</string>
<string name="msg_error">NetGuard has been disabled, because of an internal error</string>
<string name="msg_installed">\'%1$s\' installed</string>
<string name="msg_access">%1$s accessed the internet</string>
<string name="msg_access">%1$s attempted internet access</string>
<string name="msg_completed">Action completed</string>
<string name="msg_vpn">NetGuard uses a local VPN as a sinkhole to block internet traffic.
For this reason, please allow a VPN connection in the next dialog.