blob: f4a86675541dc7feaa44e6552a861d58903c3558 [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();
Marvin Ramin1c7270a2020-06-22 13:55:50 +0200216 startQueuedActions();
Jungshik Jang6f34f5a2014-07-08 21:17:29 +0900217 }
218
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900219
220 @ServiceThreadOnly
221 private List<Integer> initLocalDeviceAddresses() {
222 assertRunOnServiceThread();
223 List<Integer> addresses = new ArrayList<>();
224 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
225 addresses.add(device.getDeviceInfo().getLogicalAddress());
226 }
227 return Collections.unmodifiableList(addresses);
228 }
229
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900230
231 @ServiceThreadOnly
232 public void setSelectRequestBuffer(SelectRequestBuffer requestBuffer) {
233 assertRunOnServiceThread();
234 mSelectRequestBuffer = requestBuffer;
235 }
236
237 @ServiceThreadOnly
238 private void resetSelectRequestBuffer() {
239 assertRunOnServiceThread();
240 setSelectRequestBuffer(SelectRequestBuffer.EMPTY_BUFFER);
241 }
242
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900243 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900244 protected int getPreferredAddress() {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900245 return Constants.ADDR_TV;
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900246 }
247
248 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900249 protected void setPreferredAddress(int addr) {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900250 Slog.w(TAG, "Preferred addres will not be stored for TV");
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900251 }
252
Yuncheol Heo25c20292014-07-31 17:59:39 +0900253 @Override
254 @ServiceThreadOnly
255 boolean dispatchMessage(HdmiCecMessage message) {
256 assertRunOnServiceThread();
Donghyun Choafd26a22016-12-23 15:53:28 +0900257 if (mService.isPowerStandby() && !mService.isWakeUpMessageReceived()
258 && mStandbyHandler.handleCommand(message)) {
Yuncheol Heo25c20292014-07-31 17:59:39 +0900259 return true;
260 }
261 return super.onMessage(message);
262 }
263
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900264 /**
265 * Performs the action 'device select', or 'one touch play' initiated by TV.
266 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900267 * @param id id of HDMI device to select
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900268 * @param callback callback object to report the result with
269 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900270 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900271 void deviceSelect(int id, IHdmiControlCallback callback) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900272 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900273 HdmiDeviceInfo targetDevice = mDeviceInfos.get(id);
274 if (targetDevice == null) {
275 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
276 return;
277 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900278 int targetAddress = targetDevice.getLogicalAddress();
Jinsuk Kim58500f42014-08-05 12:48:35 +0900279 ActiveSource active = getActiveSource();
Yu.Ishihara6b2a6172015-03-25 09:44:06 +0900280 if (targetDevice.getDevicePowerStatus() == HdmiControlManager.POWER_STATUS_ON
281 && active.isValid()
282 && targetAddress == active.logicalAddress) {
Jinsuk Kim58500f42014-08-05 12:48:35 +0900283 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
284 return;
285 }
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900286 if (targetAddress == Constants.ADDR_INTERNAL) {
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900287 handleSelectInternalSource();
288 // Switching to internal source is always successful even when CEC control is disabled.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900289 setActiveSource(targetAddress, mService.getPhysicalAddress());
Jinsuk Kim7e742062014-07-30 13:19:13 +0900290 setActivePath(mService.getPhysicalAddress());
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900291 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
Jinsuk Kim83335712014-06-24 07:57:00 +0900292 return;
293 }
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900294 if (!mService.isControlEnabled()) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900295 setActiveSource(targetDevice);
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900296 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
297 return;
298 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900299 removeAction(DeviceSelectAction.class);
300 addAndStartAction(new DeviceSelectAction(this, targetDevice, callback));
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900301 }
302
Jungshik Janga5b74142014-06-23 18:03:10 +0900303 @ServiceThreadOnly
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900304 private void handleSelectInternalSource() {
Jinsuk Kima062a932014-06-18 10:00:39 +0900305 assertRunOnServiceThread();
Jinsuk Kim83335712014-06-24 07:57:00 +0900306 // Seq #18
Amy123ec402018-09-25 10:56:31 -0700307 if (mService.isControlEnabled() && getActiveSource().logicalAddress != mAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900308 updateActiveSource(mAddress, mService.getPhysicalAddress());
Jinsuk Kimd5307192014-08-22 09:07:40 +0900309 if (mSkipRoutingControl) {
310 mSkipRoutingControl = false;
311 return;
312 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900313 HdmiCecMessage activeSource = HdmiCecMessageBuilder.buildActiveSource(
314 mAddress, mService.getPhysicalAddress());
315 mService.sendCecCommand(activeSource);
316 }
317 }
318
319 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900320 void updateActiveSource(int logicalAddress, int physicalAddress) {
321 assertRunOnServiceThread();
322 updateActiveSource(ActiveSource.of(logicalAddress, physicalAddress));
323 }
324
325 @ServiceThreadOnly
326 void updateActiveSource(ActiveSource newActive) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900327 assertRunOnServiceThread();
328 // Seq #14
Amy123ec402018-09-25 10:56:31 -0700329 if (getActiveSource().equals(newActive)) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900330 return;
331 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900332 setActiveSource(newActive);
333 int logicalAddress = newActive.logicalAddress;
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900334 if (getCecDeviceInfo(logicalAddress) != null && logicalAddress != mAddress) {
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900335 if (mService.pathToPortId(newActive.physicalAddress) == getActivePortId()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900336 setPrevPortId(getActivePortId());
337 }
338 // TODO: Show the OSD banner related to the new active source device.
339 } else {
340 // TODO: If displayed, remove the OSD banner related to the previous
341 // active source device.
342 }
343 }
344
345 /**
346 * Returns the previous port id kept to handle input switching on <Inactive Source>.
347 */
348 int getPrevPortId() {
349 synchronized (mLock) {
350 return mPrevPortId;
351 }
352 }
353
354 /**
355 * Sets the previous port id. INVALID_PORT_ID invalidates it, hence no actions will be
356 * taken for <Inactive Source>.
357 */
358 void setPrevPortId(int portId) {
359 synchronized (mLock) {
360 mPrevPortId = portId;
361 }
362 }
363
364 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900365 void updateActiveInput(int path, boolean notifyInputChange) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900366 assertRunOnServiceThread();
367 // Seq #15
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900368 setActivePath(path);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900369 // TODO: Handle PAP/PIP case.
370 // Show OSD port change banner
371 if (notifyInputChange) {
372 ActiveSource activeSource = getActiveSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900373 HdmiDeviceInfo info = getCecDeviceInfo(activeSource.logicalAddress);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900374 if (info == null) {
Jinsuk Kim6ad7cbd2015-01-06 11:30:56 +0900375 info = mService.getDeviceInfoByPort(getActivePortId());
376 if (info == null) {
377 // No CEC/MHL device is present at the port. Attempt to switch to
378 // the hardware port itself for non-CEC devices that may be connected.
379 info = new HdmiDeviceInfo(path, getActivePortId());
380 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900381 }
382 mService.invokeInputChangeListener(info);
383 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900384 }
385
386 @ServiceThreadOnly
387 void doManualPortSwitching(int portId, IHdmiControlCallback callback) {
388 assertRunOnServiceThread();
389 // Seq #20
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900390 if (!mService.isValidPortId(portId)) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900391 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
Jinsuk Kima062a932014-06-18 10:00:39 +0900392 return;
393 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900394 if (portId == getActivePortId()) {
395 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
396 return;
397 }
Amy123ec402018-09-25 10:56:31 -0700398 getActiveSource().invalidate();
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900399 if (!mService.isControlEnabled()) {
400 setActivePortId(portId);
401 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
402 return;
403 }
Jinsuk Kim1f8d1c572014-08-12 15:39:53 +0900404 int oldPath = getActivePortId() != Constants.INVALID_PORT_ID
405 ? mService.portIdToPath(getActivePortId()) : getDeviceInfo().getPhysicalAddress();
Jinsuk Kimd5307192014-08-22 09:07:40 +0900406 setActivePath(oldPath);
407 if (mSkipRoutingControl) {
408 mSkipRoutingControl = false;
409 return;
410 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900411 int newPath = mService.portIdToPath(portId);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900412 startRoutingControl(oldPath, newPath, true, callback);
413 }
414
415 @ServiceThreadOnly
416 void startRoutingControl(int oldPath, int newPath, boolean queryDevicePowerStatus,
417 IHdmiControlCallback callback) {
418 assertRunOnServiceThread();
419 if (oldPath == newPath) {
420 return;
421 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900422 HdmiCecMessage routingChange =
423 HdmiCecMessageBuilder.buildRoutingChange(mAddress, oldPath, newPath);
424 mService.sendCecCommand(routingChange);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900425 removeAction(RoutingControlAction.class);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900426 addAndStartAction(
427 new RoutingControlAction(this, newPath, queryDevicePowerStatus, callback));
Jinsuk Kima062a932014-06-18 10:00:39 +0900428 }
429
Jungshik Jangf67113f2014-08-22 16:27:19 +0900430 @ServiceThreadOnly
Jinsuk Kimcae66272014-07-04 13:26:44 +0900431 int getPowerStatus() {
Jungshik Jangf67113f2014-08-22 16:27:19 +0900432 assertRunOnServiceThread();
Jinsuk Kimcae66272014-07-04 13:26:44 +0900433 return mService.getPowerStatus();
434 }
435
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900436 @Override
Donghyun Cho7609bc32016-12-02 15:31:52 +0900437 protected int findKeyReceiverAddress() {
Jinsuk Kim75434972014-09-25 13:52:50 +0900438 if (getActiveSource().isValid()) {
439 return getActiveSource().logicalAddress;
440 }
441 HdmiDeviceInfo info = getDeviceInfoByPath(getActivePath());
442 if (info != null) {
443 return info.getLogicalAddress();
444 }
445 return Constants.ADDR_INVALID;
446 }
447
Jungshik Jang092b4452014-06-11 15:19:17 +0900448 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900449 @ServiceThreadOnly
Jinsuk Kim83335712014-06-24 07:57:00 +0900450 protected boolean handleActiveSource(HdmiCecMessage message) {
451 assertRunOnServiceThread();
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900452 int logicalAddress = message.getSource();
453 int physicalAddress = HdmiUtils.twoBytesToInt(message.getParams());
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900454 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
455 if (info == null) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900456 if (!handleNewDeviceAtTheTailOfActivePath(physicalAddress)) {
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900457 HdmiLogger.debug("Device info %X not found; buffering the command", logicalAddress);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900458 mDelayedMessageBuffer.add(message);
459 }
Donghyun Cho455f09d2016-05-10 20:06:52 +0900460 } else if (isInputReady(info.getId())
461 || info.getDeviceType() == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) {
Jinsuk Kim7cc51c62015-04-30 10:39:35 +0900462 updateDevicePowerStatus(logicalAddress, HdmiControlManager.POWER_STATUS_ON);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900463 ActiveSource activeSource = ActiveSource.of(logicalAddress, physicalAddress);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900464 ActiveSourceHandler.create(this, null).process(activeSource, info.getDeviceType());
Donghyun Cho455f09d2016-05-10 20:06:52 +0900465 } else {
466 HdmiLogger.debug("Input not ready for device: %X; buffering the command", info.getId());
467 mDelayedMessageBuffer.add(message);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900468 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900469 return true;
470 }
471
472 @Override
473 @ServiceThreadOnly
474 protected boolean handleInactiveSource(HdmiCecMessage message) {
475 assertRunOnServiceThread();
476 // Seq #10
477
478 // Ignore <Inactive Source> from non-active source device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900479 if (getActiveSource().logicalAddress != message.getSource()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900480 return true;
481 }
Jinsuk Kim4d43d932014-07-03 16:43:58 +0900482 if (isProhibitMode()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900483 return true;
484 }
485 int portId = getPrevPortId();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900486 if (portId != Constants.INVALID_PORT_ID) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900487 // TODO: Do this only if TV is not showing multiview like PIP/PAP.
488
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900489 HdmiDeviceInfo inactiveSource = getCecDeviceInfo(message.getSource());
Jinsuk Kim83335712014-06-24 07:57:00 +0900490 if (inactiveSource == null) {
491 return true;
492 }
493 if (mService.pathToPortId(inactiveSource.getPhysicalAddress()) == portId) {
494 return true;
495 }
496 // TODO: Switch the TV freeze mode off
497
Jinsuk Kim83335712014-06-24 07:57:00 +0900498 doManualPortSwitching(portId, null);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900499 setPrevPortId(Constants.INVALID_PORT_ID);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900500 } else {
501 // No HDMI port to switch to was found. Notify the input change listers to
502 // switch to the lastly shown internal input.
Amy123ec402018-09-25 10:56:31 -0700503 getActiveSource().invalidate();
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900504 setActivePath(Constants.INVALID_PHYSICAL_ADDRESS);
505 mService.invokeInputChangeListener(HdmiDeviceInfo.INACTIVE_DEVICE);
Jinsuk Kim83335712014-06-24 07:57:00 +0900506 }
507 return true;
508 }
509
510 @Override
511 @ServiceThreadOnly
512 protected boolean handleRequestActiveSource(HdmiCecMessage message) {
513 assertRunOnServiceThread();
514 // Seq #19
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900515 if (mAddress == getActiveSource().logicalAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900516 mService.sendCecCommand(
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900517 HdmiCecMessageBuilder.buildActiveSource(mAddress, getActivePath()));
Jinsuk Kim83335712014-06-24 07:57:00 +0900518 }
519 return true;
520 }
521
522 @Override
523 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900524 protected boolean handleGetMenuLanguage(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900525 assertRunOnServiceThread();
Jungshik Jangf67113f2014-08-22 16:27:19 +0900526 if (!broadcastMenuLanguage(mService.getLanguage())) {
Jungshik Jang092b4452014-06-11 15:19:17 +0900527 Slog.w(TAG, "Failed to respond to <Get Menu Language>: " + message.toString());
528 }
529 return true;
530 }
531
Terry Heo1ca0a432014-08-18 10:30:32 +0900532 @ServiceThreadOnly
533 boolean broadcastMenuLanguage(String language) {
534 assertRunOnServiceThread();
535 HdmiCecMessage command = HdmiCecMessageBuilder.buildSetMenuLanguageCommand(
536 mAddress, language);
537 if (command != null) {
538 mService.sendCecCommand(command);
539 return true;
540 }
541 return false;
542 }
543
Jungshik Jang60cffce2014-06-12 18:03:04 +0900544 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900545 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900546 protected boolean handleReportPhysicalAddress(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900547 assertRunOnServiceThread();
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900548 int path = HdmiUtils.twoBytesToInt(message.getParams());
549 int address = message.getSource();
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900550 int type = message.getParams()[2];
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900551
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900552 if (updateCecSwitchInfo(address, type, path)) return true;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900553
Jungshik Jang092b4452014-06-11 15:19:17 +0900554 // Ignore if [Device Discovery Action] is going on.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900555 if (hasAction(DeviceDiscoveryAction.class)) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900556 Slog.i(TAG, "Ignored while Device Discovery Action is in progress: " + message);
Jungshik Jang092b4452014-06-11 15:19:17 +0900557 return true;
558 }
559
Jinsuk Kim4b4b9402014-09-02 10:30:10 +0900560 if (!isInDeviceList(address, path)) {
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900561 handleNewDeviceAtTheTailOfActivePath(path);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900562 }
Jinsuk Kim7cd4a582015-01-29 09:50:25 +0900563
564 // Add the device ahead with default information to handle <Active Source>
565 // promptly, rather than waiting till the new device action is finished.
566 HdmiDeviceInfo deviceInfo = new HdmiDeviceInfo(address, path, getPortId(path), type,
567 Constants.UNKNOWN_VENDOR_ID, HdmiUtils.getDefaultDeviceName(address));
568 addCecDevice(deviceInfo);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900569 startNewDeviceAction(ActiveSource.of(address, path), type);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900570 return true;
571 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900572
Jungshik Jang4480efa2014-09-04 17:08:34 +0900573 @Override
574 protected boolean handleReportPowerStatus(HdmiCecMessage command) {
575 int newStatus = command.getParams()[0] & 0xFF;
576 updateDevicePowerStatus(command.getSource(), newStatus);
577 return true;
578 }
579
580 @Override
581 protected boolean handleTimerStatus(HdmiCecMessage message) {
582 // Do nothing.
583 return true;
584 }
585
586 @Override
587 protected boolean handleRecordStatus(HdmiCecMessage message) {
588 // Do nothing.
589 return true;
590 }
591
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900592 boolean updateCecSwitchInfo(int address, int type, int path) {
593 if (address == Constants.ADDR_UNREGISTERED
594 && type == HdmiDeviceInfo.DEVICE_PURE_CEC_SWITCH) {
595 mCecSwitches.add(path);
596 updateSafeDeviceInfoList();
597 return true; // Pure switch does not need further processing. Return here.
598 }
599 if (type == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) {
600 mCecSwitches.add(path);
601 }
602 return false;
603 }
604
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900605 void startNewDeviceAction(ActiveSource activeSource, int deviceType) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900606 for (NewDeviceAction action : getActions(NewDeviceAction.class)) {
607 // If there is new device action which has the same logical address and path
608 // ignore new request.
609 // NewDeviceAction is created whenever it receives <Report Physical Address>.
610 // And there is a chance starting NewDeviceAction for the same source.
611 // Usually, new device sends <Report Physical Address> when it's plugged
612 // in. However, TV can detect a new device from HotPlugDetectionAction,
613 // which sends <Give Physical Address> to the source for newly detected
614 // device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900615 if (action.isActionOf(activeSource)) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900616 return;
617 }
618 }
619
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900620 addAndStartAction(new NewDeviceAction(this, activeSource.logicalAddress,
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900621 activeSource.physicalAddress, deviceType));
Jungshik Jang97affee2014-07-11 17:04:43 +0900622 }
623
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900624 private boolean handleNewDeviceAtTheTailOfActivePath(int path) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900625 // Seq #22
626 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900627 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim7c3a9562014-08-01 11:07:42 +0900628 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900629 startRoutingControl(getActivePath(), newPath, false, null);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900630 return true;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900631 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900632 return false;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900633 }
634
635 /**
636 * Whether the given path is located in the tail of current active path.
637 *
638 * @param path to be tested
639 * @param activePath current active path
640 * @return true if the given path is located in the tail of current active path; otherwise,
641 * false
642 */
643 static boolean isTailOfActivePath(int path, int activePath) {
644 // If active routing path is internal source, return false.
645 if (activePath == 0) {
646 return false;
647 }
648 for (int i = 12; i >= 0; i -= 4) {
649 int curActivePath = (activePath >> i) & 0xF;
650 if (curActivePath == 0) {
651 return true;
652 } else {
653 int curPath = (path >> i) & 0xF;
654 if (curPath != curActivePath) {
655 return false;
656 }
657 }
658 }
659 return false;
660 }
661
662 @Override
663 @ServiceThreadOnly
664 protected boolean handleRoutingChange(HdmiCecMessage message) {
665 assertRunOnServiceThread();
666 // Seq #21
667 byte[] params = message.getParams();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900668 int currentPath = HdmiUtils.twoBytesToInt(params);
669 if (HdmiUtils.isAffectingActiveRoutingPath(getActivePath(), currentPath)) {
Amy123ec402018-09-25 10:56:31 -0700670 getActiveSource().invalidate();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900671 removeAction(RoutingControlAction.class);
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900672 int newPath = HdmiUtils.twoBytesToInt(params, 2);
Jinsuk Kim04fd2802014-07-03 14:04:02 +0900673 addAndStartAction(new RoutingControlAction(this, newPath, true, null));
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900674 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900675 return true;
676 }
Jinsuk Kim0a3316b2014-06-14 09:33:55 +0900677
678 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900679 @ServiceThreadOnly
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900680 protected boolean handleReportAudioStatus(HdmiCecMessage message) {
681 assertRunOnServiceThread();
Marvin Raminda665a62020-03-16 14:17:58 +0100682 if (!mService.isHdmiCecVolumeControlEnabled()) {
683 return false;
684 }
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900685
Shuichi.Noguchifbb50bc2017-12-06 11:12:33 +0900686 boolean mute = HdmiUtils.isAudioStatusMute(message);
687 int volume = HdmiUtils.getAudioStatusVolume(message);
688 setAudioStatus(mute, volume);
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900689 return true;
690 }
691
Yuncheol Heo38db6292014-07-01 14:15:14 +0900692 @Override
693 @ServiceThreadOnly
694 protected boolean handleTextViewOn(HdmiCecMessage message) {
695 assertRunOnServiceThread();
Jinsuk Kimc854c952015-10-12 09:33:33 +0900696
697 // Note that <Text View On> (and <Image View On>) command won't be handled here in
698 // most cases. A dedicated microcontroller should be in charge while Android system
699 // is in sleep mode, and the command need not be passed up to this service.
700 // The only situation where the command reaches this handler is that sleep mode is
701 // implemented in such a way that Android system is not really put to standby mode
702 // but only the display is set to blank. Then the command leads to the effect of
703 // turning on the display by the invocation of PowerManager.wakeUp().
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900704 if (mService.isPowerStandbyOrTransient() && mAutoWakeup) {
Yuncheol Heo38db6292014-07-01 14:15:14 +0900705 mService.wakeUp();
706 }
Yuncheol Heo38db6292014-07-01 14:15:14 +0900707 return true;
708 }
709
710 @Override
711 @ServiceThreadOnly
712 protected boolean handleImageViewOn(HdmiCecMessage message) {
713 assertRunOnServiceThread();
714 // Currently, it's the same as <Text View On>.
715 return handleTextViewOn(message);
716 }
717
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900718 @Override
719 @ServiceThreadOnly
720 protected boolean handleSetOsdName(HdmiCecMessage message) {
721 int source = message.getSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900722 HdmiDeviceInfo deviceInfo = getCecDeviceInfo(source);
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900723 // If the device is not in device list, ignore it.
724 if (deviceInfo == null) {
725 Slog.e(TAG, "No source device info for <Set Osd Name>." + message);
726 return true;
727 }
728 String osdName = null;
729 try {
730 osdName = new String(message.getParams(), "US-ASCII");
731 } catch (UnsupportedEncodingException e) {
732 Slog.e(TAG, "Invalid <Set Osd Name> request:" + message, e);
733 return true;
734 }
735
736 if (deviceInfo.getDisplayName().equals(osdName)) {
737 Slog.i(TAG, "Ignore incoming <Set Osd Name> having same osd name:" + message);
738 return true;
739 }
740
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900741 addCecDevice(new HdmiDeviceInfo(deviceInfo.getLogicalAddress(),
Jinsuk Kim2b152012014-07-25 08:22:26 +0900742 deviceInfo.getPhysicalAddress(), deviceInfo.getPortId(),
743 deviceInfo.getDeviceType(), deviceInfo.getVendorId(), osdName));
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900744 return true;
745 }
746
Jungshik Janga5b74142014-06-23 18:03:10 +0900747 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900748 private void launchDeviceDiscovery() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900749 assertRunOnServiceThread();
750 clearDeviceInfoList();
751 DeviceDiscoveryAction action = new DeviceDiscoveryAction(this,
Jungshik Jang60cffce2014-06-12 18:03:04 +0900752 new DeviceDiscoveryCallback() {
753 @Override
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900754 public void onDeviceDiscoveryDone(List<HdmiDeviceInfo> deviceInfos) {
755 for (HdmiDeviceInfo info : deviceInfos) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900756 addCecDevice(info);
Jungshik Jang60cffce2014-06-12 18:03:04 +0900757 }
758
759 // Since we removed all devices when it's start and
760 // device discovery action does not poll local devices,
761 // we should put device info of local device manually here
762 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900763 addCecDevice(device.getDeviceInfo());
Jungshik Jang60cffce2014-06-12 18:03:04 +0900764 }
765
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900766 mSelectRequestBuffer.process();
767 resetSelectRequestBuffer();
768
Jungshik Jang79c58a42014-06-16 16:45:36 +0900769 addAndStartAction(new HotplugDetectionAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang410ca9c2014-08-07 18:04:14 +0900770 addAndStartAction(new PowerStatusMonitorAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang187d0172014-06-17 17:48:42 +0900771
Jungshik Jang339227d2014-08-25 15:37:20 +0900772 HdmiDeviceInfo avr = getAvrDeviceInfo();
773 if (avr != null) {
774 onNewAvrAdded(avr);
Wally Yau951e3e42015-04-03 15:12:52 -0700775 } else {
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900776 setSystemAudioMode(false);
Jungshik Jang187d0172014-06-17 17:48:42 +0900777 }
Jungshik Jang60cffce2014-06-12 18:03:04 +0900778 }
779 });
Jungshik Jang79c58a42014-06-16 16:45:36 +0900780 addAndStartAction(action);
781 }
782
Jungshik Jang339227d2014-08-25 15:37:20 +0900783 @ServiceThreadOnly
784 void onNewAvrAdded(HdmiDeviceInfo avr) {
785 assertRunOnServiceThread();
Donghyun Choc21f63a2016-05-10 10:37:47 +0900786 addAndStartAction(new SystemAudioAutoInitiationAction(this, avr.getLogicalAddress()));
Donghyun Cho88909ce2016-12-23 14:41:00 +0900787 if (isConnected(avr.getPortId()) && isArcFeatureEnabled(avr.getPortId())
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900788 && !hasAction(SetArcTransmissionStateAction.class)) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900789 startArcAction(true);
790 }
791 }
792
Jungshik Jang79c58a42014-06-16 16:45:36 +0900793 // Clear all device info.
Jungshik Janga5b74142014-06-23 18:03:10 +0900794 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900795 private void clearDeviceInfoList() {
796 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900797 for (HdmiDeviceInfo info : mSafeExternalInputs) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +0900798 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +0900799 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900800 mDeviceInfos.clear();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900801 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900802 }
803
Jungshik Janga5b74142014-06-23 18:03:10 +0900804 @ServiceThreadOnly
Yuncheol Heoc516d652014-07-11 18:23:24 +0900805 // Seq #32
Jungshik Jangea67c182014-06-19 22:19:20 +0900806 void changeSystemAudioMode(boolean enabled, IHdmiControlCallback callback) {
807 assertRunOnServiceThread();
Yuncheol Heoc516d652014-07-11 18:23:24 +0900808 if (!mService.isControlEnabled() || hasAction(DeviceDiscoveryAction.class)) {
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900809 setSystemAudioMode(false);
Yuncheol Heoc516d652014-07-11 18:23:24 +0900810 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
811 return;
812 }
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900813 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jangea67c182014-06-19 22:19:20 +0900814 if (avr == null) {
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900815 setSystemAudioMode(false);
Yuncheol Heod05f67f2014-07-11 16:06:40 +0900816 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
Jungshik Jangea67c182014-06-19 22:19:20 +0900817 return;
818 }
819
820 addAndStartAction(
821 new SystemAudioActionFromTv(this, avr.getLogicalAddress(), enabled, callback));
Jungshik Jangea67c182014-06-19 22:19:20 +0900822 }
823
Jungshik Jangca5be9a2014-07-01 18:01:26 +0900824 // # Seq 25
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900825 void setSystemAudioMode(boolean on) {
826 if (!isSystemAudioControlFeatureEnabled() && on) {
827 HdmiLogger.debug("Cannot turn on system audio mode "
828 + "because the System Audio Control feature is disabled.");
829 return;
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900830 }
Amy489454f2019-01-24 19:06:57 -0800831 HdmiLogger.debug("System Audio Mode change[old:%b new:%b]",
832 mService.isSystemAudioActivated(), on);
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900833 updateAudioManagerForSystemAudio(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900834 synchronized (mLock) {
Amy489454f2019-01-24 19:06:57 -0800835 if (mService.isSystemAudioActivated() != on) {
836 mService.setSystemAudioActivated(on);
Jungshik Jangea67c182014-06-19 22:19:20 +0900837 mService.announceSystemAudioModeChange(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900838 }
Hannah.Hsu45254172019-05-15 15:05:23 +0800839 if (on && !mArcEstablished) {
840 startArcAction(true);
841 } else if (!on) {
842 startArcAction(false);
843 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900844 }
845 }
846
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900847 private void updateAudioManagerForSystemAudio(boolean on) {
Jungshik Jang6b096d32014-09-12 18:49:08 +0900848 int device = mService.getAudioManager().setHdmiSystemAudioSupported(on);
849 HdmiLogger.debug("[A]UpdateSystemAudio mode[on=%b] output=[%X]", on, device);
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900850 }
851
852 boolean isSystemAudioActivated() {
Jinsuk Kim86a1e5a2014-11-19 06:04:50 +0900853 if (!hasSystemAudioDevice()) {
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900854 return false;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900855 }
Amy489454f2019-01-24 19:06:57 -0800856 return mService.isSystemAudioActivated();
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900857 }
858
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900859 @ServiceThreadOnly
860 void setSystemAudioControlFeatureEnabled(boolean enabled) {
861 assertRunOnServiceThread();
862 synchronized (mLock) {
863 mSystemAudioControlFeatureEnabled = enabled;
864 }
865 if (hasSystemAudioDevice()) {
866 changeSystemAudioMode(enabled, null);
867 }
868 }
869
870 boolean isSystemAudioControlFeatureEnabled() {
871 synchronized (mLock) {
872 return mSystemAudioControlFeatureEnabled;
873 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900874 }
875
876 /**
877 * Change ARC status into the given {@code enabled} status.
878 *
879 * @return {@code true} if ARC was in "Enabled" status
880 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900881 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900882 boolean setArcStatus(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900883 assertRunOnServiceThread();
Jungshik Jang6b096d32014-09-12 18:49:08 +0900884
885 HdmiLogger.debug("Set Arc Status[old:%b new:%b]", mArcEstablished, enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900886 boolean oldStatus = mArcEstablished;
887 // 1. Enable/disable ARC circuit.
Donghyun Chobc6e3722016-11-04 05:25:52 +0900888 enableAudioReturnChannel(enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900889 // 2. Notify arc status to audio service.
890 notifyArcStatusToAudioService(enabled);
891 // 3. Update arc status;
892 mArcEstablished = enabled;
893 return oldStatus;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900894 }
895
Jinsuk Kim757c0972015-02-23 10:15:42 +0900896 /**
897 * Switch hardware ARC circuit in the system.
898 */
899 @ServiceThreadOnly
Donghyun Chobc6e3722016-11-04 05:25:52 +0900900 void enableAudioReturnChannel(boolean enabled) {
Jinsuk Kim757c0972015-02-23 10:15:42 +0900901 assertRunOnServiceThread();
902 HdmiDeviceInfo avr = getAvrDeviceInfo();
903 if (avr != null) {
Donghyun Chobc6e3722016-11-04 05:25:52 +0900904 mService.enableAudioReturnChannel(avr.getPortId(), enabled);
Jinsuk Kim757c0972015-02-23 10:15:42 +0900905 }
906 }
907
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +0900908 @ServiceThreadOnly
Jinsuk Kim7b0cf642015-04-14 09:43:45 +0900909 boolean isConnected(int portId) {
910 assertRunOnServiceThread();
911 return mService.isConnected(portId);
912 }
913
Jungshik Janga858d222014-06-23 17:17:47 +0900914 private void notifyArcStatusToAudioService(boolean enabled) {
915 // Note that we don't set any name to ARC.
916 mService.getAudioManager().setWiredDeviceConnectionState(
917 AudioSystem.DEVICE_OUT_HDMI_ARC,
Paul McLean10804eb2015-01-28 11:16:35 -0800918 enabled ? 1 : 0, "", "");
Jungshik Janga858d222014-06-23 17:17:47 +0900919 }
920
Jungshik Jang79c58a42014-06-16 16:45:36 +0900921 /**
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900922 * Returns true if ARC is currently established on a certain port.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900923 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900924 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900925 boolean isArcEstablished() {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900926 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900927 if (mArcEstablished) {
928 for (int i = 0; i < mArcFeatureEnabled.size(); i++) {
929 if (mArcFeatureEnabled.valueAt(i)) return true;
930 }
931 }
932 return false;
Jungshik Janga13da0d2014-06-30 16:26:06 +0900933 }
934
935 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900936 void changeArcFeatureEnabled(int portId, boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900937 assertRunOnServiceThread();
Donghyun Cho88909ce2016-12-23 14:41:00 +0900938 if (mArcFeatureEnabled.get(portId) == enabled) {
939 return;
940 }
941 mArcFeatureEnabled.put(portId, enabled);
942 HdmiDeviceInfo avr = getAvrDeviceInfo();
943 if (avr == null || avr.getPortId() != portId) {
944 return;
945 }
946 if (enabled && !mArcEstablished) {
947 startArcAction(true);
948 } else if (!enabled && mArcEstablished) {
949 startArcAction(false);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900950 }
951 }
952
953 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900954 boolean isArcFeatureEnabled(int portId) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900955 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900956 return mArcFeatureEnabled.get(portId);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900957 }
958
959 @ServiceThreadOnly
Jungshik Jang339227d2014-08-25 15:37:20 +0900960 void startArcAction(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900961 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900962 HdmiDeviceInfo info = getAvrDeviceInfo();
Jungshik Janga13da0d2014-06-30 16:26:06 +0900963 if (info == null) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900964 Slog.w(TAG, "Failed to start arc action; No AVR device.");
Jungshik Janga13da0d2014-06-30 16:26:06 +0900965 return;
966 }
Jungshik Jang339227d2014-08-25 15:37:20 +0900967 if (!canStartArcUpdateAction(info.getLogicalAddress(), enabled)) {
968 Slog.w(TAG, "Failed to start arc action; ARC configuration check failed.");
969 if (enabled && !isConnectedToArcPort(info.getPhysicalAddress())) {
970 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
971 }
Jungshik Janga13da0d2014-06-30 16:26:06 +0900972 return;
973 }
974
975 // Terminate opposite action and start action if not exist.
976 if (enabled) {
977 removeAction(RequestArcTerminationAction.class);
978 if (!hasAction(RequestArcInitiationAction.class)) {
979 addAndStartAction(new RequestArcInitiationAction(this, info.getLogicalAddress()));
980 }
981 } else {
982 removeAction(RequestArcInitiationAction.class);
983 if (!hasAction(RequestArcTerminationAction.class)) {
984 addAndStartAction(new RequestArcTerminationAction(this, info.getLogicalAddress()));
985 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900986 }
987 }
988
Jungshik Jang339227d2014-08-25 15:37:20 +0900989 private boolean isDirectConnectAddress(int physicalAddress) {
990 return (physicalAddress & Constants.ROUTING_PATH_TOP_MASK) == physicalAddress;
991 }
992
Jungshik Jang79c58a42014-06-16 16:45:36 +0900993 void setAudioStatus(boolean mute, int volume) {
Marvin Raminda665a62020-03-16 14:17:58 +0100994 if (!isSystemAudioActivated() || !mService.isHdmiCecVolumeControlEnabled()) {
Shuichi.Noguchi67ed2e92017-11-27 18:57:56 +0900995 return;
996 }
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900997 synchronized (mLock) {
998 mSystemAudioMute = mute;
999 mSystemAudioVolume = volume;
Jungshik Jangb69aafbf2014-07-11 16:29:06 +09001000 int maxVolume = mService.getAudioManager().getStreamMaxVolume(
1001 AudioManager.STREAM_MUSIC);
1002 mService.setAudioStatus(mute,
1003 VolumeControlAction.scaleToCustomVolume(volume, maxVolume));
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001004 displayOsd(HdmiControlManager.OSD_MESSAGE_AVR_VOLUME_CHANGED,
1005 mute ? HdmiControlManager.AVR_VOLUME_MUTED : volume);
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001006 }
1007 }
1008
1009 @ServiceThreadOnly
1010 void changeVolume(int curVolume, int delta, int maxVolume) {
1011 assertRunOnServiceThread();
Shuichi.Noguchiad91e7e2017-11-20 18:58:29 +09001012 if (getAvrDeviceInfo() == null) {
1013 // On initialization process, getAvrDeviceInfo() may return null and cause exception
1014 return;
1015 }
Marvin Raminda665a62020-03-16 14:17:58 +01001016 if (delta == 0 || !isSystemAudioActivated() || !mService.isHdmiCecVolumeControlEnabled()) {
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001017 return;
1018 }
1019
1020 int targetVolume = curVolume + delta;
1021 int cecVolume = VolumeControlAction.scaleToCecVolume(targetVolume, maxVolume);
1022 synchronized (mLock) {
1023 // If new volume is the same as current system audio volume, just ignore it.
1024 // Note that UNKNOWN_VOLUME is not in range of cec volume scale.
1025 if (cecVolume == mSystemAudioVolume) {
1026 // Update tv volume with system volume value.
1027 mService.setAudioStatus(false,
1028 VolumeControlAction.scaleToCustomVolume(mSystemAudioVolume, maxVolume));
1029 return;
1030 }
1031 }
1032
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001033 List<VolumeControlAction> actions = getActions(VolumeControlAction.class);
1034 if (actions.isEmpty()) {
1035 addAndStartAction(new VolumeControlAction(this,
1036 getAvrDeviceInfo().getLogicalAddress(), delta > 0));
1037 } else {
1038 actions.get(0).handleVolumeChange(delta > 0);
1039 }
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001040 }
1041
1042 @ServiceThreadOnly
1043 void changeMute(boolean mute) {
1044 assertRunOnServiceThread();
Marvin Raminda665a62020-03-16 14:17:58 +01001045 if (getAvrDeviceInfo() == null || !mService.isHdmiCecVolumeControlEnabled()) {
Shuichi.Noguchiad91e7e2017-11-20 18:58:29 +09001046 // On initialization process, getAvrDeviceInfo() may return null and cause exception
1047 return;
1048 }
Jungshik Jang6b096d32014-09-12 18:49:08 +09001049 HdmiLogger.debug("[A]:Change mute:%b", mute);
Jungshik Jang720407a2014-09-15 17:48:41 +09001050 synchronized (mLock) {
1051 if (mSystemAudioMute == mute) {
1052 HdmiLogger.debug("No need to change mute.");
1053 return;
1054 }
1055 }
Jungshik Jang377dcbd2014-07-15 15:49:02 +09001056 if (!isSystemAudioActivated()) {
Jungshik Jang6b096d32014-09-12 18:49:08 +09001057 HdmiLogger.debug("[A]:System audio is not activated.");
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001058 return;
1059 }
1060
1061 // Remove existing volume action.
1062 removeAction(VolumeControlAction.class);
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001063 sendUserControlPressedAndReleased(getAvrDeviceInfo().getLogicalAddress(),
Jinsuk Kim0ab37792015-10-17 06:53:52 +09001064 HdmiCecKeycode.getMuteKey(mute));
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001065 }
1066
Jungshik Jang79c58a42014-06-16 16:45:36 +09001067 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001068 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001069 protected boolean handleInitiateArc(HdmiCecMessage message) {
1070 assertRunOnServiceThread();
Jungshik Jang339227d2014-08-25 15:37:20 +09001071
1072 if (!canStartArcUpdateAction(message.getSource(), true)) {
Kyeongkab.Namab27ffb2018-10-19 16:35:14 +09001073 HdmiDeviceInfo avrDeviceInfo = getAvrDeviceInfo();
1074 if (avrDeviceInfo == null) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001075 // AVR may not have been discovered yet. Delay the message processing.
1076 mDelayedMessageBuffer.add(message);
1077 return true;
1078 }
Jungshik Jang339227d2014-08-25 15:37:20 +09001079 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
Kyeongkab.Namab27ffb2018-10-19 16:35:14 +09001080 if (!isConnectedToArcPort(avrDeviceInfo.getPhysicalAddress())) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001081 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
1082 }
1083 return true;
1084 }
1085
Jungshik Jang79c58a42014-06-16 16:45:36 +09001086 // In case where <Initiate Arc> is started by <Request ARC Initiation>
1087 // need to clean up RequestArcInitiationAction.
1088 removeAction(RequestArcInitiationAction.class);
1089 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1090 message.getSource(), true);
1091 addAndStartAction(action);
1092 return true;
1093 }
1094
Donghyun Cho88909ce2016-12-23 14:41:00 +09001095 private boolean canStartArcUpdateAction(int avrAddress, boolean enabled) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001096 HdmiDeviceInfo avr = getAvrDeviceInfo();
1097 if (avr != null
1098 && (avrAddress == avr.getLogicalAddress())
1099 && isConnectedToArcPort(avr.getPhysicalAddress())
1100 && isDirectConnectAddress(avr.getPhysicalAddress())) {
Donghyun Cho88909ce2016-12-23 14:41:00 +09001101 if (enabled) {
1102 return isConnected(avr.getPortId()) && isArcFeatureEnabled(avr.getPortId());
Jungshik Jang339227d2014-08-25 15:37:20 +09001103 } else {
1104 return true;
1105 }
1106 } else {
1107 return false;
1108 }
1109 }
1110
Jungshik Jang79c58a42014-06-16 16:45:36 +09001111 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001112 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001113 protected boolean handleTerminateArc(HdmiCecMessage message) {
1114 assertRunOnServiceThread();
Jinsuk Kim7e4b4802015-04-20 13:17:13 +09001115 if (mService .isPowerStandbyOrTransient()) {
1116 setArcStatus(false);
1117 return true;
1118 }
1119 // Do not check ARC configuration since the AVR might have been already removed.
1120 // Clean up RequestArcTerminationAction in case <Terminate Arc> was started by
1121 // <Request ARC Termination>.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001122 removeAction(RequestArcTerminationAction.class);
1123 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1124 message.getSource(), false);
1125 addAndStartAction(action);
1126 return true;
1127 }
1128
1129 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001130 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001131 protected boolean handleSetSystemAudioMode(HdmiCecMessage message) {
1132 assertRunOnServiceThread();
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001133 boolean systemAudioStatus = HdmiUtils.parseCommandParamSystemAudioStatus(message);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001134 if (!isMessageForSystemAudio(message)) {
Jinsuk Kimad1e3d72015-01-02 13:27:15 +09001135 if (getAvrDeviceInfo() == null) {
1136 // AVR may not have been discovered yet. Delay the message processing.
1137 mDelayedMessageBuffer.add(message);
Donghyun Choc21f63a2016-05-10 10:37:47 +09001138 } else {
1139 HdmiLogger.warning("Invalid <Set System Audio Mode> message:" + message);
1140 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
Jinsuk Kimad1e3d72015-01-02 13:27:15 +09001141 }
Jungshik Jang4480efa2014-09-04 17:08:34 +09001142 return true;
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001143 } else if (systemAudioStatus && !isSystemAudioControlFeatureEnabled()) {
1144 HdmiLogger.debug("Ignoring <Set System Audio Mode> message "
1145 + "because the System Audio Control feature is disabled: %s", message);
1146 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
1147 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001148 }
Donghyun Choc21f63a2016-05-10 10:37:47 +09001149 removeAction(SystemAudioAutoInitiationAction.class);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001150 SystemAudioActionFromAvr action = new SystemAudioActionFromAvr(this,
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001151 message.getSource(), systemAudioStatus, null);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001152 addAndStartAction(action);
1153 return true;
1154 }
1155
1156 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001157 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001158 protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) {
1159 assertRunOnServiceThread();
1160 if (!isMessageForSystemAudio(message)) {
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001161 HdmiLogger.warning("Invalid <System Audio Mode Status> message:" + message);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001162 // Ignore this message.
1163 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001164 }
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001165 setSystemAudioMode(HdmiUtils.parseCommandParamSystemAudioStatus(message));
Jungshik Jang79c58a42014-06-16 16:45:36 +09001166 return true;
1167 }
1168
Jungshik Jangb6591b82014-07-23 16:10:23 +09001169 // Seq #53
1170 @Override
1171 @ServiceThreadOnly
1172 protected boolean handleRecordTvScreen(HdmiCecMessage message) {
1173 List<OneTouchRecordAction> actions = getActions(OneTouchRecordAction.class);
1174 if (!actions.isEmpty()) {
1175 // Assumes only one OneTouchRecordAction.
1176 OneTouchRecordAction action = actions.get(0);
1177 if (action.getRecorderAddress() != message.getSource()) {
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001178 announceOneTouchRecordResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001179 message.getSource(),
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001180 HdmiControlManager.ONE_TOUCH_RECORD_PREVIOUS_RECORDING_IN_PROGRESS);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001181 }
1182 return super.handleRecordTvScreen(message);
1183 }
1184
1185 int recorderAddress = message.getSource();
1186 byte[] recordSource = mService.invokeRecordRequestListener(recorderAddress);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001187 int reason = startOneTouchRecord(recorderAddress, recordSource);
1188 if (reason != Constants.ABORT_NO_ERROR) {
1189 mService.maySendFeatureAbortCommand(message, reason);
1190 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001191 return true;
1192 }
1193
Jungshik Jange5a93372014-07-25 13:41:14 +09001194 @Override
1195 protected boolean handleTimerClearedStatus(HdmiCecMessage message) {
1196 byte[] params = message.getParams();
Jungshik Jange9e0f072014-08-05 17:48:59 +09001197 int timerClearedStatusData = params[0] & 0xFF;
Jungshik Jang326aef02014-11-05 12:50:35 +09001198 announceTimerRecordingResult(message.getSource(), timerClearedStatusData);
Jungshik Jange5a93372014-07-25 13:41:14 +09001199 return true;
1200 }
1201
Jungshik Jang326aef02014-11-05 12:50:35 +09001202 void announceOneTouchRecordResult(int recorderAddress, int result) {
1203 mService.invokeOneTouchRecordResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001204 }
1205
Jungshik Jang326aef02014-11-05 12:50:35 +09001206 void announceTimerRecordingResult(int recorderAddress, int result) {
1207 mService.invokeTimerRecordingResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001208 }
1209
Jungshik Jang326aef02014-11-05 12:50:35 +09001210 void announceClearTimerRecordingResult(int recorderAddress, int result) {
1211 mService.invokeClearTimerRecordingResult(recorderAddress, result);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001212 }
1213
Jungshik Jang79c58a42014-06-16 16:45:36 +09001214 private boolean isMessageForSystemAudio(HdmiCecMessage message) {
Jungshik Jang4480efa2014-09-04 17:08:34 +09001215 return mService.isControlEnabled()
1216 && message.getSource() == Constants.ADDR_AUDIO_SYSTEM
Jungshik Jang473119f2014-08-27 16:43:22 +09001217 && (message.getDestination() == Constants.ADDR_TV
1218 || message.getDestination() == Constants.ADDR_BROADCAST)
1219 && getAvrDeviceInfo() != null;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001220 }
1221
1222 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001223 * Add a new {@link HdmiDeviceInfo}. It returns old device info which has the same
Jungshik Jang79c58a42014-06-16 16:45:36 +09001224 * logical address as new device info's.
1225 *
1226 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1227 *
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001228 * @param deviceInfo a new {@link HdmiDeviceInfo} to be added.
1229 * @return {@code null} if it is new device. Otherwise, returns old {@HdmiDeviceInfo}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001230 * that has the same logical address as new one has.
1231 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001232 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001233 private HdmiDeviceInfo addDeviceInfo(HdmiDeviceInfo deviceInfo) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001234 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001235 HdmiDeviceInfo oldDeviceInfo = getCecDeviceInfo(deviceInfo.getLogicalAddress());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001236 if (oldDeviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001237 removeDeviceInfo(deviceInfo.getId());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001238 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001239 mDeviceInfos.append(deviceInfo.getId(), deviceInfo);
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001240 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001241 return oldDeviceInfo;
1242 }
1243
1244 /**
1245 * Remove a device info corresponding to the given {@code logicalAddress}.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001246 * It returns removed {@link HdmiDeviceInfo} if exists.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001247 *
1248 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1249 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001250 * @param id id of device to be removed
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001251 * @return removed {@link HdmiDeviceInfo} it exists. Otherwise, returns {@code null}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001252 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001253 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001254 private HdmiDeviceInfo removeDeviceInfo(int id) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001255 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001256 HdmiDeviceInfo deviceInfo = mDeviceInfos.get(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001257 if (deviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001258 mDeviceInfos.remove(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001259 }
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001260 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001261 return deviceInfo;
1262 }
1263
1264 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001265 * Return a list of all {@link HdmiDeviceInfo}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001266 *
1267 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
Jungshik Jang339227d2014-08-25 15:37:20 +09001268 * This is not thread-safe. For thread safety, call {@link #getSafeExternalInputsLocked} which
Jungshik Jang8e93c842014-08-06 15:48:33 +09001269 * does not include local device.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001270 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001271 @ServiceThreadOnly
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001272 List<HdmiDeviceInfo> getDeviceInfoList(boolean includeLocalDevice) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001273 assertRunOnServiceThread();
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001274 if (includeLocalDevice) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001275 return HdmiUtils.sparseArrayToList(mDeviceInfos);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001276 } else {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001277 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001278 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001279 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001280 if (!isLocalDeviceAddress(info.getLogicalAddress())) {
1281 infoList.add(info);
1282 }
1283 }
1284 return infoList;
1285 }
1286 }
1287
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001288 /**
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001289 * Return external input devices.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001290 */
Andreas Gampea36dc622018-02-05 17:19:22 -08001291 @GuardedBy("mLock")
Jinsuk Kimed086452014-08-18 15:01:53 +09001292 List<HdmiDeviceInfo> getSafeExternalInputsLocked() {
1293 return mSafeExternalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001294 }
1295
Jungshik Janga5b74142014-06-23 18:03:10 +09001296 @ServiceThreadOnly
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001297 private void updateSafeDeviceInfoList() {
1298 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001299 List<HdmiDeviceInfo> copiedDevices = HdmiUtils.sparseArrayToList(mDeviceInfos);
1300 List<HdmiDeviceInfo> externalInputs = getInputDevices();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001301 synchronized (mLock) {
1302 mSafeAllDeviceInfos = copiedDevices;
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001303 mSafeExternalInputs = externalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001304 }
1305 }
1306
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001307 /**
1308 * Return a list of external cec input (source) devices.
1309 *
1310 * <p>Note that this effectively excludes non-source devices like system audio,
1311 * secondary TV.
1312 */
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001313 private List<HdmiDeviceInfo> getInputDevices() {
1314 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001315 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001316 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001317 if (isLocalDeviceAddress(info.getLogicalAddress())) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001318 continue;
1319 }
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001320 if (info.isSourceType() && !hideDevicesBehindLegacySwitch(info)) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001321 infoList.add(info);
1322 }
1323 }
1324 return infoList;
1325 }
1326
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001327 // Check if we are hiding CEC devices connected to a legacy (non-CEC) switch.
1328 // Returns true if the policy is set to true, and the device to check does not have
1329 // a parent CEC device (which should be the CEC-enabled switch) in the list.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001330 private boolean hideDevicesBehindLegacySwitch(HdmiDeviceInfo info) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001331 return HdmiConfig.HIDE_DEVICES_BEHIND_LEGACY_SWITCH
1332 && !isConnectedToCecSwitch(info.getPhysicalAddress(), mCecSwitches);
1333 }
1334
1335 private static boolean isConnectedToCecSwitch(int path, Collection<Integer> switches) {
1336 for (int switchPath : switches) {
1337 if (isParentPath(switchPath, path)) {
1338 return true;
1339 }
1340 }
1341 return false;
1342 }
1343
1344 private static boolean isParentPath(int parentPath, int childPath) {
1345 // (A000, AB00) (AB00, ABC0), (ABC0, ABCD)
1346 // If child's last non-zero nibble is removed, the result equals to the parent.
1347 for (int i = 0; i <= 12; i += 4) {
1348 int nibble = (childPath >> i) & 0xF;
1349 if (nibble != 0) {
1350 int parentNibble = (parentPath >> i) & 0xF;
1351 return parentNibble == 0 && (childPath >> i+4) == (parentPath >> i+4);
1352 }
1353 }
1354 return false;
1355 }
1356
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001357 private void invokeDeviceEventListener(HdmiDeviceInfo info, int status) {
Jinsuk Kim98d760e2014-12-23 07:01:51 +09001358 if (!hideDevicesBehindLegacySwitch(info)) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001359 mService.invokeDeviceEventListeners(info, status);
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001360 }
1361 }
1362
Jungshik Jang79c58a42014-06-16 16:45:36 +09001363 private boolean isLocalDeviceAddress(int address) {
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001364 return mLocalDeviceAddresses.contains(address);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001365 }
1366
Jungshik Jange9cf1582014-06-23 17:28:58 +09001367 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001368 HdmiDeviceInfo getAvrDeviceInfo() {
Jungshik Jange9cf1582014-06-23 17:28:58 +09001369 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001370 return getCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jange9cf1582014-06-23 17:28:58 +09001371 }
1372
Jungshik Jang79c58a42014-06-16 16:45:36 +09001373 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001374 * Return a {@link HdmiDeviceInfo} corresponding to the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001375 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001376 * This is not thread-safe. For thread safety, call {@link #getSafeCecDeviceInfo(int)}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001377 *
Jungshik Jang339227d2014-08-25 15:37:20 +09001378 * @param logicalAddress logical address of the device to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001379 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001380 * Returns null if no logical address matched
1381 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001382 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001383 HdmiDeviceInfo getCecDeviceInfo(int logicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001384 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001385 return mDeviceInfos.get(HdmiDeviceInfo.idForCecDevice(logicalAddress));
Jungshik Jang79c58a42014-06-16 16:45:36 +09001386 }
1387
Jungshik Jange9cf1582014-06-23 17:28:58 +09001388 boolean hasSystemAudioDevice() {
1389 return getSafeAvrDeviceInfo() != null;
1390 }
1391
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001392 HdmiDeviceInfo getSafeAvrDeviceInfo() {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001393 return getSafeCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001394 }
1395
1396 /**
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001397 * Thread safe version of {@link #getCecDeviceInfo(int)}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001398 *
1399 * @param logicalAddress logical address to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001400 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001401 * Returns null if no logical address matched
1402 */
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001403 HdmiDeviceInfo getSafeCecDeviceInfo(int logicalAddress) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001404 synchronized (mLock) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001405 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1406 if (info.isCecDevice() && info.getLogicalAddress() == logicalAddress) {
1407 return info;
1408 }
1409 }
1410 return null;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001411 }
1412 }
1413
Andreas Gampea36dc622018-02-05 17:19:22 -08001414 @GuardedBy("mLock")
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001415 List<HdmiDeviceInfo> getSafeCecDevicesLocked() {
1416 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
1417 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1418 if (isLocalDeviceAddress(info.getLogicalAddress())) {
1419 continue;
1420 }
1421 infoList.add(info);
1422 }
1423 return infoList;
1424 }
1425
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001426 /**
Jungshik Jang8f2ed352014-07-07 15:02:47 +09001427 * Called when a device is newly added or a new device is detected or
1428 * existing device is updated.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001429 *
1430 * @param info device info of a new device.
1431 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001432 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001433 final void addCecDevice(HdmiDeviceInfo info) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001434 assertRunOnServiceThread();
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001435 HdmiDeviceInfo old = addDeviceInfo(info);
Jinsuk Kim13c030e2014-06-20 13:25:17 +09001436 if (info.getLogicalAddress() == mAddress) {
1437 // The addition of TV device itself should not be notified.
1438 return;
1439 }
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001440 if (old == null) {
1441 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
1442 } else if (!old.equals(info)) {
Jinsuk Kim4fcbf0b2014-12-09 06:48:13 +09001443 invokeDeviceEventListener(old, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
1444 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001445 }
Jungshik Jang79c58a42014-06-16 16:45:36 +09001446 }
1447
1448 /**
1449 * Called when a device is removed or removal of device is detected.
1450 *
1451 * @param address a logical address of a device to be removed
1452 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001453 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001454 final void removeCecDevice(int address) {
1455 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001456 HdmiDeviceInfo info = removeDeviceInfo(HdmiDeviceInfo.idForCecDevice(address));
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001457
Jungshik Jang79c58a42014-06-16 16:45:36 +09001458 mCecMessageCache.flushMessagesFrom(address);
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001459 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001460 }
1461
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001462 @ServiceThreadOnly
1463 void handleRemoveActiveRoutingPath(int path) {
1464 assertRunOnServiceThread();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001465 // Seq #23
1466 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001467 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim546d8672014-11-24 07:30:54 +09001468 startRoutingControl(getActivePath(), newPath, true, null);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001469 }
1470 }
1471
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001472 /**
1473 * Launch routing control process.
1474 *
1475 * @param routingForBootup true if routing control is initiated due to One Touch Play
1476 * or TV power on
1477 */
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001478 @ServiceThreadOnly
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001479 void launchRoutingControl(boolean routingForBootup) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001480 assertRunOnServiceThread();
1481 // Seq #24
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001482 if (getActivePortId() != Constants.INVALID_PORT_ID) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001483 if (!routingForBootup && !isProhibitMode()) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001484 int newPath = mService.portIdToPath(getActivePortId());
1485 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +09001486 startRoutingControl(getActivePath(), newPath, routingForBootup, null);
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001487 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001488 } else {
1489 int activePath = mService.getPhysicalAddress();
1490 setActivePath(activePath);
Jinsuk Kim2ab6d9f2015-01-16 15:49:16 +09001491 if (!routingForBootup
1492 && !mDelayedMessageBuffer.isBuffered(Constants.MESSAGE_ACTIVE_SOURCE)) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001493 mService.sendCecCommand(HdmiCecMessageBuilder.buildActiveSource(mAddress,
1494 activePath));
1495 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001496 }
1497 }
1498
Jungshik Jang79c58a42014-06-16 16:45:36 +09001499 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001500 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
Jungshik Jang79c58a42014-06-16 16:45:36 +09001501 * the given routing path. CEC devices use routing path for its physical address to
1502 * describe the hierarchy of the devices in the network.
1503 *
1504 * @param path routing path or physical address
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001505 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
Jungshik Jang79c58a42014-06-16 16:45:36 +09001506 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001507 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001508 final HdmiDeviceInfo getDeviceInfoByPath(int path) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001509 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001510 for (HdmiDeviceInfo info : getDeviceInfoList(false)) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001511 if (info.getPhysicalAddress() == path) {
1512 return info;
1513 }
1514 }
1515 return null;
1516 }
1517
1518 /**
Jinsuk Kim7640d982015-01-28 16:44:07 +09001519 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
1520 * the given routing path. This is the version accessible safely from threads
1521 * other than service thread.
1522 *
1523 * @param path routing path or physical address
1524 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
1525 */
1526 HdmiDeviceInfo getSafeDeviceInfoByPath(int path) {
1527 synchronized (mLock) {
1528 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1529 if (info.getPhysicalAddress() == path) {
1530 return info;
1531 }
1532 }
1533 return null;
1534 }
1535 }
1536
1537 /**
Jungshik Jang79c58a42014-06-16 16:45:36 +09001538 * Whether a device of the specified physical address and logical address exists
1539 * in a device info list. However, both are minimal condition and it could
1540 * be different device from the original one.
1541 *
Jungshik Jang79c58a42014-06-16 16:45:36 +09001542 * @param logicalAddress logical address of a device to be searched
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001543 * @param physicalAddress physical address of a device to be searched
Jungshik Jang79c58a42014-06-16 16:45:36 +09001544 * @return true if exist; otherwise false
1545 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001546 @ServiceThreadOnly
Jinsuk Kim7cd4a582015-01-29 09:50:25 +09001547 boolean isInDeviceList(int logicalAddress, int physicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001548 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001549 HdmiDeviceInfo device = getCecDeviceInfo(logicalAddress);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001550 if (device == null) {
1551 return false;
1552 }
1553 return device.getPhysicalAddress() == physicalAddress;
1554 }
1555
1556 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001557 @ServiceThreadOnly
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001558 void onHotplug(int portId, boolean connected) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001559 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001560
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001561 if (!connected) {
1562 removeCecSwitches(portId);
1563 }
Amy07ed99c2019-10-22 18:33:53 -07001564
1565 // Turning System Audio Mode off when the AVR is unlugged or standby.
1566 // When the device is not unplugged but reawaken from standby, we check if the System
1567 // Audio Control Feature is enabled or not then decide if turning SAM on/off accordingly.
1568 if (getAvrDeviceInfo() != null && portId == getAvrDeviceInfo().getPortId()) {
1569 if (!connected) {
1570 setSystemAudioMode(false);
1571 } else if (mSystemAudioControlFeatureEnabled != mService.isSystemAudioActivated()){
1572 setSystemAudioMode(mSystemAudioControlFeatureEnabled);
1573 }
1574 }
1575
Jungshik Jang79c58a42014-06-16 16:45:36 +09001576 // Tv device will have permanent HotplugDetectionAction.
1577 List<HotplugDetectionAction> hotplugActions = getActions(HotplugDetectionAction.class);
1578 if (!hotplugActions.isEmpty()) {
1579 // Note that hotplug action is single action running on a machine.
1580 // "pollAllDevicesNow" cleans up timer and start poll action immediately.
Jungshik Jang24c23c12014-07-07 18:04:39 +09001581 // It covers seq #40, #43.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001582 hotplugActions.get(0).pollAllDevicesNow();
1583 }
Jungshik Jang60cffce2014-06-12 18:03:04 +09001584 }
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001585
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001586 private void removeCecSwitches(int portId) {
1587 Iterator<Integer> it = mCecSwitches.iterator();
1588 while (!it.hasNext()) {
1589 int path = it.next();
1590 if (pathToPortId(path) == portId) {
1591 it.remove();
1592 }
1593 }
1594 }
1595
Jinsuk Kime6e8f3d2015-05-11 14:17:04 +09001596 @Override
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001597 @ServiceThreadOnly
1598 void setAutoDeviceOff(boolean enabled) {
1599 assertRunOnServiceThread();
1600 mAutoDeviceOff = enabled;
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001601 }
1602
1603 @ServiceThreadOnly
1604 void setAutoWakeup(boolean enabled) {
1605 assertRunOnServiceThread();
1606 mAutoWakeup = enabled;
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001607 }
Yuncheol Heo38db6292014-07-01 14:15:14 +09001608
Yuncheol Heo25c20292014-07-31 17:59:39 +09001609 @ServiceThreadOnly
1610 boolean getAutoWakeup() {
1611 assertRunOnServiceThread();
1612 return mAutoWakeup;
1613 }
1614
Yuncheol Heo38db6292014-07-01 14:15:14 +09001615 @Override
1616 @ServiceThreadOnly
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001617 protected void disableDevice(boolean initiatedByCec, PendingActionClearedCallback callback) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001618 assertRunOnServiceThread();
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001619 mService.unregisterTvInputCallback(mTvInputCallback);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001620 // Remove any repeated working actions.
1621 // HotplugDetectionAction will be reinstated during the wake up process.
1622 // HdmiControlService.onWakeUp() -> initializeLocalDevices() ->
1623 // LocalDeviceTv.onAddressAllocated() -> launchDeviceDiscovery().
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001624 removeAction(DeviceDiscoveryAction.class);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001625 removeAction(HotplugDetectionAction.class);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001626 removeAction(PowerStatusMonitorAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001627 // Remove recording actions.
Jungshik Jangb6591b82014-07-23 16:10:23 +09001628 removeAction(OneTouchRecordAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001629 removeAction(TimerRecordingAction.class);
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001630
1631 disableSystemAudioIfExist();
1632 disableArcIfExist();
Jinsuk Kima5445ce2015-03-30 17:14:58 +09001633
1634 super.disableDevice(initiatedByCec, callback);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +09001635 clearDeviceInfoList();
Jinsuk Kim7cc51c62015-04-30 10:39:35 +09001636 getActiveSource().invalidate();
1637 setActivePath(Constants.INVALID_PHYSICAL_ADDRESS);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001638 checkIfPendingActionsCleared();
1639 }
1640
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001641 @ServiceThreadOnly
1642 private void disableSystemAudioIfExist() {
1643 assertRunOnServiceThread();
1644 if (getAvrDeviceInfo() == null) {
1645 return;
1646 }
1647
1648 // Seq #31.
1649 removeAction(SystemAudioActionFromAvr.class);
1650 removeAction(SystemAudioActionFromTv.class);
1651 removeAction(SystemAudioAutoInitiationAction.class);
1652 removeAction(SystemAudioStatusAction.class);
1653 removeAction(VolumeControlAction.class);
Hannah.Hsu4ee736c2019-05-15 15:38:28 +08001654
1655 if (!mService.isControlEnabled()) {
1656 setSystemAudioMode(false);
1657 }
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001658 }
1659
1660 @ServiceThreadOnly
1661 private void disableArcIfExist() {
1662 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001663 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001664 if (avr == null) {
1665 return;
1666 }
1667
1668 // Seq #44.
1669 removeAction(RequestArcInitiationAction.class);
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +09001670 if (!hasAction(RequestArcTerminationAction.class) && isArcEstablished()) {
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001671 addAndStartAction(new RequestArcTerminationAction(this, avr.getLogicalAddress()));
1672 }
1673 }
1674
Yuncheol Heo38db6292014-07-01 14:15:14 +09001675 @Override
1676 @ServiceThreadOnly
Jinsuk Kime6e8f3d2015-05-11 14:17:04 +09001677 protected void onStandby(boolean initiatedByCec, int standbyAction) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001678 assertRunOnServiceThread();
1679 // Seq #11
1680 if (!mService.isControlEnabled()) {
1681 return;
1682 }
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001683 if (!initiatedByCec && mAutoDeviceOff) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001684 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001685 mAddress, Constants.ADDR_BROADCAST));
Yuncheol Heo38db6292014-07-01 14:15:14 +09001686 }
1687 }
1688
Jinsuk Kim4d43d932014-07-03 16:43:58 +09001689 boolean isProhibitMode() {
1690 return mService.isProhibitMode();
1691 }
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001692
1693 boolean isPowerStandbyOrTransient() {
1694 return mService.isPowerStandbyOrTransient();
Jungshik Jang8866c812014-07-08 14:42:28 +09001695 }
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001696
Jungshik Jang339227d2014-08-25 15:37:20 +09001697 @ServiceThreadOnly
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001698 void displayOsd(int messageId) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001699 assertRunOnServiceThread();
1700 mService.displayOsd(messageId);
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001701 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001702
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001703 @ServiceThreadOnly
1704 void displayOsd(int messageId, int extra) {
1705 assertRunOnServiceThread();
1706 mService.displayOsd(messageId, extra);
1707 }
1708
Jungshik Jangb6591b82014-07-23 16:10:23 +09001709 // Seq #54 and #55
1710 @ServiceThreadOnly
Jungshik Jang4480efa2014-09-04 17:08:34 +09001711 int startOneTouchRecord(int recorderAddress, byte[] recordSource) {
Jungshik Jangb6591b82014-07-23 16:10:23 +09001712 assertRunOnServiceThread();
1713 if (!mService.isControlEnabled()) {
1714 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001715 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001716 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001717 }
1718
1719 if (!checkRecorder(recorderAddress)) {
1720 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001721 announceOneTouchRecordResult(recorderAddress,
1722 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001723 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001724 }
1725
1726 if (!checkRecordSource(recordSource)) {
1727 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001728 announceOneTouchRecordResult(recorderAddress,
1729 ONE_TOUCH_RECORD_FAIL_TO_RECORD_DISPLAYED_SCREEN);
Jinsuk Kimd47abef2015-01-17 07:38:24 +09001730 return Constants.ABORT_CANNOT_PROVIDE_SOURCE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001731 }
1732
1733 addAndStartAction(new OneTouchRecordAction(this, recorderAddress, recordSource));
1734 Slog.i(TAG, "Start new [One Touch Record]-Target:" + recorderAddress + ", recordSource:"
1735 + Arrays.toString(recordSource));
Jungshik Jang4480efa2014-09-04 17:08:34 +09001736 return Constants.ABORT_NO_ERROR;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001737 }
1738
1739 @ServiceThreadOnly
1740 void stopOneTouchRecord(int recorderAddress) {
1741 assertRunOnServiceThread();
1742 if (!mService.isControlEnabled()) {
1743 Slog.w(TAG, "Can not stop one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001744 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001745 return;
1746 }
1747
1748 if (!checkRecorder(recorderAddress)) {
1749 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001750 announceOneTouchRecordResult(recorderAddress,
1751 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001752 return;
1753 }
1754
1755 // Remove one touch record action so that other one touch record can be started.
1756 removeAction(OneTouchRecordAction.class);
1757 mService.sendCecCommand(HdmiCecMessageBuilder.buildRecordOff(mAddress, recorderAddress));
1758 Slog.i(TAG, "Stop [One Touch Record]-Target:" + recorderAddress);
1759 }
1760
1761 private boolean checkRecorder(int recorderAddress) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001762 HdmiDeviceInfo device = getCecDeviceInfo(recorderAddress);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001763 return (device != null)
1764 && (HdmiUtils.getTypeFromAddress(recorderAddress)
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001765 == HdmiDeviceInfo.DEVICE_RECORDER);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001766 }
1767
1768 private boolean checkRecordSource(byte[] recordSource) {
1769 return (recordSource != null) && HdmiRecordSources.checkRecordSource(recordSource);
1770 }
1771
1772 @ServiceThreadOnly
1773 void startTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1774 assertRunOnServiceThread();
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001775 if (!mService.isControlEnabled()) {
1776 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001777 announceTimerRecordingResult(recorderAddress,
1778 TIMER_RECORDING_RESULT_EXTRA_CEC_DISABLED);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001779 return;
1780 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001781
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001782 if (!checkRecorder(recorderAddress)) {
1783 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001784 announceTimerRecordingResult(recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001785 TIMER_RECORDING_RESULT_EXTRA_CHECK_RECORDER_CONNECTION);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001786 return;
1787 }
1788
1789 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1790 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
1791 announceTimerRecordingResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001792 recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001793 TIMER_RECORDING_RESULT_EXTRA_FAIL_TO_RECORD_SELECTED_SOURCE);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001794 return;
1795 }
1796
1797 addAndStartAction(
1798 new TimerRecordingAction(this, recorderAddress, sourceType, recordSource));
1799 Slog.i(TAG, "Start [Timer Recording]-Target:" + recorderAddress + ", SourceType:"
1800 + sourceType + ", RecordSource:" + Arrays.toString(recordSource));
1801 }
1802
1803 private boolean checkTimerRecordingSource(int sourceType, byte[] recordSource) {
1804 return (recordSource != null)
1805 && HdmiTimerRecordSources.checkTimerRecordSource(sourceType, recordSource);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001806 }
1807
1808 @ServiceThreadOnly
1809 void clearTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1810 assertRunOnServiceThread();
Jungshik Jange5a93372014-07-25 13:41:14 +09001811 if (!mService.isControlEnabled()) {
1812 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001813 announceClearTimerRecordingResult(recorderAddress, CLEAR_TIMER_STATUS_CEC_DISABLE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001814 return;
1815 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001816
Jungshik Jange5a93372014-07-25 13:41:14 +09001817 if (!checkRecorder(recorderAddress)) {
1818 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001819 announceClearTimerRecordingResult(recorderAddress,
1820 CLEAR_TIMER_STATUS_CHECK_RECORDER_CONNECTION);
Jungshik Jange5a93372014-07-25 13:41:14 +09001821 return;
1822 }
1823
1824 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1825 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001826 announceClearTimerRecordingResult(recorderAddress,
1827 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001828 return;
1829 }
1830
1831 sendClearTimerMessage(recorderAddress, sourceType, recordSource);
1832 }
1833
Jungshik Jang326aef02014-11-05 12:50:35 +09001834 private void sendClearTimerMessage(final int recorderAddress, int sourceType,
1835 byte[] recordSource) {
Jungshik Jange5a93372014-07-25 13:41:14 +09001836 HdmiCecMessage message = null;
1837 switch (sourceType) {
1838 case TIMER_RECORDING_TYPE_DIGITAL:
1839 message = HdmiCecMessageBuilder.buildClearDigitalTimer(mAddress, recorderAddress,
1840 recordSource);
1841 break;
1842 case TIMER_RECORDING_TYPE_ANALOGUE:
1843 message = HdmiCecMessageBuilder.buildClearAnalogueTimer(mAddress, recorderAddress,
1844 recordSource);
1845 break;
1846 case TIMER_RECORDING_TYPE_EXTERNAL:
1847 message = HdmiCecMessageBuilder.buildClearExternalTimer(mAddress, recorderAddress,
1848 recordSource);
1849 break;
1850 default:
1851 Slog.w(TAG, "Invalid source type:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001852 announceClearTimerRecordingResult(recorderAddress,
1853 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001854 return;
1855
1856 }
1857 mService.sendCecCommand(message, new SendMessageCallback() {
1858 @Override
1859 public void onSendCompleted(int error) {
Donghyun Chobc6e3722016-11-04 05:25:52 +09001860 if (error != SendMessageResult.SUCCESS) {
Jungshik Jang326aef02014-11-05 12:50:35 +09001861 announceClearTimerRecordingResult(recorderAddress,
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001862 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001863 }
1864 }
1865 });
Jungshik Jangb6591b82014-07-23 16:10:23 +09001866 }
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001867
1868 void updateDevicePowerStatus(int logicalAddress, int newPowerStatus) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001869 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001870 if (info == null) {
1871 Slog.w(TAG, "Can not update power status of non-existing device:" + logicalAddress);
1872 return;
1873 }
1874
1875 if (info.getDevicePowerStatus() == newPowerStatus) {
1876 return;
1877 }
1878
1879 HdmiDeviceInfo newInfo = HdmiUtils.cloneHdmiDeviceInfo(info, newPowerStatus);
1880 // addDeviceInfo replaces old device info with new one if exists.
1881 addDeviceInfo(newInfo);
1882
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001883 invokeDeviceEventListener(newInfo, HdmiControlManager.DEVICE_EVENT_UPDATE_DEVICE);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001884 }
Terry Heo959d2db2014-08-28 16:45:41 +09001885
1886 @Override
Yuncheol Heo184b1242014-09-12 15:09:07 +09001887 protected boolean handleMenuStatus(HdmiCecMessage message) {
1888 // Do nothing and just return true not to prevent from responding <Feature Abort>.
1889 return true;
1890 }
1891
1892 @Override
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001893 protected void sendStandby(int deviceId) {
1894 HdmiDeviceInfo targetDevice = mDeviceInfos.get(deviceId);
1895 if (targetDevice == null) {
1896 return;
1897 }
1898 int targetAddress = targetDevice.getLogicalAddress();
1899 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(mAddress, targetAddress));
1900 }
1901
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001902 @ServiceThreadOnly
1903 void processAllDelayedMessages() {
1904 assertRunOnServiceThread();
1905 mDelayedMessageBuffer.processAllMessages();
1906 }
1907
1908 @ServiceThreadOnly
1909 void processDelayedMessages(int address) {
1910 assertRunOnServiceThread();
1911 mDelayedMessageBuffer.processMessagesForDevice(address);
1912 }
1913
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +09001914 @ServiceThreadOnly
1915 void processDelayedActiveSource(int address) {
1916 assertRunOnServiceThread();
1917 mDelayedMessageBuffer.processActiveSource(address);
1918 }
1919
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001920 @Override
Terry Heo959d2db2014-08-28 16:45:41 +09001921 protected void dump(final IndentingPrintWriter pw) {
1922 super.dump(pw);
1923 pw.println("mArcEstablished: " + mArcEstablished);
1924 pw.println("mArcFeatureEnabled: " + mArcFeatureEnabled);
Terry Heo959d2db2014-08-28 16:45:41 +09001925 pw.println("mSystemAudioMute: " + mSystemAudioMute);
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001926 pw.println("mSystemAudioControlFeatureEnabled: " + mSystemAudioControlFeatureEnabled);
Terry Heo959d2db2014-08-28 16:45:41 +09001927 pw.println("mAutoDeviceOff: " + mAutoDeviceOff);
1928 pw.println("mAutoWakeup: " + mAutoWakeup);
1929 pw.println("mSkipRoutingControl: " + mSkipRoutingControl);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +09001930 pw.println("mPrevPortId: " + mPrevPortId);
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001931 pw.println("CEC devices:");
1932 pw.increaseIndent();
1933 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1934 pw.println(info);
1935 }
1936 pw.decreaseIndent();
Terry Heo959d2db2014-08-28 16:45:41 +09001937 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +09001938}