FileDocCategorySizeDatePackage
AudioPortEventHandler.javaAPI DocAndroid 5.1 API6574Thu Mar 12 22:22:30 GMT 2015android.media

AudioPortEventHandler

public class AudioPortEventHandler extends Object
The AudioPortEventHandler handles AudioManager.OnAudioPortUpdateListener callbacks posted from JNI
hide

Fields Summary
private android.os.Handler
mHandler
private final ArrayList
mListeners
private static final String
TAG
private static final int
AUDIOPORT_EVENT_PORT_LIST_UPDATED
private static final int
AUDIOPORT_EVENT_PATCH_LIST_UPDATED
private static final int
AUDIOPORT_EVENT_SERVICE_DIED
private static final int
AUDIOPORT_EVENT_NEW_LISTENER
Constructors Summary
Methods Summary
protected voidfinalize()

        native_finalize();
    
android.os.Handlerhandler()

        return mHandler;
    
voidinit()


      
        synchronized (this) {
            if (mHandler != null) {
                return;
            }
            // find the looper for our new event handler
            Looper looper = Looper.getMainLooper();

            if (looper != null) {
                mHandler = new Handler(looper) {
                    @Override
                    public void handleMessage(Message msg) {
                        ArrayList<AudioManager.OnAudioPortUpdateListener> listeners;
                        synchronized (this) {
                            if (msg.what == AUDIOPORT_EVENT_NEW_LISTENER) {
                                listeners = new ArrayList<AudioManager.OnAudioPortUpdateListener>();
                                if (mListeners.contains(msg.obj)) {
                                    listeners.add((AudioManager.OnAudioPortUpdateListener)msg.obj);
                                }
                            } else {
                                listeners = mListeners;
                            }
                        }
                        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) {
                            AudioManager.resetAudioPortGeneration();
                        }
                        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;
                        }
                    }
                };
                native_setup(new WeakReference<AudioPortEventHandler>(this));
            } else {
                mHandler = null;
            }
        }
    
private native voidnative_finalize()

private native voidnative_setup(java.lang.Object module_this)

private static voidpostEventFromNative(java.lang.Object module_ref, int what, int arg1, int arg2, java.lang.Object obj)

        AudioPortEventHandler eventHandler =
                (AudioPortEventHandler)((WeakReference)module_ref).get();
        if (eventHandler == null) {
            return;
        }

        if (eventHandler != null) {
            Handler handler = eventHandler.handler();
            if (handler != null) {
                Message m = handler.obtainMessage(what, arg1, arg2, obj);
                handler.sendMessage(m);
            }
        }
    
voidregisterListener(AudioManager.OnAudioPortUpdateListener l)

        synchronized (this) {
            mListeners.add(l);
        }
        if (mHandler != null) {
            Message m = mHandler.obtainMessage(AUDIOPORT_EVENT_NEW_LISTENER, 0, 0, l);
            mHandler.sendMessage(m);
        }
    
voidunregisterListener(AudioManager.OnAudioPortUpdateListener l)

        synchronized (this) {
            mListeners.remove(l);
        }