Search in sources :

Example 1 with PackageInfoData

use of dev.ukanth.ufirewall.Api.PackageInfoData in project afwall by ukanth.

the class LogInfo method parseLogs.

public static LogInfo parseLogs(String result, final Context ctx, String pattern, int type) {
    final int unknownUID = -1;
    StringBuilder address;
    int start, end;
    Integer uid;
    String out, src, dst, proto, spt, dpt, len;
    LogInfo logInfo = new LogInfo();
    HashMap<Integer, String> appNameMap = new HashMap<Integer, String>();
    final List<PackageInfoData> apps = Api.getApps(ctx, null);
    int pos = 0;
    try {
        while ((pos = result.indexOf(pattern, pos)) > -1) {
            if (result.indexOf(pattern) == -1)
                continue;
            uid = unknownUID;
            if (((start = result.indexOf("UID=")) != -1) && ((end = result.indexOf(" ", start)) != -1)) {
                uid = Integer.parseInt(result.substring(start + 4, end));
                if (uid != null)
                    logInfo.uid = uid;
            }
            // logInfo = new LogInfo();
            if (((start = result.indexOf("DST=")) != -1) && ((end = result.indexOf(" ", start)) != -1)) {
                dst = result.substring(start + 4, end);
                logInfo.dst = dst;
            }
            if (((start = result.indexOf("DPT=")) != -1) && ((end = result.indexOf(" ", start)) != -1)) {
                dpt = result.substring(start + 4, end);
                logInfo.dpt = Integer.parseInt(dpt);
            }
            if (((start = result.indexOf("SPT=")) != -1) && ((end = result.indexOf(" ", start)) != -1)) {
                spt = result.substring(start + 4, end);
                logInfo.spt = Integer.parseInt(spt);
            }
            if (((start = result.indexOf("PROTO=")) != -1) && ((end = result.indexOf(" ", start)) != -1)) {
                proto = result.substring(start + 6, end);
                logInfo.proto = proto;
            }
            if (((start = result.indexOf("LEN=")) != -1) && ((end = result.indexOf(" ", start)) != -1)) {
                len = result.substring(start + 4, end);
                logInfo.len = Integer.parseInt(len);
            }
            if (((start = result.indexOf("SRC=")) != -1) && ((end = result.indexOf(" ", start)) != -1)) {
                src = result.substring(start + 4, end);
                logInfo.src = src;
            }
            if (((start = result.indexOf("OUT=")) != -1) && ((end = result.indexOf(" ", start)) != -1)) {
                out = result.substring(start + 4, end);
                logInfo.out = out;
            }
            if (uid == android.os.Process.myUid()) {
                return null;
            }
            String appName = "";
            if (uid.intValue() != unknownUID) {
                if (uid == 1020) {
                    appName = "mDNS";
                } else {
                    // system level packages
                    if (!appNameMap.containsKey(uid)) {
                        appName = ctx.getPackageManager().getNameForUid(uid);
                        for (PackageInfoData app : apps) {
                            if (app.uid == uid) {
                                appName = app.names.get(0);
                                break;
                            }
                        }
                        appNameMap.put(uid, appName);
                    } else {
                        appName = appNameMap.get(uid);
                    }
                }
            } else {
                appName = ctx.getString(R.string.unknown_item);
            }
            logInfo.appName = appName;
            address = new StringBuilder();
            // address.append(ctx.getString(R.string.blocked));
            // address.append(" ");
            address.append(appName);
            address.append("(" + uid + ") ");
            address.append(logInfo.dst);
            address.append(":");
            address.append(logInfo.dpt);
            logInfo.type = type;
            if (G.showHost()) {
                try {
                    String add = Address.getHostName(InetAddress.getByName(logInfo.dst));
                    if (add != null) {
                        logInfo.host = add;
                        address.append("(" + add + ") ");
                    }
                } catch (Exception e) {
                }
            }
            address.append("\n");
            logInfo.uidString = address.toString();
            return logInfo;
        }
    } catch (Exception e) {
        Log.e(Api.TAG, "Exception in LogService", e);
    }
    return logInfo;
}
Also used : HashMap(java.util.HashMap) PackageInfoData(dev.ukanth.ufirewall.Api.PackageInfoData)

Example 2 with PackageInfoData

use of dev.ukanth.ufirewall.Api.PackageInfoData in project afwall by ukanth.

the class MainActivity method selectRevert.

private void selectRevert() {
    if (this.listview == null) {
        this.listview = this.findViewById(R.id.listview);
    }
    ListAdapter adapter = listview.getAdapter();
    if (adapter != null) {
        int count = adapter.getCount(), item;
        for (item = 0; item < count; item++) {
            PackageInfoData data = (PackageInfoData) adapter.getItem(item);
            if (data.uid != Api.SPECIAL_UID_ANY) {
                data.selected_wifi = !data.selected_wifi;
                data.selected_3g = !data.selected_3g;
                data.selected_roam = !data.selected_roam;
                data.selected_vpn = !data.selected_vpn;
                data.selected_tether = !data.selected_tether;
                data.selected_lan = !data.selected_lan;
                data.selected_tor = !data.selected_tor;
            // addToQueue(data);
            }
            setDirty(true);
        }
        ((BaseAdapter) adapter).notifyDataSetChanged();
    }
}
Also used : PackageInfoData(dev.ukanth.ufirewall.Api.PackageInfoData) BaseAdapter(android.widget.BaseAdapter) ListAdapter(android.widget.ListAdapter)

