blob: dacf372e4f936dda8062488e381ccba7e0df4f2a [file] [log] [blame]
Jinsuk Kim2918e9e2014-05-20 16:45:45 +09001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.hdmi;
18
Jungshik Jange5a93372014-07-25 13:41:14 +090019import static android.hardware.hdmi.HdmiControlManager.CLEAR_TIMER_STATUS_CEC_DISABLE;
20import static android.hardware.hdmi.HdmiControlManager.CLEAR_TIMER_STATUS_CHECK_RECORDER_CONNECTION;
21import static android.hardware.hdmi.HdmiControlManager.CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE;
Jungshik Jang12e5dce2014-07-24 15:27:44 +090022import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_CEC_DISABLED;
23import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION;
24import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_FAIL_TO_RECORD_DISPLAYED_SCREEN;
Jungshik Jang339227d2014-08-25 15:37:20 +090025import static android.hardware.hdmi.HdmiControlManager.OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT;
Jungshik Jange5a93372014-07-25 13:41:14 +090026import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_RESULT_EXTRA_CEC_DISABLED;
27import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_RESULT_EXTRA_CHECK_RECORDER_CONNECTION;
28import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_RESULT_EXTRA_FAIL_TO_RECORD_SELECTED_SOURCE;
29import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_TYPE_ANALOGUE;
30import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_TYPE_DIGITAL;
31import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_TYPE_EXTERNAL;
Jungshik Jang12e5dce2014-07-24 15:27:44 +090032
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090033import android.hardware.hdmi.HdmiControlManager;
Jinsuk Kim50084862014-08-07 13:11:40 +090034import android.hardware.hdmi.HdmiDeviceInfo;
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +090035import android.hardware.hdmi.HdmiPortInfo;
Jungshik Jangb6591b82014-07-23 16:10:23 +090036import android.hardware.hdmi.HdmiRecordSources;
Jungshik Jang12e5dce2014-07-24 15:27:44 +090037import android.hardware.hdmi.HdmiTimerRecordSources;
Jungshik Jang60cffce2014-06-12 18:03:04 +090038import android.hardware.hdmi.IHdmiControlCallback;
Donghyun Chobc6e3722016-11-04 05:25:52 +090039import android.hardware.tv.cec.V1_0.SendMessageResult;
Jungshik Jangb69aafbf2014-07-11 16:29:06 +090040import android.media.AudioManager;
Jungshik Janga858d222014-06-23 17:17:47 +090041import android.media.AudioSystem;
Jinsuk Kim7fa3a662014-11-07 15:20:24 +090042import android.media.tv.TvInputInfo;
Jinsuk Kim7fa3a662014-11-07 15:20:24 +090043import android.media.tv.TvInputManager.TvInputCallback;
Jinsuk Kim7ecfbae2014-07-11 14:16:29 +090044import android.provider.Settings.Global;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090045import android.util.ArraySet;
Jungshik Jang092b4452014-06-11 15:19:17 +090046import android.util.Slog;
Jungshik Jang79c58a42014-06-16 16:45:36 +090047import android.util.SparseArray;
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +090048import android.util.SparseBooleanArray;
Amy123ec402018-09-25 10:56:31 -070049
Jungshik Jang79c58a42014-06-16 16:45:36 +090050import com.android.internal.annotations.GuardedBy;
Terry Heo959d2db2014-08-28 16:45:41 +090051import com.android.internal.util.IndentingPrintWriter;
Jungshik Jang60cffce2014-06-12 18:03:04 +090052import com.android.server.hdmi.DeviceDiscoveryAction.DeviceDiscoveryCallback;
Jungshik Janga5b74142014-06-23 18:03:10 +090053import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly;
Jungshik Jange5a93372014-07-25 13:41:14 +090054import com.android.server.hdmi.HdmiControlService.SendMessageCallback;
Amy123ec402018-09-25 10:56:31 -070055
Jungshik Jang8f2ed352014-07-07 15:02:47 +090056import java.io.UnsupportedEncodingException;
Jungshik Jang79c58a42014-06-16 16:45:36 +090057import java.util.ArrayList;
Jungshik Jangb6591b82014-07-23 16:10:23 +090058import java.util.Arrays;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090059import java.util.Collection;
Jinsuk Kim0a3316b2014-06-14 09:33:55 +090060import java.util.Collections;
Donghyun Chobc6e3722016-11-04 05:25:52 +090061import java.util.HashMap;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090062import java.util.Iterator;
Jinsuk Kim0a3316b2014-06-14 09:33:55 +090063import java.util.List;
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090064
65/**
66 * Represent a logical device of type TV residing in Android system.
67 */
68final class HdmiCecLocalDeviceTv extends HdmiCecLocalDevice {
Jungshik Jang092b4452014-06-11 15:19:17 +090069 private static final String TAG = "HdmiCecLocalDeviceTv";
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090070
Jungshik Jang12e5dce2014-07-24 15:27:44 +090071 // Whether ARC is available or not. "true" means that ARC is established between TV and
Jungshik Janga13da0d2014-06-30 16:26:06 +090072 // AVR as audio receiver.
73 @ServiceThreadOnly
74 private boolean mArcEstablished = false;
75
Donghyun Choc1fa9af2016-12-27 18:31:09 +090076 // Stores whether ARC feature is enabled per port.
77 // True by default for all the ARC-enabled ports.
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +090078 private final SparseBooleanArray mArcFeatureEnabled = new SparseBooleanArray();
Jungshik Jang79c58a42014-06-16 16:45:36 +090079
Donghyun Choc1fa9af2016-12-27 18:31:09 +090080 // Whether the System Audio Control feature is enabled or not. True by default.
81 @GuardedBy("mLock")
82 private boolean mSystemAudioControlFeatureEnabled;
83
Jinsuk Kim83335712014-06-24 07:57:00 +090084 // The previous port id (input) before switching to the new one. This is remembered in order to
85 // be able to switch to it upon receiving <Inactive Source> from currently active source.
86 // This remains valid only when the active source was switched via one touch play operation
87 // (either by TV or source device). Manual port switching invalidates this value to
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090088 // Constants.PORT_INVALID, for which case <Inactive Source> does not do anything.
Jinsuk Kim83335712014-06-24 07:57:00 +090089 @GuardedBy("mLock")
90 private int mPrevPortId;
91
Jungshik Jang8fa36b12014-06-25 15:51:36 +090092 @GuardedBy("mLock")
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090093 private int mSystemAudioVolume = Constants.UNKNOWN_VOLUME;
Jungshik Jang8fa36b12014-06-25 15:51:36 +090094
95 @GuardedBy("mLock")
96 private boolean mSystemAudioMute = false;
97
Jungshik Jangfa8e90d2014-06-23 14:40:32 +090098 // Copy of mDeviceInfos to guarantee thread-safety.
99 @GuardedBy("mLock")
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900100 private List<HdmiDeviceInfo> mSafeAllDeviceInfos = Collections.emptyList();
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +0900101 // All external cec input(source) devices. Does not include system audio device.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900102 @GuardedBy("mLock")
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900103 private List<HdmiDeviceInfo> mSafeExternalInputs = Collections.emptyList();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900104
Jungshik Jang79c58a42014-06-16 16:45:36 +0900105 // Map-like container of all cec devices including local ones.
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900106 // device id is used as key of container.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900107 // This is not thread-safe. For external purpose use mSafeDeviceInfos.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900108 private final SparseArray<HdmiDeviceInfo> mDeviceInfos = new SparseArray<>();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900109
Jinsuk Kim160a6e52014-07-02 06:16:36 +0900110 // If true, TV going to standby mode puts other devices also to standby.
111 private boolean mAutoDeviceOff;
112
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900113 // If true, TV wakes itself up when receiving <Text/Image View On>.
114 private boolean mAutoWakeup;
115
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900116 // List of the logical address of local CEC devices. Unmodifiable, thread-safe.
117 private List<Integer> mLocalDeviceAddresses;
118
Yuncheol Heo25c20292014-07-31 17:59:39 +0900119 private final HdmiCecStandbyModeHandler mStandbyHandler;
120
Jinsuk Kimd5307192014-08-22 09:07:40 +0900121 // If true, do not do routing control/send active source for internal source.
122 // Set to true when the device was woken up by <Text/Image View On>.
123 private boolean mSkipRoutingControl;
124
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900125 // Set of physical addresses of CEC switches on the CEC bus. Managed independently from
126 // other CEC devices since they might not have logical address.
127 private final ArraySet<Integer> mCecSwitches = new ArraySet<Integer>();
128
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900129 // Message buffer used to buffer selected messages to process later. <Active Source>
130 // from a source device, for instance, needs to be buffered if the device is not
131 // discovered yet. The buffered commands are taken out and when they are ready to
132 // handle.
133 private final DelayedMessageBuffer mDelayedMessageBuffer = new DelayedMessageBuffer(this);
134
135 // Defines the callback invoked when TV input framework is updated with input status.
136 // We are interested in the notification for HDMI input addition event, in order to
137 // process any CEC commands that arrived before the input is added.
138 private final TvInputCallback mTvInputCallback = new TvInputCallback() {
139 @Override
140 public void onInputAdded(String inputId) {
141 TvInputInfo tvInfo = mService.getTvInputManager().getTvInputInfo(inputId);
Terry Heoa22d77e2015-11-05 15:53:33 +0900142 if (tvInfo == null) return;
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900143 HdmiDeviceInfo info = tvInfo.getHdmiDeviceInfo();
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900144 if (info == null) return;
145 addTvInput(inputId, info.getId());
146 if (info.isCecDevice()) {
147 processDelayedActiveSource(info.getLogicalAddress());
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900148 }
149 }
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900150
151 @Override
152 public void onInputRemoved(String inputId) {
153 removeTvInput(inputId);
154 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900155 };
156
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900157 // Keeps the mapping (TV input ID, HDMI device ID) to keep track of the TV inputs ready to
158 // accept input switching request from HDMI devices. Requests for which the corresponding
159 // input ID is not yet registered by TV input framework need to be buffered for delayed
160 // processing.
161 private final HashMap<String, Integer> mTvInputs = new HashMap<>();
162
163 @ServiceThreadOnly
164 private void addTvInput(String inputId, int deviceId) {
165 assertRunOnServiceThread();
166 mTvInputs.put(inputId, deviceId);
167 }
168
169 @ServiceThreadOnly
170 private void removeTvInput(String inputId) {
171 assertRunOnServiceThread();
172 mTvInputs.remove(inputId);
173 }
174
175 @Override
176 @ServiceThreadOnly
177 protected boolean isInputReady(int deviceId) {
178 assertRunOnServiceThread();
179 return mTvInputs.containsValue(deviceId);
180 }
181
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900182 private SelectRequestBuffer mSelectRequestBuffer;
183
Jungshik Jang3ee65722014-06-03 16:22:30 +0900184 HdmiCecLocalDeviceTv(HdmiControlService service) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900185 super(service, HdmiDeviceInfo.DEVICE_TV);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900186 mPrevPortId = Constants.INVALID_PORT_ID;
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900187 mAutoDeviceOff = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED,
188 true);
189 mAutoWakeup = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_WAKEUP_ENABLED, true);
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900190 mSystemAudioControlFeatureEnabled =
191 mService.readBooleanSetting(Global.HDMI_SYSTEM_AUDIO_CONTROL_ENABLED, true);
Yuncheol Heo25c20292014-07-31 17:59:39 +0900192 mStandbyHandler = new HdmiCecStandbyModeHandler(service, this);
Jungshik Jang8b308d92014-05-29 21:52:28 +0900193 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900194
Jungshik Jang8b308d92014-05-29 21:52:28 +0900195 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900196 @ServiceThreadOnly
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900197 protected void onAddressAllocated(int logicalAddress, int reason) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900198 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900199 List<HdmiPortInfo> ports = mService.getPortInfo();
200 for (HdmiPortInfo port : ports) {
201 mArcFeatureEnabled.put(port.getId(), port.isArcSupported());
202 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900203 mService.registerTvInputCallback(mTvInputCallback);
Jungshik Jang3ee65722014-06-03 16:22:30 +0900204 mService.sendCecCommand(HdmiCecMessageBuilder.buildReportPhysicalAddressCommand(
205 mAddress, mService.getPhysicalAddress(), mDeviceType));
206 mService.sendCecCommand(HdmiCecMessageBuilder.buildDeviceVendorIdCommand(
207 mAddress, mService.getVendorId()));
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900208 mCecSwitches.add(mService.getPhysicalAddress()); // TV is a CEC switch too.
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900209 mTvInputs.clear();
Jinsuk Kimd5307192014-08-22 09:07:40 +0900210 mSkipRoutingControl = (reason == HdmiControlService.INITIATED_BY_WAKE_UP_MESSAGE);
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900211 launchRoutingControl(reason != HdmiControlService.INITIATED_BY_ENABLE_CEC &&
212 reason != HdmiControlService.INITIATED_BY_BOOT_UP);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900213 mLocalDeviceAddresses = initLocalDeviceAddresses();
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900214 resetSelectRequestBuffer();
Jungshik Jang60cffce2014-06-12 18:03:04 +0900215 launchDeviceDiscovery();
Jungshik Jang6f34f5a2014-07-08 21:17:29 +0900216 }
217
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900218
219 @ServiceThreadOnly
220 private List<Integer> initLocalDeviceAddresses() {
221 assertRunOnServiceThread();
222 List<Integer> addresses = new ArrayList<>();
223 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
224 addresses.add(device.getDeviceInfo().getLogicalAddress());
225 }
226 return Collections.unmodifiableList(addresses);
227 }
228
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900229
230 @ServiceThreadOnly
231 public void setSelectRequestBuffer(SelectRequestBuffer requestBuffer) {
232 assertRunOnServiceThread();
233 mSelectRequestBuffer = requestBuffer;
234 }
235
236 @ServiceThreadOnly
237 private void resetSelectRequestBuffer() {
238 assertRunOnServiceThread();
239 setSelectRequestBuffer(SelectRequestBuffer.EMPTY_BUFFER);
240 }
241
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900242 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900243 protected int getPreferredAddress() {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900244 return Constants.ADDR_TV;
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900245 }
246
247 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900248 protected void setPreferredAddress(int addr) {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900249 Slog.w(TAG, "Preferred addres will not be stored for TV");
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900250 }
251
Yuncheol Heo25c20292014-07-31 17:59:39 +0900252 @Override
253 @ServiceThreadOnly
254 boolean dispatchMessage(HdmiCecMessage message) {
255 assertRunOnServiceThread();
Donghyun Choafd26a22016-12-23 15:53:28 +0900256 if (mService.isPowerStandby() && !mService.isWakeUpMessageReceived()
257 && mStandbyHandler.handleCommand(message)) {
Yuncheol Heo25c20292014-07-31 17:59:39 +0900258 return true;
259 }
260 return super.onMessage(message);
261 }
262
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900263 /**
264 * Performs the action 'device select', or 'one touch play' initiated by TV.
265 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900266 * @param id id of HDMI device to select
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900267 * @param callback callback object to report the result with
268 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900269 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900270 void deviceSelect(int id, IHdmiControlCallback callback) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900271 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900272 HdmiDeviceInfo targetDevice = mDeviceInfos.get(id);
273 if (targetDevice == null) {
274 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
275 return;
276 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900277 int targetAddress = targetDevice.getLogicalAddress();
Jinsuk Kim58500f42014-08-05 12:48:35 +0900278 ActiveSource active = getActiveSource();
Yu.Ishihara6b2a6172015-03-25 09:44:06 +0900279 if (targetDevice.getDevicePowerStatus() == HdmiControlManager.POWER_STATUS_ON
280 && active.isValid()
281 && targetAddress == active.logicalAddress) {
Jinsuk Kim58500f42014-08-05 12:48:35 +0900282 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
283 return;
284 }
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900285 if (targetAddress == Constants.ADDR_INTERNAL) {
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900286 handleSelectInternalSource();
287 // Switching to internal source is always successful even when CEC control is disabled.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900288 setActiveSource(targetAddress, mService.getPhysicalAddress());
Jinsuk Kim7e742062014-07-30 13:19:13 +0900289 setActivePath(mService.getPhysicalAddress());
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900290 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
Jinsuk Kim83335712014-06-24 07:57:00 +0900291 return;
292 }
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900293 if (!mService.isControlEnabled()) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900294 setActiveSource(targetDevice);
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900295 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
296 return;
297 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900298 removeAction(DeviceSelectAction.class);
299 addAndStartAction(new DeviceSelectAction(this, targetDevice, callback));
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900300 }
301
Jungshik Janga5b74142014-06-23 18:03:10 +0900302 @ServiceThreadOnly
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900303 private void handleSelectInternalSource() {
Jinsuk Kima062a932014-06-18 10:00:39 +0900304 assertRunOnServiceThread();
Jinsuk Kim83335712014-06-24 07:57:00 +0900305 // Seq #18
Amy123ec402018-09-25 10:56:31 -0700306 if (mService.isControlEnabled() && getActiveSource().logicalAddress != mAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900307 updateActiveSource(mAddress, mService.getPhysicalAddress());
Jinsuk Kimd5307192014-08-22 09:07:40 +0900308 if (mSkipRoutingControl) {
309 mSkipRoutingControl = false;
310 return;
311 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900312 HdmiCecMessage activeSource = HdmiCecMessageBuilder.buildActiveSource(
313 mAddress, mService.getPhysicalAddress());
314 mService.sendCecCommand(activeSource);
315 }
316 }
317
318 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900319 void updateActiveSource(int logicalAddress, int physicalAddress) {
320 assertRunOnServiceThread();
321 updateActiveSource(ActiveSource.of(logicalAddress, physicalAddress));
322 }
323
324 @ServiceThreadOnly
325 void updateActiveSource(ActiveSource newActive) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900326 assertRunOnServiceThread();
327 // Seq #14
Amy123ec402018-09-25 10:56:31 -0700328 if (getActiveSource().equals(newActive)) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900329 return;
330 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900331 setActiveSource(newActive);
332 int logicalAddress = newActive.logicalAddress;
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900333 if (getCecDeviceInfo(logicalAddress) != null && logicalAddress != mAddress) {
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900334 if (mService.pathToPortId(newActive.physicalAddress) == getActivePortId()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900335 setPrevPortId(getActivePortId());
336 }
337 // TODO: Show the OSD banner related to the new active source device.
338 } else {
339 // TODO: If displayed, remove the OSD banner related to the previous
340 // active source device.
341 }
342 }
343
344 /**
345 * Returns the previous port id kept to handle input switching on <Inactive Source>.
346 */
347 int getPrevPortId() {
348 synchronized (mLock) {
349 return mPrevPortId;
350 }
351 }
352
353 /**
354 * Sets the previous port id. INVALID_PORT_ID invalidates it, hence no actions will be
355 * taken for <Inactive Source>.
356 */
357 void setPrevPortId(int portId) {
358 synchronized (mLock) {
359 mPrevPortId = portId;
360 }
361 }
362
363 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900364 void updateActiveInput(int path, boolean notifyInputChange) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900365 assertRunOnServiceThread();
366 // Seq #15
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900367 setActivePath(path);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900368 // TODO: Handle PAP/PIP case.
369 // Show OSD port change banner
370 if (notifyInputChange) {
371 ActiveSource activeSource = getActiveSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900372 HdmiDeviceInfo info = getCecDeviceInfo(activeSource.logicalAddress);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900373 if (info == null) {
Jinsuk Kim6ad7cbd2015-01-06 11:30:56 +0900374 info = mService.getDeviceInfoByPort(getActivePortId());
375 if (info == null) {
376 // No CEC/MHL device is present at the port. Attempt to switch to
377 // the hardware port itself for non-CEC devices that may be connected.
378 info = new HdmiDeviceInfo(path, getActivePortId());
379 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900380 }
381 mService.invokeInputChangeListener(info);
382 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900383 }
384
385 @ServiceThreadOnly
386 void doManualPortSwitching(int portId, IHdmiControlCallback callback) {
387 assertRunOnServiceThread();
388 // Seq #20
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900389 if (!mService.isValidPortId(portId)) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900390 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
Jinsuk Kima062a932014-06-18 10:00:39 +0900391 return;
392 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900393 if (portId == getActivePortId()) {
394 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
395 return;
396 }
Amy123ec402018-09-25 10:56:31 -0700397 getActiveSource().invalidate();
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900398 if (!mService.isControlEnabled()) {
399 setActivePortId(portId);
400 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
401 return;
402 }
Jinsuk Kim1f8d1c572014-08-12 15:39:53 +0900403 int oldPath = getActivePortId() != Constants.INVALID_PORT_ID
404 ? mService.portIdToPath(getActivePortId()) : getDeviceInfo().getPhysicalAddress();
Jinsuk Kimd5307192014-08-22 09:07:40 +0900405 setActivePath(oldPath);
406 if (mSkipRoutingControl) {
407 mSkipRoutingControl = false;
408 return;
409 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900410 int newPath = mService.portIdToPath(portId);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900411 startRoutingControl(oldPath, newPath, true, callback);
412 }
413
414 @ServiceThreadOnly
415 void startRoutingControl(int oldPath, int newPath, boolean queryDevicePowerStatus,
416 IHdmiControlCallback callback) {
417 assertRunOnServiceThread();
418 if (oldPath == newPath) {
419 return;
420 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900421 HdmiCecMessage routingChange =
422 HdmiCecMessageBuilder.buildRoutingChange(mAddress, oldPath, newPath);
423 mService.sendCecCommand(routingChange);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900424 removeAction(RoutingControlAction.class);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900425 addAndStartAction(
426 new RoutingControlAction(this, newPath, queryDevicePowerStatus, callback));
Jinsuk Kima062a932014-06-18 10:00:39 +0900427 }
428
Jungshik Jangf67113f2014-08-22 16:27:19 +0900429 @ServiceThreadOnly
Jinsuk Kimcae66272014-07-04 13:26:44 +0900430 int getPowerStatus() {
Jungshik Jangf67113f2014-08-22 16:27:19 +0900431 assertRunOnServiceThread();
Jinsuk Kimcae66272014-07-04 13:26:44 +0900432 return mService.getPowerStatus();
433 }
434
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900435 @Override
Donghyun Cho7609bc32016-12-02 15:31:52 +0900436 protected int findKeyReceiverAddress() {
Jinsuk Kim75434972014-09-25 13:52:50 +0900437 if (getActiveSource().isValid()) {
438 return getActiveSource().logicalAddress;
439 }
440 HdmiDeviceInfo info = getDeviceInfoByPath(getActivePath());
441 if (info != null) {
442 return info.getLogicalAddress();
443 }
444 return Constants.ADDR_INVALID;
445 }
446
Jungshik Jang092b4452014-06-11 15:19:17 +0900447 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900448 @ServiceThreadOnly
Jinsuk Kim83335712014-06-24 07:57:00 +0900449 protected boolean handleActiveSource(HdmiCecMessage message) {
450 assertRunOnServiceThread();
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900451 int logicalAddress = message.getSource();
452 int physicalAddress = HdmiUtils.twoBytesToInt(message.getParams());
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900453 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
454 if (info == null) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900455 if (!handleNewDeviceAtTheTailOfActivePath(physicalAddress)) {
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900456 HdmiLogger.debug("Device info %X not found; buffering the command", logicalAddress);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900457 mDelayedMessageBuffer.add(message);
458 }
Donghyun Cho455f09d2016-05-10 20:06:52 +0900459 } else if (isInputReady(info.getId())
460 || info.getDeviceType() == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) {
Jinsuk Kim7cc51c62015-04-30 10:39:35 +0900461 updateDevicePowerStatus(logicalAddress, HdmiControlManager.POWER_STATUS_ON);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900462 ActiveSource activeSource = ActiveSource.of(logicalAddress, physicalAddress);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900463 ActiveSourceHandler.create(this, null).process(activeSource, info.getDeviceType());
Donghyun Cho455f09d2016-05-10 20:06:52 +0900464 } else {
465 HdmiLogger.debug("Input not ready for device: %X; buffering the command", info.getId());
466 mDelayedMessageBuffer.add(message);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900467 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900468 return true;
469 }
470
471 @Override
472 @ServiceThreadOnly
473 protected boolean handleInactiveSource(HdmiCecMessage message) {
474 assertRunOnServiceThread();
475 // Seq #10
476
477 // Ignore <Inactive Source> from non-active source device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900478 if (getActiveSource().logicalAddress != message.getSource()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900479 return true;
480 }
Jinsuk Kim4d43d932014-07-03 16:43:58 +0900481 if (isProhibitMode()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900482 return true;
483 }
484 int portId = getPrevPortId();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900485 if (portId != Constants.INVALID_PORT_ID) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900486 // TODO: Do this only if TV is not showing multiview like PIP/PAP.
487
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900488 HdmiDeviceInfo inactiveSource = getCecDeviceInfo(message.getSource());
Jinsuk Kim83335712014-06-24 07:57:00 +0900489 if (inactiveSource == null) {
490 return true;
491 }
492 if (mService.pathToPortId(inactiveSource.getPhysicalAddress()) == portId) {
493 return true;
494 }
495 // TODO: Switch the TV freeze mode off
496
Jinsuk Kim83335712014-06-24 07:57:00 +0900497 doManualPortSwitching(portId, null);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900498 setPrevPortId(Constants.INVALID_PORT_ID);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900499 } else {
500 // No HDMI port to switch to was found. Notify the input change listers to
501 // switch to the lastly shown internal input.
Amy123ec402018-09-25 10:56:31 -0700502 getActiveSource().invalidate();
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900503 setActivePath(Constants.INVALID_PHYSICAL_ADDRESS);
504 mService.invokeInputChangeListener(HdmiDeviceInfo.INACTIVE_DEVICE);
Jinsuk Kim83335712014-06-24 07:57:00 +0900505 }
506 return true;
507 }
508
509 @Override
510 @ServiceThreadOnly
511 protected boolean handleRequestActiveSource(HdmiCecMessage message) {
512 assertRunOnServiceThread();
513 // Seq #19
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900514 if (mAddress == getActiveSource().logicalAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900515 mService.sendCecCommand(
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900516 HdmiCecMessageBuilder.buildActiveSource(mAddress, getActivePath()));
Jinsuk Kim83335712014-06-24 07:57:00 +0900517 }
518 return true;
519 }
520
521 @Override
522 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900523 protected boolean handleGetMenuLanguage(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900524 assertRunOnServiceThread();
Jungshik Jangf67113f2014-08-22 16:27:19 +0900525 if (!broadcastMenuLanguage(mService.getLanguage())) {
Jungshik Jang092b4452014-06-11 15:19:17 +0900526 Slog.w(TAG, "Failed to respond to <Get Menu Language>: " + message.toString());
527 }
528 return true;
529 }
530
Terry Heo1ca0a432014-08-18 10:30:32 +0900531 @ServiceThreadOnly
532 boolean broadcastMenuLanguage(String language) {
533 assertRunOnServiceThread();
534 HdmiCecMessage command = HdmiCecMessageBuilder.buildSetMenuLanguageCommand(
535 mAddress, language);
536 if (command != null) {
537 mService.sendCecCommand(command);
538 return true;
539 }
540 return false;
541 }
542
Jungshik Jang60cffce2014-06-12 18:03:04 +0900543 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900544 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900545 protected boolean handleReportPhysicalAddress(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900546 assertRunOnServiceThread();
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900547 int path = HdmiUtils.twoBytesToInt(message.getParams());
548 int address = message.getSource();
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900549 int type = message.getParams()[2];
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900550
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900551 if (updateCecSwitchInfo(address, type, path)) return true;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900552
Jungshik Jang092b4452014-06-11 15:19:17 +0900553 // Ignore if [Device Discovery Action] is going on.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900554 if (hasAction(DeviceDiscoveryAction.class)) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900555 Slog.i(TAG, "Ignored while Device Discovery Action is in progress: " + message);
Jungshik Jang092b4452014-06-11 15:19:17 +0900556 return true;
557 }
558
Jinsuk Kim4b4b9402014-09-02 10:30:10 +0900559 if (!isInDeviceList(address, path)) {
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900560 handleNewDeviceAtTheTailOfActivePath(path);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900561 }
Jinsuk Kim7cd4a582015-01-29 09:50:25 +0900562
563 // Add the device ahead with default information to handle <Active Source>
564 // promptly, rather than waiting till the new device action is finished.
565 HdmiDeviceInfo deviceInfo = new HdmiDeviceInfo(address, path, getPortId(path), type,
566 Constants.UNKNOWN_VENDOR_ID, HdmiUtils.getDefaultDeviceName(address));
567 addCecDevice(deviceInfo);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900568 startNewDeviceAction(ActiveSource.of(address, path), type);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900569 return true;
570 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900571
Jungshik Jang4480efa2014-09-04 17:08:34 +0900572 @Override
573 protected boolean handleReportPowerStatus(HdmiCecMessage command) {
574 int newStatus = command.getParams()[0] & 0xFF;
575 updateDevicePowerStatus(command.getSource(), newStatus);
576 return true;
577 }
578
579 @Override
580 protected boolean handleTimerStatus(HdmiCecMessage message) {
581 // Do nothing.
582 return true;
583 }
584
585 @Override
586 protected boolean handleRecordStatus(HdmiCecMessage message) {
587 // Do nothing.
588 return true;
589 }
590
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900591 boolean updateCecSwitchInfo(int address, int type, int path) {
592 if (address == Constants.ADDR_UNREGISTERED
593 && type == HdmiDeviceInfo.DEVICE_PURE_CEC_SWITCH) {
594 mCecSwitches.add(path);
595 updateSafeDeviceInfoList();
596 return true; // Pure switch does not need further processing. Return here.
597 }
598 if (type == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) {
599 mCecSwitches.add(path);
600 }
601 return false;
602 }
603
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900604 void startNewDeviceAction(ActiveSource activeSource, int deviceType) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900605 for (NewDeviceAction action : getActions(NewDeviceAction.class)) {
606 // If there is new device action which has the same logical address and path
607 // ignore new request.
608 // NewDeviceAction is created whenever it receives <Report Physical Address>.
609 // And there is a chance starting NewDeviceAction for the same source.
610 // Usually, new device sends <Report Physical Address> when it's plugged
611 // in. However, TV can detect a new device from HotPlugDetectionAction,
612 // which sends <Give Physical Address> to the source for newly detected
613 // device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900614 if (action.isActionOf(activeSource)) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900615 return;
616 }
617 }
618
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900619 addAndStartAction(new NewDeviceAction(this, activeSource.logicalAddress,
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900620 activeSource.physicalAddress, deviceType));
Jungshik Jang97affee2014-07-11 17:04:43 +0900621 }
622
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900623 private boolean handleNewDeviceAtTheTailOfActivePath(int path) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900624 // Seq #22
625 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900626 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim7c3a9562014-08-01 11:07:42 +0900627 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900628 startRoutingControl(getActivePath(), newPath, false, null);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900629 return true;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900630 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900631 return false;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900632 }
633
634 /**
635 * Whether the given path is located in the tail of current active path.
636 *
637 * @param path to be tested
638 * @param activePath current active path
639 * @return true if the given path is located in the tail of current active path; otherwise,
640 * false
641 */
642 static boolean isTailOfActivePath(int path, int activePath) {
643 // If active routing path is internal source, return false.
644 if (activePath == 0) {
645 return false;
646 }
647 for (int i = 12; i >= 0; i -= 4) {
648 int curActivePath = (activePath >> i) & 0xF;
649 if (curActivePath == 0) {
650 return true;
651 } else {
652 int curPath = (path >> i) & 0xF;
653 if (curPath != curActivePath) {
654 return false;
655 }
656 }
657 }
658 return false;
659 }
660
661 @Override
662 @ServiceThreadOnly
663 protected boolean handleRoutingChange(HdmiCecMessage message) {
664 assertRunOnServiceThread();
665 // Seq #21
666 byte[] params = message.getParams();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900667 int currentPath = HdmiUtils.twoBytesToInt(params);
668 if (HdmiUtils.isAffectingActiveRoutingPath(getActivePath(), currentPath)) {
Amy123ec402018-09-25 10:56:31 -0700669 getActiveSource().invalidate();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900670 removeAction(RoutingControlAction.class);
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900671 int newPath = HdmiUtils.twoBytesToInt(params, 2);
Jinsuk Kim04fd2802014-07-03 14:04:02 +0900672 addAndStartAction(new RoutingControlAction(this, newPath, true, null));
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900673 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900674 return true;
675 }
Jinsuk Kim0a3316b2014-06-14 09:33:55 +0900676
677 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900678 @ServiceThreadOnly
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900679 protected boolean handleReportAudioStatus(HdmiCecMessage message) {
680 assertRunOnServiceThread();
681
Shuichi.Noguchifbb50bc2017-12-06 11:12:33 +0900682 boolean mute = HdmiUtils.isAudioStatusMute(message);
683 int volume = HdmiUtils.getAudioStatusVolume(message);
684 setAudioStatus(mute, volume);
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900685 return true;
686 }
687
Yuncheol Heo38db6292014-07-01 14:15:14 +0900688 @Override
689 @ServiceThreadOnly
690 protected boolean handleTextViewOn(HdmiCecMessage message) {
691 assertRunOnServiceThread();
Jinsuk Kimc854c952015-10-12 09:33:33 +0900692
693 // Note that <Text View On> (and <Image View On>) command won't be handled here in
694 // most cases. A dedicated microcontroller should be in charge while Android system
695 // is in sleep mode, and the command need not be passed up to this service.
696 // The only situation where the command reaches this handler is that sleep mode is
697 // implemented in such a way that Android system is not really put to standby mode
698 // but only the display is set to blank. Then the command leads to the effect of
699 // turning on the display by the invocation of PowerManager.wakeUp().
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900700 if (mService.isPowerStandbyOrTransient() && mAutoWakeup) {
Yuncheol Heo38db6292014-07-01 14:15:14 +0900701 mService.wakeUp();
702 }
Yuncheol Heo38db6292014-07-01 14:15:14 +0900703 return true;
704 }
705
706 @Override
707 @ServiceThreadOnly
708 protected boolean handleImageViewOn(HdmiCecMessage message) {
709 assertRunOnServiceThread();
710 // Currently, it's the same as <Text View On>.
711 return handleTextViewOn(message);
712 }
713
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900714 @Override
715 @ServiceThreadOnly
716 protected boolean handleSetOsdName(HdmiCecMessage message) {
717 int source = message.getSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900718 HdmiDeviceInfo deviceInfo = getCecDeviceInfo(source);
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900719 // If the device is not in device list, ignore it.
720 if (deviceInfo == null) {
721 Slog.e(TAG, "No source device info for <Set Osd Name>." + message);
722 return true;
723 }
724 String osdName = null;
725 try {
726 osdName = new String(message.getParams(), "US-ASCII");
727 } catch (UnsupportedEncodingException e) {
728 Slog.e(TAG, "Invalid <Set Osd Name> request:" + message, e);
729 return true;
730 }
731
732 if (deviceInfo.getDisplayName().equals(osdName)) {
733 Slog.i(TAG, "Ignore incoming <Set Osd Name> having same osd name:" + message);
734 return true;
735 }
736
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900737 addCecDevice(new HdmiDeviceInfo(deviceInfo.getLogicalAddress(),
Jinsuk Kim2b152012014-07-25 08:22:26 +0900738 deviceInfo.getPhysicalAddress(), deviceInfo.getPortId(),
739 deviceInfo.getDeviceType(), deviceInfo.getVendorId(), osdName));
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900740 return true;
741 }
742
Jungshik Janga5b74142014-06-23 18:03:10 +0900743 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900744 private void launchDeviceDiscovery() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900745 assertRunOnServiceThread();
746 clearDeviceInfoList();
747 DeviceDiscoveryAction action = new DeviceDiscoveryAction(this,
Jungshik Jang60cffce2014-06-12 18:03:04 +0900748 new DeviceDiscoveryCallback() {
749 @Override
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900750 public void onDeviceDiscoveryDone(List<HdmiDeviceInfo> deviceInfos) {
751 for (HdmiDeviceInfo info : deviceInfos) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900752 addCecDevice(info);
Jungshik Jang60cffce2014-06-12 18:03:04 +0900753 }
754
755 // Since we removed all devices when it's start and
756 // device discovery action does not poll local devices,
757 // we should put device info of local device manually here
758 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900759 addCecDevice(device.getDeviceInfo());
Jungshik Jang60cffce2014-06-12 18:03:04 +0900760 }
761
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900762 mSelectRequestBuffer.process();
763 resetSelectRequestBuffer();
764
Jungshik Jang79c58a42014-06-16 16:45:36 +0900765 addAndStartAction(new HotplugDetectionAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang410ca9c2014-08-07 18:04:14 +0900766 addAndStartAction(new PowerStatusMonitorAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang187d0172014-06-17 17:48:42 +0900767
Jungshik Jang339227d2014-08-25 15:37:20 +0900768 HdmiDeviceInfo avr = getAvrDeviceInfo();
769 if (avr != null) {
770 onNewAvrAdded(avr);
Wally Yau951e3e42015-04-03 15:12:52 -0700771 } else {
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900772 setSystemAudioMode(false);
Jungshik Jang187d0172014-06-17 17:48:42 +0900773 }
Jungshik Jang60cffce2014-06-12 18:03:04 +0900774 }
775 });
Jungshik Jang79c58a42014-06-16 16:45:36 +0900776 addAndStartAction(action);
777 }
778
Jungshik Jang339227d2014-08-25 15:37:20 +0900779 @ServiceThreadOnly
780 void onNewAvrAdded(HdmiDeviceInfo avr) {
781 assertRunOnServiceThread();
Donghyun Choc21f63a2016-05-10 10:37:47 +0900782 addAndStartAction(new SystemAudioAutoInitiationAction(this, avr.getLogicalAddress()));
Donghyun Cho88909ce2016-12-23 14:41:00 +0900783 if (isConnected(avr.getPortId()) && isArcFeatureEnabled(avr.getPortId())
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900784 && !hasAction(SetArcTransmissionStateAction.class)) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900785 startArcAction(true);
786 }
787 }
788
Jungshik Jang79c58a42014-06-16 16:45:36 +0900789 // Clear all device info.
Jungshik Janga5b74142014-06-23 18:03:10 +0900790 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900791 private void clearDeviceInfoList() {
792 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900793 for (HdmiDeviceInfo info : mSafeExternalInputs) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +0900794 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +0900795 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900796 mDeviceInfos.clear();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900797 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900798 }
799
Jungshik Janga5b74142014-06-23 18:03:10 +0900800 @ServiceThreadOnly
Yuncheol Heoc516d652014-07-11 18:23:24 +0900801 // Seq #32
Jungshik Jangea67c182014-06-19 22:19:20 +0900802 void changeSystemAudioMode(boolean enabled, IHdmiControlCallback callback) {
803 assertRunOnServiceThread();
Yuncheol Heoc516d652014-07-11 18:23:24 +0900804 if (!mService.isControlEnabled() || hasAction(DeviceDiscoveryAction.class)) {
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900805 setSystemAudioMode(false);
Yuncheol Heoc516d652014-07-11 18:23:24 +0900806 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
807 return;
808 }
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900809 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jangea67c182014-06-19 22:19:20 +0900810 if (avr == null) {
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900811 setSystemAudioMode(false);
Yuncheol Heod05f67f2014-07-11 16:06:40 +0900812 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
Jungshik Jangea67c182014-06-19 22:19:20 +0900813 return;
814 }
815
816 addAndStartAction(
817 new SystemAudioActionFromTv(this, avr.getLogicalAddress(), enabled, callback));
Jungshik Jangea67c182014-06-19 22:19:20 +0900818 }
819
Jungshik Jangca5be9a2014-07-01 18:01:26 +0900820 // # Seq 25
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900821 void setSystemAudioMode(boolean on) {
822 if (!isSystemAudioControlFeatureEnabled() && on) {
823 HdmiLogger.debug("Cannot turn on system audio mode "
824 + "because the System Audio Control feature is disabled.");
825 return;
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900826 }
Amy489454f2019-01-24 19:06:57 -0800827 HdmiLogger.debug("System Audio Mode change[old:%b new:%b]",
828 mService.isSystemAudioActivated(), on);
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900829 updateAudioManagerForSystemAudio(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900830 synchronized (mLock) {
Amy489454f2019-01-24 19:06:57 -0800831 if (mService.isSystemAudioActivated() != on) {
832 mService.setSystemAudioActivated(on);
Jungshik Jangea67c182014-06-19 22:19:20 +0900833 mService.announceSystemAudioModeChange(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900834 }
Hannah.Hsu45254172019-05-15 15:05:23 +0800835 if (on && !mArcEstablished) {
836 startArcAction(true);
837 } else if (!on) {
838 startArcAction(false);
839 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900840 }
841 }
842
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900843 private void updateAudioManagerForSystemAudio(boolean on) {
Jungshik Jang6b096d32014-09-12 18:49:08 +0900844 int device = mService.getAudioManager().setHdmiSystemAudioSupported(on);
845 HdmiLogger.debug("[A]UpdateSystemAudio mode[on=%b] output=[%X]", on, device);
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900846 }
847
848 boolean isSystemAudioActivated() {
Jinsuk Kim86a1e5a2014-11-19 06:04:50 +0900849 if (!hasSystemAudioDevice()) {
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900850 return false;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900851 }
Amy489454f2019-01-24 19:06:57 -0800852 return mService.isSystemAudioActivated();
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900853 }
854
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900855 @ServiceThreadOnly
856 void setSystemAudioControlFeatureEnabled(boolean enabled) {
857 assertRunOnServiceThread();
858 synchronized (mLock) {
859 mSystemAudioControlFeatureEnabled = enabled;
860 }
861 if (hasSystemAudioDevice()) {
862 changeSystemAudioMode(enabled, null);
863 }
864 }
865
866 boolean isSystemAudioControlFeatureEnabled() {
867 synchronized (mLock) {
868 return mSystemAudioControlFeatureEnabled;
869 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900870 }
871
872 /**
873 * Change ARC status into the given {@code enabled} status.
874 *
875 * @return {@code true} if ARC was in "Enabled" status
876 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900877 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900878 boolean setArcStatus(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900879 assertRunOnServiceThread();
Jungshik Jang6b096d32014-09-12 18:49:08 +0900880
881 HdmiLogger.debug("Set Arc Status[old:%b new:%b]", mArcEstablished, enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900882 boolean oldStatus = mArcEstablished;
883 // 1. Enable/disable ARC circuit.
Donghyun Chobc6e3722016-11-04 05:25:52 +0900884 enableAudioReturnChannel(enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900885 // 2. Notify arc status to audio service.
886 notifyArcStatusToAudioService(enabled);
887 // 3. Update arc status;
888 mArcEstablished = enabled;
889 return oldStatus;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900890 }
891
Jinsuk Kim757c0972015-02-23 10:15:42 +0900892 /**
893 * Switch hardware ARC circuit in the system.
894 */
895 @ServiceThreadOnly
Donghyun Chobc6e3722016-11-04 05:25:52 +0900896 void enableAudioReturnChannel(boolean enabled) {
Jinsuk Kim757c0972015-02-23 10:15:42 +0900897 assertRunOnServiceThread();
898 HdmiDeviceInfo avr = getAvrDeviceInfo();
899 if (avr != null) {
Donghyun Chobc6e3722016-11-04 05:25:52 +0900900 mService.enableAudioReturnChannel(avr.getPortId(), enabled);
Jinsuk Kim757c0972015-02-23 10:15:42 +0900901 }
902 }
903
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +0900904 @ServiceThreadOnly
Jinsuk Kim7b0cf642015-04-14 09:43:45 +0900905 boolean isConnected(int portId) {
906 assertRunOnServiceThread();
907 return mService.isConnected(portId);
908 }
909
Jungshik Janga858d222014-06-23 17:17:47 +0900910 private void notifyArcStatusToAudioService(boolean enabled) {
911 // Note that we don't set any name to ARC.
912 mService.getAudioManager().setWiredDeviceConnectionState(
913 AudioSystem.DEVICE_OUT_HDMI_ARC,
Paul McLean10804eb2015-01-28 11:16:35 -0800914 enabled ? 1 : 0, "", "");
Jungshik Janga858d222014-06-23 17:17:47 +0900915 }
916
Jungshik Jang79c58a42014-06-16 16:45:36 +0900917 /**
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900918 * Returns true if ARC is currently established on a certain port.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900919 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900920 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900921 boolean isArcEstablished() {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900922 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900923 if (mArcEstablished) {
924 for (int i = 0; i < mArcFeatureEnabled.size(); i++) {
925 if (mArcFeatureEnabled.valueAt(i)) return true;
926 }
927 }
928 return false;
Jungshik Janga13da0d2014-06-30 16:26:06 +0900929 }
930
931 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900932 void changeArcFeatureEnabled(int portId, boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900933 assertRunOnServiceThread();
Donghyun Cho88909ce2016-12-23 14:41:00 +0900934 if (mArcFeatureEnabled.get(portId) == enabled) {
935 return;
936 }
937 mArcFeatureEnabled.put(portId, enabled);
938 HdmiDeviceInfo avr = getAvrDeviceInfo();
939 if (avr == null || avr.getPortId() != portId) {
940 return;
941 }
942 if (enabled && !mArcEstablished) {
943 startArcAction(true);
944 } else if (!enabled && mArcEstablished) {
945 startArcAction(false);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900946 }
947 }
948
949 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900950 boolean isArcFeatureEnabled(int portId) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900951 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900952 return mArcFeatureEnabled.get(portId);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900953 }
954
955 @ServiceThreadOnly
Jungshik Jang339227d2014-08-25 15:37:20 +0900956 void startArcAction(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900957 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900958 HdmiDeviceInfo info = getAvrDeviceInfo();
Jungshik Janga13da0d2014-06-30 16:26:06 +0900959 if (info == null) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900960 Slog.w(TAG, "Failed to start arc action; No AVR device.");
Jungshik Janga13da0d2014-06-30 16:26:06 +0900961 return;
962 }
Jungshik Jang339227d2014-08-25 15:37:20 +0900963 if (!canStartArcUpdateAction(info.getLogicalAddress(), enabled)) {
964 Slog.w(TAG, "Failed to start arc action; ARC configuration check failed.");
965 if (enabled && !isConnectedToArcPort(info.getPhysicalAddress())) {
966 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
967 }
Jungshik Janga13da0d2014-06-30 16:26:06 +0900968 return;
969 }
970
971 // Terminate opposite action and start action if not exist.
972 if (enabled) {
973 removeAction(RequestArcTerminationAction.class);
974 if (!hasAction(RequestArcInitiationAction.class)) {
975 addAndStartAction(new RequestArcInitiationAction(this, info.getLogicalAddress()));
976 }
977 } else {
978 removeAction(RequestArcInitiationAction.class);
979 if (!hasAction(RequestArcTerminationAction.class)) {
980 addAndStartAction(new RequestArcTerminationAction(this, info.getLogicalAddress()));
981 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900982 }
983 }
984
Jungshik Jang339227d2014-08-25 15:37:20 +0900985 private boolean isDirectConnectAddress(int physicalAddress) {
986 return (physicalAddress & Constants.ROUTING_PATH_TOP_MASK) == physicalAddress;
987 }
988
Jungshik Jang79c58a42014-06-16 16:45:36 +0900989 void setAudioStatus(boolean mute, int volume) {
Shuichi.Noguchi67ed2e92017-11-27 18:57:56 +0900990 if (!isSystemAudioActivated()) {
991 return;
992 }
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900993 synchronized (mLock) {
994 mSystemAudioMute = mute;
995 mSystemAudioVolume = volume;
Jungshik Jangb69aafbf2014-07-11 16:29:06 +0900996 int maxVolume = mService.getAudioManager().getStreamMaxVolume(
997 AudioManager.STREAM_MUSIC);
998 mService.setAudioStatus(mute,
999 VolumeControlAction.scaleToCustomVolume(volume, maxVolume));
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001000 displayOsd(HdmiControlManager.OSD_MESSAGE_AVR_VOLUME_CHANGED,
1001 mute ? HdmiControlManager.AVR_VOLUME_MUTED : volume);
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001002 }
1003 }
1004
1005 @ServiceThreadOnly
1006 void changeVolume(int curVolume, int delta, int maxVolume) {
1007 assertRunOnServiceThread();
Shuichi.Noguchiad91e7e2017-11-20 18:58:29 +09001008 if (getAvrDeviceInfo() == null) {
1009 // On initialization process, getAvrDeviceInfo() may return null and cause exception
1010 return;
1011 }
Jungshik Jang377dcbd2014-07-15 15:49:02 +09001012 if (delta == 0 || !isSystemAudioActivated()) {
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001013 return;
1014 }
1015
1016 int targetVolume = curVolume + delta;
1017 int cecVolume = VolumeControlAction.scaleToCecVolume(targetVolume, maxVolume);
1018 synchronized (mLock) {
1019 // If new volume is the same as current system audio volume, just ignore it.
1020 // Note that UNKNOWN_VOLUME is not in range of cec volume scale.
1021 if (cecVolume == mSystemAudioVolume) {
1022 // Update tv volume with system volume value.
1023 mService.setAudioStatus(false,
1024 VolumeControlAction.scaleToCustomVolume(mSystemAudioVolume, maxVolume));
1025 return;
1026 }
1027 }
1028
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001029 List<VolumeControlAction> actions = getActions(VolumeControlAction.class);
1030 if (actions.isEmpty()) {
1031 addAndStartAction(new VolumeControlAction(this,
1032 getAvrDeviceInfo().getLogicalAddress(), delta > 0));
1033 } else {
1034 actions.get(0).handleVolumeChange(delta > 0);
1035 }
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001036 }
1037
1038 @ServiceThreadOnly
1039 void changeMute(boolean mute) {
1040 assertRunOnServiceThread();
Shuichi.Noguchiad91e7e2017-11-20 18:58:29 +09001041 if (getAvrDeviceInfo() == null) {
1042 // On initialization process, getAvrDeviceInfo() may return null and cause exception
1043 return;
1044 }
Jungshik Jang6b096d32014-09-12 18:49:08 +09001045 HdmiLogger.debug("[A]:Change mute:%b", mute);
Jungshik Jang720407a2014-09-15 17:48:41 +09001046 synchronized (mLock) {
1047 if (mSystemAudioMute == mute) {
1048 HdmiLogger.debug("No need to change mute.");
1049 return;
1050 }
1051 }
Jungshik Jang377dcbd2014-07-15 15:49:02 +09001052 if (!isSystemAudioActivated()) {
Jungshik Jang6b096d32014-09-12 18:49:08 +09001053 HdmiLogger.debug("[A]:System audio is not activated.");
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001054 return;
1055 }
1056
1057 // Remove existing volume action.
1058 removeAction(VolumeControlAction.class);
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001059 sendUserControlPressedAndReleased(getAvrDeviceInfo().getLogicalAddress(),
Jinsuk Kim0ab37792015-10-17 06:53:52 +09001060 HdmiCecKeycode.getMuteKey(mute));
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001061 }
1062
Jungshik Jang79c58a42014-06-16 16:45:36 +09001063 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001064 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001065 protected boolean handleInitiateArc(HdmiCecMessage message) {
1066 assertRunOnServiceThread();
Jungshik Jang339227d2014-08-25 15:37:20 +09001067
1068 if (!canStartArcUpdateAction(message.getSource(), true)) {
Kyeongkab.Namab27ffb2018-10-19 16:35:14 +09001069 HdmiDeviceInfo avrDeviceInfo = getAvrDeviceInfo();
1070 if (avrDeviceInfo == null) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001071 // AVR may not have been discovered yet. Delay the message processing.
1072 mDelayedMessageBuffer.add(message);
1073 return true;
1074 }
Jungshik Jang339227d2014-08-25 15:37:20 +09001075 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
Kyeongkab.Namab27ffb2018-10-19 16:35:14 +09001076 if (!isConnectedToArcPort(avrDeviceInfo.getPhysicalAddress())) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001077 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
1078 }
1079 return true;
1080 }
1081
Jungshik Jang79c58a42014-06-16 16:45:36 +09001082 // In case where <Initiate Arc> is started by <Request ARC Initiation>
1083 // need to clean up RequestArcInitiationAction.
1084 removeAction(RequestArcInitiationAction.class);
1085 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1086 message.getSource(), true);
1087 addAndStartAction(action);
1088 return true;
1089 }
1090
Donghyun Cho88909ce2016-12-23 14:41:00 +09001091 private boolean canStartArcUpdateAction(int avrAddress, boolean enabled) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001092 HdmiDeviceInfo avr = getAvrDeviceInfo();
1093 if (avr != null
1094 && (avrAddress == avr.getLogicalAddress())
1095 && isConnectedToArcPort(avr.getPhysicalAddress())
1096 && isDirectConnectAddress(avr.getPhysicalAddress())) {
Donghyun Cho88909ce2016-12-23 14:41:00 +09001097 if (enabled) {
1098 return isConnected(avr.getPortId()) && isArcFeatureEnabled(avr.getPortId());
Jungshik Jang339227d2014-08-25 15:37:20 +09001099 } else {
1100 return true;
1101 }
1102 } else {
1103 return false;
1104 }
1105 }
1106
Jungshik Jang79c58a42014-06-16 16:45:36 +09001107 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001108 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001109 protected boolean handleTerminateArc(HdmiCecMessage message) {
1110 assertRunOnServiceThread();
Jinsuk Kim7e4b4802015-04-20 13:17:13 +09001111 if (mService .isPowerStandbyOrTransient()) {
1112 setArcStatus(false);
1113 return true;
1114 }
1115 // Do not check ARC configuration since the AVR might have been already removed.
1116 // Clean up RequestArcTerminationAction in case <Terminate Arc> was started by
1117 // <Request ARC Termination>.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001118 removeAction(RequestArcTerminationAction.class);
1119 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1120 message.getSource(), false);
1121 addAndStartAction(action);
1122 return true;
1123 }
1124
1125 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001126 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001127 protected boolean handleSetSystemAudioMode(HdmiCecMessage message) {
1128 assertRunOnServiceThread();
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001129 boolean systemAudioStatus = HdmiUtils.parseCommandParamSystemAudioStatus(message);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001130 if (!isMessageForSystemAudio(message)) {
Jinsuk Kimad1e3d72015-01-02 13:27:15 +09001131 if (getAvrDeviceInfo() == null) {
1132 // AVR may not have been discovered yet. Delay the message processing.
1133 mDelayedMessageBuffer.add(message);
Donghyun Choc21f63a2016-05-10 10:37:47 +09001134 } else {
1135 HdmiLogger.warning("Invalid <Set System Audio Mode> message:" + message);
1136 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
Jinsuk Kimad1e3d72015-01-02 13:27:15 +09001137 }
Jungshik Jang4480efa2014-09-04 17:08:34 +09001138 return true;
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001139 } else if (systemAudioStatus && !isSystemAudioControlFeatureEnabled()) {
1140 HdmiLogger.debug("Ignoring <Set System Audio Mode> message "
1141 + "because the System Audio Control feature is disabled: %s", message);
1142 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
1143 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001144 }
Donghyun Choc21f63a2016-05-10 10:37:47 +09001145 removeAction(SystemAudioAutoInitiationAction.class);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001146 SystemAudioActionFromAvr action = new SystemAudioActionFromAvr(this,
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001147 message.getSource(), systemAudioStatus, null);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001148 addAndStartAction(action);
1149 return true;
1150 }
1151
1152 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001153 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001154 protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) {
1155 assertRunOnServiceThread();
1156 if (!isMessageForSystemAudio(message)) {
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001157 HdmiLogger.warning("Invalid <System Audio Mode Status> message:" + message);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001158 // Ignore this message.
1159 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001160 }
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001161 setSystemAudioMode(HdmiUtils.parseCommandParamSystemAudioStatus(message));
Jungshik Jang79c58a42014-06-16 16:45:36 +09001162 return true;
1163 }
1164
Jungshik Jangb6591b82014-07-23 16:10:23 +09001165 // Seq #53
1166 @Override
1167 @ServiceThreadOnly
1168 protected boolean handleRecordTvScreen(HdmiCecMessage message) {
1169 List<OneTouchRecordAction> actions = getActions(OneTouchRecordAction.class);
1170 if (!actions.isEmpty()) {
1171 // Assumes only one OneTouchRecordAction.
1172 OneTouchRecordAction action = actions.get(0);
1173 if (action.getRecorderAddress() != message.getSource()) {
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001174 announceOneTouchRecordResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001175 message.getSource(),
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001176 HdmiControlManager.ONE_TOUCH_RECORD_PREVIOUS_RECORDING_IN_PROGRESS);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001177 }
1178 return super.handleRecordTvScreen(message);
1179 }
1180
1181 int recorderAddress = message.getSource();
1182 byte[] recordSource = mService.invokeRecordRequestListener(recorderAddress);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001183 int reason = startOneTouchRecord(recorderAddress, recordSource);
1184 if (reason != Constants.ABORT_NO_ERROR) {
1185 mService.maySendFeatureAbortCommand(message, reason);
1186 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001187 return true;
1188 }
1189
Jungshik Jange5a93372014-07-25 13:41:14 +09001190 @Override
1191 protected boolean handleTimerClearedStatus(HdmiCecMessage message) {
1192 byte[] params = message.getParams();
Jungshik Jange9e0f072014-08-05 17:48:59 +09001193 int timerClearedStatusData = params[0] & 0xFF;
Jungshik Jang326aef02014-11-05 12:50:35 +09001194 announceTimerRecordingResult(message.getSource(), timerClearedStatusData);
Jungshik Jange5a93372014-07-25 13:41:14 +09001195 return true;
1196 }
1197
Jungshik Jang326aef02014-11-05 12:50:35 +09001198 void announceOneTouchRecordResult(int recorderAddress, int result) {
1199 mService.invokeOneTouchRecordResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001200 }
1201
Jungshik Jang326aef02014-11-05 12:50:35 +09001202 void announceTimerRecordingResult(int recorderAddress, int result) {
1203 mService.invokeTimerRecordingResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001204 }
1205
Jungshik Jang326aef02014-11-05 12:50:35 +09001206 void announceClearTimerRecordingResult(int recorderAddress, int result) {
1207 mService.invokeClearTimerRecordingResult(recorderAddress, result);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001208 }
1209
Jungshik Jang79c58a42014-06-16 16:45:36 +09001210 private boolean isMessageForSystemAudio(HdmiCecMessage message) {
Jungshik Jang4480efa2014-09-04 17:08:34 +09001211 return mService.isControlEnabled()
1212 && message.getSource() == Constants.ADDR_AUDIO_SYSTEM
Jungshik Jang473119f2014-08-27 16:43:22 +09001213 && (message.getDestination() == Constants.ADDR_TV
1214 || message.getDestination() == Constants.ADDR_BROADCAST)
1215 && getAvrDeviceInfo() != null;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001216 }
1217
1218 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001219 * Add a new {@link HdmiDeviceInfo}. It returns old device info which has the same
Jungshik Jang79c58a42014-06-16 16:45:36 +09001220 * logical address as new device info's.
1221 *
1222 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1223 *
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001224 * @param deviceInfo a new {@link HdmiDeviceInfo} to be added.
1225 * @return {@code null} if it is new device. Otherwise, returns old {@HdmiDeviceInfo}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001226 * that has the same logical address as new one has.
1227 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001228 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001229 private HdmiDeviceInfo addDeviceInfo(HdmiDeviceInfo deviceInfo) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001230 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001231 HdmiDeviceInfo oldDeviceInfo = getCecDeviceInfo(deviceInfo.getLogicalAddress());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001232 if (oldDeviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001233 removeDeviceInfo(deviceInfo.getId());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001234 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001235 mDeviceInfos.append(deviceInfo.getId(), deviceInfo);
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001236 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001237 return oldDeviceInfo;
1238 }
1239
1240 /**
1241 * Remove a device info corresponding to the given {@code logicalAddress}.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001242 * It returns removed {@link HdmiDeviceInfo} if exists.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001243 *
1244 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1245 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001246 * @param id id of device to be removed
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001247 * @return removed {@link HdmiDeviceInfo} it exists. Otherwise, returns {@code null}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001248 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001249 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001250 private HdmiDeviceInfo removeDeviceInfo(int id) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001251 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001252 HdmiDeviceInfo deviceInfo = mDeviceInfos.get(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001253 if (deviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001254 mDeviceInfos.remove(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001255 }
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001256 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001257 return deviceInfo;
1258 }
1259
1260 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001261 * Return a list of all {@link HdmiDeviceInfo}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001262 *
1263 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
Jungshik Jang339227d2014-08-25 15:37:20 +09001264 * This is not thread-safe. For thread safety, call {@link #getSafeExternalInputsLocked} which
Jungshik Jang8e93c842014-08-06 15:48:33 +09001265 * does not include local device.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001266 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001267 @ServiceThreadOnly
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001268 List<HdmiDeviceInfo> getDeviceInfoList(boolean includeLocalDevice) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001269 assertRunOnServiceThread();
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001270 if (includeLocalDevice) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001271 return HdmiUtils.sparseArrayToList(mDeviceInfos);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001272 } else {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001273 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001274 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001275 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001276 if (!isLocalDeviceAddress(info.getLogicalAddress())) {
1277 infoList.add(info);
1278 }
1279 }
1280 return infoList;
1281 }
1282 }
1283
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001284 /**
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001285 * Return external input devices.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001286 */
Andreas Gampea36dc622018-02-05 17:19:22 -08001287 @GuardedBy("mLock")
Jinsuk Kimed086452014-08-18 15:01:53 +09001288 List<HdmiDeviceInfo> getSafeExternalInputsLocked() {
1289 return mSafeExternalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001290 }
1291
Jungshik Janga5b74142014-06-23 18:03:10 +09001292 @ServiceThreadOnly
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001293 private void updateSafeDeviceInfoList() {
1294 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001295 List<HdmiDeviceInfo> copiedDevices = HdmiUtils.sparseArrayToList(mDeviceInfos);
1296 List<HdmiDeviceInfo> externalInputs = getInputDevices();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001297 synchronized (mLock) {
1298 mSafeAllDeviceInfos = copiedDevices;
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001299 mSafeExternalInputs = externalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001300 }
1301 }
1302
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001303 /**
1304 * Return a list of external cec input (source) devices.
1305 *
1306 * <p>Note that this effectively excludes non-source devices like system audio,
1307 * secondary TV.
1308 */
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001309 private List<HdmiDeviceInfo> getInputDevices() {
1310 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001311 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001312 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001313 if (isLocalDeviceAddress(info.getLogicalAddress())) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001314 continue;
1315 }
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001316 if (info.isSourceType() && !hideDevicesBehindLegacySwitch(info)) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001317 infoList.add(info);
1318 }
1319 }
1320 return infoList;
1321 }
1322
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001323 // Check if we are hiding CEC devices connected to a legacy (non-CEC) switch.
1324 // Returns true if the policy is set to true, and the device to check does not have
1325 // a parent CEC device (which should be the CEC-enabled switch) in the list.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001326 private boolean hideDevicesBehindLegacySwitch(HdmiDeviceInfo info) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001327 return HdmiConfig.HIDE_DEVICES_BEHIND_LEGACY_SWITCH
1328 && !isConnectedToCecSwitch(info.getPhysicalAddress(), mCecSwitches);
1329 }
1330
1331 private static boolean isConnectedToCecSwitch(int path, Collection<Integer> switches) {
1332 for (int switchPath : switches) {
1333 if (isParentPath(switchPath, path)) {
1334 return true;
1335 }
1336 }
1337 return false;
1338 }
1339
1340 private static boolean isParentPath(int parentPath, int childPath) {
1341 // (A000, AB00) (AB00, ABC0), (ABC0, ABCD)
1342 // If child's last non-zero nibble is removed, the result equals to the parent.
1343 for (int i = 0; i <= 12; i += 4) {
1344 int nibble = (childPath >> i) & 0xF;
1345 if (nibble != 0) {
1346 int parentNibble = (parentPath >> i) & 0xF;
1347 return parentNibble == 0 && (childPath >> i+4) == (parentPath >> i+4);
1348 }
1349 }
1350 return false;
1351 }
1352
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001353 private void invokeDeviceEventListener(HdmiDeviceInfo info, int status) {
Jinsuk Kim98d760e2014-12-23 07:01:51 +09001354 if (!hideDevicesBehindLegacySwitch(info)) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001355 mService.invokeDeviceEventListeners(info, status);
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001356 }
1357 }
1358
Jungshik Jang79c58a42014-06-16 16:45:36 +09001359 private boolean isLocalDeviceAddress(int address) {
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001360 return mLocalDeviceAddresses.contains(address);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001361 }
1362
Jungshik Jange9cf1582014-06-23 17:28:58 +09001363 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001364 HdmiDeviceInfo getAvrDeviceInfo() {
Jungshik Jange9cf1582014-06-23 17:28:58 +09001365 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001366 return getCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jange9cf1582014-06-23 17:28:58 +09001367 }
1368
Jungshik Jang79c58a42014-06-16 16:45:36 +09001369 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001370 * Return a {@link HdmiDeviceInfo} corresponding to the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001371 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001372 * This is not thread-safe. For thread safety, call {@link #getSafeCecDeviceInfo(int)}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001373 *
Jungshik Jang339227d2014-08-25 15:37:20 +09001374 * @param logicalAddress logical address of the device to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001375 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001376 * Returns null if no logical address matched
1377 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001378 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001379 HdmiDeviceInfo getCecDeviceInfo(int logicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001380 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001381 return mDeviceInfos.get(HdmiDeviceInfo.idForCecDevice(logicalAddress));
Jungshik Jang79c58a42014-06-16 16:45:36 +09001382 }
1383
Jungshik Jange9cf1582014-06-23 17:28:58 +09001384 boolean hasSystemAudioDevice() {
1385 return getSafeAvrDeviceInfo() != null;
1386 }
1387
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001388 HdmiDeviceInfo getSafeAvrDeviceInfo() {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001389 return getSafeCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001390 }
1391
1392 /**
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001393 * Thread safe version of {@link #getCecDeviceInfo(int)}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001394 *
1395 * @param logicalAddress logical address to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001396 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001397 * Returns null if no logical address matched
1398 */
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001399 HdmiDeviceInfo getSafeCecDeviceInfo(int logicalAddress) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001400 synchronized (mLock) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001401 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1402 if (info.isCecDevice() && info.getLogicalAddress() == logicalAddress) {
1403 return info;
1404 }
1405 }
1406 return null;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001407 }
1408 }
1409
Andreas Gampea36dc622018-02-05 17:19:22 -08001410 @GuardedBy("mLock")
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001411 List<HdmiDeviceInfo> getSafeCecDevicesLocked() {
1412 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
1413 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1414 if (isLocalDeviceAddress(info.getLogicalAddress())) {
1415 continue;
1416 }
1417 infoList.add(info);
1418 }
1419 return infoList;
1420 }
1421
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001422 /**
Jungshik Jang8f2ed352014-07-07 15:02:47 +09001423 * Called when a device is newly added or a new device is detected or
1424 * existing device is updated.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001425 *
1426 * @param info device info of a new device.
1427 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001428 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001429 final void addCecDevice(HdmiDeviceInfo info) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001430 assertRunOnServiceThread();
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001431 HdmiDeviceInfo old = addDeviceInfo(info);
Jinsuk Kim13c030e2014-06-20 13:25:17 +09001432 if (info.getLogicalAddress() == mAddress) {
1433 // The addition of TV device itself should not be notified.
1434 return;
1435 }
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001436 if (old == null) {
1437 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
1438 } else if (!old.equals(info)) {
Jinsuk Kim4fcbf0b2014-12-09 06:48:13 +09001439 invokeDeviceEventListener(old, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
1440 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001441 }
Jungshik Jang79c58a42014-06-16 16:45:36 +09001442 }
1443
1444 /**
1445 * Called when a device is removed or removal of device is detected.
1446 *
1447 * @param address a logical address of a device to be removed
1448 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001449 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001450 final void removeCecDevice(int address) {
1451 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001452 HdmiDeviceInfo info = removeDeviceInfo(HdmiDeviceInfo.idForCecDevice(address));
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001453
Jungshik Jang79c58a42014-06-16 16:45:36 +09001454 mCecMessageCache.flushMessagesFrom(address);
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001455 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001456 }
1457
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001458 @ServiceThreadOnly
1459 void handleRemoveActiveRoutingPath(int path) {
1460 assertRunOnServiceThread();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001461 // Seq #23
1462 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001463 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim546d8672014-11-24 07:30:54 +09001464 startRoutingControl(getActivePath(), newPath, true, null);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001465 }
1466 }
1467
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001468 /**
1469 * Launch routing control process.
1470 *
1471 * @param routingForBootup true if routing control is initiated due to One Touch Play
1472 * or TV power on
1473 */
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001474 @ServiceThreadOnly
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001475 void launchRoutingControl(boolean routingForBootup) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001476 assertRunOnServiceThread();
1477 // Seq #24
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001478 if (getActivePortId() != Constants.INVALID_PORT_ID) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001479 if (!routingForBootup && !isProhibitMode()) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001480 int newPath = mService.portIdToPath(getActivePortId());
1481 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +09001482 startRoutingControl(getActivePath(), newPath, routingForBootup, null);
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001483 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001484 } else {
1485 int activePath = mService.getPhysicalAddress();
1486 setActivePath(activePath);
Jinsuk Kim2ab6d9f2015-01-16 15:49:16 +09001487 if (!routingForBootup
1488 && !mDelayedMessageBuffer.isBuffered(Constants.MESSAGE_ACTIVE_SOURCE)) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001489 mService.sendCecCommand(HdmiCecMessageBuilder.buildActiveSource(mAddress,
1490 activePath));
1491 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001492 }
1493 }
1494
Jungshik Jang79c58a42014-06-16 16:45:36 +09001495 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001496 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
Jungshik Jang79c58a42014-06-16 16:45:36 +09001497 * the given routing path. CEC devices use routing path for its physical address to
1498 * describe the hierarchy of the devices in the network.
1499 *
1500 * @param path routing path or physical address
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001501 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
Jungshik Jang79c58a42014-06-16 16:45:36 +09001502 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001503 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001504 final HdmiDeviceInfo getDeviceInfoByPath(int path) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001505 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001506 for (HdmiDeviceInfo info : getDeviceInfoList(false)) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001507 if (info.getPhysicalAddress() == path) {
1508 return info;
1509 }
1510 }
1511 return null;
1512 }
1513
1514 /**
Jinsuk Kim7640d982015-01-28 16:44:07 +09001515 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
1516 * the given routing path. This is the version accessible safely from threads
1517 * other than service thread.
1518 *
1519 * @param path routing path or physical address
1520 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
1521 */
1522 HdmiDeviceInfo getSafeDeviceInfoByPath(int path) {
1523 synchronized (mLock) {
1524 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1525 if (info.getPhysicalAddress() == path) {
1526 return info;
1527 }
1528 }
1529 return null;
1530 }
1531 }
1532
1533 /**
Jungshik Jang79c58a42014-06-16 16:45:36 +09001534 * Whether a device of the specified physical address and logical address exists
1535 * in a device info list. However, both are minimal condition and it could
1536 * be different device from the original one.
1537 *
Jungshik Jang79c58a42014-06-16 16:45:36 +09001538 * @param logicalAddress logical address of a device to be searched
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001539 * @param physicalAddress physical address of a device to be searched
Jungshik Jang79c58a42014-06-16 16:45:36 +09001540 * @return true if exist; otherwise false
1541 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001542 @ServiceThreadOnly
Jinsuk Kim7cd4a582015-01-29 09:50:25 +09001543 boolean isInDeviceList(int logicalAddress, int physicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001544 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001545 HdmiDeviceInfo device = getCecDeviceInfo(logicalAddress);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001546 if (device == null) {
1547 return false;
1548 }
1549 return device.getPhysicalAddress() == physicalAddress;
1550 }
1551
1552 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001553 @ServiceThreadOnly
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001554 void onHotplug(int portId, boolean connected) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001555 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001556
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001557 if (!connected) {
1558 removeCecSwitches(portId);
1559 }
Jungshik Jang79c58a42014-06-16 16:45:36 +09001560 // Tv device will have permanent HotplugDetectionAction.
1561 List<HotplugDetectionAction> hotplugActions = getActions(HotplugDetectionAction.class);
1562 if (!hotplugActions.isEmpty()) {
1563 // Note that hotplug action is single action running on a machine.
1564 // "pollAllDevicesNow" cleans up timer and start poll action immediately.
Jungshik Jang24c23c12014-07-07 18:04:39 +09001565 // It covers seq #40, #43.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001566 hotplugActions.get(0).pollAllDevicesNow();
1567 }
Jungshik Jang60cffce2014-06-12 18:03:04 +09001568 }
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001569
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001570 private void removeCecSwitches(int portId) {
1571 Iterator<Integer> it = mCecSwitches.iterator();
1572 while (!it.hasNext()) {
1573 int path = it.next();
1574 if (pathToPortId(path) == portId) {
1575 it.remove();
1576 }
1577 }
1578 }
1579
Jinsuk Kime6e8f3d2015-05-11 14:17:04 +09001580 @Override
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001581 @ServiceThreadOnly
1582 void setAutoDeviceOff(boolean enabled) {
1583 assertRunOnServiceThread();
1584 mAutoDeviceOff = enabled;
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001585 }
1586
1587 @ServiceThreadOnly
1588 void setAutoWakeup(boolean enabled) {
1589 assertRunOnServiceThread();
1590 mAutoWakeup = enabled;
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001591 }
Yuncheol Heo38db6292014-07-01 14:15:14 +09001592
Yuncheol Heo25c20292014-07-31 17:59:39 +09001593 @ServiceThreadOnly
1594 boolean getAutoWakeup() {
1595 assertRunOnServiceThread();
1596 return mAutoWakeup;
1597 }
1598
Yuncheol Heo38db6292014-07-01 14:15:14 +09001599 @Override
1600 @ServiceThreadOnly
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001601 protected void disableDevice(boolean initiatedByCec, PendingActionClearedCallback callback) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001602 assertRunOnServiceThread();
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001603 mService.unregisterTvInputCallback(mTvInputCallback);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001604 // Remove any repeated working actions.
1605 // HotplugDetectionAction will be reinstated during the wake up process.
1606 // HdmiControlService.onWakeUp() -> initializeLocalDevices() ->
1607 // LocalDeviceTv.onAddressAllocated() -> launchDeviceDiscovery().
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001608 removeAction(DeviceDiscoveryAction.class);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001609 removeAction(HotplugDetectionAction.class);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001610 removeAction(PowerStatusMonitorAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001611 // Remove recording actions.
Jungshik Jangb6591b82014-07-23 16:10:23 +09001612 removeAction(OneTouchRecordAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001613 removeAction(TimerRecordingAction.class);
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001614
1615 disableSystemAudioIfExist();
1616 disableArcIfExist();
Jinsuk Kima5445ce2015-03-30 17:14:58 +09001617
1618 super.disableDevice(initiatedByCec, callback);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +09001619 clearDeviceInfoList();
Jinsuk Kim7cc51c62015-04-30 10:39:35 +09001620 getActiveSource().invalidate();
1621 setActivePath(Constants.INVALID_PHYSICAL_ADDRESS);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001622 checkIfPendingActionsCleared();
1623 }
1624
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001625 @ServiceThreadOnly
1626 private void disableSystemAudioIfExist() {
1627 assertRunOnServiceThread();
1628 if (getAvrDeviceInfo() == null) {
1629 return;
1630 }
1631
1632 // Seq #31.
1633 removeAction(SystemAudioActionFromAvr.class);
1634 removeAction(SystemAudioActionFromTv.class);
1635 removeAction(SystemAudioAutoInitiationAction.class);
1636 removeAction(SystemAudioStatusAction.class);
1637 removeAction(VolumeControlAction.class);
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001638 }
1639
1640 @ServiceThreadOnly
1641 private void disableArcIfExist() {
1642 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001643 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001644 if (avr == null) {
1645 return;
1646 }
1647
1648 // Seq #44.
1649 removeAction(RequestArcInitiationAction.class);
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +09001650 if (!hasAction(RequestArcTerminationAction.class) && isArcEstablished()) {
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001651 addAndStartAction(new RequestArcTerminationAction(this, avr.getLogicalAddress()));
1652 }
1653 }
1654
Yuncheol Heo38db6292014-07-01 14:15:14 +09001655 @Override
1656 @ServiceThreadOnly
Jinsuk Kime6e8f3d2015-05-11 14:17:04 +09001657 protected void onStandby(boolean initiatedByCec, int standbyAction) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001658 assertRunOnServiceThread();
1659 // Seq #11
1660 if (!mService.isControlEnabled()) {
1661 return;
1662 }
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001663 if (!initiatedByCec && mAutoDeviceOff) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001664 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001665 mAddress, Constants.ADDR_BROADCAST));
Yuncheol Heo38db6292014-07-01 14:15:14 +09001666 }
1667 }
1668
Jinsuk Kim4d43d932014-07-03 16:43:58 +09001669 boolean isProhibitMode() {
1670 return mService.isProhibitMode();
1671 }
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001672
1673 boolean isPowerStandbyOrTransient() {
1674 return mService.isPowerStandbyOrTransient();
Jungshik Jang8866c812014-07-08 14:42:28 +09001675 }
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001676
Jungshik Jang339227d2014-08-25 15:37:20 +09001677 @ServiceThreadOnly
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001678 void displayOsd(int messageId) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001679 assertRunOnServiceThread();
1680 mService.displayOsd(messageId);
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001681 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001682
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001683 @ServiceThreadOnly
1684 void displayOsd(int messageId, int extra) {
1685 assertRunOnServiceThread();
1686 mService.displayOsd(messageId, extra);
1687 }
1688
Jungshik Jangb6591b82014-07-23 16:10:23 +09001689 // Seq #54 and #55
1690 @ServiceThreadOnly
Jungshik Jang4480efa2014-09-04 17:08:34 +09001691 int startOneTouchRecord(int recorderAddress, byte[] recordSource) {
Jungshik Jangb6591b82014-07-23 16:10:23 +09001692 assertRunOnServiceThread();
1693 if (!mService.isControlEnabled()) {
1694 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001695 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001696 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001697 }
1698
1699 if (!checkRecorder(recorderAddress)) {
1700 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001701 announceOneTouchRecordResult(recorderAddress,
1702 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001703 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001704 }
1705
1706 if (!checkRecordSource(recordSource)) {
1707 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001708 announceOneTouchRecordResult(recorderAddress,
1709 ONE_TOUCH_RECORD_FAIL_TO_RECORD_DISPLAYED_SCREEN);
Jinsuk Kimd47abef2015-01-17 07:38:24 +09001710 return Constants.ABORT_CANNOT_PROVIDE_SOURCE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001711 }
1712
1713 addAndStartAction(new OneTouchRecordAction(this, recorderAddress, recordSource));
1714 Slog.i(TAG, "Start new [One Touch Record]-Target:" + recorderAddress + ", recordSource:"
1715 + Arrays.toString(recordSource));
Jungshik Jang4480efa2014-09-04 17:08:34 +09001716 return Constants.ABORT_NO_ERROR;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001717 }
1718
1719 @ServiceThreadOnly
1720 void stopOneTouchRecord(int recorderAddress) {
1721 assertRunOnServiceThread();
1722 if (!mService.isControlEnabled()) {
1723 Slog.w(TAG, "Can not stop one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001724 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001725 return;
1726 }
1727
1728 if (!checkRecorder(recorderAddress)) {
1729 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001730 announceOneTouchRecordResult(recorderAddress,
1731 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001732 return;
1733 }
1734
1735 // Remove one touch record action so that other one touch record can be started.
1736 removeAction(OneTouchRecordAction.class);
1737 mService.sendCecCommand(HdmiCecMessageBuilder.buildRecordOff(mAddress, recorderAddress));
1738 Slog.i(TAG, "Stop [One Touch Record]-Target:" + recorderAddress);
1739 }
1740
1741 private boolean checkRecorder(int recorderAddress) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001742 HdmiDeviceInfo device = getCecDeviceInfo(recorderAddress);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001743 return (device != null)
1744 && (HdmiUtils.getTypeFromAddress(recorderAddress)
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001745 == HdmiDeviceInfo.DEVICE_RECORDER);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001746 }
1747
1748 private boolean checkRecordSource(byte[] recordSource) {
1749 return (recordSource != null) && HdmiRecordSources.checkRecordSource(recordSource);
1750 }
1751
1752 @ServiceThreadOnly
1753 void startTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1754 assertRunOnServiceThread();
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001755 if (!mService.isControlEnabled()) {
1756 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001757 announceTimerRecordingResult(recorderAddress,
1758 TIMER_RECORDING_RESULT_EXTRA_CEC_DISABLED);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001759 return;
1760 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001761
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001762 if (!checkRecorder(recorderAddress)) {
1763 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001764 announceTimerRecordingResult(recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001765 TIMER_RECORDING_RESULT_EXTRA_CHECK_RECORDER_CONNECTION);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001766 return;
1767 }
1768
1769 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1770 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
1771 announceTimerRecordingResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001772 recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001773 TIMER_RECORDING_RESULT_EXTRA_FAIL_TO_RECORD_SELECTED_SOURCE);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001774 return;
1775 }
1776
1777 addAndStartAction(
1778 new TimerRecordingAction(this, recorderAddress, sourceType, recordSource));
1779 Slog.i(TAG, "Start [Timer Recording]-Target:" + recorderAddress + ", SourceType:"
1780 + sourceType + ", RecordSource:" + Arrays.toString(recordSource));
1781 }
1782
1783 private boolean checkTimerRecordingSource(int sourceType, byte[] recordSource) {
1784 return (recordSource != null)
1785 && HdmiTimerRecordSources.checkTimerRecordSource(sourceType, recordSource);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001786 }
1787
1788 @ServiceThreadOnly
1789 void clearTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1790 assertRunOnServiceThread();
Jungshik Jange5a93372014-07-25 13:41:14 +09001791 if (!mService.isControlEnabled()) {
1792 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001793 announceClearTimerRecordingResult(recorderAddress, CLEAR_TIMER_STATUS_CEC_DISABLE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001794 return;
1795 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001796
Jungshik Jange5a93372014-07-25 13:41:14 +09001797 if (!checkRecorder(recorderAddress)) {
1798 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001799 announceClearTimerRecordingResult(recorderAddress,
1800 CLEAR_TIMER_STATUS_CHECK_RECORDER_CONNECTION);
Jungshik Jange5a93372014-07-25 13:41:14 +09001801 return;
1802 }
1803
1804 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1805 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001806 announceClearTimerRecordingResult(recorderAddress,
1807 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001808 return;
1809 }
1810
1811 sendClearTimerMessage(recorderAddress, sourceType, recordSource);
1812 }
1813
Jungshik Jang326aef02014-11-05 12:50:35 +09001814 private void sendClearTimerMessage(final int recorderAddress, int sourceType,
1815 byte[] recordSource) {
Jungshik Jange5a93372014-07-25 13:41:14 +09001816 HdmiCecMessage message = null;
1817 switch (sourceType) {
1818 case TIMER_RECORDING_TYPE_DIGITAL:
1819 message = HdmiCecMessageBuilder.buildClearDigitalTimer(mAddress, recorderAddress,
1820 recordSource);
1821 break;
1822 case TIMER_RECORDING_TYPE_ANALOGUE:
1823 message = HdmiCecMessageBuilder.buildClearAnalogueTimer(mAddress, recorderAddress,
1824 recordSource);
1825 break;
1826 case TIMER_RECORDING_TYPE_EXTERNAL:
1827 message = HdmiCecMessageBuilder.buildClearExternalTimer(mAddress, recorderAddress,
1828 recordSource);
1829 break;
1830 default:
1831 Slog.w(TAG, "Invalid source type:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001832 announceClearTimerRecordingResult(recorderAddress,
1833 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001834 return;
1835
1836 }
1837 mService.sendCecCommand(message, new SendMessageCallback() {
1838 @Override
1839 public void onSendCompleted(int error) {
Donghyun Chobc6e3722016-11-04 05:25:52 +09001840 if (error != SendMessageResult.SUCCESS) {
Jungshik Jang326aef02014-11-05 12:50:35 +09001841 announceClearTimerRecordingResult(recorderAddress,
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001842 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001843 }
1844 }
1845 });
Jungshik Jangb6591b82014-07-23 16:10:23 +09001846 }
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001847
1848 void updateDevicePowerStatus(int logicalAddress, int newPowerStatus) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001849 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001850 if (info == null) {
1851 Slog.w(TAG, "Can not update power status of non-existing device:" + logicalAddress);
1852 return;
1853 }
1854
1855 if (info.getDevicePowerStatus() == newPowerStatus) {
1856 return;
1857 }
1858
1859 HdmiDeviceInfo newInfo = HdmiUtils.cloneHdmiDeviceInfo(info, newPowerStatus);
1860 // addDeviceInfo replaces old device info with new one if exists.
1861 addDeviceInfo(newInfo);
1862
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001863 invokeDeviceEventListener(newInfo, HdmiControlManager.DEVICE_EVENT_UPDATE_DEVICE);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001864 }
Terry Heo959d2db2014-08-28 16:45:41 +09001865
1866 @Override
Yuncheol Heo184b1242014-09-12 15:09:07 +09001867 protected boolean handleMenuStatus(HdmiCecMessage message) {
1868 // Do nothing and just return true not to prevent from responding <Feature Abort>.
1869 return true;
1870 }
1871
1872 @Override
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001873 protected void sendStandby(int deviceId) {
1874 HdmiDeviceInfo targetDevice = mDeviceInfos.get(deviceId);
1875 if (targetDevice == null) {
1876 return;
1877 }
1878 int targetAddress = targetDevice.getLogicalAddress();
1879 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(mAddress, targetAddress));
1880 }
1881
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001882 @ServiceThreadOnly
1883 void processAllDelayedMessages() {
1884 assertRunOnServiceThread();
1885 mDelayedMessageBuffer.processAllMessages();
1886 }
1887
1888 @ServiceThreadOnly
1889 void processDelayedMessages(int address) {
1890 assertRunOnServiceThread();
1891 mDelayedMessageBuffer.processMessagesForDevice(address);
1892 }
1893
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +09001894 @ServiceThreadOnly
1895 void processDelayedActiveSource(int address) {
1896 assertRunOnServiceThread();
1897 mDelayedMessageBuffer.processActiveSource(address);
1898 }
1899
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001900 @Override
Terry Heo959d2db2014-08-28 16:45:41 +09001901 protected void dump(final IndentingPrintWriter pw) {
1902 super.dump(pw);
1903 pw.println("mArcEstablished: " + mArcEstablished);
1904 pw.println("mArcFeatureEnabled: " + mArcFeatureEnabled);
Terry Heo959d2db2014-08-28 16:45:41 +09001905 pw.println("mSystemAudioMute: " + mSystemAudioMute);
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001906 pw.println("mSystemAudioControlFeatureEnabled: " + mSystemAudioControlFeatureEnabled);
Terry Heo959d2db2014-08-28 16:45:41 +09001907 pw.println("mAutoDeviceOff: " + mAutoDeviceOff);
1908 pw.println("mAutoWakeup: " + mAutoWakeup);
1909 pw.println("mSkipRoutingControl: " + mSkipRoutingControl);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +09001910 pw.println("mPrevPortId: " + mPrevPortId);
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001911 pw.println("CEC devices:");
1912 pw.increaseIndent();
1913 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1914 pw.println(info);
1915 }
1916 pw.decreaseIndent();
Terry Heo959d2db2014-08-28 16:45:41 +09001917 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +09001918}