summaryrefslogtreecommitdiffstats
path: root/location/java/android/location/LocalListenerHelper.java
diff options
context:
space:
mode:
Diffstat (limited to 'location/java/android/location/LocalListenerHelper.java')
-rw-r--r--location/java/android/location/LocalListenerHelper.java25
1 files changed, 14 insertions, 11 deletions
diff --git a/location/java/android/location/LocalListenerHelper.java b/location/java/android/location/LocalListenerHelper.java
index 1f3bf67..458c451 100644
--- a/location/java/android/location/LocalListenerHelper.java
+++ b/location/java/android/location/LocalListenerHelper.java
@@ -19,6 +19,7 @@ package android.location;
import com.android.internal.util.Preconditions;
import android.annotation.NonNull;
+import android.content.Context;
import android.os.RemoteException;
import android.util.Log;
@@ -32,17 +33,19 @@ import java.util.HashSet;
* @hide
*/
abstract class LocalListenerHelper<TListener> {
- private final HashSet<TListener> mListeners = new HashSet<TListener>();
+ private final HashSet<TListener> mListeners = new HashSet<>();
+
private final String mTag;
+ private final Context mContext;
- protected LocalListenerHelper(String name) {
+ protected LocalListenerHelper(Context context, String name) {
Preconditions.checkNotNull(name);
+ mContext = context;
mTag = name;
}
public boolean add(@NonNull TListener listener) {
Preconditions.checkNotNull(listener);
-
synchronized (mListeners) {
// we need to register with the service first, because we need to find out if the
// service will actually support the request before we attempt anything
@@ -59,18 +62,15 @@ abstract class LocalListenerHelper<TListener> {
return false;
}
}
-
if (mListeners.contains(listener)) {
return true;
}
- mListeners.add(listener);
+ return mListeners.add(listener);
}
- return true;
}
public void remove(@NonNull TListener listener) {
Preconditions.checkNotNull(listener);
-
synchronized (mListeners) {
boolean removed = mListeners.remove(listener);
boolean isLastRemoved = removed && mListeners.isEmpty();
@@ -78,7 +78,7 @@ abstract class LocalListenerHelper<TListener> {
try {
unregisterFromServer();
} catch (RemoteException e) {
-
+ Log.v(mTag, "Error handling last listener removal", e);
}
}
}
@@ -91,12 +91,15 @@ abstract class LocalListenerHelper<TListener> {
void execute(TListener listener) throws RemoteException;
}
- protected void foreach(ListenerOperation operation) {
+ protected Context getContext() {
+ return mContext;
+ }
+
+ protected void foreach(ListenerOperation<TListener> operation) {
Collection<TListener> listeners;
synchronized (mListeners) {
- listeners = new ArrayList<TListener>(mListeners);
+ listeners = new ArrayList<>(mListeners);
}
-
for (TListener listener : listeners) {
try {
operation.execute(listener);