2016-01-27 10:59:16 +00:00
|
|
|
package eu.faircode.netguard;
|
|
|
|
|
2016-01-30 13:26:30 +00:00
|
|
|
/*
|
|
|
|
This file is part of NetGuard.
|
|
|
|
|
|
|
|
NetGuard is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
NetGuard is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2015-2016 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
2016-01-27 10:59:16 +00:00
|
|
|
import android.content.Context;
|
2016-01-31 08:04:54 +00:00
|
|
|
import android.content.res.TypedArray;
|
2016-01-27 10:59:16 +00:00
|
|
|
import android.database.Cursor;
|
2016-02-01 12:07:20 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
2016-02-09 15:31:56 +00:00
|
|
|
import android.os.AsyncTask;
|
2016-02-01 12:07:20 +00:00
|
|
|
import android.os.Build;
|
|
|
|
import android.support.v4.graphics.drawable.DrawableCompat;
|
2016-01-31 08:04:54 +00:00
|
|
|
import android.util.TypedValue;
|
2016-01-27 10:59:16 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.CursorAdapter;
|
2016-01-30 13:26:30 +00:00
|
|
|
import android.widget.ImageView;
|
2016-01-27 10:59:16 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
2016-02-09 15:31:56 +00:00
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.UnknownHostException;
|
2016-01-27 10:59:16 +00:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
2016-02-13 08:45:03 +00:00
|
|
|
public class AdapterAccess extends CursorAdapter {
|
2016-01-27 10:59:16 +00:00
|
|
|
private static String TAG = "NetGuard.Access";
|
|
|
|
|
2016-02-13 08:45:03 +00:00
|
|
|
private int colID;
|
2016-02-05 19:44:12 +00:00
|
|
|
private int colVersion;
|
|
|
|
private int colProtocol;
|
2016-01-27 10:59:16 +00:00
|
|
|
private int colDaddr;
|
|
|
|
private int colDPort;
|
|
|
|
private int colTime;
|
2016-01-31 08:04:54 +00:00
|
|
|
private int colAllowed;
|
2016-01-30 11:46:00 +00:00
|
|
|
private int colBlock;
|
2016-01-27 10:59:16 +00:00
|
|
|
|
2016-01-31 08:04:54 +00:00
|
|
|
private int colorText;
|
2016-01-31 08:41:09 +00:00
|
|
|
private int colorOn;
|
|
|
|
private int colorOff;
|
2016-01-31 08:04:54 +00:00
|
|
|
|
2016-02-13 08:45:03 +00:00
|
|
|
public AdapterAccess(Context context, Cursor cursor) {
|
2016-01-27 10:59:16 +00:00
|
|
|
super(context, cursor, 0);
|
2016-02-13 08:45:03 +00:00
|
|
|
colID = cursor.getColumnIndex("ID");
|
2016-02-05 19:44:12 +00:00
|
|
|
colVersion = cursor.getColumnIndex("version");
|
|
|
|
colProtocol = cursor.getColumnIndex("protocol");
|
2016-01-27 10:59:16 +00:00
|
|
|
colDaddr = cursor.getColumnIndex("daddr");
|
|
|
|
colDPort = cursor.getColumnIndex("dport");
|
|
|
|
colTime = cursor.getColumnIndex("time");
|
2016-01-31 08:04:54 +00:00
|
|
|
colAllowed = cursor.getColumnIndex("allowed");
|
2016-01-30 11:46:00 +00:00
|
|
|
colBlock = cursor.getColumnIndex("block");
|
2016-01-31 08:04:54 +00:00
|
|
|
|
|
|
|
TypedArray ta = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorSecondary});
|
|
|
|
try {
|
|
|
|
colorText = ta.getColor(0, 0);
|
|
|
|
} finally {
|
|
|
|
ta.recycle();
|
|
|
|
}
|
|
|
|
|
|
|
|
TypedValue tv = new TypedValue();
|
2016-01-31 08:41:09 +00:00
|
|
|
context.getTheme().resolveAttribute(R.attr.colorOn, tv, true);
|
|
|
|
colorOn = tv.data;
|
|
|
|
context.getTheme().resolveAttribute(R.attr.colorOff, tv, true);
|
|
|
|
colorOff = tv.data;
|
2016-01-27 10:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
|
|
|
return LayoutInflater.from(context).inflate(R.layout.access, parent, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void bindView(final View view, final Context context, final Cursor cursor) {
|
|
|
|
// Get values
|
2016-02-13 08:45:03 +00:00
|
|
|
final long id = cursor.getLong(colID);
|
2016-02-09 15:31:56 +00:00
|
|
|
final int version = cursor.getInt(colVersion);
|
|
|
|
final int protocol = cursor.getInt(colProtocol);
|
|
|
|
final String daddr = cursor.getString(colDaddr);
|
|
|
|
final int dport = cursor.getInt(colDPort);
|
2016-01-27 10:59:16 +00:00
|
|
|
long time = cursor.getLong(colTime);
|
2016-01-31 08:04:54 +00:00
|
|
|
int allowed = cursor.getInt(colAllowed);
|
2016-01-30 11:46:00 +00:00
|
|
|
int block = cursor.getInt(colBlock);
|
2016-01-27 10:59:16 +00:00
|
|
|
|
|
|
|
// Get views
|
|
|
|
TextView tvTime = (TextView) view.findViewById(R.id.tvTime);
|
2016-01-30 13:26:30 +00:00
|
|
|
ImageView ivBlock = (ImageView) view.findViewById(R.id.ivBlock);
|
2016-01-27 12:09:08 +00:00
|
|
|
final TextView tvDest = (TextView) view.findViewById(R.id.tvDest);
|
2016-01-27 10:59:16 +00:00
|
|
|
|
|
|
|
// Set values
|
2016-01-30 19:44:58 +00:00
|
|
|
tvTime.setText(new SimpleDateFormat("dd HH:mm").format(time));
|
2016-01-30 13:26:30 +00:00
|
|
|
if (block < 0)
|
|
|
|
ivBlock.setImageDrawable(null);
|
2016-02-01 12:07:20 +00:00
|
|
|
else {
|
2016-01-30 13:26:30 +00:00
|
|
|
ivBlock.setImageResource(block > 0 ? R.drawable.host_blocked : R.drawable.host_allowed);
|
2016-02-01 12:07:20 +00:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
Drawable wrap = DrawableCompat.wrap(ivBlock.getDrawable());
|
|
|
|
DrawableCompat.setTint(wrap, block > 0 ? colorOff : colorOn);
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 15:31:56 +00:00
|
|
|
|
2016-02-05 19:44:12 +00:00
|
|
|
tvDest.setText(
|
2016-02-09 15:31:56 +00:00
|
|
|
Util.getProtocolName(protocol, version, true) +
|
|
|
|
" " + daddr + (dport > 0 ? "/" + dport : ""));
|
|
|
|
|
2016-02-14 09:16:44 +00:00
|
|
|
if (Util.isNumericAddress(daddr) && tvDest.getTag() == null)
|
2016-02-13 08:45:03 +00:00
|
|
|
new AsyncTask<String, Object, String>() {
|
2016-02-14 09:16:44 +00:00
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
|
|
|
tvDest.setTag(id);
|
|
|
|
}
|
|
|
|
|
2016-02-13 08:45:03 +00:00
|
|
|
@Override
|
|
|
|
protected String doInBackground(String... args) {
|
|
|
|
try {
|
|
|
|
return InetAddress.getByName(args[0]).getHostName();
|
|
|
|
} catch (UnknownHostException ignored) {
|
|
|
|
return args[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(String addr) {
|
|
|
|
if ((Long) tvDest.getTag() == id)
|
|
|
|
tvDest.setText(
|
|
|
|
Util.getProtocolName(protocol, version, true) +
|
|
|
|
" " + addr + (dport > 0 ? "/" + dport : ""));
|
2016-02-14 09:16:44 +00:00
|
|
|
tvDest.setTag(null);
|
2016-02-13 08:45:03 +00:00
|
|
|
}
|
|
|
|
}.execute(daddr);
|
|
|
|
|
2016-01-31 08:04:54 +00:00
|
|
|
if (allowed < 0)
|
|
|
|
tvDest.setTextColor(colorText);
|
|
|
|
else if (allowed > 0)
|
2016-01-31 08:41:09 +00:00
|
|
|
tvDest.setTextColor(colorOn);
|
2016-01-31 08:04:54 +00:00
|
|
|
else
|
2016-01-31 08:41:09 +00:00
|
|
|
tvDest.setTextColor(colorOff);
|
2016-01-27 10:59:16 +00:00
|
|
|
}
|
|
|
|
}
|