am 16cfa8dd: Merge "AudioManager: make AudioPortEventHandler static" into lmp-mr1-dev

* commit '16cfa8ddfd6c18577f5d38e545597a63889d9779':
  AudioManager: make AudioPortEventHandler static
This commit is contained in:
Eric Laurent
2015-01-15 22:16:28 +00:00
committed by Android Git Automerger
2 changed files with 92 additions and 90 deletions

View File

@ -65,7 +65,7 @@ public class AudioManager {
private final boolean mUseFixedVolume; private final boolean mUseFixedVolume;
private final Binder mToken = new Binder(); private final Binder mToken = new Binder();
private static String TAG = "AudioManager"; private static String TAG = "AudioManager";
AudioPortEventHandler mAudioPortEventHandler; private static final AudioPortEventHandler sAudioPortEventHandler = new AudioPortEventHandler();
/** /**
* Broadcast intent, a hint for applications that audio is about to become * Broadcast intent, a hint for applications that audio is about to become
@ -646,9 +646,9 @@ public class AudioManager {
com.android.internal.R.bool.config_useMasterVolume); com.android.internal.R.bool.config_useMasterVolume);
mUseVolumeKeySounds = mContext.getResources().getBoolean( mUseVolumeKeySounds = mContext.getResources().getBoolean(
com.android.internal.R.bool.config_useVolumeKeySounds); com.android.internal.R.bool.config_useVolumeKeySounds);
mAudioPortEventHandler = new AudioPortEventHandler(this);
mUseFixedVolume = mContext.getResources().getBoolean( mUseFixedVolume = mContext.getResources().getBoolean(
com.android.internal.R.bool.config_useFixedVolume); com.android.internal.R.bool.config_useFixedVolume);
sAudioPortEventHandler.init();
} }
private static IAudioService getService() private static IAudioService getService()
@ -3607,7 +3607,7 @@ public class AudioManager {
* @hide * @hide
*/ */
public void registerAudioPortUpdateListener(OnAudioPortUpdateListener l) { public void registerAudioPortUpdateListener(OnAudioPortUpdateListener l) {
mAudioPortEventHandler.registerListener(l); sAudioPortEventHandler.registerListener(l);
} }
/** /**
@ -3615,7 +3615,7 @@ public class AudioManager {
* @hide * @hide
*/ */
public void unregisterAudioPortUpdateListener(OnAudioPortUpdateListener l) { public void unregisterAudioPortUpdateListener(OnAudioPortUpdateListener l) {
mAudioPortEventHandler.unregisterListener(l); sAudioPortEventHandler.unregisterListener(l);
} }
// //
@ -3623,23 +3623,23 @@ public class AudioManager {
// //
static final int AUDIOPORT_GENERATION_INIT = 0; static final int AUDIOPORT_GENERATION_INIT = 0;
Integer mAudioPortGeneration = new Integer(AUDIOPORT_GENERATION_INIT); static Integer sAudioPortGeneration = new Integer(AUDIOPORT_GENERATION_INIT);
ArrayList<AudioPort> mAudioPortsCached = new ArrayList<AudioPort>(); static ArrayList<AudioPort> sAudioPortsCached = new ArrayList<AudioPort>();
ArrayList<AudioPatch> mAudioPatchesCached = new ArrayList<AudioPatch>(); static ArrayList<AudioPatch> sAudioPatchesCached = new ArrayList<AudioPatch>();
int resetAudioPortGeneration() { static int resetAudioPortGeneration() {
int generation; int generation;
synchronized (mAudioPortGeneration) { synchronized (sAudioPortGeneration) {
generation = mAudioPortGeneration; generation = sAudioPortGeneration;
mAudioPortGeneration = AUDIOPORT_GENERATION_INIT; sAudioPortGeneration = AUDIOPORT_GENERATION_INIT;
} }
return generation; return generation;
} }
int updateAudioPortCache(ArrayList<AudioPort> ports, ArrayList<AudioPatch> patches) { static int updateAudioPortCache(ArrayList<AudioPort> ports, ArrayList<AudioPatch> patches) {
synchronized (mAudioPortGeneration) { synchronized (sAudioPortGeneration) {
if (mAudioPortGeneration == AUDIOPORT_GENERATION_INIT) { if (sAudioPortGeneration == AUDIOPORT_GENERATION_INIT) {
int[] patchGeneration = new int[1]; int[] patchGeneration = new int[1];
int[] portGeneration = new int[1]; int[] portGeneration = new int[1];
int status; int status;
@ -3678,23 +3678,23 @@ public class AudioManager {
} }
} }
mAudioPortsCached = newPorts; sAudioPortsCached = newPorts;
mAudioPatchesCached = newPatches; sAudioPatchesCached = newPatches;
mAudioPortGeneration = portGeneration[0]; sAudioPortGeneration = portGeneration[0];
} }
if (ports != null) { if (ports != null) {
ports.clear(); ports.clear();
ports.addAll(mAudioPortsCached); ports.addAll(sAudioPortsCached);
} }
if (patches != null) { if (patches != null) {
patches.clear(); patches.clear();
patches.addAll(mAudioPatchesCached); patches.addAll(sAudioPatchesCached);
} }
} }
return SUCCESS; return SUCCESS;
} }
AudioPortConfig updatePortConfig(AudioPortConfig portCfg, ArrayList<AudioPort> ports) { static AudioPortConfig updatePortConfig(AudioPortConfig portCfg, ArrayList<AudioPort> ports) {
AudioPort port = portCfg.port(); AudioPort port = portCfg.port();
int k; int k;
for (k = 0; k < ports.size(); k++) { for (k = 0; k < ports.size(); k++) {

View File

@ -31,94 +31,96 @@ import java.lang.ref.WeakReference;
*/ */
class AudioPortEventHandler { class AudioPortEventHandler {
private final Handler mHandler; private Handler mHandler;
private ArrayList<AudioManager.OnAudioPortUpdateListener> mListeners; private final ArrayList<AudioManager.OnAudioPortUpdateListener> mListeners =
private AudioManager mAudioManager; new ArrayList<AudioManager.OnAudioPortUpdateListener>();
private static String TAG = "AudioPortEventHandler"; private static final String TAG = "AudioPortEventHandler";
private static final int AUDIOPORT_EVENT_PORT_LIST_UPDATED = 1; private static final int AUDIOPORT_EVENT_PORT_LIST_UPDATED = 1;
private static final int AUDIOPORT_EVENT_PATCH_LIST_UPDATED = 2; private static final int AUDIOPORT_EVENT_PATCH_LIST_UPDATED = 2;
private static final int AUDIOPORT_EVENT_SERVICE_DIED = 3; private static final int AUDIOPORT_EVENT_SERVICE_DIED = 3;
private static final int AUDIOPORT_EVENT_NEW_LISTENER = 4; private static final int AUDIOPORT_EVENT_NEW_LISTENER = 4;
AudioPortEventHandler(AudioManager audioManager) { void init() {
mAudioManager = audioManager; synchronized (this) {
mListeners = new ArrayList<AudioManager.OnAudioPortUpdateListener>(); if (mHandler != null) {
return;
}
// find the looper for our new event handler
Looper looper = Looper.getMainLooper();
// find the looper for our new event handler if (looper != null) {
Looper looper = Looper.getMainLooper(); mHandler = new Handler(looper) {
@Override
if (looper != null) { public void handleMessage(Message msg) {
mHandler = new Handler(looper) { ArrayList<AudioManager.OnAudioPortUpdateListener> listeners;
@Override synchronized (this) {
public void handleMessage(Message msg) { if (msg.what == AUDIOPORT_EVENT_NEW_LISTENER) {
ArrayList<AudioManager.OnAudioPortUpdateListener> listeners; listeners = new ArrayList<AudioManager.OnAudioPortUpdateListener>();
synchronized (this) { if (mListeners.contains(msg.obj)) {
if (msg.what == AUDIOPORT_EVENT_NEW_LISTENER) { listeners.add((AudioManager.OnAudioPortUpdateListener)msg.obj);
listeners = new ArrayList<AudioManager.OnAudioPortUpdateListener>(); }
if (mListeners.contains(msg.obj)) { } else {
listeners.add((AudioManager.OnAudioPortUpdateListener)msg.obj); listeners = mListeners;
} }
} else {
listeners = mListeners;
} }
} if (listeners.isEmpty()) {
if (listeners.isEmpty()) {
return;
}
// reset audio port cache if the event corresponds to a change coming
// from audio policy service or if mediaserver process died.
if (msg.what == AUDIOPORT_EVENT_PORT_LIST_UPDATED ||
msg.what == AUDIOPORT_EVENT_PATCH_LIST_UPDATED ||
msg.what == AUDIOPORT_EVENT_SERVICE_DIED) {
mAudioManager.resetAudioPortGeneration();
}
ArrayList<AudioPort> ports = new ArrayList<AudioPort>();
ArrayList<AudioPatch> patches = new ArrayList<AudioPatch>();
if (msg.what != AUDIOPORT_EVENT_SERVICE_DIED) {
int status = mAudioManager.updateAudioPortCache(ports, patches);
if (status != AudioManager.SUCCESS) {
return; return;
} }
} // reset audio port cache if the event corresponds to a change coming
// from audio policy service or if mediaserver process died.
switch (msg.what) { if (msg.what == AUDIOPORT_EVENT_PORT_LIST_UPDATED ||
case AUDIOPORT_EVENT_NEW_LISTENER: msg.what == AUDIOPORT_EVENT_PATCH_LIST_UPDATED ||
case AUDIOPORT_EVENT_PORT_LIST_UPDATED: msg.what == AUDIOPORT_EVENT_SERVICE_DIED) {
AudioPort[] portList = ports.toArray(new AudioPort[0]); AudioManager.resetAudioPortGeneration();
for (int i = 0; i < listeners.size(); i++) {
listeners.get(i).onAudioPortListUpdate(portList);
} }
if (msg.what == AUDIOPORT_EVENT_PORT_LIST_UPDATED) { ArrayList<AudioPort> ports = new ArrayList<AudioPort>();
ArrayList<AudioPatch> patches = new ArrayList<AudioPatch>();
if (msg.what != AUDIOPORT_EVENT_SERVICE_DIED) {
int status = AudioManager.updateAudioPortCache(ports, patches);
if (status != AudioManager.SUCCESS) {
return;
}
}
switch (msg.what) {
case AUDIOPORT_EVENT_NEW_LISTENER:
case AUDIOPORT_EVENT_PORT_LIST_UPDATED:
AudioPort[] portList = ports.toArray(new AudioPort[0]);
for (int i = 0; i < listeners.size(); i++) {
listeners.get(i).onAudioPortListUpdate(portList);
}
if (msg.what == AUDIOPORT_EVENT_PORT_LIST_UPDATED) {
break;
}
// FALL THROUGH
case AUDIOPORT_EVENT_PATCH_LIST_UPDATED:
AudioPatch[] patchList = patches.toArray(new AudioPatch[0]);
for (int i = 0; i < listeners.size(); i++) {
listeners.get(i).onAudioPatchListUpdate(patchList);
}
break;
case AUDIOPORT_EVENT_SERVICE_DIED:
for (int i = 0; i < listeners.size(); i++) {
listeners.get(i).onServiceDied();
}
break;
default:
break; break;
} }
// FALL THROUGH
case AUDIOPORT_EVENT_PATCH_LIST_UPDATED:
AudioPatch[] patchList = patches.toArray(new AudioPatch[0]);
for (int i = 0; i < listeners.size(); i++) {
listeners.get(i).onAudioPatchListUpdate(patchList);
}
break;
case AUDIOPORT_EVENT_SERVICE_DIED:
for (int i = 0; i < listeners.size(); i++) {
listeners.get(i).onServiceDied();
}
break;
default:
break;
} }
} };
}; native_setup(new WeakReference<AudioPortEventHandler>(this));
} else { } else {
mHandler = null; mHandler = null;
}
} }
native_setup(new WeakReference<AudioPortEventHandler>(this));
} }
private native void native_setup(Object module_this); private native void native_setup(Object module_this);
@Override @Override