blob: c4f410f3c701c9f94d3cb65b7ad203d78c7ef64e [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 Jange5a93372014-07-25 13:41:14 +090019import static android.hardware.hdmi.HdmiControlManager.CLEAR_TIMER_STATUS_CEC_DISABLE;
20import static android.hardware.hdmi.HdmiControlManager.CLEAR_TIMER_STATUS_CHECK_RECORDER_CONNECTION;
21import static android.hardware.hdmi.HdmiControlManager.CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE;
Jungshik Jang12e5dce2014-07-24 15:27:44 +090022import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_CEC_DISABLED;
23import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION;
24import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_FAIL_TO_RECORD_DISPLAYED_SCREEN;
Jungshik Jang339227d2014-08-25 15:37:20 +090025import static android.hardware.hdmi.HdmiControlManager.OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT;
Jungshik Jange5a93372014-07-25 13:41:14 +090026import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_RESULT_EXTRA_CEC_DISABLED;
27import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_RESULT_EXTRA_CHECK_RECORDER_CONNECTION;
28import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_RESULT_EXTRA_FAIL_TO_RECORD_SELECTED_SOURCE;
29import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_TYPE_ANALOGUE;
30import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_TYPE_DIGITAL;
31import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_TYPE_EXTERNAL;
Jungshik Jang12e5dce2014-07-24 15:27:44 +090032
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090033import android.hardware.hdmi.HdmiControlManager;
Jinsuk Kim50084862014-08-07 13:11:40 +090034import android.hardware.hdmi.HdmiDeviceInfo;
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +090035import android.hardware.hdmi.HdmiPortInfo;
Jungshik Jangb6591b82014-07-23 16:10:23 +090036import android.hardware.hdmi.HdmiRecordSources;
Jungshik Jang12e5dce2014-07-24 15:27:44 +090037import android.hardware.hdmi.HdmiTimerRecordSources;
Jungshik Jang60cffce2014-06-12 18:03:04 +090038import android.hardware.hdmi.IHdmiControlCallback;
Jungshik Jangb69aafbf2014-07-11 16:29:06 +090039import android.media.AudioManager;
Jungshik Janga858d222014-06-23 17:17:47 +090040import android.media.AudioSystem;
Jinsuk Kim7fa3a662014-11-07 15:20:24 +090041import android.media.tv.TvInputInfo;
Jinsuk Kim7fa3a662014-11-07 15:20:24 +090042import android.media.tv.TvInputManager.TvInputCallback;
Jinsuk Kima6ce7702014-05-11 06:54:49 +090043import android.os.RemoteException;
Jinsuk Kim7ecfbae2014-07-11 14:16:29 +090044import android.provider.Settings.Global;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090045import android.util.ArraySet;
Jungshik Jang092b4452014-06-11 15:19:17 +090046import android.util.Slog;
Jungshik Jang79c58a42014-06-16 16:45:36 +090047import android.util.SparseArray;
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +090048import android.util.SparseBooleanArray;
Jungshik Jang092b4452014-06-11 15:19:17 +090049
Jungshik Jang79c58a42014-06-16 16:45:36 +090050import com.android.internal.annotations.GuardedBy;
Terry Heo959d2db2014-08-28 16:45:41 +090051import com.android.internal.util.IndentingPrintWriter;
Jungshik Jang60cffce2014-06-12 18:03:04 +090052import com.android.server.hdmi.DeviceDiscoveryAction.DeviceDiscoveryCallback;
Jungshik Janga5b74142014-06-23 18:03:10 +090053import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly;
Jungshik Jange5a93372014-07-25 13:41:14 +090054import com.android.server.hdmi.HdmiControlService.SendMessageCallback;
Jungshik Jang8f2ed352014-07-07 15:02:47 +090055import java.io.UnsupportedEncodingException;
Jungshik Jang79c58a42014-06-16 16:45:36 +090056import java.util.ArrayList;
Jungshik Jangb6591b82014-07-23 16:10:23 +090057import java.util.Arrays;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090058import java.util.Collection;
Jinsuk Kim0a3316b2014-06-14 09:33:55 +090059import java.util.Collections;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090060import java.util.Iterator;
Jinsuk Kim0a3316b2014-06-14 09:33:55 +090061import java.util.List;
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +090062import java.util.HashMap;
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090063
64/**
65 * Represent a logical device of type TV residing in Android system.
66 */
67final class HdmiCecLocalDeviceTv extends HdmiCecLocalDevice {
Jungshik Jang092b4452014-06-11 15:19:17 +090068 private static final String TAG = "HdmiCecLocalDeviceTv";
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090069
Jungshik Jang12e5dce2014-07-24 15:27:44 +090070 // Whether ARC is available or not. "true" means that ARC is established between TV and
Jungshik Janga13da0d2014-06-30 16:26:06 +090071 // AVR as audio receiver.
72 @ServiceThreadOnly
73 private boolean mArcEstablished = false;
74
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +090075 // Stores whether ARC feature is enabled per port. True by default for all the ARC-enabled ports.
76 private final SparseBooleanArray mArcFeatureEnabled = new SparseBooleanArray();
Jungshik Jang79c58a42014-06-16 16:45:36 +090077
Jungshik Jang377dcbd2014-07-15 15:49:02 +090078 // Whether System audio mode is activated or not.
79 // This becomes true only when all system audio sequences are finished.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +090080 @GuardedBy("mLock")
Jungshik Jang377dcbd2014-07-15 15:49:02 +090081 private boolean mSystemAudioActivated = false;
Jungshik Jang79c58a42014-06-16 16:45:36 +090082
Jinsuk Kim83335712014-06-24 07:57:00 +090083 // The previous port id (input) before switching to the new one. This is remembered in order to
84 // be able to switch to it upon receiving <Inactive Source> from currently active source.
85 // This remains valid only when the active source was switched via one touch play operation
86 // (either by TV or source device). Manual port switching invalidates this value to
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090087 // Constants.PORT_INVALID, for which case <Inactive Source> does not do anything.
Jinsuk Kim83335712014-06-24 07:57:00 +090088 @GuardedBy("mLock")
89 private int mPrevPortId;
90
Jungshik Jang8fa36b12014-06-25 15:51:36 +090091 @GuardedBy("mLock")
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090092 private int mSystemAudioVolume = Constants.UNKNOWN_VOLUME;
Jungshik Jang8fa36b12014-06-25 15:51:36 +090093
94 @GuardedBy("mLock")
95 private boolean mSystemAudioMute = false;
96
Jungshik Jangfa8e90d2014-06-23 14:40:32 +090097 // Copy of mDeviceInfos to guarantee thread-safety.
98 @GuardedBy("mLock")
Jungshik Jang61f4fbd2014-08-06 19:21:12 +090099 private List<HdmiDeviceInfo> mSafeAllDeviceInfos = Collections.emptyList();
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +0900100 // All external cec input(source) devices. Does not include system audio device.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900101 @GuardedBy("mLock")
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900102 private List<HdmiDeviceInfo> mSafeExternalInputs = Collections.emptyList();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900103
Jungshik Jang79c58a42014-06-16 16:45:36 +0900104 // Map-like container of all cec devices including local ones.
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900105 // device id is used as key of container.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900106 // This is not thread-safe. For external purpose use mSafeDeviceInfos.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900107 private final SparseArray<HdmiDeviceInfo> mDeviceInfos = new SparseArray<>();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900108
Jinsuk Kim160a6e52014-07-02 06:16:36 +0900109 // If true, TV going to standby mode puts other devices also to standby.
110 private boolean mAutoDeviceOff;
111
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900112 // If true, TV wakes itself up when receiving <Text/Image View On>.
113 private boolean mAutoWakeup;
114
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900115 // List of the logical address of local CEC devices. Unmodifiable, thread-safe.
116 private List<Integer> mLocalDeviceAddresses;
117
Yuncheol Heo25c20292014-07-31 17:59:39 +0900118 private final HdmiCecStandbyModeHandler mStandbyHandler;
119
Jinsuk Kimd5307192014-08-22 09:07:40 +0900120 // If true, do not do routing control/send active source for internal source.
121 // Set to true when the device was woken up by <Text/Image View On>.
122 private boolean mSkipRoutingControl;
123
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900124 // Set of physical addresses of CEC switches on the CEC bus. Managed independently from
125 // other CEC devices since they might not have logical address.
126 private final ArraySet<Integer> mCecSwitches = new ArraySet<Integer>();
127
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900128 // Message buffer used to buffer selected messages to process later. <Active Source>
129 // from a source device, for instance, needs to be buffered if the device is not
130 // discovered yet. The buffered commands are taken out and when they are ready to
131 // handle.
132 private final DelayedMessageBuffer mDelayedMessageBuffer = new DelayedMessageBuffer(this);
133
134 // Defines the callback invoked when TV input framework is updated with input status.
135 // We are interested in the notification for HDMI input addition event, in order to
136 // process any CEC commands that arrived before the input is added.
137 private final TvInputCallback mTvInputCallback = new TvInputCallback() {
138 @Override
139 public void onInputAdded(String inputId) {
140 TvInputInfo tvInfo = mService.getTvInputManager().getTvInputInfo(inputId);
141 HdmiDeviceInfo info = tvInfo.getHdmiDeviceInfo();
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900142 if (info == null) return;
143 addTvInput(inputId, info.getId());
144 if (info.isCecDevice()) {
145 processDelayedActiveSource(info.getLogicalAddress());
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900146 }
147 }
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900148
149 @Override
150 public void onInputRemoved(String inputId) {
151 removeTvInput(inputId);
152 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900153 };
154
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900155 // Keeps the mapping (TV input ID, HDMI device ID) to keep track of the TV inputs ready to
156 // accept input switching request from HDMI devices. Requests for which the corresponding
157 // input ID is not yet registered by TV input framework need to be buffered for delayed
158 // processing.
159 private final HashMap<String, Integer> mTvInputs = new HashMap<>();
160
161 @ServiceThreadOnly
162 private void addTvInput(String inputId, int deviceId) {
163 assertRunOnServiceThread();
164 mTvInputs.put(inputId, deviceId);
165 }
166
167 @ServiceThreadOnly
168 private void removeTvInput(String inputId) {
169 assertRunOnServiceThread();
170 mTvInputs.remove(inputId);
171 }
172
173 @Override
174 @ServiceThreadOnly
175 protected boolean isInputReady(int deviceId) {
176 assertRunOnServiceThread();
177 return mTvInputs.containsValue(deviceId);
178 }
179
Jungshik Jang3ee65722014-06-03 16:22:30 +0900180 HdmiCecLocalDeviceTv(HdmiControlService service) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900181 super(service, HdmiDeviceInfo.DEVICE_TV);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900182 mPrevPortId = Constants.INVALID_PORT_ID;
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900183 mAutoDeviceOff = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED,
184 true);
185 mAutoWakeup = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_WAKEUP_ENABLED, true);
Yuncheol Heo25c20292014-07-31 17:59:39 +0900186 mStandbyHandler = new HdmiCecStandbyModeHandler(service, this);
Jungshik Jang8b308d92014-05-29 21:52:28 +0900187 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900188
Jungshik Jang8b308d92014-05-29 21:52:28 +0900189 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900190 @ServiceThreadOnly
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900191 protected void onAddressAllocated(int logicalAddress, int reason) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900192 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900193 List<HdmiPortInfo> ports = mService.getPortInfo();
194 for (HdmiPortInfo port : ports) {
195 mArcFeatureEnabled.put(port.getId(), port.isArcSupported());
196 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900197 mService.registerTvInputCallback(mTvInputCallback);
Jungshik Jang3ee65722014-06-03 16:22:30 +0900198 mService.sendCecCommand(HdmiCecMessageBuilder.buildReportPhysicalAddressCommand(
199 mAddress, mService.getPhysicalAddress(), mDeviceType));
200 mService.sendCecCommand(HdmiCecMessageBuilder.buildDeviceVendorIdCommand(
201 mAddress, mService.getVendorId()));
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900202 mCecSwitches.add(mService.getPhysicalAddress()); // TV is a CEC switch too.
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900203 mTvInputs.clear();
Jinsuk Kimd5307192014-08-22 09:07:40 +0900204 mSkipRoutingControl = (reason == HdmiControlService.INITIATED_BY_WAKE_UP_MESSAGE);
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900205 launchRoutingControl(reason != HdmiControlService.INITIATED_BY_ENABLE_CEC &&
206 reason != HdmiControlService.INITIATED_BY_BOOT_UP);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900207 mLocalDeviceAddresses = initLocalDeviceAddresses();
Jungshik Jang60cffce2014-06-12 18:03:04 +0900208 launchDeviceDiscovery();
Jungshik Jang6f34f5a2014-07-08 21:17:29 +0900209 }
210
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900211
212 @ServiceThreadOnly
213 private List<Integer> initLocalDeviceAddresses() {
214 assertRunOnServiceThread();
215 List<Integer> addresses = new ArrayList<>();
216 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
217 addresses.add(device.getDeviceInfo().getLogicalAddress());
218 }
219 return Collections.unmodifiableList(addresses);
220 }
221
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900222 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900223 protected int getPreferredAddress() {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900224 return Constants.ADDR_TV;
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900225 }
226
227 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900228 protected void setPreferredAddress(int addr) {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900229 Slog.w(TAG, "Preferred addres will not be stored for TV");
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900230 }
231
Yuncheol Heo25c20292014-07-31 17:59:39 +0900232 @Override
233 @ServiceThreadOnly
234 boolean dispatchMessage(HdmiCecMessage message) {
235 assertRunOnServiceThread();
236 if (mService.isPowerStandby() && mStandbyHandler.handleCommand(message)) {
237 return true;
238 }
239 return super.onMessage(message);
240 }
241
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900242 /**
243 * Performs the action 'device select', or 'one touch play' initiated by TV.
244 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900245 * @param id id of HDMI device to select
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900246 * @param callback callback object to report the result with
247 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900248 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900249 void deviceSelect(int id, IHdmiControlCallback callback) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900250 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900251 HdmiDeviceInfo targetDevice = mDeviceInfos.get(id);
252 if (targetDevice == null) {
253 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
254 return;
255 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900256 int targetAddress = targetDevice.getLogicalAddress();
Jinsuk Kim58500f42014-08-05 12:48:35 +0900257 ActiveSource active = getActiveSource();
Yu.Ishihara6b2a6172015-03-25 09:44:06 +0900258 if (targetDevice.getDevicePowerStatus() == HdmiControlManager.POWER_STATUS_ON
259 && active.isValid()
260 && targetAddress == active.logicalAddress) {
Jinsuk Kim58500f42014-08-05 12:48:35 +0900261 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
262 return;
263 }
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900264 if (targetAddress == Constants.ADDR_INTERNAL) {
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900265 handleSelectInternalSource();
266 // Switching to internal source is always successful even when CEC control is disabled.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900267 setActiveSource(targetAddress, mService.getPhysicalAddress());
Jinsuk Kim7e742062014-07-30 13:19:13 +0900268 setActivePath(mService.getPhysicalAddress());
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900269 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
Jinsuk Kim83335712014-06-24 07:57:00 +0900270 return;
271 }
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900272 if (!mService.isControlEnabled()) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900273 setActiveSource(targetDevice);
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900274 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
275 return;
276 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900277 removeAction(DeviceSelectAction.class);
278 addAndStartAction(new DeviceSelectAction(this, targetDevice, callback));
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900279 }
280
Jungshik Janga5b74142014-06-23 18:03:10 +0900281 @ServiceThreadOnly
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900282 private void handleSelectInternalSource() {
Jinsuk Kima062a932014-06-18 10:00:39 +0900283 assertRunOnServiceThread();
Jinsuk Kim83335712014-06-24 07:57:00 +0900284 // Seq #18
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900285 if (mService.isControlEnabled() && mActiveSource.logicalAddress != mAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900286 updateActiveSource(mAddress, mService.getPhysicalAddress());
Jinsuk Kimd5307192014-08-22 09:07:40 +0900287 if (mSkipRoutingControl) {
288 mSkipRoutingControl = false;
289 return;
290 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900291 HdmiCecMessage activeSource = HdmiCecMessageBuilder.buildActiveSource(
292 mAddress, mService.getPhysicalAddress());
293 mService.sendCecCommand(activeSource);
294 }
295 }
296
297 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900298 void updateActiveSource(int logicalAddress, int physicalAddress) {
299 assertRunOnServiceThread();
300 updateActiveSource(ActiveSource.of(logicalAddress, physicalAddress));
301 }
302
303 @ServiceThreadOnly
304 void updateActiveSource(ActiveSource newActive) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900305 assertRunOnServiceThread();
306 // Seq #14
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900307 if (mActiveSource.equals(newActive)) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900308 return;
309 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900310 setActiveSource(newActive);
311 int logicalAddress = newActive.logicalAddress;
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900312 if (getCecDeviceInfo(logicalAddress) != null && logicalAddress != mAddress) {
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900313 if (mService.pathToPortId(newActive.physicalAddress) == getActivePortId()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900314 setPrevPortId(getActivePortId());
315 }
316 // TODO: Show the OSD banner related to the new active source device.
317 } else {
318 // TODO: If displayed, remove the OSD banner related to the previous
319 // active source device.
320 }
321 }
322
Jinsuk Kim2b152012014-07-25 08:22:26 +0900323 int getPortId(int physicalAddress) {
324 return mService.pathToPortId(physicalAddress);
325 }
326
Jinsuk Kim83335712014-06-24 07:57:00 +0900327 /**
328 * Returns the previous port id kept to handle input switching on <Inactive Source>.
329 */
330 int getPrevPortId() {
331 synchronized (mLock) {
332 return mPrevPortId;
333 }
334 }
335
336 /**
337 * Sets the previous port id. INVALID_PORT_ID invalidates it, hence no actions will be
338 * taken for <Inactive Source>.
339 */
340 void setPrevPortId(int portId) {
341 synchronized (mLock) {
342 mPrevPortId = portId;
343 }
344 }
345
346 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900347 void updateActiveInput(int path, boolean notifyInputChange) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900348 assertRunOnServiceThread();
349 // Seq #15
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900350 setActivePath(path);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900351 // TODO: Handle PAP/PIP case.
352 // Show OSD port change banner
353 if (notifyInputChange) {
354 ActiveSource activeSource = getActiveSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900355 HdmiDeviceInfo info = getCecDeviceInfo(activeSource.logicalAddress);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900356 if (info == null) {
Jinsuk Kim6ad7cbd2015-01-06 11:30:56 +0900357 info = mService.getDeviceInfoByPort(getActivePortId());
358 if (info == null) {
359 // No CEC/MHL device is present at the port. Attempt to switch to
360 // the hardware port itself for non-CEC devices that may be connected.
361 info = new HdmiDeviceInfo(path, getActivePortId());
362 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900363 }
364 mService.invokeInputChangeListener(info);
365 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900366 }
367
368 @ServiceThreadOnly
369 void doManualPortSwitching(int portId, IHdmiControlCallback callback) {
370 assertRunOnServiceThread();
371 // Seq #20
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900372 if (!mService.isValidPortId(portId)) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900373 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
Jinsuk Kima062a932014-06-18 10:00:39 +0900374 return;
375 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900376 if (portId == getActivePortId()) {
377 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
378 return;
379 }
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900380 mActiveSource.invalidate();
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900381 if (!mService.isControlEnabled()) {
382 setActivePortId(portId);
383 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
384 return;
385 }
Jinsuk Kim1f8d1c572014-08-12 15:39:53 +0900386 int oldPath = getActivePortId() != Constants.INVALID_PORT_ID
387 ? mService.portIdToPath(getActivePortId()) : getDeviceInfo().getPhysicalAddress();
Jinsuk Kimd5307192014-08-22 09:07:40 +0900388 setActivePath(oldPath);
389 if (mSkipRoutingControl) {
390 mSkipRoutingControl = false;
391 return;
392 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900393 int newPath = mService.portIdToPath(portId);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900394 startRoutingControl(oldPath, newPath, true, callback);
395 }
396
397 @ServiceThreadOnly
398 void startRoutingControl(int oldPath, int newPath, boolean queryDevicePowerStatus,
399 IHdmiControlCallback callback) {
400 assertRunOnServiceThread();
401 if (oldPath == newPath) {
402 return;
403 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900404 HdmiCecMessage routingChange =
405 HdmiCecMessageBuilder.buildRoutingChange(mAddress, oldPath, newPath);
406 mService.sendCecCommand(routingChange);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900407 removeAction(RoutingControlAction.class);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900408 addAndStartAction(
409 new RoutingControlAction(this, newPath, queryDevicePowerStatus, callback));
Jinsuk Kima062a932014-06-18 10:00:39 +0900410 }
411
Jungshik Jangf67113f2014-08-22 16:27:19 +0900412 @ServiceThreadOnly
Jinsuk Kimcae66272014-07-04 13:26:44 +0900413 int getPowerStatus() {
Jungshik Jangf67113f2014-08-22 16:27:19 +0900414 assertRunOnServiceThread();
Jinsuk Kimcae66272014-07-04 13:26:44 +0900415 return mService.getPowerStatus();
416 }
417
Jinsuk Kima062a932014-06-18 10:00:39 +0900418 /**
419 * Sends key to a target CEC device.
420 *
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900421 * @param keyCode key code to send. Defined in {@link android.view.KeyEvent}.
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900422 * @param isPressed true if this is key press event
Jinsuk Kima062a932014-06-18 10:00:39 +0900423 */
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900424 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900425 @ServiceThreadOnly
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900426 protected void sendKeyEvent(int keyCode, boolean isPressed) {
Jinsuk Kima062a932014-06-18 10:00:39 +0900427 assertRunOnServiceThread();
Dongil Seob64c2ba2014-08-26 19:41:39 +0900428 if (!HdmiCecKeycode.isSupportedKeycode(keyCode)) {
429 Slog.w(TAG, "Unsupported key: " + keyCode);
430 return;
431 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900432 List<SendKeyAction> action = getActions(SendKeyAction.class);
Jinsuk Kim454fab52015-01-29 16:38:36 +0900433 int logicalAddress = findKeyReceiverAddress();
434 if (logicalAddress == mAddress) {
435 Slog.w(TAG, "Discard key event to itself :" + keyCode + " pressed:" + isPressed);
436 return;
437 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900438 if (!action.isEmpty()) {
439 action.get(0).processKeyEvent(keyCode, isPressed);
440 } else {
Jinsuk Kim75434972014-09-25 13:52:50 +0900441 if (isPressed) {
Jinsuk Kim75434972014-09-25 13:52:50 +0900442 if (logicalAddress != Constants.ADDR_INVALID) {
443 addAndStartAction(new SendKeyAction(this, logicalAddress, keyCode));
444 return;
445 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900446 }
Jinsuk Kim75434972014-09-25 13:52:50 +0900447 Slog.w(TAG, "Discard key event: " + keyCode + " pressed:" + isPressed);
Jinsuk Kima062a932014-06-18 10:00:39 +0900448 }
449 }
450
Jinsuk Kim75434972014-09-25 13:52:50 +0900451 private int findKeyReceiverAddress() {
452 if (getActiveSource().isValid()) {
453 return getActiveSource().logicalAddress;
454 }
455 HdmiDeviceInfo info = getDeviceInfoByPath(getActivePath());
456 if (info != null) {
457 return info.getLogicalAddress();
458 }
459 return Constants.ADDR_INVALID;
460 }
461
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900462 private static void invokeCallback(IHdmiControlCallback callback, int result) {
Jinsuk Kim4893c7e2014-06-19 14:13:22 +0900463 if (callback == null) {
464 return;
465 }
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900466 try {
467 callback.onComplete(result);
468 } catch (RemoteException e) {
469 Slog.e(TAG, "Invoking callback failed:" + e);
470 }
471 }
472
Jungshik Jang092b4452014-06-11 15:19:17 +0900473 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900474 @ServiceThreadOnly
Jinsuk Kim83335712014-06-24 07:57:00 +0900475 protected boolean handleActiveSource(HdmiCecMessage message) {
476 assertRunOnServiceThread();
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900477 int logicalAddress = message.getSource();
478 int physicalAddress = HdmiUtils.twoBytesToInt(message.getParams());
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900479 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
480 if (info == null) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900481 if (!handleNewDeviceAtTheTailOfActivePath(physicalAddress)) {
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900482 HdmiLogger.debug("Device info %X not found; buffering the command", logicalAddress);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900483 mDelayedMessageBuffer.add(message);
484 }
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900485 } else if (!isInputReady(info.getId())) {
486 HdmiLogger.debug("Input not ready for device: %X; buffering the command", info.getId());
487 mDelayedMessageBuffer.add(message);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900488 } else {
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900489 ActiveSource activeSource = ActiveSource.of(logicalAddress, physicalAddress);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900490 ActiveSourceHandler.create(this, null).process(activeSource, info.getDeviceType());
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900491 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900492 return true;
493 }
494
495 @Override
496 @ServiceThreadOnly
497 protected boolean handleInactiveSource(HdmiCecMessage message) {
498 assertRunOnServiceThread();
499 // Seq #10
500
501 // Ignore <Inactive Source> from non-active source device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900502 if (getActiveSource().logicalAddress != message.getSource()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900503 return true;
504 }
Jinsuk Kim4d43d932014-07-03 16:43:58 +0900505 if (isProhibitMode()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900506 return true;
507 }
508 int portId = getPrevPortId();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900509 if (portId != Constants.INVALID_PORT_ID) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900510 // TODO: Do this only if TV is not showing multiview like PIP/PAP.
511
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900512 HdmiDeviceInfo inactiveSource = getCecDeviceInfo(message.getSource());
Jinsuk Kim83335712014-06-24 07:57:00 +0900513 if (inactiveSource == null) {
514 return true;
515 }
516 if (mService.pathToPortId(inactiveSource.getPhysicalAddress()) == portId) {
517 return true;
518 }
519 // TODO: Switch the TV freeze mode off
520
Jinsuk Kim83335712014-06-24 07:57:00 +0900521 doManualPortSwitching(portId, null);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900522 setPrevPortId(Constants.INVALID_PORT_ID);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900523 } else {
524 // No HDMI port to switch to was found. Notify the input change listers to
525 // switch to the lastly shown internal input.
526 mActiveSource.invalidate();
527 setActivePath(Constants.INVALID_PHYSICAL_ADDRESS);
528 mService.invokeInputChangeListener(HdmiDeviceInfo.INACTIVE_DEVICE);
Jinsuk Kim83335712014-06-24 07:57:00 +0900529 }
530 return true;
531 }
532
533 @Override
534 @ServiceThreadOnly
535 protected boolean handleRequestActiveSource(HdmiCecMessage message) {
536 assertRunOnServiceThread();
537 // Seq #19
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900538 if (mAddress == getActiveSource().logicalAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900539 mService.sendCecCommand(
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900540 HdmiCecMessageBuilder.buildActiveSource(mAddress, getActivePath()));
Jinsuk Kim83335712014-06-24 07:57:00 +0900541 }
542 return true;
543 }
544
545 @Override
546 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900547 protected boolean handleGetMenuLanguage(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900548 assertRunOnServiceThread();
Jungshik Jangf67113f2014-08-22 16:27:19 +0900549 if (!broadcastMenuLanguage(mService.getLanguage())) {
Jungshik Jang092b4452014-06-11 15:19:17 +0900550 Slog.w(TAG, "Failed to respond to <Get Menu Language>: " + message.toString());
551 }
552 return true;
553 }
554
Terry Heo1ca0a432014-08-18 10:30:32 +0900555 @ServiceThreadOnly
556 boolean broadcastMenuLanguage(String language) {
557 assertRunOnServiceThread();
558 HdmiCecMessage command = HdmiCecMessageBuilder.buildSetMenuLanguageCommand(
559 mAddress, language);
560 if (command != null) {
561 mService.sendCecCommand(command);
562 return true;
563 }
564 return false;
565 }
566
Jungshik Jang60cffce2014-06-12 18:03:04 +0900567 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900568 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900569 protected boolean handleReportPhysicalAddress(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900570 assertRunOnServiceThread();
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900571 int path = HdmiUtils.twoBytesToInt(message.getParams());
572 int address = message.getSource();
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900573 int type = message.getParams()[2];
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900574
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900575 if (updateCecSwitchInfo(address, type, path)) return true;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900576
Jungshik Jang092b4452014-06-11 15:19:17 +0900577 // Ignore if [Device Discovery Action] is going on.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900578 if (hasAction(DeviceDiscoveryAction.class)) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900579 Slog.i(TAG, "Ignored while Device Discovery Action is in progress: " + message);
Jungshik Jang092b4452014-06-11 15:19:17 +0900580 return true;
581 }
582
Jinsuk Kim4b4b9402014-09-02 10:30:10 +0900583 if (!isInDeviceList(address, path)) {
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900584 handleNewDeviceAtTheTailOfActivePath(path);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900585 }
Jinsuk Kim7cd4a582015-01-29 09:50:25 +0900586
587 // Add the device ahead with default information to handle <Active Source>
588 // promptly, rather than waiting till the new device action is finished.
589 HdmiDeviceInfo deviceInfo = new HdmiDeviceInfo(address, path, getPortId(path), type,
590 Constants.UNKNOWN_VENDOR_ID, HdmiUtils.getDefaultDeviceName(address));
591 addCecDevice(deviceInfo);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900592 startNewDeviceAction(ActiveSource.of(address, path), type);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900593 return true;
594 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900595
Jungshik Jang4480efa2014-09-04 17:08:34 +0900596 @Override
597 protected boolean handleReportPowerStatus(HdmiCecMessage command) {
598 int newStatus = command.getParams()[0] & 0xFF;
599 updateDevicePowerStatus(command.getSource(), newStatus);
600 return true;
601 }
602
603 @Override
604 protected boolean handleTimerStatus(HdmiCecMessage message) {
605 // Do nothing.
606 return true;
607 }
608
609 @Override
610 protected boolean handleRecordStatus(HdmiCecMessage message) {
611 // Do nothing.
612 return true;
613 }
614
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900615 boolean updateCecSwitchInfo(int address, int type, int path) {
616 if (address == Constants.ADDR_UNREGISTERED
617 && type == HdmiDeviceInfo.DEVICE_PURE_CEC_SWITCH) {
618 mCecSwitches.add(path);
619 updateSafeDeviceInfoList();
620 return true; // Pure switch does not need further processing. Return here.
621 }
622 if (type == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) {
623 mCecSwitches.add(path);
624 }
625 return false;
626 }
627
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900628 void startNewDeviceAction(ActiveSource activeSource, int deviceType) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900629 for (NewDeviceAction action : getActions(NewDeviceAction.class)) {
630 // If there is new device action which has the same logical address and path
631 // ignore new request.
632 // NewDeviceAction is created whenever it receives <Report Physical Address>.
633 // And there is a chance starting NewDeviceAction for the same source.
634 // Usually, new device sends <Report Physical Address> when it's plugged
635 // in. However, TV can detect a new device from HotPlugDetectionAction,
636 // which sends <Give Physical Address> to the source for newly detected
637 // device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900638 if (action.isActionOf(activeSource)) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900639 return;
640 }
641 }
642
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900643 addAndStartAction(new NewDeviceAction(this, activeSource.logicalAddress,
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900644 activeSource.physicalAddress, deviceType));
Jungshik Jang97affee2014-07-11 17:04:43 +0900645 }
646
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900647 private boolean handleNewDeviceAtTheTailOfActivePath(int path) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900648 // Seq #22
649 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900650 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim7c3a9562014-08-01 11:07:42 +0900651 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900652 startRoutingControl(getActivePath(), newPath, false, null);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900653 return true;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900654 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900655 return false;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900656 }
657
658 /**
659 * Whether the given path is located in the tail of current active path.
660 *
661 * @param path to be tested
662 * @param activePath current active path
663 * @return true if the given path is located in the tail of current active path; otherwise,
664 * false
665 */
666 static boolean isTailOfActivePath(int path, int activePath) {
667 // If active routing path is internal source, return false.
668 if (activePath == 0) {
669 return false;
670 }
671 for (int i = 12; i >= 0; i -= 4) {
672 int curActivePath = (activePath >> i) & 0xF;
673 if (curActivePath == 0) {
674 return true;
675 } else {
676 int curPath = (path >> i) & 0xF;
677 if (curPath != curActivePath) {
678 return false;
679 }
680 }
681 }
682 return false;
683 }
684
685 @Override
686 @ServiceThreadOnly
687 protected boolean handleRoutingChange(HdmiCecMessage message) {
688 assertRunOnServiceThread();
689 // Seq #21
690 byte[] params = message.getParams();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900691 int currentPath = HdmiUtils.twoBytesToInt(params);
692 if (HdmiUtils.isAffectingActiveRoutingPath(getActivePath(), currentPath)) {
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900693 mActiveSource.invalidate();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900694 removeAction(RoutingControlAction.class);
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900695 int newPath = HdmiUtils.twoBytesToInt(params, 2);
Jinsuk Kim04fd2802014-07-03 14:04:02 +0900696 addAndStartAction(new RoutingControlAction(this, newPath, true, null));
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900697 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900698 return true;
699 }
Jinsuk Kim0a3316b2014-06-14 09:33:55 +0900700
701 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900702 @ServiceThreadOnly
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900703 protected boolean handleReportAudioStatus(HdmiCecMessage message) {
704 assertRunOnServiceThread();
705
706 byte params[] = message.getParams();
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900707 int mute = params[0] & 0x80;
708 int volume = params[0] & 0x7F;
709 setAudioStatus(mute == 0x80, volume);
710 return true;
711 }
712
Yuncheol Heo38db6292014-07-01 14:15:14 +0900713 @Override
714 @ServiceThreadOnly
715 protected boolean handleTextViewOn(HdmiCecMessage message) {
716 assertRunOnServiceThread();
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900717 if (mService.isPowerStandbyOrTransient() && mAutoWakeup) {
Yuncheol Heo38db6292014-07-01 14:15:14 +0900718 mService.wakeUp();
719 }
Yuncheol Heo38db6292014-07-01 14:15:14 +0900720 return true;
721 }
722
723 @Override
724 @ServiceThreadOnly
725 protected boolean handleImageViewOn(HdmiCecMessage message) {
726 assertRunOnServiceThread();
727 // Currently, it's the same as <Text View On>.
728 return handleTextViewOn(message);
729 }
730
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900731 @Override
732 @ServiceThreadOnly
733 protected boolean handleSetOsdName(HdmiCecMessage message) {
734 int source = message.getSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900735 HdmiDeviceInfo deviceInfo = getCecDeviceInfo(source);
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900736 // If the device is not in device list, ignore it.
737 if (deviceInfo == null) {
738 Slog.e(TAG, "No source device info for <Set Osd Name>." + message);
739 return true;
740 }
741 String osdName = null;
742 try {
743 osdName = new String(message.getParams(), "US-ASCII");
744 } catch (UnsupportedEncodingException e) {
745 Slog.e(TAG, "Invalid <Set Osd Name> request:" + message, e);
746 return true;
747 }
748
749 if (deviceInfo.getDisplayName().equals(osdName)) {
750 Slog.i(TAG, "Ignore incoming <Set Osd Name> having same osd name:" + message);
751 return true;
752 }
753
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900754 addCecDevice(new HdmiDeviceInfo(deviceInfo.getLogicalAddress(),
Jinsuk Kim2b152012014-07-25 08:22:26 +0900755 deviceInfo.getPhysicalAddress(), deviceInfo.getPortId(),
756 deviceInfo.getDeviceType(), deviceInfo.getVendorId(), osdName));
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900757 return true;
758 }
759
Jungshik Janga5b74142014-06-23 18:03:10 +0900760 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900761 private void launchDeviceDiscovery() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900762 assertRunOnServiceThread();
763 clearDeviceInfoList();
764 DeviceDiscoveryAction action = new DeviceDiscoveryAction(this,
Jungshik Jang60cffce2014-06-12 18:03:04 +0900765 new DeviceDiscoveryCallback() {
766 @Override
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900767 public void onDeviceDiscoveryDone(List<HdmiDeviceInfo> deviceInfos) {
768 for (HdmiDeviceInfo info : deviceInfos) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900769 addCecDevice(info);
Jungshik Jang60cffce2014-06-12 18:03:04 +0900770 }
771
772 // Since we removed all devices when it's start and
773 // device discovery action does not poll local devices,
774 // we should put device info of local device manually here
775 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900776 addCecDevice(device.getDeviceInfo());
Jungshik Jang60cffce2014-06-12 18:03:04 +0900777 }
778
Jungshik Jang79c58a42014-06-16 16:45:36 +0900779 addAndStartAction(new HotplugDetectionAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang410ca9c2014-08-07 18:04:14 +0900780 addAndStartAction(new PowerStatusMonitorAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang187d0172014-06-17 17:48:42 +0900781
782 // If there is AVR, initiate System Audio Auto initiation action,
783 // which turns on and off system audio according to last system
784 // audio setting.
Jungshik Jang339227d2014-08-25 15:37:20 +0900785 HdmiDeviceInfo avr = getAvrDeviceInfo();
786 if (avr != null) {
787 onNewAvrAdded(avr);
Wally Yau951e3e42015-04-03 15:12:52 -0700788 } else {
789 setSystemAudioMode(false, true);
Jungshik Jang187d0172014-06-17 17:48:42 +0900790 }
Jungshik Jang60cffce2014-06-12 18:03:04 +0900791 }
792 });
Jungshik Jang79c58a42014-06-16 16:45:36 +0900793 addAndStartAction(action);
794 }
795
Jungshik Jang339227d2014-08-25 15:37:20 +0900796 @ServiceThreadOnly
797 void onNewAvrAdded(HdmiDeviceInfo avr) {
798 assertRunOnServiceThread();
Jinsuk Kimad1e3d72015-01-02 13:27:15 +0900799 if (getSystemAudioModeSetting() && !isSystemAudioActivated()) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900800 addAndStartAction(new SystemAudioAutoInitiationAction(this, avr.getLogicalAddress()));
801 }
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900802 if (isArcFeatureEnabled(avr.getPortId())
803 && !hasAction(SetArcTransmissionStateAction.class)) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900804 startArcAction(true);
805 }
806 }
807
Jungshik Jang79c58a42014-06-16 16:45:36 +0900808 // Clear all device info.
Jungshik Janga5b74142014-06-23 18:03:10 +0900809 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900810 private void clearDeviceInfoList() {
811 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900812 for (HdmiDeviceInfo info : mSafeExternalInputs) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +0900813 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +0900814 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900815 mDeviceInfos.clear();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900816 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900817 }
818
Jungshik Janga5b74142014-06-23 18:03:10 +0900819 @ServiceThreadOnly
Yuncheol Heoc516d652014-07-11 18:23:24 +0900820 // Seq #32
Jungshik Jangea67c182014-06-19 22:19:20 +0900821 void changeSystemAudioMode(boolean enabled, IHdmiControlCallback callback) {
822 assertRunOnServiceThread();
Yuncheol Heoc516d652014-07-11 18:23:24 +0900823 if (!mService.isControlEnabled() || hasAction(DeviceDiscoveryAction.class)) {
824 setSystemAudioMode(false, true);
825 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
826 return;
827 }
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900828 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jangea67c182014-06-19 22:19:20 +0900829 if (avr == null) {
Yuncheol Heoc516d652014-07-11 18:23:24 +0900830 setSystemAudioMode(false, true);
Yuncheol Heod05f67f2014-07-11 16:06:40 +0900831 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
Jungshik Jangea67c182014-06-19 22:19:20 +0900832 return;
833 }
834
835 addAndStartAction(
836 new SystemAudioActionFromTv(this, avr.getLogicalAddress(), enabled, callback));
Jungshik Jangea67c182014-06-19 22:19:20 +0900837 }
838
Jungshik Jangca5be9a2014-07-01 18:01:26 +0900839 // # Seq 25
Jinsuk Kim7ecfbae2014-07-11 14:16:29 +0900840 void setSystemAudioMode(boolean on, boolean updateSetting) {
Jungshik Jang2e8f1b62014-09-03 08:28:02 +0900841 HdmiLogger.debug("System Audio Mode change[old:%b new:%b]", mSystemAudioActivated, on);
Jungshik Jang473119f2014-08-27 16:43:22 +0900842
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900843 if (updateSetting) {
844 mService.writeBooleanSetting(Global.HDMI_SYSTEM_AUDIO_ENABLED, on);
845 }
846 updateAudioManagerForSystemAudio(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900847 synchronized (mLock) {
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900848 if (mSystemAudioActivated != on) {
849 mSystemAudioActivated = on;
Jungshik Jangea67c182014-06-19 22:19:20 +0900850 mService.announceSystemAudioModeChange(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900851 }
852 }
853 }
854
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900855 private void updateAudioManagerForSystemAudio(boolean on) {
Jungshik Jang6b096d32014-09-12 18:49:08 +0900856 int device = mService.getAudioManager().setHdmiSystemAudioSupported(on);
857 HdmiLogger.debug("[A]UpdateSystemAudio mode[on=%b] output=[%X]", on, device);
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900858 }
859
860 boolean isSystemAudioActivated() {
Jinsuk Kim86a1e5a2014-11-19 06:04:50 +0900861 if (!hasSystemAudioDevice()) {
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900862 return false;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900863 }
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900864 synchronized (mLock) {
865 return mSystemAudioActivated;
866 }
867 }
868
869 boolean getSystemAudioModeSetting() {
870 return mService.readBooleanSetting(Global.HDMI_SYSTEM_AUDIO_ENABLED, false);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900871 }
872
873 /**
874 * Change ARC status into the given {@code enabled} status.
875 *
876 * @return {@code true} if ARC was in "Enabled" status
877 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900878 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900879 boolean setArcStatus(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900880 assertRunOnServiceThread();
Jungshik Jang6b096d32014-09-12 18:49:08 +0900881
882 HdmiLogger.debug("Set Arc Status[old:%b new:%b]", mArcEstablished, enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900883 boolean oldStatus = mArcEstablished;
884 // 1. Enable/disable ARC circuit.
Jinsuk Kim757c0972015-02-23 10:15:42 +0900885 setAudioReturnChannel(enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900886 // 2. Notify arc status to audio service.
887 notifyArcStatusToAudioService(enabled);
888 // 3. Update arc status;
889 mArcEstablished = enabled;
890 return oldStatus;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900891 }
892
Jinsuk Kim757c0972015-02-23 10:15:42 +0900893 /**
894 * Switch hardware ARC circuit in the system.
895 */
896 @ServiceThreadOnly
897 void setAudioReturnChannel(boolean enabled) {
898 assertRunOnServiceThread();
899 HdmiDeviceInfo avr = getAvrDeviceInfo();
900 if (avr != null) {
901 mService.setAudioReturnChannel(avr.getPortId(), enabled);
902 }
903 }
904
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +0900905 @ServiceThreadOnly
906 private void updateArcFeatureStatus(int portId, boolean isConnected) {
907 assertRunOnServiceThread();
908 // HEAC 2.4, HEACT 5-15
909 // Should not activate ARC if +5V status is false.
910 HdmiPortInfo portInfo = mService.getPortInfo(portId);
911 if (portInfo.isArcSupported()) {
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900912 changeArcFeatureEnabled(portId, isConnected);
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +0900913 }
914 }
915
Jinsuk Kim7b0cf642015-04-14 09:43:45 +0900916 @ServiceThreadOnly
917 boolean isConnected(int portId) {
918 assertRunOnServiceThread();
919 return mService.isConnected(portId);
920 }
921
Jungshik Janga858d222014-06-23 17:17:47 +0900922 private void notifyArcStatusToAudioService(boolean enabled) {
923 // Note that we don't set any name to ARC.
924 mService.getAudioManager().setWiredDeviceConnectionState(
925 AudioSystem.DEVICE_OUT_HDMI_ARC,
Paul McLean10804eb2015-01-28 11:16:35 -0800926 enabled ? 1 : 0, "", "");
Jungshik Janga858d222014-06-23 17:17:47 +0900927 }
928
Jungshik Jang79c58a42014-06-16 16:45:36 +0900929 /**
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900930 * Returns true if ARC is currently established on a certain port.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900931 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900932 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900933 boolean isArcEstablished() {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900934 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900935 if (mArcEstablished) {
936 for (int i = 0; i < mArcFeatureEnabled.size(); i++) {
937 if (mArcFeatureEnabled.valueAt(i)) return true;
938 }
939 }
940 return false;
Jungshik Janga13da0d2014-06-30 16:26:06 +0900941 }
942
943 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900944 void changeArcFeatureEnabled(int portId, boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900945 assertRunOnServiceThread();
946
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900947 if (mArcFeatureEnabled.get(portId) != enabled) {
948 mArcFeatureEnabled.put(portId, enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900949 if (enabled) {
950 if (!mArcEstablished) {
951 startArcAction(true);
952 }
953 } else {
954 if (mArcEstablished) {
955 startArcAction(false);
956 }
957 }
Jungshik Janga13da0d2014-06-30 16:26:06 +0900958 }
959 }
960
961 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900962 boolean isArcFeatureEnabled(int portId) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900963 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900964 return mArcFeatureEnabled.get(portId);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900965 }
966
967 @ServiceThreadOnly
Jungshik Jang339227d2014-08-25 15:37:20 +0900968 void startArcAction(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900969 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900970 HdmiDeviceInfo info = getAvrDeviceInfo();
Jungshik Janga13da0d2014-06-30 16:26:06 +0900971 if (info == null) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900972 Slog.w(TAG, "Failed to start arc action; No AVR device.");
Jungshik Janga13da0d2014-06-30 16:26:06 +0900973 return;
974 }
Jungshik Jang339227d2014-08-25 15:37:20 +0900975 if (!canStartArcUpdateAction(info.getLogicalAddress(), enabled)) {
976 Slog.w(TAG, "Failed to start arc action; ARC configuration check failed.");
977 if (enabled && !isConnectedToArcPort(info.getPhysicalAddress())) {
978 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
979 }
Jungshik Janga13da0d2014-06-30 16:26:06 +0900980 return;
981 }
982
983 // Terminate opposite action and start action if not exist.
984 if (enabled) {
985 removeAction(RequestArcTerminationAction.class);
986 if (!hasAction(RequestArcInitiationAction.class)) {
987 addAndStartAction(new RequestArcInitiationAction(this, info.getLogicalAddress()));
988 }
989 } else {
990 removeAction(RequestArcInitiationAction.class);
991 if (!hasAction(RequestArcTerminationAction.class)) {
992 addAndStartAction(new RequestArcTerminationAction(this, info.getLogicalAddress()));
993 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900994 }
995 }
996
Jungshik Jang339227d2014-08-25 15:37:20 +0900997 private boolean isDirectConnectAddress(int physicalAddress) {
998 return (physicalAddress & Constants.ROUTING_PATH_TOP_MASK) == physicalAddress;
999 }
1000
Jungshik Jang79c58a42014-06-16 16:45:36 +09001001 void setAudioStatus(boolean mute, int volume) {
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001002 synchronized (mLock) {
1003 mSystemAudioMute = mute;
1004 mSystemAudioVolume = volume;
Jungshik Jangb69aafbf2014-07-11 16:29:06 +09001005 int maxVolume = mService.getAudioManager().getStreamMaxVolume(
1006 AudioManager.STREAM_MUSIC);
1007 mService.setAudioStatus(mute,
1008 VolumeControlAction.scaleToCustomVolume(volume, maxVolume));
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001009 displayOsd(HdmiControlManager.OSD_MESSAGE_AVR_VOLUME_CHANGED,
1010 mute ? HdmiControlManager.AVR_VOLUME_MUTED : volume);
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001011 }
1012 }
1013
1014 @ServiceThreadOnly
1015 void changeVolume(int curVolume, int delta, int maxVolume) {
1016 assertRunOnServiceThread();
Jungshik Jang377dcbd2014-07-15 15:49:02 +09001017 if (delta == 0 || !isSystemAudioActivated()) {
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001018 return;
1019 }
1020
1021 int targetVolume = curVolume + delta;
1022 int cecVolume = VolumeControlAction.scaleToCecVolume(targetVolume, maxVolume);
1023 synchronized (mLock) {
1024 // If new volume is the same as current system audio volume, just ignore it.
1025 // Note that UNKNOWN_VOLUME is not in range of cec volume scale.
1026 if (cecVolume == mSystemAudioVolume) {
1027 // Update tv volume with system volume value.
1028 mService.setAudioStatus(false,
1029 VolumeControlAction.scaleToCustomVolume(mSystemAudioVolume, maxVolume));
1030 return;
1031 }
1032 }
1033
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001034 List<VolumeControlAction> actions = getActions(VolumeControlAction.class);
1035 if (actions.isEmpty()) {
1036 addAndStartAction(new VolumeControlAction(this,
1037 getAvrDeviceInfo().getLogicalAddress(), delta > 0));
1038 } else {
1039 actions.get(0).handleVolumeChange(delta > 0);
1040 }
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001041 }
1042
1043 @ServiceThreadOnly
1044 void changeMute(boolean mute) {
1045 assertRunOnServiceThread();
Jungshik Jang6b096d32014-09-12 18:49:08 +09001046 HdmiLogger.debug("[A]:Change mute:%b", mute);
Jungshik Jang720407a2014-09-15 17:48:41 +09001047 synchronized (mLock) {
1048 if (mSystemAudioMute == mute) {
1049 HdmiLogger.debug("No need to change mute.");
1050 return;
1051 }
1052 }
Jungshik Jang377dcbd2014-07-15 15:49:02 +09001053 if (!isSystemAudioActivated()) {
Jungshik Jang6b096d32014-09-12 18:49:08 +09001054 HdmiLogger.debug("[A]:System audio is not activated.");
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001055 return;
1056 }
1057
1058 // Remove existing volume action.
1059 removeAction(VolumeControlAction.class);
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001060 sendUserControlPressedAndReleased(getAvrDeviceInfo().getLogicalAddress(),
1061 mute ? HdmiCecKeycode.CEC_KEYCODE_MUTE_FUNCTION :
1062 HdmiCecKeycode.CEC_KEYCODE_RESTORE_VOLUME_FUNCTION);
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001063 }
1064
Jungshik Jang79c58a42014-06-16 16:45:36 +09001065 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001066 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001067 protected boolean handleInitiateArc(HdmiCecMessage message) {
1068 assertRunOnServiceThread();
Jungshik Jang339227d2014-08-25 15:37:20 +09001069
1070 if (!canStartArcUpdateAction(message.getSource(), true)) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001071 if (getAvrDeviceInfo() == null) {
1072 // AVR may not have been discovered yet. Delay the message processing.
1073 mDelayedMessageBuffer.add(message);
1074 return true;
1075 }
Jungshik Jang339227d2014-08-25 15:37:20 +09001076 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
1077 if (!isConnectedToArcPort(message.getSource())) {
1078 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
1079 }
1080 return true;
1081 }
1082
Jungshik Jang79c58a42014-06-16 16:45:36 +09001083 // In case where <Initiate Arc> is started by <Request ARC Initiation>
1084 // need to clean up RequestArcInitiationAction.
1085 removeAction(RequestArcInitiationAction.class);
1086 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1087 message.getSource(), true);
1088 addAndStartAction(action);
1089 return true;
1090 }
1091
Jungshik Jang339227d2014-08-25 15:37:20 +09001092 private boolean canStartArcUpdateAction(int avrAddress, boolean shouldCheckArcFeatureEnabled) {
1093 HdmiDeviceInfo avr = getAvrDeviceInfo();
1094 if (avr != null
1095 && (avrAddress == avr.getLogicalAddress())
1096 && isConnectedToArcPort(avr.getPhysicalAddress())
1097 && isDirectConnectAddress(avr.getPhysicalAddress())) {
1098 if (shouldCheckArcFeatureEnabled) {
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +09001099 return isArcFeatureEnabled(avr.getPortId());
Jungshik Jang339227d2014-08-25 15:37:20 +09001100 } else {
1101 return true;
1102 }
1103 } else {
1104 return false;
1105 }
1106 }
1107
Jungshik Jang79c58a42014-06-16 16:45:36 +09001108 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001109 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001110 protected boolean handleTerminateArc(HdmiCecMessage message) {
1111 assertRunOnServiceThread();
Jinsuk Kim7e4b4802015-04-20 13:17:13 +09001112 if (mService .isPowerStandbyOrTransient()) {
1113 setArcStatus(false);
1114 return true;
1115 }
1116 // Do not check ARC configuration since the AVR might have been already removed.
1117 // Clean up RequestArcTerminationAction in case <Terminate Arc> was started by
1118 // <Request ARC Termination>.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001119 removeAction(RequestArcTerminationAction.class);
1120 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1121 message.getSource(), false);
1122 addAndStartAction(action);
1123 return true;
1124 }
1125
1126 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001127 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001128 protected boolean handleSetSystemAudioMode(HdmiCecMessage message) {
1129 assertRunOnServiceThread();
1130 if (!isMessageForSystemAudio(message)) {
Jinsuk Kimad1e3d72015-01-02 13:27:15 +09001131 if (getAvrDeviceInfo() == null) {
1132 // AVR may not have been discovered yet. Delay the message processing.
1133 mDelayedMessageBuffer.add(message);
1134 return true;
1135 }
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001136 HdmiLogger.warning("Invalid <Set System Audio Mode> message:" + message);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001137 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
1138 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001139 }
1140 SystemAudioActionFromAvr action = new SystemAudioActionFromAvr(this,
Jungshik Jang7f0a1c52014-06-23 16:00:07 +09001141 message.getSource(), HdmiUtils.parseCommandParamSystemAudioStatus(message), null);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001142 addAndStartAction(action);
1143 return true;
1144 }
1145
1146 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001147 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001148 protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) {
1149 assertRunOnServiceThread();
1150 if (!isMessageForSystemAudio(message)) {
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001151 HdmiLogger.warning("Invalid <System Audio Mode Status> message:" + message);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001152 // Ignore this message.
1153 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001154 }
Jinsuk Kim7ecfbae2014-07-11 14:16:29 +09001155 setSystemAudioMode(HdmiUtils.parseCommandParamSystemAudioStatus(message), true);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001156 return true;
1157 }
1158
Jungshik Jangb6591b82014-07-23 16:10:23 +09001159 // Seq #53
1160 @Override
1161 @ServiceThreadOnly
1162 protected boolean handleRecordTvScreen(HdmiCecMessage message) {
1163 List<OneTouchRecordAction> actions = getActions(OneTouchRecordAction.class);
1164 if (!actions.isEmpty()) {
1165 // Assumes only one OneTouchRecordAction.
1166 OneTouchRecordAction action = actions.get(0);
1167 if (action.getRecorderAddress() != message.getSource()) {
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001168 announceOneTouchRecordResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001169 message.getSource(),
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001170 HdmiControlManager.ONE_TOUCH_RECORD_PREVIOUS_RECORDING_IN_PROGRESS);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001171 }
1172 return super.handleRecordTvScreen(message);
1173 }
1174
1175 int recorderAddress = message.getSource();
1176 byte[] recordSource = mService.invokeRecordRequestListener(recorderAddress);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001177 int reason = startOneTouchRecord(recorderAddress, recordSource);
1178 if (reason != Constants.ABORT_NO_ERROR) {
1179 mService.maySendFeatureAbortCommand(message, reason);
1180 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001181 return true;
1182 }
1183
Jungshik Jange5a93372014-07-25 13:41:14 +09001184 @Override
1185 protected boolean handleTimerClearedStatus(HdmiCecMessage message) {
1186 byte[] params = message.getParams();
Jungshik Jange9e0f072014-08-05 17:48:59 +09001187 int timerClearedStatusData = params[0] & 0xFF;
Jungshik Jang326aef02014-11-05 12:50:35 +09001188 announceTimerRecordingResult(message.getSource(), timerClearedStatusData);
Jungshik Jange5a93372014-07-25 13:41:14 +09001189 return true;
1190 }
1191
Jungshik Jang326aef02014-11-05 12:50:35 +09001192 void announceOneTouchRecordResult(int recorderAddress, int result) {
1193 mService.invokeOneTouchRecordResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001194 }
1195
Jungshik Jang326aef02014-11-05 12:50:35 +09001196 void announceTimerRecordingResult(int recorderAddress, int result) {
1197 mService.invokeTimerRecordingResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001198 }
1199
Jungshik Jang326aef02014-11-05 12:50:35 +09001200 void announceClearTimerRecordingResult(int recorderAddress, int result) {
1201 mService.invokeClearTimerRecordingResult(recorderAddress, result);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001202 }
1203
Jungshik Jang79c58a42014-06-16 16:45:36 +09001204 private boolean isMessageForSystemAudio(HdmiCecMessage message) {
Jungshik Jang4480efa2014-09-04 17:08:34 +09001205 return mService.isControlEnabled()
1206 && message.getSource() == Constants.ADDR_AUDIO_SYSTEM
Jungshik Jang473119f2014-08-27 16:43:22 +09001207 && (message.getDestination() == Constants.ADDR_TV
1208 || message.getDestination() == Constants.ADDR_BROADCAST)
1209 && getAvrDeviceInfo() != null;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001210 }
1211
1212 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001213 * Add a new {@link HdmiDeviceInfo}. It returns old device info which has the same
Jungshik Jang79c58a42014-06-16 16:45:36 +09001214 * logical address as new device info's.
1215 *
1216 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1217 *
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001218 * @param deviceInfo a new {@link HdmiDeviceInfo} to be added.
1219 * @return {@code null} if it is new device. Otherwise, returns old {@HdmiDeviceInfo}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001220 * that has the same logical address as new one has.
1221 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001222 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001223 private HdmiDeviceInfo addDeviceInfo(HdmiDeviceInfo deviceInfo) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001224 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001225 HdmiDeviceInfo oldDeviceInfo = getCecDeviceInfo(deviceInfo.getLogicalAddress());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001226 if (oldDeviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001227 removeDeviceInfo(deviceInfo.getId());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001228 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001229 mDeviceInfos.append(deviceInfo.getId(), deviceInfo);
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001230 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001231 return oldDeviceInfo;
1232 }
1233
1234 /**
1235 * Remove a device info corresponding to the given {@code logicalAddress}.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001236 * It returns removed {@link HdmiDeviceInfo} if exists.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001237 *
1238 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1239 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001240 * @param id id of device to be removed
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001241 * @return removed {@link HdmiDeviceInfo} it exists. Otherwise, returns {@code null}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001242 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001243 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001244 private HdmiDeviceInfo removeDeviceInfo(int id) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001245 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001246 HdmiDeviceInfo deviceInfo = mDeviceInfos.get(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001247 if (deviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001248 mDeviceInfos.remove(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001249 }
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001250 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001251 return deviceInfo;
1252 }
1253
1254 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001255 * Return a list of all {@link HdmiDeviceInfo}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001256 *
1257 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
Jungshik Jang339227d2014-08-25 15:37:20 +09001258 * This is not thread-safe. For thread safety, call {@link #getSafeExternalInputsLocked} which
Jungshik Jang8e93c842014-08-06 15:48:33 +09001259 * does not include local device.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001260 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001261 @ServiceThreadOnly
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001262 List<HdmiDeviceInfo> getDeviceInfoList(boolean includeLocalDevice) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001263 assertRunOnServiceThread();
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001264 if (includeLocalDevice) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001265 return HdmiUtils.sparseArrayToList(mDeviceInfos);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001266 } else {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001267 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001268 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001269 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001270 if (!isLocalDeviceAddress(info.getLogicalAddress())) {
1271 infoList.add(info);
1272 }
1273 }
1274 return infoList;
1275 }
1276 }
1277
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001278 /**
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001279 * Return external input devices.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001280 */
Jinsuk Kimed086452014-08-18 15:01:53 +09001281 List<HdmiDeviceInfo> getSafeExternalInputsLocked() {
1282 return mSafeExternalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001283 }
1284
Jungshik Janga5b74142014-06-23 18:03:10 +09001285 @ServiceThreadOnly
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001286 private void updateSafeDeviceInfoList() {
1287 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001288 List<HdmiDeviceInfo> copiedDevices = HdmiUtils.sparseArrayToList(mDeviceInfos);
1289 List<HdmiDeviceInfo> externalInputs = getInputDevices();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001290 synchronized (mLock) {
1291 mSafeAllDeviceInfos = copiedDevices;
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001292 mSafeExternalInputs = externalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001293 }
1294 }
1295
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001296 /**
1297 * Return a list of external cec input (source) devices.
1298 *
1299 * <p>Note that this effectively excludes non-source devices like system audio,
1300 * secondary TV.
1301 */
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001302 private List<HdmiDeviceInfo> getInputDevices() {
1303 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001304 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001305 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001306 if (isLocalDeviceAddress(info.getLogicalAddress())) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001307 continue;
1308 }
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001309 if (info.isSourceType() && !hideDevicesBehindLegacySwitch(info)) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001310 infoList.add(info);
1311 }
1312 }
1313 return infoList;
1314 }
1315
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001316 // Check if we are hiding CEC devices connected to a legacy (non-CEC) switch.
1317 // Returns true if the policy is set to true, and the device to check does not have
1318 // a parent CEC device (which should be the CEC-enabled switch) in the list.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001319 private boolean hideDevicesBehindLegacySwitch(HdmiDeviceInfo info) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001320 return HdmiConfig.HIDE_DEVICES_BEHIND_LEGACY_SWITCH
1321 && !isConnectedToCecSwitch(info.getPhysicalAddress(), mCecSwitches);
1322 }
1323
1324 private static boolean isConnectedToCecSwitch(int path, Collection<Integer> switches) {
1325 for (int switchPath : switches) {
1326 if (isParentPath(switchPath, path)) {
1327 return true;
1328 }
1329 }
1330 return false;
1331 }
1332
1333 private static boolean isParentPath(int parentPath, int childPath) {
1334 // (A000, AB00) (AB00, ABC0), (ABC0, ABCD)
1335 // If child's last non-zero nibble is removed, the result equals to the parent.
1336 for (int i = 0; i <= 12; i += 4) {
1337 int nibble = (childPath >> i) & 0xF;
1338 if (nibble != 0) {
1339 int parentNibble = (parentPath >> i) & 0xF;
1340 return parentNibble == 0 && (childPath >> i+4) == (parentPath >> i+4);
1341 }
1342 }
1343 return false;
1344 }
1345
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001346 private void invokeDeviceEventListener(HdmiDeviceInfo info, int status) {
Jinsuk Kim98d760e2014-12-23 07:01:51 +09001347 if (!hideDevicesBehindLegacySwitch(info)) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001348 mService.invokeDeviceEventListeners(info, status);
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001349 }
1350 }
1351
Jungshik Jang79c58a42014-06-16 16:45:36 +09001352 private boolean isLocalDeviceAddress(int address) {
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001353 return mLocalDeviceAddresses.contains(address);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001354 }
1355
Jungshik Jange9cf1582014-06-23 17:28:58 +09001356 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001357 HdmiDeviceInfo getAvrDeviceInfo() {
Jungshik Jange9cf1582014-06-23 17:28:58 +09001358 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001359 return getCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jange9cf1582014-06-23 17:28:58 +09001360 }
1361
Jungshik Jang79c58a42014-06-16 16:45:36 +09001362 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001363 * Return a {@link HdmiDeviceInfo} corresponding to the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001364 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001365 * This is not thread-safe. For thread safety, call {@link #getSafeCecDeviceInfo(int)}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001366 *
Jungshik Jang339227d2014-08-25 15:37:20 +09001367 * @param logicalAddress logical address of the device to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001368 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001369 * Returns null if no logical address matched
1370 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001371 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001372 HdmiDeviceInfo getCecDeviceInfo(int logicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001373 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001374 return mDeviceInfos.get(HdmiDeviceInfo.idForCecDevice(logicalAddress));
Jungshik Jang79c58a42014-06-16 16:45:36 +09001375 }
1376
Jungshik Jange9cf1582014-06-23 17:28:58 +09001377 boolean hasSystemAudioDevice() {
1378 return getSafeAvrDeviceInfo() != null;
1379 }
1380
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001381 HdmiDeviceInfo getSafeAvrDeviceInfo() {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001382 return getSafeCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001383 }
1384
1385 /**
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001386 * Thread safe version of {@link #getCecDeviceInfo(int)}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001387 *
1388 * @param logicalAddress logical address to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001389 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001390 * Returns null if no logical address matched
1391 */
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001392 HdmiDeviceInfo getSafeCecDeviceInfo(int logicalAddress) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001393 synchronized (mLock) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001394 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1395 if (info.isCecDevice() && info.getLogicalAddress() == logicalAddress) {
1396 return info;
1397 }
1398 }
1399 return null;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001400 }
1401 }
1402
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001403 List<HdmiDeviceInfo> getSafeCecDevicesLocked() {
1404 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
1405 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1406 if (isLocalDeviceAddress(info.getLogicalAddress())) {
1407 continue;
1408 }
1409 infoList.add(info);
1410 }
1411 return infoList;
1412 }
1413
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001414 /**
Jungshik Jang8f2ed352014-07-07 15:02:47 +09001415 * Called when a device is newly added or a new device is detected or
1416 * existing device is updated.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001417 *
1418 * @param info device info of a new device.
1419 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001420 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001421 final void addCecDevice(HdmiDeviceInfo info) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001422 assertRunOnServiceThread();
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001423 HdmiDeviceInfo old = addDeviceInfo(info);
Jinsuk Kim13c030e2014-06-20 13:25:17 +09001424 if (info.getLogicalAddress() == mAddress) {
1425 // The addition of TV device itself should not be notified.
1426 return;
1427 }
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001428 if (old == null) {
1429 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
1430 } else if (!old.equals(info)) {
Jinsuk Kim4fcbf0b2014-12-09 06:48:13 +09001431 invokeDeviceEventListener(old, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
1432 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001433 }
Jungshik Jang79c58a42014-06-16 16:45:36 +09001434 }
1435
1436 /**
1437 * Called when a device is removed or removal of device is detected.
1438 *
1439 * @param address a logical address of a device to be removed
1440 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001441 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001442 final void removeCecDevice(int address) {
1443 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001444 HdmiDeviceInfo info = removeDeviceInfo(HdmiDeviceInfo.idForCecDevice(address));
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001445
Jungshik Jang79c58a42014-06-16 16:45:36 +09001446 mCecMessageCache.flushMessagesFrom(address);
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001447 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001448 }
1449
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001450 @ServiceThreadOnly
1451 void handleRemoveActiveRoutingPath(int path) {
1452 assertRunOnServiceThread();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001453 // Seq #23
1454 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001455 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim546d8672014-11-24 07:30:54 +09001456 startRoutingControl(getActivePath(), newPath, true, null);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001457 }
1458 }
1459
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001460 /**
1461 * Launch routing control process.
1462 *
1463 * @param routingForBootup true if routing control is initiated due to One Touch Play
1464 * or TV power on
1465 */
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001466 @ServiceThreadOnly
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001467 void launchRoutingControl(boolean routingForBootup) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001468 assertRunOnServiceThread();
1469 // Seq #24
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001470 if (getActivePortId() != Constants.INVALID_PORT_ID) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001471 if (!routingForBootup && !isProhibitMode()) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001472 int newPath = mService.portIdToPath(getActivePortId());
1473 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +09001474 startRoutingControl(getActivePath(), newPath, routingForBootup, null);
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001475 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001476 } else {
1477 int activePath = mService.getPhysicalAddress();
1478 setActivePath(activePath);
Jinsuk Kim2ab6d9f2015-01-16 15:49:16 +09001479 if (!routingForBootup
1480 && !mDelayedMessageBuffer.isBuffered(Constants.MESSAGE_ACTIVE_SOURCE)) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001481 mService.sendCecCommand(HdmiCecMessageBuilder.buildActiveSource(mAddress,
1482 activePath));
1483 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001484 }
1485 }
1486
Jungshik Jang79c58a42014-06-16 16:45:36 +09001487 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001488 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
Jungshik Jang79c58a42014-06-16 16:45:36 +09001489 * the given routing path. CEC devices use routing path for its physical address to
1490 * describe the hierarchy of the devices in the network.
1491 *
1492 * @param path routing path or physical address
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001493 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
Jungshik Jang79c58a42014-06-16 16:45:36 +09001494 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001495 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001496 final HdmiDeviceInfo getDeviceInfoByPath(int path) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001497 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001498 for (HdmiDeviceInfo info : getDeviceInfoList(false)) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001499 if (info.getPhysicalAddress() == path) {
1500 return info;
1501 }
1502 }
1503 return null;
1504 }
1505
1506 /**
Jinsuk Kim7640d982015-01-28 16:44:07 +09001507 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
1508 * the given routing path. This is the version accessible safely from threads
1509 * other than service thread.
1510 *
1511 * @param path routing path or physical address
1512 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
1513 */
1514 HdmiDeviceInfo getSafeDeviceInfoByPath(int path) {
1515 synchronized (mLock) {
1516 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1517 if (info.getPhysicalAddress() == path) {
1518 return info;
1519 }
1520 }
1521 return null;
1522 }
1523 }
1524
1525 /**
Jungshik Jang79c58a42014-06-16 16:45:36 +09001526 * Whether a device of the specified physical address and logical address exists
1527 * in a device info list. However, both are minimal condition and it could
1528 * be different device from the original one.
1529 *
Jungshik Jang79c58a42014-06-16 16:45:36 +09001530 * @param logicalAddress logical address of a device to be searched
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001531 * @param physicalAddress physical address of a device to be searched
Jungshik Jang79c58a42014-06-16 16:45:36 +09001532 * @return true if exist; otherwise false
1533 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001534 @ServiceThreadOnly
Jinsuk Kim7cd4a582015-01-29 09:50:25 +09001535 boolean isInDeviceList(int logicalAddress, int physicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001536 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001537 HdmiDeviceInfo device = getCecDeviceInfo(logicalAddress);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001538 if (device == null) {
1539 return false;
1540 }
1541 return device.getPhysicalAddress() == physicalAddress;
1542 }
1543
1544 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001545 @ServiceThreadOnly
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001546 void onHotplug(int portId, boolean connected) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001547 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001548
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001549 if (!connected) {
1550 removeCecSwitches(portId);
1551 }
Jungshik Jang79c58a42014-06-16 16:45:36 +09001552 // Tv device will have permanent HotplugDetectionAction.
1553 List<HotplugDetectionAction> hotplugActions = getActions(HotplugDetectionAction.class);
1554 if (!hotplugActions.isEmpty()) {
1555 // Note that hotplug action is single action running on a machine.
1556 // "pollAllDevicesNow" cleans up timer and start poll action immediately.
Jungshik Jang24c23c12014-07-07 18:04:39 +09001557 // It covers seq #40, #43.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001558 hotplugActions.get(0).pollAllDevicesNow();
1559 }
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +09001560 updateArcFeatureStatus(portId, connected);
Jungshik Jang60cffce2014-06-12 18:03:04 +09001561 }
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001562
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001563 private void removeCecSwitches(int portId) {
1564 Iterator<Integer> it = mCecSwitches.iterator();
1565 while (!it.hasNext()) {
1566 int path = it.next();
1567 if (pathToPortId(path) == portId) {
1568 it.remove();
1569 }
1570 }
1571 }
1572
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001573 @ServiceThreadOnly
1574 void setAutoDeviceOff(boolean enabled) {
1575 assertRunOnServiceThread();
1576 mAutoDeviceOff = enabled;
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001577 }
1578
1579 @ServiceThreadOnly
1580 void setAutoWakeup(boolean enabled) {
1581 assertRunOnServiceThread();
1582 mAutoWakeup = enabled;
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001583 }
Yuncheol Heo38db6292014-07-01 14:15:14 +09001584
Yuncheol Heo25c20292014-07-31 17:59:39 +09001585 @ServiceThreadOnly
1586 boolean getAutoWakeup() {
1587 assertRunOnServiceThread();
1588 return mAutoWakeup;
1589 }
1590
Yuncheol Heo38db6292014-07-01 14:15:14 +09001591 @Override
1592 @ServiceThreadOnly
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001593 protected void disableDevice(boolean initiatedByCec, PendingActionClearedCallback callback) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001594 assertRunOnServiceThread();
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001595 mService.unregisterTvInputCallback(mTvInputCallback);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001596 // Remove any repeated working actions.
1597 // HotplugDetectionAction will be reinstated during the wake up process.
1598 // HdmiControlService.onWakeUp() -> initializeLocalDevices() ->
1599 // LocalDeviceTv.onAddressAllocated() -> launchDeviceDiscovery().
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001600 removeAction(DeviceDiscoveryAction.class);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001601 removeAction(HotplugDetectionAction.class);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001602 removeAction(PowerStatusMonitorAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001603 // Remove recording actions.
Jungshik Jangb6591b82014-07-23 16:10:23 +09001604 removeAction(OneTouchRecordAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001605 removeAction(TimerRecordingAction.class);
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001606
1607 disableSystemAudioIfExist();
1608 disableArcIfExist();
Jinsuk Kima5445ce2015-03-30 17:14:58 +09001609
1610 super.disableDevice(initiatedByCec, callback);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +09001611 clearDeviceInfoList();
Yuncheol Heo38db6292014-07-01 14:15:14 +09001612 checkIfPendingActionsCleared();
1613 }
1614
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001615 @ServiceThreadOnly
1616 private void disableSystemAudioIfExist() {
1617 assertRunOnServiceThread();
1618 if (getAvrDeviceInfo() == null) {
1619 return;
1620 }
1621
1622 // Seq #31.
1623 removeAction(SystemAudioActionFromAvr.class);
1624 removeAction(SystemAudioActionFromTv.class);
1625 removeAction(SystemAudioAutoInitiationAction.class);
1626 removeAction(SystemAudioStatusAction.class);
1627 removeAction(VolumeControlAction.class);
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001628 }
1629
1630 @ServiceThreadOnly
1631 private void disableArcIfExist() {
1632 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001633 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001634 if (avr == null) {
1635 return;
1636 }
1637
1638 // Seq #44.
1639 removeAction(RequestArcInitiationAction.class);
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +09001640 if (!hasAction(RequestArcTerminationAction.class) && isArcEstablished()) {
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001641 addAndStartAction(new RequestArcTerminationAction(this, avr.getLogicalAddress()));
1642 }
1643 }
1644
Yuncheol Heo38db6292014-07-01 14:15:14 +09001645 @Override
1646 @ServiceThreadOnly
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001647 protected void onStandby(boolean initiatedByCec) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001648 assertRunOnServiceThread();
1649 // Seq #11
1650 if (!mService.isControlEnabled()) {
1651 return;
1652 }
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001653 if (!initiatedByCec && mAutoDeviceOff) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001654 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001655 mAddress, Constants.ADDR_BROADCAST));
Yuncheol Heo38db6292014-07-01 14:15:14 +09001656 }
1657 }
1658
Jinsuk Kim4d43d932014-07-03 16:43:58 +09001659 boolean isProhibitMode() {
1660 return mService.isProhibitMode();
1661 }
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001662
1663 boolean isPowerStandbyOrTransient() {
1664 return mService.isPowerStandbyOrTransient();
Jungshik Jang8866c812014-07-08 14:42:28 +09001665 }
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001666
Jungshik Jang339227d2014-08-25 15:37:20 +09001667 @ServiceThreadOnly
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001668 void displayOsd(int messageId) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001669 assertRunOnServiceThread();
1670 mService.displayOsd(messageId);
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001671 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001672
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001673 @ServiceThreadOnly
1674 void displayOsd(int messageId, int extra) {
1675 assertRunOnServiceThread();
1676 mService.displayOsd(messageId, extra);
1677 }
1678
Jungshik Jangb6591b82014-07-23 16:10:23 +09001679 // Seq #54 and #55
1680 @ServiceThreadOnly
Jungshik Jang4480efa2014-09-04 17:08:34 +09001681 int startOneTouchRecord(int recorderAddress, byte[] recordSource) {
Jungshik Jangb6591b82014-07-23 16:10:23 +09001682 assertRunOnServiceThread();
1683 if (!mService.isControlEnabled()) {
1684 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001685 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001686 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001687 }
1688
1689 if (!checkRecorder(recorderAddress)) {
1690 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001691 announceOneTouchRecordResult(recorderAddress,
1692 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001693 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001694 }
1695
1696 if (!checkRecordSource(recordSource)) {
1697 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001698 announceOneTouchRecordResult(recorderAddress,
1699 ONE_TOUCH_RECORD_FAIL_TO_RECORD_DISPLAYED_SCREEN);
Jinsuk Kimd47abef2015-01-17 07:38:24 +09001700 return Constants.ABORT_CANNOT_PROVIDE_SOURCE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001701 }
1702
1703 addAndStartAction(new OneTouchRecordAction(this, recorderAddress, recordSource));
1704 Slog.i(TAG, "Start new [One Touch Record]-Target:" + recorderAddress + ", recordSource:"
1705 + Arrays.toString(recordSource));
Jungshik Jang4480efa2014-09-04 17:08:34 +09001706 return Constants.ABORT_NO_ERROR;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001707 }
1708
1709 @ServiceThreadOnly
1710 void stopOneTouchRecord(int recorderAddress) {
1711 assertRunOnServiceThread();
1712 if (!mService.isControlEnabled()) {
1713 Slog.w(TAG, "Can not stop one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001714 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001715 return;
1716 }
1717
1718 if (!checkRecorder(recorderAddress)) {
1719 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001720 announceOneTouchRecordResult(recorderAddress,
1721 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001722 return;
1723 }
1724
1725 // Remove one touch record action so that other one touch record can be started.
1726 removeAction(OneTouchRecordAction.class);
1727 mService.sendCecCommand(HdmiCecMessageBuilder.buildRecordOff(mAddress, recorderAddress));
1728 Slog.i(TAG, "Stop [One Touch Record]-Target:" + recorderAddress);
1729 }
1730
1731 private boolean checkRecorder(int recorderAddress) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001732 HdmiDeviceInfo device = getCecDeviceInfo(recorderAddress);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001733 return (device != null)
1734 && (HdmiUtils.getTypeFromAddress(recorderAddress)
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001735 == HdmiDeviceInfo.DEVICE_RECORDER);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001736 }
1737
1738 private boolean checkRecordSource(byte[] recordSource) {
1739 return (recordSource != null) && HdmiRecordSources.checkRecordSource(recordSource);
1740 }
1741
1742 @ServiceThreadOnly
1743 void startTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1744 assertRunOnServiceThread();
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001745 if (!mService.isControlEnabled()) {
1746 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001747 announceTimerRecordingResult(recorderAddress,
1748 TIMER_RECORDING_RESULT_EXTRA_CEC_DISABLED);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001749 return;
1750 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001751
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001752 if (!checkRecorder(recorderAddress)) {
1753 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001754 announceTimerRecordingResult(recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001755 TIMER_RECORDING_RESULT_EXTRA_CHECK_RECORDER_CONNECTION);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001756 return;
1757 }
1758
1759 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1760 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
1761 announceTimerRecordingResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001762 recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001763 TIMER_RECORDING_RESULT_EXTRA_FAIL_TO_RECORD_SELECTED_SOURCE);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001764 return;
1765 }
1766
1767 addAndStartAction(
1768 new TimerRecordingAction(this, recorderAddress, sourceType, recordSource));
1769 Slog.i(TAG, "Start [Timer Recording]-Target:" + recorderAddress + ", SourceType:"
1770 + sourceType + ", RecordSource:" + Arrays.toString(recordSource));
1771 }
1772
1773 private boolean checkTimerRecordingSource(int sourceType, byte[] recordSource) {
1774 return (recordSource != null)
1775 && HdmiTimerRecordSources.checkTimerRecordSource(sourceType, recordSource);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001776 }
1777
1778 @ServiceThreadOnly
1779 void clearTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1780 assertRunOnServiceThread();
Jungshik Jange5a93372014-07-25 13:41:14 +09001781 if (!mService.isControlEnabled()) {
1782 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001783 announceClearTimerRecordingResult(recorderAddress, CLEAR_TIMER_STATUS_CEC_DISABLE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001784 return;
1785 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001786
Jungshik Jange5a93372014-07-25 13:41:14 +09001787 if (!checkRecorder(recorderAddress)) {
1788 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001789 announceClearTimerRecordingResult(recorderAddress,
1790 CLEAR_TIMER_STATUS_CHECK_RECORDER_CONNECTION);
Jungshik Jange5a93372014-07-25 13:41:14 +09001791 return;
1792 }
1793
1794 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1795 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001796 announceClearTimerRecordingResult(recorderAddress,
1797 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001798 return;
1799 }
1800
1801 sendClearTimerMessage(recorderAddress, sourceType, recordSource);
1802 }
1803
Jungshik Jang326aef02014-11-05 12:50:35 +09001804 private void sendClearTimerMessage(final int recorderAddress, int sourceType,
1805 byte[] recordSource) {
Jungshik Jange5a93372014-07-25 13:41:14 +09001806 HdmiCecMessage message = null;
1807 switch (sourceType) {
1808 case TIMER_RECORDING_TYPE_DIGITAL:
1809 message = HdmiCecMessageBuilder.buildClearDigitalTimer(mAddress, recorderAddress,
1810 recordSource);
1811 break;
1812 case TIMER_RECORDING_TYPE_ANALOGUE:
1813 message = HdmiCecMessageBuilder.buildClearAnalogueTimer(mAddress, recorderAddress,
1814 recordSource);
1815 break;
1816 case TIMER_RECORDING_TYPE_EXTERNAL:
1817 message = HdmiCecMessageBuilder.buildClearExternalTimer(mAddress, recorderAddress,
1818 recordSource);
1819 break;
1820 default:
1821 Slog.w(TAG, "Invalid source type:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001822 announceClearTimerRecordingResult(recorderAddress,
1823 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001824 return;
1825
1826 }
1827 mService.sendCecCommand(message, new SendMessageCallback() {
1828 @Override
1829 public void onSendCompleted(int error) {
1830 if (error != Constants.SEND_RESULT_SUCCESS) {
Jungshik Jang326aef02014-11-05 12:50:35 +09001831 announceClearTimerRecordingResult(recorderAddress,
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001832 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001833 }
1834 }
1835 });
Jungshik Jangb6591b82014-07-23 16:10:23 +09001836 }
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001837
1838 void updateDevicePowerStatus(int logicalAddress, int newPowerStatus) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001839 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001840 if (info == null) {
1841 Slog.w(TAG, "Can not update power status of non-existing device:" + logicalAddress);
1842 return;
1843 }
1844
1845 if (info.getDevicePowerStatus() == newPowerStatus) {
1846 return;
1847 }
1848
1849 HdmiDeviceInfo newInfo = HdmiUtils.cloneHdmiDeviceInfo(info, newPowerStatus);
1850 // addDeviceInfo replaces old device info with new one if exists.
1851 addDeviceInfo(newInfo);
1852
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001853 invokeDeviceEventListener(newInfo, HdmiControlManager.DEVICE_EVENT_UPDATE_DEVICE);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001854 }
Terry Heo959d2db2014-08-28 16:45:41 +09001855
1856 @Override
Yuncheol Heo184b1242014-09-12 15:09:07 +09001857 protected boolean handleMenuStatus(HdmiCecMessage message) {
1858 // Do nothing and just return true not to prevent from responding <Feature Abort>.
1859 return true;
1860 }
1861
1862 @Override
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001863 protected void sendStandby(int deviceId) {
1864 HdmiDeviceInfo targetDevice = mDeviceInfos.get(deviceId);
1865 if (targetDevice == null) {
1866 return;
1867 }
1868 int targetAddress = targetDevice.getLogicalAddress();
1869 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(mAddress, targetAddress));
1870 }
1871
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001872 @ServiceThreadOnly
1873 void processAllDelayedMessages() {
1874 assertRunOnServiceThread();
1875 mDelayedMessageBuffer.processAllMessages();
1876 }
1877
1878 @ServiceThreadOnly
1879 void processDelayedMessages(int address) {
1880 assertRunOnServiceThread();
1881 mDelayedMessageBuffer.processMessagesForDevice(address);
1882 }
1883
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +09001884 @ServiceThreadOnly
1885 void processDelayedActiveSource(int address) {
1886 assertRunOnServiceThread();
1887 mDelayedMessageBuffer.processActiveSource(address);
1888 }
1889
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001890 @Override
Terry Heo959d2db2014-08-28 16:45:41 +09001891 protected void dump(final IndentingPrintWriter pw) {
1892 super.dump(pw);
1893 pw.println("mArcEstablished: " + mArcEstablished);
1894 pw.println("mArcFeatureEnabled: " + mArcFeatureEnabled);
1895 pw.println("mSystemAudioActivated: " + mSystemAudioActivated);
1896 pw.println("mSystemAudioMute: " + mSystemAudioMute);
1897 pw.println("mAutoDeviceOff: " + mAutoDeviceOff);
1898 pw.println("mAutoWakeup: " + mAutoWakeup);
1899 pw.println("mSkipRoutingControl: " + mSkipRoutingControl);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +09001900 pw.println("mPrevPortId: " + mPrevPortId);
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001901 pw.println("CEC devices:");
1902 pw.increaseIndent();
1903 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1904 pw.println(info);
1905 }
1906 pw.decreaseIndent();
Terry Heo959d2db2014-08-28 16:45:41 +09001907 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +09001908}