Merge "Adding lock to sendEventFromNative" into rvc-dev am: 50980050a6 am: 6d69fa96d8
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/11720016 Change-Id: I3c012c18cb12b48151eec66836dc2f9125b96fc4
This commit is contained in:
@@ -16,28 +16,22 @@
|
|||||||
|
|
||||||
package android.media;
|
package android.media;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileDescriptor;
|
|
||||||
import java.lang.ref.WeakReference;
|
|
||||||
|
|
||||||
import android.annotation.NonNull;
|
import android.annotation.NonNull;
|
||||||
import android.annotation.Nullable;
|
import android.annotation.Nullable;
|
||||||
import android.app.ActivityThread;
|
|
||||||
import android.app.AppOpsManager;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.AssetFileDescriptor;
|
import android.content.res.AssetFileDescriptor;
|
||||||
import android.media.PlayerBase;
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
import android.os.ParcelFileDescriptor;
|
import android.os.ParcelFileDescriptor;
|
||||||
import android.os.Process;
|
|
||||||
import android.os.RemoteException;
|
|
||||||
import android.os.ServiceManager;
|
|
||||||
import android.util.AndroidRuntimeException;
|
import android.util.AndroidRuntimeException;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileDescriptor;
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The SoundPool class manages and plays audio resources for applications.
|
* The SoundPool class manages and plays audio resources for applications.
|
||||||
@@ -122,13 +116,12 @@ public class SoundPool extends PlayerBase {
|
|||||||
private final static String TAG = "SoundPool";
|
private final static String TAG = "SoundPool";
|
||||||
private final static boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
|
private final static boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
|
||||||
|
|
||||||
|
private final AtomicReference<EventHandler> mEventHandler = new AtomicReference<>(null);
|
||||||
|
|
||||||
private long mNativeContext; // accessed by native methods
|
private long mNativeContext; // accessed by native methods
|
||||||
|
|
||||||
private EventHandler mEventHandler;
|
|
||||||
private SoundPool.OnLoadCompleteListener mOnLoadCompleteListener;
|
|
||||||
private boolean mHasAppOpsPlayAudio;
|
private boolean mHasAppOpsPlayAudio;
|
||||||
|
|
||||||
private final Object mLock;
|
|
||||||
private final AudioAttributes mAttributes;
|
private final AudioAttributes mAttributes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -159,7 +152,6 @@ public class SoundPool extends PlayerBase {
|
|||||||
if (native_setup(new WeakReference<SoundPool>(this), maxStreams, attributes) != 0) {
|
if (native_setup(new WeakReference<SoundPool>(this), maxStreams, attributes) != 0) {
|
||||||
throw new RuntimeException("Native setup failed");
|
throw new RuntimeException("Native setup failed");
|
||||||
}
|
}
|
||||||
mLock = new Object();
|
|
||||||
mAttributes = attributes;
|
mAttributes = attributes;
|
||||||
|
|
||||||
baseRegisterPlayer();
|
baseRegisterPlayer();
|
||||||
@@ -491,21 +483,18 @@ public class SoundPool extends PlayerBase {
|
|||||||
* Sets the callback hook for the OnLoadCompleteListener.
|
* Sets the callback hook for the OnLoadCompleteListener.
|
||||||
*/
|
*/
|
||||||
public void setOnLoadCompleteListener(OnLoadCompleteListener listener) {
|
public void setOnLoadCompleteListener(OnLoadCompleteListener listener) {
|
||||||
synchronized(mLock) {
|
if (listener == null) {
|
||||||
if (listener != null) {
|
mEventHandler.set(null);
|
||||||
// setup message handler
|
return;
|
||||||
Looper looper;
|
}
|
||||||
if ((looper = Looper.myLooper()) != null) {
|
|
||||||
mEventHandler = new EventHandler(looper);
|
Looper looper;
|
||||||
} else if ((looper = Looper.getMainLooper()) != null) {
|
if ((looper = Looper.myLooper()) != null) {
|
||||||
mEventHandler = new EventHandler(looper);
|
mEventHandler.set(new EventHandler(looper, listener));
|
||||||
} else {
|
} else if ((looper = Looper.getMainLooper()) != null) {
|
||||||
mEventHandler = null;
|
mEventHandler.set(new EventHandler(looper, listener));
|
||||||
}
|
} else {
|
||||||
} else {
|
mEventHandler.set(null);
|
||||||
mEventHandler = null;
|
|
||||||
}
|
|
||||||
mOnLoadCompleteListener = listener;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -525,35 +514,36 @@ public class SoundPool extends PlayerBase {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private static void postEventFromNative(Object ref, int msg, int arg1, int arg2, Object obj) {
|
private static void postEventFromNative(Object ref, int msg, int arg1, int arg2, Object obj) {
|
||||||
SoundPool soundPool = ((WeakReference<SoundPool>) ref).get();
|
SoundPool soundPool = ((WeakReference<SoundPool>) ref).get();
|
||||||
if (soundPool == null)
|
if (soundPool == null) {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (soundPool.mEventHandler != null) {
|
|
||||||
Message m = soundPool.mEventHandler.obtainMessage(msg, arg1, arg2, obj);
|
|
||||||
soundPool.mEventHandler.sendMessage(m);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Handler eventHandler = soundPool.mEventHandler.get();
|
||||||
|
if (eventHandler == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Message message = eventHandler.obtainMessage(msg, arg1, arg2, obj);
|
||||||
|
eventHandler.sendMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class EventHandler extends Handler {
|
private final class EventHandler extends Handler {
|
||||||
public EventHandler(Looper looper) {
|
private final OnLoadCompleteListener mOnLoadCompleteListener;
|
||||||
|
|
||||||
|
EventHandler(Looper looper, @NonNull OnLoadCompleteListener onLoadCompleteListener) {
|
||||||
super(looper);
|
super(looper);
|
||||||
|
mOnLoadCompleteListener = onLoadCompleteListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleMessage(Message msg) {
|
public void handleMessage(Message msg) {
|
||||||
switch(msg.what) {
|
if (msg.what != SAMPLE_LOADED) {
|
||||||
case SAMPLE_LOADED:
|
|
||||||
if (DEBUG) Log.d(TAG, "Sample " + msg.arg1 + " loaded");
|
|
||||||
synchronized(mLock) {
|
|
||||||
if (mOnLoadCompleteListener != null) {
|
|
||||||
mOnLoadCompleteListener.onLoadComplete(SoundPool.this, msg.arg1, msg.arg2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Log.e(TAG, "Unknown message type " + msg.what);
|
Log.e(TAG, "Unknown message type " + msg.what);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (DEBUG) Log.d(TAG, "Sample " + msg.arg1 + " loaded");
|
||||||
|
mOnLoadCompleteListener.onLoadComplete(SoundPool.this, msg.arg1, msg.arg2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user