List of usage examples for android.content.pm PackageManager getPackagesForUid
public abstract @Nullable String[] getPackagesForUid(int uid);
From source file:android_network.hetnet.vpn_service.Receiver.java
public static void notifyNewApplication(int uid, Context context) { if (uid < 0) return;//w w w.j ava2 s . c o m SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); try { // Get application info String name = TextUtils.join(", ", Util.getApplicationNames(uid, context)); // Get application info PackageManager pm = context.getPackageManager(); String[] packages = pm.getPackagesForUid(uid); if (packages == null || packages.length < 1) throw new PackageManager.NameNotFoundException(Integer.toString(uid)); boolean internet = Util.hasInternet(uid, context); // Build notification Intent main = new Intent(context, MainActivity.class); main.putExtra(MainActivity.EXTRA_REFRESH, true); main.putExtra(MainActivity.EXTRA_SEARCH, Integer.toString(uid)); PendingIntent pi = PendingIntent.getActivity(context, uid, main, PendingIntent.FLAG_UPDATE_CURRENT); Util.setTheme(context); TypedValue tv = new TypedValue(); context.getTheme().resolveAttribute(R.attr.colorPrimary, tv, true); NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_security_white_24dp).setContentIntent(pi).setColor(tv.data) .setAutoCancel(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) builder.setContentTitle(name).setContentText(context.getString(R.string.msg_installed_n)); else builder.setContentTitle(context.getString(R.string.app_name)) .setContentText(context.getString(R.string.msg_installed, name)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { builder.setCategory(Notification.CATEGORY_STATUS).setVisibility(Notification.VISIBILITY_SECRET); } // Get defaults SharedPreferences prefs_wifi = context.getSharedPreferences("wifi", Context.MODE_PRIVATE); SharedPreferences prefs_other = context.getSharedPreferences("other", Context.MODE_PRIVATE); boolean wifi = prefs_wifi.getBoolean(packages[0], prefs.getBoolean("whitelist_wifi", true)); boolean other = prefs_other.getBoolean(packages[0], prefs.getBoolean("whitelist_other", true)); // Build Wi-Fi action Intent riWifi = new Intent(context, ServiceSinkhole.class); riWifi.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set); riWifi.putExtra(ServiceSinkhole.EXTRA_NETWORK, "wifi"); riWifi.putExtra(ServiceSinkhole.EXTRA_UID, uid); riWifi.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]); riWifi.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !wifi); PendingIntent piWifi = PendingIntent.getService(context, uid, riWifi, PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(wifi ? R.drawable.wifi_on : R.drawable.wifi_off, context.getString(wifi ? R.string.title_allow : R.string.title_block), piWifi); // Build mobile action Intent riOther = new Intent(context, ServiceSinkhole.class); riOther.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set); riOther.putExtra(ServiceSinkhole.EXTRA_NETWORK, "other"); riOther.putExtra(ServiceSinkhole.EXTRA_UID, uid); riOther.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]); riOther.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !other); PendingIntent piOther = PendingIntent.getService(context, uid + 10000, riOther, PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(other ? R.drawable.other_on : R.drawable.other_off, context.getString(other ? R.string.title_allow : R.string.title_block), piOther); // Show notification if (internet) NotificationManagerCompat.from(context).notify(uid, builder.build()); else { NotificationCompat.BigTextStyle expanded = new NotificationCompat.BigTextStyle(builder); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) expanded.bigText(context.getString(R.string.msg_installed_n)); else expanded.bigText(context.getString(R.string.msg_installed, name)); expanded.setSummaryText(context.getString(R.string.title_internet)); NotificationManagerCompat.from(context).notify(uid, expanded.build()); } } catch (PackageManager.NameNotFoundException ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); } }
From source file:com.noshufou.android.su.util.Util.java
public static String getAppPackage(Context c, int uid) { if (sSystemUids.get(uid) != null) { return sSystemUids.get(uid); }/*from w w w .jav a 2 s .c om*/ PackageManager pm = c.getPackageManager(); String[] packages = pm.getPackagesForUid(uid); String appPackage = "unknown"; if (packages != null) { if (packages.length == 1) { appPackage = packages[0]; } else if (packages.length > 1) { appPackage = ""; for (int i = 0; i < packages.length; i++) { appPackage += packages[i]; if (i < packages.length - 1) { appPackage += ", "; } } } } else { Log.e(TAG, "Package not found"); } return appPackage; }
From source file:android_network.hetnet.vpn_service.Util.java
public static boolean isSystem(int uid, Context context) { PackageManager pm = context.getPackageManager(); String[] pkgs = pm.getPackagesForUid(uid); if (pkgs != null) for (String pkg : pkgs) if (isSystem(pkg, context)) return true; return false; }
From source file:android_network.hetnet.vpn_service.Util.java
public static boolean hasInternet(int uid, Context context) { PackageManager pm = context.getPackageManager(); String[] pkgs = pm.getPackagesForUid(uid); if (pkgs != null) for (String pkg : pkgs) if (hasInternet(pkg, context)) return true; return false; }
From source file:android_network.hetnet.vpn_service.Util.java
public static List<String> getApplicationNames(int uid, Context context) { List<String> listResult = new ArrayList<>(); if (uid == 0) listResult.add(context.getString(R.string.title_root)); else if (uid == 1013) listResult.add(context.getString(R.string.title_mediaserver)); else if (uid == 9999) listResult.add(context.getString(R.string.title_nobody)); else {//from w w w . j av a 2s . com PackageManager pm = context.getPackageManager(); String[] pkgs = pm.getPackagesForUid(uid); if (pkgs == null) listResult.add(Integer.toString(uid)); else for (String pkg : pkgs) try { ApplicationInfo info = pm.getApplicationInfo(pkg, 0); listResult.add(pm.getApplicationLabel(info).toString()); } catch (PackageManager.NameNotFoundException ignored) { } Collections.sort(listResult); } return listResult; }
From source file:com.noshufou.android.su.util.Util.java
public static String getAppName(Context c, int uid, boolean withUid) { if (sSystemUids.get(uid) != null) { return sSystemUids.get(uid); }//from ww w . j ava2 s . c om PackageManager pm = c.getPackageManager(); String appName = "Unknown"; String[] packages = pm.getPackagesForUid(uid); if (packages != null) { try { if (packages.length == 1) { appName = pm.getApplicationLabel(pm.getApplicationInfo(packages[0], 0)).toString(); } else if (packages.length > 1) { appName = ""; for (int i = 0; i < packages.length; i++) { appName += packages[i]; if (i < packages.length - 1) { appName += ", "; } } } } catch (NameNotFoundException e) { Log.e(TAG, "Package name not found", e); } } else { Log.e(TAG, "Package not found for uid " + uid); } if (withUid) { appName += " (" + uid + ")"; } return appName; }
From source file:com.noshufou.android.su.util.Util.java
public static Drawable getAppIcon(Context c, int uid) { PackageManager pm = c.getPackageManager(); Drawable appIcon = c.getResources().getDrawable(R.drawable.sym_def_app_icon); String[] packages = pm.getPackagesForUid(uid); if (packages != null) { // if (packages.length == 1) { try {/*w ww .ja v a2 s. c om*/ ApplicationInfo appInfo = pm.getApplicationInfo(packages[0], 0); appIcon = pm.getApplicationIcon(appInfo); } catch (NameNotFoundException e) { Log.e(TAG, "No package found matching with the uid " + uid); } // } } else { Log.e(TAG, "Package not found for uid " + uid); } return appIcon; }
From source file:uk.ac.bournemouth.darwin.auth.AuthTokenPermissionActivity.java
@Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding = DataBindingUtil.setContentView(this, R.layout.get_permission); mBinding.setAccount(getIntent().<Account>getParcelableExtra(DarwinAuthenticator.KEY_ACCOUNT)); mCallerUid = getIntent().getIntExtra(AccountManager.KEY_CALLER_UID, -1); final PackageManager pm = getPackageManager(); String callerPackage = pm.getPackagesForUid(mCallerUid)[0]; String packageName;/*from ww w .ja v a 2 s.c o m*/ try { final PackageInfo packageInfo = pm.getPackageInfo(callerPackage, 0); int labelRes = packageInfo.applicationInfo.labelRes; packageName = pm.getResourcesForApplication(packageInfo.applicationInfo).getString(labelRes); } catch (NameNotFoundException e) { Log.w(TAG, "onCreate: ", e); packageName = callerPackage; } mBinding.setCallerName(packageName); mBinding.cancelbutton.setOnClickListener(this); mBinding.okbutton.setOnClickListener(this); }
From source file:net.sf.fakenames.fddemo.PermissionActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Intent intent = getIntent(); if (intent == null) { finish();/* w w w. j a v a2 s . c o m*/ return; } uid = intent.getIntExtra(EXTRA_UID, -1); mode = intent.getStringExtra(EXTRA_MODE); resultReceiver = intent.getParcelableExtra(EXTRA_CALLBACK); packageName = intent.getStringExtra(EXTRA_CALLER); path = intent.getStringExtra(EXTRA_PATH); if (uid == -1 || mode == null || resultReceiver == null) { finish(); return; } setContentView(R.layout.permission_dialog); allow.setText(R.string.allow); deny.setText(R.string.deny); CharSequence name; String packageNames[]; final PackageManager pm = getPackageManager(); if (TextUtils.isEmpty(packageName)) { packageNames = pm.getPackagesForUid(uid); } else { packageNames = new String[] { packageName }; } name = toLabel(uid, packageNames); final CharSequence req = SpanFormatter.format(getString(R.string.perm_req), name, accessPattern(mode), path); message.setText(req); }
From source file:com.example.android.commitcontent.app.ImageKeyboard.java
private boolean validatePackageName(@Nullable EditorInfo editorInfo) { if (editorInfo == null) { return false; }// w ww. ja v a2 s .co m final String packageName = editorInfo.packageName; if (packageName == null) { return false; } // In Android L MR-1 and prior devices, EditorInfo.packageName is not a reliable identifier // of the target application because: // 1. the system does not verify it [1] // 2. InputMethodManager.startInputInner() had filled EditorInfo.packageName with // view.getContext().getPackageName() [2] // [1]: https://android.googlesource.com/platform/frameworks/base/+/a0f3ad1b5aabe04d9eb1df8bad34124b826ab641 // [2]: https://android.googlesource.com/platform/frameworks/base/+/02df328f0cd12f2af87ca96ecf5819c8a3470dc8 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return true; } final InputBinding inputBinding = getCurrentInputBinding(); if (inputBinding == null) { // Due to b.android.com/225029, it is possible that getCurrentInputBinding() returns // null even after onStartInputView() is called. // TODO: Come up with a way to work around this bug.... Log.e(TAG, "inputBinding should not be null here. " + "You are likely to be hitting b.android.com/225029"); return false; } final int packageUid = inputBinding.getUid(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { final AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE); try { appOpsManager.checkPackage(packageUid, packageName); } catch (Exception e) { return false; } return true; } final PackageManager packageManager = getPackageManager(); final String possiblePackageNames[] = packageManager.getPackagesForUid(packageUid); for (final String possiblePackageName : possiblePackageNames) { if (packageName.equals(possiblePackageName)) { return true; } } return false; }