blob: b24bc65dbb9358159f81ac05275938b9a6464d37 [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();
Jungshik Jang3ee65722014-06-03 16:22:30 +0900179 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900180
Jungshik Jang8b308d92014-05-29 21:52:28 +0900181 /**
Jungshik Jang3ee65722014-06-03 16:22:30 +0900182 * Called once a logical address of the local device is allocated.
Jungshik Jang8b308d92014-05-29 21:52:28 +0900183 */
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900184 protected abstract void onAddressAllocated(int logicalAddress, int reason);
Jungshik Jang8b308d92014-05-29 21:52:28 +0900185
Jungshik Jang092b4452014-06-11 15:19:17 +0900186 /**
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900187 * Get the preferred logical address from system properties.
188 */
189 protected abstract int getPreferredAddress();
190
191 /**
192 * Set the preferred logical address to system properties.
193 */
194 protected abstract void setPreferredAddress(int addr);
195
196 /**
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900197 * Returns true if the TV input associated with the CEC device is ready
198 * to accept further processing such as input switching. This is used
199 * to buffer certain CEC commands and process it later if the input is not
200 * ready yet. For other types of local devices(non-TV), this method returns
201 * true by default to let the commands be processed right away.
202 */
203 protected boolean isInputReady(int deviceId) {
204 return true;
205 }
206
207 /**
Jinsuk Kime26d8332015-01-09 08:55:41 +0900208 * Returns true if the local device allows the system to be put to standby.
209 * The default implementation returns true.
210 */
211 protected boolean canGoToStandby() {
212 return true;
213 }
214
215 /**
Jungshik Jang092b4452014-06-11 15:19:17 +0900216 * Dispatch incoming message.
217 *
218 * @param message incoming message
219 * @return true if consumed a message; otherwise, return false.
220 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900221 @ServiceThreadOnly
Yuncheol Heo25c20292014-07-31 17:59:39 +0900222 boolean dispatchMessage(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900223 assertRunOnServiceThread();
Jungshik Jang092b4452014-06-11 15:19:17 +0900224 int dest = message.getDestination();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900225 if (dest != mAddress && dest != Constants.ADDR_BROADCAST) {
Jungshik Jang092b4452014-06-11 15:19:17 +0900226 return false;
227 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900228 // Cache incoming message. Note that it caches only white-listed one.
229 mCecMessageCache.cacheMessage(message);
Jungshik Jang092b4452014-06-11 15:19:17 +0900230 return onMessage(message);
231 }
232
Jungshik Janga5b74142014-06-23 18:03:10 +0900233 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900234 protected final boolean onMessage(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900235 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900236 if (dispatchMessageToAction(message)) {
237 return true;
238 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900239 switch (message.getOpcode()) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900240 case Constants.MESSAGE_ACTIVE_SOURCE:
Jinsuk Kim83335712014-06-24 07:57:00 +0900241 return handleActiveSource(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900242 case Constants.MESSAGE_INACTIVE_SOURCE:
Jinsuk Kim83335712014-06-24 07:57:00 +0900243 return handleInactiveSource(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900244 case Constants.MESSAGE_REQUEST_ACTIVE_SOURCE:
Jinsuk Kim83335712014-06-24 07:57:00 +0900245 return handleRequestActiveSource(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900246 case Constants.MESSAGE_GET_MENU_LANGUAGE:
Jungshik Jang092b4452014-06-11 15:19:17 +0900247 return handleGetMenuLanguage(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900248 case Constants.MESSAGE_GIVE_PHYSICAL_ADDRESS:
Jungshik Jang092b4452014-06-11 15:19:17 +0900249 return handleGivePhysicalAddress();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900250 case Constants.MESSAGE_GIVE_OSD_NAME:
Jungshik Jang092b4452014-06-11 15:19:17 +0900251 return handleGiveOsdName(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900252 case Constants.MESSAGE_GIVE_DEVICE_VENDOR_ID:
Jungshik Jang092b4452014-06-11 15:19:17 +0900253 return handleGiveDeviceVendorId();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900254 case Constants.MESSAGE_GET_CEC_VERSION:
Jungshik Jang092b4452014-06-11 15:19:17 +0900255 return handleGetCecVersion(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900256 case Constants.MESSAGE_REPORT_PHYSICAL_ADDRESS:
Jungshik Jang60cffce2014-06-12 18:03:04 +0900257 return handleReportPhysicalAddress(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900258 case Constants.MESSAGE_ROUTING_CHANGE:
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900259 return handleRoutingChange(message);
Yuncheol Heo64bafd92014-08-11 11:17:54 +0900260 case Constants.MESSAGE_ROUTING_INFORMATION:
261 return handleRoutingInformation(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900262 case Constants.MESSAGE_INITIATE_ARC:
Jungshik Jang79c58a42014-06-16 16:45:36 +0900263 return handleInitiateArc(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900264 case Constants.MESSAGE_TERMINATE_ARC:
Jungshik Jang79c58a42014-06-16 16:45:36 +0900265 return handleTerminateArc(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900266 case Constants.MESSAGE_SET_SYSTEM_AUDIO_MODE:
Jungshik Jang79c58a42014-06-16 16:45:36 +0900267 return handleSetSystemAudioMode(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900268 case Constants.MESSAGE_SYSTEM_AUDIO_MODE_STATUS:
Jungshik Jang79c58a42014-06-16 16:45:36 +0900269 return handleSystemAudioModeStatus(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900270 case Constants.MESSAGE_REPORT_AUDIO_STATUS:
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900271 return handleReportAudioStatus(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900272 case Constants.MESSAGE_STANDBY:
Yuncheol Heo38db6292014-07-01 14:15:14 +0900273 return handleStandby(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900274 case Constants.MESSAGE_TEXT_VIEW_ON:
Yuncheol Heo38db6292014-07-01 14:15:14 +0900275 return handleTextViewOn(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900276 case Constants.MESSAGE_IMAGE_VIEW_ON:
Yuncheol Heo38db6292014-07-01 14:15:14 +0900277 return handleImageViewOn(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900278 case Constants.MESSAGE_USER_CONTROL_PRESSED:
Yuncheol Heo38db6292014-07-01 14:15:14 +0900279 return handleUserControlPressed(message);
Terry Heo3e1564e2014-08-12 14:41:00 +0900280 case Constants.MESSAGE_USER_CONTROL_RELEASED:
281 return handleUserControlReleased();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900282 case Constants.MESSAGE_SET_STREAM_PATH:
Yuncheol Heo38db6292014-07-01 14:15:14 +0900283 return handleSetStreamPath(message);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900284 case Constants.MESSAGE_GIVE_DEVICE_POWER_STATUS:
Yuncheol Heo38db6292014-07-01 14:15:14 +0900285 return handleGiveDevicePowerStatus(message);
Terry Heo3e1564e2014-08-12 14:41:00 +0900286 case Constants.MESSAGE_MENU_REQUEST:
Yuncheol Heo184b1242014-09-12 15:09:07 +0900287 return handleMenuRequest(message);
288 case Constants.MESSAGE_MENU_STATUS:
289 return handleMenuStatus(message);
Jinsuk Kim119160a2014-07-07 18:48:10 +0900290 case Constants.MESSAGE_VENDOR_COMMAND:
291 return handleVendorCommand(message);
292 case Constants.MESSAGE_VENDOR_COMMAND_WITH_ID:
293 return handleVendorCommandWithId(message);
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900294 case Constants.MESSAGE_SET_OSD_NAME:
295 return handleSetOsdName(message);
Jungshik Jangb6591b82014-07-23 16:10:23 +0900296 case Constants.MESSAGE_RECORD_TV_SCREEN:
297 return handleRecordTvScreen(message);
Jungshik Jange5a93372014-07-25 13:41:14 +0900298 case Constants.MESSAGE_TIMER_CLEARED_STATUS:
299 return handleTimerClearedStatus(message);
Jungshik Jang4480efa2014-09-04 17:08:34 +0900300 case Constants.MESSAGE_REPORT_POWER_STATUS:
301 return handleReportPowerStatus(message);
302 case Constants.MESSAGE_TIMER_STATUS:
303 return handleTimerStatus(message);
304 case Constants.MESSAGE_RECORD_STATUS:
305 return handleRecordStatus(message);
Jungshik Jang092b4452014-06-11 15:19:17 +0900306 default:
307 return false;
308 }
309 }
310
Jungshik Janga5b74142014-06-23 18:03:10 +0900311 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900312 private boolean dispatchMessageToAction(HdmiCecMessage message) {
Jungshik Janga5b74142014-06-23 18:03:10 +0900313 assertRunOnServiceThread();
Jungshik Jang5352081c2014-09-22 15:14:49 +0900314 boolean processed = false;
315 // Use copied action list in that processCommand may remove itself.
316 for (HdmiCecFeatureAction action : new ArrayList<>(mActions)) {
317 // Iterates all actions to check whether incoming message is consumed.
318 boolean result = action.processCommand(message);
319 processed = processed || result;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900320 }
Jungshik Jang5352081c2014-09-22 15:14:49 +0900321 return processed;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900322 }
323
Jungshik Janga5b74142014-06-23 18:03:10 +0900324 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900325 protected boolean handleGivePhysicalAddress() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900326 assertRunOnServiceThread();
327
Jungshik Jang092b4452014-06-11 15:19:17 +0900328 int physicalAddress = mService.getPhysicalAddress();
329 HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildReportPhysicalAddressCommand(
330 mAddress, physicalAddress, mDeviceType);
331 mService.sendCecCommand(cecMessage);
332 return true;
333 }
334
Jungshik Janga5b74142014-06-23 18:03:10 +0900335 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900336 protected boolean handleGiveDeviceVendorId() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900337 assertRunOnServiceThread();
Jungshik Jang092b4452014-06-11 15:19:17 +0900338 int vendorId = mService.getVendorId();
339 HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildDeviceVendorIdCommand(
340 mAddress, vendorId);
341 mService.sendCecCommand(cecMessage);
342 return true;
343 }
344
Jungshik Janga5b74142014-06-23 18:03:10 +0900345 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900346 protected boolean handleGetCecVersion(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900347 assertRunOnServiceThread();
Jungshik Jang092b4452014-06-11 15:19:17 +0900348 int version = mService.getCecVersion();
349 HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildCecVersion(message.getDestination(),
350 message.getSource(), version);
351 mService.sendCecCommand(cecMessage);
352 return true;
353 }
354
Jungshik Janga5b74142014-06-23 18:03:10 +0900355 @ServiceThreadOnly
Jinsuk Kim83335712014-06-24 07:57:00 +0900356 protected boolean handleActiveSource(HdmiCecMessage message) {
357 return false;
358 }
359
360 @ServiceThreadOnly
361 protected boolean handleInactiveSource(HdmiCecMessage message) {
362 return false;
363 }
364
365 @ServiceThreadOnly
366 protected boolean handleRequestActiveSource(HdmiCecMessage message) {
367 return false;
368 }
369
370 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900371 protected boolean handleGetMenuLanguage(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900372 assertRunOnServiceThread();
Jungshik Jang092b4452014-06-11 15:19:17 +0900373 Slog.w(TAG, "Only TV can handle <Get Menu Language>:" + message.toString());
Yuncheol Heo6aae6522014-08-05 14:48:37 +0900374 // 'return false' will cause to reply with <Feature Abort>.
375 return false;
Jungshik Jang092b4452014-06-11 15:19:17 +0900376 }
377
Jungshik Janga5b74142014-06-23 18:03:10 +0900378 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900379 protected boolean handleGiveOsdName(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900380 assertRunOnServiceThread();
Jungshik Jang092b4452014-06-11 15:19:17 +0900381 // Note that since this method is called after logical address allocation is done,
382 // mDeviceInfo should not be null.
383 HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildSetOsdNameCommand(
384 mAddress, message.getSource(), mDeviceInfo.getDisplayName());
385 if (cecMessage != null) {
386 mService.sendCecCommand(cecMessage);
387 } else {
388 Slog.w(TAG, "Failed to build <Get Osd Name>:" + mDeviceInfo.getDisplayName());
389 }
390 return true;
391 }
392
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900393 protected boolean handleRoutingChange(HdmiCecMessage message) {
394 return false;
395 }
396
Yuncheol Heo64bafd92014-08-11 11:17:54 +0900397 protected boolean handleRoutingInformation(HdmiCecMessage message) {
398 return false;
399 }
400
Jungshik Jang60cffce2014-06-12 18:03:04 +0900401 protected boolean handleReportPhysicalAddress(HdmiCecMessage message) {
402 return false;
403 }
404
Jungshik Jang79c58a42014-06-16 16:45:36 +0900405 protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) {
406 return false;
407 }
408
409 protected boolean handleSetSystemAudioMode(HdmiCecMessage message) {
410 return false;
411 }
412
413 protected boolean handleTerminateArc(HdmiCecMessage message) {
414 return false;
415 }
416
417 protected boolean handleInitiateArc(HdmiCecMessage message) {
418 return false;
419 }
420
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900421 protected boolean handleReportAudioStatus(HdmiCecMessage message) {
422 return false;
423 }
424
Jungshik Janga5b74142014-06-23 18:03:10 +0900425 @ServiceThreadOnly
Yuncheol Heo38db6292014-07-01 14:15:14 +0900426 protected boolean handleStandby(HdmiCecMessage message) {
427 assertRunOnServiceThread();
428 // Seq #12
Jinsuk Kim4d43d932014-07-03 16:43:58 +0900429 if (mService.isControlEnabled() && !mService.isProhibitMode()
Yuncheol Heo38db6292014-07-01 14:15:14 +0900430 && mService.isPowerOnOrTransient()) {
431 mService.standby();
432 return true;
433 }
434 return false;
435 }
436
437 @ServiceThreadOnly
438 protected boolean handleUserControlPressed(HdmiCecMessage message) {
439 assertRunOnServiceThread();
Terry Heo3e1564e2014-08-12 14:41:00 +0900440 mHandler.removeMessages(MSG_USER_CONTROL_RELEASE_TIMEOUT);
Yuncheol Heo38db6292014-07-01 14:15:14 +0900441 if (mService.isPowerOnOrTransient() && isPowerOffOrToggleCommand(message)) {
442 mService.standby();
443 return true;
444 } else if (mService.isPowerStandbyOrTransient() && isPowerOnOrToggleCommand(message)) {
445 mService.wakeUp();
446 return true;
447 }
Terry Heo3e1564e2014-08-12 14:41:00 +0900448
449 final long downTime = SystemClock.uptimeMillis();
450 final byte[] params = message.getParams();
Jungshik Jang73483b6b2014-09-26 14:00:59 +0900451 final int keycode = HdmiCecKeycode.cecKeycodeAndParamsToAndroidKey(params);
Terry Heo3e1564e2014-08-12 14:41:00 +0900452 int keyRepeatCount = 0;
453 if (mLastKeycode != HdmiCecKeycode.UNSUPPORTED_KEYCODE) {
454 if (keycode == mLastKeycode) {
455 keyRepeatCount = mLastKeyRepeatCount + 1;
456 } else {
457 injectKeyEvent(downTime, KeyEvent.ACTION_UP, mLastKeycode, 0);
458 }
459 }
460 mLastKeycode = keycode;
461 mLastKeyRepeatCount = keyRepeatCount;
462
463 if (keycode != HdmiCecKeycode.UNSUPPORTED_KEYCODE) {
464 injectKeyEvent(downTime, KeyEvent.ACTION_DOWN, keycode, keyRepeatCount);
465 mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_USER_CONTROL_RELEASE_TIMEOUT),
466 FOLLOWER_SAFETY_TIMEOUT);
467 return true;
468 }
Yuncheol Heo38db6292014-07-01 14:15:14 +0900469 return false;
470 }
471
Terry Heo3e1564e2014-08-12 14:41:00 +0900472 @ServiceThreadOnly
473 protected boolean handleUserControlReleased() {
474 assertRunOnServiceThread();
475 mHandler.removeMessages(MSG_USER_CONTROL_RELEASE_TIMEOUT);
476 mLastKeyRepeatCount = 0;
477 if (mLastKeycode != HdmiCecKeycode.UNSUPPORTED_KEYCODE) {
478 final long upTime = SystemClock.uptimeMillis();
479 injectKeyEvent(upTime, KeyEvent.ACTION_UP, mLastKeycode, 0);
480 mLastKeycode = HdmiCecKeycode.UNSUPPORTED_KEYCODE;
481 return true;
482 }
483 return false;
484 }
485
486 static void injectKeyEvent(long time, int action, int keycode, int repeat) {
487 KeyEvent keyEvent = KeyEvent.obtain(time, time, action, keycode,
488 repeat, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FROM_SYSTEM,
489 InputDevice.SOURCE_HDMI, null);
490 InputManager.getInstance().injectInputEvent(keyEvent,
491 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
492 keyEvent.recycle();
493 }
494
Yuncheol Heo25c20292014-07-31 17:59:39 +0900495 static boolean isPowerOnOrToggleCommand(HdmiCecMessage message) {
Yuncheol Heo38db6292014-07-01 14:15:14 +0900496 byte[] params = message.getParams();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900497 return message.getOpcode() == Constants.MESSAGE_USER_CONTROL_PRESSED
Jungshik Jang210d73d2014-07-04 11:11:29 +0900498 && (params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER
499 || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_ON_FUNCTION
500 || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_TOGGLE_FUNCTION);
Yuncheol Heo38db6292014-07-01 14:15:14 +0900501 }
502
Yuncheol Heo25c20292014-07-31 17:59:39 +0900503 static boolean isPowerOffOrToggleCommand(HdmiCecMessage message) {
Yuncheol Heo38db6292014-07-01 14:15:14 +0900504 byte[] params = message.getParams();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900505 return message.getOpcode() == Constants.MESSAGE_USER_CONTROL_PRESSED
Jungshik Jang210d73d2014-07-04 11:11:29 +0900506 && (params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER
507 || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_OFF_FUNCTION
508 || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_TOGGLE_FUNCTION);
Yuncheol Heo38db6292014-07-01 14:15:14 +0900509 }
510
511 protected boolean handleTextViewOn(HdmiCecMessage message) {
512 return false;
513 }
514
515 protected boolean handleImageViewOn(HdmiCecMessage message) {
516 return false;
517 }
518
519 protected boolean handleSetStreamPath(HdmiCecMessage message) {
520 return false;
521 }
522
523 protected boolean handleGiveDevicePowerStatus(HdmiCecMessage message) {
524 mService.sendCecCommand(HdmiCecMessageBuilder.buildReportPowerStatus(
525 mAddress, message.getSource(), mService.getPowerStatus()));
526 return true;
527 }
528
Yuncheol Heo184b1242014-09-12 15:09:07 +0900529 protected boolean handleMenuRequest(HdmiCecMessage message) {
Terry Heo3e1564e2014-08-12 14:41:00 +0900530 // Always report menu active to receive Remote Control.
531 mService.sendCecCommand(HdmiCecMessageBuilder.buildReportMenuStatus(
532 mAddress, message.getSource(), Constants.MENU_STATE_ACTIVATED));
533 return true;
534 }
535
Yuncheol Heo184b1242014-09-12 15:09:07 +0900536 protected boolean handleMenuStatus(HdmiCecMessage message) {
537 return false;
538 }
539
Jinsuk Kim119160a2014-07-07 18:48:10 +0900540 protected boolean handleVendorCommand(HdmiCecMessage message) {
Yuncheol Heo0608b932014-10-13 16:39:18 +0900541 if (!mService.invokeVendorCommandListenersOnReceived(mDeviceType, message.getSource(),
542 message.getDestination(), message.getParams(), false)) {
Jinsuk Kimd4a94db2014-09-12 13:51:10 +0900543 // Vendor command listener may not have been registered yet. Respond with
544 // <Feature Abort> [NOT_IN_CORRECT_MODE] so that the sender can try again later.
545 mService.maySendFeatureAbortCommand(message, Constants.ABORT_NOT_IN_CORRECT_MODE);
546 }
Jinsuk Kim119160a2014-07-07 18:48:10 +0900547 return true;
548 }
549
550 protected boolean handleVendorCommandWithId(HdmiCecMessage message) {
551 byte[] params = message.getParams();
552 int vendorId = HdmiUtils.threeBytesToInt(params);
553 if (vendorId == mService.getVendorId()) {
Yuncheol Heo0608b932014-10-13 16:39:18 +0900554 if (!mService.invokeVendorCommandListenersOnReceived(mDeviceType, message.getSource(),
555 message.getDestination(), params, true)) {
Jinsuk Kimd4a94db2014-09-12 13:51:10 +0900556 mService.maySendFeatureAbortCommand(message, Constants.ABORT_NOT_IN_CORRECT_MODE);
557 }
Jinsuk Kim119160a2014-07-07 18:48:10 +0900558 } else if (message.getDestination() != Constants.ADDR_BROADCAST &&
559 message.getSource() != Constants.ADDR_UNREGISTERED) {
560 Slog.v(TAG, "Wrong direct vendor command. Replying with <Feature Abort>");
Yuncheol Heo6aae6522014-08-05 14:48:37 +0900561 mService.maySendFeatureAbortCommand(message, Constants.ABORT_UNRECOGNIZED_OPCODE);
Jinsuk Kim119160a2014-07-07 18:48:10 +0900562 } else {
563 Slog.v(TAG, "Wrong broadcast vendor command. Ignoring");
564 }
565 return true;
566 }
567
Jinsuk Kimd4a94db2014-09-12 13:51:10 +0900568 protected void sendStandby(int deviceId) {
569 // Do nothing.
570 }
571
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900572 protected boolean handleSetOsdName(HdmiCecMessage message) {
573 // The default behavior of <Set Osd Name> is doing nothing.
574 return true;
575 }
576
Jungshik Jangb6591b82014-07-23 16:10:23 +0900577 protected boolean handleRecordTvScreen(HdmiCecMessage message) {
Yuncheol Heo25c20292014-07-31 17:59:39 +0900578 // The default behavior of <Record TV Screen> is replying <Feature Abort> with
579 // "Cannot provide source".
Yuncheol Heo6aae6522014-08-05 14:48:37 +0900580 mService.maySendFeatureAbortCommand(message, Constants.ABORT_CANNOT_PROVIDE_SOURCE);
Jungshik Jangb6591b82014-07-23 16:10:23 +0900581 return true;
582 }
583
Jungshik Jange5a93372014-07-25 13:41:14 +0900584 protected boolean handleTimerClearedStatus(HdmiCecMessage message) {
585 return false;
586 }
587
Jungshik Jang4480efa2014-09-04 17:08:34 +0900588 protected boolean handleReportPowerStatus(HdmiCecMessage message) {
589 return false;
590 }
591
592 protected boolean handleTimerStatus(HdmiCecMessage message) {
593 return false;
594 }
595
596 protected boolean handleRecordStatus(HdmiCecMessage message) {
597 return false;
598 }
599
Yuncheol Heo38db6292014-07-01 14:15:14 +0900600 @ServiceThreadOnly
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900601 final void handleAddressAllocated(int logicalAddress, int reason) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900602 assertRunOnServiceThread();
Jungshik Jang3ee65722014-06-03 16:22:30 +0900603 mAddress = mPreferredAddress = logicalAddress;
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900604 onAddressAllocated(logicalAddress, reason);
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900605 setPreferredAddress(logicalAddress);
Jungshik Jang1a4485d2014-05-26 11:02:36 +0900606 }
607
Yuncheol Heob5021862014-09-02 10:36:04 +0900608 int getType() {
609 return mDeviceType;
610 }
611
Jungshik Janga5b74142014-06-23 18:03:10 +0900612 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900613 HdmiDeviceInfo getDeviceInfo() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900614 assertRunOnServiceThread();
Jungshik Jang1a4485d2014-05-26 11:02:36 +0900615 return mDeviceInfo;
616 }
617
Jungshik Janga5b74142014-06-23 18:03:10 +0900618 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900619 void setDeviceInfo(HdmiDeviceInfo info) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900620 assertRunOnServiceThread();
Jungshik Jang1a4485d2014-05-26 11:02:36 +0900621 mDeviceInfo = info;
622 }
623
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900624 // Returns true if the logical address is same as the argument.
Jungshik Janga5b74142014-06-23 18:03:10 +0900625 @ServiceThreadOnly
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900626 boolean isAddressOf(int addr) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900627 assertRunOnServiceThread();
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900628 return addr == mAddress;
629 }
630
631 // Resets the logical address to unregistered(15), meaning the logical device is invalid.
Jungshik Janga5b74142014-06-23 18:03:10 +0900632 @ServiceThreadOnly
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900633 void clearAddress() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900634 assertRunOnServiceThread();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900635 mAddress = Constants.ADDR_UNREGISTERED;
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900636 }
637
Jungshik Janga5b74142014-06-23 18:03:10 +0900638 @ServiceThreadOnly
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900639 void addAndStartAction(final HdmiCecFeatureAction action) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900640 assertRunOnServiceThread();
Jinsuk Kim6f87b4e2014-10-10 14:40:29 +0900641 mActions.add(action);
Jinsuk Kima5445ce2015-03-30 17:14:58 +0900642 if (mService.isPowerStandby()) {
Jinsuk Kim6f87b4e2014-10-10 14:40:29 +0900643 Slog.i(TAG, "Not ready to start action. Queued for deferred start:" + action);
Yuncheol Heo38db6292014-07-01 14:15:14 +0900644 return;
645 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900646 action.start();
647 }
648
Jinsuk Kim6f87b4e2014-10-10 14:40:29 +0900649 @ServiceThreadOnly
650 void startQueuedActions() {
651 assertRunOnServiceThread();
652 for (HdmiCecFeatureAction action : mActions) {
653 if (!action.started()) {
654 Slog.i(TAG, "Starting queued action:" + action);
655 action.start();
656 }
657 }
658 }
659
Jungshik Jang79c58a42014-06-16 16:45:36 +0900660 // See if we have an action of a given type in progress.
Jungshik Janga5b74142014-06-23 18:03:10 +0900661 @ServiceThreadOnly
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900662 <T extends HdmiCecFeatureAction> boolean hasAction(final Class<T> clazz) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900663 assertRunOnServiceThread();
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900664 for (HdmiCecFeatureAction action : mActions) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900665 if (action.getClass().equals(clazz)) {
666 return true;
667 }
668 }
669 return false;
670 }
671
672 // Returns all actions matched with given class type.
Jungshik Janga5b74142014-06-23 18:03:10 +0900673 @ServiceThreadOnly
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900674 <T extends HdmiCecFeatureAction> List<T> getActions(final Class<T> clazz) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900675 assertRunOnServiceThread();
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900676 List<T> actions = Collections.<T>emptyList();
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900677 for (HdmiCecFeatureAction action : mActions) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900678 if (action.getClass().equals(clazz)) {
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900679 if (actions.isEmpty()) {
680 actions = new ArrayList<T>();
681 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900682 actions.add((T) action);
683 }
684 }
685 return actions;
686 }
687
688 /**
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900689 * Remove the given {@link HdmiCecFeatureAction} object from the action queue.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900690 *
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900691 * @param action {@link HdmiCecFeatureAction} to remove
Jungshik Jang79c58a42014-06-16 16:45:36 +0900692 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900693 @ServiceThreadOnly
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900694 void removeAction(final HdmiCecFeatureAction action) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900695 assertRunOnServiceThread();
Yuncheol Heoc516d652014-07-11 18:23:24 +0900696 action.finish(false);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900697 mActions.remove(action);
Yuncheol Heo38db6292014-07-01 14:15:14 +0900698 checkIfPendingActionsCleared();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900699 }
700
701 // Remove all actions matched with the given Class type.
Jungshik Janga5b74142014-06-23 18:03:10 +0900702 @ServiceThreadOnly
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900703 <T extends HdmiCecFeatureAction> void removeAction(final Class<T> clazz) {
Jungshik Janga5b74142014-06-23 18:03:10 +0900704 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900705 removeActionExcept(clazz, null);
706 }
707
708 // Remove all actions matched with the given Class type besides |exception|.
Jungshik Janga5b74142014-06-23 18:03:10 +0900709 @ServiceThreadOnly
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900710 <T extends HdmiCecFeatureAction> void removeActionExcept(final Class<T> clazz,
711 final HdmiCecFeatureAction exception) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900712 assertRunOnServiceThread();
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900713 Iterator<HdmiCecFeatureAction> iter = mActions.iterator();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900714 while (iter.hasNext()) {
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900715 HdmiCecFeatureAction action = iter.next();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900716 if (action != exception && action.getClass().equals(clazz)) {
Yuncheol Heoc516d652014-07-11 18:23:24 +0900717 action.finish(false);
718 iter.remove();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900719 }
720 }
Yuncheol Heo38db6292014-07-01 14:15:14 +0900721 checkIfPendingActionsCleared();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900722 }
723
Yuncheol Heo38db6292014-07-01 14:15:14 +0900724 protected void checkIfPendingActionsCleared() {
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900725 if (mActions.isEmpty() && mPendingActionClearedCallback != null) {
Yuncheol Heo26ba7fd2014-07-29 18:21:25 +0900726 PendingActionClearedCallback callback = mPendingActionClearedCallback;
727 // To prevent from calling the callback again during handling the callback itself.
728 mPendingActionClearedCallback = null;
729 callback.onCleared(this);
Yuncheol Heo38db6292014-07-01 14:15:14 +0900730 }
731 }
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900732
Jungshik Jang79c58a42014-06-16 16:45:36 +0900733 protected void assertRunOnServiceThread() {
734 if (Looper.myLooper() != mService.getServiceLooper()) {
735 throw new IllegalStateException("Should run on service thread.");
736 }
737 }
738
739 /**
740 * Called when a hot-plug event issued.
741 *
742 * @param portId id of port where a hot-plug event happened
743 * @param connected whether to connected or not on the event
744 */
745 void onHotplug(int portId, boolean connected) {
746 }
747
748 final HdmiControlService getService() {
749 return mService;
750 }
751
Jungshik Janga5b74142014-06-23 18:03:10 +0900752 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900753 final boolean isConnectedToArcPort(int path) {
Jungshik Janga5b74142014-06-23 18:03:10 +0900754 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900755 return mService.isConnectedToArcPort(path);
756 }
757
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900758 ActiveSource getActiveSource() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900759 synchronized (mLock) {
760 return mActiveSource;
761 }
762 }
763
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900764 void setActiveSource(ActiveSource newActive) {
765 setActiveSource(newActive.logicalAddress, newActive.physicalAddress);
766 }
767
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900768 void setActiveSource(HdmiDeviceInfo info) {
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900769 setActiveSource(info.getLogicalAddress(), info.getPhysicalAddress());
770 }
771
772 void setActiveSource(int logicalAddress, int physicalAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900773 synchronized (mLock) {
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900774 mActiveSource.logicalAddress = logicalAddress;
775 mActiveSource.physicalAddress = physicalAddress;
Jinsuk Kim83335712014-06-24 07:57:00 +0900776 }
Jinsuk Kime9f6ed32014-08-20 17:45:22 +0900777 mService.setLastInputForMhl(Constants.INVALID_PORT_ID);
Jinsuk Kim83335712014-06-24 07:57:00 +0900778 }
779
Jungshik Jang79c58a42014-06-16 16:45:36 +0900780 int getActivePath() {
781 synchronized (mLock) {
782 return mActiveRoutingPath;
783 }
784 }
785
Jinsuk Kim83335712014-06-24 07:57:00 +0900786 void setActivePath(int path) {
787 synchronized (mLock) {
788 mActiveRoutingPath = path;
789 }
Jungshik Jang867b4e02014-08-12 13:41:30 +0900790 mService.setActivePortId(pathToPortId(path));
Jinsuk Kim83335712014-06-24 07:57:00 +0900791 }
792
Jungshik Jang79c58a42014-06-16 16:45:36 +0900793 /**
Jinsuk Kima062a932014-06-18 10:00:39 +0900794 * Returns the ID of the active HDMI port. The active port is the one that has the active
795 * routing path connected to it directly or indirectly under the device hierarchy.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900796 */
Jinsuk Kima062a932014-06-18 10:00:39 +0900797 int getActivePortId() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900798 synchronized (mLock) {
799 return mService.pathToPortId(mActiveRoutingPath);
800 }
801 }
802
Jinsuk Kima062a932014-06-18 10:00:39 +0900803 /**
804 * Update the active port.
805 *
806 * @param portId the new active port id
807 */
808 void setActivePortId(int portId) {
Jungshik Jang867b4e02014-08-12 13:41:30 +0900809 // We update active routing path instead, since we get the active port id from
810 // the active routing path.
811 setActivePath(mService.portIdToPath(portId));
Jinsuk Kima062a932014-06-18 10:00:39 +0900812 }
813
Jungshik Janga5b74142014-06-23 18:03:10 +0900814 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900815 HdmiCecMessageCache getCecMessageCache() {
816 assertRunOnServiceThread();
817 return mCecMessageCache;
818 }
819
Jungshik Janga5b74142014-06-23 18:03:10 +0900820 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900821 int pathToPortId(int newPath) {
822 assertRunOnServiceThread();
823 return mService.pathToPortId(newPath);
824 }
Yuncheol Heo38db6292014-07-01 14:15:14 +0900825
826 /**
Yuncheol Heo38db6292014-07-01 14:15:14 +0900827 * Called when the system goes to standby mode.
828 *
829 * @param initiatedByCec true if this power sequence is initiated
Jungshik Jangb3e114a2014-07-11 17:50:05 +0900830 * by the reception the CEC messages like &lt;Standby&gt;
Yuncheol Heo38db6292014-07-01 14:15:14 +0900831 */
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900832 protected void onStandby(boolean initiatedByCec) {}
833
834 /**
835 * Disable device. {@code callback} is used to get notified when all pending
836 * actions are completed or timeout is issued.
837 *
838 * @param initiatedByCec true if this sequence is initiated
Jungshik Jangb3e114a2014-07-11 17:50:05 +0900839 * by the reception the CEC messages like &lt;Standby&gt;
840 * @param origialCallback callback interface to get notified when all pending actions are
841 * cleared
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900842 */
Jungshik Jangb3e114a2014-07-11 17:50:05 +0900843 protected void disableDevice(boolean initiatedByCec,
844 final PendingActionClearedCallback origialCallback) {
845 mPendingActionClearedCallback = new PendingActionClearedCallback() {
846 @Override
847 public void onCleared(HdmiCecLocalDevice device) {
848 mHandler.removeMessages(MSG_DISABLE_DEVICE_TIMEOUT);
849 origialCallback.onCleared(device);
850 }
851 };
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900852 mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_DISABLE_DEVICE_TIMEOUT),
853 DEVICE_CLEANUP_TIMEOUT);
854 }
855
856 @ServiceThreadOnly
857 private void handleDisableDeviceTimeout() {
858 assertRunOnServiceThread();
859
860 // If all actions are not cleared in DEVICE_CLEANUP_TIMEOUT, enforce to finish them.
861 // onCleard will be called at the last action's finish method.
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900862 Iterator<HdmiCecFeatureAction> iter = mActions.iterator();
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900863 while (iter.hasNext()) {
Jungshik Jangb509c2e2014-08-07 13:45:01 +0900864 HdmiCecFeatureAction action = iter.next();
Yuncheol Heoc516d652014-07-11 18:23:24 +0900865 action.finish(false);
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900866 iter.remove();
867 }
868 }
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900869
870 /**
871 * Send a key event to other device.
872 *
Jungshik Jang4fc1d102014-07-09 19:24:50 +0900873 * @param keyCode key code defined in {@link android.view.KeyEvent}
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900874 * @param isPressed {@code true} for key down event
875 */
876 protected void sendKeyEvent(int keyCode, boolean isPressed) {
877 Slog.w(TAG, "sendKeyEvent not implemented");
878 }
Terry Heo959d2db2014-08-28 16:45:41 +0900879
Jungshik Jang2e8f1b62014-09-03 08:28:02 +0900880 void sendUserControlPressedAndReleased(int targetAddress, int cecKeycode) {
881 mService.sendCecCommand(HdmiCecMessageBuilder.buildUserControlPressed(
882 mAddress, targetAddress, cecKeycode));
883 mService.sendCecCommand(HdmiCecMessageBuilder.buildUserControlReleased(
884 mAddress, targetAddress));
885 }
886
Terry Heo959d2db2014-08-28 16:45:41 +0900887 /**
888 * Dump internal status of HdmiCecLocalDevice object.
889 */
890 protected void dump(final IndentingPrintWriter pw) {
891 pw.println("mDeviceType: " + mDeviceType);
892 pw.println("mAddress: " + mAddress);
893 pw.println("mPreferredAddress: " + mPreferredAddress);
894 pw.println("mDeviceInfo: " + mDeviceInfo);
895 pw.println("mActiveSource: " + mActiveSource);
896 pw.println(String.format("mActiveRoutingPath: 0x%04x", mActiveRoutingPath));
897 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900898}