FileDocCategorySizeDatePackage
DelayedMessageBuffer.javaAPI DocAndroid 5.1 API5524Thu Mar 12 22:22:42 GMT 2015com.android.server.hdmi

DelayedMessageBuffer

public final class DelayedMessageBuffer extends Object
Buffer storage to keep incoming messages for later processing. Used to handle messages that arrive when the device is not ready. Useful when keeping the messages from a connected device which are not discovered yet.

Fields Summary
private final ArrayList
mBuffer
private final HdmiCecLocalDevice
mDevice
Constructors Summary
DelayedMessageBuffer(HdmiCecLocalDevice device)


      
        mDevice = device;
    
Methods Summary
voidadd(HdmiCecMessage message)
Add a new message to the buffer. The buffer keeps selected messages in the order they are received.

param
message {@link HdmiCecMessage} to add

        boolean buffered = true;

        // Note that all the messages are not handled in the same manner.
        // For <Active Source> we keep the latest one only.
        // TODO: This might not be the best way to choose the active source.
        //       Devise a better way to pick up the best one.
        switch (message.getOpcode()) {
            case Constants.MESSAGE_ACTIVE_SOURCE:
                removeActiveSource();
                mBuffer.add(message);
                break;
            case Constants.MESSAGE_INITIATE_ARC:
            case Constants.MESSAGE_SET_SYSTEM_AUDIO_MODE:
                mBuffer.add(message);
                break;
            default:
                buffered = false;
                break;
        }
        if (buffered) {
            HdmiLogger.debug("Buffering message:" + message);
        }
    
booleanisBuffered(int opcode)

        for (HdmiCecMessage message : mBuffer) {
            if (message.getOpcode() == opcode) {
                return true;
            }
        }
        return false;
    
voidprocessActiveSource(int address)
Process <Active Source>.

The message has a dependency on TV input framework. Should be invoked after we get the callback {@link android.media.tv.TvInputManager.TvInputCallback#onInputAdded(String)} to ensure the processing of the message takes effect when transformed to input change callback.

param
address logical address of the device to be the active source

        ArrayList<HdmiCecMessage> copiedBuffer = new ArrayList<HdmiCecMessage>(mBuffer);
        mBuffer.clear();
        for (HdmiCecMessage message : copiedBuffer) {
            if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE
                    && message.getSource() == address) {
                mDevice.onMessage(message);
                HdmiLogger.debug("Processing message:" + message);
            } else {
                mBuffer.add(message);
            }
        }
    
voidprocessAllMessages()

        // Use the copied buffer.
        ArrayList<HdmiCecMessage> copiedBuffer = new ArrayList<HdmiCecMessage>(mBuffer);
        mBuffer.clear();
        for (HdmiCecMessage message : copiedBuffer) {
            mDevice.onMessage(message);
            HdmiLogger.debug("Processing message:" + message);
        }
    
voidprocessMessagesForDevice(int address)
Process messages from a given logical device. Called by {@link NewDeviceAction} actions when they finish adding the device information.

<Active Source> is processed only when the TV input is ready. If not, {@link #processActiveSource()} will be invoked later to handle it.

param
address logical address of CEC device which the messages to process are associated with

        ArrayList<HdmiCecMessage> copiedBuffer = new ArrayList<HdmiCecMessage>(mBuffer);
        mBuffer.clear();
        HdmiLogger.debug("Checking message for address:" + address);
        for (HdmiCecMessage message : copiedBuffer) {
            if (message.getSource() != address) {
                mBuffer.add(message);
                continue;
            }
            if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE
                    && !mDevice.isInputReady(HdmiDeviceInfo.idForCecDevice(address))) {
                mBuffer.add(message);
                continue;
            }
            mDevice.onMessage(message);
            HdmiLogger.debug("Processing message:" + message);
        }
    
private voidremoveActiveSource()

        // Uses iterator to remove elements while looping through the list.
        for (Iterator<HdmiCecMessage> iter = mBuffer.iterator(); iter.hasNext(); ) {
            HdmiCecMessage message = iter.next();
            if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE) {
                iter.remove();
            }
        }