Java Code Examples for android.content.pm.PackageManager#getNameForUid()

The following examples show how to use android.content.pm.PackageManager#getNameForUid() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SyncOperation.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
static String reasonToString(PackageManager pm, int reason) {
    if (reason >= 0) {
        if (pm != null) {
            final String[] packages = pm.getPackagesForUid(reason);
            if (packages != null && packages.length == 1) {
                return packages[0];
            }
            final String name = pm.getNameForUid(reason);
            if (name != null) {
                return name;
            }
            return String.valueOf(reason);
        } else {
            return String.valueOf(reason);
        }
    } else {
        final int index = -reason - 1;
        if (index >= REASON_NAMES.length) {
            return String.valueOf(reason);
        } else {
            return REASON_NAMES[index];
        }
    }
}
 
Example 2
Source File: ScalarSensorService.java    From science-journal with Apache License 2.0 5 votes vote down vote up
/**
 * Check that the connecting app is one we trust to stream sensor data to.
 *
 * <p>By default, this will only accept Science Journal installed from the Play Store.
 *
 * <p>Note that this method only returns valid results when called from within methods defined on
 * the Binder class, not methods like onBind on the service itself.
 */
protected boolean binderHasAllowedSignature() {
  int uid = Binder.getCallingUid();
  PackageManager pm = getPackageManager();
  String bindingName = pm.getNameForUid(uid);
  try {
    PackageInfo info = pm.getPackageInfo(bindingName, PackageManager.GET_SIGNATURES);
    Signature[] signatures = info.signatures;
    Set<String> allowedSignatures = allowedSignatures();
    if (Log.isLoggable(TAG, Log.INFO)) {
      Log.i(
          TAG, "Number of signatures of binding app [" + bindingName + "]: " + signatures.length);
    }
    for (Signature signature : signatures) {
      String charString = signature.toCharsString();
      if (Log.isLoggable(TAG, Log.INFO)) {
        Log.i(TAG, "Checking signature: " + charString);
      }
      if (allowedSignatures.contains(charString)) {
        if (Log.isLoggable(TAG, Log.INFO)) {
          Log.i(TAG, "Signature match!");
        }
        return true;
      }
    }
  } catch (PackageManager.NameNotFoundException e) {
    if (Log.isLoggable(TAG, Log.ERROR)) {
      Log.e(TAG, "Unknown package name: " + bindingName);
    }
  }
  if (Log.isLoggable(TAG, Log.ERROR)) {
    Log.e(TAG, "Signature check failed");
  }
  return false;
}