Jinsuk Kim | 7fa3a66 | 2014-11-07 15:20:24 +0900 | [diff] [blame] | 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 | import android.util.Slog; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import 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 | */ |
| 30 | final 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 | mBuffer.add(message); |
| 58 | break; |
| 59 | default: |
| 60 | buffered = false; |
| 61 | break; |
| 62 | } |
| 63 | if (buffered) { |
| 64 | HdmiLogger.debug("Buffering message:" + message); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private void removeActiveSource() { |
| 69 | // Uses iterator to remove elements while looping through the list. |
| 70 | for (Iterator<HdmiCecMessage> iter = mBuffer.iterator(); iter.hasNext(); ) { |
| 71 | HdmiCecMessage message = iter.next(); |
| 72 | if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE) { |
| 73 | iter.remove(); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void processAllMessages() { |
| 79 | for (HdmiCecMessage message : mBuffer) { |
| 80 | mDevice.onMessage(message); |
| 81 | HdmiLogger.debug("Processing message:" + message); |
| 82 | } |
| 83 | mBuffer.clear(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Process messages from a given logical device. Called by |
| 88 | * {@link NewDeviceAction} actions when they finish adding the device |
| 89 | * information. |
| 90 | * <p><Active Source> is not processed in this method but processed |
| 91 | * separately via {@link #processActiveSource()}. |
| 92 | * |
| 93 | * @param address logical address of CEC device which the messages to process |
| 94 | * are associated with |
| 95 | */ |
| 96 | void processMessagesForDevice(int address) { |
| 97 | HdmiLogger.debug("Processing message for address:" + address); |
| 98 | for (Iterator<HdmiCecMessage> iter = mBuffer.iterator(); iter.hasNext(); ) { |
| 99 | HdmiCecMessage message = iter.next(); |
| 100 | if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE) { |
| 101 | continue; |
| 102 | } |
| 103 | if (message.getSource() == address) { |
| 104 | mDevice.onMessage(message); |
| 105 | HdmiLogger.debug("Processing message:" + message); |
| 106 | iter.remove(); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Process <Active Source>. |
| 113 | * |
| 114 | * <p>The message has a dependency on TV input framework. Should be invoked |
| 115 | * after we get the callback |
| 116 | * {@link android.media.tv.TvInputManager.TvInputCallback#onInputAdded(String)} |
| 117 | * to ensure the processing of the message takes effect when transformed |
| 118 | * to input change callback. |
| 119 | * |
| 120 | * @param address logical address of the device to be the active source |
| 121 | */ |
| 122 | void processActiveSource(int address) { |
| 123 | for (Iterator<HdmiCecMessage> iter = mBuffer.iterator(); iter.hasNext(); ) { |
| 124 | HdmiCecMessage message = iter.next(); |
| 125 | if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE |
| 126 | && message.getSource() == address) { |
| 127 | mDevice.onMessage(message); |
| 128 | HdmiLogger.debug("Processing message:" + message); |
| 129 | iter.remove(); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |