Move to Android N-MR1 SDK.
[android_tools.git] / sdk / sources / android-25 / com / android / server / hdmi / HdmiCecMessageCache.java
blobfb4460f4540dcb938fae7c22afcc4898df85250c
1 /*
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;
22 /**
23 * Cache for incoming message. It caches {@link HdmiCecMessage} with source address and opcode
24 * as a key.
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,
36 });
38 // It's like [Source Logical Address, [Opcode, HdmiCecMessage]].
39 private final SparseArray<SparseArray<HdmiCecMessage>> mCache = new SparseArray<>();
41 HdmiCecMessageCache() {
44 /**
45 * Return a {@link HdmiCecMessage} corresponding to the given {@code address} and
46 * {@code opcode}.
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
51 * opcode}
53 public HdmiCecMessage getMessage(int address, int opcode) {
54 SparseArray<HdmiCecMessage> messages = mCache.get(address);
55 if (messages == null) {
56 return null;
59 return messages.get(opcode);
62 /**
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);
71 /**
72 * Flush all cached {@link HdmiCecMessage}s.
74 public void flushAll() {
75 mCache.clear();
78 /**
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)) {
87 return;
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);