blob: 8031c058c4ce0727d52d1c2d9979e18173a2ff1f [file] [log] [blame]
Jinsuk Kim2918e9e2014-05-20 16:45:45 +09001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.hdmi;
18
Jungshik Jang61f4fbd2014-08-06 19:21:12 +090019import android.hardware.hdmi.HdmiDeviceInfo;
Terry Heo3e1564e2014-08-12 14:41:00 +090020import android.hardware.input.InputManager;
Jungshik Jang4fc1d102014-07-09 19:24:50 +090021import android.os.Handler;
Jungshik Jang79c58a42014-06-16 16:45:36 +090022import android.os.Looper;
Jungshik Jang4fc1d102014-07-09 19:24:50 +090023import android.os.Message;
Terry Heo3e1564e2014-08-12 14:41:00 +090024import android.os.SystemClock;
Jungshik Jang092b4452014-06-11 15:19:17 +090025import android.util.Slog;
Terry Heo3e1564e2014-08-12 14:41:00 +090026import android.view.InputDevice;
27import android.view.KeyCharacterMap;
28import android.view.KeyEvent;
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090029
Jungshik Jang79c58a42014-06-16 16:45:36 +090030import com.android.internal.annotations.GuardedBy;
Terry Heo959d2db2014-08-28 16:45:41 +090031import com.android.internal.util.IndentingPrintWriter;
Jungshik Janga5b74142014-06-23 18:03:10 +090032import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly;
Jungshik Jang79c58a42014-06-16 16:45:36 +090033
34import java.util.ArrayList;
Jinsuk Kim43c23e22014-07-29 13:59:14 +090035import java.util.Collections;
Jungshik Jang79c58a42014-06-16 16:45:36 +090036import java.util.Iterator;
Jungshik Jang79c58a42014-06-16 16:45:36 +090037import java.util.List;
38
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090039/**
40 * Class that models a logical CEC device hosted in this system. Handles initialization,
41 * CEC commands that call for actions customized per device type.
42 */
43abstract class HdmiCecLocalDevice {
Jungshik Jang092b4452014-06-11 15:19:17 +090044 private static final String TAG = "HdmiCecLocalDevice";
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090045
Jungshik Jang4fc1d102014-07-09 19:24:50 +090046 private static final int MSG_DISABLE_DEVICE_TIMEOUT = 1;
Terry Heo3e1564e2014-08-12 14:41:00 +090047 private static final int MSG_USER_CONTROL_RELEASE_TIMEOUT = 2;
Jungshik Jang4fc1d102014-07-09 19:24:50 +090048 // Timeout in millisecond for device clean up (5s).
49 // Normal actions timeout is 2s but some of them would have several sequence of timeout.
50 private static final int DEVICE_CLEANUP_TIMEOUT = 5000;
Terry Heo3e1564e2014-08-12 14:41:00 +090051 // Within the timer, a received <User Control Pressed> will start "Press and Hold" behavior.
52 // When it expires, we can assume <User Control Release> is received.
53 private static final int FOLLOWER_SAFETY_TIMEOUT = 550;
Jungshik Jang4fc1d102014-07-09 19:24:50 +090054
Jungshik Jang3ee65722014-06-03 16:22:30 +090055 protected final HdmiControlService mService;
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090056 protected final int mDeviceType;
57 protected int mAddress;
58 protected int mPreferredAddress;
Jungshik Jang61f4fbd2014-08-06 19:21:12 +090059 protected HdmiDeviceInfo mDeviceInfo;
Terry Heo3e1564e2014-08-12 14:41:00 +090060 protected int mLastKeycode = HdmiCecKeycode.UNSUPPORTED_KEYCODE;
61 protected int mLastKeyRepeatCount = 0;
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090062
Jinsuk Kim72b7d732014-07-24 09:15:35 +090063 static class ActiveSource {
64 int logicalAddress;
65 int physicalAddress;
66
Jinsuk Kim43c23e22014-07-29 13:59:14 +090067 public ActiveSource() {
68 invalidate();
69 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +090070 public ActiveSource(int logical, int physical) {
71 logicalAddress = logical;
72 physicalAddress = physical;
73 }
Jinsuk Kim04f813c2015-04-02 16:56:49 +090074 public static ActiveSource of(ActiveSource source) {
75 return new ActiveSource(source.logicalAddress, source.physicalAddress);
76 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +090077 public static ActiveSource of(int logical, int physical) {
78 return new ActiveSource(logical, physical);
79 }
80 public boolean isValid() {
81 return HdmiUtils.isValidAddress(logicalAddress);
82 }
Jinsuk Kim43c23e22014-07-29 13:59:14 +090083 public void invalidate() {
84 logicalAddress = Constants.ADDR_INVALID;
85 physicalAddress = Constants.INVALID_PHYSICAL_ADDRESS;
86 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +090087 public boolean equals(int logical, int physical) {
88 return logicalAddress == logical && physicalAddress == physical;
89 }
90 @Override
91 public boolean equals(Object obj) {
92 if (obj instanceof ActiveSource) {
93 ActiveSource that = (ActiveSource) obj;
94 return that.logicalAddress == logicalAddress &&
95 that.physicalAddress == physicalAddress;
96 }
97 return false;
98 }
99 @Override
100 public int hashCode() {
101 return logicalAddress * 29 + physicalAddress;
102 }
Terry Heo959d2db2014-08-28 16:45:41 +0900103 @Override
104 public String toString() {
105 StringBuffer s = new StringBuffer();
106 String logicalAddressString = (logicalAddress == Constants.ADDR_INVALID)
107 ? "invalid" : String.format("0x%02x", logicalAddress);
Jinsuk Kim04f813c2015-04-02 16:56:49 +0900108 s.append("(").append(logicalAddressString);
Terry Heo959d2db2014-08-28 16:45:41 +0900109 String physicalAddressString = (physicalAddress == Constants.INVALID_PHYSICAL_ADDRESS)
110 ? "invalid" : String.format("0x%04x", physicalAddress);
Jinsuk Kim04f813c2015-04-02 16:56:49 +0900111 s.append(", ").append(physicalAddressString).append(")");
Terry Heo959d2db2014-08-28 16:45:41 +0900112 return s.toString();
113 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900114 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900115 // Logical address of the active source.
116 @GuardedBy("mLock")
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900117 protected final ActiveSource mActiveSource = new ActiveSource();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900118
119 // Active routing path. Physical address of the active source but not all the time, such as
120 // when the new active source does not claim itself to be one. Note that we don't keep
121 // the active port id (or active input) since it can be gotten by {@link #pathToPortId(int)}.
122 @GuardedBy("mLock")
123 private int mActiveRoutingPath;
124
Jungshik Jang79c58a42014-06-16 16:45:36 +0900125 protected final HdmiCecMessageCache mCecMessageCache = new HdmiCecMessageCache();
126 protected final Object mLock;
127
128 // A collection of FeatureAction.
129 // Note that access to this collection should happen in service thread.
Jungshik Jang5352081c2014-09-22 15:14:49 +0900130 private final ArrayList<HdmiCecFeatureAction> mActions = new ArrayList<>();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900131
Yuncheol Heoc516d652014-07-11 18:23:24 +0900132 private final Handler mHandler = new Handler () {
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900133 @Override
134 public void handleMessage(Message msg) {
135 switch (msg.what) {
136 case MSG_DISABLE_DEVICE_TIMEOUT:
137 handleDisableDeviceTimeout();
138 break;
Terry Heo3e1564e2014-08-12 14:41:00 +0900139 case MSG_USER_CONTROL_RELEASE_TIMEOUT:
140 handleUserControlReleased();
141 break;
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900142 }
143 }
144 };
145
146 /**
147 * A callback interface to get notified when all pending action is cleared.
148 * It can be called when timeout happened.
149 */
150 interface PendingActionClearedCallback {
151 void onCleared(HdmiCecLocalDevice device);
152 }
153
154 protected PendingActionClearedCallback mPendingActionClearedCallback;
155
Jungshik Jang3ee65722014-06-03 16:22:30 +0900156 protected HdmiCecLocalDevice(HdmiControlService service, int deviceType) {
157 mService = service;
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900158 mDeviceType = deviceType;
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900159 mAddress = Constants.ADDR_UNREGISTERED;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900160 mLock = service.getServiceLock();
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900161 }
162
163 // Factory method that returns HdmiCecLocalDevice of corresponding type.
Jungshik Jang3ee65722014-06-03 16:22:30 +0900164 static HdmiCecLocalDevice create(HdmiControlService service, int deviceType) {
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900165 switch (deviceType) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900166 case HdmiDeviceInfo.DEVICE_TV:
Jungshik Jang3ee65722014-06-03 16:22:30 +0900167 return new HdmiCecLocalDeviceTv(service);
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900168 case HdmiDeviceInfo.DEVICE_PLAYBACK:
Jungshik Jang3ee65722014-06-03 16:22:30 +0900169 return new HdmiCecLocalDevicePlayback(service);
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900170 default:
171 return null;
172 }
173 }
174
Jungshik Janga5b74142014-06-23 18:03:10 +0900175 @ServiceThreadOnly
Jungshik Jang3ee65722014-06-03 16:22:30 +0900176 void init() {
Jungshik Janga5b74142014-06-23 18:03:10 +0900177 assertRunOnServiceThread();
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900178 mPreferredAddress = getPreferredAddress();
Jinsuk Kim7e4b4802015-04-20 13:17:13 +0900179 mPendingActionClearedCallback = null;
Jungshik Jang3ee65722014-06-03 16:22:30 +0900180 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900181
Jungshik Jang8b308d92014-05-29 21:52:28 +0900182 /**
Jungshik Jang3ee65722014-06-03 16:22:30 +0900183 * Called once a logical address of the local device is allocated.
Jungshik Jang8b308d92014-05-29 21:52:28 +0900184 */
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900185 protected abstract void onAddressAllocated(int logicalAddress, int reason);
Jungshik Jang8b308d92014-05-29 21:52:28 +0900186
Jungshik Jang092b4452014-06-11 15:19:17 +0900187 /**
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900188 * Get the preferred logical address from system properties.
189 */
190 protected abstract int getPreferredAddress();
191
192 /**
193 * Set the preferred logical address to system properties.
194 */
195 protected abstract void setPreferredAddress(int addr);
196
197 /**
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900198 * Returns true if the TV input associated with the CEC device is ready
199 * to accept further processing such as input switching. This is used
200 * to buffer certain CEC commands and process it later if the input is not
201 * ready yet. For other types of local devices(non-TV), this method returns
202 * true by default to let the commands be processed right away.
203 */
204 protected boolean isInputReady(int deviceId) {
205 return true;
206 }
207
208 /**
Jinsuk Kime26d8332015-01-09 08:55:41 +0900209 * Returns true if the local device allows the system to be put to standby.
210 * The default implementation returns true.
211 */
212 protected boolean canGoToStandby() {
213 return true;
214 }
215
216 /**
Jungshik Jang092b4452014-06-11 15:19:17 +0900217 * Dispatch incoming message.
218 *
219 * @param message incoming message
220 * @return true if consumed a message; otherwise, return false.
221 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900222 @ServiceThreadOnly
Yuncheol Heo25c20292014-07-31 17:59:39 +0900223 boolean dispatchMessage(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900224 assertRunOnServiceThread();
Jungshik Jang092b4452014-06-11 15:19:17 +0900225 int dest = message.getDestination();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900226 if (dest != mAddress && dest != Constants.ADDR_BROADCAST) {
Jungshik Jang092b4452014-06-11 15:19:17 +0900227 return false;
228 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900229 // Cache incoming message. Note that it caches only white-listed one.
230 mCecMessageCache.cacheMessage(message);
Jungshik Jang092b4452014-06-11 15:19:17 +0900231 return onMessage(message);
232 }
233
Jungshik Janga5b74142014-06-23 18:03:10 +0900234 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900235 protected final boolean onMessage(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900236 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900237 if (dispatchMessageToAction(message)) {
238 return true;
239 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900240 switch (message.getOpcode()) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900241 case Constants.MESSAGE_ACTIVE_SOURCE:
Jinsuk Kim83335712014-06-24 07:57:00 +0900242 return handleActiveSource(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900243 case Constants.MESSAGE_INACTIVE_SOURCE:
Jinsuk Kim83335712014-06-24 07:57:00 +0900244 return handleInactiveSource(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900245 case Constants.MESSAGE_REQUEST_ACTIVE_SOURCE:
Jinsuk Kim83335712014-06-24 07:57:00 +0900246 return handleRequestActiveSource(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900247 case Constants.MESSAGE_GET_MENU_LANGUAGE:
Jungshik Jang092b4452014-06-11 15:19:17 +0900248 return handleGetMenuLanguage(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900249 case Constants.MESSAGE_GIVE_PHYSICAL_ADDRESS:
Jungshik Jang092b4452014-06-11 15:19:17 +0900250 return handleGivePhysicalAddress();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900251 case Constants.MESSAGE_GIVE_OSD_NAME:
Jungshik Jang092b4452014-06-11 15:19:17 +0900252 return handleGiveOsdName(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900253 case Constants.MESSAGE_GIVE_DEVICE_VENDOR_ID:
Jungshik Jang092b4452014-06-11 15:19:17 +0900254 return handleGiveDeviceVendorId();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900255 case Constants.MESSAGE_GET_CEC_VERSION:
Jungshik Jang092b4452014-06-11 15:19:17 +0900256 return handleGetCecVersion(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900257 case Constants.MESSAGE_REPORT_PHYSICAL_ADDRESS:
Jungshik Jang60cffce2014-06-12 18:03:04 +0900258 return handleReportPhysicalAddress(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900259 case Constants.MESSAGE_ROUTING_CHANGE:
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900260 return handleRoutingChange(message);
Yuncheol Heo64bafd92014-08-11 11:17:54 +0900261 case Constants.MESSAGE_ROUTING_INFORMATION:
262 return handleRoutingInformation(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900263 case Constants.MESSAGE_INITIATE_ARC:
Jungshik Jang79c58a42014-06-16 16:45:36 +0900264 return handleInitiateArc(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900265 case Constants.MESSAGE_TERMINATE_ARC:
Jungshik Jang79c58a42014-06-16 16:45:36 +0900266 return handleTerminateArc(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900267 case Constants.MESSAGE_SET_SYSTEM_AUDIO_MODE:
Jungshik Jang79c58a42014-06-16 16:45:36 +0900268 return handleSetSystemAudioMode(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900269 case Constants.MESSAGE_SYSTEM_AUDIO_MODE_STATUS:
Jungshik Jang79c58a42014-06-16 16:45:36 +0900270 return handleSystemAudioModeStatus(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900271 case Constants.MESSAGE_REPORT_AUDIO_STATUS:
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900272 return handleReportAudioStatus(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900273 case Constants.MESSAGE_STANDBY:
Yuncheol Heo38db6292014-07-01 14:15:14 +0900274 return handleStandby(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900275 case Constants.MESSAGE_TEXT_VIEW_ON:
Yuncheol Heo38db6292014-07-01 14:15:14 +0900276 return handleTextViewOn(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900277 case Constants.MESSAGE_IMAGE_VIEW_ON:
Yuncheol Heo38db6292014-07-01 14:15:14 +0900278 return handleImageViewOn(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900279 case Constants.MESSAGE_USER_CONTROL_PRESSED:
Yuncheol Heo38db6292014-07-01 14:15:14 +0900280 return handleUserControlPressed(message);
Terry Heo3e1564e2014-08-12 14:41:00 +0900281 case Constants.MESSAGE_USER_CONTROL_RELEASED:
282 return handleUserControlReleased();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900283 case Constants.MESSAGE_SET_STREAM_PATH:
Yuncheol Heo38db6292014-07-01 14:15:14 +0900284 return handleSetStreamPath(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900285 case Constants.MESSAGE_GIVE_DEVICE_POWER_STATUS:
Yuncheol Heo38db6292014-07-01 14:15:14 +0900286 return handleGiveDevicePowerStatus(message);
Terry Heo3e1564e2014-08-12 14:41:00 +0900287 case Constants.MESSAGE_MENU_REQUEST:
Yuncheol Heo184b1242014-09-12 15:09:07 +0900288 return handleMenuRequest(message);
289 case Constants.MESSAGE_MENU_STATUS:
290 return handleMenuStatus(message);
Jinsuk Kim119160a2014-07-07 18:48:10 +0900291 case Constants.MESSAGE_VENDOR_COMMAND:
292 return handleVendorCommand(message);
293 case Constants.MESSAGE_VENDOR_COMMAND_WITH_ID:
294 return handleVendorCommandWithId(message);
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900295 case Constants.MESSAGE_SET_OSD_NAME:
296 return handleSetOsdName(message);
Jungshik Jangb6591b82014-07-23 16:10:23 +0900297 case Constants.MESSAGE_RECORD_TV_SCREEN:
298 return handleRecordTvScreen(message);
Jungshik Jange5a93372014-07-25 13:41:14 +0900299 case Constants.MESSAGE_TIMER_CLEARED_STATUS:
300 return handleTimerClearedStatus(message);
Jungshik Jang4480efa2014-09-04 17:08:34 +0900301 case Constants.MESSAGE_REPORT_POWER_STATUS:
302 return handleReportPowerStatus(message);
303 case Constants.MESSAGE_TIMER_STATUS:
304 return handleTimerStatus(message);
305 case Constants.MESSAGE_RECORD_STATUS:
306 return handleRecordStatus(message);
Jungshik Jang092b4452014-06-11 15:19:17 +0900307 default:
308 return false;
309 }
310 }
311
Jungshik Janga5b74142014-06-23 18:03:10 +0900312 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900313 private boolean dispatchMessageToAction(HdmiCecMessage message) {
Jungshik Janga5b74142014-06-23 18:03:10 +0900314 assertRunOnServiceThread();
Jungshik Jang5352081c2014-09-22 15:14:49 +0900315 boolean processed = false;
316 // Use copied action list in that processCommand may remove itself.
317 for (HdmiCecFeatureAction action : new ArrayList<>(mActions)) {
318 // Iterates all actions to check whether incoming message is consumed.
319 boolean result = action.processCommand(message);
320 processed = processed || result;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900321 }
Jungshik Jang5352081c2014-09-22 15:14:49 +0900322 return processed;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900323 }
324
Jungshik Janga5b74142014-06-23 18:03:10 +0900325 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900326 protected boolean handleGivePhysicalAddress() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900327 assertRunOnServiceThread();
328
Jungshik Jang092b4452014-06-11 15:19:17 +0900329 int physicalAddress = mService.getPhysicalAddress();
330 HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildReportPhysicalAddressCommand(
331 mAddress, physicalAddress, mDeviceType);
332 mService.sendCecCommand(cecMessage);
333 return true;
334 }
335
Jungshik Janga5b74142014-06-23 18:03:10 +0900336 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900337 protected boolean handleGiveDeviceVendorId() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900338 assertRunOnServiceThread();
Jungshik Jang092b4452014-06-11 15:19:17 +0900339 int vendorId = mService.getVendorId();
340 HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildDeviceVendorIdCommand(
341 mAddress, vendorId);
342 mService.sendCecCommand(cecMessage);
343 return true;
344 }
345
Jungshik Janga5b74142014-06-23 18:03:10 +0900346 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900347 protected boolean handleGetCecVersion(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900348 assertRunOnServiceThread();
Jungshik Jang092b4452014-06-11 15:19:17 +0900349 int version = mService.getCecVersion();
350 HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildCecVersion(message.getDestination(),
351 message.getSource(), version);
352 mService.sendCecCommand(cecMessage);
353 return true;
354 }
355
Jungshik Janga5b74142014-06-23 18:03:10 +0900356 @ServiceThreadOnly
Jinsuk Kim83335712014-06-24 07:57:00 +0900357 protected boolean handleActiveSource(HdmiCecMessage message) {
358 return false;
359 }
360
361 @ServiceThreadOnly
362 protected boolean handleInactiveSource(HdmiCecMessage message) {
363 return false;
364 }
365
366 @ServiceThreadOnly
367 protected boolean handleRequestActiveSource(HdmiCecMessage message) {
368 return false;
369 }
370
371 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900372 protected boolean handleGetMenuLanguage(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900373 assertRunOnServiceThread();
Jungshik Jang092b4452014-06-11 15:19:17 +0900374 Slog.w(TAG, "Only TV can handle <Get Menu Language>:" + message.toString());
Yuncheol Heo6aae6522014-08-05 14:48:37 +0900375 // 'return false' will cause to reply with <Feature Abort>.
376 return false;
Jungshik Jang092b4452014-06-11 15:19:17 +0900377 }
378
Jungshik Janga5b74142014-06-23 18:03:10 +0900379 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900380 protected boolean handleGiveOsdName(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900381 assertRunOnServiceThread();
Jungshik Jang092b4452014-06-11 15:19:17 +0900382 // Note that since this method is called after logical address allocation is done,
383 // mDeviceInfo should not be null.
384 HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildSetOsdNameCommand(
385 mAddress, message.getSource(), mDeviceInfo.getDisplayName());
386 if (cecMessage != null) {
387 mService.sendCecCommand(cecMessage);
388 } else {
389 Slog.w(TAG, "Failed to build <Get Osd Name>:" + mDeviceInfo.getDisplayName());
390 }
391 return true;
392 }
393
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900394 protected boolean handleRoutingChange(HdmiCecMessage message) {
395 return false;
396 }
397
Yuncheol Heo64bafd92014-08-11 11:17:54 +0900398 protected boolean handleRoutingInformation(HdmiCecMessage message) {
399 return false;
400 }
401
Jungshik Jang60cffce2014-06-12 18:03:04 +0900402 protected boolean handleReportPhysicalAddress(HdmiCecMessage message) {
403 return false;
404 }
405
Jungshik Jang79c58a42014-06-16 16:45:36 +0900406 protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) {
407 return false;
408 }
409
410 protected boolean handleSetSystemAudioMode(HdmiCecMessage message) {
411 return false;
412 }
413
414 protected boolean handleTerminateArc(HdmiCecMessage message) {
415 return false;
416 }
417
418 protected boolean handleInitiateArc(HdmiCecMessage message) {
419 return false;
420 }
421
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900422 protected boolean handleReportAudioStatus(HdmiCecMessage message) {
423 return false;
424 }
425
Jungshik Janga5b74142014-06-23 18:03:10 +0900426 @ServiceThreadOnly
Yuncheol Heo38db6292014-07-01 14:15:14 +0900427 protected boolean handleStandby(HdmiCecMessage message) {
428 assertRunOnServiceThread();
429 // Seq #12
Jinsuk Kim4d43d932014-07-03 16:43:58 +0900430 if (mService.isControlEnabled() && !mService.isProhibitMode()
Yuncheol Heo38db6292014-07-01 14:15:14 +0900431 && mService.isPowerOnOrTransient()) {
432 mService.standby();
433 return true;
434 }
435 return false;
436 }
437
438 @ServiceThreadOnly
439 protected boolean handleUserControlPressed(HdmiCecMessage message) {
440 assertRunOnServiceThread();
Terry Heo3e1564e2014-08-12 14:41:00 +0900441 mHandler.removeMessages(MSG_USER_CONTROL_RELEASE_TIMEOUT);
Yuncheol Heo38db6292014-07-01 14:15:14 +0900442 if (mService.isPowerOnOrTransient() && isPowerOffOrToggleCommand(message)) {
443 mService.standby();
444 return true;
445 } else if (mService.isPowerStandbyOrTransient() && isPowerOnOrToggleCommand(message)) {
446 mService.wakeUp();
447 return true;
448 }
Terry Heo3e1564e2014-08-12 14:41:00 +0900449
450 final long downTime = SystemClock.uptimeMillis();
451 final byte[] params = message.getParams();
Jungshik Jang73483b6b2014-09-26 14:00:59 +0900452 final int keycode = HdmiCecKeycode.cecKeycodeAndParamsToAndroidKey(params);
Terry Heo3e1564e2014-08-12 14:41:00 +0900453 int keyRepeatCount = 0;
454 if (mLastKeycode != HdmiCecKeycode.UNSUPPORTED_KEYCODE) {
455 if (keycode == mLastKeycode) {
456 keyRepeatCount = mLastKeyRepeatCount + 1;
457 } else {
458 injectKeyEvent(downTime, KeyEvent.ACTION_UP, mLastKeycode, 0);
459 }
460 }
461 mLastKeycode = keycode;
462 mLastKeyRepeatCount = keyRepeatCount;
463
464 if (keycode != HdmiCecKeycode.UNSUPPORTED_KEYCODE) {
465 injectKeyEvent(downTime, KeyEvent.ACTION_DOWN, keycode, keyRepeatCount);
466 mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_USER_CONTROL_RELEASE_TIMEOUT),
467 FOLLOWER_SAFETY_TIMEOUT);
468 return true;
469 }
Yuncheol Heo38db6292014-07-01 14:15:14 +0900470 return false;
471 }
472
Terry Heo3e1564e2014-08-12 14:41:00 +0900473 @ServiceThreadOnly
474 protected boolean handleUserControlReleased() {
475 assertRunOnServiceThread();
476 mHandler.removeMessages(MSG_USER_CONTROL_RELEASE_TIMEOUT);
477 mLastKeyRepeatCount = 0;
478 if (mLastKeycode != HdmiCecKeycode.UNSUPPORTED_KEYCODE) {
479 final long upTime = SystemClock.uptimeMillis();
480 injectKeyEvent(upTime, KeyEvent.ACTION_UP, mLastKeycode, 0);
481 mLastKeycode = HdmiCecKeycode.UNSUPPORTED_KEYCODE;
482 return true;
483 }
484 return false;
485 }
486
487 static void injectKeyEvent(long time, int action, int keycode, int repeat) {
488 KeyEvent keyEvent = KeyEvent.obtain(time, time, action, keycode,
489 repeat, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM,
490 InputDevice.SOURCE_HDMI, null);
491 InputManager.getInstance().injectInputEvent(keyEvent,
492 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
493 keyEvent.recycle();
494 }
495
Yuncheol Heo25c20292014-07-31 17:59:39 +0900496 static boolean isPowerOnOrToggleCommand(HdmiCecMessage message) {
Yuncheol Heo38db6292014-07-01 14:15:14 +0900497 byte[] params = message.getParams();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900498 return message.getOpcode() == Constants.MESSAGE_USER_CONTROL_PRESSED
Jungshik Jang210d73d2014-07-04 11:11:29 +0900499 && (params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER
500 || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_ON_FUNCTION
501 || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_TOGGLE_FUNCTION);
Yuncheol Heo38db6292014-07-01 14:15:14 +0900502 }
503
Yuncheol Heo25c20292014-07-31 17:59:39 +0900504 static boolean isPowerOffOrToggleCommand(HdmiCecMessage message) {
Yuncheol Heo38db6292014-07-01 14:15:14 +0900505 byte[] params = message.getParams();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900506 return message.getOpcode() == Constants.MESSAGE_USER_CONTROL_PRESSED
Jungshik Jang210d73d2014-07-04 11:11:29 +0900507 && (params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER
508 || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_OFF_FUNCTION
509 || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_TOGGLE_FUNCTION);
Yuncheol Heo38db6292014-07-01 14:15:14 +0900510 }
511
512 protected boolean handleTextViewOn(HdmiCecMessage message) {
513 return false;
514 }
515
516 protected boolean handleImageViewOn(HdmiCecMessage message) {
517 return false;
518 }
519
520 protected boolean handleSetStreamPath(HdmiCecMessage message) {
521 return false;
522 }
523
524 protected boolean handleGiveDevicePowerStatus(HdmiCecMessage message) {
525 mService.sendCecCommand(HdmiCecMessageBuilder.buildReportPowerStatus(
526 mAddress, message.getSource(), mService.getPowerStatus()));
527 return true;
528 }
529
Yuncheol Heo184b1242014-09-12 15:09:07 +0900530 protected boolean handleMenuRequest(HdmiCecMessage message) {
Terry Heo3e1564e2014-08-12 14:41:00 +0900531 // Always report menu active to receive Remote Control.
532 mService.sendCecCommand(HdmiCecMessageBuilder.buildReportMenuStatus(
533 mAddress, message.getSource(), Constants.MENU_STATE_ACTIVATED));
534 return true;
535 }
536
Yuncheol Heo184b1242014-09-12 15:09:07 +0900537 protected boolean handleMenuStatus(HdmiCecMessage message) {
538 return false;
539 }
540
Jinsuk Kim119160a2014-07-07 18:48:10 +0900541 protected boolean handleVendorCommand(HdmiCecMessage message) {
Yuncheol Heo0608b932014-10-13 16:39:18 +0900542 if (!mService.invokeVendorCommandListenersOnReceived(mDeviceType, message.getSource(),
543 message.getDestination(), message.getParams(), false)) {
Jinsuk Kimd4a94db2014-09-12 13:51:10 +0900544 // Vendor command listener may not have been registered yet. Respond with
545 // <Feature Abort> [NOT_IN_CORRECT_MODE] so that the sender can try again later.
546 mService.maySendFeatureAbortCommand(message, Constants.ABORT_NOT_IN_CORRECT_MODE);
547 }
Jinsuk Kim119160a2014-07-07 18:48:10 +0900548 return true;
549 }
550
551 protected boolean handleVendorCommandWithId(HdmiCecMessage message) {
552 byte[] params = message.getParams();
553 int vendorId = HdmiUtils.threeBytesToInt(params);
554 if (vendorId == mService.getVendorId()) {
Yuncheol Heo0608b932014-10-13 16:39:18 +0900555 if (!mService.invokeVendorCommandListenersOnReceived(mDeviceType, message.getSource(),
556 message.getDestination(), params, true)) {
Jinsuk Kimd4a94db2014-09-12 13:51:10 +0900557 mService.maySendFeatureAbortCommand(message, Constants.ABORT_NOT_IN_CORRECT_MODE);
558 }
Jinsuk Kim119160a2014-07-07 18:48:10 +0900559 } else if (message.getDestination() != Constants.ADDR_BROADCAST &&
560 message.getSource() != Constants.ADDR_UNREGISTERED) {
561 Slog.v(TAG, "Wrong direct vendor command. Replying with <Feature Abort>");
Yuncheol Heo6aae6522014-08-05 14:48:37 +0900562 mService.maySendFeatureAbortCommand(message, Constants.ABORT_UNRECOGNIZED_OPCODE);
Jinsuk Kim119160a2014-07-07 18:48:10 +0900563 } else {
564 Slog.v(TAG, "Wrong broadcast vendor command. Ignoring");
565 }
566 return true;
567 }
568
Jinsuk Kimd4a94db2014-09-12 13:51:10 +0900569 protected void sendStandby(int deviceId) {
570 // Do nothing.
571 }
572
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900573 protected boolean handleSetOsdName(HdmiCecMessage message) {
574 // The default behavior of <Set Osd Name> is doing nothing.
575 return true;
576 }
577
Jungshik Jangb6591b82014-07-23 16:10:23 +0900578 protected boolean handleRecordTvScreen(HdmiCecMessage message) {
Yuncheol Heo25c20292014-07-31 17:59:39 +0900579 // The default behavior of <Record TV Screen> is replying <Feature Abort> with
580 // "Cannot provide source".
Yuncheol Heo6aae6522014-08-05 14:48:37 +0900581 mService.maySendFeatureAbortCommand(message, Constants.ABORT_CANNOT_PROVIDE_SOURCE);
Jungshik Jangb6591b82014-07-23 16:10:23 +0900582 return true;
583 }
584
Jungshik Jange5a93372014-07-25 13:41:14 +0900585 protected boolean handleTimerClearedStatus(HdmiCecMessage message) {
586 return false;
587 }
588
Jungshik Jang4480efa2014-09-04 17:08:34 +0900589 protected boolean handleReportPowerStatus(HdmiCecMessage message) {
590 return false;
591 }
592
593 protected boolean handleTimerStatus(HdmiCecMessage message) {
594 return false;
595 }
596
597 protected boolean handleRecordStatus(HdmiCecMessage message) {
598 return false;
599 }
600
Yuncheol Heo38db6292014-07-01 14:15:14 +0900601 @ServiceThreadOnly
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900602 final void handleAddressAllocated(int logicalAddress, int reason) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900603 assertRunOnServiceThread();
Jungshik Jang3ee65722014-06-03 16:22:30 +0900604 mAddress = mPreferredAddress = logicalAddress;
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900605 onAddressAllocated(logicalAddress, reason);
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900606 setPreferredAddress(logicalAddress);
Jungshik Jang1a4485d2014-05-26 11:02:36 +0900607 }
608
Yuncheol Heob5021862014-09-02 10:36:04 +0900609 int getType() {
610 return mDeviceType;
611 }
612
Jungshik Janga5b74142014-06-23 18:03:10 +0900613 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900614 HdmiDeviceInfo getDeviceInfo() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900615 assertRunOnServiceThread();
Jungshik Jang1a4485d2014-05-26 11:02:36 +0900616 return mDeviceInfo;
617 }
618
Jungshik Janga5b74142014-06-23 18:03:10 +0900619 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900620 void setDeviceInfo(HdmiDeviceInfo info) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900621 assertRunOnServiceThread();
Jungshik Jang1a4485d2014-05-26 11:02:36 +0900622 mDeviceInfo = info;
623 }
624
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900625 // Returns true if the logical address is same as the argument.
Jungshik Janga5b74142014-06-23 18:03:10 +0900626 @ServiceThreadOnly
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900627 boolean isAddressOf(int addr) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900628 assertRunOnServiceThread();
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900629 return addr == mAddress;
630 }
631
632 // Resets the logical address to unregistered(15), meaning the logical device is invalid.
Jungshik Janga5b74142014-06-23 18:03:10 +0900633 @ServiceThreadOnly
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900634 void clearAddress() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900635 assertRunOnServiceThread();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900636 mAddress = Constants.ADDR_UNREGISTERED;
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900637 }
638
Jungshik Janga5b74142014-06-23 18:03:10 +0900639 @ServiceThreadOnly
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900640 void addAndStartAction(final HdmiCecFeatureAction action) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900641 assertRunOnServiceThread();
Jinsuk Kim6f87b4e2014-10-10 14:40:29 +0900642 mActions.add(action);
Jinsuk Kima5445ce2015-03-30 17:14:58 +0900643 if (mService.isPowerStandby()) {
Jinsuk Kim6f87b4e2014-10-10 14:40:29 +0900644 Slog.i(TAG, "Not ready to start action. Queued for deferred start:" + action);
Yuncheol Heo38db6292014-07-01 14:15:14 +0900645 return;
646 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900647 action.start();
648 }
649
Jinsuk Kim6f87b4e2014-10-10 14:40:29 +0900650 @ServiceThreadOnly
651 void startQueuedActions() {
652 assertRunOnServiceThread();
653 for (HdmiCecFeatureAction action : mActions) {
654 if (!action.started()) {
655 Slog.i(TAG, "Starting queued action:" + action);
656 action.start();
657 }
658 }
659 }
660
Jungshik Jang79c58a42014-06-16 16:45:36 +0900661 // See if we have an action of a given type in progress.
Jungshik Janga5b74142014-06-23 18:03:10 +0900662 @ServiceThreadOnly
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900663 <T extends HdmiCecFeatureAction> boolean hasAction(final Class<T> clazz) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900664 assertRunOnServiceThread();
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900665 for (HdmiCecFeatureAction action : mActions) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900666 if (action.getClass().equals(clazz)) {
667 return true;
668 }
669 }
670 return false;
671 }
672
673 // Returns all actions matched with given class type.
Jungshik Janga5b74142014-06-23 18:03:10 +0900674 @ServiceThreadOnly
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900675 <T extends HdmiCecFeatureAction> List<T> getActions(final Class<T> clazz) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900676 assertRunOnServiceThread();
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900677 List<T> actions = Collections.<T>emptyList();
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900678 for (HdmiCecFeatureAction action : mActions) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900679 if (action.getClass().equals(clazz)) {
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900680 if (actions.isEmpty()) {
681 actions = new ArrayList<T>();
682 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900683 actions.add((T) action);
684 }
685 }
686 return actions;
687 }
688
689 /**
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900690 * Remove the given {@link HdmiCecFeatureAction} object from the action queue.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900691 *
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900692 * @param action {@link HdmiCecFeatureAction} to remove
Jungshik Jang79c58a42014-06-16 16:45:36 +0900693 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900694 @ServiceThreadOnly
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900695 void removeAction(final HdmiCecFeatureAction action) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900696 assertRunOnServiceThread();
Yuncheol Heoc516d652014-07-11 18:23:24 +0900697 action.finish(false);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900698 mActions.remove(action);
Yuncheol Heo38db6292014-07-01 14:15:14 +0900699 checkIfPendingActionsCleared();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900700 }
701
702 // Remove all actions matched with the given Class type.
Jungshik Janga5b74142014-06-23 18:03:10 +0900703 @ServiceThreadOnly
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900704 <T extends HdmiCecFeatureAction> void removeAction(final Class<T> clazz) {
Jungshik Janga5b74142014-06-23 18:03:10 +0900705 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900706 removeActionExcept(clazz, null);
707 }
708
709 // Remove all actions matched with the given Class type besides |exception|.
Jungshik Janga5b74142014-06-23 18:03:10 +0900710 @ServiceThreadOnly
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900711 <T extends HdmiCecFeatureAction> void removeActionExcept(final Class<T> clazz,
712 final HdmiCecFeatureAction exception) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900713 assertRunOnServiceThread();
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900714 Iterator<HdmiCecFeatureAction> iter = mActions.iterator();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900715 while (iter.hasNext()) {
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900716 HdmiCecFeatureAction action = iter.next();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900717 if (action != exception && action.getClass().equals(clazz)) {
Yuncheol Heoc516d652014-07-11 18:23:24 +0900718 action.finish(false);
719 iter.remove();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900720 }
721 }
Yuncheol Heo38db6292014-07-01 14:15:14 +0900722 checkIfPendingActionsCleared();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900723 }
724
Yuncheol Heo38db6292014-07-01 14:15:14 +0900725 protected void checkIfPendingActionsCleared() {
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900726 if (mActions.isEmpty() && mPendingActionClearedCallback != null) {
Yuncheol Heo26ba7fd2014-07-29 18:21:25 +0900727 PendingActionClearedCallback callback = mPendingActionClearedCallback;
728 // To prevent from calling the callback again during handling the callback itself.
729 mPendingActionClearedCallback = null;
730 callback.onCleared(this);
Yuncheol Heo38db6292014-07-01 14:15:14 +0900731 }
732 }
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900733
Jungshik Jang79c58a42014-06-16 16:45:36 +0900734 protected void assertRunOnServiceThread() {
735 if (Looper.myLooper() != mService.getServiceLooper()) {
736 throw new IllegalStateException("Should run on service thread.");
737 }
738 }
739
740 /**
741 * Called when a hot-plug event issued.
742 *
743 * @param portId id of port where a hot-plug event happened
744 * @param connected whether to connected or not on the event
745 */
746 void onHotplug(int portId, boolean connected) {
747 }
748
749 final HdmiControlService getService() {
750 return mService;
751 }
752
Jungshik Janga5b74142014-06-23 18:03:10 +0900753 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900754 final boolean isConnectedToArcPort(int path) {
Jungshik Janga5b74142014-06-23 18:03:10 +0900755 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900756 return mService.isConnectedToArcPort(path);
757 }
758
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900759 ActiveSource getActiveSource() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900760 synchronized (mLock) {
761 return mActiveSource;
762 }
763 }
764
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900765 void setActiveSource(ActiveSource newActive) {
766 setActiveSource(newActive.logicalAddress, newActive.physicalAddress);
767 }
768
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900769 void setActiveSource(HdmiDeviceInfo info) {
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900770 setActiveSource(info.getLogicalAddress(), info.getPhysicalAddress());
771 }
772
773 void setActiveSource(int logicalAddress, int physicalAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900774 synchronized (mLock) {
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900775 mActiveSource.logicalAddress = logicalAddress;
776 mActiveSource.physicalAddress = physicalAddress;
Jinsuk Kim83335712014-06-24 07:57:00 +0900777 }
Jinsuk Kime9f6ed32014-08-20 17:45:22 +0900778 mService.setLastInputForMhl(Constants.INVALID_PORT_ID);
Jinsuk Kim83335712014-06-24 07:57:00 +0900779 }
780
Jungshik Jang79c58a42014-06-16 16:45:36 +0900781 int getActivePath() {
782 synchronized (mLock) {
783 return mActiveRoutingPath;
784 }
785 }
786
Jinsuk Kim83335712014-06-24 07:57:00 +0900787 void setActivePath(int path) {
788 synchronized (mLock) {
789 mActiveRoutingPath = path;
790 }
Jungshik Jang867b4e02014-08-12 13:41:30 +0900791 mService.setActivePortId(pathToPortId(path));
Jinsuk Kim83335712014-06-24 07:57:00 +0900792 }
793
Jungshik Jang79c58a42014-06-16 16:45:36 +0900794 /**
Jinsuk Kima062a932014-06-18 10:00:39 +0900795 * Returns the ID of the active HDMI port. The active port is the one that has the active
796 * routing path connected to it directly or indirectly under the device hierarchy.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900797 */
Jinsuk Kima062a932014-06-18 10:00:39 +0900798 int getActivePortId() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900799 synchronized (mLock) {
800 return mService.pathToPortId(mActiveRoutingPath);
801 }
802 }
803
Jinsuk Kima062a932014-06-18 10:00:39 +0900804 /**
805 * Update the active port.
806 *
807 * @param portId the new active port id
808 */
809 void setActivePortId(int portId) {
Jungshik Jang867b4e02014-08-12 13:41:30 +0900810 // We update active routing path instead, since we get the active port id from
811 // the active routing path.
812 setActivePath(mService.portIdToPath(portId));
Jinsuk Kima062a932014-06-18 10:00:39 +0900813 }
814
Jungshik Janga5b74142014-06-23 18:03:10 +0900815 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900816 HdmiCecMessageCache getCecMessageCache() {
817 assertRunOnServiceThread();
818 return mCecMessageCache;
819 }
820
Jungshik Janga5b74142014-06-23 18:03:10 +0900821 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900822 int pathToPortId(int newPath) {
823 assertRunOnServiceThread();
824 return mService.pathToPortId(newPath);
825 }
Yuncheol Heo38db6292014-07-01 14:15:14 +0900826
827 /**
Yuncheol Heo38db6292014-07-01 14:15:14 +0900828 * Called when the system goes to standby mode.
829 *
830 * @param initiatedByCec true if this power sequence is initiated
Jungshik Jangb3e114a2014-07-11 17:50:05 +0900831 * by the reception the CEC messages like &lt;Standby&gt;
Yuncheol Heo38db6292014-07-01 14:15:14 +0900832 */
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900833 protected void onStandby(boolean initiatedByCec) {}
834
835 /**
836 * Disable device. {@code callback} is used to get notified when all pending
837 * actions are completed or timeout is issued.
838 *
839 * @param initiatedByCec true if this sequence is initiated
Jungshik Jangb3e114a2014-07-11 17:50:05 +0900840 * by the reception the CEC messages like &lt;Standby&gt;
Jinsuk Kim7e4b4802015-04-20 13:17:13 +0900841 * @param originalCallback callback interface to get notified when all pending actions are
Jungshik Jangb3e114a2014-07-11 17:50:05 +0900842 * cleared
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900843 */
Jungshik Jangb3e114a2014-07-11 17:50:05 +0900844 protected void disableDevice(boolean initiatedByCec,
Jinsuk Kim7e4b4802015-04-20 13:17:13 +0900845 final PendingActionClearedCallback originalCallback) {
Jungshik Jangb3e114a2014-07-11 17:50:05 +0900846 mPendingActionClearedCallback = new PendingActionClearedCallback() {
847 @Override
848 public void onCleared(HdmiCecLocalDevice device) {
849 mHandler.removeMessages(MSG_DISABLE_DEVICE_TIMEOUT);
Jinsuk Kim7e4b4802015-04-20 13:17:13 +0900850 originalCallback.onCleared(device);
Jungshik Jangb3e114a2014-07-11 17:50:05 +0900851 }
852 };
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900853 mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_DISABLE_DEVICE_TIMEOUT),
854 DEVICE_CLEANUP_TIMEOUT);
855 }
856
857 @ServiceThreadOnly
858 private void handleDisableDeviceTimeout() {
859 assertRunOnServiceThread();
860
861 // If all actions are not cleared in DEVICE_CLEANUP_TIMEOUT, enforce to finish them.
862 // onCleard will be called at the last action's finish method.
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900863 Iterator<HdmiCecFeatureAction> iter = mActions.iterator();
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900864 while (iter.hasNext()) {
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900865 HdmiCecFeatureAction action = iter.next();
Yuncheol Heoc516d652014-07-11 18:23:24 +0900866 action.finish(false);
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900867 iter.remove();
868 }
Jinsuk Kim7e4b4802015-04-20 13:17:13 +0900869 if (mPendingActionClearedCallback != null) {
870 mPendingActionClearedCallback.onCleared(this);
871 }
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900872 }
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900873
874 /**
875 * Send a key event to other device.
876 *
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900877 * @param keyCode key code defined in {@link android.view.KeyEvent}
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900878 * @param isPressed {@code true} for key down event
879 */
880 protected void sendKeyEvent(int keyCode, boolean isPressed) {
881 Slog.w(TAG, "sendKeyEvent not implemented");
882 }
Terry Heo959d2db2014-08-28 16:45:41 +0900883
Jungshik Jang2e8f1b62014-09-03 08:28:02 +0900884 void sendUserControlPressedAndReleased(int targetAddress, int cecKeycode) {
885 mService.sendCecCommand(HdmiCecMessageBuilder.buildUserControlPressed(
886 mAddress, targetAddress, cecKeycode));
887 mService.sendCecCommand(HdmiCecMessageBuilder.buildUserControlReleased(
888 mAddress, targetAddress));
889 }
890
Terry Heo959d2db2014-08-28 16:45:41 +0900891 /**
892 * Dump internal status of HdmiCecLocalDevice object.
893 */
894 protected void dump(final IndentingPrintWriter pw) {
895 pw.println("mDeviceType: " + mDeviceType);
896 pw.println("mAddress: " + mAddress);
897 pw.println("mPreferredAddress: " + mPreferredAddress);
898 pw.println("mDeviceInfo: " + mDeviceInfo);
899 pw.println("mActiveSource: " + mActiveSource);
900 pw.println(String.format("mActiveRoutingPath: 0x%04x", mActiveRoutingPath));
901 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900902}