Get application names

This commit is contained in:
M66B 2016-01-27 09:00:53 +01:00
parent f60b3e5b6b
commit 4560925d6d
3 changed files with 23 additions and 16 deletions

View File

@ -21,7 +21,6 @@ package eu.faircode.netguard;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
@ -31,6 +30,7 @@ import android.preference.PreferenceManager;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.Menu; 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 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"))); final int uid = (cursor.isNull(cursor.getColumnIndex("uid")) ? -1 : cursor.getInt(cursor.getColumnIndex("uid")));
// Get package name // Get application name(s)
String name = null; String name = (uid == 0 ? "root" : TextUtils.join(",", Util.getApplicationNames(uid, ActivityLog.this)));
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 external address // Get external address
InetAddress addr = null; InetAddress addr = null;

View File

@ -27,12 +27,12 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.VpnService;
import android.os.Build; import android.os.Build;
import android.os.PowerManager; import android.os.PowerManager;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationManagerCompat;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.TypedValue; import android.util.TypedValue;
@ -107,13 +107,15 @@ public class Receiver extends BroadcastReceiver {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
try { try {
// Get application name(s)
String name = TextUtils.join(",", Util.getApplicationNames(uid, context));
// Get application info // Get application info
PackageManager pm = context.getPackageManager(); PackageManager pm = context.getPackageManager();
String[] packages = pm.getPackagesForUid(uid); String[] packages = pm.getPackagesForUid(uid);
if (packages.length < 1) if (packages.length < 1)
throw new PackageManager.NameNotFoundException(Integer.toString(uid)); throw new PackageManager.NameNotFoundException(Integer.toString(uid));
ApplicationInfo info = pm.getApplicationInfo(packages[0], 0); 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); boolean internet = (pm.checkPermission("android.permission.INTERNET", info.packageName) == PackageManager.PERMISSION_GRANTED);
// Build notification // Build notification

View File

@ -57,6 +57,8 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -305,6 +307,20 @@ public class Util {
} }
} }
public static List<String> getApplicationNames(int uid, Context context) {
List<String> 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) { public static boolean isDebuggable(Context context) {
return ((context.getApplicationContext().getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0); return ((context.getApplicationContext().getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
} }