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 | |
Amy | 1d0b137 | 2018-05-24 14:36:25 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 20 | import android.hardware.hdmi.HdmiDeviceInfo; |
shubang | d932cb5 | 2018-09-14 17:55:16 -0700 | [diff] [blame] | 21 | import android.hardware.hdmi.IHdmiControlCallback; |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 22 | import android.hardware.input.InputManager; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 23 | import android.os.Handler; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 24 | import android.os.Looper; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 25 | import android.os.Message; |
shubang | d932cb5 | 2018-09-14 17:55:16 -0700 | [diff] [blame] | 26 | import android.os.RemoteException; |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 27 | import android.os.SystemClock; |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 28 | import android.util.Slog; |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 29 | import android.view.InputDevice; |
| 30 | import android.view.KeyCharacterMap; |
| 31 | import android.view.KeyEvent; |
Amy | aefab64 | 2018-08-22 19:10:14 -0700 | [diff] [blame] | 32 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 33 | import com.android.internal.annotations.GuardedBy; |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame] | 34 | import com.android.internal.util.IndentingPrintWriter; |
Amy | 79b54e9 | 2018-09-11 17:02:28 -0700 | [diff] [blame] | 35 | import com.android.server.hdmi.Constants.LocalActivePort; |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 36 | import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly; |
Amy | 1d0b137 | 2018-05-24 14:36:25 -0700 | [diff] [blame] | 37 | import com.android.server.hdmi.HdmiControlService.SendMessageCallback; |
Amy | aefab64 | 2018-08-22 19:10:14 -0700 | [diff] [blame] | 38 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 39 | import java.util.ArrayList; |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 40 | import java.util.Collections; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 41 | import java.util.Iterator; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 42 | import java.util.List; |
| 43 | |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 44 | /** |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 45 | * Class that models a logical CEC device hosted in this system. Handles initialization, CEC |
| 46 | * commands that call for actions customized per device type. |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 47 | */ |
| 48 | abstract class HdmiCecLocalDevice { |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 49 | private static final String TAG = "HdmiCecLocalDevice"; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 50 | |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 51 | private static final int MSG_DISABLE_DEVICE_TIMEOUT = 1; |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 52 | private static final int MSG_USER_CONTROL_RELEASE_TIMEOUT = 2; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 53 | // Timeout in millisecond for device clean up (5s). |
| 54 | // Normal actions timeout is 2s but some of them would have several sequence of timeout. |
| 55 | private static final int DEVICE_CLEANUP_TIMEOUT = 5000; |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 56 | // Within the timer, a received <User Control Pressed> will start "Press and Hold" behavior. |
| 57 | // When it expires, we can assume <User Control Release> is received. |
| 58 | private static final int FOLLOWER_SAFETY_TIMEOUT = 550; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 59 | |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 60 | protected final HdmiControlService mService; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 61 | protected final int mDeviceType; |
| 62 | protected int mAddress; |
| 63 | protected int mPreferredAddress; |
Amy | 0361dd7 | 2018-11-07 16:54:25 -0800 | [diff] [blame] | 64 | @GuardedBy("mLock") |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 65 | protected HdmiDeviceInfo mDeviceInfo; |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 66 | protected int mLastKeycode = HdmiCecKeycode.UNSUPPORTED_KEYCODE; |
| 67 | protected int mLastKeyRepeatCount = 0; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 68 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 69 | static class ActiveSource { |
| 70 | int logicalAddress; |
| 71 | int physicalAddress; |
| 72 | |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 73 | public ActiveSource() { |
| 74 | invalidate(); |
| 75 | } |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 76 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 77 | public ActiveSource(int logical, int physical) { |
| 78 | logicalAddress = logical; |
| 79 | physicalAddress = physical; |
| 80 | } |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 81 | |
Jinsuk Kim | 04f813c | 2015-04-02 16:56:49 +0900 | [diff] [blame] | 82 | public static ActiveSource of(ActiveSource source) { |
| 83 | return new ActiveSource(source.logicalAddress, source.physicalAddress); |
| 84 | } |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 85 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 86 | public static ActiveSource of(int logical, int physical) { |
| 87 | return new ActiveSource(logical, physical); |
| 88 | } |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 89 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 90 | public boolean isValid() { |
| 91 | return HdmiUtils.isValidAddress(logicalAddress); |
| 92 | } |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 93 | |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 94 | public void invalidate() { |
| 95 | logicalAddress = Constants.ADDR_INVALID; |
| 96 | physicalAddress = Constants.INVALID_PHYSICAL_ADDRESS; |
| 97 | } |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 98 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 99 | public boolean equals(int logical, int physical) { |
| 100 | return logicalAddress == logical && physicalAddress == physical; |
| 101 | } |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 102 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 103 | @Override |
| 104 | public boolean equals(Object obj) { |
| 105 | if (obj instanceof ActiveSource) { |
| 106 | ActiveSource that = (ActiveSource) obj; |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 107 | return that.logicalAddress == logicalAddress |
| 108 | && that.physicalAddress == physicalAddress; |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 109 | } |
| 110 | return false; |
| 111 | } |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 112 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 113 | @Override |
| 114 | public int hashCode() { |
| 115 | return logicalAddress * 29 + physicalAddress; |
| 116 | } |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 117 | |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame] | 118 | @Override |
| 119 | public String toString() { |
| 120 | StringBuffer s = new StringBuffer(); |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 121 | String logicalAddressString = |
| 122 | (logicalAddress == Constants.ADDR_INVALID) |
| 123 | ? "invalid" |
| 124 | : String.format("0x%02x", logicalAddress); |
Jinsuk Kim | 04f813c | 2015-04-02 16:56:49 +0900 | [diff] [blame] | 125 | s.append("(").append(logicalAddressString); |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 126 | String physicalAddressString = |
| 127 | (physicalAddress == Constants.INVALID_PHYSICAL_ADDRESS) |
| 128 | ? "invalid" |
| 129 | : String.format("0x%04x", physicalAddress); |
Jinsuk Kim | 04f813c | 2015-04-02 16:56:49 +0900 | [diff] [blame] | 130 | s.append(", ").append(physicalAddressString).append(")"); |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame] | 131 | return s.toString(); |
| 132 | } |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 133 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 134 | |
| 135 | // Active routing path. Physical address of the active source but not all the time, such as |
| 136 | // when the new active source does not claim itself to be one. Note that we don't keep |
| 137 | // the active port id (or active input) since it can be gotten by {@link #pathToPortId(int)}. |
| 138 | @GuardedBy("mLock") |
| 139 | private int mActiveRoutingPath; |
| 140 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 141 | protected final HdmiCecMessageCache mCecMessageCache = new HdmiCecMessageCache(); |
| 142 | protected final Object mLock; |
| 143 | |
| 144 | // A collection of FeatureAction. |
| 145 | // Note that access to this collection should happen in service thread. |
Jungshik Jang | 5352081c | 2014-09-22 15:14:49 +0900 | [diff] [blame] | 146 | private final ArrayList<HdmiCecFeatureAction> mActions = new ArrayList<>(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 147 | |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 148 | private final Handler mHandler = |
| 149 | new Handler() { |
| 150 | @Override |
| 151 | public void handleMessage(Message msg) { |
| 152 | switch (msg.what) { |
| 153 | case MSG_DISABLE_DEVICE_TIMEOUT: |
| 154 | handleDisableDeviceTimeout(); |
| 155 | break; |
| 156 | case MSG_USER_CONTROL_RELEASE_TIMEOUT: |
| 157 | handleUserControlReleased(); |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | }; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 162 | |
| 163 | /** |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 164 | * A callback interface to get notified when all pending action is cleared. It can be called |
| 165 | * when timeout happened. |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 166 | */ |
| 167 | interface PendingActionClearedCallback { |
| 168 | void onCleared(HdmiCecLocalDevice device); |
| 169 | } |
| 170 | |
| 171 | protected PendingActionClearedCallback mPendingActionClearedCallback; |
| 172 | |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 173 | protected HdmiCecLocalDevice(HdmiControlService service, int deviceType) { |
| 174 | mService = service; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 175 | mDeviceType = deviceType; |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 176 | mAddress = Constants.ADDR_UNREGISTERED; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 177 | mLock = service.getServiceLock(); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Factory method that returns HdmiCecLocalDevice of corresponding type. |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 181 | static HdmiCecLocalDevice create(HdmiControlService service, int deviceType) { |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 182 | switch (deviceType) { |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 183 | case HdmiDeviceInfo.DEVICE_TV: |
| 184 | return new HdmiCecLocalDeviceTv(service); |
| 185 | case HdmiDeviceInfo.DEVICE_PLAYBACK: |
| 186 | return new HdmiCecLocalDevicePlayback(service); |
| 187 | case HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM: |
| 188 | return new HdmiCecLocalDeviceAudioSystem(service); |
| 189 | default: |
| 190 | return null; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 194 | @ServiceThreadOnly |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 195 | void init() { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 196 | assertRunOnServiceThread(); |
Jinsuk Kim | af2acf0 | 2014-07-11 18:43:04 +0900 | [diff] [blame] | 197 | mPreferredAddress = getPreferredAddress(); |
Jinsuk Kim | 7e4b480 | 2015-04-20 13:17:13 +0900 | [diff] [blame] | 198 | mPendingActionClearedCallback = null; |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 199 | } |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 200 | |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 201 | /** Called once a logical address of the local device is allocated. */ |
Yuncheol Heo | fc44e4e | 2014-08-04 19:41:09 +0900 | [diff] [blame] | 202 | protected abstract void onAddressAllocated(int logicalAddress, int reason); |
Jungshik Jang | 8b308d9 | 2014-05-29 21:52:28 +0900 | [diff] [blame] | 203 | |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 204 | /** Get the preferred logical address from system properties. */ |
Jinsuk Kim | af2acf0 | 2014-07-11 18:43:04 +0900 | [diff] [blame] | 205 | protected abstract int getPreferredAddress(); |
| 206 | |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 207 | /** Set the preferred logical address to system properties. */ |
Jinsuk Kim | af2acf0 | 2014-07-11 18:43:04 +0900 | [diff] [blame] | 208 | protected abstract void setPreferredAddress(int addr); |
| 209 | |
| 210 | /** |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 211 | * Returns true if the TV input associated with the CEC device is ready to accept further |
| 212 | * processing such as input switching. |
| 213 | * |
| 214 | * <p>This is used to buffer certain CEC commands and process it later if the input is not ready |
| 215 | * yet. For other types of local devices(non-TV), this method returns true by default to let the |
| 216 | * commands be processed right away. |
Jinsuk Kim | 6e26f7f | 2015-01-07 16:45:14 +0900 | [diff] [blame] | 217 | */ |
| 218 | protected boolean isInputReady(int deviceId) { |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | /** |
Jinsuk Kim | e26d833 | 2015-01-09 08:55:41 +0900 | [diff] [blame] | 223 | * Returns true if the local device allows the system to be put to standby. |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 224 | * |
| 225 | * <p>The default implementation returns true. |
Jinsuk Kim | e26d833 | 2015-01-09 08:55:41 +0900 | [diff] [blame] | 226 | */ |
| 227 | protected boolean canGoToStandby() { |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | /** |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 232 | * Dispatch incoming message. |
| 233 | * |
| 234 | * @param message incoming message |
| 235 | * @return true if consumed a message; otherwise, return false. |
| 236 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 237 | @ServiceThreadOnly |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 238 | boolean dispatchMessage(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 239 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 240 | int dest = message.getDestination(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 241 | if (dest != mAddress && dest != Constants.ADDR_BROADCAST) { |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 242 | return false; |
| 243 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 244 | // Cache incoming message. Note that it caches only white-listed one. |
| 245 | mCecMessageCache.cacheMessage(message); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 246 | return onMessage(message); |
| 247 | } |
| 248 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 249 | @ServiceThreadOnly |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 250 | protected final boolean onMessage(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 251 | assertRunOnServiceThread(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 252 | if (dispatchMessageToAction(message)) { |
| 253 | return true; |
| 254 | } |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 255 | switch (message.getOpcode()) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 256 | case Constants.MESSAGE_ACTIVE_SOURCE: |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 257 | return handleActiveSource(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 258 | case Constants.MESSAGE_INACTIVE_SOURCE: |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 259 | return handleInactiveSource(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 260 | case Constants.MESSAGE_REQUEST_ACTIVE_SOURCE: |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 261 | return handleRequestActiveSource(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 262 | case Constants.MESSAGE_GET_MENU_LANGUAGE: |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 263 | return handleGetMenuLanguage(message); |
Terry Heo | 795415b | 2014-10-01 15:03:53 +0900 | [diff] [blame] | 264 | case Constants.MESSAGE_SET_MENU_LANGUAGE: |
| 265 | return handleSetMenuLanguage(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 266 | case Constants.MESSAGE_GIVE_PHYSICAL_ADDRESS: |
Amy | 1d0b137 | 2018-05-24 14:36:25 -0700 | [diff] [blame] | 267 | return handleGivePhysicalAddress(null); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 268 | case Constants.MESSAGE_GIVE_OSD_NAME: |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 269 | return handleGiveOsdName(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 270 | case Constants.MESSAGE_GIVE_DEVICE_VENDOR_ID: |
Amy | 1d0b137 | 2018-05-24 14:36:25 -0700 | [diff] [blame] | 271 | return handleGiveDeviceVendorId(null); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 272 | case Constants.MESSAGE_GET_CEC_VERSION: |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 273 | return handleGetCecVersion(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 274 | case Constants.MESSAGE_REPORT_PHYSICAL_ADDRESS: |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 275 | return handleReportPhysicalAddress(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 276 | case Constants.MESSAGE_ROUTING_CHANGE: |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 277 | return handleRoutingChange(message); |
Yuncheol Heo | 64bafd9 | 2014-08-11 11:17:54 +0900 | [diff] [blame] | 278 | case Constants.MESSAGE_ROUTING_INFORMATION: |
| 279 | return handleRoutingInformation(message); |
Amy | 34ee91d | 2018-06-04 12:00:09 -0700 | [diff] [blame] | 280 | case Constants.MESSAGE_REQUEST_ARC_INITIATION: |
| 281 | return handleRequestArcInitiate(message); |
| 282 | case Constants.MESSAGE_REQUEST_ARC_TERMINATION: |
| 283 | return handleRequestArcTermination(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 284 | case Constants.MESSAGE_INITIATE_ARC: |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 285 | return handleInitiateArc(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 286 | case Constants.MESSAGE_TERMINATE_ARC: |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 287 | return handleTerminateArc(message); |
Amy | 34ee91d | 2018-06-04 12:00:09 -0700 | [diff] [blame] | 288 | case Constants.MESSAGE_REPORT_ARC_INITIATED: |
| 289 | return handleReportArcInitiate(message); |
| 290 | case Constants.MESSAGE_REPORT_ARC_TERMINATED: |
| 291 | return handleReportArcTermination(message); |
| 292 | case Constants.MESSAGE_SYSTEM_AUDIO_MODE_REQUEST: |
| 293 | return handleSystemAudioModeRequest(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 294 | case Constants.MESSAGE_SET_SYSTEM_AUDIO_MODE: |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 295 | return handleSetSystemAudioMode(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 296 | case Constants.MESSAGE_SYSTEM_AUDIO_MODE_STATUS: |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 297 | return handleSystemAudioModeStatus(message); |
Amy | 34ee91d | 2018-06-04 12:00:09 -0700 | [diff] [blame] | 298 | case Constants.MESSAGE_GIVE_SYSTEM_AUDIO_MODE_STATUS: |
| 299 | return handleGiveSystemAudioModeStatus(message); |
| 300 | case Constants.MESSAGE_GIVE_AUDIO_STATUS: |
| 301 | return handleGiveAudioStatus(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 302 | case Constants.MESSAGE_REPORT_AUDIO_STATUS: |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 303 | return handleReportAudioStatus(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 304 | case Constants.MESSAGE_STANDBY: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 305 | return handleStandby(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 306 | case Constants.MESSAGE_TEXT_VIEW_ON: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 307 | return handleTextViewOn(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 308 | case Constants.MESSAGE_IMAGE_VIEW_ON: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 309 | return handleImageViewOn(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 310 | case Constants.MESSAGE_USER_CONTROL_PRESSED: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 311 | return handleUserControlPressed(message); |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 312 | case Constants.MESSAGE_USER_CONTROL_RELEASED: |
| 313 | return handleUserControlReleased(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 314 | case Constants.MESSAGE_SET_STREAM_PATH: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 315 | return handleSetStreamPath(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 316 | case Constants.MESSAGE_GIVE_DEVICE_POWER_STATUS: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 317 | return handleGiveDevicePowerStatus(message); |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 318 | case Constants.MESSAGE_MENU_REQUEST: |
Yuncheol Heo | 184b124 | 2014-09-12 15:09:07 +0900 | [diff] [blame] | 319 | return handleMenuRequest(message); |
| 320 | case Constants.MESSAGE_MENU_STATUS: |
| 321 | return handleMenuStatus(message); |
Jinsuk Kim | 119160a | 2014-07-07 18:48:10 +0900 | [diff] [blame] | 322 | case Constants.MESSAGE_VENDOR_COMMAND: |
| 323 | return handleVendorCommand(message); |
| 324 | case Constants.MESSAGE_VENDOR_COMMAND_WITH_ID: |
| 325 | return handleVendorCommandWithId(message); |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 326 | case Constants.MESSAGE_SET_OSD_NAME: |
| 327 | return handleSetOsdName(message); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 328 | case Constants.MESSAGE_RECORD_TV_SCREEN: |
| 329 | return handleRecordTvScreen(message); |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 330 | case Constants.MESSAGE_TIMER_CLEARED_STATUS: |
| 331 | return handleTimerClearedStatus(message); |
Jungshik Jang | 4480efa | 2014-09-04 17:08:34 +0900 | [diff] [blame] | 332 | case Constants.MESSAGE_REPORT_POWER_STATUS: |
| 333 | return handleReportPowerStatus(message); |
| 334 | case Constants.MESSAGE_TIMER_STATUS: |
| 335 | return handleTimerStatus(message); |
| 336 | case Constants.MESSAGE_RECORD_STATUS: |
| 337 | return handleRecordStatus(message); |
Nick Chalko | 6f5d69e | 2018-07-17 16:07:11 -0700 | [diff] [blame] | 338 | case Constants.MESSAGE_REQUEST_SHORT_AUDIO_DESCRIPTOR: |
| 339 | return handleRequestShortAudioDescriptor(message); |
| 340 | case Constants.MESSAGE_REPORT_SHORT_AUDIO_DESCRIPTOR: |
| 341 | return handleReportShortAudioDescriptor(message); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 342 | default: |
| 343 | return false; |
| 344 | } |
| 345 | } |
| 346 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 347 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 348 | private boolean dispatchMessageToAction(HdmiCecMessage message) { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 349 | assertRunOnServiceThread(); |
Jungshik Jang | 5352081c | 2014-09-22 15:14:49 +0900 | [diff] [blame] | 350 | boolean processed = false; |
| 351 | // Use copied action list in that processCommand may remove itself. |
| 352 | for (HdmiCecFeatureAction action : new ArrayList<>(mActions)) { |
| 353 | // Iterates all actions to check whether incoming message is consumed. |
| 354 | boolean result = action.processCommand(message); |
| 355 | processed = processed || result; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 356 | } |
Jungshik Jang | 5352081c | 2014-09-22 15:14:49 +0900 | [diff] [blame] | 357 | return processed; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 358 | } |
| 359 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 360 | @ServiceThreadOnly |
Amy | 1d0b137 | 2018-05-24 14:36:25 -0700 | [diff] [blame] | 361 | protected boolean handleGivePhysicalAddress(@Nullable SendMessageCallback callback) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 362 | assertRunOnServiceThread(); |
| 363 | |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 364 | int physicalAddress = mService.getPhysicalAddress(); |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 365 | HdmiCecMessage cecMessage = |
| 366 | HdmiCecMessageBuilder.buildReportPhysicalAddressCommand( |
| 367 | mAddress, physicalAddress, mDeviceType); |
Amy | 1d0b137 | 2018-05-24 14:36:25 -0700 | [diff] [blame] | 368 | mService.sendCecCommand(cecMessage, callback); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 369 | return true; |
| 370 | } |
| 371 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 372 | @ServiceThreadOnly |
Amy | 1d0b137 | 2018-05-24 14:36:25 -0700 | [diff] [blame] | 373 | protected boolean handleGiveDeviceVendorId(@Nullable SendMessageCallback callback) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 374 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 375 | int vendorId = mService.getVendorId(); |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 376 | HdmiCecMessage cecMessage = |
| 377 | HdmiCecMessageBuilder.buildDeviceVendorIdCommand(mAddress, vendorId); |
Amy | 1d0b137 | 2018-05-24 14:36:25 -0700 | [diff] [blame] | 378 | mService.sendCecCommand(cecMessage, callback); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 379 | return true; |
| 380 | } |
| 381 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 382 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 383 | protected boolean handleGetCecVersion(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 384 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 385 | int version = mService.getCecVersion(); |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 386 | HdmiCecMessage cecMessage = |
| 387 | HdmiCecMessageBuilder.buildCecVersion( |
| 388 | message.getDestination(), message.getSource(), version); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 389 | mService.sendCecCommand(cecMessage); |
| 390 | return true; |
| 391 | } |
| 392 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 393 | @ServiceThreadOnly |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 394 | protected boolean handleActiveSource(HdmiCecMessage message) { |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | @ServiceThreadOnly |
| 399 | protected boolean handleInactiveSource(HdmiCecMessage message) { |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | @ServiceThreadOnly |
| 404 | protected boolean handleRequestActiveSource(HdmiCecMessage message) { |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 409 | protected boolean handleGetMenuLanguage(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 410 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 411 | Slog.w(TAG, "Only TV can handle <Get Menu Language>:" + message.toString()); |
Yuncheol Heo | 6aae652 | 2014-08-05 14:48:37 +0900 | [diff] [blame] | 412 | // 'return false' will cause to reply with <Feature Abort>. |
| 413 | return false; |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 414 | } |
| 415 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 416 | @ServiceThreadOnly |
Terry Heo | 795415b | 2014-10-01 15:03:53 +0900 | [diff] [blame] | 417 | protected boolean handleSetMenuLanguage(HdmiCecMessage message) { |
| 418 | assertRunOnServiceThread(); |
| 419 | Slog.w(TAG, "Only Playback device can handle <Set Menu Language>:" + message.toString()); |
| 420 | // 'return false' will cause to reply with <Feature Abort>. |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 425 | protected boolean handleGiveOsdName(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 426 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 427 | // Note that since this method is called after logical address allocation is done, |
| 428 | // mDeviceInfo should not be null. |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 429 | HdmiCecMessage cecMessage = |
| 430 | HdmiCecMessageBuilder.buildSetOsdNameCommand( |
| 431 | mAddress, message.getSource(), mDeviceInfo.getDisplayName()); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 432 | if (cecMessage != null) { |
| 433 | mService.sendCecCommand(cecMessage); |
| 434 | } else { |
| 435 | Slog.w(TAG, "Failed to build <Get Osd Name>:" + mDeviceInfo.getDisplayName()); |
| 436 | } |
| 437 | return true; |
| 438 | } |
| 439 | |
Amy | aefab64 | 2018-08-22 19:10:14 -0700 | [diff] [blame] | 440 | // Audio System device with no Playback device type |
| 441 | // needs to refactor this function if it's also a switch |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 442 | protected boolean handleRoutingChange(HdmiCecMessage message) { |
| 443 | return false; |
| 444 | } |
| 445 | |
Amy | aefab64 | 2018-08-22 19:10:14 -0700 | [diff] [blame] | 446 | // Audio System device with no Playback device type |
| 447 | // needs to refactor this function if it's also a switch |
Yuncheol Heo | 64bafd9 | 2014-08-11 11:17:54 +0900 | [diff] [blame] | 448 | protected boolean handleRoutingInformation(HdmiCecMessage message) { |
| 449 | return false; |
| 450 | } |
| 451 | |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 452 | protected boolean handleReportPhysicalAddress(HdmiCecMessage message) { |
| 453 | return false; |
| 454 | } |
| 455 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 456 | protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) { |
| 457 | return false; |
| 458 | } |
| 459 | |
Amy | 34ee91d | 2018-06-04 12:00:09 -0700 | [diff] [blame] | 460 | protected boolean handleGiveSystemAudioModeStatus(HdmiCecMessage message) { |
| 461 | return false; |
| 462 | } |
| 463 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 464 | protected boolean handleSetSystemAudioMode(HdmiCecMessage message) { |
| 465 | return false; |
| 466 | } |
| 467 | |
Amy | 34ee91d | 2018-06-04 12:00:09 -0700 | [diff] [blame] | 468 | protected boolean handleSystemAudioModeRequest(HdmiCecMessage message) { |
| 469 | return false; |
| 470 | } |
| 471 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 472 | protected boolean handleTerminateArc(HdmiCecMessage message) { |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | protected boolean handleInitiateArc(HdmiCecMessage message) { |
| 477 | return false; |
| 478 | } |
| 479 | |
Amy | 34ee91d | 2018-06-04 12:00:09 -0700 | [diff] [blame] | 480 | protected boolean handleRequestArcInitiate(HdmiCecMessage message) { |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | protected boolean handleRequestArcTermination(HdmiCecMessage message) { |
| 485 | return false; |
| 486 | } |
| 487 | |
| 488 | protected boolean handleReportArcInitiate(HdmiCecMessage message) { |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | protected boolean handleReportArcTermination(HdmiCecMessage message) { |
| 493 | return false; |
| 494 | } |
| 495 | |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 496 | protected boolean handleReportAudioStatus(HdmiCecMessage message) { |
| 497 | return false; |
| 498 | } |
| 499 | |
Amy | 34ee91d | 2018-06-04 12:00:09 -0700 | [diff] [blame] | 500 | protected boolean handleGiveAudioStatus(HdmiCecMessage message) { |
| 501 | return false; |
| 502 | } |
| 503 | |
Nick Chalko | 6f5d69e | 2018-07-17 16:07:11 -0700 | [diff] [blame] | 504 | protected boolean handleRequestShortAudioDescriptor(HdmiCecMessage message) { |
| 505 | return false; |
| 506 | } |
| 507 | |
| 508 | protected boolean handleReportShortAudioDescriptor(HdmiCecMessage message) { |
| 509 | return false; |
| 510 | } |
| 511 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 512 | @ServiceThreadOnly |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 513 | protected boolean handleStandby(HdmiCecMessage message) { |
| 514 | assertRunOnServiceThread(); |
| 515 | // Seq #12 |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 516 | if (mService.isControlEnabled() |
| 517 | && !mService.isProhibitMode() |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 518 | && mService.isPowerOnOrTransient()) { |
| 519 | mService.standby(); |
| 520 | return true; |
| 521 | } |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | @ServiceThreadOnly |
| 526 | protected boolean handleUserControlPressed(HdmiCecMessage message) { |
| 527 | assertRunOnServiceThread(); |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 528 | mHandler.removeMessages(MSG_USER_CONTROL_RELEASE_TIMEOUT); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 529 | if (mService.isPowerOnOrTransient() && isPowerOffOrToggleCommand(message)) { |
| 530 | mService.standby(); |
| 531 | return true; |
| 532 | } else if (mService.isPowerStandbyOrTransient() && isPowerOnOrToggleCommand(message)) { |
| 533 | mService.wakeUp(); |
| 534 | return true; |
| 535 | } |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 536 | |
| 537 | final long downTime = SystemClock.uptimeMillis(); |
| 538 | final byte[] params = message.getParams(); |
Jungshik Jang | 73483b6b | 2014-09-26 14:00:59 +0900 | [diff] [blame] | 539 | final int keycode = HdmiCecKeycode.cecKeycodeAndParamsToAndroidKey(params); |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 540 | int keyRepeatCount = 0; |
| 541 | if (mLastKeycode != HdmiCecKeycode.UNSUPPORTED_KEYCODE) { |
| 542 | if (keycode == mLastKeycode) { |
| 543 | keyRepeatCount = mLastKeyRepeatCount + 1; |
| 544 | } else { |
| 545 | injectKeyEvent(downTime, KeyEvent.ACTION_UP, mLastKeycode, 0); |
| 546 | } |
| 547 | } |
| 548 | mLastKeycode = keycode; |
| 549 | mLastKeyRepeatCount = keyRepeatCount; |
| 550 | |
| 551 | if (keycode != HdmiCecKeycode.UNSUPPORTED_KEYCODE) { |
| 552 | injectKeyEvent(downTime, KeyEvent.ACTION_DOWN, keycode, keyRepeatCount); |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 553 | mHandler.sendMessageDelayed( |
| 554 | Message.obtain(mHandler, MSG_USER_CONTROL_RELEASE_TIMEOUT), |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 555 | FOLLOWER_SAFETY_TIMEOUT); |
| 556 | return true; |
| 557 | } |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 558 | return false; |
| 559 | } |
| 560 | |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 561 | @ServiceThreadOnly |
| 562 | protected boolean handleUserControlReleased() { |
| 563 | assertRunOnServiceThread(); |
| 564 | mHandler.removeMessages(MSG_USER_CONTROL_RELEASE_TIMEOUT); |
| 565 | mLastKeyRepeatCount = 0; |
| 566 | if (mLastKeycode != HdmiCecKeycode.UNSUPPORTED_KEYCODE) { |
| 567 | final long upTime = SystemClock.uptimeMillis(); |
| 568 | injectKeyEvent(upTime, KeyEvent.ACTION_UP, mLastKeycode, 0); |
| 569 | mLastKeycode = HdmiCecKeycode.UNSUPPORTED_KEYCODE; |
| 570 | return true; |
| 571 | } |
| 572 | return false; |
| 573 | } |
| 574 | |
| 575 | static void injectKeyEvent(long time, int action, int keycode, int repeat) { |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 576 | KeyEvent keyEvent = |
| 577 | KeyEvent.obtain( |
| 578 | time, |
| 579 | time, |
| 580 | action, |
| 581 | keycode, |
| 582 | repeat, |
| 583 | 0, |
| 584 | KeyCharacterMap.VIRTUAL_KEYBOARD, |
| 585 | 0, |
| 586 | KeyEvent.FLAG_FROM_SYSTEM, |
| 587 | InputDevice.SOURCE_HDMI, |
| 588 | null); |
| 589 | InputManager.getInstance() |
| 590 | .injectInputEvent(keyEvent, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 591 | keyEvent.recycle(); |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 592 | } |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 593 | |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 594 | static boolean isPowerOnOrToggleCommand(HdmiCecMessage message) { |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 595 | byte[] params = message.getParams(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 596 | return message.getOpcode() == Constants.MESSAGE_USER_CONTROL_PRESSED |
Jungshik Jang | 210d73d | 2014-07-04 11:11:29 +0900 | [diff] [blame] | 597 | && (params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER |
| 598 | || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_ON_FUNCTION |
| 599 | || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_TOGGLE_FUNCTION); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 600 | } |
| 601 | |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 602 | static boolean isPowerOffOrToggleCommand(HdmiCecMessage message) { |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 603 | byte[] params = message.getParams(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 604 | return message.getOpcode() == Constants.MESSAGE_USER_CONTROL_PRESSED |
Shuichi.Noguchi | 55269f7 | 2018-01-13 23:59:19 +0900 | [diff] [blame] | 605 | && (params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_OFF_FUNCTION |
Jungshik Jang | 210d73d | 2014-07-04 11:11:29 +0900 | [diff] [blame] | 606 | || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_TOGGLE_FUNCTION); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | protected boolean handleTextViewOn(HdmiCecMessage message) { |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | protected boolean handleImageViewOn(HdmiCecMessage message) { |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | protected boolean handleSetStreamPath(HdmiCecMessage message) { |
| 618 | return false; |
| 619 | } |
| 620 | |
| 621 | protected boolean handleGiveDevicePowerStatus(HdmiCecMessage message) { |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 622 | mService.sendCecCommand( |
| 623 | HdmiCecMessageBuilder.buildReportPowerStatus( |
| 624 | mAddress, message.getSource(), mService.getPowerStatus())); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 625 | return true; |
| 626 | } |
| 627 | |
Yuncheol Heo | 184b124 | 2014-09-12 15:09:07 +0900 | [diff] [blame] | 628 | protected boolean handleMenuRequest(HdmiCecMessage message) { |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 629 | // Always report menu active to receive Remote Control. |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 630 | mService.sendCecCommand( |
| 631 | HdmiCecMessageBuilder.buildReportMenuStatus( |
| 632 | mAddress, message.getSource(), Constants.MENU_STATE_ACTIVATED)); |
Terry Heo | 3e1564e | 2014-08-12 14:41:00 +0900 | [diff] [blame] | 633 | return true; |
| 634 | } |
| 635 | |
Yuncheol Heo | 184b124 | 2014-09-12 15:09:07 +0900 | [diff] [blame] | 636 | protected boolean handleMenuStatus(HdmiCecMessage message) { |
| 637 | return false; |
| 638 | } |
| 639 | |
Jinsuk Kim | 119160a | 2014-07-07 18:48:10 +0900 | [diff] [blame] | 640 | protected boolean handleVendorCommand(HdmiCecMessage message) { |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 641 | if (!mService.invokeVendorCommandListenersOnReceived( |
| 642 | mDeviceType, |
| 643 | message.getSource(), |
| 644 | message.getDestination(), |
| 645 | message.getParams(), |
| 646 | false)) { |
Jinsuk Kim | d4a94db | 2014-09-12 13:51:10 +0900 | [diff] [blame] | 647 | // Vendor command listener may not have been registered yet. Respond with |
| 648 | // <Feature Abort> [NOT_IN_CORRECT_MODE] so that the sender can try again later. |
| 649 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_NOT_IN_CORRECT_MODE); |
| 650 | } |
Jinsuk Kim | 119160a | 2014-07-07 18:48:10 +0900 | [diff] [blame] | 651 | return true; |
| 652 | } |
| 653 | |
| 654 | protected boolean handleVendorCommandWithId(HdmiCecMessage message) { |
| 655 | byte[] params = message.getParams(); |
| 656 | int vendorId = HdmiUtils.threeBytesToInt(params); |
| 657 | if (vendorId == mService.getVendorId()) { |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 658 | if (!mService.invokeVendorCommandListenersOnReceived( |
| 659 | mDeviceType, message.getSource(), message.getDestination(), params, true)) { |
Jinsuk Kim | d4a94db | 2014-09-12 13:51:10 +0900 | [diff] [blame] | 660 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_NOT_IN_CORRECT_MODE); |
| 661 | } |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 662 | } else if (message.getDestination() != Constants.ADDR_BROADCAST |
| 663 | && message.getSource() != Constants.ADDR_UNREGISTERED) { |
Jinsuk Kim | 119160a | 2014-07-07 18:48:10 +0900 | [diff] [blame] | 664 | Slog.v(TAG, "Wrong direct vendor command. Replying with <Feature Abort>"); |
Yuncheol Heo | 6aae652 | 2014-08-05 14:48:37 +0900 | [diff] [blame] | 665 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_UNRECOGNIZED_OPCODE); |
Jinsuk Kim | 119160a | 2014-07-07 18:48:10 +0900 | [diff] [blame] | 666 | } else { |
| 667 | Slog.v(TAG, "Wrong broadcast vendor command. Ignoring"); |
| 668 | } |
| 669 | return true; |
| 670 | } |
| 671 | |
Jinsuk Kim | d4a94db | 2014-09-12 13:51:10 +0900 | [diff] [blame] | 672 | protected void sendStandby(int deviceId) { |
| 673 | // Do nothing. |
| 674 | } |
| 675 | |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 676 | protected boolean handleSetOsdName(HdmiCecMessage message) { |
| 677 | // The default behavior of <Set Osd Name> is doing nothing. |
| 678 | return true; |
| 679 | } |
| 680 | |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 681 | protected boolean handleRecordTvScreen(HdmiCecMessage message) { |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 682 | // The default behavior of <Record TV Screen> is replying <Feature Abort> with |
| 683 | // "Cannot provide source". |
Yuncheol Heo | 6aae652 | 2014-08-05 14:48:37 +0900 | [diff] [blame] | 684 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_CANNOT_PROVIDE_SOURCE); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 685 | return true; |
| 686 | } |
| 687 | |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 688 | protected boolean handleTimerClearedStatus(HdmiCecMessage message) { |
| 689 | return false; |
| 690 | } |
| 691 | |
Jungshik Jang | 4480efa | 2014-09-04 17:08:34 +0900 | [diff] [blame] | 692 | protected boolean handleReportPowerStatus(HdmiCecMessage message) { |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | protected boolean handleTimerStatus(HdmiCecMessage message) { |
| 697 | return false; |
| 698 | } |
| 699 | |
| 700 | protected boolean handleRecordStatus(HdmiCecMessage message) { |
| 701 | return false; |
| 702 | } |
| 703 | |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 704 | @ServiceThreadOnly |
Yuncheol Heo | fc44e4e | 2014-08-04 19:41:09 +0900 | [diff] [blame] | 705 | final void handleAddressAllocated(int logicalAddress, int reason) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 706 | assertRunOnServiceThread(); |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 707 | mAddress = mPreferredAddress = logicalAddress; |
Yuncheol Heo | fc44e4e | 2014-08-04 19:41:09 +0900 | [diff] [blame] | 708 | onAddressAllocated(logicalAddress, reason); |
Jinsuk Kim | af2acf0 | 2014-07-11 18:43:04 +0900 | [diff] [blame] | 709 | setPreferredAddress(logicalAddress); |
Jungshik Jang | 1a4485d | 2014-05-26 11:02:36 +0900 | [diff] [blame] | 710 | } |
| 711 | |
Yuncheol Heo | b502186 | 2014-09-02 10:36:04 +0900 | [diff] [blame] | 712 | int getType() { |
| 713 | return mDeviceType; |
| 714 | } |
| 715 | |
Amy | 0361dd7 | 2018-11-07 16:54:25 -0800 | [diff] [blame] | 716 | @GuardedBy("mLock") |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 717 | HdmiDeviceInfo getDeviceInfo() { |
Amy | 0361dd7 | 2018-11-07 16:54:25 -0800 | [diff] [blame] | 718 | synchronized (mLock) { |
| 719 | return mDeviceInfo; |
| 720 | } |
Jungshik Jang | 1a4485d | 2014-05-26 11:02:36 +0900 | [diff] [blame] | 721 | } |
| 722 | |
Amy | 0361dd7 | 2018-11-07 16:54:25 -0800 | [diff] [blame] | 723 | @GuardedBy("mLock") |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 724 | void setDeviceInfo(HdmiDeviceInfo info) { |
Amy | 0361dd7 | 2018-11-07 16:54:25 -0800 | [diff] [blame] | 725 | synchronized (mLock) { |
| 726 | mDeviceInfo = info; |
| 727 | } |
Jungshik Jang | 1a4485d | 2014-05-26 11:02:36 +0900 | [diff] [blame] | 728 | } |
| 729 | |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 730 | // Returns true if the logical address is same as the argument. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 731 | @ServiceThreadOnly |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 732 | boolean isAddressOf(int addr) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 733 | assertRunOnServiceThread(); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 734 | return addr == mAddress; |
| 735 | } |
| 736 | |
| 737 | // 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] | 738 | @ServiceThreadOnly |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 739 | void clearAddress() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 740 | assertRunOnServiceThread(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 741 | mAddress = Constants.ADDR_UNREGISTERED; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 742 | } |
| 743 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 744 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 745 | void addAndStartAction(final HdmiCecFeatureAction action) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 746 | assertRunOnServiceThread(); |
Jinsuk Kim | 6f87b4e | 2014-10-10 14:40:29 +0900 | [diff] [blame] | 747 | mActions.add(action); |
Donghyun Cho | fc462b9 | 2016-05-13 21:06:02 +0900 | [diff] [blame] | 748 | if (mService.isPowerStandby() || !mService.isAddressAllocated()) { |
Jinsuk Kim | 6f87b4e | 2014-10-10 14:40:29 +0900 | [diff] [blame] | 749 | Slog.i(TAG, "Not ready to start action. Queued for deferred start:" + action); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 750 | return; |
| 751 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 752 | action.start(); |
| 753 | } |
| 754 | |
Jinsuk Kim | 6f87b4e | 2014-10-10 14:40:29 +0900 | [diff] [blame] | 755 | @ServiceThreadOnly |
| 756 | void startQueuedActions() { |
| 757 | assertRunOnServiceThread(); |
Shuichi.Noguchi | c4426af | 2017-12-19 19:44:48 +0900 | [diff] [blame] | 758 | // Use copied action list in that start() may remove itself. |
| 759 | for (HdmiCecFeatureAction action : new ArrayList<>(mActions)) { |
Jinsuk Kim | 6f87b4e | 2014-10-10 14:40:29 +0900 | [diff] [blame] | 760 | if (!action.started()) { |
| 761 | Slog.i(TAG, "Starting queued action:" + action); |
| 762 | action.start(); |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 767 | // See if we have an action of a given type in progress. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 768 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 769 | <T extends HdmiCecFeatureAction> boolean hasAction(final Class<T> clazz) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 770 | assertRunOnServiceThread(); |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 771 | for (HdmiCecFeatureAction action : mActions) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 772 | if (action.getClass().equals(clazz)) { |
| 773 | return true; |
| 774 | } |
| 775 | } |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | // Returns all actions matched with given class type. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 780 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 781 | <T extends HdmiCecFeatureAction> List<T> getActions(final Class<T> clazz) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 782 | assertRunOnServiceThread(); |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 783 | List<T> actions = Collections.<T>emptyList(); |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 784 | for (HdmiCecFeatureAction action : mActions) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 785 | if (action.getClass().equals(clazz)) { |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 786 | if (actions.isEmpty()) { |
| 787 | actions = new ArrayList<T>(); |
| 788 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 789 | actions.add((T) action); |
| 790 | } |
| 791 | } |
| 792 | return actions; |
| 793 | } |
| 794 | |
| 795 | /** |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 796 | * Remove the given {@link HdmiCecFeatureAction} object from the action queue. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 797 | * |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 798 | * @param action {@link HdmiCecFeatureAction} to remove |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 799 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 800 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 801 | void removeAction(final HdmiCecFeatureAction action) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 802 | assertRunOnServiceThread(); |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 803 | action.finish(false); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 804 | mActions.remove(action); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 805 | checkIfPendingActionsCleared(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | // Remove all actions matched with the given Class type. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 809 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 810 | <T extends HdmiCecFeatureAction> void removeAction(final Class<T> clazz) { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 811 | assertRunOnServiceThread(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 812 | removeActionExcept(clazz, null); |
| 813 | } |
| 814 | |
| 815 | // Remove all actions matched with the given Class type besides |exception|. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 816 | @ServiceThreadOnly |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 817 | <T extends HdmiCecFeatureAction> void removeActionExcept( |
| 818 | final Class<T> clazz, final HdmiCecFeatureAction exception) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 819 | assertRunOnServiceThread(); |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 820 | Iterator<HdmiCecFeatureAction> iter = mActions.iterator(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 821 | while (iter.hasNext()) { |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 822 | HdmiCecFeatureAction action = iter.next(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 823 | if (action != exception && action.getClass().equals(clazz)) { |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 824 | action.finish(false); |
| 825 | iter.remove(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 826 | } |
| 827 | } |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 828 | checkIfPendingActionsCleared(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 829 | } |
| 830 | |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 831 | protected void checkIfPendingActionsCleared() { |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 832 | if (mActions.isEmpty() && mPendingActionClearedCallback != null) { |
Yuncheol Heo | 26ba7fd | 2014-07-29 18:21:25 +0900 | [diff] [blame] | 833 | PendingActionClearedCallback callback = mPendingActionClearedCallback; |
| 834 | // To prevent from calling the callback again during handling the callback itself. |
| 835 | mPendingActionClearedCallback = null; |
| 836 | callback.onCleared(this); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 837 | } |
| 838 | } |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 839 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 840 | protected void assertRunOnServiceThread() { |
| 841 | if (Looper.myLooper() != mService.getServiceLooper()) { |
| 842 | throw new IllegalStateException("Should run on service thread."); |
| 843 | } |
| 844 | } |
| 845 | |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 846 | void setAutoDeviceOff(boolean enabled) {} |
Jinsuk Kim | e6e8f3d | 2015-05-11 14:17:04 +0900 | [diff] [blame] | 847 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 848 | /** |
| 849 | * Called when a hot-plug event issued. |
| 850 | * |
| 851 | * @param portId id of port where a hot-plug event happened |
| 852 | * @param connected whether to connected or not on the event |
| 853 | */ |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 854 | void onHotplug(int portId, boolean connected) {} |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 855 | |
| 856 | final HdmiControlService getService() { |
| 857 | return mService; |
| 858 | } |
| 859 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 860 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 861 | final boolean isConnectedToArcPort(int path) { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 862 | assertRunOnServiceThread(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 863 | return mService.isConnectedToArcPort(path); |
| 864 | } |
| 865 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 866 | ActiveSource getActiveSource() { |
Amy | 123ec40 | 2018-09-25 10:56:31 -0700 | [diff] [blame] | 867 | return mService.getActiveSource(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 868 | } |
| 869 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 870 | void setActiveSource(ActiveSource newActive) { |
| 871 | setActiveSource(newActive.logicalAddress, newActive.physicalAddress); |
| 872 | } |
| 873 | |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 874 | void setActiveSource(HdmiDeviceInfo info) { |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 875 | setActiveSource(info.getLogicalAddress(), info.getPhysicalAddress()); |
| 876 | } |
| 877 | |
| 878 | void setActiveSource(int logicalAddress, int physicalAddress) { |
Amy | 123ec40 | 2018-09-25 10:56:31 -0700 | [diff] [blame] | 879 | mService.setActiveSource(logicalAddress, physicalAddress); |
Jinsuk Kim | e9f6ed3 | 2014-08-20 17:45:22 +0900 | [diff] [blame] | 880 | mService.setLastInputForMhl(Constants.INVALID_PORT_ID); |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 881 | } |
| 882 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 883 | int getActivePath() { |
| 884 | synchronized (mLock) { |
| 885 | return mActiveRoutingPath; |
| 886 | } |
| 887 | } |
| 888 | |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 889 | void setActivePath(int path) { |
| 890 | synchronized (mLock) { |
| 891 | mActiveRoutingPath = path; |
| 892 | } |
Jungshik Jang | 867b4e0 | 2014-08-12 13:41:30 +0900 | [diff] [blame] | 893 | mService.setActivePortId(pathToPortId(path)); |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 894 | } |
| 895 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 896 | /** |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 897 | * Returns the ID of the active HDMI port. The active port is the one that has the active |
| 898 | * routing path connected to it directly or indirectly under the device hierarchy. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 899 | */ |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 900 | int getActivePortId() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 901 | synchronized (mLock) { |
| 902 | return mService.pathToPortId(mActiveRoutingPath); |
| 903 | } |
| 904 | } |
| 905 | |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 906 | /** |
| 907 | * Update the active port. |
| 908 | * |
| 909 | * @param portId the new active port id |
| 910 | */ |
| 911 | void setActivePortId(int portId) { |
Jungshik Jang | 867b4e0 | 2014-08-12 13:41:30 +0900 | [diff] [blame] | 912 | // We update active routing path instead, since we get the active port id from |
| 913 | // the active routing path. |
| 914 | setActivePath(mService.portIdToPath(portId)); |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 915 | } |
| 916 | |
Amy | c00cd4e | 2018-10-16 20:21:33 -0700 | [diff] [blame] | 917 | // Returns the id of the port that the target device is connected to. |
| 918 | int getPortId(int physicalAddress) { |
| 919 | return mService.pathToPortId(physicalAddress); |
| 920 | } |
| 921 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 922 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 923 | HdmiCecMessageCache getCecMessageCache() { |
| 924 | assertRunOnServiceThread(); |
| 925 | return mCecMessageCache; |
| 926 | } |
| 927 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 928 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 929 | int pathToPortId(int newPath) { |
| 930 | assertRunOnServiceThread(); |
| 931 | return mService.pathToPortId(newPath); |
| 932 | } |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 933 | |
| 934 | /** |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 935 | * Called when the system goes to standby mode. |
| 936 | * |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 937 | * @param initiatedByCec true if this power sequence is initiated by the reception the CEC |
| 938 | * messages like <Standby> |
| 939 | * @param standbyAction Intent action that drives the standby process, either {@link |
| 940 | * HdmiControlService#STANDBY_SCREEN_OFF} or {@link HdmiControlService#STANDBY_SHUTDOWN} |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 941 | */ |
Jinsuk Kim | e6e8f3d | 2015-05-11 14:17:04 +0900 | [diff] [blame] | 942 | protected void onStandby(boolean initiatedByCec, int standbyAction) {} |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 943 | |
| 944 | /** |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 945 | * Disable device. {@code callback} is used to get notified when all pending actions are |
| 946 | * completed or timeout is issued. |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 947 | * |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 948 | * @param initiatedByCec true if this sequence is initiated by the reception the CEC messages |
| 949 | * like <Standby> |
Jinsuk Kim | 7e4b480 | 2015-04-20 13:17:13 +0900 | [diff] [blame] | 950 | * @param originalCallback callback interface to get notified when all pending actions are |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 951 | * cleared |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 952 | */ |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 953 | protected void disableDevice( |
| 954 | boolean initiatedByCec, final PendingActionClearedCallback originalCallback) { |
| 955 | mPendingActionClearedCallback = |
| 956 | new PendingActionClearedCallback() { |
| 957 | @Override |
| 958 | public void onCleared(HdmiCecLocalDevice device) { |
| 959 | mHandler.removeMessages(MSG_DISABLE_DEVICE_TIMEOUT); |
| 960 | originalCallback.onCleared(device); |
| 961 | } |
| 962 | }; |
| 963 | mHandler.sendMessageDelayed( |
| 964 | Message.obtain(mHandler, MSG_DISABLE_DEVICE_TIMEOUT), DEVICE_CLEANUP_TIMEOUT); |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | @ServiceThreadOnly |
| 968 | private void handleDisableDeviceTimeout() { |
| 969 | assertRunOnServiceThread(); |
| 970 | |
| 971 | // If all actions are not cleared in DEVICE_CLEANUP_TIMEOUT, enforce to finish them. |
| 972 | // onCleard will be called at the last action's finish method. |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 973 | Iterator<HdmiCecFeatureAction> iter = mActions.iterator(); |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 974 | while (iter.hasNext()) { |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 975 | HdmiCecFeatureAction action = iter.next(); |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 976 | action.finish(false); |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 977 | iter.remove(); |
| 978 | } |
Jinsuk Kim | 7e4b480 | 2015-04-20 13:17:13 +0900 | [diff] [blame] | 979 | if (mPendingActionClearedCallback != null) { |
| 980 | mPendingActionClearedCallback.onCleared(this); |
| 981 | } |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 982 | } |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 983 | |
| 984 | /** |
Donghyun Cho | 7609bc3 | 2016-12-02 15:31:52 +0900 | [diff] [blame] | 985 | * Send a key event to other CEC device. The logical address of target device will be given by |
| 986 | * {@link #findKeyReceiverAddress}. |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 987 | * |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 988 | * @param keyCode key code defined in {@link android.view.KeyEvent} |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 989 | * @param isPressed {@code true} for key down event |
Donghyun Cho | 7609bc3 | 2016-12-02 15:31:52 +0900 | [diff] [blame] | 990 | * @see #findKeyReceiverAddress() |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 991 | */ |
Donghyun Cho | 7609bc3 | 2016-12-02 15:31:52 +0900 | [diff] [blame] | 992 | @ServiceThreadOnly |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 993 | protected void sendKeyEvent(int keyCode, boolean isPressed) { |
Donghyun Cho | 7609bc3 | 2016-12-02 15:31:52 +0900 | [diff] [blame] | 994 | assertRunOnServiceThread(); |
| 995 | if (!HdmiCecKeycode.isSupportedKeycode(keyCode)) { |
| 996 | Slog.w(TAG, "Unsupported key: " + keyCode); |
| 997 | return; |
| 998 | } |
| 999 | List<SendKeyAction> action = getActions(SendKeyAction.class); |
| 1000 | int logicalAddress = findKeyReceiverAddress(); |
| 1001 | if (logicalAddress == Constants.ADDR_INVALID || logicalAddress == mAddress) { |
| 1002 | // Don't send key event to invalid device or itself. |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 1003 | Slog.w( |
| 1004 | TAG, |
| 1005 | "Discard key event: " |
| 1006 | + keyCode |
| 1007 | + ", pressed:" |
| 1008 | + isPressed |
| 1009 | + ", receiverAddr=" |
| 1010 | + logicalAddress); |
Donghyun Cho | 7609bc3 | 2016-12-02 15:31:52 +0900 | [diff] [blame] | 1011 | } else if (!action.isEmpty()) { |
| 1012 | action.get(0).processKeyEvent(keyCode, isPressed); |
| 1013 | } else if (isPressed) { |
| 1014 | addAndStartAction(new SendKeyAction(this, logicalAddress, keyCode)); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | /** |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 1019 | * Returns the logical address of the device which will receive key events via {@link |
| 1020 | * #sendKeyEvent}. |
Donghyun Cho | 7609bc3 | 2016-12-02 15:31:52 +0900 | [diff] [blame] | 1021 | * |
| 1022 | * @see #sendKeyEvent(int, boolean) |
| 1023 | */ |
| 1024 | protected int findKeyReceiverAddress() { |
| 1025 | Slog.w(TAG, "findKeyReceiverAddress is not implemented"); |
| 1026 | return Constants.ADDR_INVALID; |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 1027 | } |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame] | 1028 | |
shubang | d932cb5 | 2018-09-14 17:55:16 -0700 | [diff] [blame] | 1029 | @ServiceThreadOnly |
| 1030 | void invokeCallback(IHdmiControlCallback callback, int result) { |
| 1031 | assertRunOnServiceThread(); |
| 1032 | if (callback == null) { |
| 1033 | return; |
| 1034 | } |
| 1035 | try { |
| 1036 | callback.onComplete(result); |
| 1037 | } catch (RemoteException e) { |
| 1038 | Slog.e(TAG, "Invoking callback failed:" + e); |
| 1039 | } |
| 1040 | } |
| 1041 | |
Jungshik Jang | 2e8f1b6 | 2014-09-03 08:28:02 +0900 | [diff] [blame] | 1042 | void sendUserControlPressedAndReleased(int targetAddress, int cecKeycode) { |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 1043 | mService.sendCecCommand( |
| 1044 | HdmiCecMessageBuilder.buildUserControlPressed(mAddress, targetAddress, cecKeycode)); |
| 1045 | mService.sendCecCommand( |
| 1046 | HdmiCecMessageBuilder.buildUserControlReleased(mAddress, targetAddress)); |
Jungshik Jang | 2e8f1b6 | 2014-09-03 08:28:02 +0900 | [diff] [blame] | 1047 | } |
| 1048 | |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 1049 | /** Dump internal status of HdmiCecLocalDevice object. */ |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame] | 1050 | protected void dump(final IndentingPrintWriter pw) { |
| 1051 | pw.println("mDeviceType: " + mDeviceType); |
| 1052 | pw.println("mAddress: " + mAddress); |
| 1053 | pw.println("mPreferredAddress: " + mPreferredAddress); |
| 1054 | pw.println("mDeviceInfo: " + mDeviceInfo); |
Amy | 123ec40 | 2018-09-25 10:56:31 -0700 | [diff] [blame] | 1055 | pw.println("mActiveSource: " + getActiveSource()); |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame] | 1056 | pw.println(String.format("mActiveRoutingPath: 0x%04x", mActiveRoutingPath)); |
| 1057 | } |
Amy | aefab64 | 2018-08-22 19:10:14 -0700 | [diff] [blame] | 1058 | |
Amy | 79b54e9 | 2018-09-11 17:02:28 -0700 | [diff] [blame] | 1059 | /** Calculates the physical address for {@code activePortId}. |
| 1060 | * |
| 1061 | * <p>This method assumes current device physical address is valid. |
| 1062 | * <p>If the current device is already the leaf of the whole CEC system |
| 1063 | * and can't have devices under it, will return its own physical address. |
| 1064 | * |
| 1065 | * @param activePortId is the local active port Id |
| 1066 | * @return the calculated physical address of the port |
| 1067 | */ |
| 1068 | protected int getActivePathOnSwitchFromActivePortId(@LocalActivePort int activePortId) { |
| 1069 | int myPhysicalAddress = mService.getPhysicalAddress(); |
| 1070 | int finalMask = activePortId << 8; |
| 1071 | int mask; |
| 1072 | for (mask = 0x0F00; mask > 0x000F; mask >>= 4) { |
| 1073 | if ((myPhysicalAddress & mask) == 0) { |
| 1074 | break; |
| 1075 | } else { |
| 1076 | finalMask >>= 4; |
| 1077 | } |
| 1078 | } |
| 1079 | return finalMask | myPhysicalAddress; |
| 1080 | } |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 1081 | } |