DelayedMessageBuffer.java revision 964c00dd7b270dcf80aea3450bbfc23502965cce
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 &lt;Active Source&gt; 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        // Use the copied buffer.
81        ArrayList<HdmiCecMessage> copiedBuffer = new ArrayList<HdmiCecMessage>(mBuffer);
82        mBuffer.clear();
83        for (HdmiCecMessage message : copiedBuffer) {
84            mDevice.onMessage(message);
85            HdmiLogger.debug("Processing message:" + message);
86        }
87    }
88
89    /**
90     * Process messages from a given logical device. Called by
91     * {@link NewDeviceAction} actions when they finish adding the device
92     * information.
93     * <p>&lt;Active Source&gt; is processed only when the TV input is ready.
94     * If not, {@link #processActiveSource()} will be invoked later to handle it.
95     *
96     * @param address logical address of CEC device which the messages to process
97     *        are associated with
98     */
99    void processMessagesForDevice(int address) {
100        ArrayList<HdmiCecMessage> copiedBuffer = new ArrayList<HdmiCecMessage>(mBuffer);
101        mBuffer.clear();
102        HdmiLogger.debug("Checking message for address:" + address);
103        for (HdmiCecMessage message : copiedBuffer) {
104            if (message.getSource() != address) {
105                mBuffer.add(message);
106                continue;
107            }
108            if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE
109                    && !mDevice.isInputReady(HdmiDeviceInfo.idForCecDevice(address))) {
110                mBuffer.add(message);
111                continue;
112            }
113            mDevice.onMessage(message);
114            HdmiLogger.debug("Processing message:" + message);
115        }
116    }
117
118    /**
119     * Process &lt;Active Source&gt;.
120     *
121     * <p>The message has a dependency on TV input framework. Should be invoked
122     * after we get the callback
123     * {@link android.media.tv.TvInputManager.TvInputCallback#onInputAdded(String)}
124     * to ensure the processing of the message takes effect when transformed
125     * to input change callback.
126     *
127     * @param address logical address of the device to be the active source
128     */
129    void processActiveSource(int address) {
130        ArrayList<HdmiCecMessage> copiedBuffer = new ArrayList<HdmiCecMessage>(mBuffer);
131        mBuffer.clear();
132        for (HdmiCecMessage message : copiedBuffer) {
133            if (message.getOpcode() == Constants.MESSAGE_ACTIVE_SOURCE
134                    && message.getSource() == address) {
135                mDevice.onMessage(message);
136                HdmiLogger.debug("Processing message:" + message);
137            } else {
138                mBuffer.add(message);
139            }
140        }
141    }
142}
143