Nick Chalko | 7206f3b | 2018-06-19 10:17:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | package com.android.server.hdmi; |
| 17 | |
| 18 | import android.hardware.hdmi.HdmiPortInfo; |
| 19 | import android.hardware.tv.cec.V1_0.SendMessageResult; |
| 20 | import android.os.MessageQueue; |
Nick Chalko | 90d4432 | 2018-07-27 08:35:12 -0700 | [diff] [blame] | 21 | |
Nick Chalko | 10e52c9 | 2018-07-25 16:07:16 -0700 | [diff] [blame] | 22 | import com.android.internal.annotations.VisibleForTesting; |
Nick Chalko | 7206f3b | 2018-06-19 10:17:03 -0700 | [diff] [blame] | 23 | import com.android.server.hdmi.HdmiCecController.NativeWrapper; |
Nick Chalko | 90d4432 | 2018-07-27 08:35:12 -0700 | [diff] [blame] | 24 | |
| 25 | import com.google.common.collect.Iterables; |
| 26 | |
Shubang | eff3c88 | 2018-07-19 16:00:29 -0700 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
Nick Chalko | 7206f3b | 2018-06-19 10:17:03 -0700 | [diff] [blame] | 29 | |
| 30 | /** Fake {@link NativeWrapper} useful for testing. */ |
| 31 | final class FakeNativeWrapper implements NativeWrapper { |
| 32 | private final int[] mPollAddressResponse = |
| 33 | new int[] { |
| 34 | SendMessageResult.NACK, |
| 35 | SendMessageResult.NACK, |
| 36 | SendMessageResult.NACK, |
| 37 | SendMessageResult.NACK, |
| 38 | SendMessageResult.NACK, |
| 39 | SendMessageResult.NACK, |
| 40 | SendMessageResult.NACK, |
| 41 | SendMessageResult.NACK, |
| 42 | SendMessageResult.NACK, |
| 43 | SendMessageResult.NACK, |
| 44 | SendMessageResult.NACK, |
| 45 | SendMessageResult.NACK, |
| 46 | SendMessageResult.NACK, |
| 47 | SendMessageResult.NACK, |
| 48 | SendMessageResult.NACK, |
| 49 | }; |
| 50 | |
Shubang | eff3c88 | 2018-07-19 16:00:29 -0700 | [diff] [blame] | 51 | private final List<HdmiCecMessage> mResultMessages = new ArrayList<>(); |
Amy | ac1a55c | 2018-07-13 16:38:23 -0700 | [diff] [blame] | 52 | private int mMyPhysicalAddress = 0; |
Amy | a00e118 | 2018-10-17 16:45:03 -0700 | [diff] [blame] | 53 | private HdmiPortInfo[] mHdmiPortInfo = null; |
Nick Chalko | 7206f3b | 2018-06-19 10:17:03 -0700 | [diff] [blame] | 54 | |
| 55 | @Override |
| 56 | public long nativeInit(HdmiCecController handler, MessageQueue messageQueue) { |
| 57 | return 1L; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public int nativeSendCecCommand( |
| 62 | long controllerPtr, int srcAddress, int dstAddress, byte[] body) { |
| 63 | if (body.length == 0) { |
| 64 | return mPollAddressResponse[dstAddress]; |
| 65 | } else { |
Shubang | eff3c88 | 2018-07-19 16:00:29 -0700 | [diff] [blame] | 66 | mResultMessages.add(HdmiCecMessageBuilder.of(srcAddress, dstAddress, body)); |
Nick Chalko | 7206f3b | 2018-06-19 10:17:03 -0700 | [diff] [blame] | 67 | } |
| 68 | return SendMessageResult.SUCCESS; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public int nativeAddLogicalAddress(long controllerPtr, int logicalAddress) { |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void nativeClearLogicalAddress(long controllerPtr) {} |
| 78 | |
| 79 | @Override |
| 80 | public int nativeGetPhysicalAddress(long controllerPtr) { |
Amy | ac1a55c | 2018-07-13 16:38:23 -0700 | [diff] [blame] | 81 | return mMyPhysicalAddress; |
Nick Chalko | 7206f3b | 2018-06-19 10:17:03 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int nativeGetVersion(long controllerPtr) { |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public int nativeGetVendorId(long controllerPtr) { |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public HdmiPortInfo[] nativeGetPortInfos(long controllerPtr) { |
Amy | a00e118 | 2018-10-17 16:45:03 -0700 | [diff] [blame] | 96 | if (mHdmiPortInfo == null) { |
| 97 | mHdmiPortInfo = new HdmiPortInfo[1]; |
| 98 | mHdmiPortInfo[0] = new HdmiPortInfo(1, 1, 0x1000, true, true, true); |
| 99 | } |
| 100 | return mHdmiPortInfo; |
Nick Chalko | 7206f3b | 2018-06-19 10:17:03 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void nativeSetOption(long controllerPtr, int flag, boolean enabled) {} |
| 105 | |
| 106 | @Override |
| 107 | public void nativeSetLanguage(long controllerPtr, String language) {} |
| 108 | |
| 109 | @Override |
| 110 | public void nativeEnableAudioReturnChannel(long controllerPtr, int port, boolean flag) {} |
| 111 | |
| 112 | @Override |
| 113 | public boolean nativeIsConnected(long controllerPtr, int port) { |
| 114 | return false; |
| 115 | } |
| 116 | |
Shubang | eff3c88 | 2018-07-19 16:00:29 -0700 | [diff] [blame] | 117 | public List<HdmiCecMessage> getResultMessages() { |
| 118 | return new ArrayList<>(mResultMessages); |
| 119 | } |
| 120 | |
Nick Chalko | 90d4432 | 2018-07-27 08:35:12 -0700 | [diff] [blame] | 121 | public HdmiCecMessage getOnlyResultMessage() throws IllegalArgumentException { |
| 122 | return Iterables.getOnlyElement(mResultMessages); |
| 123 | } |
| 124 | |
| 125 | public void clearResultMessages() { |
| 126 | mResultMessages.clear(); |
Nick Chalko | 7206f3b | 2018-06-19 10:17:03 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | public void setPollAddressResponse(int logicalAddress, int response) { |
| 130 | mPollAddressResponse[logicalAddress] = response; |
| 131 | } |
Amy | ac1a55c | 2018-07-13 16:38:23 -0700 | [diff] [blame] | 132 | |
| 133 | @VisibleForTesting |
| 134 | protected void setPhysicalAddress(int physicalAddress) { |
| 135 | mMyPhysicalAddress = physicalAddress; |
| 136 | } |
Amy | a00e118 | 2018-10-17 16:45:03 -0700 | [diff] [blame] | 137 | |
| 138 | @VisibleForTesting |
| 139 | protected void setPortInfo(HdmiPortInfo[] hdmiPortInfo) { |
| 140 | mHdmiPortInfo = new HdmiPortInfo[hdmiPortInfo.length]; |
| 141 | System.arraycopy(hdmiPortInfo, 0, mHdmiPortInfo, 0, hdmiPortInfo.length); |
| 142 | } |
Nick Chalko | 7206f3b | 2018-06-19 10:17:03 -0700 | [diff] [blame] | 143 | } |