DelayedMessageBuffer.java revision ad1e3d7df42376cd2a257b6c3b2fed540658a6e3
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 not processed in this method but processed 92 * separately via {@link #processActiveSource()}. 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("Processing message for address:" + address); 99 for (Iterator<HdmiCecMessage> iter = mBuffer.iterator(); iter.hasNext(); ) { 100 HdmiCecMessage message = iter.next(); 101 if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE) { 102 continue; 103 } 104 if (message.getSource() == address) { 105 mDevice.onMessage(message); 106 HdmiLogger.debug("Processing message:" + message); 107 iter.remove(); 108 } 109 } 110 } 111 112 /** 113 * Process <Active Source>. 114 * 115 * <p>The message has a dependency on TV input framework. Should be invoked 116 * after we get the callback 117 * {@link android.media.tv.TvInputManager.TvInputCallback#onInputAdded(String)} 118 * to ensure the processing of the message takes effect when transformed 119 * to input change callback. 120 * 121 * @param address logical address of the device to be the active source 122 */ 123 void processActiveSource(int address) { 124 for (Iterator<HdmiCecMessage> iter = mBuffer.iterator(); iter.hasNext(); ) { 125 HdmiCecMessage message = iter.next(); 126 if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE 127 && message.getSource() == address) { 128 mDevice.onMessage(message); 129 HdmiLogger.debug("Processing message:" + message); 130 iter.remove(); 131 } 132 } 133 } 134} 135