Fix potential null package name

If a client doesn't pass a context, ActivityThread.currentPackageName()
may return null if the client is part of the Android framework. To
ensure logic doesn't break due to a null package name, use
PackageManager.getPackagesForUid() to get a valid package name for the
caller.

Bug: 243457763
Test: Run CHQTS and verify it now passes on a device previously seeing
null package names

Merged-In: Ia706a503f9ca068fc997c0acdcd74ca62a1424be
Change-Id: Ia706a503f9ca068fc997c0acdcd74ca62a1424be
(cherry picked from commit 2b69e2cf52)
This commit is contained in:
Anthony Stange
2022-07-01 21:34:04 +00:00
parent 28930b91d8
commit 9d77288dcd

View File

@@ -322,6 +322,17 @@ public class ContextHubClientBroker extends IContextHubClient.Stub
} else {
mPendingIntentRequest = new PendingIntentRequest(pendingIntent, nanoAppId);
}
if (packageName == null) {
String[] packages = mContext.getPackageManager().getPackagesForUid(
Binder.getCallingUid());
if (packages != null && packages.length > 0) {
packageName = packages[0];
}
Log.e(TAG, "createClient: Provided package name null. Using first package name "
+ packageName);
}
mPackage = packageName;
mAttributionTag = attributionTag;
mTransactionManager = transactionManager;