Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 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 | |
| 17 | package com.android.server.hdmi; |
| 18 | |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 19 | import android.hardware.hdmi.HdmiDeviceInfo; |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 20 | import android.hardware.input.InputManager; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 21 | import android.os.Handler; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 22 | import android.os.Looper; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 23 | import android.os.Message; |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 24 | import android.os.SystemClock; |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 25 | import android.util.Slog; |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 26 | import android.view.InputDevice; |
| 27 | import android.view.KeyCharacterMap; |
| 28 | import android.view.KeyEvent; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 29 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 30 | import com.android.internal.annotations.GuardedBy; |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame] | 31 | import com.android.internal.util.IndentingPrintWriter; |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 32 | import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 33 | |
| 34 | import java.util.ArrayList; |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 35 | import java.util.Collections; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 36 | import java.util.Iterator; |
| 37 | import java.util.LinkedList; |
| 38 | import java.util.List; |
| 39 | |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 40 | /** |
| 41 | * Class that models a logical CEC device hosted in this system. Handles initialization, |
| 42 | * CEC commands that call for actions customized per device type. |
| 43 | */ |
| 44 | abstract class HdmiCecLocalDevice { |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 45 | private static final String TAG = "HdmiCecLocalDevice"; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 46 | |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 47 | private static final int MSG_DISABLE_DEVICE_TIMEOUT = 1; |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 48 | private static final int MSG_USER_CONTROL_RELEASE_TIMEOUT = 2; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 49 | // Timeout in millisecond for device clean up (5s). |
| 50 | // Normal actions timeout is 2s but some of them would have several sequence of timeout. |
| 51 | private static final int DEVICE_CLEANUP_TIMEOUT = 5000; |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 52 | // Within the timer, a received <User Control Pressed> will start "Press and Hold" behavior. |
| 53 | // When it expires, we can assume <User Control Release> is received. |
| 54 | private static final int FOLLOWER_SAFETY_TIMEOUT = 550; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 55 | |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 56 | protected final HdmiControlService mService; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 57 | protected final int mDeviceType; |
| 58 | protected int mAddress; |
| 59 | protected int mPreferredAddress; |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 60 | protected HdmiDeviceInfo mDeviceInfo; |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 61 | protected int mLastKeycode = HdmiCecKeycode.UNSUPPORTED_KEYCODE; |
| 62 | protected int mLastKeyRepeatCount = 0; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 63 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 64 | static class ActiveSource { |
| 65 | int logicalAddress; |
| 66 | int physicalAddress; |
| 67 | |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 68 | public ActiveSource() { |
| 69 | invalidate(); |
| 70 | } |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 71 | public ActiveSource(int logical, int physical) { |
| 72 | logicalAddress = logical; |
| 73 | physicalAddress = physical; |
| 74 | } |
| 75 | public static ActiveSource of(int logical, int physical) { |
| 76 | return new ActiveSource(logical, physical); |
| 77 | } |
| 78 | public boolean isValid() { |
| 79 | return HdmiUtils.isValidAddress(logicalAddress); |
| 80 | } |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 81 | public void invalidate() { |
| 82 | logicalAddress = Constants.ADDR_INVALID; |
| 83 | physicalAddress = Constants.INVALID_PHYSICAL_ADDRESS; |
| 84 | } |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 85 | public boolean equals(int logical, int physical) { |
| 86 | return logicalAddress == logical && physicalAddress == physical; |
| 87 | } |
| 88 | @Override |
| 89 | public boolean equals(Object obj) { |
| 90 | if (obj instanceof ActiveSource) { |
| 91 | ActiveSource that = (ActiveSource) obj; |
| 92 | return that.logicalAddress == logicalAddress && |
| 93 | that.physicalAddress == physicalAddress; |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | @Override |
| 98 | public int hashCode() { |
| 99 | return logicalAddress * 29 + physicalAddress; |
| 100 | } |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame] | 101 | @Override |
| 102 | public String toString() { |
| 103 | StringBuffer s = new StringBuffer(); |
| 104 | String logicalAddressString = (logicalAddress == Constants.ADDR_INVALID) |
| 105 | ? "invalid" : String.format("0x%02x", logicalAddress); |
| 106 | s.append("logical_address: ").append(logicalAddressString); |
| 107 | String physicalAddressString = (physicalAddress == Constants.INVALID_PHYSICAL_ADDRESS) |
| 108 | ? "invalid" : String.format("0x%04x", physicalAddress); |
| 109 | s.append(", physical_address: ").append(physicalAddressString); |
| 110 | return s.toString(); |
| 111 | } |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 112 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 113 | // Logical address of the active source. |
| 114 | @GuardedBy("mLock") |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 115 | protected final ActiveSource mActiveSource = new ActiveSource(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 116 | |
| 117 | // Active routing path. Physical address of the active source but not all the time, such as |
| 118 | // when the new active source does not claim itself to be one. Note that we don't keep |
| 119 | // the active port id (or active input) since it can be gotten by {@link #pathToPortId(int)}. |
| 120 | @GuardedBy("mLock") |
| 121 | private int mActiveRoutingPath; |
| 122 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 123 | protected final HdmiCecMessageCache mCecMessageCache = new HdmiCecMessageCache(); |
| 124 | protected final Object mLock; |
| 125 | |
| 126 | // A collection of FeatureAction. |
| 127 | // Note that access to this collection should happen in service thread. |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 128 | private final LinkedList<HdmiCecFeatureAction> mActions = new LinkedList<>(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 129 | |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 130 | private final Handler mHandler = new Handler () { |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 131 | @Override |
| 132 | public void handleMessage(Message msg) { |
| 133 | switch (msg.what) { |
| 134 | case MSG_DISABLE_DEVICE_TIMEOUT: |
| 135 | handleDisableDeviceTimeout(); |
| 136 | break; |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 137 | case MSG_USER_CONTROL_RELEASE_TIMEOUT: |
| 138 | handleUserControlReleased(); |
| 139 | break; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | /** |
| 145 | * A callback interface to get notified when all pending action is cleared. |
| 146 | * It can be called when timeout happened. |
| 147 | */ |
| 148 | interface PendingActionClearedCallback { |
| 149 | void onCleared(HdmiCecLocalDevice device); |
| 150 | } |
| 151 | |
| 152 | protected PendingActionClearedCallback mPendingActionClearedCallback; |
| 153 | |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 154 | protected HdmiCecLocalDevice(HdmiControlService service, int deviceType) { |
| 155 | mService = service; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 156 | mDeviceType = deviceType; |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 157 | mAddress = Constants.ADDR_UNREGISTERED; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 158 | mLock = service.getServiceLock(); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Factory method that returns HdmiCecLocalDevice of corresponding type. |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 162 | static HdmiCecLocalDevice create(HdmiControlService service, int deviceType) { |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 163 | switch (deviceType) { |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 164 | case HdmiDeviceInfo.DEVICE_TV: |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 165 | return new HdmiCecLocalDeviceTv(service); |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 166 | case HdmiDeviceInfo.DEVICE_PLAYBACK: |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 167 | return new HdmiCecLocalDevicePlayback(service); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 168 | default: |
| 169 | return null; |
| 170 | } |
| 171 | } |
| 172 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 173 | @ServiceThreadOnly |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 174 | void init() { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 175 | assertRunOnServiceThread(); |
Jinsuk Kim | af2acf0 | 2014-07-11 18:43:04 +0900 | [diff] [blame] | 176 | mPreferredAddress = getPreferredAddress(); |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 177 | } |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 178 | |
Jungshik Jang | 8b308d9 | 2014-05-29 21:52:28 +0900 | [diff] [blame] | 179 | /** |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 180 | * Called once a logical address of the local device is allocated. |
Jungshik Jang | 8b308d9 | 2014-05-29 21:52:28 +0900 | [diff] [blame] | 181 | */ |
Yuncheol Heo | fc44e4e | 2014-08-04 19:41:09 +0900 | [diff] [blame] | 182 | protected abstract void onAddressAllocated(int logicalAddress, int reason); |
Jungshik Jang | 8b308d9 | 2014-05-29 21:52:28 +0900 | [diff] [blame] | 183 | |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 184 | /** |
Jinsuk Kim | af2acf0 | 2014-07-11 18:43:04 +0900 | [diff] [blame] | 185 | * Get the preferred logical address from system properties. |
| 186 | */ |
| 187 | protected abstract int getPreferredAddress(); |
| 188 | |
| 189 | /** |
| 190 | * Set the preferred logical address to system properties. |
| 191 | */ |
| 192 | protected abstract void setPreferredAddress(int addr); |
| 193 | |
| 194 | /** |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 195 | * Dispatch incoming message. |
| 196 | * |
| 197 | * @param message incoming message |
| 198 | * @return true if consumed a message; otherwise, return false. |
| 199 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 200 | @ServiceThreadOnly |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 201 | boolean dispatchMessage(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 202 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 203 | int dest = message.getDestination(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 204 | if (dest != mAddress && dest != Constants.ADDR_BROADCAST) { |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 205 | return false; |
| 206 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 207 | // Cache incoming message. Note that it caches only white-listed one. |
| 208 | mCecMessageCache.cacheMessage(message); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 209 | return onMessage(message); |
| 210 | } |
| 211 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 212 | @ServiceThreadOnly |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 213 | protected final boolean onMessage(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 214 | assertRunOnServiceThread(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 215 | if (dispatchMessageToAction(message)) { |
| 216 | return true; |
| 217 | } |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 218 | switch (message.getOpcode()) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 219 | case Constants.MESSAGE_ACTIVE_SOURCE: |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 220 | return handleActiveSource(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 221 | case Constants.MESSAGE_INACTIVE_SOURCE: |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 222 | return handleInactiveSource(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 223 | case Constants.MESSAGE_REQUEST_ACTIVE_SOURCE: |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 224 | return handleRequestActiveSource(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 225 | case Constants.MESSAGE_GET_MENU_LANGUAGE: |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 226 | return handleGetMenuLanguage(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 227 | case Constants.MESSAGE_GIVE_PHYSICAL_ADDRESS: |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 228 | return handleGivePhysicalAddress(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 229 | case Constants.MESSAGE_GIVE_OSD_NAME: |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 230 | return handleGiveOsdName(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 231 | case Constants.MESSAGE_GIVE_DEVICE_VENDOR_ID: |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 232 | return handleGiveDeviceVendorId(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 233 | case Constants.MESSAGE_GET_CEC_VERSION: |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 234 | return handleGetCecVersion(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 235 | case Constants.MESSAGE_REPORT_PHYSICAL_ADDRESS: |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 236 | return handleReportPhysicalAddress(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 237 | case Constants.MESSAGE_ROUTING_CHANGE: |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 238 | return handleRoutingChange(message); |
Yuncheol Heo | 64bafd9 | 2014-08-11 11:17:54 +0900 | [diff] [blame] | 239 | case Constants.MESSAGE_ROUTING_INFORMATION: |
| 240 | return handleRoutingInformation(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 241 | case Constants.MESSAGE_INITIATE_ARC: |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 242 | return handleInitiateArc(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 243 | case Constants.MESSAGE_TERMINATE_ARC: |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 244 | return handleTerminateArc(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 245 | case Constants.MESSAGE_SET_SYSTEM_AUDIO_MODE: |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 246 | return handleSetSystemAudioMode(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 247 | case Constants.MESSAGE_SYSTEM_AUDIO_MODE_STATUS: |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 248 | return handleSystemAudioModeStatus(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 249 | case Constants.MESSAGE_REPORT_AUDIO_STATUS: |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 250 | return handleReportAudioStatus(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 251 | case Constants.MESSAGE_STANDBY: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 252 | return handleStandby(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 253 | case Constants.MESSAGE_TEXT_VIEW_ON: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 254 | return handleTextViewOn(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 255 | case Constants.MESSAGE_IMAGE_VIEW_ON: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 256 | return handleImageViewOn(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 257 | case Constants.MESSAGE_USER_CONTROL_PRESSED: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 258 | return handleUserControlPressed(message); |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 259 | case Constants.MESSAGE_USER_CONTROL_RELEASED: |
| 260 | return handleUserControlReleased(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 261 | case Constants.MESSAGE_SET_STREAM_PATH: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 262 | return handleSetStreamPath(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 263 | case Constants.MESSAGE_GIVE_DEVICE_POWER_STATUS: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 264 | return handleGiveDevicePowerStatus(message); |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 265 | case Constants.MESSAGE_MENU_REQUEST: |
| 266 | return handleGiveDeviceMenuStatus(message); |
Jinsuk Kim | 119160a | 2014-07-07 18:48:10 +0900 | [diff] [blame] | 267 | case Constants.MESSAGE_VENDOR_COMMAND: |
| 268 | return handleVendorCommand(message); |
| 269 | case Constants.MESSAGE_VENDOR_COMMAND_WITH_ID: |
| 270 | return handleVendorCommandWithId(message); |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 271 | case Constants.MESSAGE_SET_OSD_NAME: |
| 272 | return handleSetOsdName(message); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 273 | case Constants.MESSAGE_RECORD_TV_SCREEN: |
| 274 | return handleRecordTvScreen(message); |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 275 | case Constants.MESSAGE_TIMER_CLEARED_STATUS: |
| 276 | return handleTimerClearedStatus(message); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 277 | default: |
| 278 | return false; |
| 279 | } |
| 280 | } |
| 281 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 282 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 283 | private boolean dispatchMessageToAction(HdmiCecMessage message) { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 284 | assertRunOnServiceThread(); |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 285 | for (HdmiCecFeatureAction action : mActions) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 286 | if (action.processCommand(message)) { |
| 287 | return true; |
| 288 | } |
| 289 | } |
| 290 | return false; |
| 291 | } |
| 292 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 293 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 294 | protected boolean handleGivePhysicalAddress() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 295 | assertRunOnServiceThread(); |
| 296 | |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 297 | int physicalAddress = mService.getPhysicalAddress(); |
| 298 | HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildReportPhysicalAddressCommand( |
| 299 | mAddress, physicalAddress, mDeviceType); |
| 300 | mService.sendCecCommand(cecMessage); |
| 301 | return true; |
| 302 | } |
| 303 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 304 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 305 | protected boolean handleGiveDeviceVendorId() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 306 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 307 | int vendorId = mService.getVendorId(); |
| 308 | HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildDeviceVendorIdCommand( |
| 309 | mAddress, vendorId); |
| 310 | mService.sendCecCommand(cecMessage); |
| 311 | return true; |
| 312 | } |
| 313 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 314 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 315 | protected boolean handleGetCecVersion(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 316 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 317 | int version = mService.getCecVersion(); |
| 318 | HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildCecVersion(message.getDestination(), |
| 319 | message.getSource(), version); |
| 320 | mService.sendCecCommand(cecMessage); |
| 321 | return true; |
| 322 | } |
| 323 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 324 | @ServiceThreadOnly |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 325 | protected boolean handleActiveSource(HdmiCecMessage message) { |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | @ServiceThreadOnly |
| 330 | protected boolean handleInactiveSource(HdmiCecMessage message) { |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | @ServiceThreadOnly |
| 335 | protected boolean handleRequestActiveSource(HdmiCecMessage message) { |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 340 | protected boolean handleGetMenuLanguage(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 341 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 342 | Slog.w(TAG, "Only TV can handle <Get Menu Language>:" + message.toString()); |
Yuncheol Heo | 6aae652 | 2014-08-05 14:48:37 +0900 | [diff] [blame] | 343 | // 'return false' will cause to reply with <Feature Abort>. |
| 344 | return false; |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 345 | } |
| 346 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 347 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 348 | protected boolean handleGiveOsdName(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 349 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 350 | // Note that since this method is called after logical address allocation is done, |
| 351 | // mDeviceInfo should not be null. |
| 352 | HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildSetOsdNameCommand( |
| 353 | mAddress, message.getSource(), mDeviceInfo.getDisplayName()); |
| 354 | if (cecMessage != null) { |
| 355 | mService.sendCecCommand(cecMessage); |
| 356 | } else { |
| 357 | Slog.w(TAG, "Failed to build <Get Osd Name>:" + mDeviceInfo.getDisplayName()); |
| 358 | } |
| 359 | return true; |
| 360 | } |
| 361 | |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 362 | protected boolean handleRoutingChange(HdmiCecMessage message) { |
| 363 | return false; |
| 364 | } |
| 365 | |
Yuncheol Heo | 64bafd9 | 2014-08-11 11:17:54 +0900 | [diff] [blame] | 366 | protected boolean handleRoutingInformation(HdmiCecMessage message) { |
| 367 | return false; |
| 368 | } |
| 369 | |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 370 | protected boolean handleReportPhysicalAddress(HdmiCecMessage message) { |
| 371 | return false; |
| 372 | } |
| 373 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 374 | protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) { |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | protected boolean handleSetSystemAudioMode(HdmiCecMessage message) { |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | protected boolean handleTerminateArc(HdmiCecMessage message) { |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | protected boolean handleInitiateArc(HdmiCecMessage message) { |
| 387 | return false; |
| 388 | } |
| 389 | |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 390 | protected boolean handleReportAudioStatus(HdmiCecMessage message) { |
| 391 | return false; |
| 392 | } |
| 393 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 394 | @ServiceThreadOnly |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 395 | protected boolean handleStandby(HdmiCecMessage message) { |
| 396 | assertRunOnServiceThread(); |
| 397 | // Seq #12 |
Jinsuk Kim | 4d43d93 | 2014-07-03 16:43:58 +0900 | [diff] [blame] | 398 | if (mService.isControlEnabled() && !mService.isProhibitMode() |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 399 | && mService.isPowerOnOrTransient()) { |
| 400 | mService.standby(); |
| 401 | return true; |
| 402 | } |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | @ServiceThreadOnly |
| 407 | protected boolean handleUserControlPressed(HdmiCecMessage message) { |
| 408 | assertRunOnServiceThread(); |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 409 | mHandler.removeMessages(MSG_USER_CONTROL_RELEASE_TIMEOUT); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 410 | if (mService.isPowerOnOrTransient() && isPowerOffOrToggleCommand(message)) { |
| 411 | mService.standby(); |
| 412 | return true; |
| 413 | } else if (mService.isPowerStandbyOrTransient() && isPowerOnOrToggleCommand(message)) { |
| 414 | mService.wakeUp(); |
| 415 | return true; |
| 416 | } |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 417 | |
| 418 | final long downTime = SystemClock.uptimeMillis(); |
| 419 | final byte[] params = message.getParams(); |
Jungshik Jang | 5691b2f | 2014-08-18 16:50:12 +0900 | [diff] [blame] | 420 | // Note that we don't support parameterized keycode now. |
| 421 | // TODO: translate parameterized keycode as well. |
| 422 | final int keycode = HdmiCecKeycode.cecKeyToAndroidKey(params[0]); |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 423 | int keyRepeatCount = 0; |
| 424 | if (mLastKeycode != HdmiCecKeycode.UNSUPPORTED_KEYCODE) { |
| 425 | if (keycode == mLastKeycode) { |
| 426 | keyRepeatCount = mLastKeyRepeatCount + 1; |
| 427 | } else { |
| 428 | injectKeyEvent(downTime, KeyEvent.ACTION_UP, mLastKeycode, 0); |
| 429 | } |
| 430 | } |
| 431 | mLastKeycode = keycode; |
| 432 | mLastKeyRepeatCount = keyRepeatCount; |
| 433 | |
| 434 | if (keycode != HdmiCecKeycode.UNSUPPORTED_KEYCODE) { |
| 435 | injectKeyEvent(downTime, KeyEvent.ACTION_DOWN, keycode, keyRepeatCount); |
| 436 | mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_USER_CONTROL_RELEASE_TIMEOUT), |
| 437 | FOLLOWER_SAFETY_TIMEOUT); |
| 438 | return true; |
| 439 | } |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 440 | return false; |
| 441 | } |
| 442 | |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 443 | @ServiceThreadOnly |
| 444 | protected boolean handleUserControlReleased() { |
| 445 | assertRunOnServiceThread(); |
| 446 | mHandler.removeMessages(MSG_USER_CONTROL_RELEASE_TIMEOUT); |
| 447 | mLastKeyRepeatCount = 0; |
| 448 | if (mLastKeycode != HdmiCecKeycode.UNSUPPORTED_KEYCODE) { |
| 449 | final long upTime = SystemClock.uptimeMillis(); |
| 450 | injectKeyEvent(upTime, KeyEvent.ACTION_UP, mLastKeycode, 0); |
| 451 | mLastKeycode = HdmiCecKeycode.UNSUPPORTED_KEYCODE; |
| 452 | return true; |
| 453 | } |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | static void injectKeyEvent(long time, int action, int keycode, int repeat) { |
| 458 | KeyEvent keyEvent = KeyEvent.obtain(time, time, action, keycode, |
| 459 | repeat, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM, |
| 460 | InputDevice.SOURCE_HDMI, null); |
| 461 | InputManager.getInstance().injectInputEvent(keyEvent, |
| 462 | InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); |
| 463 | keyEvent.recycle(); |
| 464 | } |
| 465 | |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 466 | static boolean isPowerOnOrToggleCommand(HdmiCecMessage message) { |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 467 | byte[] params = message.getParams(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 468 | return message.getOpcode() == Constants.MESSAGE_USER_CONTROL_PRESSED |
Jungshik Jang | 210d73d | 2014-07-04 11:11:29 +0900 | [diff] [blame] | 469 | && (params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER |
| 470 | || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_ON_FUNCTION |
| 471 | || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_TOGGLE_FUNCTION); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 472 | } |
| 473 | |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 474 | static boolean isPowerOffOrToggleCommand(HdmiCecMessage message) { |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 475 | byte[] params = message.getParams(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 476 | return message.getOpcode() == Constants.MESSAGE_USER_CONTROL_PRESSED |
Jungshik Jang | 210d73d | 2014-07-04 11:11:29 +0900 | [diff] [blame] | 477 | && (params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER |
| 478 | || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_OFF_FUNCTION |
| 479 | || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_TOGGLE_FUNCTION); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | protected boolean handleTextViewOn(HdmiCecMessage message) { |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | protected boolean handleImageViewOn(HdmiCecMessage message) { |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | protected boolean handleSetStreamPath(HdmiCecMessage message) { |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | protected boolean handleGiveDevicePowerStatus(HdmiCecMessage message) { |
| 495 | mService.sendCecCommand(HdmiCecMessageBuilder.buildReportPowerStatus( |
| 496 | mAddress, message.getSource(), mService.getPowerStatus())); |
| 497 | return true; |
| 498 | } |
| 499 | |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 500 | protected boolean handleGiveDeviceMenuStatus(HdmiCecMessage message) { |
| 501 | // Always report menu active to receive Remote Control. |
| 502 | mService.sendCecCommand(HdmiCecMessageBuilder.buildReportMenuStatus( |
| 503 | mAddress, message.getSource(), Constants.MENU_STATE_ACTIVATED)); |
| 504 | return true; |
| 505 | } |
| 506 | |
Jinsuk Kim | 119160a | 2014-07-07 18:48:10 +0900 | [diff] [blame] | 507 | protected boolean handleVendorCommand(HdmiCecMessage message) { |
| 508 | mService.invokeVendorCommandListeners(mDeviceType, message.getSource(), |
| 509 | message.getParams(), false); |
| 510 | return true; |
| 511 | } |
| 512 | |
| 513 | protected boolean handleVendorCommandWithId(HdmiCecMessage message) { |
| 514 | byte[] params = message.getParams(); |
| 515 | int vendorId = HdmiUtils.threeBytesToInt(params); |
| 516 | if (vendorId == mService.getVendorId()) { |
| 517 | mService.invokeVendorCommandListeners(mDeviceType, message.getSource(), params, true); |
| 518 | } else if (message.getDestination() != Constants.ADDR_BROADCAST && |
| 519 | message.getSource() != Constants.ADDR_UNREGISTERED) { |
| 520 | Slog.v(TAG, "Wrong direct vendor command. Replying with <Feature Abort>"); |
Yuncheol Heo | 6aae652 | 2014-08-05 14:48:37 +0900 | [diff] [blame] | 521 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_UNRECOGNIZED_OPCODE); |
Jinsuk Kim | 119160a | 2014-07-07 18:48:10 +0900 | [diff] [blame] | 522 | } else { |
| 523 | Slog.v(TAG, "Wrong broadcast vendor command. Ignoring"); |
| 524 | } |
| 525 | return true; |
| 526 | } |
| 527 | |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 528 | protected boolean handleSetOsdName(HdmiCecMessage message) { |
| 529 | // The default behavior of <Set Osd Name> is doing nothing. |
| 530 | return true; |
| 531 | } |
| 532 | |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 533 | protected boolean handleRecordTvScreen(HdmiCecMessage message) { |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 534 | // The default behavior of <Record TV Screen> is replying <Feature Abort> with |
| 535 | // "Cannot provide source". |
Yuncheol Heo | 6aae652 | 2014-08-05 14:48:37 +0900 | [diff] [blame] | 536 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_CANNOT_PROVIDE_SOURCE); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 537 | return true; |
| 538 | } |
| 539 | |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 540 | protected boolean handleTimerClearedStatus(HdmiCecMessage message) { |
| 541 | return false; |
| 542 | } |
| 543 | |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 544 | @ServiceThreadOnly |
Yuncheol Heo | fc44e4e | 2014-08-04 19:41:09 +0900 | [diff] [blame] | 545 | final void handleAddressAllocated(int logicalAddress, int reason) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 546 | assertRunOnServiceThread(); |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 547 | mAddress = mPreferredAddress = logicalAddress; |
Yuncheol Heo | fc44e4e | 2014-08-04 19:41:09 +0900 | [diff] [blame] | 548 | onAddressAllocated(logicalAddress, reason); |
Jinsuk Kim | af2acf0 | 2014-07-11 18:43:04 +0900 | [diff] [blame] | 549 | setPreferredAddress(logicalAddress); |
Jungshik Jang | 1a4485d | 2014-05-26 11:02:36 +0900 | [diff] [blame] | 550 | } |
| 551 | |
Yuncheol Heo | b502186 | 2014-09-02 10:36:04 +0900 | [diff] [blame] | 552 | int getType() { |
| 553 | return mDeviceType; |
| 554 | } |
| 555 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 556 | @ServiceThreadOnly |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 557 | HdmiDeviceInfo getDeviceInfo() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 558 | assertRunOnServiceThread(); |
Jungshik Jang | 1a4485d | 2014-05-26 11:02:36 +0900 | [diff] [blame] | 559 | return mDeviceInfo; |
| 560 | } |
| 561 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 562 | @ServiceThreadOnly |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 563 | void setDeviceInfo(HdmiDeviceInfo info) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 564 | assertRunOnServiceThread(); |
Jungshik Jang | 1a4485d | 2014-05-26 11:02:36 +0900 | [diff] [blame] | 565 | mDeviceInfo = info; |
| 566 | } |
| 567 | |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 568 | // Returns true if the logical address is same as the argument. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 569 | @ServiceThreadOnly |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 570 | boolean isAddressOf(int addr) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 571 | assertRunOnServiceThread(); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 572 | return addr == mAddress; |
| 573 | } |
| 574 | |
| 575 | // Resets the logical address to unregistered(15), meaning the logical device is invalid. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 576 | @ServiceThreadOnly |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 577 | void clearAddress() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 578 | assertRunOnServiceThread(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 579 | mAddress = Constants.ADDR_UNREGISTERED; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 580 | } |
| 581 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 582 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 583 | void addAndStartAction(final HdmiCecFeatureAction action) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 584 | assertRunOnServiceThread(); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 585 | if (mService.isPowerStandbyOrTransient()) { |
| 586 | Slog.w(TAG, "Skip the action during Standby: " + action); |
| 587 | return; |
| 588 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 589 | mActions.add(action); |
| 590 | action.start(); |
| 591 | } |
| 592 | |
| 593 | // See if we have an action of a given type in progress. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 594 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 595 | <T extends HdmiCecFeatureAction> boolean hasAction(final Class<T> clazz) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 596 | assertRunOnServiceThread(); |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 597 | for (HdmiCecFeatureAction action : mActions) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 598 | if (action.getClass().equals(clazz)) { |
| 599 | return true; |
| 600 | } |
| 601 | } |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | // Returns all actions matched with given class type. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 606 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 607 | <T extends HdmiCecFeatureAction> List<T> getActions(final Class<T> clazz) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 608 | assertRunOnServiceThread(); |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 609 | List<T> actions = Collections.<T>emptyList(); |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 610 | for (HdmiCecFeatureAction action : mActions) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 611 | if (action.getClass().equals(clazz)) { |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 612 | if (actions.isEmpty()) { |
| 613 | actions = new ArrayList<T>(); |
| 614 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 615 | actions.add((T) action); |
| 616 | } |
| 617 | } |
| 618 | return actions; |
| 619 | } |
| 620 | |
| 621 | /** |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 622 | * Remove the given {@link HdmiCecFeatureAction} object from the action queue. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 623 | * |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 624 | * @param action {@link HdmiCecFeatureAction} to remove |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 625 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 626 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 627 | void removeAction(final HdmiCecFeatureAction action) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 628 | assertRunOnServiceThread(); |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 629 | action.finish(false); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 630 | mActions.remove(action); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 631 | checkIfPendingActionsCleared(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | // Remove all actions matched with the given Class type. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 635 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 636 | <T extends HdmiCecFeatureAction> void removeAction(final Class<T> clazz) { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 637 | assertRunOnServiceThread(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 638 | removeActionExcept(clazz, null); |
| 639 | } |
| 640 | |
| 641 | // Remove all actions matched with the given Class type besides |exception|. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 642 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 643 | <T extends HdmiCecFeatureAction> void removeActionExcept(final Class<T> clazz, |
| 644 | final HdmiCecFeatureAction exception) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 645 | assertRunOnServiceThread(); |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 646 | Iterator<HdmiCecFeatureAction> iter = mActions.iterator(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 647 | while (iter.hasNext()) { |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 648 | HdmiCecFeatureAction action = iter.next(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 649 | if (action != exception && action.getClass().equals(clazz)) { |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 650 | action.finish(false); |
| 651 | iter.remove(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 652 | } |
| 653 | } |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 654 | checkIfPendingActionsCleared(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 655 | } |
| 656 | |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 657 | protected void checkIfPendingActionsCleared() { |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 658 | if (mActions.isEmpty() && mPendingActionClearedCallback != null) { |
Yuncheol Heo | 26ba7fd | 2014-07-29 18:21:25 +0900 | [diff] [blame] | 659 | PendingActionClearedCallback callback = mPendingActionClearedCallback; |
| 660 | // To prevent from calling the callback again during handling the callback itself. |
| 661 | mPendingActionClearedCallback = null; |
| 662 | callback.onCleared(this); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 663 | } |
| 664 | } |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 665 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 666 | protected void assertRunOnServiceThread() { |
| 667 | if (Looper.myLooper() != mService.getServiceLooper()) { |
| 668 | throw new IllegalStateException("Should run on service thread."); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * Called when a hot-plug event issued. |
| 674 | * |
| 675 | * @param portId id of port where a hot-plug event happened |
| 676 | * @param connected whether to connected or not on the event |
| 677 | */ |
| 678 | void onHotplug(int portId, boolean connected) { |
| 679 | } |
| 680 | |
| 681 | final HdmiControlService getService() { |
| 682 | return mService; |
| 683 | } |
| 684 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 685 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 686 | final boolean isConnectedToArcPort(int path) { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 687 | assertRunOnServiceThread(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 688 | return mService.isConnectedToArcPort(path); |
| 689 | } |
| 690 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 691 | ActiveSource getActiveSource() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 692 | synchronized (mLock) { |
| 693 | return mActiveSource; |
| 694 | } |
| 695 | } |
| 696 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 697 | void setActiveSource(ActiveSource newActive) { |
| 698 | setActiveSource(newActive.logicalAddress, newActive.physicalAddress); |
| 699 | } |
| 700 | |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 701 | void setActiveSource(HdmiDeviceInfo info) { |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 702 | setActiveSource(info.getLogicalAddress(), info.getPhysicalAddress()); |
| 703 | } |
| 704 | |
| 705 | void setActiveSource(int logicalAddress, int physicalAddress) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 706 | synchronized (mLock) { |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 707 | mActiveSource.logicalAddress = logicalAddress; |
| 708 | mActiveSource.physicalAddress = physicalAddress; |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 709 | } |
Jinsuk Kim | e9f6ed3 | 2014-08-20 17:45:22 +0900 | [diff] [blame] | 710 | mService.setLastInputForMhl(Constants.INVALID_PORT_ID); |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 711 | } |
| 712 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 713 | int getActivePath() { |
| 714 | synchronized (mLock) { |
| 715 | return mActiveRoutingPath; |
| 716 | } |
| 717 | } |
| 718 | |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 719 | void setActivePath(int path) { |
| 720 | synchronized (mLock) { |
| 721 | mActiveRoutingPath = path; |
| 722 | } |
Jungshik Jang | 867b4e0 | 2014-08-12 13:41:30 +0900 | [diff] [blame] | 723 | mService.setActivePortId(pathToPortId(path)); |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 724 | } |
| 725 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 726 | /** |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 727 | * Returns the ID of the active HDMI port. The active port is the one that has the active |
| 728 | * routing path connected to it directly or indirectly under the device hierarchy. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 729 | */ |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 730 | int getActivePortId() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 731 | synchronized (mLock) { |
| 732 | return mService.pathToPortId(mActiveRoutingPath); |
| 733 | } |
| 734 | } |
| 735 | |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 736 | /** |
| 737 | * Update the active port. |
| 738 | * |
| 739 | * @param portId the new active port id |
| 740 | */ |
| 741 | void setActivePortId(int portId) { |
Jungshik Jang | 867b4e0 | 2014-08-12 13:41:30 +0900 | [diff] [blame] | 742 | // We update active routing path instead, since we get the active port id from |
| 743 | // the active routing path. |
| 744 | setActivePath(mService.portIdToPath(portId)); |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 745 | } |
| 746 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 747 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 748 | HdmiCecMessageCache getCecMessageCache() { |
| 749 | assertRunOnServiceThread(); |
| 750 | return mCecMessageCache; |
| 751 | } |
| 752 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 753 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 754 | int pathToPortId(int newPath) { |
| 755 | assertRunOnServiceThread(); |
| 756 | return mService.pathToPortId(newPath); |
| 757 | } |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 758 | |
| 759 | /** |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 760 | * Called when the system goes to standby mode. |
| 761 | * |
| 762 | * @param initiatedByCec true if this power sequence is initiated |
Jungshik Jang | b3e114a | 2014-07-11 17:50:05 +0900 | [diff] [blame] | 763 | * by the reception the CEC messages like <Standby> |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 764 | */ |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 765 | protected void onStandby(boolean initiatedByCec) {} |
| 766 | |
| 767 | /** |
| 768 | * Disable device. {@code callback} is used to get notified when all pending |
| 769 | * actions are completed or timeout is issued. |
| 770 | * |
| 771 | * @param initiatedByCec true if this sequence is initiated |
Jungshik Jang | b3e114a | 2014-07-11 17:50:05 +0900 | [diff] [blame] | 772 | * by the reception the CEC messages like <Standby> |
| 773 | * @param origialCallback callback interface to get notified when all pending actions are |
| 774 | * cleared |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 775 | */ |
Jungshik Jang | b3e114a | 2014-07-11 17:50:05 +0900 | [diff] [blame] | 776 | protected void disableDevice(boolean initiatedByCec, |
| 777 | final PendingActionClearedCallback origialCallback) { |
| 778 | mPendingActionClearedCallback = new PendingActionClearedCallback() { |
| 779 | @Override |
| 780 | public void onCleared(HdmiCecLocalDevice device) { |
| 781 | mHandler.removeMessages(MSG_DISABLE_DEVICE_TIMEOUT); |
| 782 | origialCallback.onCleared(device); |
| 783 | } |
| 784 | }; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 785 | mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_DISABLE_DEVICE_TIMEOUT), |
| 786 | DEVICE_CLEANUP_TIMEOUT); |
| 787 | } |
| 788 | |
| 789 | @ServiceThreadOnly |
| 790 | private void handleDisableDeviceTimeout() { |
| 791 | assertRunOnServiceThread(); |
| 792 | |
| 793 | // If all actions are not cleared in DEVICE_CLEANUP_TIMEOUT, enforce to finish them. |
| 794 | // onCleard will be called at the last action's finish method. |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 795 | Iterator<HdmiCecFeatureAction> iter = mActions.iterator(); |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 796 | while (iter.hasNext()) { |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 797 | HdmiCecFeatureAction action = iter.next(); |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 798 | action.finish(false); |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 799 | iter.remove(); |
| 800 | } |
| 801 | } |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 802 | |
| 803 | /** |
| 804 | * Send a key event to other device. |
| 805 | * |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 806 | * @param keyCode key code defined in {@link android.view.KeyEvent} |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 807 | * @param isPressed {@code true} for key down event |
| 808 | */ |
| 809 | protected void sendKeyEvent(int keyCode, boolean isPressed) { |
| 810 | Slog.w(TAG, "sendKeyEvent not implemented"); |
| 811 | } |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame] | 812 | |
| 813 | /** |
| 814 | * Dump internal status of HdmiCecLocalDevice object. |
| 815 | */ |
| 816 | protected void dump(final IndentingPrintWriter pw) { |
| 817 | pw.println("mDeviceType: " + mDeviceType); |
| 818 | pw.println("mAddress: " + mAddress); |
| 819 | pw.println("mPreferredAddress: " + mPreferredAddress); |
| 820 | pw.println("mDeviceInfo: " + mDeviceInfo); |
| 821 | pw.println("mActiveSource: " + mActiveSource); |
| 822 | pw.println(String.format("mActiveRoutingPath: 0x%04x", mActiveRoutingPath)); |
| 823 | } |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 824 | } |