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 17 package com.android.server.hdmi; 18 19 import android.hardware.hdmi.HdmiDeviceInfo; 20 21 import java.util.ArrayList; 22 import java.util.Iterator; 23 import java.util.List; 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 */ 30 final class DelayedMessageBuffer { 31 private final ArrayList<HdmiCecMessage> mBuffer = new ArrayList<>(); 32 private final HdmiCecLocalDevice mDevice; 33 DelayedMessageBuffer(HdmiCecLocalDevice device)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 */ add(HdmiCecMessage message)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 removeActiveSource()69 protected 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 isBuffered(int opcode)79 boolean isBuffered(int opcode) { 80 for (HdmiCecMessage message : mBuffer) { 81 if (message.getOpcode() == opcode) { 82 return true; 83 } 84 } 85 return false; 86 } 87 getBufferedMessagesWithOpcode(int opcode)88 List<HdmiCecMessage> getBufferedMessagesWithOpcode(int opcode) { 89 List<HdmiCecMessage> messages = new ArrayList<>(); 90 for (HdmiCecMessage message : mBuffer) { 91 if (message.getOpcode() == opcode) { 92 messages.add(message); 93 } 94 } 95 return messages; 96 } 97 processAllMessages()98 void processAllMessages() { 99 // Use the copied buffer. 100 ArrayList<HdmiCecMessage> copiedBuffer = new ArrayList<>(mBuffer); 101 mBuffer.clear(); 102 for (HdmiCecMessage message : copiedBuffer) { 103 mDevice.onMessage(message); 104 HdmiLogger.debug("Processing message:" + message); 105 } 106 } 107 108 /** 109 * Process messages from a given logical device. Called by 110 * {@link NewDeviceAction} actions when they finish adding the device 111 * information. 112 * <p><Active Source> is processed only when the TV input is ready. 113 * If not, {@link #processActiveSource()} will be invoked later to handle it. 114 * 115 * @param address logical address of CEC device which the messages to process 116 * are associated with 117 */ processMessagesForDevice(int address)118 void processMessagesForDevice(int address) { 119 ArrayList<HdmiCecMessage> copiedBuffer = new ArrayList<>(mBuffer); 120 mBuffer.clear(); 121 HdmiLogger.debug("Checking message for address:" + address); 122 for (HdmiCecMessage message : copiedBuffer) { 123 if (message.getSource() != address) { 124 mBuffer.add(message); 125 continue; 126 } 127 if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE 128 && !mDevice.isInputReady(HdmiDeviceInfo.idForCecDevice(address))) { 129 mBuffer.add(message); 130 continue; 131 } 132 mDevice.onMessage(message); 133 HdmiLogger.debug("Processing message:" + message); 134 } 135 } 136 137 /** 138 * Process <Active Source>. 139 * 140 * <p>The message has a dependency on TV input framework. Should be invoked 141 * after we get the callback 142 * {@link android.media.tv.TvInputManager.TvInputCallback#onInputAdded(String)} 143 * to ensure the processing of the message takes effect when transformed 144 * to input change callback. 145 * 146 * @param address logical address of the device to be the active source 147 */ processActiveSource(int address)148 void processActiveSource(int address) { 149 ArrayList<HdmiCecMessage> copiedBuffer = new ArrayList<>(mBuffer); 150 mBuffer.clear(); 151 for (HdmiCecMessage message : copiedBuffer) { 152 if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE 153 && message.getSource() == address) { 154 mDevice.onMessage(message); 155 HdmiLogger.debug("Processing message:" + message); 156 } else { 157 mBuffer.add(message); 158 } 159 } 160 } 161 } 162