HdmiCecMessageCache.java revision e81e108c4035ea8933525baa8108cb392f8abf5d
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.HdmiCec; 20import android.hardware.hdmi.HdmiCecMessage; 21import android.util.FastImmutableArraySet; 22import android.util.SparseArray; 23 24/** 25 * Cache for incoming message. It caches {@link HdmiCecMessage} with source address and opcode 26 * as a key. 27 * 28 * <p>Note that whenever a device is removed it should call {@link #flushMessagesFrom(int)} 29 * to clean up messages come from the device. 30 */ 31final class HdmiCecMessageCache { 32 private static final FastImmutableArraySet<Integer> CACHEABLE_OPCODES = new FastImmutableArraySet<>( 33 new Integer[] { 34 HdmiCec.MESSAGE_SET_OSD_NAME, 35 HdmiCec.MESSAGE_REPORT_PHYSICAL_ADDRESS, 36 HdmiCec.MESSAGE_DEVICE_VENDOR_ID, 37 HdmiCec.MESSAGE_CEC_VERSION, 38 }); 39 40 // It's like [Source Logical Address, [Opcode, HdmiCecMessage]]. 41 private final SparseArray<SparseArray<HdmiCecMessage>> mCache = new SparseArray<>(); 42 43 HdmiCecMessageCache() { 44 } 45 46 /** 47 * Return a {@link HdmiCecMessage} corresponding to the given {@code address} and 48 * {@code opcode}. 49 * 50 * @param address a logical address of source device 51 * @param opcode opcode of message 52 * @return null if has no {@link HdmiCecMessage} matched to the given {@code address} and {code 53 * opcode} 54 */ 55 public HdmiCecMessage getMessage(int address, int opcode) { 56 SparseArray<HdmiCecMessage> messages = mCache.get(address); 57 if (messages == null) { 58 return null; 59 } 60 61 return messages.get(opcode); 62 } 63 64 /** 65 * Flush all {@link HdmiCecMessage}s sent from the given {@code address}. 66 * 67 * @param address a logical address of source device 68 */ 69 public void flushMessagesFrom(int address) { 70 mCache.remove(address); 71 } 72 73 /** 74 * Flush all cached {@link HdmiCecMessage}s. 75 */ 76 public void flushAll() { 77 mCache.clear(); 78 } 79 80 /** 81 * Cache incoming {@link HdmiCecMessage}. If opcode of message is not listed on 82 * cacheable opcodes list, just ignore it. 83 * 84 * @param message a {@link HdmiCecMessage} to be cached 85 */ 86 public void cacheMessage(HdmiCecMessage message) { 87 int opcode = message.getOpcode(); 88 if (!isCacheable(opcode)) { 89 return; 90 } 91 92 int source = message.getSource(); 93 SparseArray<HdmiCecMessage> messages = mCache.get(source); 94 if (messages == null) { 95 messages = new SparseArray<>(); 96 mCache.put(source, messages); 97 } 98 messages.put(opcode, message); 99 } 100 101 private boolean isCacheable(int opcode) { 102 return CACHEABLE_OPCODES.contains(opcode); 103 } 104} 105