2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com
.android
.server
.hdmi
;
19 import android
.util
.FastImmutableArraySet
;
20 import android
.util
.SparseArray
;
23 * Cache for incoming message. It caches {@link HdmiCecMessage} with source address and opcode
26 * <p>Note that whenever a device is removed it should call {@link #flushMessagesFrom(int)}
27 * to clean up messages come from the device.
29 final class HdmiCecMessageCache
{
30 private static final FastImmutableArraySet
<Integer
> CACHEABLE_OPCODES
=
31 new FastImmutableArraySet
<>(new Integer
[] {
32 Constants
.MESSAGE_SET_OSD_NAME
,
33 Constants
.MESSAGE_REPORT_PHYSICAL_ADDRESS
,
34 Constants
.MESSAGE_DEVICE_VENDOR_ID
,
35 Constants
.MESSAGE_CEC_VERSION
,
38 // It's like [Source Logical Address, [Opcode, HdmiCecMessage]].
39 private final SparseArray
<SparseArray
<HdmiCecMessage
>> mCache
= new SparseArray
<>();
41 HdmiCecMessageCache() {
45 * Return a {@link HdmiCecMessage} corresponding to the given {@code address} and
48 * @param address a logical address of source device
49 * @param opcode opcode of message
50 * @return null if has no {@link HdmiCecMessage} matched to the given {@code address} and {code
53 public HdmiCecMessage
getMessage(int address
, int opcode
) {
54 SparseArray
<HdmiCecMessage
> messages
= mCache
.get(address
);
55 if (messages
== null) {
59 return messages
.get(opcode
);
63 * Flush all {@link HdmiCecMessage}s sent from the given {@code address}.
65 * @param address a logical address of source device
67 public void flushMessagesFrom(int address
) {
68 mCache
.remove(address
);
72 * Flush all cached {@link HdmiCecMessage}s.
74 public void flushAll() {
79 * Cache incoming {@link HdmiCecMessage}. If opcode of message is not listed on
80 * cacheable opcodes list, just ignore it.
82 * @param message a {@link HdmiCecMessage} to be cached
84 public void cacheMessage(HdmiCecMessage message
) {
85 int opcode
= message
.getOpcode();
86 if (!isCacheable(opcode
)) {
90 int source
= message
.getSource();
91 SparseArray
<HdmiCecMessage
> messages
= mCache
.get(source
);
92 if (messages
== null) {
93 messages
= new SparseArray
<>();
94 mCache
.put(source
, messages
);
96 messages
.put(opcode
, message
);
99 private boolean isCacheable(int opcode
) {
100 return CACHEABLE_OPCODES
.contains(opcode
);