DelayedMessageBuffer.java revision 6e26f7f7b09dfd8495ec5478a7a4713dab346bc1
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server.hdmi; 18 19import android.hardware.hdmi.HdmiDeviceInfo; 20import android.util.Slog; 21 22import java.util.ArrayList; 23import java.util.Iterator; 24 25/** 26 * Buffer storage to keep incoming messages for later processing. Used to 27 * handle messages that arrive when the device is not ready. Useful when 28 * keeping the messages from a connected device which are not discovered yet. 29 */ 30final class DelayedMessageBuffer { 31 private final ArrayList<HdmiCecMessage> mBuffer = new ArrayList<>(); 32 private final HdmiCecLocalDevice mDevice; 33 34 DelayedMessageBuffer(HdmiCecLocalDevice device) { 35 mDevice = device; 36 } 37 38 /** 39 * Add a new message to the buffer. The buffer keeps selected messages in 40 * the order they are received. 41 * 42 * @param message {@link HdmiCecMessage} to add 43 */ 44 void add(HdmiCecMessage message) { 45 boolean buffered = true; 46 47 // Note that all the messages are not handled in the same manner. 48 // For <Active Source> we keep the latest one only. 49 // TODO: This might not be the best way to choose the active source. 50 // Devise a better way to pick up the best one. 51 switch (message.getOpcode()) { 52 case Constants.MESSAGE_ACTIVE_SOURCE: 53 removeActiveSource(); 54 mBuffer.add(message); 55 break; 56 case Constants.MESSAGE_INITIATE_ARC: 57 case Constants.MESSAGE_SET_SYSTEM_AUDIO_MODE: 58 mBuffer.add(message); 59 break; 60 default: 61 buffered = false; 62 break; 63 } 64 if (buffered) { 65 HdmiLogger.debug("Buffering message:" + message); 66 } 67 } 68 69 private void removeActiveSource() { 70 // Uses iterator to remove elements while looping through the list. 71 for (Iterator<HdmiCecMessage> iter = mBuffer.iterator(); iter.hasNext(); ) { 72 HdmiCecMessage message = iter.next(); 73 if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE) { 74 iter.remove(); 75 } 76 } 77 } 78 79 void processAllMessages() { 80 for (HdmiCecMessage message : mBuffer) { 81 mDevice.onMessage(message); 82 HdmiLogger.debug("Processing message:" + message); 83 } 84 mBuffer.clear(); 85 } 86 87 /** 88 * Process messages from a given logical device. Called by 89 * {@link NewDeviceAction} actions when they finish adding the device 90 * information. 91 * <p><Active Source> is processed only when the TV input is ready. 92 * If not, {@link #processActiveSource()} will be invoked later to handle it. 93 * 94 * @param address logical address of CEC device which the messages to process 95 * are associated with 96 */ 97 void processMessagesForDevice(int address) { 98 HdmiLogger.debug("Checking message for address:" + address); 99 for (Iterator<HdmiCecMessage> iter = mBuffer.iterator(); iter.hasNext(); ) { 100 HdmiCecMessage message = iter.next(); 101 if (message.getSource() != address) continue; 102 if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE 103 && !mDevice.isInputReady(HdmiDeviceInfo.idForCecDevice(address))) continue; 104 mDevice.onMessage(message); 105 HdmiLogger.debug("Processing message:" + message); 106 iter.remove(); 107 } 108 } 109 110 /** 111 * Process <Active Source>. 112 * 113 * <p>The message has a dependency on TV input framework. Should be invoked 114 * after we get the callback 115 * {@link android.media.tv.TvInputManager.TvInputCallback#onInputAdded(String)} 116 * to ensure the processing of the message takes effect when transformed 117 * to input change callback. 118 * 119 * @param address logical address of the device to be the active source 120 */ 121 void processActiveSource(int address) { 122 for (Iterator<HdmiCecMessage> iter = mBuffer.iterator(); iter.hasNext(); ) { 123 HdmiCecMessage message = iter.next(); 124 if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE 125 && message.getSource() == address) { 126 mDevice.onMessage(message); 127 HdmiLogger.debug("Processing message:" + message); 128 iter.remove(); 129 } 130 } 131 } 132} 133