blob: 43d8bac73a50414201922d1ae8471e6020475526 [file] [log] [blame]
Jinsuk Kim2918e9e2014-05-20 16:45:45 +09001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.hdmi;
18
Jungshik Jange5a93372014-07-25 13:41:14 +090019import static android.hardware.hdmi.HdmiControlManager.CLEAR_TIMER_STATUS_CEC_DISABLE;
20import static android.hardware.hdmi.HdmiControlManager.CLEAR_TIMER_STATUS_CHECK_RECORDER_CONNECTION;
21import static android.hardware.hdmi.HdmiControlManager.CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE;
Jungshik Jang12e5dce2014-07-24 15:27:44 +090022import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_CEC_DISABLED;
23import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION;
24import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_FAIL_TO_RECORD_DISPLAYED_SCREEN;
Jungshik Jang339227d2014-08-25 15:37:20 +090025import static android.hardware.hdmi.HdmiControlManager.OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT;
Jungshik Jange5a93372014-07-25 13:41:14 +090026import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_RESULT_EXTRA_CEC_DISABLED;
27import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_RESULT_EXTRA_CHECK_RECORDER_CONNECTION;
28import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_RESULT_EXTRA_FAIL_TO_RECORD_SELECTED_SOURCE;
29import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_TYPE_ANALOGUE;
30import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_TYPE_DIGITAL;
31import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_TYPE_EXTERNAL;
Jungshik Jang12e5dce2014-07-24 15:27:44 +090032
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090033import android.hardware.hdmi.HdmiControlManager;
Jinsuk Kim50084862014-08-07 13:11:40 +090034import android.hardware.hdmi.HdmiDeviceInfo;
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +090035import android.hardware.hdmi.HdmiPortInfo;
Jungshik Jangb6591b82014-07-23 16:10:23 +090036import android.hardware.hdmi.HdmiRecordSources;
Jungshik Jang12e5dce2014-07-24 15:27:44 +090037import android.hardware.hdmi.HdmiTimerRecordSources;
Jungshik Jang60cffce2014-06-12 18:03:04 +090038import android.hardware.hdmi.IHdmiControlCallback;
Jungshik Jangb69aafbf2014-07-11 16:29:06 +090039import android.media.AudioManager;
Jungshik Janga858d222014-06-23 17:17:47 +090040import android.media.AudioSystem;
Jinsuk Kim7fa3a662014-11-07 15:20:24 +090041import android.media.tv.TvInputInfo;
Jinsuk Kim7fa3a662014-11-07 15:20:24 +090042import android.media.tv.TvInputManager.TvInputCallback;
Jinsuk Kima6ce7702014-05-11 06:54:49 +090043import android.os.RemoteException;
Jinsuk Kim7ecfbae2014-07-11 14:16:29 +090044import android.provider.Settings.Global;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090045import android.util.ArraySet;
Jungshik Jang092b4452014-06-11 15:19:17 +090046import android.util.Slog;
Jungshik Jang79c58a42014-06-16 16:45:36 +090047import android.util.SparseArray;
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +090048import android.util.SparseBooleanArray;
Jungshik Jang092b4452014-06-11 15:19:17 +090049
Jungshik Jang79c58a42014-06-16 16:45:36 +090050import com.android.internal.annotations.GuardedBy;
Terry Heo959d2db2014-08-28 16:45:41 +090051import com.android.internal.util.IndentingPrintWriter;
Jungshik Jang60cffce2014-06-12 18:03:04 +090052import com.android.server.hdmi.DeviceDiscoveryAction.DeviceDiscoveryCallback;
Jungshik Janga5b74142014-06-23 18:03:10 +090053import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly;
Jungshik Jange5a93372014-07-25 13:41:14 +090054import com.android.server.hdmi.HdmiControlService.SendMessageCallback;
Jungshik Jang8f2ed352014-07-07 15:02:47 +090055import java.io.UnsupportedEncodingException;
Jungshik Jang79c58a42014-06-16 16:45:36 +090056import java.util.ArrayList;
Jungshik Jangb6591b82014-07-23 16:10:23 +090057import java.util.Arrays;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090058import java.util.Collection;
Jinsuk Kim0a3316b2014-06-14 09:33:55 +090059import java.util.Collections;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +090060import java.util.Iterator;
Jinsuk Kim0a3316b2014-06-14 09:33:55 +090061import java.util.List;
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +090062import java.util.HashMap;
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090063
64/**
65 * Represent a logical device of type TV residing in Android system.
66 */
67final class HdmiCecLocalDeviceTv extends HdmiCecLocalDevice {
Jungshik Jang092b4452014-06-11 15:19:17 +090068 private static final String TAG = "HdmiCecLocalDeviceTv";
Jinsuk Kim2918e9e2014-05-20 16:45:45 +090069
Jungshik Jang12e5dce2014-07-24 15:27:44 +090070 // Whether ARC is available or not. "true" means that ARC is established between TV and
Jungshik Janga13da0d2014-06-30 16:26:06 +090071 // AVR as audio receiver.
72 @ServiceThreadOnly
73 private boolean mArcEstablished = false;
74
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +090075 // Stores whether ARC feature is enabled per port. True by default for all the ARC-enabled ports.
76 private final SparseBooleanArray mArcFeatureEnabled = new SparseBooleanArray();
Jungshik Jang79c58a42014-06-16 16:45:36 +090077
Jungshik Jang377dcbd2014-07-15 15:49:02 +090078 // Whether System audio mode is activated or not.
79 // This becomes true only when all system audio sequences are finished.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +090080 @GuardedBy("mLock")
Jungshik Jang377dcbd2014-07-15 15:49:02 +090081 private boolean mSystemAudioActivated = false;
Jungshik Jang79c58a42014-06-16 16:45:36 +090082
Jinsuk Kim83335712014-06-24 07:57:00 +090083 // The previous port id (input) before switching to the new one. This is remembered in order to
84 // be able to switch to it upon receiving <Inactive Source> from currently active source.
85 // This remains valid only when the active source was switched via one touch play operation
86 // (either by TV or source device). Manual port switching invalidates this value to
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090087 // Constants.PORT_INVALID, for which case <Inactive Source> does not do anything.
Jinsuk Kim83335712014-06-24 07:57:00 +090088 @GuardedBy("mLock")
89 private int mPrevPortId;
90
Jungshik Jang8fa36b12014-06-25 15:51:36 +090091 @GuardedBy("mLock")
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090092 private int mSystemAudioVolume = Constants.UNKNOWN_VOLUME;
Jungshik Jang8fa36b12014-06-25 15:51:36 +090093
94 @GuardedBy("mLock")
95 private boolean mSystemAudioMute = false;
96
Jungshik Jangfa8e90d2014-06-23 14:40:32 +090097 // Copy of mDeviceInfos to guarantee thread-safety.
98 @GuardedBy("mLock")
Jungshik Jang61f4fbd2014-08-06 19:21:12 +090099 private List<HdmiDeviceInfo> mSafeAllDeviceInfos = Collections.emptyList();
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +0900100 // All external cec input(source) devices. Does not include system audio device.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900101 @GuardedBy("mLock")
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900102 private List<HdmiDeviceInfo> mSafeExternalInputs = Collections.emptyList();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900103
Jungshik Jang79c58a42014-06-16 16:45:36 +0900104 // Map-like container of all cec devices including local ones.
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900105 // device id is used as key of container.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900106 // This is not thread-safe. For external purpose use mSafeDeviceInfos.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900107 private final SparseArray<HdmiDeviceInfo> mDeviceInfos = new SparseArray<>();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900108
Jinsuk Kim160a6e52014-07-02 06:16:36 +0900109 // If true, TV going to standby mode puts other devices also to standby.
110 private boolean mAutoDeviceOff;
111
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900112 // If true, TV wakes itself up when receiving <Text/Image View On>.
113 private boolean mAutoWakeup;
114
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900115 // List of the logical address of local CEC devices. Unmodifiable, thread-safe.
116 private List<Integer> mLocalDeviceAddresses;
117
Yuncheol Heo25c20292014-07-31 17:59:39 +0900118 private final HdmiCecStandbyModeHandler mStandbyHandler;
119
Jinsuk Kimd5307192014-08-22 09:07:40 +0900120 // If true, do not do routing control/send active source for internal source.
121 // Set to true when the device was woken up by <Text/Image View On>.
122 private boolean mSkipRoutingControl;
123
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900124 // Set of physical addresses of CEC switches on the CEC bus. Managed independently from
125 // other CEC devices since they might not have logical address.
126 private final ArraySet<Integer> mCecSwitches = new ArraySet<Integer>();
127
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900128 // Message buffer used to buffer selected messages to process later. <Active Source>
129 // from a source device, for instance, needs to be buffered if the device is not
130 // discovered yet. The buffered commands are taken out and when they are ready to
131 // handle.
132 private final DelayedMessageBuffer mDelayedMessageBuffer = new DelayedMessageBuffer(this);
133
134 // Defines the callback invoked when TV input framework is updated with input status.
135 // We are interested in the notification for HDMI input addition event, in order to
136 // process any CEC commands that arrived before the input is added.
137 private final TvInputCallback mTvInputCallback = new TvInputCallback() {
138 @Override
139 public void onInputAdded(String inputId) {
140 TvInputInfo tvInfo = mService.getTvInputManager().getTvInputInfo(inputId);
Terry Heoa22d77e2015-11-05 15:53:33 +0900141 if (tvInfo == null) return;
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900142 HdmiDeviceInfo info = tvInfo.getHdmiDeviceInfo();
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900143 if (info == null) return;
144 addTvInput(inputId, info.getId());
145 if (info.isCecDevice()) {
146 processDelayedActiveSource(info.getLogicalAddress());
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900147 }
148 }
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900149
150 @Override
151 public void onInputRemoved(String inputId) {
152 removeTvInput(inputId);
153 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900154 };
155
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900156 // Keeps the mapping (TV input ID, HDMI device ID) to keep track of the TV inputs ready to
157 // accept input switching request from HDMI devices. Requests for which the corresponding
158 // input ID is not yet registered by TV input framework need to be buffered for delayed
159 // processing.
160 private final HashMap<String, Integer> mTvInputs = new HashMap<>();
161
162 @ServiceThreadOnly
163 private void addTvInput(String inputId, int deviceId) {
164 assertRunOnServiceThread();
165 mTvInputs.put(inputId, deviceId);
166 }
167
168 @ServiceThreadOnly
169 private void removeTvInput(String inputId) {
170 assertRunOnServiceThread();
171 mTvInputs.remove(inputId);
172 }
173
174 @Override
175 @ServiceThreadOnly
176 protected boolean isInputReady(int deviceId) {
177 assertRunOnServiceThread();
178 return mTvInputs.containsValue(deviceId);
179 }
180
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900181 private SelectRequestBuffer mSelectRequestBuffer;
182
Jungshik Jang3ee65722014-06-03 16:22:30 +0900183 HdmiCecLocalDeviceTv(HdmiControlService service) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900184 super(service, HdmiDeviceInfo.DEVICE_TV);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900185 mPrevPortId = Constants.INVALID_PORT_ID;
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900186 mAutoDeviceOff = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED,
187 true);
188 mAutoWakeup = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_WAKEUP_ENABLED, true);
Yuncheol Heo25c20292014-07-31 17:59:39 +0900189 mStandbyHandler = new HdmiCecStandbyModeHandler(service, this);
Jungshik Jang8b308d92014-05-29 21:52:28 +0900190 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900191
Jungshik Jang8b308d92014-05-29 21:52:28 +0900192 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900193 @ServiceThreadOnly
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900194 protected void onAddressAllocated(int logicalAddress, int reason) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900195 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900196 List<HdmiPortInfo> ports = mService.getPortInfo();
197 for (HdmiPortInfo port : ports) {
198 mArcFeatureEnabled.put(port.getId(), port.isArcSupported());
199 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900200 mService.registerTvInputCallback(mTvInputCallback);
Jungshik Jang3ee65722014-06-03 16:22:30 +0900201 mService.sendCecCommand(HdmiCecMessageBuilder.buildReportPhysicalAddressCommand(
202 mAddress, mService.getPhysicalAddress(), mDeviceType));
203 mService.sendCecCommand(HdmiCecMessageBuilder.buildDeviceVendorIdCommand(
204 mAddress, mService.getVendorId()));
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900205 mCecSwitches.add(mService.getPhysicalAddress()); // TV is a CEC switch too.
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900206 mTvInputs.clear();
Jinsuk Kimd5307192014-08-22 09:07:40 +0900207 mSkipRoutingControl = (reason == HdmiControlService.INITIATED_BY_WAKE_UP_MESSAGE);
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900208 launchRoutingControl(reason != HdmiControlService.INITIATED_BY_ENABLE_CEC &&
209 reason != HdmiControlService.INITIATED_BY_BOOT_UP);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900210 mLocalDeviceAddresses = initLocalDeviceAddresses();
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900211 resetSelectRequestBuffer();
Jungshik Jang60cffce2014-06-12 18:03:04 +0900212 launchDeviceDiscovery();
Jungshik Jang6f34f5a2014-07-08 21:17:29 +0900213 }
214
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900215
216 @ServiceThreadOnly
217 private List<Integer> initLocalDeviceAddresses() {
218 assertRunOnServiceThread();
219 List<Integer> addresses = new ArrayList<>();
220 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
221 addresses.add(device.getDeviceInfo().getLogicalAddress());
222 }
223 return Collections.unmodifiableList(addresses);
224 }
225
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900226
227 @ServiceThreadOnly
228 public void setSelectRequestBuffer(SelectRequestBuffer requestBuffer) {
229 assertRunOnServiceThread();
230 mSelectRequestBuffer = requestBuffer;
231 }
232
233 @ServiceThreadOnly
234 private void resetSelectRequestBuffer() {
235 assertRunOnServiceThread();
236 setSelectRequestBuffer(SelectRequestBuffer.EMPTY_BUFFER);
237 }
238
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900239 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900240 protected int getPreferredAddress() {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900241 return Constants.ADDR_TV;
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900242 }
243
244 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900245 protected void setPreferredAddress(int addr) {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900246 Slog.w(TAG, "Preferred addres will not be stored for TV");
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900247 }
248
Yuncheol Heo25c20292014-07-31 17:59:39 +0900249 @Override
250 @ServiceThreadOnly
251 boolean dispatchMessage(HdmiCecMessage message) {
252 assertRunOnServiceThread();
253 if (mService.isPowerStandby() && mStandbyHandler.handleCommand(message)) {
254 return true;
255 }
256 return super.onMessage(message);
257 }
258
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900259 /**
260 * Performs the action 'device select', or 'one touch play' initiated by TV.
261 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900262 * @param id id of HDMI device to select
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900263 * @param callback callback object to report the result with
264 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900265 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900266 void deviceSelect(int id, IHdmiControlCallback callback) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900267 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900268 HdmiDeviceInfo targetDevice = mDeviceInfos.get(id);
269 if (targetDevice == null) {
270 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
271 return;
272 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900273 int targetAddress = targetDevice.getLogicalAddress();
Jinsuk Kim58500f42014-08-05 12:48:35 +0900274 ActiveSource active = getActiveSource();
Yu.Ishihara6b2a6172015-03-25 09:44:06 +0900275 if (targetDevice.getDevicePowerStatus() == HdmiControlManager.POWER_STATUS_ON
276 && active.isValid()
277 && targetAddress == active.logicalAddress) {
Jinsuk Kim58500f42014-08-05 12:48:35 +0900278 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
279 return;
280 }
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900281 if (targetAddress == Constants.ADDR_INTERNAL) {
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900282 handleSelectInternalSource();
283 // Switching to internal source is always successful even when CEC control is disabled.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900284 setActiveSource(targetAddress, mService.getPhysicalAddress());
Jinsuk Kim7e742062014-07-30 13:19:13 +0900285 setActivePath(mService.getPhysicalAddress());
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900286 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
Jinsuk Kim83335712014-06-24 07:57:00 +0900287 return;
288 }
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900289 if (!mService.isControlEnabled()) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900290 setActiveSource(targetDevice);
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900291 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
292 return;
293 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900294 removeAction(DeviceSelectAction.class);
295 addAndStartAction(new DeviceSelectAction(this, targetDevice, callback));
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900296 }
297
Jungshik Janga5b74142014-06-23 18:03:10 +0900298 @ServiceThreadOnly
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900299 private void handleSelectInternalSource() {
Jinsuk Kima062a932014-06-18 10:00:39 +0900300 assertRunOnServiceThread();
Jinsuk Kim83335712014-06-24 07:57:00 +0900301 // Seq #18
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900302 if (mService.isControlEnabled() && mActiveSource.logicalAddress != mAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900303 updateActiveSource(mAddress, mService.getPhysicalAddress());
Jinsuk Kimd5307192014-08-22 09:07:40 +0900304 if (mSkipRoutingControl) {
305 mSkipRoutingControl = false;
306 return;
307 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900308 HdmiCecMessage activeSource = HdmiCecMessageBuilder.buildActiveSource(
309 mAddress, mService.getPhysicalAddress());
310 mService.sendCecCommand(activeSource);
311 }
312 }
313
314 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900315 void updateActiveSource(int logicalAddress, int physicalAddress) {
316 assertRunOnServiceThread();
317 updateActiveSource(ActiveSource.of(logicalAddress, physicalAddress));
318 }
319
320 @ServiceThreadOnly
321 void updateActiveSource(ActiveSource newActive) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900322 assertRunOnServiceThread();
323 // Seq #14
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900324 if (mActiveSource.equals(newActive)) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900325 return;
326 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900327 setActiveSource(newActive);
328 int logicalAddress = newActive.logicalAddress;
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900329 if (getCecDeviceInfo(logicalAddress) != null && logicalAddress != mAddress) {
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900330 if (mService.pathToPortId(newActive.physicalAddress) == getActivePortId()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900331 setPrevPortId(getActivePortId());
332 }
333 // TODO: Show the OSD banner related to the new active source device.
334 } else {
335 // TODO: If displayed, remove the OSD banner related to the previous
336 // active source device.
337 }
338 }
339
Jinsuk Kim2b152012014-07-25 08:22:26 +0900340 int getPortId(int physicalAddress) {
341 return mService.pathToPortId(physicalAddress);
342 }
343
Jinsuk Kim83335712014-06-24 07:57:00 +0900344 /**
345 * Returns the previous port id kept to handle input switching on <Inactive Source>.
346 */
347 int getPrevPortId() {
348 synchronized (mLock) {
349 return mPrevPortId;
350 }
351 }
352
353 /**
354 * Sets the previous port id. INVALID_PORT_ID invalidates it, hence no actions will be
355 * taken for <Inactive Source>.
356 */
357 void setPrevPortId(int portId) {
358 synchronized (mLock) {
359 mPrevPortId = portId;
360 }
361 }
362
363 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900364 void updateActiveInput(int path, boolean notifyInputChange) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900365 assertRunOnServiceThread();
366 // Seq #15
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900367 setActivePath(path);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900368 // TODO: Handle PAP/PIP case.
369 // Show OSD port change banner
370 if (notifyInputChange) {
371 ActiveSource activeSource = getActiveSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900372 HdmiDeviceInfo info = getCecDeviceInfo(activeSource.logicalAddress);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900373 if (info == null) {
Jinsuk Kim6ad7cbd2015-01-06 11:30:56 +0900374 info = mService.getDeviceInfoByPort(getActivePortId());
375 if (info == null) {
376 // No CEC/MHL device is present at the port. Attempt to switch to
377 // the hardware port itself for non-CEC devices that may be connected.
378 info = new HdmiDeviceInfo(path, getActivePortId());
379 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900380 }
381 mService.invokeInputChangeListener(info);
382 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900383 }
384
385 @ServiceThreadOnly
386 void doManualPortSwitching(int portId, IHdmiControlCallback callback) {
387 assertRunOnServiceThread();
388 // Seq #20
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900389 if (!mService.isValidPortId(portId)) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900390 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
Jinsuk Kima062a932014-06-18 10:00:39 +0900391 return;
392 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900393 if (portId == getActivePortId()) {
394 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
395 return;
396 }
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900397 mActiveSource.invalidate();
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900398 if (!mService.isControlEnabled()) {
399 setActivePortId(portId);
400 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
401 return;
402 }
Jinsuk Kim1f8d1c572014-08-12 15:39:53 +0900403 int oldPath = getActivePortId() != Constants.INVALID_PORT_ID
404 ? mService.portIdToPath(getActivePortId()) : getDeviceInfo().getPhysicalAddress();
Jinsuk Kimd5307192014-08-22 09:07:40 +0900405 setActivePath(oldPath);
406 if (mSkipRoutingControl) {
407 mSkipRoutingControl = false;
408 return;
409 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900410 int newPath = mService.portIdToPath(portId);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900411 startRoutingControl(oldPath, newPath, true, callback);
412 }
413
414 @ServiceThreadOnly
415 void startRoutingControl(int oldPath, int newPath, boolean queryDevicePowerStatus,
416 IHdmiControlCallback callback) {
417 assertRunOnServiceThread();
418 if (oldPath == newPath) {
419 return;
420 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900421 HdmiCecMessage routingChange =
422 HdmiCecMessageBuilder.buildRoutingChange(mAddress, oldPath, newPath);
423 mService.sendCecCommand(routingChange);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900424 removeAction(RoutingControlAction.class);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900425 addAndStartAction(
426 new RoutingControlAction(this, newPath, queryDevicePowerStatus, callback));
Jinsuk Kima062a932014-06-18 10:00:39 +0900427 }
428
Jungshik Jangf67113f2014-08-22 16:27:19 +0900429 @ServiceThreadOnly
Jinsuk Kimcae66272014-07-04 13:26:44 +0900430 int getPowerStatus() {
Jungshik Jangf67113f2014-08-22 16:27:19 +0900431 assertRunOnServiceThread();
Jinsuk Kimcae66272014-07-04 13:26:44 +0900432 return mService.getPowerStatus();
433 }
434
Jinsuk Kima062a932014-06-18 10:00:39 +0900435 /**
436 * Sends key to a target CEC device.
437 *
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900438 * @param keyCode key code to send. Defined in {@link android.view.KeyEvent}.
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900439 * @param isPressed true if this is key press event
Jinsuk Kima062a932014-06-18 10:00:39 +0900440 */
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900441 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900442 @ServiceThreadOnly
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900443 protected void sendKeyEvent(int keyCode, boolean isPressed) {
Jinsuk Kima062a932014-06-18 10:00:39 +0900444 assertRunOnServiceThread();
Dongil Seob64c2ba2014-08-26 19:41:39 +0900445 if (!HdmiCecKeycode.isSupportedKeycode(keyCode)) {
446 Slog.w(TAG, "Unsupported key: " + keyCode);
447 return;
448 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900449 List<SendKeyAction> action = getActions(SendKeyAction.class);
Jinsuk Kim454fab52015-01-29 16:38:36 +0900450 int logicalAddress = findKeyReceiverAddress();
451 if (logicalAddress == mAddress) {
452 Slog.w(TAG, "Discard key event to itself :" + keyCode + " pressed:" + isPressed);
453 return;
454 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900455 if (!action.isEmpty()) {
456 action.get(0).processKeyEvent(keyCode, isPressed);
457 } else {
Jinsuk Kim75434972014-09-25 13:52:50 +0900458 if (isPressed) {
Jinsuk Kim75434972014-09-25 13:52:50 +0900459 if (logicalAddress != Constants.ADDR_INVALID) {
460 addAndStartAction(new SendKeyAction(this, logicalAddress, keyCode));
461 return;
462 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900463 }
Jinsuk Kim75434972014-09-25 13:52:50 +0900464 Slog.w(TAG, "Discard key event: " + keyCode + " pressed:" + isPressed);
Jinsuk Kima062a932014-06-18 10:00:39 +0900465 }
466 }
467
Jinsuk Kim75434972014-09-25 13:52:50 +0900468 private int findKeyReceiverAddress() {
469 if (getActiveSource().isValid()) {
470 return getActiveSource().logicalAddress;
471 }
472 HdmiDeviceInfo info = getDeviceInfoByPath(getActivePath());
473 if (info != null) {
474 return info.getLogicalAddress();
475 }
476 return Constants.ADDR_INVALID;
477 }
478
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900479 private static void invokeCallback(IHdmiControlCallback callback, int result) {
Jinsuk Kim4893c7e2014-06-19 14:13:22 +0900480 if (callback == null) {
481 return;
482 }
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900483 try {
484 callback.onComplete(result);
485 } catch (RemoteException e) {
486 Slog.e(TAG, "Invoking callback failed:" + e);
487 }
488 }
489
Jungshik Jang092b4452014-06-11 15:19:17 +0900490 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900491 @ServiceThreadOnly
Jinsuk Kim83335712014-06-24 07:57:00 +0900492 protected boolean handleActiveSource(HdmiCecMessage message) {
493 assertRunOnServiceThread();
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900494 int logicalAddress = message.getSource();
495 int physicalAddress = HdmiUtils.twoBytesToInt(message.getParams());
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900496 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
497 if (info == null) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900498 if (!handleNewDeviceAtTheTailOfActivePath(physicalAddress)) {
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900499 HdmiLogger.debug("Device info %X not found; buffering the command", logicalAddress);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900500 mDelayedMessageBuffer.add(message);
501 }
Donghyun Cho455f09d2016-05-10 20:06:52 +0900502 } else if (isInputReady(info.getId())
503 || info.getDeviceType() == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) {
Jinsuk Kim7cc51c62015-04-30 10:39:35 +0900504 updateDevicePowerStatus(logicalAddress, HdmiControlManager.POWER_STATUS_ON);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900505 ActiveSource activeSource = ActiveSource.of(logicalAddress, physicalAddress);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900506 ActiveSourceHandler.create(this, null).process(activeSource, info.getDeviceType());
Donghyun Cho455f09d2016-05-10 20:06:52 +0900507 } else {
508 HdmiLogger.debug("Input not ready for device: %X; buffering the command", info.getId());
509 mDelayedMessageBuffer.add(message);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900510 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900511 return true;
512 }
513
514 @Override
515 @ServiceThreadOnly
516 protected boolean handleInactiveSource(HdmiCecMessage message) {
517 assertRunOnServiceThread();
518 // Seq #10
519
520 // Ignore <Inactive Source> from non-active source device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900521 if (getActiveSource().logicalAddress != message.getSource()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900522 return true;
523 }
Jinsuk Kim4d43d932014-07-03 16:43:58 +0900524 if (isProhibitMode()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900525 return true;
526 }
527 int portId = getPrevPortId();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900528 if (portId != Constants.INVALID_PORT_ID) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900529 // TODO: Do this only if TV is not showing multiview like PIP/PAP.
530
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900531 HdmiDeviceInfo inactiveSource = getCecDeviceInfo(message.getSource());
Jinsuk Kim83335712014-06-24 07:57:00 +0900532 if (inactiveSource == null) {
533 return true;
534 }
535 if (mService.pathToPortId(inactiveSource.getPhysicalAddress()) == portId) {
536 return true;
537 }
538 // TODO: Switch the TV freeze mode off
539
Jinsuk Kim83335712014-06-24 07:57:00 +0900540 doManualPortSwitching(portId, null);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900541 setPrevPortId(Constants.INVALID_PORT_ID);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900542 } else {
543 // No HDMI port to switch to was found. Notify the input change listers to
544 // switch to the lastly shown internal input.
545 mActiveSource.invalidate();
546 setActivePath(Constants.INVALID_PHYSICAL_ADDRESS);
547 mService.invokeInputChangeListener(HdmiDeviceInfo.INACTIVE_DEVICE);
Jinsuk Kim83335712014-06-24 07:57:00 +0900548 }
549 return true;
550 }
551
552 @Override
553 @ServiceThreadOnly
554 protected boolean handleRequestActiveSource(HdmiCecMessage message) {
555 assertRunOnServiceThread();
556 // Seq #19
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900557 if (mAddress == getActiveSource().logicalAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900558 mService.sendCecCommand(
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900559 HdmiCecMessageBuilder.buildActiveSource(mAddress, getActivePath()));
Jinsuk Kim83335712014-06-24 07:57:00 +0900560 }
561 return true;
562 }
563
564 @Override
565 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900566 protected boolean handleGetMenuLanguage(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900567 assertRunOnServiceThread();
Jungshik Jangf67113f2014-08-22 16:27:19 +0900568 if (!broadcastMenuLanguage(mService.getLanguage())) {
Jungshik Jang092b4452014-06-11 15:19:17 +0900569 Slog.w(TAG, "Failed to respond to <Get Menu Language>: " + message.toString());
570 }
571 return true;
572 }
573
Terry Heo1ca0a432014-08-18 10:30:32 +0900574 @ServiceThreadOnly
575 boolean broadcastMenuLanguage(String language) {
576 assertRunOnServiceThread();
577 HdmiCecMessage command = HdmiCecMessageBuilder.buildSetMenuLanguageCommand(
578 mAddress, language);
579 if (command != null) {
580 mService.sendCecCommand(command);
581 return true;
582 }
583 return false;
584 }
585
Jungshik Jang60cffce2014-06-12 18:03:04 +0900586 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900587 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900588 protected boolean handleReportPhysicalAddress(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900589 assertRunOnServiceThread();
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900590 int path = HdmiUtils.twoBytesToInt(message.getParams());
591 int address = message.getSource();
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900592 int type = message.getParams()[2];
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900593
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900594 if (updateCecSwitchInfo(address, type, path)) return true;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900595
Jungshik Jang092b4452014-06-11 15:19:17 +0900596 // Ignore if [Device Discovery Action] is going on.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900597 if (hasAction(DeviceDiscoveryAction.class)) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900598 Slog.i(TAG, "Ignored while Device Discovery Action is in progress: " + message);
Jungshik Jang092b4452014-06-11 15:19:17 +0900599 return true;
600 }
601
Jinsuk Kim4b4b9402014-09-02 10:30:10 +0900602 if (!isInDeviceList(address, path)) {
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900603 handleNewDeviceAtTheTailOfActivePath(path);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900604 }
Jinsuk Kim7cd4a582015-01-29 09:50:25 +0900605
606 // Add the device ahead with default information to handle <Active Source>
607 // promptly, rather than waiting till the new device action is finished.
608 HdmiDeviceInfo deviceInfo = new HdmiDeviceInfo(address, path, getPortId(path), type,
609 Constants.UNKNOWN_VENDOR_ID, HdmiUtils.getDefaultDeviceName(address));
610 addCecDevice(deviceInfo);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900611 startNewDeviceAction(ActiveSource.of(address, path), type);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900612 return true;
613 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900614
Jungshik Jang4480efa2014-09-04 17:08:34 +0900615 @Override
616 protected boolean handleReportPowerStatus(HdmiCecMessage command) {
617 int newStatus = command.getParams()[0] & 0xFF;
618 updateDevicePowerStatus(command.getSource(), newStatus);
619 return true;
620 }
621
622 @Override
623 protected boolean handleTimerStatus(HdmiCecMessage message) {
624 // Do nothing.
625 return true;
626 }
627
628 @Override
629 protected boolean handleRecordStatus(HdmiCecMessage message) {
630 // Do nothing.
631 return true;
632 }
633
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900634 boolean updateCecSwitchInfo(int address, int type, int path) {
635 if (address == Constants.ADDR_UNREGISTERED
636 && type == HdmiDeviceInfo.DEVICE_PURE_CEC_SWITCH) {
637 mCecSwitches.add(path);
638 updateSafeDeviceInfoList();
639 return true; // Pure switch does not need further processing. Return here.
640 }
641 if (type == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) {
642 mCecSwitches.add(path);
643 }
644 return false;
645 }
646
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900647 void startNewDeviceAction(ActiveSource activeSource, int deviceType) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900648 for (NewDeviceAction action : getActions(NewDeviceAction.class)) {
649 // If there is new device action which has the same logical address and path
650 // ignore new request.
651 // NewDeviceAction is created whenever it receives <Report Physical Address>.
652 // And there is a chance starting NewDeviceAction for the same source.
653 // Usually, new device sends <Report Physical Address> when it's plugged
654 // in. However, TV can detect a new device from HotPlugDetectionAction,
655 // which sends <Give Physical Address> to the source for newly detected
656 // device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900657 if (action.isActionOf(activeSource)) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900658 return;
659 }
660 }
661
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900662 addAndStartAction(new NewDeviceAction(this, activeSource.logicalAddress,
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900663 activeSource.physicalAddress, deviceType));
Jungshik Jang97affee2014-07-11 17:04:43 +0900664 }
665
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900666 private boolean handleNewDeviceAtTheTailOfActivePath(int path) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900667 // Seq #22
668 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900669 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim7c3a9562014-08-01 11:07:42 +0900670 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900671 startRoutingControl(getActivePath(), newPath, false, null);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900672 return true;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900673 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900674 return false;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900675 }
676
677 /**
678 * Whether the given path is located in the tail of current active path.
679 *
680 * @param path to be tested
681 * @param activePath current active path
682 * @return true if the given path is located in the tail of current active path; otherwise,
683 * false
684 */
685 static boolean isTailOfActivePath(int path, int activePath) {
686 // If active routing path is internal source, return false.
687 if (activePath == 0) {
688 return false;
689 }
690 for (int i = 12; i >= 0; i -= 4) {
691 int curActivePath = (activePath >> i) & 0xF;
692 if (curActivePath == 0) {
693 return true;
694 } else {
695 int curPath = (path >> i) & 0xF;
696 if (curPath != curActivePath) {
697 return false;
698 }
699 }
700 }
701 return false;
702 }
703
704 @Override
705 @ServiceThreadOnly
706 protected boolean handleRoutingChange(HdmiCecMessage message) {
707 assertRunOnServiceThread();
708 // Seq #21
709 byte[] params = message.getParams();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900710 int currentPath = HdmiUtils.twoBytesToInt(params);
711 if (HdmiUtils.isAffectingActiveRoutingPath(getActivePath(), currentPath)) {
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900712 mActiveSource.invalidate();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900713 removeAction(RoutingControlAction.class);
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900714 int newPath = HdmiUtils.twoBytesToInt(params, 2);
Jinsuk Kim04fd2802014-07-03 14:04:02 +0900715 addAndStartAction(new RoutingControlAction(this, newPath, true, null));
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900716 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900717 return true;
718 }
Jinsuk Kim0a3316b2014-06-14 09:33:55 +0900719
720 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900721 @ServiceThreadOnly
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900722 protected boolean handleReportAudioStatus(HdmiCecMessage message) {
723 assertRunOnServiceThread();
724
725 byte params[] = message.getParams();
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900726 int mute = params[0] & 0x80;
727 int volume = params[0] & 0x7F;
728 setAudioStatus(mute == 0x80, volume);
729 return true;
730 }
731
Yuncheol Heo38db6292014-07-01 14:15:14 +0900732 @Override
733 @ServiceThreadOnly
734 protected boolean handleTextViewOn(HdmiCecMessage message) {
735 assertRunOnServiceThread();
Jinsuk Kimc854c952015-10-12 09:33:33 +0900736
737 // Note that <Text View On> (and <Image View On>) command won't be handled here in
738 // most cases. A dedicated microcontroller should be in charge while Android system
739 // is in sleep mode, and the command need not be passed up to this service.
740 // The only situation where the command reaches this handler is that sleep mode is
741 // implemented in such a way that Android system is not really put to standby mode
742 // but only the display is set to blank. Then the command leads to the effect of
743 // turning on the display by the invocation of PowerManager.wakeUp().
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900744 if (mService.isPowerStandbyOrTransient() && mAutoWakeup) {
Yuncheol Heo38db6292014-07-01 14:15:14 +0900745 mService.wakeUp();
746 }
Yuncheol Heo38db6292014-07-01 14:15:14 +0900747 return true;
748 }
749
750 @Override
751 @ServiceThreadOnly
752 protected boolean handleImageViewOn(HdmiCecMessage message) {
753 assertRunOnServiceThread();
754 // Currently, it's the same as <Text View On>.
755 return handleTextViewOn(message);
756 }
757
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900758 @Override
759 @ServiceThreadOnly
760 protected boolean handleSetOsdName(HdmiCecMessage message) {
761 int source = message.getSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900762 HdmiDeviceInfo deviceInfo = getCecDeviceInfo(source);
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900763 // If the device is not in device list, ignore it.
764 if (deviceInfo == null) {
765 Slog.e(TAG, "No source device info for <Set Osd Name>." + message);
766 return true;
767 }
768 String osdName = null;
769 try {
770 osdName = new String(message.getParams(), "US-ASCII");
771 } catch (UnsupportedEncodingException e) {
772 Slog.e(TAG, "Invalid <Set Osd Name> request:" + message, e);
773 return true;
774 }
775
776 if (deviceInfo.getDisplayName().equals(osdName)) {
777 Slog.i(TAG, "Ignore incoming <Set Osd Name> having same osd name:" + message);
778 return true;
779 }
780
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900781 addCecDevice(new HdmiDeviceInfo(deviceInfo.getLogicalAddress(),
Jinsuk Kim2b152012014-07-25 08:22:26 +0900782 deviceInfo.getPhysicalAddress(), deviceInfo.getPortId(),
783 deviceInfo.getDeviceType(), deviceInfo.getVendorId(), osdName));
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900784 return true;
785 }
786
Jungshik Janga5b74142014-06-23 18:03:10 +0900787 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900788 private void launchDeviceDiscovery() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900789 assertRunOnServiceThread();
790 clearDeviceInfoList();
791 DeviceDiscoveryAction action = new DeviceDiscoveryAction(this,
Jungshik Jang60cffce2014-06-12 18:03:04 +0900792 new DeviceDiscoveryCallback() {
793 @Override
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900794 public void onDeviceDiscoveryDone(List<HdmiDeviceInfo> deviceInfos) {
795 for (HdmiDeviceInfo info : deviceInfos) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900796 addCecDevice(info);
Jungshik Jang60cffce2014-06-12 18:03:04 +0900797 }
798
799 // Since we removed all devices when it's start and
800 // device discovery action does not poll local devices,
801 // we should put device info of local device manually here
802 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900803 addCecDevice(device.getDeviceInfo());
Jungshik Jang60cffce2014-06-12 18:03:04 +0900804 }
805
Jinsuk Kimf98b9e82015-10-05 14:24:48 +0900806 mSelectRequestBuffer.process();
807 resetSelectRequestBuffer();
808
Jungshik Jang79c58a42014-06-16 16:45:36 +0900809 addAndStartAction(new HotplugDetectionAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang410ca9c2014-08-07 18:04:14 +0900810 addAndStartAction(new PowerStatusMonitorAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang187d0172014-06-17 17:48:42 +0900811
812 // If there is AVR, initiate System Audio Auto initiation action,
813 // which turns on and off system audio according to last system
814 // audio setting.
Jungshik Jang339227d2014-08-25 15:37:20 +0900815 HdmiDeviceInfo avr = getAvrDeviceInfo();
816 if (avr != null) {
817 onNewAvrAdded(avr);
Wally Yau951e3e42015-04-03 15:12:52 -0700818 } else {
819 setSystemAudioMode(false, true);
Jungshik Jang187d0172014-06-17 17:48:42 +0900820 }
Jungshik Jang60cffce2014-06-12 18:03:04 +0900821 }
822 });
Jungshik Jang79c58a42014-06-16 16:45:36 +0900823 addAndStartAction(action);
824 }
825
Jungshik Jang339227d2014-08-25 15:37:20 +0900826 @ServiceThreadOnly
827 void onNewAvrAdded(HdmiDeviceInfo avr) {
828 assertRunOnServiceThread();
Donghyun Choc21f63a2016-05-10 10:37:47 +0900829 addAndStartAction(new SystemAudioAutoInitiationAction(this, avr.getLogicalAddress()));
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900830 if (isArcFeatureEnabled(avr.getPortId())
831 && !hasAction(SetArcTransmissionStateAction.class)) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900832 startArcAction(true);
833 }
834 }
835
Jungshik Jang79c58a42014-06-16 16:45:36 +0900836 // Clear all device info.
Jungshik Janga5b74142014-06-23 18:03:10 +0900837 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900838 private void clearDeviceInfoList() {
839 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900840 for (HdmiDeviceInfo info : mSafeExternalInputs) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +0900841 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +0900842 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900843 mDeviceInfos.clear();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900844 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900845 }
846
Jungshik Janga5b74142014-06-23 18:03:10 +0900847 @ServiceThreadOnly
Yuncheol Heoc516d652014-07-11 18:23:24 +0900848 // Seq #32
Jungshik Jangea67c182014-06-19 22:19:20 +0900849 void changeSystemAudioMode(boolean enabled, IHdmiControlCallback callback) {
850 assertRunOnServiceThread();
Yuncheol Heoc516d652014-07-11 18:23:24 +0900851 if (!mService.isControlEnabled() || hasAction(DeviceDiscoveryAction.class)) {
852 setSystemAudioMode(false, true);
853 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
854 return;
855 }
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900856 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jangea67c182014-06-19 22:19:20 +0900857 if (avr == null) {
Yuncheol Heoc516d652014-07-11 18:23:24 +0900858 setSystemAudioMode(false, true);
Yuncheol Heod05f67f2014-07-11 16:06:40 +0900859 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
Jungshik Jangea67c182014-06-19 22:19:20 +0900860 return;
861 }
862
863 addAndStartAction(
864 new SystemAudioActionFromTv(this, avr.getLogicalAddress(), enabled, callback));
Jungshik Jangea67c182014-06-19 22:19:20 +0900865 }
866
Jungshik Jangca5be9a2014-07-01 18:01:26 +0900867 // # Seq 25
Jinsuk Kim7ecfbae2014-07-11 14:16:29 +0900868 void setSystemAudioMode(boolean on, boolean updateSetting) {
Jungshik Jang2e8f1b62014-09-03 08:28:02 +0900869 HdmiLogger.debug("System Audio Mode change[old:%b new:%b]", mSystemAudioActivated, on);
Jungshik Jang473119f2014-08-27 16:43:22 +0900870
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900871 if (updateSetting) {
872 mService.writeBooleanSetting(Global.HDMI_SYSTEM_AUDIO_ENABLED, on);
873 }
874 updateAudioManagerForSystemAudio(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900875 synchronized (mLock) {
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900876 if (mSystemAudioActivated != on) {
877 mSystemAudioActivated = on;
Jungshik Jangea67c182014-06-19 22:19:20 +0900878 mService.announceSystemAudioModeChange(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900879 }
880 }
881 }
882
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900883 private void updateAudioManagerForSystemAudio(boolean on) {
Jungshik Jang6b096d32014-09-12 18:49:08 +0900884 int device = mService.getAudioManager().setHdmiSystemAudioSupported(on);
885 HdmiLogger.debug("[A]UpdateSystemAudio mode[on=%b] output=[%X]", on, device);
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900886 }
887
888 boolean isSystemAudioActivated() {
Jinsuk Kim86a1e5a2014-11-19 06:04:50 +0900889 if (!hasSystemAudioDevice()) {
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900890 return false;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900891 }
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900892 synchronized (mLock) {
893 return mSystemAudioActivated;
894 }
895 }
896
897 boolean getSystemAudioModeSetting() {
898 return mService.readBooleanSetting(Global.HDMI_SYSTEM_AUDIO_ENABLED, false);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900899 }
900
901 /**
902 * Change ARC status into the given {@code enabled} status.
903 *
904 * @return {@code true} if ARC was in "Enabled" status
905 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900906 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900907 boolean setArcStatus(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900908 assertRunOnServiceThread();
Jungshik Jang6b096d32014-09-12 18:49:08 +0900909
910 HdmiLogger.debug("Set Arc Status[old:%b new:%b]", mArcEstablished, enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900911 boolean oldStatus = mArcEstablished;
912 // 1. Enable/disable ARC circuit.
Jinsuk Kim757c0972015-02-23 10:15:42 +0900913 setAudioReturnChannel(enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900914 // 2. Notify arc status to audio service.
915 notifyArcStatusToAudioService(enabled);
916 // 3. Update arc status;
917 mArcEstablished = enabled;
918 return oldStatus;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900919 }
920
Jinsuk Kim757c0972015-02-23 10:15:42 +0900921 /**
922 * Switch hardware ARC circuit in the system.
923 */
924 @ServiceThreadOnly
925 void setAudioReturnChannel(boolean enabled) {
926 assertRunOnServiceThread();
927 HdmiDeviceInfo avr = getAvrDeviceInfo();
928 if (avr != null) {
929 mService.setAudioReturnChannel(avr.getPortId(), enabled);
930 }
931 }
932
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +0900933 @ServiceThreadOnly
934 private void updateArcFeatureStatus(int portId, boolean isConnected) {
935 assertRunOnServiceThread();
Jinsuk Kim37f5bc282015-05-11 07:49:51 +0900936 HdmiPortInfo portInfo = mService.getPortInfo(portId);
937 if (!portInfo.isArcSupported()) {
938 return;
939 }
Jinsuk Kima6d5dad2015-04-24 08:02:57 +0900940 HdmiDeviceInfo avr = getAvrDeviceInfo();
941 if (avr == null) {
Jinsuk Kim37f5bc282015-05-11 07:49:51 +0900942 if (isConnected) {
943 // Update the status (since TV may not have seen AVR yet) so
944 // that ARC can be initiated after discovery.
945 mArcFeatureEnabled.put(portId, isConnected);
946 }
Jinsuk Kima6d5dad2015-04-24 08:02:57 +0900947 return;
948 }
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +0900949 // HEAC 2.4, HEACT 5-15
950 // Should not activate ARC if +5V status is false.
Jinsuk Kim37f5bc282015-05-11 07:49:51 +0900951 if (avr.getPortId() == portId) {
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900952 changeArcFeatureEnabled(portId, isConnected);
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +0900953 }
954 }
955
Jinsuk Kim7b0cf642015-04-14 09:43:45 +0900956 @ServiceThreadOnly
957 boolean isConnected(int portId) {
958 assertRunOnServiceThread();
959 return mService.isConnected(portId);
960 }
961
Jungshik Janga858d222014-06-23 17:17:47 +0900962 private void notifyArcStatusToAudioService(boolean enabled) {
963 // Note that we don't set any name to ARC.
964 mService.getAudioManager().setWiredDeviceConnectionState(
965 AudioSystem.DEVICE_OUT_HDMI_ARC,
Paul McLean10804eb2015-01-28 11:16:35 -0800966 enabled ? 1 : 0, "", "");
Jungshik Janga858d222014-06-23 17:17:47 +0900967 }
968
Jungshik Jang79c58a42014-06-16 16:45:36 +0900969 /**
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900970 * Returns true if ARC is currently established on a certain port.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900971 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900972 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900973 boolean isArcEstablished() {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900974 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900975 if (mArcEstablished) {
976 for (int i = 0; i < mArcFeatureEnabled.size(); i++) {
977 if (mArcFeatureEnabled.valueAt(i)) return true;
978 }
979 }
980 return false;
Jungshik Janga13da0d2014-06-30 16:26:06 +0900981 }
982
983 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900984 void changeArcFeatureEnabled(int portId, boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900985 assertRunOnServiceThread();
986
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900987 if (mArcFeatureEnabled.get(portId) != enabled) {
988 mArcFeatureEnabled.put(portId, enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900989 if (enabled) {
990 if (!mArcEstablished) {
991 startArcAction(true);
992 }
993 } else {
994 if (mArcEstablished) {
995 startArcAction(false);
996 }
997 }
Jungshik Janga13da0d2014-06-30 16:26:06 +0900998 }
999 }
1000
1001 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +09001002 boolean isArcFeatureEnabled(int portId) {
Jungshik Janga13da0d2014-06-30 16:26:06 +09001003 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +09001004 return mArcFeatureEnabled.get(portId);
Jungshik Janga13da0d2014-06-30 16:26:06 +09001005 }
1006
1007 @ServiceThreadOnly
Jungshik Jang339227d2014-08-25 15:37:20 +09001008 void startArcAction(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +09001009 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001010 HdmiDeviceInfo info = getAvrDeviceInfo();
Jungshik Janga13da0d2014-06-30 16:26:06 +09001011 if (info == null) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001012 Slog.w(TAG, "Failed to start arc action; No AVR device.");
Jungshik Janga13da0d2014-06-30 16:26:06 +09001013 return;
1014 }
Jungshik Jang339227d2014-08-25 15:37:20 +09001015 if (!canStartArcUpdateAction(info.getLogicalAddress(), enabled)) {
1016 Slog.w(TAG, "Failed to start arc action; ARC configuration check failed.");
1017 if (enabled && !isConnectedToArcPort(info.getPhysicalAddress())) {
1018 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
1019 }
Jungshik Janga13da0d2014-06-30 16:26:06 +09001020 return;
1021 }
1022
1023 // Terminate opposite action and start action if not exist.
1024 if (enabled) {
1025 removeAction(RequestArcTerminationAction.class);
1026 if (!hasAction(RequestArcInitiationAction.class)) {
1027 addAndStartAction(new RequestArcInitiationAction(this, info.getLogicalAddress()));
1028 }
1029 } else {
1030 removeAction(RequestArcInitiationAction.class);
1031 if (!hasAction(RequestArcTerminationAction.class)) {
1032 addAndStartAction(new RequestArcTerminationAction(this, info.getLogicalAddress()));
1033 }
Jungshik Jang79c58a42014-06-16 16:45:36 +09001034 }
1035 }
1036
Jungshik Jang339227d2014-08-25 15:37:20 +09001037 private boolean isDirectConnectAddress(int physicalAddress) {
1038 return (physicalAddress & Constants.ROUTING_PATH_TOP_MASK) == physicalAddress;
1039 }
1040
Jungshik Jang79c58a42014-06-16 16:45:36 +09001041 void setAudioStatus(boolean mute, int volume) {
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001042 synchronized (mLock) {
1043 mSystemAudioMute = mute;
1044 mSystemAudioVolume = volume;
Jungshik Jangb69aafbf2014-07-11 16:29:06 +09001045 int maxVolume = mService.getAudioManager().getStreamMaxVolume(
1046 AudioManager.STREAM_MUSIC);
1047 mService.setAudioStatus(mute,
1048 VolumeControlAction.scaleToCustomVolume(volume, maxVolume));
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001049 displayOsd(HdmiControlManager.OSD_MESSAGE_AVR_VOLUME_CHANGED,
1050 mute ? HdmiControlManager.AVR_VOLUME_MUTED : volume);
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001051 }
1052 }
1053
1054 @ServiceThreadOnly
1055 void changeVolume(int curVolume, int delta, int maxVolume) {
1056 assertRunOnServiceThread();
Jungshik Jang377dcbd2014-07-15 15:49:02 +09001057 if (delta == 0 || !isSystemAudioActivated()) {
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001058 return;
1059 }
1060
1061 int targetVolume = curVolume + delta;
1062 int cecVolume = VolumeControlAction.scaleToCecVolume(targetVolume, maxVolume);
1063 synchronized (mLock) {
1064 // If new volume is the same as current system audio volume, just ignore it.
1065 // Note that UNKNOWN_VOLUME is not in range of cec volume scale.
1066 if (cecVolume == mSystemAudioVolume) {
1067 // Update tv volume with system volume value.
1068 mService.setAudioStatus(false,
1069 VolumeControlAction.scaleToCustomVolume(mSystemAudioVolume, maxVolume));
1070 return;
1071 }
1072 }
1073
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001074 List<VolumeControlAction> actions = getActions(VolumeControlAction.class);
1075 if (actions.isEmpty()) {
1076 addAndStartAction(new VolumeControlAction(this,
1077 getAvrDeviceInfo().getLogicalAddress(), delta > 0));
1078 } else {
1079 actions.get(0).handleVolumeChange(delta > 0);
1080 }
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001081 }
1082
1083 @ServiceThreadOnly
1084 void changeMute(boolean mute) {
1085 assertRunOnServiceThread();
Jungshik Jang6b096d32014-09-12 18:49:08 +09001086 HdmiLogger.debug("[A]:Change mute:%b", mute);
Jungshik Jang720407a2014-09-15 17:48:41 +09001087 synchronized (mLock) {
1088 if (mSystemAudioMute == mute) {
1089 HdmiLogger.debug("No need to change mute.");
1090 return;
1091 }
1092 }
Jungshik Jang377dcbd2014-07-15 15:49:02 +09001093 if (!isSystemAudioActivated()) {
Jungshik Jang6b096d32014-09-12 18:49:08 +09001094 HdmiLogger.debug("[A]:System audio is not activated.");
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001095 return;
1096 }
1097
1098 // Remove existing volume action.
1099 removeAction(VolumeControlAction.class);
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001100 sendUserControlPressedAndReleased(getAvrDeviceInfo().getLogicalAddress(),
Jinsuk Kim0ab37792015-10-17 06:53:52 +09001101 HdmiCecKeycode.getMuteKey(mute));
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001102 }
1103
Jungshik Jang79c58a42014-06-16 16:45:36 +09001104 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001105 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001106 protected boolean handleInitiateArc(HdmiCecMessage message) {
1107 assertRunOnServiceThread();
Jungshik Jang339227d2014-08-25 15:37:20 +09001108
1109 if (!canStartArcUpdateAction(message.getSource(), true)) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001110 if (getAvrDeviceInfo() == null) {
1111 // AVR may not have been discovered yet. Delay the message processing.
1112 mDelayedMessageBuffer.add(message);
1113 return true;
1114 }
Jungshik Jang339227d2014-08-25 15:37:20 +09001115 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
1116 if (!isConnectedToArcPort(message.getSource())) {
1117 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
1118 }
1119 return true;
1120 }
1121
Jungshik Jang79c58a42014-06-16 16:45:36 +09001122 // In case where <Initiate Arc> is started by <Request ARC Initiation>
1123 // need to clean up RequestArcInitiationAction.
1124 removeAction(RequestArcInitiationAction.class);
1125 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1126 message.getSource(), true);
1127 addAndStartAction(action);
1128 return true;
1129 }
1130
Jungshik Jang339227d2014-08-25 15:37:20 +09001131 private boolean canStartArcUpdateAction(int avrAddress, boolean shouldCheckArcFeatureEnabled) {
1132 HdmiDeviceInfo avr = getAvrDeviceInfo();
1133 if (avr != null
1134 && (avrAddress == avr.getLogicalAddress())
1135 && isConnectedToArcPort(avr.getPhysicalAddress())
1136 && isDirectConnectAddress(avr.getPhysicalAddress())) {
1137 if (shouldCheckArcFeatureEnabled) {
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +09001138 return isArcFeatureEnabled(avr.getPortId());
Jungshik Jang339227d2014-08-25 15:37:20 +09001139 } else {
1140 return true;
1141 }
1142 } else {
1143 return false;
1144 }
1145 }
1146
Jungshik Jang79c58a42014-06-16 16:45:36 +09001147 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001148 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001149 protected boolean handleTerminateArc(HdmiCecMessage message) {
1150 assertRunOnServiceThread();
Jinsuk Kim7e4b4802015-04-20 13:17:13 +09001151 if (mService .isPowerStandbyOrTransient()) {
1152 setArcStatus(false);
1153 return true;
1154 }
1155 // Do not check ARC configuration since the AVR might have been already removed.
1156 // Clean up RequestArcTerminationAction in case <Terminate Arc> was started by
1157 // <Request ARC Termination>.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001158 removeAction(RequestArcTerminationAction.class);
1159 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1160 message.getSource(), false);
1161 addAndStartAction(action);
1162 return true;
1163 }
1164
1165 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001166 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001167 protected boolean handleSetSystemAudioMode(HdmiCecMessage message) {
1168 assertRunOnServiceThread();
1169 if (!isMessageForSystemAudio(message)) {
Jinsuk Kimad1e3d72015-01-02 13:27:15 +09001170 if (getAvrDeviceInfo() == null) {
1171 // AVR may not have been discovered yet. Delay the message processing.
1172 mDelayedMessageBuffer.add(message);
Donghyun Choc21f63a2016-05-10 10:37:47 +09001173 } else {
1174 HdmiLogger.warning("Invalid <Set System Audio Mode> message:" + message);
1175 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
Jinsuk Kimad1e3d72015-01-02 13:27:15 +09001176 }
Jungshik Jang4480efa2014-09-04 17:08:34 +09001177 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001178 }
Donghyun Choc21f63a2016-05-10 10:37:47 +09001179 removeAction(SystemAudioAutoInitiationAction.class);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001180 SystemAudioActionFromAvr action = new SystemAudioActionFromAvr(this,
Jungshik Jang7f0a1c52014-06-23 16:00:07 +09001181 message.getSource(), HdmiUtils.parseCommandParamSystemAudioStatus(message), null);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001182 addAndStartAction(action);
1183 return true;
1184 }
1185
1186 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001187 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001188 protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) {
1189 assertRunOnServiceThread();
1190 if (!isMessageForSystemAudio(message)) {
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001191 HdmiLogger.warning("Invalid <System Audio Mode Status> message:" + message);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001192 // Ignore this message.
1193 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001194 }
Jinsuk Kim7ecfbae2014-07-11 14:16:29 +09001195 setSystemAudioMode(HdmiUtils.parseCommandParamSystemAudioStatus(message), true);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001196 return true;
1197 }
1198
Jungshik Jangb6591b82014-07-23 16:10:23 +09001199 // Seq #53
1200 @Override
1201 @ServiceThreadOnly
1202 protected boolean handleRecordTvScreen(HdmiCecMessage message) {
1203 List<OneTouchRecordAction> actions = getActions(OneTouchRecordAction.class);
1204 if (!actions.isEmpty()) {
1205 // Assumes only one OneTouchRecordAction.
1206 OneTouchRecordAction action = actions.get(0);
1207 if (action.getRecorderAddress() != message.getSource()) {
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001208 announceOneTouchRecordResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001209 message.getSource(),
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001210 HdmiControlManager.ONE_TOUCH_RECORD_PREVIOUS_RECORDING_IN_PROGRESS);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001211 }
1212 return super.handleRecordTvScreen(message);
1213 }
1214
1215 int recorderAddress = message.getSource();
1216 byte[] recordSource = mService.invokeRecordRequestListener(recorderAddress);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001217 int reason = startOneTouchRecord(recorderAddress, recordSource);
1218 if (reason != Constants.ABORT_NO_ERROR) {
1219 mService.maySendFeatureAbortCommand(message, reason);
1220 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001221 return true;
1222 }
1223
Jungshik Jange5a93372014-07-25 13:41:14 +09001224 @Override
1225 protected boolean handleTimerClearedStatus(HdmiCecMessage message) {
1226 byte[] params = message.getParams();
Jungshik Jange9e0f072014-08-05 17:48:59 +09001227 int timerClearedStatusData = params[0] & 0xFF;
Jungshik Jang326aef02014-11-05 12:50:35 +09001228 announceTimerRecordingResult(message.getSource(), timerClearedStatusData);
Jungshik Jange5a93372014-07-25 13:41:14 +09001229 return true;
1230 }
1231
Jungshik Jang326aef02014-11-05 12:50:35 +09001232 void announceOneTouchRecordResult(int recorderAddress, int result) {
1233 mService.invokeOneTouchRecordResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001234 }
1235
Jungshik Jang326aef02014-11-05 12:50:35 +09001236 void announceTimerRecordingResult(int recorderAddress, int result) {
1237 mService.invokeTimerRecordingResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001238 }
1239
Jungshik Jang326aef02014-11-05 12:50:35 +09001240 void announceClearTimerRecordingResult(int recorderAddress, int result) {
1241 mService.invokeClearTimerRecordingResult(recorderAddress, result);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001242 }
1243
Jungshik Jang79c58a42014-06-16 16:45:36 +09001244 private boolean isMessageForSystemAudio(HdmiCecMessage message) {
Jungshik Jang4480efa2014-09-04 17:08:34 +09001245 return mService.isControlEnabled()
1246 && message.getSource() == Constants.ADDR_AUDIO_SYSTEM
Jungshik Jang473119f2014-08-27 16:43:22 +09001247 && (message.getDestination() == Constants.ADDR_TV
1248 || message.getDestination() == Constants.ADDR_BROADCAST)
1249 && getAvrDeviceInfo() != null;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001250 }
1251
1252 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001253 * Add a new {@link HdmiDeviceInfo}. It returns old device info which has the same
Jungshik Jang79c58a42014-06-16 16:45:36 +09001254 * logical address as new device info's.
1255 *
1256 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1257 *
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001258 * @param deviceInfo a new {@link HdmiDeviceInfo} to be added.
1259 * @return {@code null} if it is new device. Otherwise, returns old {@HdmiDeviceInfo}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001260 * that has the same logical address as new one has.
1261 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001262 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001263 private HdmiDeviceInfo addDeviceInfo(HdmiDeviceInfo deviceInfo) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001264 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001265 HdmiDeviceInfo oldDeviceInfo = getCecDeviceInfo(deviceInfo.getLogicalAddress());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001266 if (oldDeviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001267 removeDeviceInfo(deviceInfo.getId());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001268 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001269 mDeviceInfos.append(deviceInfo.getId(), deviceInfo);
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001270 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001271 return oldDeviceInfo;
1272 }
1273
1274 /**
1275 * Remove a device info corresponding to the given {@code logicalAddress}.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001276 * It returns removed {@link HdmiDeviceInfo} if exists.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001277 *
1278 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1279 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001280 * @param id id of device to be removed
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001281 * @return removed {@link HdmiDeviceInfo} it exists. Otherwise, returns {@code null}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001282 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001283 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001284 private HdmiDeviceInfo removeDeviceInfo(int id) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001285 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001286 HdmiDeviceInfo deviceInfo = mDeviceInfos.get(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001287 if (deviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001288 mDeviceInfos.remove(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001289 }
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001290 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001291 return deviceInfo;
1292 }
1293
1294 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001295 * Return a list of all {@link HdmiDeviceInfo}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001296 *
1297 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
Jungshik Jang339227d2014-08-25 15:37:20 +09001298 * This is not thread-safe. For thread safety, call {@link #getSafeExternalInputsLocked} which
Jungshik Jang8e93c842014-08-06 15:48:33 +09001299 * does not include local device.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001300 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001301 @ServiceThreadOnly
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001302 List<HdmiDeviceInfo> getDeviceInfoList(boolean includeLocalDevice) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001303 assertRunOnServiceThread();
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001304 if (includeLocalDevice) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001305 return HdmiUtils.sparseArrayToList(mDeviceInfos);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001306 } else {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001307 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001308 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001309 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001310 if (!isLocalDeviceAddress(info.getLogicalAddress())) {
1311 infoList.add(info);
1312 }
1313 }
1314 return infoList;
1315 }
1316 }
1317
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001318 /**
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001319 * Return external input devices.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001320 */
Jinsuk Kimed086452014-08-18 15:01:53 +09001321 List<HdmiDeviceInfo> getSafeExternalInputsLocked() {
1322 return mSafeExternalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001323 }
1324
Jungshik Janga5b74142014-06-23 18:03:10 +09001325 @ServiceThreadOnly
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001326 private void updateSafeDeviceInfoList() {
1327 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001328 List<HdmiDeviceInfo> copiedDevices = HdmiUtils.sparseArrayToList(mDeviceInfos);
1329 List<HdmiDeviceInfo> externalInputs = getInputDevices();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001330 synchronized (mLock) {
1331 mSafeAllDeviceInfos = copiedDevices;
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001332 mSafeExternalInputs = externalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001333 }
1334 }
1335
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001336 /**
1337 * Return a list of external cec input (source) devices.
1338 *
1339 * <p>Note that this effectively excludes non-source devices like system audio,
1340 * secondary TV.
1341 */
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001342 private List<HdmiDeviceInfo> getInputDevices() {
1343 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001344 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001345 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001346 if (isLocalDeviceAddress(info.getLogicalAddress())) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001347 continue;
1348 }
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001349 if (info.isSourceType() && !hideDevicesBehindLegacySwitch(info)) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001350 infoList.add(info);
1351 }
1352 }
1353 return infoList;
1354 }
1355
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001356 // Check if we are hiding CEC devices connected to a legacy (non-CEC) switch.
1357 // Returns true if the policy is set to true, and the device to check does not have
1358 // a parent CEC device (which should be the CEC-enabled switch) in the list.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001359 private boolean hideDevicesBehindLegacySwitch(HdmiDeviceInfo info) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001360 return HdmiConfig.HIDE_DEVICES_BEHIND_LEGACY_SWITCH
1361 && !isConnectedToCecSwitch(info.getPhysicalAddress(), mCecSwitches);
1362 }
1363
1364 private static boolean isConnectedToCecSwitch(int path, Collection<Integer> switches) {
1365 for (int switchPath : switches) {
1366 if (isParentPath(switchPath, path)) {
1367 return true;
1368 }
1369 }
1370 return false;
1371 }
1372
1373 private static boolean isParentPath(int parentPath, int childPath) {
1374 // (A000, AB00) (AB00, ABC0), (ABC0, ABCD)
1375 // If child's last non-zero nibble is removed, the result equals to the parent.
1376 for (int i = 0; i <= 12; i += 4) {
1377 int nibble = (childPath >> i) & 0xF;
1378 if (nibble != 0) {
1379 int parentNibble = (parentPath >> i) & 0xF;
1380 return parentNibble == 0 && (childPath >> i+4) == (parentPath >> i+4);
1381 }
1382 }
1383 return false;
1384 }
1385
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001386 private void invokeDeviceEventListener(HdmiDeviceInfo info, int status) {
Jinsuk Kim98d760e2014-12-23 07:01:51 +09001387 if (!hideDevicesBehindLegacySwitch(info)) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001388 mService.invokeDeviceEventListeners(info, status);
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001389 }
1390 }
1391
Jungshik Jang79c58a42014-06-16 16:45:36 +09001392 private boolean isLocalDeviceAddress(int address) {
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001393 return mLocalDeviceAddresses.contains(address);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001394 }
1395
Jungshik Jange9cf1582014-06-23 17:28:58 +09001396 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001397 HdmiDeviceInfo getAvrDeviceInfo() {
Jungshik Jange9cf1582014-06-23 17:28:58 +09001398 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001399 return getCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jange9cf1582014-06-23 17:28:58 +09001400 }
1401
Jungshik Jang79c58a42014-06-16 16:45:36 +09001402 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001403 * Return a {@link HdmiDeviceInfo} corresponding to the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001404 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001405 * This is not thread-safe. For thread safety, call {@link #getSafeCecDeviceInfo(int)}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001406 *
Jungshik Jang339227d2014-08-25 15:37:20 +09001407 * @param logicalAddress logical address of the device to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001408 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001409 * Returns null if no logical address matched
1410 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001411 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001412 HdmiDeviceInfo getCecDeviceInfo(int logicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001413 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001414 return mDeviceInfos.get(HdmiDeviceInfo.idForCecDevice(logicalAddress));
Jungshik Jang79c58a42014-06-16 16:45:36 +09001415 }
1416
Jungshik Jange9cf1582014-06-23 17:28:58 +09001417 boolean hasSystemAudioDevice() {
1418 return getSafeAvrDeviceInfo() != null;
1419 }
1420
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001421 HdmiDeviceInfo getSafeAvrDeviceInfo() {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001422 return getSafeCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001423 }
1424
1425 /**
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001426 * Thread safe version of {@link #getCecDeviceInfo(int)}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001427 *
1428 * @param logicalAddress logical address to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001429 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001430 * Returns null if no logical address matched
1431 */
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001432 HdmiDeviceInfo getSafeCecDeviceInfo(int logicalAddress) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001433 synchronized (mLock) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001434 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1435 if (info.isCecDevice() && info.getLogicalAddress() == logicalAddress) {
1436 return info;
1437 }
1438 }
1439 return null;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001440 }
1441 }
1442
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001443 List<HdmiDeviceInfo> getSafeCecDevicesLocked() {
1444 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
1445 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1446 if (isLocalDeviceAddress(info.getLogicalAddress())) {
1447 continue;
1448 }
1449 infoList.add(info);
1450 }
1451 return infoList;
1452 }
1453
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001454 /**
Jungshik Jang8f2ed352014-07-07 15:02:47 +09001455 * Called when a device is newly added or a new device is detected or
1456 * existing device is updated.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001457 *
1458 * @param info device info of a new device.
1459 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001460 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001461 final void addCecDevice(HdmiDeviceInfo info) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001462 assertRunOnServiceThread();
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001463 HdmiDeviceInfo old = addDeviceInfo(info);
Jinsuk Kim13c030e2014-06-20 13:25:17 +09001464 if (info.getLogicalAddress() == mAddress) {
1465 // The addition of TV device itself should not be notified.
1466 return;
1467 }
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001468 if (old == null) {
1469 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
1470 } else if (!old.equals(info)) {
Jinsuk Kim4fcbf0b2014-12-09 06:48:13 +09001471 invokeDeviceEventListener(old, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
1472 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001473 }
Jungshik Jang79c58a42014-06-16 16:45:36 +09001474 }
1475
1476 /**
1477 * Called when a device is removed or removal of device is detected.
1478 *
1479 * @param address a logical address of a device to be removed
1480 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001481 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001482 final void removeCecDevice(int address) {
1483 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001484 HdmiDeviceInfo info = removeDeviceInfo(HdmiDeviceInfo.idForCecDevice(address));
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001485
Jungshik Jang79c58a42014-06-16 16:45:36 +09001486 mCecMessageCache.flushMessagesFrom(address);
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001487 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001488 }
1489
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001490 @ServiceThreadOnly
1491 void handleRemoveActiveRoutingPath(int path) {
1492 assertRunOnServiceThread();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001493 // Seq #23
1494 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001495 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim546d8672014-11-24 07:30:54 +09001496 startRoutingControl(getActivePath(), newPath, true, null);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001497 }
1498 }
1499
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001500 /**
1501 * Launch routing control process.
1502 *
1503 * @param routingForBootup true if routing control is initiated due to One Touch Play
1504 * or TV power on
1505 */
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001506 @ServiceThreadOnly
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001507 void launchRoutingControl(boolean routingForBootup) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001508 assertRunOnServiceThread();
1509 // Seq #24
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001510 if (getActivePortId() != Constants.INVALID_PORT_ID) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001511 if (!routingForBootup && !isProhibitMode()) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001512 int newPath = mService.portIdToPath(getActivePortId());
1513 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +09001514 startRoutingControl(getActivePath(), newPath, routingForBootup, null);
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001515 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001516 } else {
1517 int activePath = mService.getPhysicalAddress();
1518 setActivePath(activePath);
Jinsuk Kim2ab6d9f2015-01-16 15:49:16 +09001519 if (!routingForBootup
1520 && !mDelayedMessageBuffer.isBuffered(Constants.MESSAGE_ACTIVE_SOURCE)) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001521 mService.sendCecCommand(HdmiCecMessageBuilder.buildActiveSource(mAddress,
1522 activePath));
1523 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001524 }
1525 }
1526
Jungshik Jang79c58a42014-06-16 16:45:36 +09001527 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001528 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
Jungshik Jang79c58a42014-06-16 16:45:36 +09001529 * the given routing path. CEC devices use routing path for its physical address to
1530 * describe the hierarchy of the devices in the network.
1531 *
1532 * @param path routing path or physical address
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001533 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
Jungshik Jang79c58a42014-06-16 16:45:36 +09001534 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001535 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001536 final HdmiDeviceInfo getDeviceInfoByPath(int path) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001537 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001538 for (HdmiDeviceInfo info : getDeviceInfoList(false)) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001539 if (info.getPhysicalAddress() == path) {
1540 return info;
1541 }
1542 }
1543 return null;
1544 }
1545
1546 /**
Jinsuk Kim7640d982015-01-28 16:44:07 +09001547 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
1548 * the given routing path. This is the version accessible safely from threads
1549 * other than service thread.
1550 *
1551 * @param path routing path or physical address
1552 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
1553 */
1554 HdmiDeviceInfo getSafeDeviceInfoByPath(int path) {
1555 synchronized (mLock) {
1556 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1557 if (info.getPhysicalAddress() == path) {
1558 return info;
1559 }
1560 }
1561 return null;
1562 }
1563 }
1564
1565 /**
Jungshik Jang79c58a42014-06-16 16:45:36 +09001566 * Whether a device of the specified physical address and logical address exists
1567 * in a device info list. However, both are minimal condition and it could
1568 * be different device from the original one.
1569 *
Jungshik Jang79c58a42014-06-16 16:45:36 +09001570 * @param logicalAddress logical address of a device to be searched
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001571 * @param physicalAddress physical address of a device to be searched
Jungshik Jang79c58a42014-06-16 16:45:36 +09001572 * @return true if exist; otherwise false
1573 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001574 @ServiceThreadOnly
Jinsuk Kim7cd4a582015-01-29 09:50:25 +09001575 boolean isInDeviceList(int logicalAddress, int physicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001576 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001577 HdmiDeviceInfo device = getCecDeviceInfo(logicalAddress);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001578 if (device == null) {
1579 return false;
1580 }
1581 return device.getPhysicalAddress() == physicalAddress;
1582 }
1583
1584 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001585 @ServiceThreadOnly
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001586 void onHotplug(int portId, boolean connected) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001587 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001588
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001589 if (!connected) {
1590 removeCecSwitches(portId);
1591 }
Jungshik Jang79c58a42014-06-16 16:45:36 +09001592 // Tv device will have permanent HotplugDetectionAction.
1593 List<HotplugDetectionAction> hotplugActions = getActions(HotplugDetectionAction.class);
1594 if (!hotplugActions.isEmpty()) {
1595 // Note that hotplug action is single action running on a machine.
1596 // "pollAllDevicesNow" cleans up timer and start poll action immediately.
Jungshik Jang24c23c12014-07-07 18:04:39 +09001597 // It covers seq #40, #43.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001598 hotplugActions.get(0).pollAllDevicesNow();
1599 }
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +09001600 updateArcFeatureStatus(portId, connected);
Jungshik Jang60cffce2014-06-12 18:03:04 +09001601 }
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001602
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001603 private void removeCecSwitches(int portId) {
1604 Iterator<Integer> it = mCecSwitches.iterator();
1605 while (!it.hasNext()) {
1606 int path = it.next();
1607 if (pathToPortId(path) == portId) {
1608 it.remove();
1609 }
1610 }
1611 }
1612
Jinsuk Kime6e8f3d2015-05-11 14:17:04 +09001613 @Override
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001614 @ServiceThreadOnly
1615 void setAutoDeviceOff(boolean enabled) {
1616 assertRunOnServiceThread();
1617 mAutoDeviceOff = enabled;
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001618 }
1619
1620 @ServiceThreadOnly
1621 void setAutoWakeup(boolean enabled) {
1622 assertRunOnServiceThread();
1623 mAutoWakeup = enabled;
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001624 }
Yuncheol Heo38db6292014-07-01 14:15:14 +09001625
Yuncheol Heo25c20292014-07-31 17:59:39 +09001626 @ServiceThreadOnly
1627 boolean getAutoWakeup() {
1628 assertRunOnServiceThread();
1629 return mAutoWakeup;
1630 }
1631
Yuncheol Heo38db6292014-07-01 14:15:14 +09001632 @Override
1633 @ServiceThreadOnly
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001634 protected void disableDevice(boolean initiatedByCec, PendingActionClearedCallback callback) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001635 assertRunOnServiceThread();
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001636 mService.unregisterTvInputCallback(mTvInputCallback);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001637 // Remove any repeated working actions.
1638 // HotplugDetectionAction will be reinstated during the wake up process.
1639 // HdmiControlService.onWakeUp() -> initializeLocalDevices() ->
1640 // LocalDeviceTv.onAddressAllocated() -> launchDeviceDiscovery().
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001641 removeAction(DeviceDiscoveryAction.class);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001642 removeAction(HotplugDetectionAction.class);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001643 removeAction(PowerStatusMonitorAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001644 // Remove recording actions.
Jungshik Jangb6591b82014-07-23 16:10:23 +09001645 removeAction(OneTouchRecordAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001646 removeAction(TimerRecordingAction.class);
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001647
1648 disableSystemAudioIfExist();
1649 disableArcIfExist();
Jinsuk Kima5445ce2015-03-30 17:14:58 +09001650
1651 super.disableDevice(initiatedByCec, callback);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +09001652 clearDeviceInfoList();
Jinsuk Kim7cc51c62015-04-30 10:39:35 +09001653 getActiveSource().invalidate();
1654 setActivePath(Constants.INVALID_PHYSICAL_ADDRESS);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001655 checkIfPendingActionsCleared();
1656 }
1657
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001658 @ServiceThreadOnly
1659 private void disableSystemAudioIfExist() {
1660 assertRunOnServiceThread();
1661 if (getAvrDeviceInfo() == null) {
1662 return;
1663 }
1664
1665 // Seq #31.
1666 removeAction(SystemAudioActionFromAvr.class);
1667 removeAction(SystemAudioActionFromTv.class);
1668 removeAction(SystemAudioAutoInitiationAction.class);
1669 removeAction(SystemAudioStatusAction.class);
1670 removeAction(VolumeControlAction.class);
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001671 }
1672
1673 @ServiceThreadOnly
1674 private void disableArcIfExist() {
1675 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001676 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001677 if (avr == null) {
1678 return;
1679 }
1680
1681 // Seq #44.
1682 removeAction(RequestArcInitiationAction.class);
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +09001683 if (!hasAction(RequestArcTerminationAction.class) && isArcEstablished()) {
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001684 addAndStartAction(new RequestArcTerminationAction(this, avr.getLogicalAddress()));
1685 }
1686 }
1687
Yuncheol Heo38db6292014-07-01 14:15:14 +09001688 @Override
1689 @ServiceThreadOnly
Jinsuk Kime6e8f3d2015-05-11 14:17:04 +09001690 protected void onStandby(boolean initiatedByCec, int standbyAction) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001691 assertRunOnServiceThread();
1692 // Seq #11
1693 if (!mService.isControlEnabled()) {
1694 return;
1695 }
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001696 if (!initiatedByCec && mAutoDeviceOff) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001697 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001698 mAddress, Constants.ADDR_BROADCAST));
Yuncheol Heo38db6292014-07-01 14:15:14 +09001699 }
1700 }
1701
Jinsuk Kim4d43d932014-07-03 16:43:58 +09001702 boolean isProhibitMode() {
1703 return mService.isProhibitMode();
1704 }
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001705
1706 boolean isPowerStandbyOrTransient() {
1707 return mService.isPowerStandbyOrTransient();
Jungshik Jang8866c812014-07-08 14:42:28 +09001708 }
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001709
Jungshik Jang339227d2014-08-25 15:37:20 +09001710 @ServiceThreadOnly
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001711 void displayOsd(int messageId) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001712 assertRunOnServiceThread();
1713 mService.displayOsd(messageId);
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001714 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001715
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001716 @ServiceThreadOnly
1717 void displayOsd(int messageId, int extra) {
1718 assertRunOnServiceThread();
1719 mService.displayOsd(messageId, extra);
1720 }
1721
Jungshik Jangb6591b82014-07-23 16:10:23 +09001722 // Seq #54 and #55
1723 @ServiceThreadOnly
Jungshik Jang4480efa2014-09-04 17:08:34 +09001724 int startOneTouchRecord(int recorderAddress, byte[] recordSource) {
Jungshik Jangb6591b82014-07-23 16:10:23 +09001725 assertRunOnServiceThread();
1726 if (!mService.isControlEnabled()) {
1727 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001728 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001729 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001730 }
1731
1732 if (!checkRecorder(recorderAddress)) {
1733 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001734 announceOneTouchRecordResult(recorderAddress,
1735 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001736 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001737 }
1738
1739 if (!checkRecordSource(recordSource)) {
1740 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001741 announceOneTouchRecordResult(recorderAddress,
1742 ONE_TOUCH_RECORD_FAIL_TO_RECORD_DISPLAYED_SCREEN);
Jinsuk Kimd47abef2015-01-17 07:38:24 +09001743 return Constants.ABORT_CANNOT_PROVIDE_SOURCE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001744 }
1745
1746 addAndStartAction(new OneTouchRecordAction(this, recorderAddress, recordSource));
1747 Slog.i(TAG, "Start new [One Touch Record]-Target:" + recorderAddress + ", recordSource:"
1748 + Arrays.toString(recordSource));
Jungshik Jang4480efa2014-09-04 17:08:34 +09001749 return Constants.ABORT_NO_ERROR;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001750 }
1751
1752 @ServiceThreadOnly
1753 void stopOneTouchRecord(int recorderAddress) {
1754 assertRunOnServiceThread();
1755 if (!mService.isControlEnabled()) {
1756 Slog.w(TAG, "Can not stop one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001757 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001758 return;
1759 }
1760
1761 if (!checkRecorder(recorderAddress)) {
1762 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001763 announceOneTouchRecordResult(recorderAddress,
1764 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001765 return;
1766 }
1767
1768 // Remove one touch record action so that other one touch record can be started.
1769 removeAction(OneTouchRecordAction.class);
1770 mService.sendCecCommand(HdmiCecMessageBuilder.buildRecordOff(mAddress, recorderAddress));
1771 Slog.i(TAG, "Stop [One Touch Record]-Target:" + recorderAddress);
1772 }
1773
1774 private boolean checkRecorder(int recorderAddress) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001775 HdmiDeviceInfo device = getCecDeviceInfo(recorderAddress);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001776 return (device != null)
1777 && (HdmiUtils.getTypeFromAddress(recorderAddress)
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001778 == HdmiDeviceInfo.DEVICE_RECORDER);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001779 }
1780
1781 private boolean checkRecordSource(byte[] recordSource) {
1782 return (recordSource != null) && HdmiRecordSources.checkRecordSource(recordSource);
1783 }
1784
1785 @ServiceThreadOnly
1786 void startTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1787 assertRunOnServiceThread();
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001788 if (!mService.isControlEnabled()) {
1789 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001790 announceTimerRecordingResult(recorderAddress,
1791 TIMER_RECORDING_RESULT_EXTRA_CEC_DISABLED);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001792 return;
1793 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001794
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001795 if (!checkRecorder(recorderAddress)) {
1796 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001797 announceTimerRecordingResult(recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001798 TIMER_RECORDING_RESULT_EXTRA_CHECK_RECORDER_CONNECTION);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001799 return;
1800 }
1801
1802 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1803 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
1804 announceTimerRecordingResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001805 recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001806 TIMER_RECORDING_RESULT_EXTRA_FAIL_TO_RECORD_SELECTED_SOURCE);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001807 return;
1808 }
1809
1810 addAndStartAction(
1811 new TimerRecordingAction(this, recorderAddress, sourceType, recordSource));
1812 Slog.i(TAG, "Start [Timer Recording]-Target:" + recorderAddress + ", SourceType:"
1813 + sourceType + ", RecordSource:" + Arrays.toString(recordSource));
1814 }
1815
1816 private boolean checkTimerRecordingSource(int sourceType, byte[] recordSource) {
1817 return (recordSource != null)
1818 && HdmiTimerRecordSources.checkTimerRecordSource(sourceType, recordSource);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001819 }
1820
1821 @ServiceThreadOnly
1822 void clearTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1823 assertRunOnServiceThread();
Jungshik Jange5a93372014-07-25 13:41:14 +09001824 if (!mService.isControlEnabled()) {
1825 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001826 announceClearTimerRecordingResult(recorderAddress, CLEAR_TIMER_STATUS_CEC_DISABLE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001827 return;
1828 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001829
Jungshik Jange5a93372014-07-25 13:41:14 +09001830 if (!checkRecorder(recorderAddress)) {
1831 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001832 announceClearTimerRecordingResult(recorderAddress,
1833 CLEAR_TIMER_STATUS_CHECK_RECORDER_CONNECTION);
Jungshik Jange5a93372014-07-25 13:41:14 +09001834 return;
1835 }
1836
1837 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1838 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001839 announceClearTimerRecordingResult(recorderAddress,
1840 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001841 return;
1842 }
1843
1844 sendClearTimerMessage(recorderAddress, sourceType, recordSource);
1845 }
1846
Jungshik Jang326aef02014-11-05 12:50:35 +09001847 private void sendClearTimerMessage(final int recorderAddress, int sourceType,
1848 byte[] recordSource) {
Jungshik Jange5a93372014-07-25 13:41:14 +09001849 HdmiCecMessage message = null;
1850 switch (sourceType) {
1851 case TIMER_RECORDING_TYPE_DIGITAL:
1852 message = HdmiCecMessageBuilder.buildClearDigitalTimer(mAddress, recorderAddress,
1853 recordSource);
1854 break;
1855 case TIMER_RECORDING_TYPE_ANALOGUE:
1856 message = HdmiCecMessageBuilder.buildClearAnalogueTimer(mAddress, recorderAddress,
1857 recordSource);
1858 break;
1859 case TIMER_RECORDING_TYPE_EXTERNAL:
1860 message = HdmiCecMessageBuilder.buildClearExternalTimer(mAddress, recorderAddress,
1861 recordSource);
1862 break;
1863 default:
1864 Slog.w(TAG, "Invalid source type:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001865 announceClearTimerRecordingResult(recorderAddress,
1866 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001867 return;
1868
1869 }
1870 mService.sendCecCommand(message, new SendMessageCallback() {
1871 @Override
1872 public void onSendCompleted(int error) {
1873 if (error != Constants.SEND_RESULT_SUCCESS) {
Jungshik Jang326aef02014-11-05 12:50:35 +09001874 announceClearTimerRecordingResult(recorderAddress,
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001875 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001876 }
1877 }
1878 });
Jungshik Jangb6591b82014-07-23 16:10:23 +09001879 }
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001880
1881 void updateDevicePowerStatus(int logicalAddress, int newPowerStatus) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001882 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001883 if (info == null) {
1884 Slog.w(TAG, "Can not update power status of non-existing device:" + logicalAddress);
1885 return;
1886 }
1887
1888 if (info.getDevicePowerStatus() == newPowerStatus) {
1889 return;
1890 }
1891
1892 HdmiDeviceInfo newInfo = HdmiUtils.cloneHdmiDeviceInfo(info, newPowerStatus);
1893 // addDeviceInfo replaces old device info with new one if exists.
1894 addDeviceInfo(newInfo);
1895
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001896 invokeDeviceEventListener(newInfo, HdmiControlManager.DEVICE_EVENT_UPDATE_DEVICE);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001897 }
Terry Heo959d2db2014-08-28 16:45:41 +09001898
1899 @Override
Yuncheol Heo184b1242014-09-12 15:09:07 +09001900 protected boolean handleMenuStatus(HdmiCecMessage message) {
1901 // Do nothing and just return true not to prevent from responding <Feature Abort>.
1902 return true;
1903 }
1904
1905 @Override
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001906 protected void sendStandby(int deviceId) {
1907 HdmiDeviceInfo targetDevice = mDeviceInfos.get(deviceId);
1908 if (targetDevice == null) {
1909 return;
1910 }
1911 int targetAddress = targetDevice.getLogicalAddress();
1912 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(mAddress, targetAddress));
1913 }
1914
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001915 @ServiceThreadOnly
1916 void processAllDelayedMessages() {
1917 assertRunOnServiceThread();
1918 mDelayedMessageBuffer.processAllMessages();
1919 }
1920
1921 @ServiceThreadOnly
1922 void processDelayedMessages(int address) {
1923 assertRunOnServiceThread();
1924 mDelayedMessageBuffer.processMessagesForDevice(address);
1925 }
1926
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +09001927 @ServiceThreadOnly
1928 void processDelayedActiveSource(int address) {
1929 assertRunOnServiceThread();
1930 mDelayedMessageBuffer.processActiveSource(address);
1931 }
1932
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001933 @Override
Terry Heo959d2db2014-08-28 16:45:41 +09001934 protected void dump(final IndentingPrintWriter pw) {
1935 super.dump(pw);
1936 pw.println("mArcEstablished: " + mArcEstablished);
1937 pw.println("mArcFeatureEnabled: " + mArcFeatureEnabled);
1938 pw.println("mSystemAudioActivated: " + mSystemAudioActivated);
1939 pw.println("mSystemAudioMute: " + mSystemAudioMute);
1940 pw.println("mAutoDeviceOff: " + mAutoDeviceOff);
1941 pw.println("mAutoWakeup: " + mAutoWakeup);
1942 pw.println("mSkipRoutingControl: " + mSkipRoutingControl);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +09001943 pw.println("mPrevPortId: " + mPrevPortId);
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001944 pw.println("CEC devices:");
1945 pw.increaseIndent();
1946 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1947 pw.println(info);
1948 }
1949 pw.decreaseIndent();
Terry Heo959d2db2014-08-28 16:45:41 +09001950 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +09001951}