blob: 0ac4f9ee4c6d729445b21a5fe7d3960b3a112cc0 [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;
Donghyun Chobc6e3722016-11-04 05:25:52 +090039import android.hardware.tv.cec.V1_0.SendMessageResult;
Jungshik Jangb69aafbf2014-07-11 16:29:06 +090040import android.media.AudioManager;
Jungshik Janga858d222014-06-23 17:17:47 +090041import android.media.AudioSystem;
Jinsuk Kim7fa3a662014-11-07 15:20:24 +090042import android.media.tv.TvInputInfo;
Jinsuk Kim7fa3a662014-11-07 15:20:24 +090043import android.media.tv.TvInputManager.TvInputCallback;
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;
Amy123ec402018-09-25 10:56:31 -070049
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;
Amy123ec402018-09-25 10:56:31 -070055
Jungshik Jang8f2ed352014-07-07 15:02:47 +090056import java.io.UnsupportedEncodingException;
Jungshik Jang79c58a42014-06-16 16:45:36 +090057import java.util.ArrayList;
Jungshik Jangb6591b82014-07-23 16:10:23 +090058import java.util.Arrays;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090059import java.util.Collection;
Jinsuk Kim0a3316b2014-06-14 09:33:55 +090060import java.util.Collections;
Donghyun Chobc6e3722016-11-04 05:25:52 +090061import java.util.HashMap;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090062import java.util.Iterator;
Jinsuk Kim0a3316b2014-06-14 09:33:55 +090063import java.util.List;
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090064
65/**
66 * Represent a logical device of type TV residing in Android system.
67 */
68final class HdmiCecLocalDeviceTv extends HdmiCecLocalDevice {
Jungshik Jang092b4452014-06-11 15:19:17 +090069 private static final String TAG = "HdmiCecLocalDeviceTv";
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090070
Jungshik Jang12e5dce2014-07-24 15:27:44 +090071 // Whether ARC is available or not. "true" means that ARC is established between TV and
Jungshik Janga13da0d2014-06-30 16:26:06 +090072 // AVR as audio receiver.
73 @ServiceThreadOnly
74 private boolean mArcEstablished = false;
75
Donghyun Choc1fa9af2016-12-27 18:31:09 +090076 // Stores whether ARC feature is enabled per port.
77 // True by default for all the ARC-enabled ports.
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +090078 private final SparseBooleanArray mArcFeatureEnabled = new SparseBooleanArray();
Jungshik Jang79c58a42014-06-16 16:45:36 +090079
Donghyun Choc1fa9af2016-12-27 18:31:09 +090080 // Whether the System Audio Control feature is enabled or not. True by default.
81 @GuardedBy("mLock")
82 private boolean mSystemAudioControlFeatureEnabled;
83
Jinsuk Kim83335712014-06-24 07:57:00 +090084 // The previous port id (input) before switching to the new one. This is remembered in order to
85 // be able to switch to it upon receiving <Inactive Source> from currently active source.
86 // This remains valid only when the active source was switched via one touch play operation
87 // (either by TV or source device). Manual port switching invalidates this value to
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090088 // Constants.PORT_INVALID, for which case <Inactive Source> does not do anything.
Jinsuk Kim83335712014-06-24 07:57:00 +090089 @GuardedBy("mLock")
90 private int mPrevPortId;
91
Jungshik Jang8fa36b12014-06-25 15:51:36 +090092 @GuardedBy("mLock")
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090093 private int mSystemAudioVolume = Constants.UNKNOWN_VOLUME;
Jungshik Jang8fa36b12014-06-25 15:51:36 +090094
95 @GuardedBy("mLock")
96 private boolean mSystemAudioMute = false;
97
Jungshik Jangfa8e90d2014-06-23 14:40:32 +090098 // Copy of mDeviceInfos to guarantee thread-safety.
99 @GuardedBy("mLock")
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900100 private List<HdmiDeviceInfo> mSafeAllDeviceInfos = Collections.emptyList();
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +0900101 // All external cec input(source) devices. Does not include system audio device.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900102 @GuardedBy("mLock")
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900103 private List<HdmiDeviceInfo> mSafeExternalInputs = Collections.emptyList();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900104
Jungshik Jang79c58a42014-06-16 16:45:36 +0900105 // Map-like container of all cec devices including local ones.
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900106 // device id is used as key of container.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900107 // This is not thread-safe. For external purpose use mSafeDeviceInfos.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900108 private final SparseArray<HdmiDeviceInfo> mDeviceInfos = new SparseArray<>();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900109
Jinsuk Kim160a6e52014-07-02 06:16:36 +0900110 // If true, TV going to standby mode puts other devices also to standby.
111 private boolean mAutoDeviceOff;
112
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900113 // If true, TV wakes itself up when receiving <Text/Image View On>.
114 private boolean mAutoWakeup;
115
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900116 // List of the logical address of local CEC devices. Unmodifiable, thread-safe.
117 private List<Integer> mLocalDeviceAddresses;
118
Yuncheol Heo25c20292014-07-31 17:59:39 +0900119 private final HdmiCecStandbyModeHandler mStandbyHandler;
120
Jinsuk Kimd5307192014-08-22 09:07:40 +0900121 // If true, do not do routing control/send active source for internal source.
122 // Set to true when the device was woken up by <Text/Image View On>.
123 private boolean mSkipRoutingControl;
124
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900125 // Set of physical addresses of CEC switches on the CEC bus. Managed independently from
126 // other CEC devices since they might not have logical address.
127 private final ArraySet<Integer> mCecSwitches = new ArraySet<Integer>();
128
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900129 // Message buffer used to buffer selected messages to process later. <Active Source>
130 // from a source device, for instance, needs to be buffered if the device is not
131 // discovered yet. The buffered commands are taken out and when they are ready to
132 // handle.
133 private final DelayedMessageBuffer mDelayedMessageBuffer = new DelayedMessageBuffer(this);
134
135 // Defines the callback invoked when TV input framework is updated with input status.
136 // We are interested in the notification for HDMI input addition event, in order to
137 // process any CEC commands that arrived before the input is added.
138 private final TvInputCallback mTvInputCallback = new TvInputCallback() {
139 @Override
140 public void onInputAdded(String inputId) {
141 TvInputInfo tvInfo = mService.getTvInputManager().getTvInputInfo(inputId);
Terry Heoa22d77e2015-11-05 15:53:33 +0900142 if (tvInfo == null) return;
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900143 HdmiDeviceInfo info = tvInfo.getHdmiDeviceInfo();
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900144 if (info == null) return;
145 addTvInput(inputId, info.getId());
146 if (info.isCecDevice()) {
147 processDelayedActiveSource(info.getLogicalAddress());
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900148 }
149 }
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900150
151 @Override
152 public void onInputRemoved(String inputId) {
153 removeTvInput(inputId);
154 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900155 };
156
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900157 // Keeps the mapping (TV input ID, HDMI device ID) to keep track of the TV inputs ready to
158 // accept input switching request from HDMI devices. Requests for which the corresponding
159 // input ID is not yet registered by TV input framework need to be buffered for delayed
160 // processing.
161 private final HashMap<String, Integer> mTvInputs = new HashMap<>();
162
163 @ServiceThreadOnly
164 private void addTvInput(String inputId, int deviceId) {
165 assertRunOnServiceThread();
166 mTvInputs.put(inputId, deviceId);
167 }
168
169 @ServiceThreadOnly
170 private void removeTvInput(String inputId) {
171 assertRunOnServiceThread();
172 mTvInputs.remove(inputId);
173 }
174
175 @Override
176 @ServiceThreadOnly
177 protected boolean isInputReady(int deviceId) {
178 assertRunOnServiceThread();
179 return mTvInputs.containsValue(deviceId);
180 }
181
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900182 private SelectRequestBuffer mSelectRequestBuffer;
183
Jungshik Jang3ee65722014-06-03 16:22:30 +0900184 HdmiCecLocalDeviceTv(HdmiControlService service) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900185 super(service, HdmiDeviceInfo.DEVICE_TV);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900186 mPrevPortId = Constants.INVALID_PORT_ID;
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900187 mAutoDeviceOff = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED,
188 true);
189 mAutoWakeup = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_WAKEUP_ENABLED, true);
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900190 mSystemAudioControlFeatureEnabled =
191 mService.readBooleanSetting(Global.HDMI_SYSTEM_AUDIO_CONTROL_ENABLED, true);
Yuncheol Heo25c20292014-07-31 17:59:39 +0900192 mStandbyHandler = new HdmiCecStandbyModeHandler(service, this);
Jungshik Jang8b308d92014-05-29 21:52:28 +0900193 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900194
Jungshik Jang8b308d92014-05-29 21:52:28 +0900195 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900196 @ServiceThreadOnly
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900197 protected void onAddressAllocated(int logicalAddress, int reason) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900198 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900199 List<HdmiPortInfo> ports = mService.getPortInfo();
200 for (HdmiPortInfo port : ports) {
201 mArcFeatureEnabled.put(port.getId(), port.isArcSupported());
202 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900203 mService.registerTvInputCallback(mTvInputCallback);
Jungshik Jang3ee65722014-06-03 16:22:30 +0900204 mService.sendCecCommand(HdmiCecMessageBuilder.buildReportPhysicalAddressCommand(
205 mAddress, mService.getPhysicalAddress(), mDeviceType));
206 mService.sendCecCommand(HdmiCecMessageBuilder.buildDeviceVendorIdCommand(
207 mAddress, mService.getVendorId()));
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900208 mCecSwitches.add(mService.getPhysicalAddress()); // TV is a CEC switch too.
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900209 mTvInputs.clear();
Jinsuk Kimd5307192014-08-22 09:07:40 +0900210 mSkipRoutingControl = (reason == HdmiControlService.INITIATED_BY_WAKE_UP_MESSAGE);
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900211 launchRoutingControl(reason != HdmiControlService.INITIATED_BY_ENABLE_CEC &&
212 reason != HdmiControlService.INITIATED_BY_BOOT_UP);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900213 mLocalDeviceAddresses = initLocalDeviceAddresses();
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900214 resetSelectRequestBuffer();
Jungshik Jang60cffce2014-06-12 18:03:04 +0900215 launchDeviceDiscovery();
Jungshik Jang6f34f5a2014-07-08 21:17:29 +0900216 }
217
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900218
219 @ServiceThreadOnly
220 private List<Integer> initLocalDeviceAddresses() {
221 assertRunOnServiceThread();
222 List<Integer> addresses = new ArrayList<>();
223 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
224 addresses.add(device.getDeviceInfo().getLogicalAddress());
225 }
226 return Collections.unmodifiableList(addresses);
227 }
228
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900229
230 @ServiceThreadOnly
231 public void setSelectRequestBuffer(SelectRequestBuffer requestBuffer) {
232 assertRunOnServiceThread();
233 mSelectRequestBuffer = requestBuffer;
234 }
235
236 @ServiceThreadOnly
237 private void resetSelectRequestBuffer() {
238 assertRunOnServiceThread();
239 setSelectRequestBuffer(SelectRequestBuffer.EMPTY_BUFFER);
240 }
241
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900242 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900243 protected int getPreferredAddress() {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900244 return Constants.ADDR_TV;
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900245 }
246
247 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900248 protected void setPreferredAddress(int addr) {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900249 Slog.w(TAG, "Preferred addres will not be stored for TV");
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900250 }
251
Yuncheol Heo25c20292014-07-31 17:59:39 +0900252 @Override
253 @ServiceThreadOnly
254 boolean dispatchMessage(HdmiCecMessage message) {
255 assertRunOnServiceThread();
Donghyun Choafd26a22016-12-23 15:53:28 +0900256 if (mService.isPowerStandby() && !mService.isWakeUpMessageReceived()
257 && mStandbyHandler.handleCommand(message)) {
Yuncheol Heo25c20292014-07-31 17:59:39 +0900258 return true;
259 }
260 return super.onMessage(message);
261 }
262
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900263 /**
264 * Performs the action 'device select', or 'one touch play' initiated by TV.
265 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900266 * @param id id of HDMI device to select
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900267 * @param callback callback object to report the result with
268 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900269 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900270 void deviceSelect(int id, IHdmiControlCallback callback) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900271 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900272 HdmiDeviceInfo targetDevice = mDeviceInfos.get(id);
273 if (targetDevice == null) {
274 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
275 return;
276 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900277 int targetAddress = targetDevice.getLogicalAddress();
Jinsuk Kim58500f42014-08-05 12:48:35 +0900278 ActiveSource active = getActiveSource();
Yu.Ishihara6b2a6172015-03-25 09:44:06 +0900279 if (targetDevice.getDevicePowerStatus() == HdmiControlManager.POWER_STATUS_ON
280 && active.isValid()
281 && targetAddress == active.logicalAddress) {
Jinsuk Kim58500f42014-08-05 12:48:35 +0900282 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
283 return;
284 }
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900285 if (targetAddress == Constants.ADDR_INTERNAL) {
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900286 handleSelectInternalSource();
287 // Switching to internal source is always successful even when CEC control is disabled.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900288 setActiveSource(targetAddress, mService.getPhysicalAddress());
Jinsuk Kim7e742062014-07-30 13:19:13 +0900289 setActivePath(mService.getPhysicalAddress());
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900290 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
Jinsuk Kim83335712014-06-24 07:57:00 +0900291 return;
292 }
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900293 if (!mService.isControlEnabled()) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900294 setActiveSource(targetDevice);
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900295 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
296 return;
297 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900298 removeAction(DeviceSelectAction.class);
299 addAndStartAction(new DeviceSelectAction(this, targetDevice, callback));
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900300 }
301
Jungshik Janga5b74142014-06-23 18:03:10 +0900302 @ServiceThreadOnly
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900303 private void handleSelectInternalSource() {
Jinsuk Kima062a932014-06-18 10:00:39 +0900304 assertRunOnServiceThread();
Jinsuk Kim83335712014-06-24 07:57:00 +0900305 // Seq #18
Amy123ec402018-09-25 10:56:31 -0700306 if (mService.isControlEnabled() && getActiveSource().logicalAddress != mAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900307 updateActiveSource(mAddress, mService.getPhysicalAddress());
Jinsuk Kimd5307192014-08-22 09:07:40 +0900308 if (mSkipRoutingControl) {
309 mSkipRoutingControl = false;
310 return;
311 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900312 HdmiCecMessage activeSource = HdmiCecMessageBuilder.buildActiveSource(
313 mAddress, mService.getPhysicalAddress());
314 mService.sendCecCommand(activeSource);
315 }
316 }
317
318 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900319 void updateActiveSource(int logicalAddress, int physicalAddress) {
320 assertRunOnServiceThread();
321 updateActiveSource(ActiveSource.of(logicalAddress, physicalAddress));
322 }
323
324 @ServiceThreadOnly
325 void updateActiveSource(ActiveSource newActive) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900326 assertRunOnServiceThread();
327 // Seq #14
Amy123ec402018-09-25 10:56:31 -0700328 if (getActiveSource().equals(newActive)) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900329 return;
330 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900331 setActiveSource(newActive);
332 int logicalAddress = newActive.logicalAddress;
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900333 if (getCecDeviceInfo(logicalAddress) != null && logicalAddress != mAddress) {
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900334 if (mService.pathToPortId(newActive.physicalAddress) == getActivePortId()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900335 setPrevPortId(getActivePortId());
336 }
337 // TODO: Show the OSD banner related to the new active source device.
338 } else {
339 // TODO: If displayed, remove the OSD banner related to the previous
340 // active source device.
341 }
342 }
343
344 /**
345 * Returns the previous port id kept to handle input switching on <Inactive Source>.
346 */
347 int getPrevPortId() {
348 synchronized (mLock) {
349 return mPrevPortId;
350 }
351 }
352
353 /**
354 * Sets the previous port id. INVALID_PORT_ID invalidates it, hence no actions will be
355 * taken for <Inactive Source>.
356 */
357 void setPrevPortId(int portId) {
358 synchronized (mLock) {
359 mPrevPortId = portId;
360 }
361 }
362
363 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900364 void updateActiveInput(int path, boolean notifyInputChange) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900365 assertRunOnServiceThread();
366 // Seq #15
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900367 setActivePath(path);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900368 // TODO: Handle PAP/PIP case.
369 // Show OSD port change banner
370 if (notifyInputChange) {
371 ActiveSource activeSource = getActiveSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900372 HdmiDeviceInfo info = getCecDeviceInfo(activeSource.logicalAddress);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900373 if (info == null) {
Jinsuk Kim6ad7cbd2015-01-06 11:30:56 +0900374 info = mService.getDeviceInfoByPort(getActivePortId());
375 if (info == null) {
376 // No CEC/MHL device is present at the port. Attempt to switch to
377 // the hardware port itself for non-CEC devices that may be connected.
378 info = new HdmiDeviceInfo(path, getActivePortId());
379 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900380 }
381 mService.invokeInputChangeListener(info);
382 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900383 }
384
385 @ServiceThreadOnly
386 void doManualPortSwitching(int portId, IHdmiControlCallback callback) {
387 assertRunOnServiceThread();
388 // Seq #20
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900389 if (!mService.isValidPortId(portId)) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900390 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
Jinsuk Kima062a932014-06-18 10:00:39 +0900391 return;
392 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900393 if (portId == getActivePortId()) {
394 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
395 return;
396 }
Amy123ec402018-09-25 10:56:31 -0700397 getActiveSource().invalidate();
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900398 if (!mService.isControlEnabled()) {
399 setActivePortId(portId);
400 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
401 return;
402 }
Jinsuk Kim1f8d1c572014-08-12 15:39:53 +0900403 int oldPath = getActivePortId() != Constants.INVALID_PORT_ID
404 ? mService.portIdToPath(getActivePortId()) : getDeviceInfo().getPhysicalAddress();
Jinsuk Kimd5307192014-08-22 09:07:40 +0900405 setActivePath(oldPath);
406 if (mSkipRoutingControl) {
407 mSkipRoutingControl = false;
408 return;
409 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900410 int newPath = mService.portIdToPath(portId);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900411 startRoutingControl(oldPath, newPath, true, callback);
412 }
413
414 @ServiceThreadOnly
415 void startRoutingControl(int oldPath, int newPath, boolean queryDevicePowerStatus,
416 IHdmiControlCallback callback) {
417 assertRunOnServiceThread();
418 if (oldPath == newPath) {
419 return;
420 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900421 HdmiCecMessage routingChange =
422 HdmiCecMessageBuilder.buildRoutingChange(mAddress, oldPath, newPath);
423 mService.sendCecCommand(routingChange);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900424 removeAction(RoutingControlAction.class);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900425 addAndStartAction(
426 new RoutingControlAction(this, newPath, queryDevicePowerStatus, callback));
Jinsuk Kima062a932014-06-18 10:00:39 +0900427 }
428
Jungshik Jangf67113f2014-08-22 16:27:19 +0900429 @ServiceThreadOnly
Jinsuk Kimcae66272014-07-04 13:26:44 +0900430 int getPowerStatus() {
Jungshik Jangf67113f2014-08-22 16:27:19 +0900431 assertRunOnServiceThread();
Jinsuk Kimcae66272014-07-04 13:26:44 +0900432 return mService.getPowerStatus();
433 }
434
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900435 @Override
Donghyun Cho7609bc32016-12-02 15:31:52 +0900436 protected int findKeyReceiverAddress() {
Jinsuk Kim75434972014-09-25 13:52:50 +0900437 if (getActiveSource().isValid()) {
438 return getActiveSource().logicalAddress;
439 }
440 HdmiDeviceInfo info = getDeviceInfoByPath(getActivePath());
441 if (info != null) {
442 return info.getLogicalAddress();
443 }
444 return Constants.ADDR_INVALID;
445 }
446
Jungshik Jang092b4452014-06-11 15:19:17 +0900447 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900448 @ServiceThreadOnly
Jinsuk Kim83335712014-06-24 07:57:00 +0900449 protected boolean handleActiveSource(HdmiCecMessage message) {
450 assertRunOnServiceThread();
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900451 int logicalAddress = message.getSource();
452 int physicalAddress = HdmiUtils.twoBytesToInt(message.getParams());
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900453 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
454 if (info == null) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900455 if (!handleNewDeviceAtTheTailOfActivePath(physicalAddress)) {
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900456 HdmiLogger.debug("Device info %X not found; buffering the command", logicalAddress);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900457 mDelayedMessageBuffer.add(message);
458 }
Donghyun Cho455f09d2016-05-10 20:06:52 +0900459 } else if (isInputReady(info.getId())
460 || info.getDeviceType() == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) {
Jinsuk Kim7cc51c62015-04-30 10:39:35 +0900461 updateDevicePowerStatus(logicalAddress, HdmiControlManager.POWER_STATUS_ON);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900462 ActiveSource activeSource = ActiveSource.of(logicalAddress, physicalAddress);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900463 ActiveSourceHandler.create(this, null).process(activeSource, info.getDeviceType());
Donghyun Cho455f09d2016-05-10 20:06:52 +0900464 } else {
465 HdmiLogger.debug("Input not ready for device: %X; buffering the command", info.getId());
466 mDelayedMessageBuffer.add(message);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900467 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900468 return true;
469 }
470
471 @Override
472 @ServiceThreadOnly
473 protected boolean handleInactiveSource(HdmiCecMessage message) {
474 assertRunOnServiceThread();
475 // Seq #10
476
477 // Ignore <Inactive Source> from non-active source device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900478 if (getActiveSource().logicalAddress != message.getSource()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900479 return true;
480 }
Jinsuk Kim4d43d932014-07-03 16:43:58 +0900481 if (isProhibitMode()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900482 return true;
483 }
484 int portId = getPrevPortId();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900485 if (portId != Constants.INVALID_PORT_ID) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900486 // TODO: Do this only if TV is not showing multiview like PIP/PAP.
487
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900488 HdmiDeviceInfo inactiveSource = getCecDeviceInfo(message.getSource());
Jinsuk Kim83335712014-06-24 07:57:00 +0900489 if (inactiveSource == null) {
490 return true;
491 }
492 if (mService.pathToPortId(inactiveSource.getPhysicalAddress()) == portId) {
493 return true;
494 }
495 // TODO: Switch the TV freeze mode off
496
Jinsuk Kim83335712014-06-24 07:57:00 +0900497 doManualPortSwitching(portId, null);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900498 setPrevPortId(Constants.INVALID_PORT_ID);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900499 } else {
500 // No HDMI port to switch to was found. Notify the input change listers to
501 // switch to the lastly shown internal input.
Amy123ec402018-09-25 10:56:31 -0700502 getActiveSource().invalidate();
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900503 setActivePath(Constants.INVALID_PHYSICAL_ADDRESS);
504 mService.invokeInputChangeListener(HdmiDeviceInfo.INACTIVE_DEVICE);
Jinsuk Kim83335712014-06-24 07:57:00 +0900505 }
506 return true;
507 }
508
509 @Override
510 @ServiceThreadOnly
511 protected boolean handleRequestActiveSource(HdmiCecMessage message) {
512 assertRunOnServiceThread();
513 // Seq #19
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900514 if (mAddress == getActiveSource().logicalAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900515 mService.sendCecCommand(
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900516 HdmiCecMessageBuilder.buildActiveSource(mAddress, getActivePath()));
Jinsuk Kim83335712014-06-24 07:57:00 +0900517 }
518 return true;
519 }
520
521 @Override
522 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900523 protected boolean handleGetMenuLanguage(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900524 assertRunOnServiceThread();
Jungshik Jangf67113f2014-08-22 16:27:19 +0900525 if (!broadcastMenuLanguage(mService.getLanguage())) {
Jungshik Jang092b4452014-06-11 15:19:17 +0900526 Slog.w(TAG, "Failed to respond to <Get Menu Language>: " + message.toString());
527 }
528 return true;
529 }
530
Terry Heo1ca0a432014-08-18 10:30:32 +0900531 @ServiceThreadOnly
532 boolean broadcastMenuLanguage(String language) {
533 assertRunOnServiceThread();
534 HdmiCecMessage command = HdmiCecMessageBuilder.buildSetMenuLanguageCommand(
535 mAddress, language);
536 if (command != null) {
537 mService.sendCecCommand(command);
538 return true;
539 }
540 return false;
541 }
542
Jungshik Jang60cffce2014-06-12 18:03:04 +0900543 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900544 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900545 protected boolean handleReportPhysicalAddress(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900546 assertRunOnServiceThread();
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900547 int path = HdmiUtils.twoBytesToInt(message.getParams());
548 int address = message.getSource();
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900549 int type = message.getParams()[2];
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900550
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900551 if (updateCecSwitchInfo(address, type, path)) return true;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900552
Jungshik Jang092b4452014-06-11 15:19:17 +0900553 // Ignore if [Device Discovery Action] is going on.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900554 if (hasAction(DeviceDiscoveryAction.class)) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900555 Slog.i(TAG, "Ignored while Device Discovery Action is in progress: " + message);
Jungshik Jang092b4452014-06-11 15:19:17 +0900556 return true;
557 }
558
Jinsuk Kim4b4b9402014-09-02 10:30:10 +0900559 if (!isInDeviceList(address, path)) {
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900560 handleNewDeviceAtTheTailOfActivePath(path);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900561 }
Jinsuk Kim7cd4a582015-01-29 09:50:25 +0900562
563 // Add the device ahead with default information to handle <Active Source>
564 // promptly, rather than waiting till the new device action is finished.
565 HdmiDeviceInfo deviceInfo = new HdmiDeviceInfo(address, path, getPortId(path), type,
566 Constants.UNKNOWN_VENDOR_ID, HdmiUtils.getDefaultDeviceName(address));
567 addCecDevice(deviceInfo);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900568 startNewDeviceAction(ActiveSource.of(address, path), type);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900569 return true;
570 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900571
Jungshik Jang4480efa2014-09-04 17:08:34 +0900572 @Override
573 protected boolean handleReportPowerStatus(HdmiCecMessage command) {
574 int newStatus = command.getParams()[0] & 0xFF;
575 updateDevicePowerStatus(command.getSource(), newStatus);
576 return true;
577 }
578
579 @Override
580 protected boolean handleTimerStatus(HdmiCecMessage message) {
581 // Do nothing.
582 return true;
583 }
584
585 @Override
586 protected boolean handleRecordStatus(HdmiCecMessage message) {
587 // Do nothing.
588 return true;
589 }
590
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900591 boolean updateCecSwitchInfo(int address, int type, int path) {
592 if (address == Constants.ADDR_UNREGISTERED
593 && type == HdmiDeviceInfo.DEVICE_PURE_CEC_SWITCH) {
594 mCecSwitches.add(path);
595 updateSafeDeviceInfoList();
596 return true; // Pure switch does not need further processing. Return here.
597 }
598 if (type == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) {
599 mCecSwitches.add(path);
600 }
601 return false;
602 }
603
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900604 void startNewDeviceAction(ActiveSource activeSource, int deviceType) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900605 for (NewDeviceAction action : getActions(NewDeviceAction.class)) {
606 // If there is new device action which has the same logical address and path
607 // ignore new request.
608 // NewDeviceAction is created whenever it receives <Report Physical Address>.
609 // And there is a chance starting NewDeviceAction for the same source.
610 // Usually, new device sends <Report Physical Address> when it's plugged
611 // in. However, TV can detect a new device from HotPlugDetectionAction,
612 // which sends <Give Physical Address> to the source for newly detected
613 // device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900614 if (action.isActionOf(activeSource)) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900615 return;
616 }
617 }
618
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900619 addAndStartAction(new NewDeviceAction(this, activeSource.logicalAddress,
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900620 activeSource.physicalAddress, deviceType));
Jungshik Jang97affee2014-07-11 17:04:43 +0900621 }
622
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900623 private boolean handleNewDeviceAtTheTailOfActivePath(int path) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900624 // Seq #22
625 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900626 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim7c3a9562014-08-01 11:07:42 +0900627 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900628 startRoutingControl(getActivePath(), newPath, false, null);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900629 return true;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900630 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900631 return false;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900632 }
633
634 /**
635 * Whether the given path is located in the tail of current active path.
636 *
637 * @param path to be tested
638 * @param activePath current active path
639 * @return true if the given path is located in the tail of current active path; otherwise,
640 * false
641 */
642 static boolean isTailOfActivePath(int path, int activePath) {
643 // If active routing path is internal source, return false.
644 if (activePath == 0) {
645 return false;
646 }
647 for (int i = 12; i >= 0; i -= 4) {
648 int curActivePath = (activePath >> i) & 0xF;
649 if (curActivePath == 0) {
650 return true;
651 } else {
652 int curPath = (path >> i) & 0xF;
653 if (curPath != curActivePath) {
654 return false;
655 }
656 }
657 }
658 return false;
659 }
660
661 @Override
662 @ServiceThreadOnly
663 protected boolean handleRoutingChange(HdmiCecMessage message) {
664 assertRunOnServiceThread();
665 // Seq #21
666 byte[] params = message.getParams();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900667 int currentPath = HdmiUtils.twoBytesToInt(params);
668 if (HdmiUtils.isAffectingActiveRoutingPath(getActivePath(), currentPath)) {
Amy123ec402018-09-25 10:56:31 -0700669 getActiveSource().invalidate();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900670 removeAction(RoutingControlAction.class);
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900671 int newPath = HdmiUtils.twoBytesToInt(params, 2);
Jinsuk Kim04fd2802014-07-03 14:04:02 +0900672 addAndStartAction(new RoutingControlAction(this, newPath, true, null));
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900673 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900674 return true;
675 }
Jinsuk Kim0a3316b2014-06-14 09:33:55 +0900676
677 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900678 @ServiceThreadOnly
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900679 protected boolean handleReportAudioStatus(HdmiCecMessage message) {
680 assertRunOnServiceThread();
Marvin Raminda665a62020-03-16 14:17:58 +0100681 if (!mService.isHdmiCecVolumeControlEnabled()) {
682 return false;
683 }
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900684
Shuichi.Noguchifbb50bc2017-12-06 11:12:33 +0900685 boolean mute = HdmiUtils.isAudioStatusMute(message);
686 int volume = HdmiUtils.getAudioStatusVolume(message);
687 setAudioStatus(mute, volume);
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900688 return true;
689 }
690
Yuncheol Heo38db6292014-07-01 14:15:14 +0900691 @Override
692 @ServiceThreadOnly
693 protected boolean handleTextViewOn(HdmiCecMessage message) {
694 assertRunOnServiceThread();
Jinsuk Kimc854c952015-10-12 09:33:33 +0900695
696 // Note that <Text View On> (and <Image View On>) command won't be handled here in
697 // most cases. A dedicated microcontroller should be in charge while Android system
698 // is in sleep mode, and the command need not be passed up to this service.
699 // The only situation where the command reaches this handler is that sleep mode is
700 // implemented in such a way that Android system is not really put to standby mode
701 // but only the display is set to blank. Then the command leads to the effect of
702 // turning on the display by the invocation of PowerManager.wakeUp().
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900703 if (mService.isPowerStandbyOrTransient() && mAutoWakeup) {
Yuncheol Heo38db6292014-07-01 14:15:14 +0900704 mService.wakeUp();
705 }
Yuncheol Heo38db6292014-07-01 14:15:14 +0900706 return true;
707 }
708
709 @Override
710 @ServiceThreadOnly
711 protected boolean handleImageViewOn(HdmiCecMessage message) {
712 assertRunOnServiceThread();
713 // Currently, it's the same as <Text View On>.
714 return handleTextViewOn(message);
715 }
716
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900717 @Override
718 @ServiceThreadOnly
719 protected boolean handleSetOsdName(HdmiCecMessage message) {
720 int source = message.getSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900721 HdmiDeviceInfo deviceInfo = getCecDeviceInfo(source);
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900722 // If the device is not in device list, ignore it.
723 if (deviceInfo == null) {
724 Slog.e(TAG, "No source device info for <Set Osd Name>." + message);
725 return true;
726 }
727 String osdName = null;
728 try {
729 osdName = new String(message.getParams(), "US-ASCII");
730 } catch (UnsupportedEncodingException e) {
731 Slog.e(TAG, "Invalid <Set Osd Name> request:" + message, e);
732 return true;
733 }
734
735 if (deviceInfo.getDisplayName().equals(osdName)) {
736 Slog.i(TAG, "Ignore incoming <Set Osd Name> having same osd name:" + message);
737 return true;
738 }
739
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900740 addCecDevice(new HdmiDeviceInfo(deviceInfo.getLogicalAddress(),
Jinsuk Kim2b152012014-07-25 08:22:26 +0900741 deviceInfo.getPhysicalAddress(), deviceInfo.getPortId(),
742 deviceInfo.getDeviceType(), deviceInfo.getVendorId(), osdName));
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900743 return true;
744 }
745
Jungshik Janga5b74142014-06-23 18:03:10 +0900746 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900747 private void launchDeviceDiscovery() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900748 assertRunOnServiceThread();
749 clearDeviceInfoList();
750 DeviceDiscoveryAction action = new DeviceDiscoveryAction(this,
Jungshik Jang60cffce2014-06-12 18:03:04 +0900751 new DeviceDiscoveryCallback() {
752 @Override
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900753 public void onDeviceDiscoveryDone(List<HdmiDeviceInfo> deviceInfos) {
754 for (HdmiDeviceInfo info : deviceInfos) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900755 addCecDevice(info);
Jungshik Jang60cffce2014-06-12 18:03:04 +0900756 }
757
758 // Since we removed all devices when it's start and
759 // device discovery action does not poll local devices,
760 // we should put device info of local device manually here
761 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900762 addCecDevice(device.getDeviceInfo());
Jungshik Jang60cffce2014-06-12 18:03:04 +0900763 }
764
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900765 mSelectRequestBuffer.process();
766 resetSelectRequestBuffer();
767
Jungshik Jang79c58a42014-06-16 16:45:36 +0900768 addAndStartAction(new HotplugDetectionAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang410ca9c2014-08-07 18:04:14 +0900769 addAndStartAction(new PowerStatusMonitorAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang187d0172014-06-17 17:48:42 +0900770
Jungshik Jang339227d2014-08-25 15:37:20 +0900771 HdmiDeviceInfo avr = getAvrDeviceInfo();
772 if (avr != null) {
773 onNewAvrAdded(avr);
Wally Yau951e3e42015-04-03 15:12:52 -0700774 } else {
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900775 setSystemAudioMode(false);
Jungshik Jang187d0172014-06-17 17:48:42 +0900776 }
Jungshik Jang60cffce2014-06-12 18:03:04 +0900777 }
778 });
Jungshik Jang79c58a42014-06-16 16:45:36 +0900779 addAndStartAction(action);
780 }
781
Jungshik Jang339227d2014-08-25 15:37:20 +0900782 @ServiceThreadOnly
783 void onNewAvrAdded(HdmiDeviceInfo avr) {
784 assertRunOnServiceThread();
Donghyun Choc21f63a2016-05-10 10:37:47 +0900785 addAndStartAction(new SystemAudioAutoInitiationAction(this, avr.getLogicalAddress()));
Donghyun Cho88909ce2016-12-23 14:41:00 +0900786 if (isConnected(avr.getPortId()) && isArcFeatureEnabled(avr.getPortId())
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900787 && !hasAction(SetArcTransmissionStateAction.class)) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900788 startArcAction(true);
789 }
790 }
791
Jungshik Jang79c58a42014-06-16 16:45:36 +0900792 // Clear all device info.
Jungshik Janga5b74142014-06-23 18:03:10 +0900793 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900794 private void clearDeviceInfoList() {
795 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900796 for (HdmiDeviceInfo info : mSafeExternalInputs) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +0900797 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +0900798 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900799 mDeviceInfos.clear();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900800 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900801 }
802
Jungshik Janga5b74142014-06-23 18:03:10 +0900803 @ServiceThreadOnly
Yuncheol Heoc516d652014-07-11 18:23:24 +0900804 // Seq #32
Jungshik Jangea67c182014-06-19 22:19:20 +0900805 void changeSystemAudioMode(boolean enabled, IHdmiControlCallback callback) {
806 assertRunOnServiceThread();
Yuncheol Heoc516d652014-07-11 18:23:24 +0900807 if (!mService.isControlEnabled() || hasAction(DeviceDiscoveryAction.class)) {
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900808 setSystemAudioMode(false);
Yuncheol Heoc516d652014-07-11 18:23:24 +0900809 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
810 return;
811 }
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900812 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jangea67c182014-06-19 22:19:20 +0900813 if (avr == null) {
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900814 setSystemAudioMode(false);
Yuncheol Heod05f67f2014-07-11 16:06:40 +0900815 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
Jungshik Jangea67c182014-06-19 22:19:20 +0900816 return;
817 }
818
819 addAndStartAction(
820 new SystemAudioActionFromTv(this, avr.getLogicalAddress(), enabled, callback));
Jungshik Jangea67c182014-06-19 22:19:20 +0900821 }
822
Jungshik Jangca5be9a2014-07-01 18:01:26 +0900823 // # Seq 25
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900824 void setSystemAudioMode(boolean on) {
825 if (!isSystemAudioControlFeatureEnabled() && on) {
826 HdmiLogger.debug("Cannot turn on system audio mode "
827 + "because the System Audio Control feature is disabled.");
828 return;
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900829 }
Amy489454f2019-01-24 19:06:57 -0800830 HdmiLogger.debug("System Audio Mode change[old:%b new:%b]",
831 mService.isSystemAudioActivated(), on);
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900832 updateAudioManagerForSystemAudio(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900833 synchronized (mLock) {
Amy489454f2019-01-24 19:06:57 -0800834 if (mService.isSystemAudioActivated() != on) {
835 mService.setSystemAudioActivated(on);
Jungshik Jangea67c182014-06-19 22:19:20 +0900836 mService.announceSystemAudioModeChange(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900837 }
Hannah.Hsu45254172019-05-15 15:05:23 +0800838 if (on && !mArcEstablished) {
839 startArcAction(true);
840 } else if (!on) {
841 startArcAction(false);
842 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900843 }
844 }
845
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900846 private void updateAudioManagerForSystemAudio(boolean on) {
Jungshik Jang6b096d32014-09-12 18:49:08 +0900847 int device = mService.getAudioManager().setHdmiSystemAudioSupported(on);
848 HdmiLogger.debug("[A]UpdateSystemAudio mode[on=%b] output=[%X]", on, device);
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900849 }
850
851 boolean isSystemAudioActivated() {
Jinsuk Kim86a1e5a2014-11-19 06:04:50 +0900852 if (!hasSystemAudioDevice()) {
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900853 return false;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900854 }
Amy489454f2019-01-24 19:06:57 -0800855 return mService.isSystemAudioActivated();
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900856 }
857
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900858 @ServiceThreadOnly
859 void setSystemAudioControlFeatureEnabled(boolean enabled) {
860 assertRunOnServiceThread();
861 synchronized (mLock) {
862 mSystemAudioControlFeatureEnabled = enabled;
863 }
864 if (hasSystemAudioDevice()) {
865 changeSystemAudioMode(enabled, null);
866 }
867 }
868
869 boolean isSystemAudioControlFeatureEnabled() {
870 synchronized (mLock) {
871 return mSystemAudioControlFeatureEnabled;
872 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900873 }
874
875 /**
876 * Change ARC status into the given {@code enabled} status.
877 *
878 * @return {@code true} if ARC was in "Enabled" status
879 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900880 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900881 boolean setArcStatus(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900882 assertRunOnServiceThread();
Jungshik Jang6b096d32014-09-12 18:49:08 +0900883
884 HdmiLogger.debug("Set Arc Status[old:%b new:%b]", mArcEstablished, enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900885 boolean oldStatus = mArcEstablished;
886 // 1. Enable/disable ARC circuit.
Donghyun Chobc6e3722016-11-04 05:25:52 +0900887 enableAudioReturnChannel(enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900888 // 2. Notify arc status to audio service.
889 notifyArcStatusToAudioService(enabled);
890 // 3. Update arc status;
891 mArcEstablished = enabled;
892 return oldStatus;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900893 }
894
Jinsuk Kim757c0972015-02-23 10:15:42 +0900895 /**
896 * Switch hardware ARC circuit in the system.
897 */
898 @ServiceThreadOnly
Donghyun Chobc6e3722016-11-04 05:25:52 +0900899 void enableAudioReturnChannel(boolean enabled) {
Jinsuk Kim757c0972015-02-23 10:15:42 +0900900 assertRunOnServiceThread();
901 HdmiDeviceInfo avr = getAvrDeviceInfo();
902 if (avr != null) {
Donghyun Chobc6e3722016-11-04 05:25:52 +0900903 mService.enableAudioReturnChannel(avr.getPortId(), enabled);
Jinsuk Kim757c0972015-02-23 10:15:42 +0900904 }
905 }
906
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +0900907 @ServiceThreadOnly
Jinsuk Kim7b0cf642015-04-14 09:43:45 +0900908 boolean isConnected(int portId) {
909 assertRunOnServiceThread();
910 return mService.isConnected(portId);
911 }
912
Jungshik Janga858d222014-06-23 17:17:47 +0900913 private void notifyArcStatusToAudioService(boolean enabled) {
914 // Note that we don't set any name to ARC.
915 mService.getAudioManager().setWiredDeviceConnectionState(
916 AudioSystem.DEVICE_OUT_HDMI_ARC,
Paul McLean10804eb2015-01-28 11:16:35 -0800917 enabled ? 1 : 0, "", "");
Jungshik Janga858d222014-06-23 17:17:47 +0900918 }
919
Jungshik Jang79c58a42014-06-16 16:45:36 +0900920 /**
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900921 * Returns true if ARC is currently established on a certain port.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900922 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900923 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900924 boolean isArcEstablished() {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900925 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900926 if (mArcEstablished) {
927 for (int i = 0; i < mArcFeatureEnabled.size(); i++) {
928 if (mArcFeatureEnabled.valueAt(i)) return true;
929 }
930 }
931 return false;
Jungshik Janga13da0d2014-06-30 16:26:06 +0900932 }
933
934 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900935 void changeArcFeatureEnabled(int portId, boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900936 assertRunOnServiceThread();
Donghyun Cho88909ce2016-12-23 14:41:00 +0900937 if (mArcFeatureEnabled.get(portId) == enabled) {
938 return;
939 }
940 mArcFeatureEnabled.put(portId, enabled);
941 HdmiDeviceInfo avr = getAvrDeviceInfo();
942 if (avr == null || avr.getPortId() != portId) {
943 return;
944 }
945 if (enabled && !mArcEstablished) {
946 startArcAction(true);
947 } else if (!enabled && mArcEstablished) {
948 startArcAction(false);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900949 }
950 }
951
952 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900953 boolean isArcFeatureEnabled(int portId) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900954 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900955 return mArcFeatureEnabled.get(portId);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900956 }
957
958 @ServiceThreadOnly
Jungshik Jang339227d2014-08-25 15:37:20 +0900959 void startArcAction(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900960 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900961 HdmiDeviceInfo info = getAvrDeviceInfo();
Jungshik Janga13da0d2014-06-30 16:26:06 +0900962 if (info == null) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900963 Slog.w(TAG, "Failed to start arc action; No AVR device.");
Jungshik Janga13da0d2014-06-30 16:26:06 +0900964 return;
965 }
Jungshik Jang339227d2014-08-25 15:37:20 +0900966 if (!canStartArcUpdateAction(info.getLogicalAddress(), enabled)) {
967 Slog.w(TAG, "Failed to start arc action; ARC configuration check failed.");
968 if (enabled && !isConnectedToArcPort(info.getPhysicalAddress())) {
969 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
970 }
Jungshik Janga13da0d2014-06-30 16:26:06 +0900971 return;
972 }
973
974 // Terminate opposite action and start action if not exist.
975 if (enabled) {
976 removeAction(RequestArcTerminationAction.class);
977 if (!hasAction(RequestArcInitiationAction.class)) {
978 addAndStartAction(new RequestArcInitiationAction(this, info.getLogicalAddress()));
979 }
980 } else {
981 removeAction(RequestArcInitiationAction.class);
982 if (!hasAction(RequestArcTerminationAction.class)) {
983 addAndStartAction(new RequestArcTerminationAction(this, info.getLogicalAddress()));
984 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900985 }
986 }
987
Jungshik Jang339227d2014-08-25 15:37:20 +0900988 private boolean isDirectConnectAddress(int physicalAddress) {
989 return (physicalAddress & Constants.ROUTING_PATH_TOP_MASK) == physicalAddress;
990 }
991
Jungshik Jang79c58a42014-06-16 16:45:36 +0900992 void setAudioStatus(boolean mute, int volume) {
Marvin Raminda665a62020-03-16 14:17:58 +0100993 if (!isSystemAudioActivated() || !mService.isHdmiCecVolumeControlEnabled()) {
Shuichi.Noguchi67ed2e92017-11-27 18:57:56 +0900994 return;
995 }
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900996 synchronized (mLock) {
997 mSystemAudioMute = mute;
998 mSystemAudioVolume = volume;
Jungshik Jangb69aafbf2014-07-11 16:29:06 +0900999 int maxVolume = mService.getAudioManager().getStreamMaxVolume(
1000 AudioManager.STREAM_MUSIC);
1001 mService.setAudioStatus(mute,
1002 VolumeControlAction.scaleToCustomVolume(volume, maxVolume));
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001003 displayOsd(HdmiControlManager.OSD_MESSAGE_AVR_VOLUME_CHANGED,
1004 mute ? HdmiControlManager.AVR_VOLUME_MUTED : volume);
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001005 }
1006 }
1007
1008 @ServiceThreadOnly
1009 void changeVolume(int curVolume, int delta, int maxVolume) {
1010 assertRunOnServiceThread();
Shuichi.Noguchiad91e7e2017-11-20 18:58:29 +09001011 if (getAvrDeviceInfo() == null) {
1012 // On initialization process, getAvrDeviceInfo() may return null and cause exception
1013 return;
1014 }
Marvin Raminda665a62020-03-16 14:17:58 +01001015 if (delta == 0 || !isSystemAudioActivated() || !mService.isHdmiCecVolumeControlEnabled()) {
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001016 return;
1017 }
1018
1019 int targetVolume = curVolume + delta;
1020 int cecVolume = VolumeControlAction.scaleToCecVolume(targetVolume, maxVolume);
1021 synchronized (mLock) {
1022 // If new volume is the same as current system audio volume, just ignore it.
1023 // Note that UNKNOWN_VOLUME is not in range of cec volume scale.
1024 if (cecVolume == mSystemAudioVolume) {
1025 // Update tv volume with system volume value.
1026 mService.setAudioStatus(false,
1027 VolumeControlAction.scaleToCustomVolume(mSystemAudioVolume, maxVolume));
1028 return;
1029 }
1030 }
1031
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001032 List<VolumeControlAction> actions = getActions(VolumeControlAction.class);
1033 if (actions.isEmpty()) {
1034 addAndStartAction(new VolumeControlAction(this,
1035 getAvrDeviceInfo().getLogicalAddress(), delta > 0));
1036 } else {
1037 actions.get(0).handleVolumeChange(delta > 0);
1038 }
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001039 }
1040
1041 @ServiceThreadOnly
1042 void changeMute(boolean mute) {
1043 assertRunOnServiceThread();
Marvin Raminda665a62020-03-16 14:17:58 +01001044 if (getAvrDeviceInfo() == null || !mService.isHdmiCecVolumeControlEnabled()) {
Shuichi.Noguchiad91e7e2017-11-20 18:58:29 +09001045 // On initialization process, getAvrDeviceInfo() may return null and cause exception
1046 return;
1047 }
Jungshik Jang6b096d32014-09-12 18:49:08 +09001048 HdmiLogger.debug("[A]:Change mute:%b", mute);
Jungshik Jang720407a2014-09-15 17:48:41 +09001049 synchronized (mLock) {
1050 if (mSystemAudioMute == mute) {
1051 HdmiLogger.debug("No need to change mute.");
1052 return;
1053 }
1054 }
Jungshik Jang377dcbd2014-07-15 15:49:02 +09001055 if (!isSystemAudioActivated()) {
Jungshik Jang6b096d32014-09-12 18:49:08 +09001056 HdmiLogger.debug("[A]:System audio is not activated.");
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001057 return;
1058 }
1059
1060 // Remove existing volume action.
1061 removeAction(VolumeControlAction.class);
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001062 sendUserControlPressedAndReleased(getAvrDeviceInfo().getLogicalAddress(),
Jinsuk Kim0ab37792015-10-17 06:53:52 +09001063 HdmiCecKeycode.getMuteKey(mute));
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001064 }
1065
Jungshik Jang79c58a42014-06-16 16:45:36 +09001066 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001067 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001068 protected boolean handleInitiateArc(HdmiCecMessage message) {
1069 assertRunOnServiceThread();
Jungshik Jang339227d2014-08-25 15:37:20 +09001070
1071 if (!canStartArcUpdateAction(message.getSource(), true)) {
Kyeongkab.Namab27ffb2018-10-19 16:35:14 +09001072 HdmiDeviceInfo avrDeviceInfo = getAvrDeviceInfo();
1073 if (avrDeviceInfo == null) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001074 // AVR may not have been discovered yet. Delay the message processing.
1075 mDelayedMessageBuffer.add(message);
1076 return true;
1077 }
Jungshik Jang339227d2014-08-25 15:37:20 +09001078 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
Kyeongkab.Namab27ffb2018-10-19 16:35:14 +09001079 if (!isConnectedToArcPort(avrDeviceInfo.getPhysicalAddress())) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001080 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
1081 }
1082 return true;
1083 }
1084
Jungshik Jang79c58a42014-06-16 16:45:36 +09001085 // In case where <Initiate Arc> is started by <Request ARC Initiation>
1086 // need to clean up RequestArcInitiationAction.
1087 removeAction(RequestArcInitiationAction.class);
1088 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1089 message.getSource(), true);
1090 addAndStartAction(action);
1091 return true;
1092 }
1093
Donghyun Cho88909ce2016-12-23 14:41:00 +09001094 private boolean canStartArcUpdateAction(int avrAddress, boolean enabled) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001095 HdmiDeviceInfo avr = getAvrDeviceInfo();
1096 if (avr != null
1097 && (avrAddress == avr.getLogicalAddress())
1098 && isConnectedToArcPort(avr.getPhysicalAddress())
1099 && isDirectConnectAddress(avr.getPhysicalAddress())) {
Donghyun Cho88909ce2016-12-23 14:41:00 +09001100 if (enabled) {
1101 return isConnected(avr.getPortId()) && isArcFeatureEnabled(avr.getPortId());
Jungshik Jang339227d2014-08-25 15:37:20 +09001102 } else {
1103 return true;
1104 }
1105 } else {
1106 return false;
1107 }
1108 }
1109
Jungshik Jang79c58a42014-06-16 16:45:36 +09001110 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001111 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001112 protected boolean handleTerminateArc(HdmiCecMessage message) {
1113 assertRunOnServiceThread();
Jinsuk Kim7e4b4802015-04-20 13:17:13 +09001114 if (mService .isPowerStandbyOrTransient()) {
1115 setArcStatus(false);
1116 return true;
1117 }
1118 // Do not check ARC configuration since the AVR might have been already removed.
1119 // Clean up RequestArcTerminationAction in case <Terminate Arc> was started by
1120 // <Request ARC Termination>.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001121 removeAction(RequestArcTerminationAction.class);
1122 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1123 message.getSource(), false);
1124 addAndStartAction(action);
1125 return true;
1126 }
1127
1128 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001129 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001130 protected boolean handleSetSystemAudioMode(HdmiCecMessage message) {
1131 assertRunOnServiceThread();
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001132 boolean systemAudioStatus = HdmiUtils.parseCommandParamSystemAudioStatus(message);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001133 if (!isMessageForSystemAudio(message)) {
Jinsuk Kimad1e3d72015-01-02 13:27:15 +09001134 if (getAvrDeviceInfo() == null) {
1135 // AVR may not have been discovered yet. Delay the message processing.
1136 mDelayedMessageBuffer.add(message);
Donghyun Choc21f63a2016-05-10 10:37:47 +09001137 } else {
1138 HdmiLogger.warning("Invalid <Set System Audio Mode> message:" + message);
1139 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
Jinsuk Kimad1e3d72015-01-02 13:27:15 +09001140 }
Jungshik Jang4480efa2014-09-04 17:08:34 +09001141 return true;
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001142 } else if (systemAudioStatus && !isSystemAudioControlFeatureEnabled()) {
1143 HdmiLogger.debug("Ignoring <Set System Audio Mode> message "
1144 + "because the System Audio Control feature is disabled: %s", message);
1145 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
1146 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001147 }
Donghyun Choc21f63a2016-05-10 10:37:47 +09001148 removeAction(SystemAudioAutoInitiationAction.class);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001149 SystemAudioActionFromAvr action = new SystemAudioActionFromAvr(this,
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001150 message.getSource(), systemAudioStatus, null);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001151 addAndStartAction(action);
1152 return true;
1153 }
1154
1155 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001156 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001157 protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) {
1158 assertRunOnServiceThread();
1159 if (!isMessageForSystemAudio(message)) {
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001160 HdmiLogger.warning("Invalid <System Audio Mode Status> message:" + message);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001161 // Ignore this message.
1162 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001163 }
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001164 setSystemAudioMode(HdmiUtils.parseCommandParamSystemAudioStatus(message));
Jungshik Jang79c58a42014-06-16 16:45:36 +09001165 return true;
1166 }
1167
Jungshik Jangb6591b82014-07-23 16:10:23 +09001168 // Seq #53
1169 @Override
1170 @ServiceThreadOnly
1171 protected boolean handleRecordTvScreen(HdmiCecMessage message) {
1172 List<OneTouchRecordAction> actions = getActions(OneTouchRecordAction.class);
1173 if (!actions.isEmpty()) {
1174 // Assumes only one OneTouchRecordAction.
1175 OneTouchRecordAction action = actions.get(0);
1176 if (action.getRecorderAddress() != message.getSource()) {
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001177 announceOneTouchRecordResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001178 message.getSource(),
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001179 HdmiControlManager.ONE_TOUCH_RECORD_PREVIOUS_RECORDING_IN_PROGRESS);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001180 }
1181 return super.handleRecordTvScreen(message);
1182 }
1183
1184 int recorderAddress = message.getSource();
1185 byte[] recordSource = mService.invokeRecordRequestListener(recorderAddress);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001186 int reason = startOneTouchRecord(recorderAddress, recordSource);
1187 if (reason != Constants.ABORT_NO_ERROR) {
1188 mService.maySendFeatureAbortCommand(message, reason);
1189 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001190 return true;
1191 }
1192
Jungshik Jange5a93372014-07-25 13:41:14 +09001193 @Override
1194 protected boolean handleTimerClearedStatus(HdmiCecMessage message) {
1195 byte[] params = message.getParams();
Jungshik Jange9e0f072014-08-05 17:48:59 +09001196 int timerClearedStatusData = params[0] & 0xFF;
Jungshik Jang326aef02014-11-05 12:50:35 +09001197 announceTimerRecordingResult(message.getSource(), timerClearedStatusData);
Jungshik Jange5a93372014-07-25 13:41:14 +09001198 return true;
1199 }
1200
Jungshik Jang326aef02014-11-05 12:50:35 +09001201 void announceOneTouchRecordResult(int recorderAddress, int result) {
1202 mService.invokeOneTouchRecordResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001203 }
1204
Jungshik Jang326aef02014-11-05 12:50:35 +09001205 void announceTimerRecordingResult(int recorderAddress, int result) {
1206 mService.invokeTimerRecordingResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001207 }
1208
Jungshik Jang326aef02014-11-05 12:50:35 +09001209 void announceClearTimerRecordingResult(int recorderAddress, int result) {
1210 mService.invokeClearTimerRecordingResult(recorderAddress, result);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001211 }
1212
Jungshik Jang79c58a42014-06-16 16:45:36 +09001213 private boolean isMessageForSystemAudio(HdmiCecMessage message) {
Jungshik Jang4480efa2014-09-04 17:08:34 +09001214 return mService.isControlEnabled()
1215 && message.getSource() == Constants.ADDR_AUDIO_SYSTEM
Jungshik Jang473119f2014-08-27 16:43:22 +09001216 && (message.getDestination() == Constants.ADDR_TV
1217 || message.getDestination() == Constants.ADDR_BROADCAST)
1218 && getAvrDeviceInfo() != null;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001219 }
1220
1221 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001222 * Add a new {@link HdmiDeviceInfo}. It returns old device info which has the same
Jungshik Jang79c58a42014-06-16 16:45:36 +09001223 * logical address as new device info's.
1224 *
1225 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1226 *
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001227 * @param deviceInfo a new {@link HdmiDeviceInfo} to be added.
1228 * @return {@code null} if it is new device. Otherwise, returns old {@HdmiDeviceInfo}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001229 * that has the same logical address as new one has.
1230 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001231 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001232 private HdmiDeviceInfo addDeviceInfo(HdmiDeviceInfo deviceInfo) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001233 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001234 HdmiDeviceInfo oldDeviceInfo = getCecDeviceInfo(deviceInfo.getLogicalAddress());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001235 if (oldDeviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001236 removeDeviceInfo(deviceInfo.getId());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001237 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001238 mDeviceInfos.append(deviceInfo.getId(), deviceInfo);
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001239 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001240 return oldDeviceInfo;
1241 }
1242
1243 /**
1244 * Remove a device info corresponding to the given {@code logicalAddress}.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001245 * It returns removed {@link HdmiDeviceInfo} if exists.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001246 *
1247 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1248 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001249 * @param id id of device to be removed
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001250 * @return removed {@link HdmiDeviceInfo} it exists. Otherwise, returns {@code null}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001251 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001252 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001253 private HdmiDeviceInfo removeDeviceInfo(int id) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001254 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001255 HdmiDeviceInfo deviceInfo = mDeviceInfos.get(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001256 if (deviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001257 mDeviceInfos.remove(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001258 }
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001259 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001260 return deviceInfo;
1261 }
1262
1263 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001264 * Return a list of all {@link HdmiDeviceInfo}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001265 *
1266 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
Jungshik Jang339227d2014-08-25 15:37:20 +09001267 * This is not thread-safe. For thread safety, call {@link #getSafeExternalInputsLocked} which
Jungshik Jang8e93c842014-08-06 15:48:33 +09001268 * does not include local device.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001269 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001270 @ServiceThreadOnly
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001271 List<HdmiDeviceInfo> getDeviceInfoList(boolean includeLocalDevice) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001272 assertRunOnServiceThread();
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001273 if (includeLocalDevice) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001274 return HdmiUtils.sparseArrayToList(mDeviceInfos);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001275 } else {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001276 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001277 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001278 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001279 if (!isLocalDeviceAddress(info.getLogicalAddress())) {
1280 infoList.add(info);
1281 }
1282 }
1283 return infoList;
1284 }
1285 }
1286
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001287 /**
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001288 * Return external input devices.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001289 */
Andreas Gampea36dc622018-02-05 17:19:22 -08001290 @GuardedBy("mLock")
Jinsuk Kimed086452014-08-18 15:01:53 +09001291 List<HdmiDeviceInfo> getSafeExternalInputsLocked() {
1292 return mSafeExternalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001293 }
1294
Jungshik Janga5b74142014-06-23 18:03:10 +09001295 @ServiceThreadOnly
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001296 private void updateSafeDeviceInfoList() {
1297 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001298 List<HdmiDeviceInfo> copiedDevices = HdmiUtils.sparseArrayToList(mDeviceInfos);
1299 List<HdmiDeviceInfo> externalInputs = getInputDevices();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001300 synchronized (mLock) {
1301 mSafeAllDeviceInfos = copiedDevices;
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001302 mSafeExternalInputs = externalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001303 }
1304 }
1305
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001306 /**
1307 * Return a list of external cec input (source) devices.
1308 *
1309 * <p>Note that this effectively excludes non-source devices like system audio,
1310 * secondary TV.
1311 */
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001312 private List<HdmiDeviceInfo> getInputDevices() {
1313 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001314 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001315 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001316 if (isLocalDeviceAddress(info.getLogicalAddress())) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001317 continue;
1318 }
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001319 if (info.isSourceType() && !hideDevicesBehindLegacySwitch(info)) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001320 infoList.add(info);
1321 }
1322 }
1323 return infoList;
1324 }
1325
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001326 // Check if we are hiding CEC devices connected to a legacy (non-CEC) switch.
1327 // Returns true if the policy is set to true, and the device to check does not have
1328 // a parent CEC device (which should be the CEC-enabled switch) in the list.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001329 private boolean hideDevicesBehindLegacySwitch(HdmiDeviceInfo info) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001330 return HdmiConfig.HIDE_DEVICES_BEHIND_LEGACY_SWITCH
1331 && !isConnectedToCecSwitch(info.getPhysicalAddress(), mCecSwitches);
1332 }
1333
1334 private static boolean isConnectedToCecSwitch(int path, Collection<Integer> switches) {
1335 for (int switchPath : switches) {
1336 if (isParentPath(switchPath, path)) {
1337 return true;
1338 }
1339 }
1340 return false;
1341 }
1342
1343 private static boolean isParentPath(int parentPath, int childPath) {
1344 // (A000, AB00) (AB00, ABC0), (ABC0, ABCD)
1345 // If child's last non-zero nibble is removed, the result equals to the parent.
1346 for (int i = 0; i <= 12; i += 4) {
1347 int nibble = (childPath >> i) & 0xF;
1348 if (nibble != 0) {
1349 int parentNibble = (parentPath >> i) & 0xF;
1350 return parentNibble == 0 && (childPath >> i+4) == (parentPath >> i+4);
1351 }
1352 }
1353 return false;
1354 }
1355
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001356 private void invokeDeviceEventListener(HdmiDeviceInfo info, int status) {
Jinsuk Kim98d760e2014-12-23 07:01:51 +09001357 if (!hideDevicesBehindLegacySwitch(info)) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001358 mService.invokeDeviceEventListeners(info, status);
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001359 }
1360 }
1361
Jungshik Jang79c58a42014-06-16 16:45:36 +09001362 private boolean isLocalDeviceAddress(int address) {
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001363 return mLocalDeviceAddresses.contains(address);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001364 }
1365
Jungshik Jange9cf1582014-06-23 17:28:58 +09001366 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001367 HdmiDeviceInfo getAvrDeviceInfo() {
Jungshik Jange9cf1582014-06-23 17:28:58 +09001368 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001369 return getCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jange9cf1582014-06-23 17:28:58 +09001370 }
1371
Jungshik Jang79c58a42014-06-16 16:45:36 +09001372 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001373 * Return a {@link HdmiDeviceInfo} corresponding to the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001374 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001375 * This is not thread-safe. For thread safety, call {@link #getSafeCecDeviceInfo(int)}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001376 *
Jungshik Jang339227d2014-08-25 15:37:20 +09001377 * @param logicalAddress logical address of the device to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001378 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001379 * Returns null if no logical address matched
1380 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001381 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001382 HdmiDeviceInfo getCecDeviceInfo(int logicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001383 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001384 return mDeviceInfos.get(HdmiDeviceInfo.idForCecDevice(logicalAddress));
Jungshik Jang79c58a42014-06-16 16:45:36 +09001385 }
1386
Jungshik Jange9cf1582014-06-23 17:28:58 +09001387 boolean hasSystemAudioDevice() {
1388 return getSafeAvrDeviceInfo() != null;
1389 }
1390
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001391 HdmiDeviceInfo getSafeAvrDeviceInfo() {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001392 return getSafeCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001393 }
1394
1395 /**
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001396 * Thread safe version of {@link #getCecDeviceInfo(int)}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001397 *
1398 * @param logicalAddress logical address to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001399 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001400 * Returns null if no logical address matched
1401 */
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001402 HdmiDeviceInfo getSafeCecDeviceInfo(int logicalAddress) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001403 synchronized (mLock) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001404 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1405 if (info.isCecDevice() && info.getLogicalAddress() == logicalAddress) {
1406 return info;
1407 }
1408 }
1409 return null;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001410 }
1411 }
1412
Andreas Gampea36dc622018-02-05 17:19:22 -08001413 @GuardedBy("mLock")
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001414 List<HdmiDeviceInfo> getSafeCecDevicesLocked() {
1415 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
1416 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1417 if (isLocalDeviceAddress(info.getLogicalAddress())) {
1418 continue;
1419 }
1420 infoList.add(info);
1421 }
1422 return infoList;
1423 }
1424
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001425 /**
Jungshik Jang8f2ed352014-07-07 15:02:47 +09001426 * Called when a device is newly added or a new device is detected or
1427 * existing device is updated.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001428 *
1429 * @param info device info of a new device.
1430 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001431 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001432 final void addCecDevice(HdmiDeviceInfo info) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001433 assertRunOnServiceThread();
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001434 HdmiDeviceInfo old = addDeviceInfo(info);
Jinsuk Kim13c030e2014-06-20 13:25:17 +09001435 if (info.getLogicalAddress() == mAddress) {
1436 // The addition of TV device itself should not be notified.
1437 return;
1438 }
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001439 if (old == null) {
1440 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
1441 } else if (!old.equals(info)) {
Jinsuk Kim4fcbf0b2014-12-09 06:48:13 +09001442 invokeDeviceEventListener(old, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
1443 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001444 }
Jungshik Jang79c58a42014-06-16 16:45:36 +09001445 }
1446
1447 /**
1448 * Called when a device is removed or removal of device is detected.
1449 *
1450 * @param address a logical address of a device to be removed
1451 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001452 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001453 final void removeCecDevice(int address) {
1454 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001455 HdmiDeviceInfo info = removeDeviceInfo(HdmiDeviceInfo.idForCecDevice(address));
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001456
Jungshik Jang79c58a42014-06-16 16:45:36 +09001457 mCecMessageCache.flushMessagesFrom(address);
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001458 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001459 }
1460
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001461 @ServiceThreadOnly
1462 void handleRemoveActiveRoutingPath(int path) {
1463 assertRunOnServiceThread();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001464 // Seq #23
1465 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001466 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim546d8672014-11-24 07:30:54 +09001467 startRoutingControl(getActivePath(), newPath, true, null);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001468 }
1469 }
1470
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001471 /**
1472 * Launch routing control process.
1473 *
1474 * @param routingForBootup true if routing control is initiated due to One Touch Play
1475 * or TV power on
1476 */
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001477 @ServiceThreadOnly
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001478 void launchRoutingControl(boolean routingForBootup) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001479 assertRunOnServiceThread();
1480 // Seq #24
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001481 if (getActivePortId() != Constants.INVALID_PORT_ID) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001482 if (!routingForBootup && !isProhibitMode()) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001483 int newPath = mService.portIdToPath(getActivePortId());
1484 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +09001485 startRoutingControl(getActivePath(), newPath, routingForBootup, null);
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001486 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001487 } else {
1488 int activePath = mService.getPhysicalAddress();
1489 setActivePath(activePath);
Jinsuk Kim2ab6d9f2015-01-16 15:49:16 +09001490 if (!routingForBootup
1491 && !mDelayedMessageBuffer.isBuffered(Constants.MESSAGE_ACTIVE_SOURCE)) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001492 mService.sendCecCommand(HdmiCecMessageBuilder.buildActiveSource(mAddress,
1493 activePath));
1494 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001495 }
1496 }
1497
Jungshik Jang79c58a42014-06-16 16:45:36 +09001498 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001499 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
Jungshik Jang79c58a42014-06-16 16:45:36 +09001500 * the given routing path. CEC devices use routing path for its physical address to
1501 * describe the hierarchy of the devices in the network.
1502 *
1503 * @param path routing path or physical address
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001504 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
Jungshik Jang79c58a42014-06-16 16:45:36 +09001505 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001506 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001507 final HdmiDeviceInfo getDeviceInfoByPath(int path) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001508 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001509 for (HdmiDeviceInfo info : getDeviceInfoList(false)) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001510 if (info.getPhysicalAddress() == path) {
1511 return info;
1512 }
1513 }
1514 return null;
1515 }
1516
1517 /**
Jinsuk Kim7640d982015-01-28 16:44:07 +09001518 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
1519 * the given routing path. This is the version accessible safely from threads
1520 * other than service thread.
1521 *
1522 * @param path routing path or physical address
1523 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
1524 */
1525 HdmiDeviceInfo getSafeDeviceInfoByPath(int path) {
1526 synchronized (mLock) {
1527 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1528 if (info.getPhysicalAddress() == path) {
1529 return info;
1530 }
1531 }
1532 return null;
1533 }
1534 }
1535
1536 /**
Jungshik Jang79c58a42014-06-16 16:45:36 +09001537 * Whether a device of the specified physical address and logical address exists
1538 * in a device info list. However, both are minimal condition and it could
1539 * be different device from the original one.
1540 *
Jungshik Jang79c58a42014-06-16 16:45:36 +09001541 * @param logicalAddress logical address of a device to be searched
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001542 * @param physicalAddress physical address of a device to be searched
Jungshik Jang79c58a42014-06-16 16:45:36 +09001543 * @return true if exist; otherwise false
1544 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001545 @ServiceThreadOnly
Jinsuk Kim7cd4a582015-01-29 09:50:25 +09001546 boolean isInDeviceList(int logicalAddress, int physicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001547 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001548 HdmiDeviceInfo device = getCecDeviceInfo(logicalAddress);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001549 if (device == null) {
1550 return false;
1551 }
1552 return device.getPhysicalAddress() == physicalAddress;
1553 }
1554
1555 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001556 @ServiceThreadOnly
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001557 void onHotplug(int portId, boolean connected) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001558 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001559
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001560 if (!connected) {
1561 removeCecSwitches(portId);
1562 }
Amy07ed99c2019-10-22 18:33:53 -07001563
1564 // Turning System Audio Mode off when the AVR is unlugged or standby.
1565 // When the device is not unplugged but reawaken from standby, we check if the System
1566 // Audio Control Feature is enabled or not then decide if turning SAM on/off accordingly.
1567 if (getAvrDeviceInfo() != null && portId == getAvrDeviceInfo().getPortId()) {
1568 if (!connected) {
1569 setSystemAudioMode(false);
1570 } else if (mSystemAudioControlFeatureEnabled != mService.isSystemAudioActivated()){
1571 setSystemAudioMode(mSystemAudioControlFeatureEnabled);
1572 }
1573 }
1574
Jungshik Jang79c58a42014-06-16 16:45:36 +09001575 // Tv device will have permanent HotplugDetectionAction.
1576 List<HotplugDetectionAction> hotplugActions = getActions(HotplugDetectionAction.class);
1577 if (!hotplugActions.isEmpty()) {
1578 // Note that hotplug action is single action running on a machine.
1579 // "pollAllDevicesNow" cleans up timer and start poll action immediately.
Jungshik Jang24c23c12014-07-07 18:04:39 +09001580 // It covers seq #40, #43.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001581 hotplugActions.get(0).pollAllDevicesNow();
1582 }
Jungshik Jang60cffce2014-06-12 18:03:04 +09001583 }
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001584
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001585 private void removeCecSwitches(int portId) {
1586 Iterator<Integer> it = mCecSwitches.iterator();
1587 while (!it.hasNext()) {
1588 int path = it.next();
1589 if (pathToPortId(path) == portId) {
1590 it.remove();
1591 }
1592 }
1593 }
1594
Jinsuk Kime6e8f3d2015-05-11 14:17:04 +09001595 @Override
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001596 @ServiceThreadOnly
1597 void setAutoDeviceOff(boolean enabled) {
1598 assertRunOnServiceThread();
1599 mAutoDeviceOff = enabled;
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001600 }
1601
1602 @ServiceThreadOnly
1603 void setAutoWakeup(boolean enabled) {
1604 assertRunOnServiceThread();
1605 mAutoWakeup = enabled;
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001606 }
Yuncheol Heo38db6292014-07-01 14:15:14 +09001607
Yuncheol Heo25c20292014-07-31 17:59:39 +09001608 @ServiceThreadOnly
1609 boolean getAutoWakeup() {
1610 assertRunOnServiceThread();
1611 return mAutoWakeup;
1612 }
1613
Yuncheol Heo38db6292014-07-01 14:15:14 +09001614 @Override
1615 @ServiceThreadOnly
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001616 protected void disableDevice(boolean initiatedByCec, PendingActionClearedCallback callback) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001617 assertRunOnServiceThread();
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001618 mService.unregisterTvInputCallback(mTvInputCallback);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001619 // Remove any repeated working actions.
1620 // HotplugDetectionAction will be reinstated during the wake up process.
1621 // HdmiControlService.onWakeUp() -> initializeLocalDevices() ->
1622 // LocalDeviceTv.onAddressAllocated() -> launchDeviceDiscovery().
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001623 removeAction(DeviceDiscoveryAction.class);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001624 removeAction(HotplugDetectionAction.class);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001625 removeAction(PowerStatusMonitorAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001626 // Remove recording actions.
Jungshik Jangb6591b82014-07-23 16:10:23 +09001627 removeAction(OneTouchRecordAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001628 removeAction(TimerRecordingAction.class);
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001629
1630 disableSystemAudioIfExist();
1631 disableArcIfExist();
Jinsuk Kima5445ce2015-03-30 17:14:58 +09001632
1633 super.disableDevice(initiatedByCec, callback);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +09001634 clearDeviceInfoList();
Jinsuk Kim7cc51c62015-04-30 10:39:35 +09001635 getActiveSource().invalidate();
1636 setActivePath(Constants.INVALID_PHYSICAL_ADDRESS);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001637 checkIfPendingActionsCleared();
1638 }
1639
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001640 @ServiceThreadOnly
1641 private void disableSystemAudioIfExist() {
1642 assertRunOnServiceThread();
1643 if (getAvrDeviceInfo() == null) {
1644 return;
1645 }
1646
1647 // Seq #31.
1648 removeAction(SystemAudioActionFromAvr.class);
1649 removeAction(SystemAudioActionFromTv.class);
1650 removeAction(SystemAudioAutoInitiationAction.class);
1651 removeAction(SystemAudioStatusAction.class);
1652 removeAction(VolumeControlAction.class);
Hannah.Hsu4ee736c2019-05-15 15:38:28 +08001653
1654 if (!mService.isControlEnabled()) {
1655 setSystemAudioMode(false);
1656 }
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001657 }
1658
1659 @ServiceThreadOnly
1660 private void disableArcIfExist() {
1661 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001662 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001663 if (avr == null) {
1664 return;
1665 }
1666
1667 // Seq #44.
1668 removeAction(RequestArcInitiationAction.class);
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +09001669 if (!hasAction(RequestArcTerminationAction.class) && isArcEstablished()) {
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001670 addAndStartAction(new RequestArcTerminationAction(this, avr.getLogicalAddress()));
1671 }
1672 }
1673
Yuncheol Heo38db6292014-07-01 14:15:14 +09001674 @Override
1675 @ServiceThreadOnly
Jinsuk Kime6e8f3d2015-05-11 14:17:04 +09001676 protected void onStandby(boolean initiatedByCec, int standbyAction) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001677 assertRunOnServiceThread();
1678 // Seq #11
1679 if (!mService.isControlEnabled()) {
1680 return;
1681 }
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001682 if (!initiatedByCec && mAutoDeviceOff) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001683 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001684 mAddress, Constants.ADDR_BROADCAST));
Yuncheol Heo38db6292014-07-01 14:15:14 +09001685 }
1686 }
1687
Jinsuk Kim4d43d932014-07-03 16:43:58 +09001688 boolean isProhibitMode() {
1689 return mService.isProhibitMode();
1690 }
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001691
1692 boolean isPowerStandbyOrTransient() {
1693 return mService.isPowerStandbyOrTransient();
Jungshik Jang8866c812014-07-08 14:42:28 +09001694 }
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001695
Jungshik Jang339227d2014-08-25 15:37:20 +09001696 @ServiceThreadOnly
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001697 void displayOsd(int messageId) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001698 assertRunOnServiceThread();
1699 mService.displayOsd(messageId);
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001700 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001701
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001702 @ServiceThreadOnly
1703 void displayOsd(int messageId, int extra) {
1704 assertRunOnServiceThread();
1705 mService.displayOsd(messageId, extra);
1706 }
1707
Jungshik Jangb6591b82014-07-23 16:10:23 +09001708 // Seq #54 and #55
1709 @ServiceThreadOnly
Jungshik Jang4480efa2014-09-04 17:08:34 +09001710 int startOneTouchRecord(int recorderAddress, byte[] recordSource) {
Jungshik Jangb6591b82014-07-23 16:10:23 +09001711 assertRunOnServiceThread();
1712 if (!mService.isControlEnabled()) {
1713 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001714 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001715 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001716 }
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 Jang4480efa2014-09-04 17:08:34 +09001722 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001723 }
1724
1725 if (!checkRecordSource(recordSource)) {
1726 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001727 announceOneTouchRecordResult(recorderAddress,
1728 ONE_TOUCH_RECORD_FAIL_TO_RECORD_DISPLAYED_SCREEN);
Jinsuk Kimd47abef2015-01-17 07:38:24 +09001729 return Constants.ABORT_CANNOT_PROVIDE_SOURCE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001730 }
1731
1732 addAndStartAction(new OneTouchRecordAction(this, recorderAddress, recordSource));
1733 Slog.i(TAG, "Start new [One Touch Record]-Target:" + recorderAddress + ", recordSource:"
1734 + Arrays.toString(recordSource));
Jungshik Jang4480efa2014-09-04 17:08:34 +09001735 return Constants.ABORT_NO_ERROR;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001736 }
1737
1738 @ServiceThreadOnly
1739 void stopOneTouchRecord(int recorderAddress) {
1740 assertRunOnServiceThread();
1741 if (!mService.isControlEnabled()) {
1742 Slog.w(TAG, "Can not stop one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001743 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001744 return;
1745 }
1746
1747 if (!checkRecorder(recorderAddress)) {
1748 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001749 announceOneTouchRecordResult(recorderAddress,
1750 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001751 return;
1752 }
1753
1754 // Remove one touch record action so that other one touch record can be started.
1755 removeAction(OneTouchRecordAction.class);
1756 mService.sendCecCommand(HdmiCecMessageBuilder.buildRecordOff(mAddress, recorderAddress));
1757 Slog.i(TAG, "Stop [One Touch Record]-Target:" + recorderAddress);
1758 }
1759
1760 private boolean checkRecorder(int recorderAddress) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001761 HdmiDeviceInfo device = getCecDeviceInfo(recorderAddress);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001762 return (device != null)
1763 && (HdmiUtils.getTypeFromAddress(recorderAddress)
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001764 == HdmiDeviceInfo.DEVICE_RECORDER);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001765 }
1766
1767 private boolean checkRecordSource(byte[] recordSource) {
1768 return (recordSource != null) && HdmiRecordSources.checkRecordSource(recordSource);
1769 }
1770
1771 @ServiceThreadOnly
1772 void startTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1773 assertRunOnServiceThread();
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001774 if (!mService.isControlEnabled()) {
1775 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001776 announceTimerRecordingResult(recorderAddress,
1777 TIMER_RECORDING_RESULT_EXTRA_CEC_DISABLED);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001778 return;
1779 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001780
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001781 if (!checkRecorder(recorderAddress)) {
1782 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001783 announceTimerRecordingResult(recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001784 TIMER_RECORDING_RESULT_EXTRA_CHECK_RECORDER_CONNECTION);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001785 return;
1786 }
1787
1788 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1789 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
1790 announceTimerRecordingResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001791 recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001792 TIMER_RECORDING_RESULT_EXTRA_FAIL_TO_RECORD_SELECTED_SOURCE);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001793 return;
1794 }
1795
1796 addAndStartAction(
1797 new TimerRecordingAction(this, recorderAddress, sourceType, recordSource));
1798 Slog.i(TAG, "Start [Timer Recording]-Target:" + recorderAddress + ", SourceType:"
1799 + sourceType + ", RecordSource:" + Arrays.toString(recordSource));
1800 }
1801
1802 private boolean checkTimerRecordingSource(int sourceType, byte[] recordSource) {
1803 return (recordSource != null)
1804 && HdmiTimerRecordSources.checkTimerRecordSource(sourceType, recordSource);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001805 }
1806
1807 @ServiceThreadOnly
1808 void clearTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1809 assertRunOnServiceThread();
Jungshik Jange5a93372014-07-25 13:41:14 +09001810 if (!mService.isControlEnabled()) {
1811 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001812 announceClearTimerRecordingResult(recorderAddress, CLEAR_TIMER_STATUS_CEC_DISABLE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001813 return;
1814 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001815
Jungshik Jange5a93372014-07-25 13:41:14 +09001816 if (!checkRecorder(recorderAddress)) {
1817 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001818 announceClearTimerRecordingResult(recorderAddress,
1819 CLEAR_TIMER_STATUS_CHECK_RECORDER_CONNECTION);
Jungshik Jange5a93372014-07-25 13:41:14 +09001820 return;
1821 }
1822
1823 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1824 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001825 announceClearTimerRecordingResult(recorderAddress,
1826 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001827 return;
1828 }
1829
1830 sendClearTimerMessage(recorderAddress, sourceType, recordSource);
1831 }
1832
Jungshik Jang326aef02014-11-05 12:50:35 +09001833 private void sendClearTimerMessage(final int recorderAddress, int sourceType,
1834 byte[] recordSource) {
Jungshik Jange5a93372014-07-25 13:41:14 +09001835 HdmiCecMessage message = null;
1836 switch (sourceType) {
1837 case TIMER_RECORDING_TYPE_DIGITAL:
1838 message = HdmiCecMessageBuilder.buildClearDigitalTimer(mAddress, recorderAddress,
1839 recordSource);
1840 break;
1841 case TIMER_RECORDING_TYPE_ANALOGUE:
1842 message = HdmiCecMessageBuilder.buildClearAnalogueTimer(mAddress, recorderAddress,
1843 recordSource);
1844 break;
1845 case TIMER_RECORDING_TYPE_EXTERNAL:
1846 message = HdmiCecMessageBuilder.buildClearExternalTimer(mAddress, recorderAddress,
1847 recordSource);
1848 break;
1849 default:
1850 Slog.w(TAG, "Invalid source type:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001851 announceClearTimerRecordingResult(recorderAddress,
1852 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001853 return;
1854
1855 }
1856 mService.sendCecCommand(message, new SendMessageCallback() {
1857 @Override
1858 public void onSendCompleted(int error) {
Donghyun Chobc6e3722016-11-04 05:25:52 +09001859 if (error != SendMessageResult.SUCCESS) {
Jungshik Jang326aef02014-11-05 12:50:35 +09001860 announceClearTimerRecordingResult(recorderAddress,
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001861 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001862 }
1863 }
1864 });
Jungshik Jangb6591b82014-07-23 16:10:23 +09001865 }
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001866
1867 void updateDevicePowerStatus(int logicalAddress, int newPowerStatus) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001868 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001869 if (info == null) {
1870 Slog.w(TAG, "Can not update power status of non-existing device:" + logicalAddress);
1871 return;
1872 }
1873
1874 if (info.getDevicePowerStatus() == newPowerStatus) {
1875 return;
1876 }
1877
1878 HdmiDeviceInfo newInfo = HdmiUtils.cloneHdmiDeviceInfo(info, newPowerStatus);
1879 // addDeviceInfo replaces old device info with new one if exists.
1880 addDeviceInfo(newInfo);
1881
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001882 invokeDeviceEventListener(newInfo, HdmiControlManager.DEVICE_EVENT_UPDATE_DEVICE);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001883 }
Terry Heo959d2db2014-08-28 16:45:41 +09001884
1885 @Override
Yuncheol Heo184b1242014-09-12 15:09:07 +09001886 protected boolean handleMenuStatus(HdmiCecMessage message) {
1887 // Do nothing and just return true not to prevent from responding <Feature Abort>.
1888 return true;
1889 }
1890
1891 @Override
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001892 protected void sendStandby(int deviceId) {
1893 HdmiDeviceInfo targetDevice = mDeviceInfos.get(deviceId);
1894 if (targetDevice == null) {
1895 return;
1896 }
1897 int targetAddress = targetDevice.getLogicalAddress();
1898 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(mAddress, targetAddress));
1899 }
1900
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001901 @ServiceThreadOnly
1902 void processAllDelayedMessages() {
1903 assertRunOnServiceThread();
1904 mDelayedMessageBuffer.processAllMessages();
1905 }
1906
1907 @ServiceThreadOnly
1908 void processDelayedMessages(int address) {
1909 assertRunOnServiceThread();
1910 mDelayedMessageBuffer.processMessagesForDevice(address);
1911 }
1912
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +09001913 @ServiceThreadOnly
1914 void processDelayedActiveSource(int address) {
1915 assertRunOnServiceThread();
1916 mDelayedMessageBuffer.processActiveSource(address);
1917 }
1918
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001919 @Override
Terry Heo959d2db2014-08-28 16:45:41 +09001920 protected void dump(final IndentingPrintWriter pw) {
1921 super.dump(pw);
1922 pw.println("mArcEstablished: " + mArcEstablished);
1923 pw.println("mArcFeatureEnabled: " + mArcFeatureEnabled);
Terry Heo959d2db2014-08-28 16:45:41 +09001924 pw.println("mSystemAudioMute: " + mSystemAudioMute);
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001925 pw.println("mSystemAudioControlFeatureEnabled: " + mSystemAudioControlFeatureEnabled);
Terry Heo959d2db2014-08-28 16:45:41 +09001926 pw.println("mAutoDeviceOff: " + mAutoDeviceOff);
1927 pw.println("mAutoWakeup: " + mAutoWakeup);
1928 pw.println("mSkipRoutingControl: " + mSkipRoutingControl);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +09001929 pw.println("mPrevPortId: " + mPrevPortId);
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001930 pw.println("CEC devices:");
1931 pw.increaseIndent();
1932 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1933 pw.println(info);
1934 }
1935 pw.decreaseIndent();
Terry Heo959d2db2014-08-28 16:45:41 +09001936 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +09001937}