From 2e185d184add475ccf6b3007cc533a8fe7c993db Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 10 Feb 2016 20:17:22 +0100 Subject: [PATCH] Add open files to logcat, show percentage files open refs #307 --- .../eu/faircode/netguard/SinkholeService.java | 23 +++++++++++++-- .../main/java/eu/faircode/netguard/Util.java | 28 ++++++++++++++----- app/src/main/res/layout/traffic.xml | 17 ++++++++--- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/SinkholeService.java b/app/src/main/java/eu/faircode/netguard/SinkholeService.java index 53ae971a..24c6aa7b 100644 --- a/app/src/main/java/eu/faircode/netguard/SinkholeService.java +++ b/app/src/main/java/eu/faircode/netguard/SinkholeService.java @@ -633,11 +633,30 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS else remoteViews.setTextViewText(R.id.tvRx, getString(R.string.msg_mbsec, rxsec / 1000 / 1000)); + // Show session/file count if (filter && loglevel <= Log.WARN) { int[] count = jni_get_session_count(); remoteViews.setTextViewText(R.id.tvSessions, count[0] + "/" + count[1] + "/" + count[2]); - } else - remoteViews.setTextViewText(R.id.tvSessions, ""); + + File proc = new File("/proc/sys/fs/file-nr"); + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(proc)); + String line = br.readLine(); + String[] filenr = line.split("\\s+"); + if (filenr.length == 3) { + int perc = Integer.parseInt(filenr[0]) * 100 / Integer.parseInt(filenr[2]); + remoteViews.setTextViewText(R.id.tvFiles, perc + "%"); + } + } catch (Throwable ignored) { + } finally { + if (br != null) + try { + br.close(); + } catch (IOException ignored) { + } + } + } // Show notification Intent main = new Intent(SinkholeService.this, ActivityMain.class); diff --git a/app/src/main/java/eu/faircode/netguard/Util.java b/app/src/main/java/eu/faircode/netguard/Util.java index 93aeaad3..9dd8d7ed 100644 --- a/app/src/main/java/eu/faircode/netguard/Util.java +++ b/app/src/main/java/eu/faircode/netguard/Util.java @@ -757,7 +757,7 @@ public class Util { // Build intent Intent sendEmail = new Intent(Intent.ACTION_SEND); sendEmail.setType("message/rfc822"); - sendEmail.putExtra(Intent.EXTRA_EMAIL, new String[]{"marcel+netguard@faircode.eu"}); + sendEmail.putExtra(Intent.EXTRA_EMAIL, new String[]{"marcel+netguard@faircode.eu" }); sendEmail.putExtra(Intent.EXTRA_SUBJECT, "NetGuard " + version + " logcat"); sendEmail.putExtra(Intent.EXTRA_TEXT, sb.toString()); sendEmail.putExtra(Intent.EXTRA_STREAM, uri); @@ -779,12 +779,13 @@ public class Util { private static StringBuilder getLogcat() { StringBuilder builder = new StringBuilder(); - Process process = null; + Process process1 = null; + Process process2 = null; BufferedReader br = null; try { - String[] command = new String[]{"logcat", "-d", "-v", "threadtime"}; - process = Runtime.getRuntime().exec(command); - br = new BufferedReader(new InputStreamReader(process.getInputStream())); + String[] command1 = new String[]{"logcat", "-d", "-v", "threadtime" }; + process1 = Runtime.getRuntime().exec(command1); + br = new BufferedReader(new InputStreamReader(process1.getInputStream())); int count = 0; String line; while ((line = br.readLine()) != null) { @@ -792,6 +793,13 @@ public class Util { builder.append(line).append("\r\n"); } Log.i(TAG, "Logcat lines=" + count); + + String[] command2 = new String[]{"ls", "-l", "/proc/" + android.os.Process.myPid() + "/fd" }; + process2 = Runtime.getRuntime().exec(command2); + br = new BufferedReader(new InputStreamReader(process2.getInputStream())); + while ((line = br.readLine()) != null) + builder.append(line).append("\r\n"); + } catch (IOException ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); } finally { @@ -800,9 +808,15 @@ public class Util { br.close(); } catch (IOException ignored) { } - if (process != null) + if (process2 != null) try { - process.destroy(); + process2.destroy(); + } catch (Throwable ex) { + Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); + } + if (process1 != null) + try { + process1.destroy(); } catch (Throwable ex) { Log.w(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); } diff --git a/app/src/main/res/layout/traffic.xml b/app/src/main/res/layout/traffic.xml index 874d9176..9e106242 100644 --- a/app/src/main/res/layout/traffic.xml +++ b/app/src/main/res/layout/traffic.xml @@ -47,6 +47,16 @@ android:textColor="@color/colorReceive" /> + +