summaryrefslogtreecommitdiff
path: root/location/java
diff options
context:
space:
mode:
author Soonil Nagarkar <sooniln@google.com> 2020-02-28 16:57:17 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2020-02-28 16:57:17 +0000
commit5e793ba9d74ed89e6c052f74f4103c05b617cc08 (patch)
treed3638c1e139ba338e9c441607e03d5e4dfa6dd1e /location/java
parente724120c4d008a6028cd0c31361aca6d6ff3f29b (diff)
parentafe1adfb1dfa7d39bfb950968296f9e2034de536 (diff)
Merge "Updates and fixes to GnssMeasurementsProvider"
Diffstat (limited to 'location/java')
-rw-r--r--location/java/android/location/util/listeners/AbstractListenerManager.java90
1 files changed, 57 insertions, 33 deletions
diff --git a/location/java/android/location/util/listeners/AbstractListenerManager.java b/location/java/android/location/util/listeners/AbstractListenerManager.java
index d2adab0aa9d9..facb74766f2c 100644
--- a/location/java/android/location/util/listeners/AbstractListenerManager.java
+++ b/location/java/android/location/util/listeners/AbstractListenerManager.java
@@ -78,13 +78,6 @@ public abstract class AbstractListenerManager<TKey, TRequest, TListener,
return mRequest;
}
- /**
- * Returns the listener, or null if this registration is no longer registered.
- */
- protected @Nullable TListener getListener() {
- return mListener;
- }
-
boolean register() {
Preconditions.checkState(mListener != null);
return onRegister();
@@ -146,6 +139,9 @@ public abstract class AbstractListenerManager<TKey, TRequest, TListener,
private final ReentrancyGuard mReentrancyGuard = new ReentrancyGuard();
@GuardedBy("mRegistrations")
+ private boolean mActiveRegistrations = false;
+
+ @GuardedBy("mRegistrations")
private boolean mServiceRegistered = false;
@GuardedBy("mRegistrations")
@@ -216,36 +212,47 @@ public abstract class AbstractListenerManager<TKey, TRequest, TListener,
updateService();
}
- @GuardedBy("mRegistrations")
- private void updateService() {
- if (Build.IS_DEBUGGABLE) {
- Preconditions.checkState(Thread.holdsLock(mRegistrations));
- }
-
- ArrayList<TRegistration> actives = new ArrayList<>(mRegistrations.size());
- for (int i = 0; i < mRegistrations.size(); i++) {
- TRegistration registration = mRegistrations.valueAt(i);
- if (isActive(registration)) {
- actives.add(registration);
+ /**
+ * Forces a re-evalution of the active state of all registrations, the merged request for all
+ * active registrations, and service registration state.
+ */
+ protected void updateService() {
+ synchronized (mRegistrations) {
+ ArrayList<TRegistration> actives = new ArrayList<>(mRegistrations.size());
+ for (int i = 0; i < mRegistrations.size(); i++) {
+ TRegistration registration = mRegistrations.valueAt(i);
+ if (isActive(registration)) {
+ actives.add(registration);
+ }
}
- }
- if (actives.isEmpty()) {
- if (mServiceRegistered) {
- unregisterService();
- mServiceRegistered = false;
+ if (actives.isEmpty()) {
+ if (mServiceRegistered) {
+ unregisterService();
+ mServiceRegistered = false;
+ }
+ mCurrentRequest = null;
+
+ if (mActiveRegistrations) {
+ mActiveRegistrations = false;
+ onInactive();
+ }
+ return;
+ } else {
+ if (!mActiveRegistrations) {
+ mActiveRegistrations = true;
+ onActive();
+ }
}
- mCurrentRequest = null;
- return;
- }
- TMergedRequest merged = mergeRequests(actives);
- if (!mServiceRegistered || !Objects.equals(merged, mCurrentRequest)) {
- if (mServiceRegistered) {
- unregisterService();
+ TMergedRequest merged = mergeRequests(actives);
+ if (!mServiceRegistered || !Objects.equals(merged, mCurrentRequest)) {
+ if (mServiceRegistered) {
+ unregisterService();
+ }
+ mCurrentRequest = merged;
+ mServiceRegistered = registerService(mCurrentRequest);
}
- mCurrentRequest = merged;
- mServiceRegistered = registerService(mCurrentRequest);
}
}
@@ -264,7 +271,8 @@ public abstract class AbstractListenerManager<TKey, TRequest, TListener,
/**
* Performs some function on all (not just active) registrations. The function should return
* true if the active state of the registration has changed, or if the change to the
- * registration may have changed the result of {@link #mergeRequests(List)}.
+ * registration may have changed the result of {@link #mergeRequests(List)}. If the function
+ * returns true for any registration, {@link #updateService()} will be invoked.
*/
protected final void updateRegistrations(@NonNull Function<TRegistration, Boolean> function) {
synchronized (mRegistrations) {
@@ -308,6 +316,22 @@ public abstract class AbstractListenerManager<TKey, TRequest, TListener,
protected abstract void unregisterService();
/**
+ * Invoked when the listener goes from having no active registrations to having some active
+ * registrations. This is a convenient entry point for registering listeners, etc, which only
+ * need to be present while there are active registrations. This method will always be invoked
+ * before a corrosponding call to {@link #registerService(Object)}.
+ */
+ protected void onActive() {}
+
+ /**
+ * Invoked when the listener goes from having some active registrations to having no active
+ * registrations. This is a convenient entry point for unregistering listeners, etc, which only
+ * need to be present while there are active registrations. This method will always be invoked
+ * after a corrosponding call to {@link #unregisterService()}.
+ */
+ protected void onInactive() {}
+
+ /**
* Invoked when a registration is added.
*/
protected void onRegistrationAdded(@NonNull TKey key, @NonNull TRegistration registration) {}