Merge "Fix a lock issue in AudioPortEventHandler" am: 0985eea389 am: 41c9e7092e am: 4b98ce6898
am: bb3c59e776
Change-Id: I8b84cbaceae238201ef65cfd4168def364e66d4e
diff --git a/media/java/android/media/AudioPortEventHandler.java b/media/java/android/media/AudioPortEventHandler.java
index f9a4b1e..6d9d626 100644
--- a/media/java/android/media/AudioPortEventHandler.java
+++ b/media/java/android/media/AudioPortEventHandler.java
@@ -19,10 +19,12 @@
import android.annotation.UnsupportedAppUsage;
import android.os.Handler;
import android.os.HandlerThread;
-import android.os.Looper;
import android.os.Message;
-import java.util.ArrayList;
+
+import com.android.internal.annotations.GuardedBy;
+
import java.lang.ref.WeakReference;
+import java.util.ArrayList;
/**
* The AudioPortEventHandler handles AudioManager.OnAudioPortUpdateListener callbacks
@@ -33,6 +35,9 @@
class AudioPortEventHandler {
private Handler mHandler;
private HandlerThread mHandlerThread;
+ private final Object mLock = new Object();
+
+ @GuardedBy("mLock")
private final ArrayList<AudioManager.OnAudioPortUpdateListener> mListeners =
new ArrayList<AudioManager.OnAudioPortUpdateListener>();
@@ -53,7 +58,7 @@
private long mJniCallback;
void init() {
- synchronized (this) {
+ synchronized (mLock) {
if (mHandler != null) {
return;
}
@@ -66,7 +71,7 @@
@Override
public void handleMessage(Message msg) {
ArrayList<AudioManager.OnAudioPortUpdateListener> listeners;
- synchronized (this) {
+ synchronized (mLock) {
if (msg.what == AUDIOPORT_EVENT_NEW_LISTENER) {
listeners = new ArrayList<AudioManager.OnAudioPortUpdateListener>();
if (mListeners.contains(msg.obj)) {
@@ -152,7 +157,7 @@
private native void native_finalize();
void registerListener(AudioManager.OnAudioPortUpdateListener l) {
- synchronized (this) {
+ synchronized (mLock) {
mListeners.add(l);
}
if (mHandler != null) {
@@ -162,7 +167,7 @@
}
void unregisterListener(AudioManager.OnAudioPortUpdateListener l) {
- synchronized (this) {
+ synchronized (mLock) {
mListeners.remove(l);
}
}