From b219baa01494f49a217000716e902315214105ae Mon Sep 17 00:00:00 2001 From: Soonil Nagarkar Date: Thu, 7 Jul 2022 11:43:48 -0700 Subject: Refactor listener multiplexer -Simplifies class structure around ListenerRegistration, moving responsibility for requests into subclasses, adding an onRemove() callback, and simplifying the overall class structure. -Eliminates two locks (1 in ListenerMultiplexer, 1 in LocationProviderManager) in favor of sharing the same lock. This simplifies locking and reduces the changes of deadlock by messing something up. -Fixes a bug around callback invocation ordering ListenerMultiplexer.onRegistrationReplaced. -Overall normalizes ListenerMultiplexer usages with respect to other codebases. Test: presubmits Change-Id: I8ad92c1ffe802eee17f5a5774c8ecee1d875252f --- .../location/util/identity/CallerIdentity.java | 29 ++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'location/java') diff --git a/location/java/android/location/util/identity/CallerIdentity.java b/location/java/android/location/util/identity/CallerIdentity.java index ade0ea40e157..2f8d92b06f03 100644 --- a/location/java/android/location/util/identity/CallerIdentity.java +++ b/location/java/android/location/util/identity/CallerIdentity.java @@ -124,15 +124,22 @@ public final class CallerIdentity { packageName, attributionTag, listenerId); } + // in some tests these constants are loaded too early leading to an "incorrect" view of the + // current pid and uid. load lazily to prevent this problem in tests. + private static class Loader { + private static final int MY_UID = Process.myUid(); + private static final int MY_PID = Process.myPid(); + } + private final int mUid; private final int mPid; private final String mPackageName; - private final @Nullable String mAttributionTag; + @Nullable private final String mAttributionTag; - private final @Nullable String mListenerId; + @Nullable private final String mListenerId; private CallerIdentity(int uid, int pid, String packageName, @Nullable String attributionTag, @Nullable String listenerId) { @@ -181,6 +188,24 @@ public final class CallerIdentity { return mUid == Process.SYSTEM_UID; } + /** Returns true if this identity represents the same user this code is running in. */ + public boolean isMyUser() { + return UserHandle.getUserId(mUid) == UserHandle.getUserId(Loader.MY_UID); + } + + /** Returns true if this identity represents the same uid this code is running in. */ + public boolean isMyUid() { + return mUid == Loader.MY_UID; + } + + /** + * Returns true if this identity represents the same process this code is running in. Returns + * false if the identity process is unknown. + */ + public boolean isMyProcess() { + return mPid == Loader.MY_PID; + } + /** * Adds this identity to the worksource supplied, or if not worksource is supplied, creates a * new worksource representing this identity. -- cgit v1.2.3-59-g8ed1b