From 4560925d6d98caa0a49f26264d04415caa3c0f6c Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 27 Jan 2016 09:00:53 +0100 Subject: [PATCH] Get application names --- .../java/eu/faircode/netguard/ActivityLog.java | 17 +++-------------- .../java/eu/faircode/netguard/Receiver.java | 6 ++++-- .../main/java/eu/faircode/netguard/Util.java | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/ActivityLog.java b/app/src/main/java/eu/faircode/netguard/ActivityLog.java index 72d7f490..a3312ccc 100644 --- a/app/src/main/java/eu/faircode/netguard/ActivityLog.java +++ b/app/src/main/java/eu/faircode/netguard/ActivityLog.java @@ -21,7 +21,6 @@ package eu.faircode.netguard; import android.content.Intent; import android.content.SharedPreferences; -import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; @@ -31,6 +30,7 @@ import android.preference.PreferenceManager; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.SearchView; +import android.text.TextUtils; import android.util.Log; import android.view.Gravity; import android.view.Menu; @@ -128,19 +128,8 @@ public class ActivityLog extends AppCompatActivity { final int sport = (cursor.isNull(cursor.getColumnIndex("sport")) ? -1 : cursor.getInt(cursor.getColumnIndex("sport"))); final int uid = (cursor.isNull(cursor.getColumnIndex("uid")) ? -1 : cursor.getInt(cursor.getColumnIndex("uid"))); - // Get package name - String name = null; - if (uid == 0) - name = "root"; - else { - String[] pkg = pm.getPackagesForUid(uid); - if (pkg != null && pkg.length > 0) - try { - ApplicationInfo info = pm.getApplicationInfo(pkg[0], 0); - name = pm.getApplicationLabel(info).toString(); - } catch (PackageManager.NameNotFoundException ignored) { - } - } + // Get application name(s) + String name = (uid == 0 ? "root" : TextUtils.join(",", Util.getApplicationNames(uid, ActivityLog.this))); // Get external address InetAddress addr = null; diff --git a/app/src/main/java/eu/faircode/netguard/Receiver.java b/app/src/main/java/eu/faircode/netguard/Receiver.java index c06d01b1..a8b2aba9 100644 --- a/app/src/main/java/eu/faircode/netguard/Receiver.java +++ b/app/src/main/java/eu/faircode/netguard/Receiver.java @@ -27,12 +27,12 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; -import android.net.VpnService; import android.os.Build; import android.os.PowerManager; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; +import android.text.TextUtils; import android.util.Log; import android.util.TypedValue; @@ -107,13 +107,15 @@ public class Receiver extends BroadcastReceiver { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); try { + // Get application name(s) + String name = TextUtils.join(",", Util.getApplicationNames(uid, context)); + // Get application info PackageManager pm = context.getPackageManager(); String[] packages = pm.getPackagesForUid(uid); if (packages.length < 1) throw new PackageManager.NameNotFoundException(Integer.toString(uid)); ApplicationInfo info = pm.getApplicationInfo(packages[0], 0); - String name = (String) pm.getApplicationLabel(info); boolean internet = (pm.checkPermission("android.permission.INTERNET", info.packageName) == PackageManager.PERMISSION_GRANTED); // Build notification diff --git a/app/src/main/java/eu/faircode/netguard/Util.java b/app/src/main/java/eu/faircode/netguard/Util.java index fec3f760..a30429aa 100644 --- a/app/src/main/java/eu/faircode/netguard/Util.java +++ b/app/src/main/java/eu/faircode/netguard/Util.java @@ -57,6 +57,8 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; @@ -305,6 +307,20 @@ public class Util { } } + public static List getApplicationNames(int uid, Context context) { + List listResult = new ArrayList<>(); + PackageManager pm = context.getPackageManager(); + String[] pkg = pm.getPackagesForUid(uid); + if (pkg != null && pkg.length > 0) + try { + ApplicationInfo info = pm.getApplicationInfo(pkg[0], 0); + listResult.add(pm.getApplicationLabel(info).toString()); + } catch (PackageManager.NameNotFoundException ignored) { + } + Collections.sort(listResult); + return listResult; + } + public static boolean isDebuggable(Context context) { return ((context.getApplicationContext().getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0); }