NetGuard/app/src/main/java/eu/faircode/netguard/LogAdapter.java

175 lines
6.7 KiB
Java
Raw Normal View History

2016-01-05 12:40:02 +00:00
package eu.faircode.netguard;
import android.content.Context;
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;
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;
import android.widget.ImageView;
2016-01-05 12:40:02 +00:00
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.net.InetAddress;
import java.net.UnknownHostException;
2016-01-05 12:40:02 +00:00
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
2016-01-05 12:40:02 +00:00
public class LogAdapter extends CursorAdapter {
private int colTime;
private int colVersion;
2016-01-05 12:40:02 +00:00
private int colIP;
private int colProtocol;
private int colPort;
private int colFlags;
private int colUid;
private int colConnection;
private int colInteractive;
2016-01-19 19:58:51 +00:00
private int colAllowed;
private Map<String, String> mapIPHost = new HashMap<String, String>();
2016-01-05 12:40:02 +00:00
public LogAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
colTime = cursor.getColumnIndex("time");
colVersion = cursor.getColumnIndex("version");
2016-01-05 12:40:02 +00:00
colIP = cursor.getColumnIndex("ip");
colProtocol = cursor.getColumnIndex("protocol");
colPort = cursor.getColumnIndex("port");
colFlags = cursor.getColumnIndex("flags");
colUid = cursor.getColumnIndex("uid");
colConnection = cursor.getColumnIndex("connection");
colInteractive = cursor.getColumnIndex("interactive");
2016-01-19 19:58:51 +00:00
colAllowed = cursor.getColumnIndex("allowed");
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);
int version = (cursor.isNull(colVersion) ? -1 : cursor.getInt(colVersion));
2016-01-06 10:12:37 +00:00
String ip = cursor.getString(colIP);
int protocol = (cursor.isNull(colProtocol) ? -1 : cursor.getInt(colProtocol));
int port = (cursor.isNull(colPort) ? -1 : cursor.getInt(colPort));
String flags = cursor.getString(colFlags);
2016-01-10 13:26:37 +00:00
int uid = (cursor.isNull(colUid) ? -1 : cursor.getInt(colUid));
int connection = (cursor.isNull(colConnection) ? -1 : cursor.getInt(colConnection));
int interactive = (cursor.isNull(colInteractive) ? -1 : cursor.getInt(colInteractive));
2016-01-19 19:58:51 +00:00
int allowed = (cursor.isNull(colAllowed) ? -1 : cursor.getInt(colAllowed));
final String whois = (ip.length() > 1 && ip.charAt(0) == '/' ? ip.substring(1) : ip);
2016-01-05 12:40:02 +00:00
// Get views
TextView tvTime = (TextView) view.findViewById(R.id.tvTime);
ImageView ivConnection = (ImageView) view.findViewById(R.id.ivConnection);
ImageView ivInteractive = (ImageView) view.findViewById(R.id.ivInteractive);
TextView tvProtocol = (TextView) view.findViewById(R.id.tvProtocol);
TextView tvPort = (TextView) view.findViewById(R.id.tvPort);
TextView tvFlags = (TextView) view.findViewById(R.id.tvFlags);
2016-01-08 16:21:50 +00:00
ImageView ivIcon = (ImageView) view.findViewById(R.id.ivIcon);
TextView tvUid = (TextView) view.findViewById(R.id.tvUid);
final TextView tvIP = (TextView) view.findViewById(R.id.tvIP);
2016-01-08 16:21:50 +00:00
// Set values
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);
}
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));
2016-01-19 20:16:54 +00:00
tvPort.setText(port < 0 ? "" : Integer.toString(port));
2016-01-08 16:21:50 +00:00
tvFlags.setText(flags);
2016-01-05 12:40:02 +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);
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));
// tvProtocol.setText("99");
2016-01-10 13:26:37 +00:00
// tvPort.setText("88888");
// tvFlags.setText("+APFR");
// tvUid.setText("18888");
synchronized (mapIPHost) {
if (mapIPHost.containsKey(whois))
tvIP.setText(mapIPHost.get(whois));
else {
tvIP.setText(whois);
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 whois;
}
}
@Override
protected void onPostExecute(String host) {
synchronized (mapIPHost) {
if (!mapIPHost.containsKey(host))
mapIPHost.put(host, host);
}
tvIP.setText(host);
}
}.execute(whois);
}
}
2016-01-05 12:40:02 +00:00
}
}