blob: 81bccdc7dfaa4f052ebaffaecafe638435ff26c0 [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 Kima6ce7702014-05-11 06:54:49 +090044import android.os.RemoteException;
Jinsuk Kim7ecfbae2014-07-11 14:16:29 +090045import android.provider.Settings.Global;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090046import android.util.ArraySet;
Jungshik Jang092b4452014-06-11 15:19:17 +090047import android.util.Slog;
Jungshik Jang79c58a42014-06-16 16:45:36 +090048import android.util.SparseArray;
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +090049import android.util.SparseBooleanArray;
Jungshik Jang79c58a42014-06-16 16:45:36 +090050import com.android.internal.annotations.GuardedBy;
Terry Heo959d2db2014-08-28 16:45:41 +090051import com.android.internal.util.IndentingPrintWriter;
Jungshik Jang60cffce2014-06-12 18:03:04 +090052import com.android.server.hdmi.DeviceDiscoveryAction.DeviceDiscoveryCallback;
Jungshik Janga5b74142014-06-23 18:03:10 +090053import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly;
Jungshik Jange5a93372014-07-25 13:41:14 +090054import com.android.server.hdmi.HdmiControlService.SendMessageCallback;
Jungshik Jang8f2ed352014-07-07 15:02:47 +090055import java.io.UnsupportedEncodingException;
Jungshik Jang79c58a42014-06-16 16:45:36 +090056import java.util.ArrayList;
Jungshik Jangb6591b82014-07-23 16:10:23 +090057import java.util.Arrays;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090058import java.util.Collection;
Jinsuk Kim0a3316b2014-06-14 09:33:55 +090059import java.util.Collections;
Donghyun Chobc6e3722016-11-04 05:25:52 +090060import java.util.HashMap;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090061import java.util.Iterator;
Jinsuk Kim0a3316b2014-06-14 09:33:55 +090062import java.util.List;
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090063
64/**
65 * Represent a logical device of type TV residing in Android system.
66 */
67final class HdmiCecLocalDeviceTv extends HdmiCecLocalDevice {
Jungshik Jang092b4452014-06-11 15:19:17 +090068 private static final String TAG = "HdmiCecLocalDeviceTv";
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090069
Jungshik Jang12e5dce2014-07-24 15:27:44 +090070 // Whether ARC is available or not. "true" means that ARC is established between TV and
Jungshik Janga13da0d2014-06-30 16:26:06 +090071 // AVR as audio receiver.
72 @ServiceThreadOnly
73 private boolean mArcEstablished = false;
74
Donghyun Choc1fa9af2016-12-27 18:31:09 +090075 // Stores whether ARC feature is enabled per port.
76 // True by default for all the ARC-enabled ports.
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +090077 private final SparseBooleanArray mArcFeatureEnabled = new SparseBooleanArray();
Jungshik Jang79c58a42014-06-16 16:45:36 +090078
Jungshik Jang377dcbd2014-07-15 15:49:02 +090079 // Whether System audio mode is activated or not.
80 // This becomes true only when all system audio sequences are finished.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +090081 @GuardedBy("mLock")
Jungshik Jang377dcbd2014-07-15 15:49:02 +090082 private boolean mSystemAudioActivated = false;
Jungshik Jang79c58a42014-06-16 16:45:36 +090083
Donghyun Choc1fa9af2016-12-27 18:31:09 +090084 // Whether the System Audio Control feature is enabled or not. True by default.
85 @GuardedBy("mLock")
86 private boolean mSystemAudioControlFeatureEnabled;
87
Jinsuk Kim83335712014-06-24 07:57:00 +090088 // The previous port id (input) before switching to the new one. This is remembered in order to
89 // be able to switch to it upon receiving <Inactive Source> from currently active source.
90 // This remains valid only when the active source was switched via one touch play operation
91 // (either by TV or source device). Manual port switching invalidates this value to
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090092 // Constants.PORT_INVALID, for which case <Inactive Source> does not do anything.
Jinsuk Kim83335712014-06-24 07:57:00 +090093 @GuardedBy("mLock")
94 private int mPrevPortId;
95
Jungshik Jang8fa36b12014-06-25 15:51:36 +090096 @GuardedBy("mLock")
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090097 private int mSystemAudioVolume = Constants.UNKNOWN_VOLUME;
Jungshik Jang8fa36b12014-06-25 15:51:36 +090098
99 @GuardedBy("mLock")
100 private boolean mSystemAudioMute = false;
101
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900102 // Copy of mDeviceInfos to guarantee thread-safety.
103 @GuardedBy("mLock")
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900104 private List<HdmiDeviceInfo> mSafeAllDeviceInfos = Collections.emptyList();
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +0900105 // All external cec input(source) devices. Does not include system audio device.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900106 @GuardedBy("mLock")
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900107 private List<HdmiDeviceInfo> mSafeExternalInputs = Collections.emptyList();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900108
Jungshik Jang79c58a42014-06-16 16:45:36 +0900109 // Map-like container of all cec devices including local ones.
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900110 // device id is used as key of container.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900111 // This is not thread-safe. For external purpose use mSafeDeviceInfos.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900112 private final SparseArray<HdmiDeviceInfo> mDeviceInfos = new SparseArray<>();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900113
Jinsuk Kim160a6e52014-07-02 06:16:36 +0900114 // If true, TV going to standby mode puts other devices also to standby.
115 private boolean mAutoDeviceOff;
116
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900117 // If true, TV wakes itself up when receiving <Text/Image View On>.
118 private boolean mAutoWakeup;
119
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900120 // List of the logical address of local CEC devices. Unmodifiable, thread-safe.
121 private List<Integer> mLocalDeviceAddresses;
122
Yuncheol Heo25c20292014-07-31 17:59:39 +0900123 private final HdmiCecStandbyModeHandler mStandbyHandler;
124
Jinsuk Kimd5307192014-08-22 09:07:40 +0900125 // If true, do not do routing control/send active source for internal source.
126 // Set to true when the device was woken up by <Text/Image View On>.
127 private boolean mSkipRoutingControl;
128
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900129 // Set of physical addresses of CEC switches on the CEC bus. Managed independently from
130 // other CEC devices since they might not have logical address.
131 private final ArraySet<Integer> mCecSwitches = new ArraySet<Integer>();
132
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900133 // Message buffer used to buffer selected messages to process later. <Active Source>
134 // from a source device, for instance, needs to be buffered if the device is not
135 // discovered yet. The buffered commands are taken out and when they are ready to
136 // handle.
137 private final DelayedMessageBuffer mDelayedMessageBuffer = new DelayedMessageBuffer(this);
138
139 // Defines the callback invoked when TV input framework is updated with input status.
140 // We are interested in the notification for HDMI input addition event, in order to
141 // process any CEC commands that arrived before the input is added.
142 private final TvInputCallback mTvInputCallback = new TvInputCallback() {
143 @Override
144 public void onInputAdded(String inputId) {
145 TvInputInfo tvInfo = mService.getTvInputManager().getTvInputInfo(inputId);
Terry Heoa22d77e2015-11-05 15:53:33 +0900146 if (tvInfo == null) return;
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900147 HdmiDeviceInfo info = tvInfo.getHdmiDeviceInfo();
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900148 if (info == null) return;
149 addTvInput(inputId, info.getId());
150 if (info.isCecDevice()) {
151 processDelayedActiveSource(info.getLogicalAddress());
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900152 }
153 }
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900154
155 @Override
156 public void onInputRemoved(String inputId) {
157 removeTvInput(inputId);
158 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900159 };
160
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900161 // Keeps the mapping (TV input ID, HDMI device ID) to keep track of the TV inputs ready to
162 // accept input switching request from HDMI devices. Requests for which the corresponding
163 // input ID is not yet registered by TV input framework need to be buffered for delayed
164 // processing.
165 private final HashMap<String, Integer> mTvInputs = new HashMap<>();
166
167 @ServiceThreadOnly
168 private void addTvInput(String inputId, int deviceId) {
169 assertRunOnServiceThread();
170 mTvInputs.put(inputId, deviceId);
171 }
172
173 @ServiceThreadOnly
174 private void removeTvInput(String inputId) {
175 assertRunOnServiceThread();
176 mTvInputs.remove(inputId);
177 }
178
179 @Override
180 @ServiceThreadOnly
181 protected boolean isInputReady(int deviceId) {
182 assertRunOnServiceThread();
183 return mTvInputs.containsValue(deviceId);
184 }
185
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900186 private SelectRequestBuffer mSelectRequestBuffer;
187
Jungshik Jang3ee65722014-06-03 16:22:30 +0900188 HdmiCecLocalDeviceTv(HdmiControlService service) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900189 super(service, HdmiDeviceInfo.DEVICE_TV);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900190 mPrevPortId = Constants.INVALID_PORT_ID;
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900191 mAutoDeviceOff = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED,
192 true);
193 mAutoWakeup = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_WAKEUP_ENABLED, true);
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900194 mSystemAudioControlFeatureEnabled =
195 mService.readBooleanSetting(Global.HDMI_SYSTEM_AUDIO_CONTROL_ENABLED, true);
Yuncheol Heo25c20292014-07-31 17:59:39 +0900196 mStandbyHandler = new HdmiCecStandbyModeHandler(service, this);
Jungshik Jang8b308d92014-05-29 21:52:28 +0900197 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900198
Jungshik Jang8b308d92014-05-29 21:52:28 +0900199 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900200 @ServiceThreadOnly
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900201 protected void onAddressAllocated(int logicalAddress, int reason) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900202 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900203 List<HdmiPortInfo> ports = mService.getPortInfo();
204 for (HdmiPortInfo port : ports) {
205 mArcFeatureEnabled.put(port.getId(), port.isArcSupported());
206 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900207 mService.registerTvInputCallback(mTvInputCallback);
Jungshik Jang3ee65722014-06-03 16:22:30 +0900208 mService.sendCecCommand(HdmiCecMessageBuilder.buildReportPhysicalAddressCommand(
209 mAddress, mService.getPhysicalAddress(), mDeviceType));
210 mService.sendCecCommand(HdmiCecMessageBuilder.buildDeviceVendorIdCommand(
211 mAddress, mService.getVendorId()));
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900212 mCecSwitches.add(mService.getPhysicalAddress()); // TV is a CEC switch too.
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900213 mTvInputs.clear();
Jinsuk Kimd5307192014-08-22 09:07:40 +0900214 mSkipRoutingControl = (reason == HdmiControlService.INITIATED_BY_WAKE_UP_MESSAGE);
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900215 launchRoutingControl(reason != HdmiControlService.INITIATED_BY_ENABLE_CEC &&
216 reason != HdmiControlService.INITIATED_BY_BOOT_UP);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900217 mLocalDeviceAddresses = initLocalDeviceAddresses();
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900218 resetSelectRequestBuffer();
Jungshik Jang60cffce2014-06-12 18:03:04 +0900219 launchDeviceDiscovery();
Jungshik Jang6f34f5a2014-07-08 21:17:29 +0900220 }
221
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900222
223 @ServiceThreadOnly
224 private List<Integer> initLocalDeviceAddresses() {
225 assertRunOnServiceThread();
226 List<Integer> addresses = new ArrayList<>();
227 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
228 addresses.add(device.getDeviceInfo().getLogicalAddress());
229 }
230 return Collections.unmodifiableList(addresses);
231 }
232
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900233
234 @ServiceThreadOnly
235 public void setSelectRequestBuffer(SelectRequestBuffer requestBuffer) {
236 assertRunOnServiceThread();
237 mSelectRequestBuffer = requestBuffer;
238 }
239
240 @ServiceThreadOnly
241 private void resetSelectRequestBuffer() {
242 assertRunOnServiceThread();
243 setSelectRequestBuffer(SelectRequestBuffer.EMPTY_BUFFER);
244 }
245
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900246 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900247 protected int getPreferredAddress() {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900248 return Constants.ADDR_TV;
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900249 }
250
251 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900252 protected void setPreferredAddress(int addr) {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900253 Slog.w(TAG, "Preferred addres will not be stored for TV");
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900254 }
255
Yuncheol Heo25c20292014-07-31 17:59:39 +0900256 @Override
257 @ServiceThreadOnly
258 boolean dispatchMessage(HdmiCecMessage message) {
259 assertRunOnServiceThread();
Donghyun Choafd26a22016-12-23 15:53:28 +0900260 if (mService.isPowerStandby() && !mService.isWakeUpMessageReceived()
261 && mStandbyHandler.handleCommand(message)) {
Yuncheol Heo25c20292014-07-31 17:59:39 +0900262 return true;
263 }
264 return super.onMessage(message);
265 }
266
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900267 /**
268 * Performs the action 'device select', or 'one touch play' initiated by TV.
269 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900270 * @param id id of HDMI device to select
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900271 * @param callback callback object to report the result with
272 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900273 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900274 void deviceSelect(int id, IHdmiControlCallback callback) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900275 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900276 HdmiDeviceInfo targetDevice = mDeviceInfos.get(id);
277 if (targetDevice == null) {
278 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
279 return;
280 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900281 int targetAddress = targetDevice.getLogicalAddress();
Jinsuk Kim58500f42014-08-05 12:48:35 +0900282 ActiveSource active = getActiveSource();
Yu.Ishihara6b2a6172015-03-25 09:44:06 +0900283 if (targetDevice.getDevicePowerStatus() == HdmiControlManager.POWER_STATUS_ON
284 && active.isValid()
285 && targetAddress == active.logicalAddress) {
Jinsuk Kim58500f42014-08-05 12:48:35 +0900286 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
287 return;
288 }
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900289 if (targetAddress == Constants.ADDR_INTERNAL) {
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900290 handleSelectInternalSource();
291 // Switching to internal source is always successful even when CEC control is disabled.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900292 setActiveSource(targetAddress, mService.getPhysicalAddress());
Jinsuk Kim7e742062014-07-30 13:19:13 +0900293 setActivePath(mService.getPhysicalAddress());
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900294 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
Jinsuk Kim83335712014-06-24 07:57:00 +0900295 return;
296 }
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900297 if (!mService.isControlEnabled()) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900298 setActiveSource(targetDevice);
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900299 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
300 return;
301 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900302 removeAction(DeviceSelectAction.class);
303 addAndStartAction(new DeviceSelectAction(this, targetDevice, callback));
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900304 }
305
Jungshik Janga5b74142014-06-23 18:03:10 +0900306 @ServiceThreadOnly
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900307 private void handleSelectInternalSource() {
Jinsuk Kima062a932014-06-18 10:00:39 +0900308 assertRunOnServiceThread();
Jinsuk Kim83335712014-06-24 07:57:00 +0900309 // Seq #18
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900310 if (mService.isControlEnabled() && mActiveSource.logicalAddress != mAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900311 updateActiveSource(mAddress, mService.getPhysicalAddress());
Jinsuk Kimd5307192014-08-22 09:07:40 +0900312 if (mSkipRoutingControl) {
313 mSkipRoutingControl = false;
314 return;
315 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900316 HdmiCecMessage activeSource = HdmiCecMessageBuilder.buildActiveSource(
317 mAddress, mService.getPhysicalAddress());
318 mService.sendCecCommand(activeSource);
319 }
320 }
321
322 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900323 void updateActiveSource(int logicalAddress, int physicalAddress) {
324 assertRunOnServiceThread();
325 updateActiveSource(ActiveSource.of(logicalAddress, physicalAddress));
326 }
327
328 @ServiceThreadOnly
329 void updateActiveSource(ActiveSource newActive) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900330 assertRunOnServiceThread();
331 // Seq #14
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900332 if (mActiveSource.equals(newActive)) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900333 return;
334 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900335 setActiveSource(newActive);
336 int logicalAddress = newActive.logicalAddress;
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900337 if (getCecDeviceInfo(logicalAddress) != null && logicalAddress != mAddress) {
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900338 if (mService.pathToPortId(newActive.physicalAddress) == getActivePortId()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900339 setPrevPortId(getActivePortId());
340 }
341 // TODO: Show the OSD banner related to the new active source device.
342 } else {
343 // TODO: If displayed, remove the OSD banner related to the previous
344 // active source device.
345 }
346 }
347
Jinsuk Kim2b152012014-07-25 08:22:26 +0900348 int getPortId(int physicalAddress) {
349 return mService.pathToPortId(physicalAddress);
350 }
351
Jinsuk Kim83335712014-06-24 07:57:00 +0900352 /**
353 * Returns the previous port id kept to handle input switching on <Inactive Source>.
354 */
355 int getPrevPortId() {
356 synchronized (mLock) {
357 return mPrevPortId;
358 }
359 }
360
361 /**
362 * Sets the previous port id. INVALID_PORT_ID invalidates it, hence no actions will be
363 * taken for <Inactive Source>.
364 */
365 void setPrevPortId(int portId) {
366 synchronized (mLock) {
367 mPrevPortId = portId;
368 }
369 }
370
371 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900372 void updateActiveInput(int path, boolean notifyInputChange) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900373 assertRunOnServiceThread();
374 // Seq #15
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900375 setActivePath(path);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900376 // TODO: Handle PAP/PIP case.
377 // Show OSD port change banner
378 if (notifyInputChange) {
379 ActiveSource activeSource = getActiveSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900380 HdmiDeviceInfo info = getCecDeviceInfo(activeSource.logicalAddress);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900381 if (info == null) {
Jinsuk Kim6ad7cbd2015-01-06 11:30:56 +0900382 info = mService.getDeviceInfoByPort(getActivePortId());
383 if (info == null) {
384 // No CEC/MHL device is present at the port. Attempt to switch to
385 // the hardware port itself for non-CEC devices that may be connected.
386 info = new HdmiDeviceInfo(path, getActivePortId());
387 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900388 }
389 mService.invokeInputChangeListener(info);
390 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900391 }
392
393 @ServiceThreadOnly
394 void doManualPortSwitching(int portId, IHdmiControlCallback callback) {
395 assertRunOnServiceThread();
396 // Seq #20
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900397 if (!mService.isValidPortId(portId)) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900398 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
Jinsuk Kima062a932014-06-18 10:00:39 +0900399 return;
400 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900401 if (portId == getActivePortId()) {
402 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
403 return;
404 }
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900405 mActiveSource.invalidate();
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900406 if (!mService.isControlEnabled()) {
407 setActivePortId(portId);
408 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
409 return;
410 }
Jinsuk Kim1f8d1c572014-08-12 15:39:53 +0900411 int oldPath = getActivePortId() != Constants.INVALID_PORT_ID
412 ? mService.portIdToPath(getActivePortId()) : getDeviceInfo().getPhysicalAddress();
Jinsuk Kimd5307192014-08-22 09:07:40 +0900413 setActivePath(oldPath);
414 if (mSkipRoutingControl) {
415 mSkipRoutingControl = false;
416 return;
417 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900418 int newPath = mService.portIdToPath(portId);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900419 startRoutingControl(oldPath, newPath, true, callback);
420 }
421
422 @ServiceThreadOnly
423 void startRoutingControl(int oldPath, int newPath, boolean queryDevicePowerStatus,
424 IHdmiControlCallback callback) {
425 assertRunOnServiceThread();
426 if (oldPath == newPath) {
427 return;
428 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900429 HdmiCecMessage routingChange =
430 HdmiCecMessageBuilder.buildRoutingChange(mAddress, oldPath, newPath);
431 mService.sendCecCommand(routingChange);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900432 removeAction(RoutingControlAction.class);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900433 addAndStartAction(
434 new RoutingControlAction(this, newPath, queryDevicePowerStatus, callback));
Jinsuk Kima062a932014-06-18 10:00:39 +0900435 }
436
Jungshik Jangf67113f2014-08-22 16:27:19 +0900437 @ServiceThreadOnly
Jinsuk Kimcae66272014-07-04 13:26:44 +0900438 int getPowerStatus() {
Jungshik Jangf67113f2014-08-22 16:27:19 +0900439 assertRunOnServiceThread();
Jinsuk Kimcae66272014-07-04 13:26:44 +0900440 return mService.getPowerStatus();
441 }
442
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900443 @Override
Donghyun Cho7609bc32016-12-02 15:31:52 +0900444 protected int findKeyReceiverAddress() {
Jinsuk Kim75434972014-09-25 13:52:50 +0900445 if (getActiveSource().isValid()) {
446 return getActiveSource().logicalAddress;
447 }
448 HdmiDeviceInfo info = getDeviceInfoByPath(getActivePath());
449 if (info != null) {
450 return info.getLogicalAddress();
451 }
452 return Constants.ADDR_INVALID;
453 }
454
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900455 private static void invokeCallback(IHdmiControlCallback callback, int result) {
Jinsuk Kim4893c7e2014-06-19 14:13:22 +0900456 if (callback == null) {
457 return;
458 }
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900459 try {
460 callback.onComplete(result);
461 } catch (RemoteException e) {
462 Slog.e(TAG, "Invoking callback failed:" + e);
463 }
464 }
465
Jungshik Jang092b4452014-06-11 15:19:17 +0900466 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900467 @ServiceThreadOnly
Jinsuk Kim83335712014-06-24 07:57:00 +0900468 protected boolean handleActiveSource(HdmiCecMessage message) {
469 assertRunOnServiceThread();
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900470 int logicalAddress = message.getSource();
471 int physicalAddress = HdmiUtils.twoBytesToInt(message.getParams());
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900472 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
473 if (info == null) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900474 if (!handleNewDeviceAtTheTailOfActivePath(physicalAddress)) {
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900475 HdmiLogger.debug("Device info %X not found; buffering the command", logicalAddress);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900476 mDelayedMessageBuffer.add(message);
477 }
Donghyun Cho455f09d2016-05-10 20:06:52 +0900478 } else if (isInputReady(info.getId())
479 || info.getDeviceType() == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) {
Jinsuk Kim7cc51c62015-04-30 10:39:35 +0900480 updateDevicePowerStatus(logicalAddress, HdmiControlManager.POWER_STATUS_ON);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900481 ActiveSource activeSource = ActiveSource.of(logicalAddress, physicalAddress);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900482 ActiveSourceHandler.create(this, null).process(activeSource, info.getDeviceType());
Donghyun Cho455f09d2016-05-10 20:06:52 +0900483 } else {
484 HdmiLogger.debug("Input not ready for device: %X; buffering the command", info.getId());
485 mDelayedMessageBuffer.add(message);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900486 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900487 return true;
488 }
489
490 @Override
491 @ServiceThreadOnly
492 protected boolean handleInactiveSource(HdmiCecMessage message) {
493 assertRunOnServiceThread();
494 // Seq #10
495
496 // Ignore <Inactive Source> from non-active source device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900497 if (getActiveSource().logicalAddress != message.getSource()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900498 return true;
499 }
Jinsuk Kim4d43d932014-07-03 16:43:58 +0900500 if (isProhibitMode()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900501 return true;
502 }
503 int portId = getPrevPortId();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900504 if (portId != Constants.INVALID_PORT_ID) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900505 // TODO: Do this only if TV is not showing multiview like PIP/PAP.
506
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900507 HdmiDeviceInfo inactiveSource = getCecDeviceInfo(message.getSource());
Jinsuk Kim83335712014-06-24 07:57:00 +0900508 if (inactiveSource == null) {
509 return true;
510 }
511 if (mService.pathToPortId(inactiveSource.getPhysicalAddress()) == portId) {
512 return true;
513 }
514 // TODO: Switch the TV freeze mode off
515
Jinsuk Kim83335712014-06-24 07:57:00 +0900516 doManualPortSwitching(portId, null);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900517 setPrevPortId(Constants.INVALID_PORT_ID);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900518 } else {
519 // No HDMI port to switch to was found. Notify the input change listers to
520 // switch to the lastly shown internal input.
521 mActiveSource.invalidate();
522 setActivePath(Constants.INVALID_PHYSICAL_ADDRESS);
523 mService.invokeInputChangeListener(HdmiDeviceInfo.INACTIVE_DEVICE);
Jinsuk Kim83335712014-06-24 07:57:00 +0900524 }
525 return true;
526 }
527
528 @Override
529 @ServiceThreadOnly
530 protected boolean handleRequestActiveSource(HdmiCecMessage message) {
531 assertRunOnServiceThread();
532 // Seq #19
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900533 if (mAddress == getActiveSource().logicalAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900534 mService.sendCecCommand(
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900535 HdmiCecMessageBuilder.buildActiveSource(mAddress, getActivePath()));
Jinsuk Kim83335712014-06-24 07:57:00 +0900536 }
537 return true;
538 }
539
540 @Override
541 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900542 protected boolean handleGetMenuLanguage(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900543 assertRunOnServiceThread();
Jungshik Jangf67113f2014-08-22 16:27:19 +0900544 if (!broadcastMenuLanguage(mService.getLanguage())) {
Jungshik Jang092b4452014-06-11 15:19:17 +0900545 Slog.w(TAG, "Failed to respond to <Get Menu Language>: " + message.toString());
546 }
547 return true;
548 }
549
Terry Heo1ca0a432014-08-18 10:30:32 +0900550 @ServiceThreadOnly
551 boolean broadcastMenuLanguage(String language) {
552 assertRunOnServiceThread();
553 HdmiCecMessage command = HdmiCecMessageBuilder.buildSetMenuLanguageCommand(
554 mAddress, language);
555 if (command != null) {
556 mService.sendCecCommand(command);
557 return true;
558 }
559 return false;
560 }
561
Jungshik Jang60cffce2014-06-12 18:03:04 +0900562 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900563 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900564 protected boolean handleReportPhysicalAddress(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900565 assertRunOnServiceThread();
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900566 int path = HdmiUtils.twoBytesToInt(message.getParams());
567 int address = message.getSource();
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900568 int type = message.getParams()[2];
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900569
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900570 if (updateCecSwitchInfo(address, type, path)) return true;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900571
Jungshik Jang092b4452014-06-11 15:19:17 +0900572 // Ignore if [Device Discovery Action] is going on.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900573 if (hasAction(DeviceDiscoveryAction.class)) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900574 Slog.i(TAG, "Ignored while Device Discovery Action is in progress: " + message);
Jungshik Jang092b4452014-06-11 15:19:17 +0900575 return true;
576 }
577
Jinsuk Kim4b4b9402014-09-02 10:30:10 +0900578 if (!isInDeviceList(address, path)) {
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900579 handleNewDeviceAtTheTailOfActivePath(path);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900580 }
Jinsuk Kim7cd4a582015-01-29 09:50:25 +0900581
582 // Add the device ahead with default information to handle <Active Source>
583 // promptly, rather than waiting till the new device action is finished.
584 HdmiDeviceInfo deviceInfo = new HdmiDeviceInfo(address, path, getPortId(path), type,
585 Constants.UNKNOWN_VENDOR_ID, HdmiUtils.getDefaultDeviceName(address));
586 addCecDevice(deviceInfo);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900587 startNewDeviceAction(ActiveSource.of(address, path), type);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900588 return true;
589 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900590
Jungshik Jang4480efa2014-09-04 17:08:34 +0900591 @Override
592 protected boolean handleReportPowerStatus(HdmiCecMessage command) {
593 int newStatus = command.getParams()[0] & 0xFF;
594 updateDevicePowerStatus(command.getSource(), newStatus);
595 return true;
596 }
597
598 @Override
599 protected boolean handleTimerStatus(HdmiCecMessage message) {
600 // Do nothing.
601 return true;
602 }
603
604 @Override
605 protected boolean handleRecordStatus(HdmiCecMessage message) {
606 // Do nothing.
607 return true;
608 }
609
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900610 boolean updateCecSwitchInfo(int address, int type, int path) {
611 if (address == Constants.ADDR_UNREGISTERED
612 && type == HdmiDeviceInfo.DEVICE_PURE_CEC_SWITCH) {
613 mCecSwitches.add(path);
614 updateSafeDeviceInfoList();
615 return true; // Pure switch does not need further processing. Return here.
616 }
617 if (type == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) {
618 mCecSwitches.add(path);
619 }
620 return false;
621 }
622
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900623 void startNewDeviceAction(ActiveSource activeSource, int deviceType) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900624 for (NewDeviceAction action : getActions(NewDeviceAction.class)) {
625 // If there is new device action which has the same logical address and path
626 // ignore new request.
627 // NewDeviceAction is created whenever it receives <Report Physical Address>.
628 // And there is a chance starting NewDeviceAction for the same source.
629 // Usually, new device sends <Report Physical Address> when it's plugged
630 // in. However, TV can detect a new device from HotPlugDetectionAction,
631 // which sends <Give Physical Address> to the source for newly detected
632 // device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900633 if (action.isActionOf(activeSource)) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900634 return;
635 }
636 }
637
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900638 addAndStartAction(new NewDeviceAction(this, activeSource.logicalAddress,
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900639 activeSource.physicalAddress, deviceType));
Jungshik Jang97affee2014-07-11 17:04:43 +0900640 }
641
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900642 private boolean handleNewDeviceAtTheTailOfActivePath(int path) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900643 // Seq #22
644 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900645 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim7c3a9562014-08-01 11:07:42 +0900646 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900647 startRoutingControl(getActivePath(), newPath, false, null);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900648 return true;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900649 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900650 return false;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900651 }
652
653 /**
654 * Whether the given path is located in the tail of current active path.
655 *
656 * @param path to be tested
657 * @param activePath current active path
658 * @return true if the given path is located in the tail of current active path; otherwise,
659 * false
660 */
661 static boolean isTailOfActivePath(int path, int activePath) {
662 // If active routing path is internal source, return false.
663 if (activePath == 0) {
664 return false;
665 }
666 for (int i = 12; i >= 0; i -= 4) {
667 int curActivePath = (activePath >> i) & 0xF;
668 if (curActivePath == 0) {
669 return true;
670 } else {
671 int curPath = (path >> i) & 0xF;
672 if (curPath != curActivePath) {
673 return false;
674 }
675 }
676 }
677 return false;
678 }
679
680 @Override
681 @ServiceThreadOnly
682 protected boolean handleRoutingChange(HdmiCecMessage message) {
683 assertRunOnServiceThread();
684 // Seq #21
685 byte[] params = message.getParams();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900686 int currentPath = HdmiUtils.twoBytesToInt(params);
687 if (HdmiUtils.isAffectingActiveRoutingPath(getActivePath(), currentPath)) {
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900688 mActiveSource.invalidate();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900689 removeAction(RoutingControlAction.class);
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900690 int newPath = HdmiUtils.twoBytesToInt(params, 2);
Jinsuk Kim04fd2802014-07-03 14:04:02 +0900691 addAndStartAction(new RoutingControlAction(this, newPath, true, null));
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900692 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900693 return true;
694 }
Jinsuk Kim0a3316b2014-06-14 09:33:55 +0900695
696 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900697 @ServiceThreadOnly
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900698 protected boolean handleReportAudioStatus(HdmiCecMessage message) {
699 assertRunOnServiceThread();
700
701 byte params[] = message.getParams();
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900702 int mute = params[0] & 0x80;
703 int volume = params[0] & 0x7F;
704 setAudioStatus(mute == 0x80, volume);
705 return true;
706 }
707
Yuncheol Heo38db6292014-07-01 14:15:14 +0900708 @Override
709 @ServiceThreadOnly
710 protected boolean handleTextViewOn(HdmiCecMessage message) {
711 assertRunOnServiceThread();
Jinsuk Kimc854c952015-10-12 09:33:33 +0900712
713 // Note that <Text View On> (and <Image View On>) command won't be handled here in
714 // most cases. A dedicated microcontroller should be in charge while Android system
715 // is in sleep mode, and the command need not be passed up to this service.
716 // The only situation where the command reaches this handler is that sleep mode is
717 // implemented in such a way that Android system is not really put to standby mode
718 // but only the display is set to blank. Then the command leads to the effect of
719 // turning on the display by the invocation of PowerManager.wakeUp().
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900720 if (mService.isPowerStandbyOrTransient() && mAutoWakeup) {
Yuncheol Heo38db6292014-07-01 14:15:14 +0900721 mService.wakeUp();
722 }
Yuncheol Heo38db6292014-07-01 14:15:14 +0900723 return true;
724 }
725
726 @Override
727 @ServiceThreadOnly
728 protected boolean handleImageViewOn(HdmiCecMessage message) {
729 assertRunOnServiceThread();
730 // Currently, it's the same as <Text View On>.
731 return handleTextViewOn(message);
732 }
733
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900734 @Override
735 @ServiceThreadOnly
736 protected boolean handleSetOsdName(HdmiCecMessage message) {
737 int source = message.getSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900738 HdmiDeviceInfo deviceInfo = getCecDeviceInfo(source);
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900739 // If the device is not in device list, ignore it.
740 if (deviceInfo == null) {
741 Slog.e(TAG, "No source device info for <Set Osd Name>." + message);
742 return true;
743 }
744 String osdName = null;
745 try {
746 osdName = new String(message.getParams(), "US-ASCII");
747 } catch (UnsupportedEncodingException e) {
748 Slog.e(TAG, "Invalid <Set Osd Name> request:" + message, e);
749 return true;
750 }
751
752 if (deviceInfo.getDisplayName().equals(osdName)) {
753 Slog.i(TAG, "Ignore incoming <Set Osd Name> having same osd name:" + message);
754 return true;
755 }
756
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900757 addCecDevice(new HdmiDeviceInfo(deviceInfo.getLogicalAddress(),
Jinsuk Kim2b152012014-07-25 08:22:26 +0900758 deviceInfo.getPhysicalAddress(), deviceInfo.getPortId(),
759 deviceInfo.getDeviceType(), deviceInfo.getVendorId(), osdName));
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900760 return true;
761 }
762
Jungshik Janga5b74142014-06-23 18:03:10 +0900763 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900764 private void launchDeviceDiscovery() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900765 assertRunOnServiceThread();
766 clearDeviceInfoList();
767 DeviceDiscoveryAction action = new DeviceDiscoveryAction(this,
Jungshik Jang60cffce2014-06-12 18:03:04 +0900768 new DeviceDiscoveryCallback() {
769 @Override
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900770 public void onDeviceDiscoveryDone(List<HdmiDeviceInfo> deviceInfos) {
771 for (HdmiDeviceInfo info : deviceInfos) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900772 addCecDevice(info);
Jungshik Jang60cffce2014-06-12 18:03:04 +0900773 }
774
775 // Since we removed all devices when it's start and
776 // device discovery action does not poll local devices,
777 // we should put device info of local device manually here
778 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900779 addCecDevice(device.getDeviceInfo());
Jungshik Jang60cffce2014-06-12 18:03:04 +0900780 }
781
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900782 mSelectRequestBuffer.process();
783 resetSelectRequestBuffer();
784
Jungshik Jang79c58a42014-06-16 16:45:36 +0900785 addAndStartAction(new HotplugDetectionAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang410ca9c2014-08-07 18:04:14 +0900786 addAndStartAction(new PowerStatusMonitorAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang187d0172014-06-17 17:48:42 +0900787
Jungshik Jang339227d2014-08-25 15:37:20 +0900788 HdmiDeviceInfo avr = getAvrDeviceInfo();
789 if (avr != null) {
790 onNewAvrAdded(avr);
Wally Yau951e3e42015-04-03 15:12:52 -0700791 } else {
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900792 setSystemAudioMode(false);
Jungshik Jang187d0172014-06-17 17:48:42 +0900793 }
Jungshik Jang60cffce2014-06-12 18:03:04 +0900794 }
795 });
Jungshik Jang79c58a42014-06-16 16:45:36 +0900796 addAndStartAction(action);
797 }
798
Jungshik Jang339227d2014-08-25 15:37:20 +0900799 @ServiceThreadOnly
800 void onNewAvrAdded(HdmiDeviceInfo avr) {
801 assertRunOnServiceThread();
Donghyun Choc21f63a2016-05-10 10:37:47 +0900802 addAndStartAction(new SystemAudioAutoInitiationAction(this, avr.getLogicalAddress()));
Donghyun Cho88909ce2016-12-23 14:41:00 +0900803 if (isConnected(avr.getPortId()) && isArcFeatureEnabled(avr.getPortId())
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900804 && !hasAction(SetArcTransmissionStateAction.class)) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900805 startArcAction(true);
806 }
807 }
808
Jungshik Jang79c58a42014-06-16 16:45:36 +0900809 // Clear all device info.
Jungshik Janga5b74142014-06-23 18:03:10 +0900810 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900811 private void clearDeviceInfoList() {
812 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900813 for (HdmiDeviceInfo info : mSafeExternalInputs) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +0900814 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +0900815 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900816 mDeviceInfos.clear();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900817 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900818 }
819
Jungshik Janga5b74142014-06-23 18:03:10 +0900820 @ServiceThreadOnly
Yuncheol Heoc516d652014-07-11 18:23:24 +0900821 // Seq #32
Jungshik Jangea67c182014-06-19 22:19:20 +0900822 void changeSystemAudioMode(boolean enabled, IHdmiControlCallback callback) {
823 assertRunOnServiceThread();
Yuncheol Heoc516d652014-07-11 18:23:24 +0900824 if (!mService.isControlEnabled() || hasAction(DeviceDiscoveryAction.class)) {
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900825 setSystemAudioMode(false);
Yuncheol Heoc516d652014-07-11 18:23:24 +0900826 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
827 return;
828 }
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900829 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jangea67c182014-06-19 22:19:20 +0900830 if (avr == null) {
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900831 setSystemAudioMode(false);
Yuncheol Heod05f67f2014-07-11 16:06:40 +0900832 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
Jungshik Jangea67c182014-06-19 22:19:20 +0900833 return;
834 }
835
836 addAndStartAction(
837 new SystemAudioActionFromTv(this, avr.getLogicalAddress(), enabled, callback));
Jungshik Jangea67c182014-06-19 22:19:20 +0900838 }
839
Jungshik Jangca5be9a2014-07-01 18:01:26 +0900840 // # Seq 25
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900841 void setSystemAudioMode(boolean on) {
842 if (!isSystemAudioControlFeatureEnabled() && on) {
843 HdmiLogger.debug("Cannot turn on system audio mode "
844 + "because the System Audio Control feature is disabled.");
845 return;
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900846 }
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900847 HdmiLogger.debug("System Audio Mode change[old:%b new:%b]", mSystemAudioActivated, on);
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900848 updateAudioManagerForSystemAudio(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900849 synchronized (mLock) {
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900850 if (mSystemAudioActivated != on) {
851 mSystemAudioActivated = on;
Jungshik Jangea67c182014-06-19 22:19:20 +0900852 mService.announceSystemAudioModeChange(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900853 }
Sungsoo Limc9c09232017-08-07 10:40:01 +0900854 startArcAction(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900855 }
856 }
857
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900858 private void updateAudioManagerForSystemAudio(boolean on) {
Jungshik Jang6b096d32014-09-12 18:49:08 +0900859 int device = mService.getAudioManager().setHdmiSystemAudioSupported(on);
860 HdmiLogger.debug("[A]UpdateSystemAudio mode[on=%b] output=[%X]", on, device);
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900861 }
862
863 boolean isSystemAudioActivated() {
Jinsuk Kim86a1e5a2014-11-19 06:04:50 +0900864 if (!hasSystemAudioDevice()) {
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900865 return false;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900866 }
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900867 synchronized (mLock) {
868 return mSystemAudioActivated;
869 }
870 }
871
Donghyun Choc1fa9af2016-12-27 18:31:09 +0900872 @ServiceThreadOnly
873 void setSystemAudioControlFeatureEnabled(boolean enabled) {
874 assertRunOnServiceThread();
875 synchronized (mLock) {
876 mSystemAudioControlFeatureEnabled = enabled;
877 }
878 if (hasSystemAudioDevice()) {
879 changeSystemAudioMode(enabled, null);
880 }
881 }
882
883 boolean isSystemAudioControlFeatureEnabled() {
884 synchronized (mLock) {
885 return mSystemAudioControlFeatureEnabled;
886 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900887 }
888
889 /**
890 * Change ARC status into the given {@code enabled} status.
891 *
892 * @return {@code true} if ARC was in "Enabled" status
893 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900894 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900895 boolean setArcStatus(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900896 assertRunOnServiceThread();
Jungshik Jang6b096d32014-09-12 18:49:08 +0900897
898 HdmiLogger.debug("Set Arc Status[old:%b new:%b]", mArcEstablished, enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900899 boolean oldStatus = mArcEstablished;
900 // 1. Enable/disable ARC circuit.
Donghyun Chobc6e3722016-11-04 05:25:52 +0900901 enableAudioReturnChannel(enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900902 // 2. Notify arc status to audio service.
903 notifyArcStatusToAudioService(enabled);
904 // 3. Update arc status;
905 mArcEstablished = enabled;
906 return oldStatus;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900907 }
908
Jinsuk Kim757c0972015-02-23 10:15:42 +0900909 /**
910 * Switch hardware ARC circuit in the system.
911 */
912 @ServiceThreadOnly
Donghyun Chobc6e3722016-11-04 05:25:52 +0900913 void enableAudioReturnChannel(boolean enabled) {
Jinsuk Kim757c0972015-02-23 10:15:42 +0900914 assertRunOnServiceThread();
915 HdmiDeviceInfo avr = getAvrDeviceInfo();
916 if (avr != null) {
Donghyun Chobc6e3722016-11-04 05:25:52 +0900917 mService.enableAudioReturnChannel(avr.getPortId(), enabled);
Jinsuk Kim757c0972015-02-23 10:15:42 +0900918 }
919 }
920
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +0900921 @ServiceThreadOnly
Jinsuk Kim7b0cf642015-04-14 09:43:45 +0900922 boolean isConnected(int portId) {
923 assertRunOnServiceThread();
924 return mService.isConnected(portId);
925 }
926
Jungshik Janga858d222014-06-23 17:17:47 +0900927 private void notifyArcStatusToAudioService(boolean enabled) {
928 // Note that we don't set any name to ARC.
929 mService.getAudioManager().setWiredDeviceConnectionState(
930 AudioSystem.DEVICE_OUT_HDMI_ARC,
Paul McLean10804eb2015-01-28 11:16:35 -0800931 enabled ? 1 : 0, "", "");
Jungshik Janga858d222014-06-23 17:17:47 +0900932 }
933
Jungshik Jang79c58a42014-06-16 16:45:36 +0900934 /**
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900935 * Returns true if ARC is currently established on a certain port.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900936 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900937 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900938 boolean isArcEstablished() {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900939 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900940 if (mArcEstablished) {
941 for (int i = 0; i < mArcFeatureEnabled.size(); i++) {
942 if (mArcFeatureEnabled.valueAt(i)) return true;
943 }
944 }
945 return false;
Jungshik Janga13da0d2014-06-30 16:26:06 +0900946 }
947
948 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900949 void changeArcFeatureEnabled(int portId, boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900950 assertRunOnServiceThread();
Donghyun Cho88909ce2016-12-23 14:41:00 +0900951 if (mArcFeatureEnabled.get(portId) == enabled) {
952 return;
953 }
954 mArcFeatureEnabled.put(portId, enabled);
955 HdmiDeviceInfo avr = getAvrDeviceInfo();
956 if (avr == null || avr.getPortId() != portId) {
957 return;
958 }
959 if (enabled && !mArcEstablished) {
960 startArcAction(true);
961 } else if (!enabled && mArcEstablished) {
962 startArcAction(false);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900963 }
964 }
965
966 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900967 boolean isArcFeatureEnabled(int portId) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900968 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900969 return mArcFeatureEnabled.get(portId);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900970 }
971
972 @ServiceThreadOnly
Jungshik Jang339227d2014-08-25 15:37:20 +0900973 void startArcAction(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900974 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900975 HdmiDeviceInfo info = getAvrDeviceInfo();
Jungshik Janga13da0d2014-06-30 16:26:06 +0900976 if (info == null) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900977 Slog.w(TAG, "Failed to start arc action; No AVR device.");
Jungshik Janga13da0d2014-06-30 16:26:06 +0900978 return;
979 }
Jungshik Jang339227d2014-08-25 15:37:20 +0900980 if (!canStartArcUpdateAction(info.getLogicalAddress(), enabled)) {
981 Slog.w(TAG, "Failed to start arc action; ARC configuration check failed.");
982 if (enabled && !isConnectedToArcPort(info.getPhysicalAddress())) {
983 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
984 }
Jungshik Janga13da0d2014-06-30 16:26:06 +0900985 return;
986 }
987
988 // Terminate opposite action and start action if not exist.
989 if (enabled) {
990 removeAction(RequestArcTerminationAction.class);
991 if (!hasAction(RequestArcInitiationAction.class)) {
992 addAndStartAction(new RequestArcInitiationAction(this, info.getLogicalAddress()));
993 }
994 } else {
995 removeAction(RequestArcInitiationAction.class);
996 if (!hasAction(RequestArcTerminationAction.class)) {
997 addAndStartAction(new RequestArcTerminationAction(this, info.getLogicalAddress()));
998 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900999 }
1000 }
1001
Jungshik Jang339227d2014-08-25 15:37:20 +09001002 private boolean isDirectConnectAddress(int physicalAddress) {
1003 return (physicalAddress & Constants.ROUTING_PATH_TOP_MASK) == physicalAddress;
1004 }
1005
Jungshik Jang79c58a42014-06-16 16:45:36 +09001006 void setAudioStatus(boolean mute, int volume) {
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001007 synchronized (mLock) {
1008 mSystemAudioMute = mute;
1009 mSystemAudioVolume = volume;
Jungshik Jangb69aafbf2014-07-11 16:29:06 +09001010 int maxVolume = mService.getAudioManager().getStreamMaxVolume(
1011 AudioManager.STREAM_MUSIC);
1012 mService.setAudioStatus(mute,
1013 VolumeControlAction.scaleToCustomVolume(volume, maxVolume));
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001014 displayOsd(HdmiControlManager.OSD_MESSAGE_AVR_VOLUME_CHANGED,
1015 mute ? HdmiControlManager.AVR_VOLUME_MUTED : volume);
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001016 }
1017 }
1018
1019 @ServiceThreadOnly
1020 void changeVolume(int curVolume, int delta, int maxVolume) {
1021 assertRunOnServiceThread();
Jungshik Jang377dcbd2014-07-15 15:49:02 +09001022 if (delta == 0 || !isSystemAudioActivated()) {
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001023 return;
1024 }
1025
1026 int targetVolume = curVolume + delta;
1027 int cecVolume = VolumeControlAction.scaleToCecVolume(targetVolume, maxVolume);
1028 synchronized (mLock) {
1029 // If new volume is the same as current system audio volume, just ignore it.
1030 // Note that UNKNOWN_VOLUME is not in range of cec volume scale.
1031 if (cecVolume == mSystemAudioVolume) {
1032 // Update tv volume with system volume value.
1033 mService.setAudioStatus(false,
1034 VolumeControlAction.scaleToCustomVolume(mSystemAudioVolume, maxVolume));
1035 return;
1036 }
1037 }
1038
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001039 List<VolumeControlAction> actions = getActions(VolumeControlAction.class);
1040 if (actions.isEmpty()) {
1041 addAndStartAction(new VolumeControlAction(this,
1042 getAvrDeviceInfo().getLogicalAddress(), delta > 0));
1043 } else {
1044 actions.get(0).handleVolumeChange(delta > 0);
1045 }
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001046 }
1047
1048 @ServiceThreadOnly
1049 void changeMute(boolean mute) {
1050 assertRunOnServiceThread();
Jungshik Jang6b096d32014-09-12 18:49:08 +09001051 HdmiLogger.debug("[A]:Change mute:%b", mute);
Jungshik Jang720407a2014-09-15 17:48:41 +09001052 synchronized (mLock) {
1053 if (mSystemAudioMute == mute) {
1054 HdmiLogger.debug("No need to change mute.");
1055 return;
1056 }
1057 }
Jungshik Jang377dcbd2014-07-15 15:49:02 +09001058 if (!isSystemAudioActivated()) {
Jungshik Jang6b096d32014-09-12 18:49:08 +09001059 HdmiLogger.debug("[A]:System audio is not activated.");
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001060 return;
1061 }
1062
1063 // Remove existing volume action.
1064 removeAction(VolumeControlAction.class);
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001065 sendUserControlPressedAndReleased(getAvrDeviceInfo().getLogicalAddress(),
Jinsuk Kim0ab37792015-10-17 06:53:52 +09001066 HdmiCecKeycode.getMuteKey(mute));
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001067 }
1068
Jungshik Jang79c58a42014-06-16 16:45:36 +09001069 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001070 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001071 protected boolean handleInitiateArc(HdmiCecMessage message) {
1072 assertRunOnServiceThread();
Jungshik Jang339227d2014-08-25 15:37:20 +09001073
1074 if (!canStartArcUpdateAction(message.getSource(), true)) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001075 if (getAvrDeviceInfo() == null) {
1076 // AVR may not have been discovered yet. Delay the message processing.
1077 mDelayedMessageBuffer.add(message);
1078 return true;
1079 }
Jungshik Jang339227d2014-08-25 15:37:20 +09001080 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
1081 if (!isConnectedToArcPort(message.getSource())) {
1082 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
1083 }
1084 return true;
1085 }
1086
Jungshik Jang79c58a42014-06-16 16:45:36 +09001087 // In case where <Initiate Arc> is started by <Request ARC Initiation>
1088 // need to clean up RequestArcInitiationAction.
1089 removeAction(RequestArcInitiationAction.class);
1090 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1091 message.getSource(), true);
1092 addAndStartAction(action);
1093 return true;
1094 }
1095
Donghyun Cho88909ce2016-12-23 14:41:00 +09001096 private boolean canStartArcUpdateAction(int avrAddress, boolean enabled) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001097 HdmiDeviceInfo avr = getAvrDeviceInfo();
1098 if (avr != null
1099 && (avrAddress == avr.getLogicalAddress())
1100 && isConnectedToArcPort(avr.getPhysicalAddress())
1101 && isDirectConnectAddress(avr.getPhysicalAddress())) {
Donghyun Cho88909ce2016-12-23 14:41:00 +09001102 if (enabled) {
1103 return isConnected(avr.getPortId()) && isArcFeatureEnabled(avr.getPortId());
Jungshik Jang339227d2014-08-25 15:37:20 +09001104 } else {
1105 return true;
1106 }
1107 } else {
1108 return false;
1109 }
1110 }
1111
Jungshik Jang79c58a42014-06-16 16:45:36 +09001112 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001113 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001114 protected boolean handleTerminateArc(HdmiCecMessage message) {
1115 assertRunOnServiceThread();
Jinsuk Kim7e4b4802015-04-20 13:17:13 +09001116 if (mService .isPowerStandbyOrTransient()) {
1117 setArcStatus(false);
1118 return true;
1119 }
1120 // Do not check ARC configuration since the AVR might have been already removed.
1121 // Clean up RequestArcTerminationAction in case <Terminate Arc> was started by
1122 // <Request ARC Termination>.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001123 removeAction(RequestArcTerminationAction.class);
1124 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1125 message.getSource(), false);
1126 addAndStartAction(action);
1127 return true;
1128 }
1129
1130 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001131 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001132 protected boolean handleSetSystemAudioMode(HdmiCecMessage message) {
1133 assertRunOnServiceThread();
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001134 boolean systemAudioStatus = HdmiUtils.parseCommandParamSystemAudioStatus(message);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001135 if (!isMessageForSystemAudio(message)) {
Jinsuk Kimad1e3d72015-01-02 13:27:15 +09001136 if (getAvrDeviceInfo() == null) {
1137 // AVR may not have been discovered yet. Delay the message processing.
1138 mDelayedMessageBuffer.add(message);
Donghyun Choc21f63a2016-05-10 10:37:47 +09001139 } else {
1140 HdmiLogger.warning("Invalid <Set System Audio Mode> message:" + message);
1141 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
Jinsuk Kimad1e3d72015-01-02 13:27:15 +09001142 }
Jungshik Jang4480efa2014-09-04 17:08:34 +09001143 return true;
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001144 } else if (systemAudioStatus && !isSystemAudioControlFeatureEnabled()) {
1145 HdmiLogger.debug("Ignoring <Set System Audio Mode> message "
1146 + "because the System Audio Control feature is disabled: %s", message);
1147 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
1148 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001149 }
Donghyun Choc21f63a2016-05-10 10:37:47 +09001150 removeAction(SystemAudioAutoInitiationAction.class);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001151 SystemAudioActionFromAvr action = new SystemAudioActionFromAvr(this,
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001152 message.getSource(), systemAudioStatus, null);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001153 addAndStartAction(action);
1154 return true;
1155 }
1156
1157 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001158 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001159 protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) {
1160 assertRunOnServiceThread();
1161 if (!isMessageForSystemAudio(message)) {
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001162 HdmiLogger.warning("Invalid <System Audio Mode Status> message:" + message);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001163 // Ignore this message.
1164 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001165 }
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001166 setSystemAudioMode(HdmiUtils.parseCommandParamSystemAudioStatus(message));
Jungshik Jang79c58a42014-06-16 16:45:36 +09001167 return true;
1168 }
1169
Jungshik Jangb6591b82014-07-23 16:10:23 +09001170 // Seq #53
1171 @Override
1172 @ServiceThreadOnly
1173 protected boolean handleRecordTvScreen(HdmiCecMessage message) {
1174 List<OneTouchRecordAction> actions = getActions(OneTouchRecordAction.class);
1175 if (!actions.isEmpty()) {
1176 // Assumes only one OneTouchRecordAction.
1177 OneTouchRecordAction action = actions.get(0);
1178 if (action.getRecorderAddress() != message.getSource()) {
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001179 announceOneTouchRecordResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001180 message.getSource(),
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001181 HdmiControlManager.ONE_TOUCH_RECORD_PREVIOUS_RECORDING_IN_PROGRESS);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001182 }
1183 return super.handleRecordTvScreen(message);
1184 }
1185
1186 int recorderAddress = message.getSource();
1187 byte[] recordSource = mService.invokeRecordRequestListener(recorderAddress);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001188 int reason = startOneTouchRecord(recorderAddress, recordSource);
1189 if (reason != Constants.ABORT_NO_ERROR) {
1190 mService.maySendFeatureAbortCommand(message, reason);
1191 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001192 return true;
1193 }
1194
Jungshik Jange5a93372014-07-25 13:41:14 +09001195 @Override
1196 protected boolean handleTimerClearedStatus(HdmiCecMessage message) {
1197 byte[] params = message.getParams();
Jungshik Jange9e0f072014-08-05 17:48:59 +09001198 int timerClearedStatusData = params[0] & 0xFF;
Jungshik Jang326aef02014-11-05 12:50:35 +09001199 announceTimerRecordingResult(message.getSource(), timerClearedStatusData);
Jungshik Jange5a93372014-07-25 13:41:14 +09001200 return true;
1201 }
1202
Jungshik Jang326aef02014-11-05 12:50:35 +09001203 void announceOneTouchRecordResult(int recorderAddress, int result) {
1204 mService.invokeOneTouchRecordResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001205 }
1206
Jungshik Jang326aef02014-11-05 12:50:35 +09001207 void announceTimerRecordingResult(int recorderAddress, int result) {
1208 mService.invokeTimerRecordingResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001209 }
1210
Jungshik Jang326aef02014-11-05 12:50:35 +09001211 void announceClearTimerRecordingResult(int recorderAddress, int result) {
1212 mService.invokeClearTimerRecordingResult(recorderAddress, result);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001213 }
1214
Jungshik Jang79c58a42014-06-16 16:45:36 +09001215 private boolean isMessageForSystemAudio(HdmiCecMessage message) {
Jungshik Jang4480efa2014-09-04 17:08:34 +09001216 return mService.isControlEnabled()
1217 && message.getSource() == Constants.ADDR_AUDIO_SYSTEM
Jungshik Jang473119f2014-08-27 16:43:22 +09001218 && (message.getDestination() == Constants.ADDR_TV
1219 || message.getDestination() == Constants.ADDR_BROADCAST)
1220 && getAvrDeviceInfo() != null;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001221 }
1222
1223 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001224 * Add a new {@link HdmiDeviceInfo}. It returns old device info which has the same
Jungshik Jang79c58a42014-06-16 16:45:36 +09001225 * logical address as new device info's.
1226 *
1227 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1228 *
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001229 * @param deviceInfo a new {@link HdmiDeviceInfo} to be added.
1230 * @return {@code null} if it is new device. Otherwise, returns old {@HdmiDeviceInfo}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001231 * that has the same logical address as new one has.
1232 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001233 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001234 private HdmiDeviceInfo addDeviceInfo(HdmiDeviceInfo deviceInfo) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001235 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001236 HdmiDeviceInfo oldDeviceInfo = getCecDeviceInfo(deviceInfo.getLogicalAddress());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001237 if (oldDeviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001238 removeDeviceInfo(deviceInfo.getId());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001239 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001240 mDeviceInfos.append(deviceInfo.getId(), deviceInfo);
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001241 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001242 return oldDeviceInfo;
1243 }
1244
1245 /**
1246 * Remove a device info corresponding to the given {@code logicalAddress}.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001247 * It returns removed {@link HdmiDeviceInfo} if exists.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001248 *
1249 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1250 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001251 * @param id id of device to be removed
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001252 * @return removed {@link HdmiDeviceInfo} it exists. Otherwise, returns {@code null}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001253 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001254 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001255 private HdmiDeviceInfo removeDeviceInfo(int id) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001256 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001257 HdmiDeviceInfo deviceInfo = mDeviceInfos.get(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001258 if (deviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001259 mDeviceInfos.remove(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001260 }
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001261 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001262 return deviceInfo;
1263 }
1264
1265 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001266 * Return a list of all {@link HdmiDeviceInfo}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001267 *
1268 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
Jungshik Jang339227d2014-08-25 15:37:20 +09001269 * This is not thread-safe. For thread safety, call {@link #getSafeExternalInputsLocked} which
Jungshik Jang8e93c842014-08-06 15:48:33 +09001270 * does not include local device.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001271 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001272 @ServiceThreadOnly
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001273 List<HdmiDeviceInfo> getDeviceInfoList(boolean includeLocalDevice) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001274 assertRunOnServiceThread();
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001275 if (includeLocalDevice) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001276 return HdmiUtils.sparseArrayToList(mDeviceInfos);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001277 } else {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001278 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001279 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001280 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001281 if (!isLocalDeviceAddress(info.getLogicalAddress())) {
1282 infoList.add(info);
1283 }
1284 }
1285 return infoList;
1286 }
1287 }
1288
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001289 /**
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001290 * Return external input devices.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001291 */
Jinsuk Kimed086452014-08-18 15:01:53 +09001292 List<HdmiDeviceInfo> getSafeExternalInputsLocked() {
1293 return mSafeExternalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001294 }
1295
Jungshik Janga5b74142014-06-23 18:03:10 +09001296 @ServiceThreadOnly
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001297 private void updateSafeDeviceInfoList() {
1298 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001299 List<HdmiDeviceInfo> copiedDevices = HdmiUtils.sparseArrayToList(mDeviceInfos);
1300 List<HdmiDeviceInfo> externalInputs = getInputDevices();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001301 synchronized (mLock) {
1302 mSafeAllDeviceInfos = copiedDevices;
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001303 mSafeExternalInputs = externalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001304 }
1305 }
1306
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001307 /**
1308 * Return a list of external cec input (source) devices.
1309 *
1310 * <p>Note that this effectively excludes non-source devices like system audio,
1311 * secondary TV.
1312 */
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001313 private List<HdmiDeviceInfo> getInputDevices() {
1314 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001315 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001316 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001317 if (isLocalDeviceAddress(info.getLogicalAddress())) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001318 continue;
1319 }
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001320 if (info.isSourceType() && !hideDevicesBehindLegacySwitch(info)) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001321 infoList.add(info);
1322 }
1323 }
1324 return infoList;
1325 }
1326
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001327 // Check if we are hiding CEC devices connected to a legacy (non-CEC) switch.
1328 // Returns true if the policy is set to true, and the device to check does not have
1329 // a parent CEC device (which should be the CEC-enabled switch) in the list.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001330 private boolean hideDevicesBehindLegacySwitch(HdmiDeviceInfo info) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001331 return HdmiConfig.HIDE_DEVICES_BEHIND_LEGACY_SWITCH
1332 && !isConnectedToCecSwitch(info.getPhysicalAddress(), mCecSwitches);
1333 }
1334
1335 private static boolean isConnectedToCecSwitch(int path, Collection<Integer> switches) {
1336 for (int switchPath : switches) {
1337 if (isParentPath(switchPath, path)) {
1338 return true;
1339 }
1340 }
1341 return false;
1342 }
1343
1344 private static boolean isParentPath(int parentPath, int childPath) {
1345 // (A000, AB00) (AB00, ABC0), (ABC0, ABCD)
1346 // If child's last non-zero nibble is removed, the result equals to the parent.
1347 for (int i = 0; i <= 12; i += 4) {
1348 int nibble = (childPath >> i) & 0xF;
1349 if (nibble != 0) {
1350 int parentNibble = (parentPath >> i) & 0xF;
1351 return parentNibble == 0 && (childPath >> i+4) == (parentPath >> i+4);
1352 }
1353 }
1354 return false;
1355 }
1356
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001357 private void invokeDeviceEventListener(HdmiDeviceInfo info, int status) {
Jinsuk Kim98d760e2014-12-23 07:01:51 +09001358 if (!hideDevicesBehindLegacySwitch(info)) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001359 mService.invokeDeviceEventListeners(info, status);
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001360 }
1361 }
1362
Jungshik Jang79c58a42014-06-16 16:45:36 +09001363 private boolean isLocalDeviceAddress(int address) {
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001364 return mLocalDeviceAddresses.contains(address);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001365 }
1366
Jungshik Jange9cf1582014-06-23 17:28:58 +09001367 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001368 HdmiDeviceInfo getAvrDeviceInfo() {
Jungshik Jange9cf1582014-06-23 17:28:58 +09001369 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001370 return getCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jange9cf1582014-06-23 17:28:58 +09001371 }
1372
Jungshik Jang79c58a42014-06-16 16:45:36 +09001373 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001374 * Return a {@link HdmiDeviceInfo} corresponding to the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001375 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001376 * This is not thread-safe. For thread safety, call {@link #getSafeCecDeviceInfo(int)}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001377 *
Jungshik Jang339227d2014-08-25 15:37:20 +09001378 * @param logicalAddress logical address of the device to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001379 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001380 * Returns null if no logical address matched
1381 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001382 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001383 HdmiDeviceInfo getCecDeviceInfo(int logicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001384 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001385 return mDeviceInfos.get(HdmiDeviceInfo.idForCecDevice(logicalAddress));
Jungshik Jang79c58a42014-06-16 16:45:36 +09001386 }
1387
Jungshik Jange9cf1582014-06-23 17:28:58 +09001388 boolean hasSystemAudioDevice() {
1389 return getSafeAvrDeviceInfo() != null;
1390 }
1391
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001392 HdmiDeviceInfo getSafeAvrDeviceInfo() {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001393 return getSafeCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001394 }
1395
1396 /**
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001397 * Thread safe version of {@link #getCecDeviceInfo(int)}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001398 *
1399 * @param logicalAddress logical address to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001400 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001401 * Returns null if no logical address matched
1402 */
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001403 HdmiDeviceInfo getSafeCecDeviceInfo(int logicalAddress) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001404 synchronized (mLock) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001405 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1406 if (info.isCecDevice() && info.getLogicalAddress() == logicalAddress) {
1407 return info;
1408 }
1409 }
1410 return null;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001411 }
1412 }
1413
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001414 List<HdmiDeviceInfo> getSafeCecDevicesLocked() {
1415 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
1416 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1417 if (isLocalDeviceAddress(info.getLogicalAddress())) {
1418 continue;
1419 }
1420 infoList.add(info);
1421 }
1422 return infoList;
1423 }
1424
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001425 /**
Jungshik Jang8f2ed352014-07-07 15:02:47 +09001426 * Called when a device is newly added or a new device is detected or
1427 * existing device is updated.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001428 *
1429 * @param info device info of a new device.
1430 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001431 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001432 final void addCecDevice(HdmiDeviceInfo info) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001433 assertRunOnServiceThread();
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001434 HdmiDeviceInfo old = addDeviceInfo(info);
Jinsuk Kim13c030e2014-06-20 13:25:17 +09001435 if (info.getLogicalAddress() == mAddress) {
1436 // The addition of TV device itself should not be notified.
1437 return;
1438 }
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001439 if (old == null) {
1440 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
1441 } else if (!old.equals(info)) {
Jinsuk Kim4fcbf0b2014-12-09 06:48:13 +09001442 invokeDeviceEventListener(old, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
1443 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001444 }
Jungshik Jang79c58a42014-06-16 16:45:36 +09001445 }
1446
1447 /**
1448 * Called when a device is removed or removal of device is detected.
1449 *
1450 * @param address a logical address of a device to be removed
1451 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001452 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001453 final void removeCecDevice(int address) {
1454 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001455 HdmiDeviceInfo info = removeDeviceInfo(HdmiDeviceInfo.idForCecDevice(address));
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001456
Jungshik Jang79c58a42014-06-16 16:45:36 +09001457 mCecMessageCache.flushMessagesFrom(address);
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001458 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001459 }
1460
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001461 @ServiceThreadOnly
1462 void handleRemoveActiveRoutingPath(int path) {
1463 assertRunOnServiceThread();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001464 // Seq #23
1465 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001466 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim546d8672014-11-24 07:30:54 +09001467 startRoutingControl(getActivePath(), newPath, true, null);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001468 }
1469 }
1470
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001471 /**
1472 * Launch routing control process.
1473 *
1474 * @param routingForBootup true if routing control is initiated due to One Touch Play
1475 * or TV power on
1476 */
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001477 @ServiceThreadOnly
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001478 void launchRoutingControl(boolean routingForBootup) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001479 assertRunOnServiceThread();
1480 // Seq #24
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001481 if (getActivePortId() != Constants.INVALID_PORT_ID) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001482 if (!routingForBootup && !isProhibitMode()) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001483 int newPath = mService.portIdToPath(getActivePortId());
1484 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +09001485 startRoutingControl(getActivePath(), newPath, routingForBootup, null);
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001486 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001487 } else {
1488 int activePath = mService.getPhysicalAddress();
1489 setActivePath(activePath);
Jinsuk Kim2ab6d9f2015-01-16 15:49:16 +09001490 if (!routingForBootup
1491 && !mDelayedMessageBuffer.isBuffered(Constants.MESSAGE_ACTIVE_SOURCE)) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001492 mService.sendCecCommand(HdmiCecMessageBuilder.buildActiveSource(mAddress,
1493 activePath));
1494 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001495 }
1496 }
1497
Jungshik Jang79c58a42014-06-16 16:45:36 +09001498 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001499 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
Jungshik Jang79c58a42014-06-16 16:45:36 +09001500 * the given routing path. CEC devices use routing path for its physical address to
1501 * describe the hierarchy of the devices in the network.
1502 *
1503 * @param path routing path or physical address
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001504 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
Jungshik Jang79c58a42014-06-16 16:45:36 +09001505 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001506 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001507 final HdmiDeviceInfo getDeviceInfoByPath(int path) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001508 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001509 for (HdmiDeviceInfo info : getDeviceInfoList(false)) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001510 if (info.getPhysicalAddress() == path) {
1511 return info;
1512 }
1513 }
1514 return null;
1515 }
1516
1517 /**
Jinsuk Kim7640d982015-01-28 16:44:07 +09001518 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
1519 * the given routing path. This is the version accessible safely from threads
1520 * other than service thread.
1521 *
1522 * @param path routing path or physical address
1523 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
1524 */
1525 HdmiDeviceInfo getSafeDeviceInfoByPath(int path) {
1526 synchronized (mLock) {
1527 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1528 if (info.getPhysicalAddress() == path) {
1529 return info;
1530 }
1531 }
1532 return null;
1533 }
1534 }
1535
1536 /**
Jungshik Jang79c58a42014-06-16 16:45:36 +09001537 * Whether a device of the specified physical address and logical address exists
1538 * in a device info list. However, both are minimal condition and it could
1539 * be different device from the original one.
1540 *
Jungshik Jang79c58a42014-06-16 16:45:36 +09001541 * @param logicalAddress logical address of a device to be searched
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001542 * @param physicalAddress physical address of a device to be searched
Jungshik Jang79c58a42014-06-16 16:45:36 +09001543 * @return true if exist; otherwise false
1544 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001545 @ServiceThreadOnly
Jinsuk Kim7cd4a582015-01-29 09:50:25 +09001546 boolean isInDeviceList(int logicalAddress, int physicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001547 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001548 HdmiDeviceInfo device = getCecDeviceInfo(logicalAddress);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001549 if (device == null) {
1550 return false;
1551 }
1552 return device.getPhysicalAddress() == physicalAddress;
1553 }
1554
1555 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001556 @ServiceThreadOnly
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001557 void onHotplug(int portId, boolean connected) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001558 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001559
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001560 if (!connected) {
1561 removeCecSwitches(portId);
1562 }
Jungshik Jang79c58a42014-06-16 16:45:36 +09001563 // Tv device will have permanent HotplugDetectionAction.
1564 List<HotplugDetectionAction> hotplugActions = getActions(HotplugDetectionAction.class);
1565 if (!hotplugActions.isEmpty()) {
1566 // Note that hotplug action is single action running on a machine.
1567 // "pollAllDevicesNow" cleans up timer and start poll action immediately.
Jungshik Jang24c23c12014-07-07 18:04:39 +09001568 // It covers seq #40, #43.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001569 hotplugActions.get(0).pollAllDevicesNow();
1570 }
Jungshik Jang60cffce2014-06-12 18:03:04 +09001571 }
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001572
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001573 private void removeCecSwitches(int portId) {
1574 Iterator<Integer> it = mCecSwitches.iterator();
1575 while (!it.hasNext()) {
1576 int path = it.next();
1577 if (pathToPortId(path) == portId) {
1578 it.remove();
1579 }
1580 }
1581 }
1582
Jinsuk Kime6e8f3d2015-05-11 14:17:04 +09001583 @Override
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001584 @ServiceThreadOnly
1585 void setAutoDeviceOff(boolean enabled) {
1586 assertRunOnServiceThread();
1587 mAutoDeviceOff = enabled;
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001588 }
1589
1590 @ServiceThreadOnly
1591 void setAutoWakeup(boolean enabled) {
1592 assertRunOnServiceThread();
1593 mAutoWakeup = enabled;
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001594 }
Yuncheol Heo38db6292014-07-01 14:15:14 +09001595
Yuncheol Heo25c20292014-07-31 17:59:39 +09001596 @ServiceThreadOnly
1597 boolean getAutoWakeup() {
1598 assertRunOnServiceThread();
1599 return mAutoWakeup;
1600 }
1601
Yuncheol Heo38db6292014-07-01 14:15:14 +09001602 @Override
1603 @ServiceThreadOnly
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001604 protected void disableDevice(boolean initiatedByCec, PendingActionClearedCallback callback) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001605 assertRunOnServiceThread();
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001606 mService.unregisterTvInputCallback(mTvInputCallback);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001607 // Remove any repeated working actions.
1608 // HotplugDetectionAction will be reinstated during the wake up process.
1609 // HdmiControlService.onWakeUp() -> initializeLocalDevices() ->
1610 // LocalDeviceTv.onAddressAllocated() -> launchDeviceDiscovery().
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001611 removeAction(DeviceDiscoveryAction.class);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001612 removeAction(HotplugDetectionAction.class);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001613 removeAction(PowerStatusMonitorAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001614 // Remove recording actions.
Jungshik Jangb6591b82014-07-23 16:10:23 +09001615 removeAction(OneTouchRecordAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001616 removeAction(TimerRecordingAction.class);
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001617
1618 disableSystemAudioIfExist();
1619 disableArcIfExist();
Jinsuk Kima5445ce2015-03-30 17:14:58 +09001620
1621 super.disableDevice(initiatedByCec, callback);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +09001622 clearDeviceInfoList();
Jinsuk Kim7cc51c62015-04-30 10:39:35 +09001623 getActiveSource().invalidate();
1624 setActivePath(Constants.INVALID_PHYSICAL_ADDRESS);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001625 checkIfPendingActionsCleared();
1626 }
1627
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001628 @ServiceThreadOnly
1629 private void disableSystemAudioIfExist() {
1630 assertRunOnServiceThread();
1631 if (getAvrDeviceInfo() == null) {
1632 return;
1633 }
1634
1635 // Seq #31.
1636 removeAction(SystemAudioActionFromAvr.class);
1637 removeAction(SystemAudioActionFromTv.class);
1638 removeAction(SystemAudioAutoInitiationAction.class);
1639 removeAction(SystemAudioStatusAction.class);
1640 removeAction(VolumeControlAction.class);
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001641 }
1642
1643 @ServiceThreadOnly
1644 private void disableArcIfExist() {
1645 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001646 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001647 if (avr == null) {
1648 return;
1649 }
1650
1651 // Seq #44.
1652 removeAction(RequestArcInitiationAction.class);
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +09001653 if (!hasAction(RequestArcTerminationAction.class) && isArcEstablished()) {
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001654 addAndStartAction(new RequestArcTerminationAction(this, avr.getLogicalAddress()));
1655 }
1656 }
1657
Yuncheol Heo38db6292014-07-01 14:15:14 +09001658 @Override
1659 @ServiceThreadOnly
Jinsuk Kime6e8f3d2015-05-11 14:17:04 +09001660 protected void onStandby(boolean initiatedByCec, int standbyAction) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001661 assertRunOnServiceThread();
1662 // Seq #11
1663 if (!mService.isControlEnabled()) {
1664 return;
1665 }
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001666 if (!initiatedByCec && mAutoDeviceOff) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001667 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001668 mAddress, Constants.ADDR_BROADCAST));
Yuncheol Heo38db6292014-07-01 14:15:14 +09001669 }
1670 }
1671
Jinsuk Kim4d43d932014-07-03 16:43:58 +09001672 boolean isProhibitMode() {
1673 return mService.isProhibitMode();
1674 }
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001675
1676 boolean isPowerStandbyOrTransient() {
1677 return mService.isPowerStandbyOrTransient();
Jungshik Jang8866c812014-07-08 14:42:28 +09001678 }
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001679
Jungshik Jang339227d2014-08-25 15:37:20 +09001680 @ServiceThreadOnly
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001681 void displayOsd(int messageId) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001682 assertRunOnServiceThread();
1683 mService.displayOsd(messageId);
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001684 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001685
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001686 @ServiceThreadOnly
1687 void displayOsd(int messageId, int extra) {
1688 assertRunOnServiceThread();
1689 mService.displayOsd(messageId, extra);
1690 }
1691
Jungshik Jangb6591b82014-07-23 16:10:23 +09001692 // Seq #54 and #55
1693 @ServiceThreadOnly
Jungshik Jang4480efa2014-09-04 17:08:34 +09001694 int startOneTouchRecord(int recorderAddress, byte[] recordSource) {
Jungshik Jangb6591b82014-07-23 16:10:23 +09001695 assertRunOnServiceThread();
1696 if (!mService.isControlEnabled()) {
1697 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001698 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001699 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001700 }
1701
1702 if (!checkRecorder(recorderAddress)) {
1703 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001704 announceOneTouchRecordResult(recorderAddress,
1705 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001706 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001707 }
1708
1709 if (!checkRecordSource(recordSource)) {
1710 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001711 announceOneTouchRecordResult(recorderAddress,
1712 ONE_TOUCH_RECORD_FAIL_TO_RECORD_DISPLAYED_SCREEN);
Jinsuk Kimd47abef2015-01-17 07:38:24 +09001713 return Constants.ABORT_CANNOT_PROVIDE_SOURCE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001714 }
1715
1716 addAndStartAction(new OneTouchRecordAction(this, recorderAddress, recordSource));
1717 Slog.i(TAG, "Start new [One Touch Record]-Target:" + recorderAddress + ", recordSource:"
1718 + Arrays.toString(recordSource));
Jungshik Jang4480efa2014-09-04 17:08:34 +09001719 return Constants.ABORT_NO_ERROR;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001720 }
1721
1722 @ServiceThreadOnly
1723 void stopOneTouchRecord(int recorderAddress) {
1724 assertRunOnServiceThread();
1725 if (!mService.isControlEnabled()) {
1726 Slog.w(TAG, "Can not stop one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001727 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001728 return;
1729 }
1730
1731 if (!checkRecorder(recorderAddress)) {
1732 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001733 announceOneTouchRecordResult(recorderAddress,
1734 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001735 return;
1736 }
1737
1738 // Remove one touch record action so that other one touch record can be started.
1739 removeAction(OneTouchRecordAction.class);
1740 mService.sendCecCommand(HdmiCecMessageBuilder.buildRecordOff(mAddress, recorderAddress));
1741 Slog.i(TAG, "Stop [One Touch Record]-Target:" + recorderAddress);
1742 }
1743
1744 private boolean checkRecorder(int recorderAddress) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001745 HdmiDeviceInfo device = getCecDeviceInfo(recorderAddress);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001746 return (device != null)
1747 && (HdmiUtils.getTypeFromAddress(recorderAddress)
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001748 == HdmiDeviceInfo.DEVICE_RECORDER);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001749 }
1750
1751 private boolean checkRecordSource(byte[] recordSource) {
1752 return (recordSource != null) && HdmiRecordSources.checkRecordSource(recordSource);
1753 }
1754
1755 @ServiceThreadOnly
1756 void startTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1757 assertRunOnServiceThread();
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001758 if (!mService.isControlEnabled()) {
1759 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001760 announceTimerRecordingResult(recorderAddress,
1761 TIMER_RECORDING_RESULT_EXTRA_CEC_DISABLED);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001762 return;
1763 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001764
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001765 if (!checkRecorder(recorderAddress)) {
1766 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001767 announceTimerRecordingResult(recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001768 TIMER_RECORDING_RESULT_EXTRA_CHECK_RECORDER_CONNECTION);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001769 return;
1770 }
1771
1772 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1773 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
1774 announceTimerRecordingResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001775 recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001776 TIMER_RECORDING_RESULT_EXTRA_FAIL_TO_RECORD_SELECTED_SOURCE);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001777 return;
1778 }
1779
1780 addAndStartAction(
1781 new TimerRecordingAction(this, recorderAddress, sourceType, recordSource));
1782 Slog.i(TAG, "Start [Timer Recording]-Target:" + recorderAddress + ", SourceType:"
1783 + sourceType + ", RecordSource:" + Arrays.toString(recordSource));
1784 }
1785
1786 private boolean checkTimerRecordingSource(int sourceType, byte[] recordSource) {
1787 return (recordSource != null)
1788 && HdmiTimerRecordSources.checkTimerRecordSource(sourceType, recordSource);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001789 }
1790
1791 @ServiceThreadOnly
1792 void clearTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1793 assertRunOnServiceThread();
Jungshik Jange5a93372014-07-25 13:41:14 +09001794 if (!mService.isControlEnabled()) {
1795 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001796 announceClearTimerRecordingResult(recorderAddress, CLEAR_TIMER_STATUS_CEC_DISABLE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001797 return;
1798 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001799
Jungshik Jange5a93372014-07-25 13:41:14 +09001800 if (!checkRecorder(recorderAddress)) {
1801 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001802 announceClearTimerRecordingResult(recorderAddress,
1803 CLEAR_TIMER_STATUS_CHECK_RECORDER_CONNECTION);
Jungshik Jange5a93372014-07-25 13:41:14 +09001804 return;
1805 }
1806
1807 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1808 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001809 announceClearTimerRecordingResult(recorderAddress,
1810 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001811 return;
1812 }
1813
1814 sendClearTimerMessage(recorderAddress, sourceType, recordSource);
1815 }
1816
Jungshik Jang326aef02014-11-05 12:50:35 +09001817 private void sendClearTimerMessage(final int recorderAddress, int sourceType,
1818 byte[] recordSource) {
Jungshik Jange5a93372014-07-25 13:41:14 +09001819 HdmiCecMessage message = null;
1820 switch (sourceType) {
1821 case TIMER_RECORDING_TYPE_DIGITAL:
1822 message = HdmiCecMessageBuilder.buildClearDigitalTimer(mAddress, recorderAddress,
1823 recordSource);
1824 break;
1825 case TIMER_RECORDING_TYPE_ANALOGUE:
1826 message = HdmiCecMessageBuilder.buildClearAnalogueTimer(mAddress, recorderAddress,
1827 recordSource);
1828 break;
1829 case TIMER_RECORDING_TYPE_EXTERNAL:
1830 message = HdmiCecMessageBuilder.buildClearExternalTimer(mAddress, recorderAddress,
1831 recordSource);
1832 break;
1833 default:
1834 Slog.w(TAG, "Invalid source type:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001835 announceClearTimerRecordingResult(recorderAddress,
1836 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001837 return;
1838
1839 }
1840 mService.sendCecCommand(message, new SendMessageCallback() {
1841 @Override
1842 public void onSendCompleted(int error) {
Donghyun Chobc6e3722016-11-04 05:25:52 +09001843 if (error != SendMessageResult.SUCCESS) {
Jungshik Jang326aef02014-11-05 12:50:35 +09001844 announceClearTimerRecordingResult(recorderAddress,
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001845 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001846 }
1847 }
1848 });
Jungshik Jangb6591b82014-07-23 16:10:23 +09001849 }
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001850
1851 void updateDevicePowerStatus(int logicalAddress, int newPowerStatus) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001852 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001853 if (info == null) {
1854 Slog.w(TAG, "Can not update power status of non-existing device:" + logicalAddress);
1855 return;
1856 }
1857
1858 if (info.getDevicePowerStatus() == newPowerStatus) {
1859 return;
1860 }
1861
1862 HdmiDeviceInfo newInfo = HdmiUtils.cloneHdmiDeviceInfo(info, newPowerStatus);
1863 // addDeviceInfo replaces old device info with new one if exists.
1864 addDeviceInfo(newInfo);
1865
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001866 invokeDeviceEventListener(newInfo, HdmiControlManager.DEVICE_EVENT_UPDATE_DEVICE);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001867 }
Terry Heo959d2db2014-08-28 16:45:41 +09001868
1869 @Override
Yuncheol Heo184b1242014-09-12 15:09:07 +09001870 protected boolean handleMenuStatus(HdmiCecMessage message) {
1871 // Do nothing and just return true not to prevent from responding <Feature Abort>.
1872 return true;
1873 }
1874
1875 @Override
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001876 protected void sendStandby(int deviceId) {
1877 HdmiDeviceInfo targetDevice = mDeviceInfos.get(deviceId);
1878 if (targetDevice == null) {
1879 return;
1880 }
1881 int targetAddress = targetDevice.getLogicalAddress();
1882 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(mAddress, targetAddress));
1883 }
1884
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001885 @ServiceThreadOnly
1886 void processAllDelayedMessages() {
1887 assertRunOnServiceThread();
1888 mDelayedMessageBuffer.processAllMessages();
1889 }
1890
1891 @ServiceThreadOnly
1892 void processDelayedMessages(int address) {
1893 assertRunOnServiceThread();
1894 mDelayedMessageBuffer.processMessagesForDevice(address);
1895 }
1896
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +09001897 @ServiceThreadOnly
1898 void processDelayedActiveSource(int address) {
1899 assertRunOnServiceThread();
1900 mDelayedMessageBuffer.processActiveSource(address);
1901 }
1902
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001903 @Override
Terry Heo959d2db2014-08-28 16:45:41 +09001904 protected void dump(final IndentingPrintWriter pw) {
1905 super.dump(pw);
1906 pw.println("mArcEstablished: " + mArcEstablished);
1907 pw.println("mArcFeatureEnabled: " + mArcFeatureEnabled);
1908 pw.println("mSystemAudioActivated: " + mSystemAudioActivated);
1909 pw.println("mSystemAudioMute: " + mSystemAudioMute);
Donghyun Choc1fa9af2016-12-27 18:31:09 +09001910 pw.println("mSystemAudioControlFeatureEnabled: " + mSystemAudioControlFeatureEnabled);
Terry Heo959d2db2014-08-28 16:45:41 +09001911 pw.println("mAutoDeviceOff: " + mAutoDeviceOff);
1912 pw.println("mAutoWakeup: " + mAutoWakeup);
1913 pw.println("mSkipRoutingControl: " + mSkipRoutingControl);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +09001914 pw.println("mPrevPortId: " + mPrevPortId);
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001915 pw.println("CEC devices:");
1916 pw.increaseIndent();
1917 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1918 pw.println(info);
1919 }
1920 pw.decreaseIndent();
Terry Heo959d2db2014-08-28 16:45:41 +09001921 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +09001922}