2016-01-05 12:40:02 +00:00
|
|
|
package eu.faircode.netguard;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2016-01-08 07:12:14 +00:00
|
|
|
import android.content.pm.ApplicationInfo;
|
|
|
|
import android.content.pm.PackageManager;
|
2016-01-05 12:40:02 +00:00
|
|
|
import android.database.Cursor;
|
2016-01-06 10:12:37 +00:00
|
|
|
import android.net.Uri;
|
2016-01-19 21:17:40 +00:00
|
|
|
import android.os.AsyncTask;
|
2016-01-05 12:40:02 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.CursorAdapter;
|
2016-01-08 07:12:14 +00:00
|
|
|
import android.widget.ImageView;
|
2016-01-05 12:40:02 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
2016-01-08 07:12:14 +00:00
|
|
|
import com.squareup.picasso.Picasso;
|
|
|
|
|
2016-01-19 21:17:40 +00:00
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.UnknownHostException;
|
2016-01-05 12:40:02 +00:00
|
|
|
import java.text.SimpleDateFormat;
|
2016-01-20 09:45:17 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2016-01-05 12:40:02 +00:00
|
|
|
|
|
|
|
public class LogAdapter extends CursorAdapter {
|
2016-01-22 09:37:57 +00:00
|
|
|
private boolean resolve;
|
2016-01-05 12:40:02 +00:00
|
|
|
private int colTime;
|
2016-01-07 06:47:00 +00:00
|
|
|
private int colVersion;
|
|
|
|
private int colProtocol;
|
2016-01-08 07:03:04 +00:00
|
|
|
private int colFlags;
|
2016-01-22 09:37:57 +00:00
|
|
|
private int colSource;
|
|
|
|
private int colSPort;
|
|
|
|
private int colDest;
|
|
|
|
private int colDPort;
|
2016-01-07 06:47:00 +00:00
|
|
|
private int colUid;
|
2016-01-22 09:37:57 +00:00
|
|
|
private int colAllowed;
|
2016-01-08 16:08:11 +00:00
|
|
|
private int colConnection;
|
2016-01-09 07:36:17 +00:00
|
|
|
private int colInteractive;
|
2016-01-20 09:45:17 +00:00
|
|
|
private Map<String, String> mapIPHost = new HashMap<String, String>();
|
2016-01-05 12:40:02 +00:00
|
|
|
|
2016-01-22 09:37:57 +00:00
|
|
|
public LogAdapter(Context context, Cursor cursor, boolean resolve) {
|
2016-01-05 12:40:02 +00:00
|
|
|
super(context, cursor, 0);
|
2016-01-22 09:37:57 +00:00
|
|
|
this.resolve = resolve;
|
2016-01-05 12:40:02 +00:00
|
|
|
colTime = cursor.getColumnIndex("time");
|
2016-01-07 06:47:00 +00:00
|
|
|
colVersion = cursor.getColumnIndex("version");
|
|
|
|
colProtocol = cursor.getColumnIndex("protocol");
|
2016-01-08 07:03:04 +00:00
|
|
|
colFlags = cursor.getColumnIndex("flags");
|
2016-01-22 09:37:57 +00:00
|
|
|
colSource = cursor.getColumnIndex("saddr");
|
|
|
|
colSPort = cursor.getColumnIndex("sport");
|
|
|
|
colDest = cursor.getColumnIndex("daddr");
|
|
|
|
colDPort = cursor.getColumnIndex("dport");
|
2016-01-07 06:47:00 +00:00
|
|
|
colUid = cursor.getColumnIndex("uid");
|
2016-01-22 09:37:57 +00:00
|
|
|
colAllowed = cursor.getColumnIndex("allowed");
|
2016-01-08 16:08:11 +00:00
|
|
|
colConnection = cursor.getColumnIndex("connection");
|
2016-01-09 07:36:17 +00:00
|
|
|
colInteractive = cursor.getColumnIndex("interactive");
|
2016-01-05 12:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
|
|
|
return LayoutInflater.from(context).inflate(R.layout.log, parent, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void bindView(final View view, final Context context, final Cursor cursor) {
|
|
|
|
// Get values
|
2016-01-06 10:12:37 +00:00
|
|
|
long time = cursor.getLong(colTime);
|
2016-01-07 06:47:00 +00:00
|
|
|
int version = (cursor.isNull(colVersion) ? -1 : cursor.getInt(colVersion));
|
|
|
|
int protocol = (cursor.isNull(colProtocol) ? -1 : cursor.getInt(colProtocol));
|
2016-01-08 07:03:04 +00:00
|
|
|
String flags = cursor.getString(colFlags);
|
2016-01-22 09:37:57 +00:00
|
|
|
String source = cursor.getString(colSource);
|
|
|
|
int sport = (cursor.isNull(colSPort) ? -1 : cursor.getInt(colSPort));
|
|
|
|
final String dest = cursor.getString(colDest);
|
|
|
|
int dport = (cursor.isNull(colDPort) ? -1 : cursor.getInt(colDPort));
|
2016-01-10 13:26:37 +00:00
|
|
|
int uid = (cursor.isNull(colUid) ? -1 : cursor.getInt(colUid));
|
2016-01-22 09:37:57 +00:00
|
|
|
int allowed = (cursor.isNull(colAllowed) ? -1 : cursor.getInt(colAllowed));
|
2016-01-09 07:36:17 +00:00
|
|
|
int connection = (cursor.isNull(colConnection) ? -1 : cursor.getInt(colConnection));
|
|
|
|
int interactive = (cursor.isNull(colInteractive) ? -1 : cursor.getInt(colInteractive));
|
2016-01-05 12:40:02 +00:00
|
|
|
|
|
|
|
// Get views
|
|
|
|
TextView tvTime = (TextView) view.findViewById(R.id.tvTime);
|
2016-01-07 06:47:00 +00:00
|
|
|
TextView tvProtocol = (TextView) view.findViewById(R.id.tvProtocol);
|
2016-01-08 07:03:04 +00:00
|
|
|
TextView tvFlags = (TextView) view.findViewById(R.id.tvFlags);
|
2016-01-22 09:37:57 +00:00
|
|
|
final TextView tvSource = (TextView) view.findViewById(R.id.tvSource);
|
|
|
|
TextView tvSPort = (TextView) view.findViewById(R.id.tvSPort);
|
|
|
|
final TextView tvDest = (TextView) view.findViewById(R.id.tvDest);
|
|
|
|
TextView tvDPort = (TextView) view.findViewById(R.id.tvDPort);
|
2016-01-08 16:21:50 +00:00
|
|
|
ImageView ivIcon = (ImageView) view.findViewById(R.id.ivIcon);
|
2016-01-07 06:47:00 +00:00
|
|
|
TextView tvUid = (TextView) view.findViewById(R.id.tvUid);
|
2016-01-22 09:37:57 +00:00
|
|
|
ImageView ivConnection = (ImageView) view.findViewById(R.id.ivConnection);
|
|
|
|
ImageView ivInteractive = (ImageView) view.findViewById(R.id.ivInteractive);
|
2016-01-08 16:21:50 +00:00
|
|
|
|
|
|
|
// Set values
|
2016-01-09 07:36:17 +00:00
|
|
|
tvTime.setText(new SimpleDateFormat("HH:mm:ss").format(time));
|
|
|
|
|
|
|
|
if (connection <= 0)
|
|
|
|
ivConnection.setImageDrawable(null);
|
2016-01-19 19:58:51 +00:00
|
|
|
else {
|
|
|
|
if (allowed > 0)
|
|
|
|
ivConnection.setImageResource(connection == 1 ? R.drawable.wifi_on : R.drawable.other_on);
|
|
|
|
else
|
|
|
|
ivConnection.setImageResource(connection == 1 ? R.drawable.wifi_off : R.drawable.other_off);
|
|
|
|
}
|
2016-01-09 07:36:17 +00:00
|
|
|
|
|
|
|
if (interactive <= 0)
|
|
|
|
ivInteractive.setImageDrawable(null);
|
|
|
|
else
|
|
|
|
ivInteractive.setImageResource(R.drawable.screen_on);
|
2016-01-08 16:21:50 +00:00
|
|
|
|
2016-01-09 18:53:28 +00:00
|
|
|
if (protocol == 1) // ICMP
|
2016-01-08 16:21:50 +00:00
|
|
|
tvProtocol.setText("I");
|
2016-01-09 18:53:28 +00:00
|
|
|
else if (protocol == 6) // TCP
|
2016-01-08 16:21:50 +00:00
|
|
|
tvProtocol.setText("T");
|
2016-01-09 18:53:28 +00:00
|
|
|
else if (protocol == 17) // UDP
|
2016-01-08 16:21:50 +00:00
|
|
|
tvProtocol.setText("U");
|
|
|
|
else
|
|
|
|
tvProtocol.setText(protocol < 0 ? "" : Integer.toString(protocol));
|
|
|
|
|
|
|
|
tvFlags.setText(flags);
|
2016-01-05 12:40:02 +00:00
|
|
|
|
2016-01-22 09:37:57 +00:00
|
|
|
tvSPort.setText(sport < 0 ? "" : Integer.toString(sport));
|
|
|
|
tvDPort.setText(dport < 0 ? "" : Integer.toString(dport));
|
|
|
|
|
2016-01-08 07:12:14 +00:00
|
|
|
// Application icon
|
|
|
|
ApplicationInfo info = null;
|
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
String[] pkg = pm.getPackagesForUid(uid);
|
|
|
|
if (pkg != null && pkg.length > 0)
|
|
|
|
try {
|
|
|
|
info = pm.getApplicationInfo(pkg[0], 0);
|
|
|
|
} catch (PackageManager.NameNotFoundException ignored) {
|
|
|
|
}
|
2016-01-10 13:13:36 +00:00
|
|
|
if (info == null)
|
2016-01-08 11:14:31 +00:00
|
|
|
ivIcon.setImageDrawable(null);
|
2016-01-10 13:13:36 +00:00
|
|
|
else if (info.icon == 0)
|
|
|
|
Picasso.with(context).load(android.R.drawable.sym_def_app_icon).into(ivIcon);
|
2016-01-08 07:12:14 +00:00
|
|
|
else {
|
|
|
|
Uri uri = Uri.parse("android.resource://" + info.packageName + "/" + info.icon);
|
|
|
|
Picasso.with(context).load(uri).into(ivIcon);
|
|
|
|
}
|
|
|
|
|
2016-01-10 13:26:37 +00:00
|
|
|
// https://android.googlesource.com/platform/system/core/+/master/include/private/android_filesystem_config.h
|
|
|
|
uid = uid % 100000; // strip off user ID
|
|
|
|
if (uid == -1)
|
|
|
|
tvUid.setText("");
|
|
|
|
else if (uid == 0)
|
|
|
|
tvUid.setText("root");
|
|
|
|
else if (uid == 9999)
|
|
|
|
tvUid.setText("-"); // nobody
|
|
|
|
else
|
|
|
|
tvUid.setText(Integer.toString(uid));
|
2016-01-08 16:08:11 +00:00
|
|
|
|
2016-01-10 08:35:00 +00:00
|
|
|
// tvProtocol.setText("99");
|
2016-01-10 13:26:37 +00:00
|
|
|
// tvPort.setText("88888");
|
2016-01-10 08:35:00 +00:00
|
|
|
// tvFlags.setText("+APFR");
|
|
|
|
// tvUid.setText("18888");
|
|
|
|
|
2016-01-22 09:37:57 +00:00
|
|
|
// TODO resolve source when inbound
|
|
|
|
|
|
|
|
tvSource.setText(source);
|
|
|
|
if (resolve)
|
|
|
|
synchronized (mapIPHost) {
|
|
|
|
if (mapIPHost.containsKey(dest))
|
|
|
|
tvDest.setText(mapIPHost.get(dest));
|
|
|
|
else {
|
|
|
|
tvDest.setText(dest);
|
|
|
|
new AsyncTask<String, Object, String>() {
|
|
|
|
@Override
|
|
|
|
protected String doInBackground(String... args) {
|
|
|
|
try {
|
|
|
|
// This requires internet permission
|
|
|
|
return InetAddress.getByName(args[0]).getHostName();
|
|
|
|
} catch (UnknownHostException ignored) {
|
|
|
|
return dest;
|
|
|
|
}
|
2016-01-20 09:45:17 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 09:37:57 +00:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(String host) {
|
|
|
|
synchronized (mapIPHost) {
|
|
|
|
if (!mapIPHost.containsKey(host))
|
|
|
|
mapIPHost.put(host, host);
|
|
|
|
}
|
|
|
|
tvDest.setText(host);
|
2016-01-20 09:45:17 +00:00
|
|
|
}
|
2016-01-22 09:37:57 +00:00
|
|
|
}.execute(dest);
|
|
|
|
}
|
2016-01-19 21:17:40 +00:00
|
|
|
}
|
2016-01-22 09:37:57 +00:00
|
|
|
else
|
|
|
|
tvDest.setText(dest);
|
2016-01-05 12:40:02 +00:00
|
|
|
}
|
|
|
|
}
|