Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2b69e2cf authored by Anthony Stange's avatar Anthony Stange
Browse files

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: 237725362
Test: Run CHQTS and verify it now passes on a device previously seeing
null package names

Change-Id: Ia706a503f9ca068fc997c0acdcd74ca62a1424be
parent 79b80c34
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -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;