Example 3 with PackageInfoData

use of dev.ukanth.ufirewall.Api.PackageInfoData in project afwall by ukanth.

the class MainActivity method filterApps.

/**
 * Filter application based on app tpe
 *
 * @param i
 */
private void filterApps(int i) {
    Set<PackageInfoData> returnList = new HashSet<>();
    List<PackageInfoData> inputList;
    List<PackageInfoData> allApps = Api.getApps(getApplicationContext(), null);
    if (i >= 0) {
        for (PackageInfoData infoData : allApps) {
            if (infoData != null) {
                if (infoData.appType == i) {
                    returnList.add(infoData);
                }
            }
        }
        inputList = new ArrayList<>(returnList);
    } else {
        inputList = allApps;
    }
    try {
        Collections.sort(inputList, new PackageComparator());
    } catch (Exception e) {
        Log.d(Api.TAG, "Exception in filter Sorting");
    }
    ArrayAdapter appAdapter;
    if (selectedColumns <= DEFAULT_VIEW_LIMIT) {
        appAdapter = new AppListArrayAdapter(this, getApplicationContext(), inputList, true);
    } else {
        appAdapter = new AppListArrayAdapter(this, getApplicationContext(), inputList);
    }
    this.listview.setAdapter(appAdapter);
    appAdapter.notifyDataSetChanged();
    // restore
    this.listview.setSelectionFromTop(index, top);
}
Also used : PackageInfoData(dev.ukanth.ufirewall.Api.PackageInfoData) PackageComparator(dev.ukanth.ufirewall.util.PackageComparator) AppListArrayAdapter(dev.ukanth.ufirewall.util.AppListArrayAdapter) ActivityNotFoundException(android.content.ActivityNotFoundException) AppListArrayAdapter(dev.ukanth.ufirewall.util.AppListArrayAdapter) ArrayAdapter(android.widget.ArrayAdapter) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with PackageInfoData

use of dev.ukanth.ufirewall.Api.PackageInfoData in project afwall by ukanth.

the class MainActivity method selectAllLAN.

private void selectAllLAN(boolean flag) {
    if (this.listview == null) {
        this.listview = this.findViewById(R.id.listview);
    }
    ListAdapter adapter = listview.getAdapter();
    if (adapter != null) {
        int count = adapter.getCount(), item;
        for (item = 0; item < count; item++) {
            PackageInfoData data = (PackageInfoData) adapter.getItem(item);
            if (data.uid != Api.SPECIAL_UID_ANY) {
                data.selected_lan = flag;
            // addToQueue(data);
            }
            setDirty(true);
        }
        ((BaseAdapter) adapter).notifyDataSetChanged();
    }
}
Also used : PackageInfoData(dev.ukanth.ufirewall.Api.PackageInfoData) BaseAdapter(android.widget.BaseAdapter) ListAdapter(android.widget.ListAdapter)

Example 5 with PackageInfoData

use of dev.ukanth.ufirewall.Api.PackageInfoData in project afwall by ukanth.

the class MainActivity method selectRevert.

private void selectRevert(int flag) {
    if (this.listview == null) {
        this.listview = this.findViewById(R.id.listview);
    }
    ListAdapter adapter = listview.getAdapter();
    if (adapter != null) {
        int count = adapter.getCount(), item;
        for (item = 0; item < count; item++) {
            PackageInfoData data = (PackageInfoData) adapter.getItem(item);
            if (data.uid != Api.SPECIAL_UID_ANY) {
                switch(flag) {
                    case R.id.img_wifi:
                        data.selected_wifi = !data.selected_wifi;
                        break;
                    case R.id.img_3g:
                        data.selected_3g = !data.selected_3g;
                        break;
                    case R.id.img_roam:
                        data.selected_roam = !data.selected_roam;
                        break;
                    case R.id.img_vpn:
                        data.selected_vpn = !data.selected_vpn;
                        break;
                    case R.id.img_tether:
                        data.selected_tether = !data.selected_tether;
                        break;
                    case R.id.img_lan:
                        data.selected_lan = !data.selected_lan;
                        break;
                    case R.id.img_tor:
                        data.selected_tor = !data.selected_tor;
                        break;
                }
            // addToQueue(data);
            }
            setDirty(true);
        }
        ((BaseAdapter) adapter).notifyDataSetChanged();
    }
}
Also used : PackageInfoData(dev.ukanth.ufirewall.Api.PackageInfoData) BaseAdapter(android.widget.BaseAdapter) ListAdapter(android.widget.ListAdapter)

Aggregations

PackageInfoData (dev.ukanth.ufirewall.Api.PackageInfoData)16 BaseAdapter (android.widget.BaseAdapter)11 ListAdapter (android.widget.ListAdapter)11 AppListArrayAdapter (dev.ukanth.ufirewall.util.AppListArrayAdapter)3 PackageComparator (dev.ukanth.ufirewall.util.PackageComparator)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 ActivityNotFoundException (android.content.ActivityNotFoundException)2 ArrayAdapter (android.widget.ArrayAdapter)2 ArrayList (java.util.ArrayList)2 SparseArray (android.util.SparseArray)1 HashMap (java.util.HashMap)1