blob: 4ac2b48e377a8bb785f682391a1faa17b0b9e4fd [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);
141 HdmiDeviceInfo info = tvInfo.getHdmiDeviceInfo();
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900142 if (info == null) return;
143 addTvInput(inputId, info.getId());
144 if (info.isCecDevice()) {
145 processDelayedActiveSource(info.getLogicalAddress());
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900146 }
147 }
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900148
149 @Override
150 public void onInputRemoved(String inputId) {
151 removeTvInput(inputId);
152 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900153 };
154
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900155 // Keeps the mapping (TV input ID, HDMI device ID) to keep track of the TV inputs ready to
156 // accept input switching request from HDMI devices. Requests for which the corresponding
157 // input ID is not yet registered by TV input framework need to be buffered for delayed
158 // processing.
159 private final HashMap<String, Integer> mTvInputs = new HashMap<>();
160
161 @ServiceThreadOnly
162 private void addTvInput(String inputId, int deviceId) {
163 assertRunOnServiceThread();
164 mTvInputs.put(inputId, deviceId);
165 }
166
167 @ServiceThreadOnly
168 private void removeTvInput(String inputId) {
169 assertRunOnServiceThread();
170 mTvInputs.remove(inputId);
171 }
172
173 @Override
174 @ServiceThreadOnly
175 protected boolean isInputReady(int deviceId) {
176 assertRunOnServiceThread();
177 return mTvInputs.containsValue(deviceId);
178 }
179
Jungshik Jang3ee65722014-06-03 16:22:30 +0900180 HdmiCecLocalDeviceTv(HdmiControlService service) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900181 super(service, HdmiDeviceInfo.DEVICE_TV);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900182 mPrevPortId = Constants.INVALID_PORT_ID;
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900183 mAutoDeviceOff = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED,
184 true);
185 mAutoWakeup = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_WAKEUP_ENABLED, true);
Yuncheol Heo25c20292014-07-31 17:59:39 +0900186 mStandbyHandler = new HdmiCecStandbyModeHandler(service, this);
Jungshik Jang8b308d92014-05-29 21:52:28 +0900187 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +0900188
Jungshik Jang8b308d92014-05-29 21:52:28 +0900189 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900190 @ServiceThreadOnly
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900191 protected void onAddressAllocated(int logicalAddress, int reason) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900192 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900193 List<HdmiPortInfo> ports = mService.getPortInfo();
194 for (HdmiPortInfo port : ports) {
195 mArcFeatureEnabled.put(port.getId(), port.isArcSupported());
196 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900197 mService.registerTvInputCallback(mTvInputCallback);
Jungshik Jang3ee65722014-06-03 16:22:30 +0900198 mService.sendCecCommand(HdmiCecMessageBuilder.buildReportPhysicalAddressCommand(
199 mAddress, mService.getPhysicalAddress(), mDeviceType));
200 mService.sendCecCommand(HdmiCecMessageBuilder.buildDeviceVendorIdCommand(
201 mAddress, mService.getVendorId()));
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900202 mCecSwitches.add(mService.getPhysicalAddress()); // TV is a CEC switch too.
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900203 mTvInputs.clear();
Jinsuk Kimd5307192014-08-22 09:07:40 +0900204 mSkipRoutingControl = (reason == HdmiControlService.INITIATED_BY_WAKE_UP_MESSAGE);
Yuncheol Heofc44e4e2014-08-04 19:41:09 +0900205 launchRoutingControl(reason != HdmiControlService.INITIATED_BY_ENABLE_CEC &&
206 reason != HdmiControlService.INITIATED_BY_BOOT_UP);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900207 mLocalDeviceAddresses = initLocalDeviceAddresses();
Jungshik Jang60cffce2014-06-12 18:03:04 +0900208 launchDeviceDiscovery();
Jungshik Jang6f34f5a2014-07-08 21:17:29 +0900209 }
210
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900211
212 @ServiceThreadOnly
213 private List<Integer> initLocalDeviceAddresses() {
214 assertRunOnServiceThread();
215 List<Integer> addresses = new ArrayList<>();
216 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
217 addresses.add(device.getDeviceInfo().getLogicalAddress());
218 }
219 return Collections.unmodifiableList(addresses);
220 }
221
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900222 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900223 protected int getPreferredAddress() {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900224 return Constants.ADDR_TV;
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900225 }
226
227 @Override
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900228 protected void setPreferredAddress(int addr) {
Jinsuk Kimd47abef2015-01-17 07:38:24 +0900229 Slog.w(TAG, "Preferred addres will not be stored for TV");
Jinsuk Kimaf2acf02014-07-11 18:43:04 +0900230 }
231
Yuncheol Heo25c20292014-07-31 17:59:39 +0900232 @Override
233 @ServiceThreadOnly
234 boolean dispatchMessage(HdmiCecMessage message) {
235 assertRunOnServiceThread();
236 if (mService.isPowerStandby() && mStandbyHandler.handleCommand(message)) {
237 return true;
238 }
239 return super.onMessage(message);
240 }
241
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900242 /**
243 * Performs the action 'device select', or 'one touch play' initiated by TV.
244 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900245 * @param id id of HDMI device to select
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900246 * @param callback callback object to report the result with
247 */
Jungshik Janga5b74142014-06-23 18:03:10 +0900248 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900249 void deviceSelect(int id, IHdmiControlCallback callback) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900250 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900251 HdmiDeviceInfo targetDevice = mDeviceInfos.get(id);
252 if (targetDevice == null) {
253 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
254 return;
255 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900256 int targetAddress = targetDevice.getLogicalAddress();
Jinsuk Kim58500f42014-08-05 12:48:35 +0900257 ActiveSource active = getActiveSource();
Yu.Ishihara6b2a6172015-03-25 09:44:06 +0900258 if (targetDevice.getDevicePowerStatus() == HdmiControlManager.POWER_STATUS_ON
259 && active.isValid()
260 && targetAddress == active.logicalAddress) {
Jinsuk Kim58500f42014-08-05 12:48:35 +0900261 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
262 return;
263 }
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900264 if (targetAddress == Constants.ADDR_INTERNAL) {
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900265 handleSelectInternalSource();
266 // Switching to internal source is always successful even when CEC control is disabled.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900267 setActiveSource(targetAddress, mService.getPhysicalAddress());
Jinsuk Kim7e742062014-07-30 13:19:13 +0900268 setActivePath(mService.getPhysicalAddress());
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900269 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
Jinsuk Kim83335712014-06-24 07:57:00 +0900270 return;
271 }
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900272 if (!mService.isControlEnabled()) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900273 setActiveSource(targetDevice);
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900274 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
275 return;
276 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900277 removeAction(DeviceSelectAction.class);
278 addAndStartAction(new DeviceSelectAction(this, targetDevice, callback));
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900279 }
280
Jungshik Janga5b74142014-06-23 18:03:10 +0900281 @ServiceThreadOnly
Jinsuk Kim5ad57162014-07-21 13:31:45 +0900282 private void handleSelectInternalSource() {
Jinsuk Kima062a932014-06-18 10:00:39 +0900283 assertRunOnServiceThread();
Jinsuk Kim83335712014-06-24 07:57:00 +0900284 // Seq #18
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900285 if (mService.isControlEnabled() && mActiveSource.logicalAddress != mAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900286 updateActiveSource(mAddress, mService.getPhysicalAddress());
Jinsuk Kimd5307192014-08-22 09:07:40 +0900287 if (mSkipRoutingControl) {
288 mSkipRoutingControl = false;
289 return;
290 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900291 HdmiCecMessage activeSource = HdmiCecMessageBuilder.buildActiveSource(
292 mAddress, mService.getPhysicalAddress());
293 mService.sendCecCommand(activeSource);
294 }
295 }
296
297 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900298 void updateActiveSource(int logicalAddress, int physicalAddress) {
299 assertRunOnServiceThread();
300 updateActiveSource(ActiveSource.of(logicalAddress, physicalAddress));
301 }
302
303 @ServiceThreadOnly
304 void updateActiveSource(ActiveSource newActive) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900305 assertRunOnServiceThread();
306 // Seq #14
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900307 if (mActiveSource.equals(newActive)) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900308 return;
309 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900310 setActiveSource(newActive);
311 int logicalAddress = newActive.logicalAddress;
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900312 if (getCecDeviceInfo(logicalAddress) != null && logicalAddress != mAddress) {
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900313 if (mService.pathToPortId(newActive.physicalAddress) == getActivePortId()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900314 setPrevPortId(getActivePortId());
315 }
316 // TODO: Show the OSD banner related to the new active source device.
317 } else {
318 // TODO: If displayed, remove the OSD banner related to the previous
319 // active source device.
320 }
321 }
322
Jinsuk Kim2b152012014-07-25 08:22:26 +0900323 int getPortId(int physicalAddress) {
324 return mService.pathToPortId(physicalAddress);
325 }
326
Jinsuk Kim83335712014-06-24 07:57:00 +0900327 /**
328 * Returns the previous port id kept to handle input switching on <Inactive Source>.
329 */
330 int getPrevPortId() {
331 synchronized (mLock) {
332 return mPrevPortId;
333 }
334 }
335
336 /**
337 * Sets the previous port id. INVALID_PORT_ID invalidates it, hence no actions will be
338 * taken for <Inactive Source>.
339 */
340 void setPrevPortId(int portId) {
341 synchronized (mLock) {
342 mPrevPortId = portId;
343 }
344 }
345
346 @ServiceThreadOnly
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900347 void updateActiveInput(int path, boolean notifyInputChange) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900348 assertRunOnServiceThread();
349 // Seq #15
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900350 setActivePath(path);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900351 // TODO: Handle PAP/PIP case.
352 // Show OSD port change banner
353 if (notifyInputChange) {
354 ActiveSource activeSource = getActiveSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900355 HdmiDeviceInfo info = getCecDeviceInfo(activeSource.logicalAddress);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900356 if (info == null) {
Jinsuk Kim6ad7cbd2015-01-06 11:30:56 +0900357 info = mService.getDeviceInfoByPort(getActivePortId());
358 if (info == null) {
359 // No CEC/MHL device is present at the port. Attempt to switch to
360 // the hardware port itself for non-CEC devices that may be connected.
361 info = new HdmiDeviceInfo(path, getActivePortId());
362 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900363 }
364 mService.invokeInputChangeListener(info);
365 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900366 }
367
368 @ServiceThreadOnly
369 void doManualPortSwitching(int portId, IHdmiControlCallback callback) {
370 assertRunOnServiceThread();
371 // Seq #20
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900372 if (!mService.isValidPortId(portId)) {
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900373 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
Jinsuk Kima062a932014-06-18 10:00:39 +0900374 return;
375 }
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900376 if (portId == getActivePortId()) {
377 invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS);
378 return;
379 }
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900380 mActiveSource.invalidate();
Jinsuk Kim09ffc842014-07-11 17:04:32 +0900381 if (!mService.isControlEnabled()) {
382 setActivePortId(portId);
383 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
384 return;
385 }
Jinsuk Kim1f8d1c572014-08-12 15:39:53 +0900386 int oldPath = getActivePortId() != Constants.INVALID_PORT_ID
387 ? mService.portIdToPath(getActivePortId()) : getDeviceInfo().getPhysicalAddress();
Jinsuk Kimd5307192014-08-22 09:07:40 +0900388 setActivePath(oldPath);
389 if (mSkipRoutingControl) {
390 mSkipRoutingControl = false;
391 return;
392 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900393 int newPath = mService.portIdToPath(portId);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900394 startRoutingControl(oldPath, newPath, true, callback);
395 }
396
397 @ServiceThreadOnly
398 void startRoutingControl(int oldPath, int newPath, boolean queryDevicePowerStatus,
399 IHdmiControlCallback callback) {
400 assertRunOnServiceThread();
401 if (oldPath == newPath) {
402 return;
403 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900404 HdmiCecMessage routingChange =
405 HdmiCecMessageBuilder.buildRoutingChange(mAddress, oldPath, newPath);
406 mService.sendCecCommand(routingChange);
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900407 removeAction(RoutingControlAction.class);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900408 addAndStartAction(
409 new RoutingControlAction(this, newPath, queryDevicePowerStatus, callback));
Jinsuk Kima062a932014-06-18 10:00:39 +0900410 }
411
Jungshik Jangf67113f2014-08-22 16:27:19 +0900412 @ServiceThreadOnly
Jinsuk Kimcae66272014-07-04 13:26:44 +0900413 int getPowerStatus() {
Jungshik Jangf67113f2014-08-22 16:27:19 +0900414 assertRunOnServiceThread();
Jinsuk Kimcae66272014-07-04 13:26:44 +0900415 return mService.getPowerStatus();
416 }
417
Jinsuk Kima062a932014-06-18 10:00:39 +0900418 /**
419 * Sends key to a target CEC device.
420 *
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900421 * @param keyCode key code to send. Defined in {@link android.view.KeyEvent}.
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900422 * @param isPressed true if this is key press event
Jinsuk Kima062a932014-06-18 10:00:39 +0900423 */
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900424 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900425 @ServiceThreadOnly
Jinsuk Kimc068bb52014-07-07 16:59:20 +0900426 protected void sendKeyEvent(int keyCode, boolean isPressed) {
Jinsuk Kima062a932014-06-18 10:00:39 +0900427 assertRunOnServiceThread();
Dongil Seob64c2ba2014-08-26 19:41:39 +0900428 if (!HdmiCecKeycode.isSupportedKeycode(keyCode)) {
429 Slog.w(TAG, "Unsupported key: " + keyCode);
430 return;
431 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900432 List<SendKeyAction> action = getActions(SendKeyAction.class);
Jinsuk Kim454fab52015-01-29 16:38:36 +0900433 int logicalAddress = findKeyReceiverAddress();
434 if (logicalAddress == mAddress) {
435 Slog.w(TAG, "Discard key event to itself :" + keyCode + " pressed:" + isPressed);
436 return;
437 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900438 if (!action.isEmpty()) {
439 action.get(0).processKeyEvent(keyCode, isPressed);
440 } else {
Jinsuk Kim75434972014-09-25 13:52:50 +0900441 if (isPressed) {
Jinsuk Kim75434972014-09-25 13:52:50 +0900442 if (logicalAddress != Constants.ADDR_INVALID) {
443 addAndStartAction(new SendKeyAction(this, logicalAddress, keyCode));
444 return;
445 }
Jinsuk Kima062a932014-06-18 10:00:39 +0900446 }
Jinsuk Kim75434972014-09-25 13:52:50 +0900447 Slog.w(TAG, "Discard key event: " + keyCode + " pressed:" + isPressed);
Jinsuk Kima062a932014-06-18 10:00:39 +0900448 }
449 }
450
Jinsuk Kim75434972014-09-25 13:52:50 +0900451 private int findKeyReceiverAddress() {
452 if (getActiveSource().isValid()) {
453 return getActiveSource().logicalAddress;
454 }
455 HdmiDeviceInfo info = getDeviceInfoByPath(getActivePath());
456 if (info != null) {
457 return info.getLogicalAddress();
458 }
459 return Constants.ADDR_INVALID;
460 }
461
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900462 private static void invokeCallback(IHdmiControlCallback callback, int result) {
Jinsuk Kim4893c7e2014-06-19 14:13:22 +0900463 if (callback == null) {
464 return;
465 }
Jinsuk Kima6ce7702014-05-11 06:54:49 +0900466 try {
467 callback.onComplete(result);
468 } catch (RemoteException e) {
469 Slog.e(TAG, "Invoking callback failed:" + e);
470 }
471 }
472
Jungshik Jang092b4452014-06-11 15:19:17 +0900473 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900474 @ServiceThreadOnly
Jinsuk Kim83335712014-06-24 07:57:00 +0900475 protected boolean handleActiveSource(HdmiCecMessage message) {
476 assertRunOnServiceThread();
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900477 int logicalAddress = message.getSource();
478 int physicalAddress = HdmiUtils.twoBytesToInt(message.getParams());
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900479 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
480 if (info == null) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900481 if (!handleNewDeviceAtTheTailOfActivePath(physicalAddress)) {
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900482 HdmiLogger.debug("Device info %X not found; buffering the command", logicalAddress);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900483 mDelayedMessageBuffer.add(message);
484 }
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +0900485 } else if (!isInputReady(info.getId())) {
486 HdmiLogger.debug("Input not ready for device: %X; buffering the command", info.getId());
487 mDelayedMessageBuffer.add(message);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900488 } else {
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900489 ActiveSource activeSource = ActiveSource.of(logicalAddress, physicalAddress);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900490 ActiveSourceHandler.create(this, null).process(activeSource, info.getDeviceType());
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900491 }
Jinsuk Kim83335712014-06-24 07:57:00 +0900492 return true;
493 }
494
495 @Override
496 @ServiceThreadOnly
497 protected boolean handleInactiveSource(HdmiCecMessage message) {
498 assertRunOnServiceThread();
499 // Seq #10
500
501 // Ignore <Inactive Source> from non-active source device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900502 if (getActiveSource().logicalAddress != message.getSource()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900503 return true;
504 }
Jinsuk Kim4d43d932014-07-03 16:43:58 +0900505 if (isProhibitMode()) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900506 return true;
507 }
508 int portId = getPrevPortId();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900509 if (portId != Constants.INVALID_PORT_ID) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900510 // TODO: Do this only if TV is not showing multiview like PIP/PAP.
511
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900512 HdmiDeviceInfo inactiveSource = getCecDeviceInfo(message.getSource());
Jinsuk Kim83335712014-06-24 07:57:00 +0900513 if (inactiveSource == null) {
514 return true;
515 }
516 if (mService.pathToPortId(inactiveSource.getPhysicalAddress()) == portId) {
517 return true;
518 }
519 // TODO: Switch the TV freeze mode off
520
Jinsuk Kim83335712014-06-24 07:57:00 +0900521 doManualPortSwitching(portId, null);
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900522 setPrevPortId(Constants.INVALID_PORT_ID);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +0900523 } else {
524 // No HDMI port to switch to was found. Notify the input change listers to
525 // switch to the lastly shown internal input.
526 mActiveSource.invalidate();
527 setActivePath(Constants.INVALID_PHYSICAL_ADDRESS);
528 mService.invokeInputChangeListener(HdmiDeviceInfo.INACTIVE_DEVICE);
Jinsuk Kim83335712014-06-24 07:57:00 +0900529 }
530 return true;
531 }
532
533 @Override
534 @ServiceThreadOnly
535 protected boolean handleRequestActiveSource(HdmiCecMessage message) {
536 assertRunOnServiceThread();
537 // Seq #19
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900538 if (mAddress == getActiveSource().logicalAddress) {
Jinsuk Kim83335712014-06-24 07:57:00 +0900539 mService.sendCecCommand(
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900540 HdmiCecMessageBuilder.buildActiveSource(mAddress, getActivePath()));
Jinsuk Kim83335712014-06-24 07:57:00 +0900541 }
542 return true;
543 }
544
545 @Override
546 @ServiceThreadOnly
Jungshik Jang092b4452014-06-11 15:19:17 +0900547 protected boolean handleGetMenuLanguage(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900548 assertRunOnServiceThread();
Jungshik Jangf67113f2014-08-22 16:27:19 +0900549 if (!broadcastMenuLanguage(mService.getLanguage())) {
Jungshik Jang092b4452014-06-11 15:19:17 +0900550 Slog.w(TAG, "Failed to respond to <Get Menu Language>: " + message.toString());
551 }
552 return true;
553 }
554
Terry Heo1ca0a432014-08-18 10:30:32 +0900555 @ServiceThreadOnly
556 boolean broadcastMenuLanguage(String language) {
557 assertRunOnServiceThread();
558 HdmiCecMessage command = HdmiCecMessageBuilder.buildSetMenuLanguageCommand(
559 mAddress, language);
560 if (command != null) {
561 mService.sendCecCommand(command);
562 return true;
563 }
564 return false;
565 }
566
Jungshik Jang60cffce2014-06-12 18:03:04 +0900567 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900568 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900569 protected boolean handleReportPhysicalAddress(HdmiCecMessage message) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900570 assertRunOnServiceThread();
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900571 int path = HdmiUtils.twoBytesToInt(message.getParams());
572 int address = message.getSource();
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900573 int type = message.getParams()[2];
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900574
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900575 if (updateCecSwitchInfo(address, type, path)) return true;
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900576
Jungshik Jang092b4452014-06-11 15:19:17 +0900577 // Ignore if [Device Discovery Action] is going on.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900578 if (hasAction(DeviceDiscoveryAction.class)) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +0900579 Slog.i(TAG, "Ignored while Device Discovery Action is in progress: " + message);
Jungshik Jang092b4452014-06-11 15:19:17 +0900580 return true;
581 }
582
Jinsuk Kim4b4b9402014-09-02 10:30:10 +0900583 if (!isInDeviceList(address, path)) {
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900584 handleNewDeviceAtTheTailOfActivePath(path);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900585 }
Jinsuk Kim7cd4a582015-01-29 09:50:25 +0900586
587 // Add the device ahead with default information to handle <Active Source>
588 // promptly, rather than waiting till the new device action is finished.
589 HdmiDeviceInfo deviceInfo = new HdmiDeviceInfo(address, path, getPortId(path), type,
590 Constants.UNKNOWN_VENDOR_ID, HdmiUtils.getDefaultDeviceName(address));
591 addCecDevice(deviceInfo);
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900592 startNewDeviceAction(ActiveSource.of(address, path), type);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900593 return true;
594 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900595
Jungshik Jang4480efa2014-09-04 17:08:34 +0900596 @Override
597 protected boolean handleReportPowerStatus(HdmiCecMessage command) {
598 int newStatus = command.getParams()[0] & 0xFF;
599 updateDevicePowerStatus(command.getSource(), newStatus);
600 return true;
601 }
602
603 @Override
604 protected boolean handleTimerStatus(HdmiCecMessage message) {
605 // Do nothing.
606 return true;
607 }
608
609 @Override
610 protected boolean handleRecordStatus(HdmiCecMessage message) {
611 // Do nothing.
612 return true;
613 }
614
Jinsuk Kimbcfa0672014-08-11 11:56:58 +0900615 boolean updateCecSwitchInfo(int address, int type, int path) {
616 if (address == Constants.ADDR_UNREGISTERED
617 && type == HdmiDeviceInfo.DEVICE_PURE_CEC_SWITCH) {
618 mCecSwitches.add(path);
619 updateSafeDeviceInfoList();
620 return true; // Pure switch does not need further processing. Return here.
621 }
622 if (type == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) {
623 mCecSwitches.add(path);
624 }
625 return false;
626 }
627
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900628 void startNewDeviceAction(ActiveSource activeSource, int deviceType) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900629 for (NewDeviceAction action : getActions(NewDeviceAction.class)) {
630 // If there is new device action which has the same logical address and path
631 // ignore new request.
632 // NewDeviceAction is created whenever it receives <Report Physical Address>.
633 // And there is a chance starting NewDeviceAction for the same source.
634 // Usually, new device sends <Report Physical Address> when it's plugged
635 // in. However, TV can detect a new device from HotPlugDetectionAction,
636 // which sends <Give Physical Address> to the source for newly detected
637 // device.
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900638 if (action.isActionOf(activeSource)) {
Jungshik Jang97affee2014-07-11 17:04:43 +0900639 return;
640 }
641 }
642
Jinsuk Kim72b7d732014-07-24 09:15:35 +0900643 addAndStartAction(new NewDeviceAction(this, activeSource.logicalAddress,
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +0900644 activeSource.physicalAddress, deviceType));
Jungshik Jang97affee2014-07-11 17:04:43 +0900645 }
646
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900647 private boolean handleNewDeviceAtTheTailOfActivePath(int path) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900648 // Seq #22
649 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900650 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim7c3a9562014-08-01 11:07:42 +0900651 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +0900652 startRoutingControl(getActivePath(), newPath, false, null);
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900653 return true;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900654 }
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900655 return false;
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900656 }
657
658 /**
659 * Whether the given path is located in the tail of current active path.
660 *
661 * @param path to be tested
662 * @param activePath current active path
663 * @return true if the given path is located in the tail of current active path; otherwise,
664 * false
665 */
666 static boolean isTailOfActivePath(int path, int activePath) {
667 // If active routing path is internal source, return false.
668 if (activePath == 0) {
669 return false;
670 }
671 for (int i = 12; i >= 0; i -= 4) {
672 int curActivePath = (activePath >> i) & 0xF;
673 if (curActivePath == 0) {
674 return true;
675 } else {
676 int curPath = (path >> i) & 0xF;
677 if (curPath != curActivePath) {
678 return false;
679 }
680 }
681 }
682 return false;
683 }
684
685 @Override
686 @ServiceThreadOnly
687 protected boolean handleRoutingChange(HdmiCecMessage message) {
688 assertRunOnServiceThread();
689 // Seq #21
690 byte[] params = message.getParams();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900691 int currentPath = HdmiUtils.twoBytesToInt(params);
692 if (HdmiUtils.isAffectingActiveRoutingPath(getActivePath(), currentPath)) {
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900693 mActiveSource.invalidate();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900694 removeAction(RoutingControlAction.class);
Jinsuk Kim43c23e22014-07-29 13:59:14 +0900695 int newPath = HdmiUtils.twoBytesToInt(params, 2);
Jinsuk Kim04fd2802014-07-03 14:04:02 +0900696 addAndStartAction(new RoutingControlAction(this, newPath, true, null));
Jinsuk Kim92b77cf2014-06-27 16:39:26 +0900697 }
Jungshik Jang092b4452014-06-11 15:19:17 +0900698 return true;
699 }
Jinsuk Kim0a3316b2014-06-14 09:33:55 +0900700
701 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +0900702 @ServiceThreadOnly
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900703 protected boolean handleReportAudioStatus(HdmiCecMessage message) {
704 assertRunOnServiceThread();
705
706 byte params[] = message.getParams();
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900707 int mute = params[0] & 0x80;
708 int volume = params[0] & 0x7F;
709 setAudioStatus(mute == 0x80, volume);
710 return true;
711 }
712
Yuncheol Heo38db6292014-07-01 14:15:14 +0900713 @Override
714 @ServiceThreadOnly
715 protected boolean handleTextViewOn(HdmiCecMessage message) {
716 assertRunOnServiceThread();
Jinsuk Kim544b62b2014-07-14 14:01:23 +0900717 if (mService.isPowerStandbyOrTransient() && mAutoWakeup) {
Yuncheol Heo38db6292014-07-01 14:15:14 +0900718 mService.wakeUp();
719 }
Yuncheol Heo38db6292014-07-01 14:15:14 +0900720 return true;
721 }
722
723 @Override
724 @ServiceThreadOnly
725 protected boolean handleImageViewOn(HdmiCecMessage message) {
726 assertRunOnServiceThread();
727 // Currently, it's the same as <Text View On>.
728 return handleTextViewOn(message);
729 }
730
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900731 @Override
732 @ServiceThreadOnly
733 protected boolean handleSetOsdName(HdmiCecMessage message) {
734 int source = message.getSource();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +0900735 HdmiDeviceInfo deviceInfo = getCecDeviceInfo(source);
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900736 // If the device is not in device list, ignore it.
737 if (deviceInfo == null) {
738 Slog.e(TAG, "No source device info for <Set Osd Name>." + message);
739 return true;
740 }
741 String osdName = null;
742 try {
743 osdName = new String(message.getParams(), "US-ASCII");
744 } catch (UnsupportedEncodingException e) {
745 Slog.e(TAG, "Invalid <Set Osd Name> request:" + message, e);
746 return true;
747 }
748
749 if (deviceInfo.getDisplayName().equals(osdName)) {
750 Slog.i(TAG, "Ignore incoming <Set Osd Name> having same osd name:" + message);
751 return true;
752 }
753
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900754 addCecDevice(new HdmiDeviceInfo(deviceInfo.getLogicalAddress(),
Jinsuk Kim2b152012014-07-25 08:22:26 +0900755 deviceInfo.getPhysicalAddress(), deviceInfo.getPortId(),
756 deviceInfo.getDeviceType(), deviceInfo.getVendorId(), osdName));
Jungshik Jang8f2ed352014-07-07 15:02:47 +0900757 return true;
758 }
759
Jungshik Janga5b74142014-06-23 18:03:10 +0900760 @ServiceThreadOnly
Jungshik Jang60cffce2014-06-12 18:03:04 +0900761 private void launchDeviceDiscovery() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900762 assertRunOnServiceThread();
763 clearDeviceInfoList();
764 DeviceDiscoveryAction action = new DeviceDiscoveryAction(this,
Jungshik Jang60cffce2014-06-12 18:03:04 +0900765 new DeviceDiscoveryCallback() {
766 @Override
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900767 public void onDeviceDiscoveryDone(List<HdmiDeviceInfo> deviceInfos) {
768 for (HdmiDeviceInfo info : deviceInfos) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900769 addCecDevice(info);
Jungshik Jang60cffce2014-06-12 18:03:04 +0900770 }
771
772 // Since we removed all devices when it's start and
773 // device discovery action does not poll local devices,
774 // we should put device info of local device manually here
775 for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900776 addCecDevice(device.getDeviceInfo());
Jungshik Jang60cffce2014-06-12 18:03:04 +0900777 }
778
Jungshik Jang79c58a42014-06-16 16:45:36 +0900779 addAndStartAction(new HotplugDetectionAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang410ca9c2014-08-07 18:04:14 +0900780 addAndStartAction(new PowerStatusMonitorAction(HdmiCecLocalDeviceTv.this));
Jungshik Jang187d0172014-06-17 17:48:42 +0900781
782 // If there is AVR, initiate System Audio Auto initiation action,
783 // which turns on and off system audio according to last system
784 // audio setting.
Jungshik Jang339227d2014-08-25 15:37:20 +0900785 HdmiDeviceInfo avr = getAvrDeviceInfo();
786 if (avr != null) {
787 onNewAvrAdded(avr);
Wally Yau951e3e42015-04-03 15:12:52 -0700788 } else {
789 setSystemAudioMode(false, true);
Jungshik Jang187d0172014-06-17 17:48:42 +0900790 }
Jungshik Jang60cffce2014-06-12 18:03:04 +0900791 }
792 });
Jungshik Jang79c58a42014-06-16 16:45:36 +0900793 addAndStartAction(action);
794 }
795
Jungshik Jang339227d2014-08-25 15:37:20 +0900796 @ServiceThreadOnly
797 void onNewAvrAdded(HdmiDeviceInfo avr) {
798 assertRunOnServiceThread();
Jinsuk Kimad1e3d72015-01-02 13:27:15 +0900799 if (getSystemAudioModeSetting() && !isSystemAudioActivated()) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +0900800 addAndStartAction(new SystemAudioAutoInitiationAction(this, avr.getLogicalAddress()));
801 }
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900802 if (isArcFeatureEnabled(avr.getPortId())
803 && !hasAction(SetArcTransmissionStateAction.class)) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900804 startArcAction(true);
805 }
806 }
807
Jungshik Jang79c58a42014-06-16 16:45:36 +0900808 // Clear all device info.
Jungshik Janga5b74142014-06-23 18:03:10 +0900809 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900810 private void clearDeviceInfoList() {
811 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900812 for (HdmiDeviceInfo info : mSafeExternalInputs) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +0900813 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +0900814 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900815 mDeviceInfos.clear();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +0900816 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +0900817 }
818
Jungshik Janga5b74142014-06-23 18:03:10 +0900819 @ServiceThreadOnly
Yuncheol Heoc516d652014-07-11 18:23:24 +0900820 // Seq #32
Jungshik Jangea67c182014-06-19 22:19:20 +0900821 void changeSystemAudioMode(boolean enabled, IHdmiControlCallback callback) {
822 assertRunOnServiceThread();
Yuncheol Heoc516d652014-07-11 18:23:24 +0900823 if (!mService.isControlEnabled() || hasAction(DeviceDiscoveryAction.class)) {
824 setSystemAudioMode(false, true);
825 invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE);
826 return;
827 }
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900828 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jangea67c182014-06-19 22:19:20 +0900829 if (avr == null) {
Yuncheol Heoc516d652014-07-11 18:23:24 +0900830 setSystemAudioMode(false, true);
Yuncheol Heod05f67f2014-07-11 16:06:40 +0900831 invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE);
Jungshik Jangea67c182014-06-19 22:19:20 +0900832 return;
833 }
834
835 addAndStartAction(
836 new SystemAudioActionFromTv(this, avr.getLogicalAddress(), enabled, callback));
Jungshik Jangea67c182014-06-19 22:19:20 +0900837 }
838
Jungshik Jangca5be9a2014-07-01 18:01:26 +0900839 // # Seq 25
Jinsuk Kim7ecfbae2014-07-11 14:16:29 +0900840 void setSystemAudioMode(boolean on, boolean updateSetting) {
Jungshik Jang2e8f1b62014-09-03 08:28:02 +0900841 HdmiLogger.debug("System Audio Mode change[old:%b new:%b]", mSystemAudioActivated, on);
Jungshik Jang473119f2014-08-27 16:43:22 +0900842
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900843 if (updateSetting) {
844 mService.writeBooleanSetting(Global.HDMI_SYSTEM_AUDIO_ENABLED, on);
845 }
846 updateAudioManagerForSystemAudio(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900847 synchronized (mLock) {
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900848 if (mSystemAudioActivated != on) {
849 mSystemAudioActivated = on;
Jungshik Jangea67c182014-06-19 22:19:20 +0900850 mService.announceSystemAudioModeChange(on);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900851 }
852 }
853 }
854
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900855 private void updateAudioManagerForSystemAudio(boolean on) {
Jungshik Jang6b096d32014-09-12 18:49:08 +0900856 int device = mService.getAudioManager().setHdmiSystemAudioSupported(on);
857 HdmiLogger.debug("[A]UpdateSystemAudio mode[on=%b] output=[%X]", on, device);
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900858 }
859
860 boolean isSystemAudioActivated() {
Jinsuk Kim86a1e5a2014-11-19 06:04:50 +0900861 if (!hasSystemAudioDevice()) {
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900862 return false;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900863 }
Jungshik Jang377dcbd2014-07-15 15:49:02 +0900864 synchronized (mLock) {
865 return mSystemAudioActivated;
866 }
867 }
868
869 boolean getSystemAudioModeSetting() {
870 return mService.readBooleanSetting(Global.HDMI_SYSTEM_AUDIO_ENABLED, false);
Jungshik Jang79c58a42014-06-16 16:45:36 +0900871 }
872
873 /**
874 * Change ARC status into the given {@code enabled} status.
875 *
876 * @return {@code true} if ARC was in "Enabled" status
877 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900878 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +0900879 boolean setArcStatus(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900880 assertRunOnServiceThread();
Jungshik Jang6b096d32014-09-12 18:49:08 +0900881
882 HdmiLogger.debug("Set Arc Status[old:%b new:%b]", mArcEstablished, enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900883 boolean oldStatus = mArcEstablished;
884 // 1. Enable/disable ARC circuit.
Jinsuk Kim757c0972015-02-23 10:15:42 +0900885 setAudioReturnChannel(enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900886 // 2. Notify arc status to audio service.
887 notifyArcStatusToAudioService(enabled);
888 // 3. Update arc status;
889 mArcEstablished = enabled;
890 return oldStatus;
Jungshik Jang79c58a42014-06-16 16:45:36 +0900891 }
892
Jinsuk Kim757c0972015-02-23 10:15:42 +0900893 /**
894 * Switch hardware ARC circuit in the system.
895 */
896 @ServiceThreadOnly
897 void setAudioReturnChannel(boolean enabled) {
898 assertRunOnServiceThread();
899 HdmiDeviceInfo avr = getAvrDeviceInfo();
900 if (avr != null) {
901 mService.setAudioReturnChannel(avr.getPortId(), enabled);
902 }
903 }
904
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +0900905 @ServiceThreadOnly
906 private void updateArcFeatureStatus(int portId, boolean isConnected) {
907 assertRunOnServiceThread();
908 // HEAC 2.4, HEACT 5-15
909 // Should not activate ARC if +5V status is false.
910 HdmiPortInfo portInfo = mService.getPortInfo(portId);
911 if (portInfo.isArcSupported()) {
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900912 changeArcFeatureEnabled(portId, isConnected);
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +0900913 }
914 }
915
Jungshik Janga858d222014-06-23 17:17:47 +0900916 private void notifyArcStatusToAudioService(boolean enabled) {
917 // Note that we don't set any name to ARC.
918 mService.getAudioManager().setWiredDeviceConnectionState(
919 AudioSystem.DEVICE_OUT_HDMI_ARC,
Paul McLean10804eb2015-01-28 11:16:35 -0800920 enabled ? 1 : 0, "", "");
Jungshik Janga858d222014-06-23 17:17:47 +0900921 }
922
Jungshik Jang79c58a42014-06-16 16:45:36 +0900923 /**
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900924 * Returns true if ARC is currently established on a certain port.
Jungshik Jang79c58a42014-06-16 16:45:36 +0900925 */
Jungshik Janga13da0d2014-06-30 16:26:06 +0900926 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900927 boolean isArcEstablished() {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900928 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900929 if (mArcEstablished) {
930 for (int i = 0; i < mArcFeatureEnabled.size(); i++) {
931 if (mArcFeatureEnabled.valueAt(i)) return true;
932 }
933 }
934 return false;
Jungshik Janga13da0d2014-06-30 16:26:06 +0900935 }
936
937 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900938 void changeArcFeatureEnabled(int portId, boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900939 assertRunOnServiceThread();
940
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900941 if (mArcFeatureEnabled.get(portId) != enabled) {
942 mArcFeatureEnabled.put(portId, enabled);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900943 if (enabled) {
944 if (!mArcEstablished) {
945 startArcAction(true);
946 }
947 } else {
948 if (mArcEstablished) {
949 startArcAction(false);
950 }
951 }
Jungshik Janga13da0d2014-06-30 16:26:06 +0900952 }
953 }
954
955 @ServiceThreadOnly
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900956 boolean isArcFeatureEnabled(int portId) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900957 assertRunOnServiceThread();
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +0900958 return mArcFeatureEnabled.get(portId);
Jungshik Janga13da0d2014-06-30 16:26:06 +0900959 }
960
961 @ServiceThreadOnly
Jungshik Jang339227d2014-08-25 15:37:20 +0900962 void startArcAction(boolean enabled) {
Jungshik Janga13da0d2014-06-30 16:26:06 +0900963 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +0900964 HdmiDeviceInfo info = getAvrDeviceInfo();
Jungshik Janga13da0d2014-06-30 16:26:06 +0900965 if (info == null) {
Jungshik Jang339227d2014-08-25 15:37:20 +0900966 Slog.w(TAG, "Failed to start arc action; No AVR device.");
Jungshik Janga13da0d2014-06-30 16:26:06 +0900967 return;
968 }
Jungshik Jang339227d2014-08-25 15:37:20 +0900969 if (!canStartArcUpdateAction(info.getLogicalAddress(), enabled)) {
970 Slog.w(TAG, "Failed to start arc action; ARC configuration check failed.");
971 if (enabled && !isConnectedToArcPort(info.getPhysicalAddress())) {
972 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
973 }
Jungshik Janga13da0d2014-06-30 16:26:06 +0900974 return;
975 }
976
977 // Terminate opposite action and start action if not exist.
978 if (enabled) {
979 removeAction(RequestArcTerminationAction.class);
980 if (!hasAction(RequestArcInitiationAction.class)) {
981 addAndStartAction(new RequestArcInitiationAction(this, info.getLogicalAddress()));
982 }
983 } else {
984 removeAction(RequestArcInitiationAction.class);
985 if (!hasAction(RequestArcTerminationAction.class)) {
986 addAndStartAction(new RequestArcTerminationAction(this, info.getLogicalAddress()));
987 }
Jungshik Jang79c58a42014-06-16 16:45:36 +0900988 }
989 }
990
Jungshik Jang339227d2014-08-25 15:37:20 +0900991 private boolean isDirectConnectAddress(int physicalAddress) {
992 return (physicalAddress & Constants.ROUTING_PATH_TOP_MASK) == physicalAddress;
993 }
994
Jungshik Jang79c58a42014-06-16 16:45:36 +0900995 void setAudioStatus(boolean mute, int volume) {
Jungshik Jang8fa36b12014-06-25 15:51:36 +0900996 synchronized (mLock) {
997 mSystemAudioMute = mute;
998 mSystemAudioVolume = volume;
Jungshik Jangb69aafbf2014-07-11 16:29:06 +0900999 int maxVolume = mService.getAudioManager().getStreamMaxVolume(
1000 AudioManager.STREAM_MUSIC);
1001 mService.setAudioStatus(mute,
1002 VolumeControlAction.scaleToCustomVolume(volume, maxVolume));
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001003 displayOsd(HdmiControlManager.OSD_MESSAGE_AVR_VOLUME_CHANGED,
1004 mute ? HdmiControlManager.AVR_VOLUME_MUTED : volume);
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001005 }
1006 }
1007
1008 @ServiceThreadOnly
1009 void changeVolume(int curVolume, int delta, int maxVolume) {
1010 assertRunOnServiceThread();
Jungshik Jang377dcbd2014-07-15 15:49:02 +09001011 if (delta == 0 || !isSystemAudioActivated()) {
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001012 return;
1013 }
1014
1015 int targetVolume = curVolume + delta;
1016 int cecVolume = VolumeControlAction.scaleToCecVolume(targetVolume, maxVolume);
1017 synchronized (mLock) {
1018 // If new volume is the same as current system audio volume, just ignore it.
1019 // Note that UNKNOWN_VOLUME is not in range of cec volume scale.
1020 if (cecVolume == mSystemAudioVolume) {
1021 // Update tv volume with system volume value.
1022 mService.setAudioStatus(false,
1023 VolumeControlAction.scaleToCustomVolume(mSystemAudioVolume, maxVolume));
1024 return;
1025 }
1026 }
1027
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001028 List<VolumeControlAction> actions = getActions(VolumeControlAction.class);
1029 if (actions.isEmpty()) {
1030 addAndStartAction(new VolumeControlAction(this,
1031 getAvrDeviceInfo().getLogicalAddress(), delta > 0));
1032 } else {
1033 actions.get(0).handleVolumeChange(delta > 0);
1034 }
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001035 }
1036
1037 @ServiceThreadOnly
1038 void changeMute(boolean mute) {
1039 assertRunOnServiceThread();
Jungshik Jang6b096d32014-09-12 18:49:08 +09001040 HdmiLogger.debug("[A]:Change mute:%b", mute);
Jungshik Jang720407a2014-09-15 17:48:41 +09001041 synchronized (mLock) {
1042 if (mSystemAudioMute == mute) {
1043 HdmiLogger.debug("No need to change mute.");
1044 return;
1045 }
1046 }
Jungshik Jang377dcbd2014-07-15 15:49:02 +09001047 if (!isSystemAudioActivated()) {
Jungshik Jang6b096d32014-09-12 18:49:08 +09001048 HdmiLogger.debug("[A]:System audio is not activated.");
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001049 return;
1050 }
1051
1052 // Remove existing volume action.
1053 removeAction(VolumeControlAction.class);
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001054 sendUserControlPressedAndReleased(getAvrDeviceInfo().getLogicalAddress(),
1055 mute ? HdmiCecKeycode.CEC_KEYCODE_MUTE_FUNCTION :
1056 HdmiCecKeycode.CEC_KEYCODE_RESTORE_VOLUME_FUNCTION);
Jungshik Jang8fa36b12014-06-25 15:51:36 +09001057 }
1058
Jungshik Jang79c58a42014-06-16 16:45:36 +09001059 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001060 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001061 protected boolean handleInitiateArc(HdmiCecMessage message) {
1062 assertRunOnServiceThread();
Jungshik Jang339227d2014-08-25 15:37:20 +09001063
1064 if (!canStartArcUpdateAction(message.getSource(), true)) {
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001065 if (getAvrDeviceInfo() == null) {
1066 // AVR may not have been discovered yet. Delay the message processing.
1067 mDelayedMessageBuffer.add(message);
1068 return true;
1069 }
Jungshik Jang339227d2014-08-25 15:37:20 +09001070 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
1071 if (!isConnectedToArcPort(message.getSource())) {
1072 displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT);
1073 }
1074 return true;
1075 }
1076
Jungshik Jang79c58a42014-06-16 16:45:36 +09001077 // In case where <Initiate Arc> is started by <Request ARC Initiation>
1078 // need to clean up RequestArcInitiationAction.
1079 removeAction(RequestArcInitiationAction.class);
1080 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1081 message.getSource(), true);
1082 addAndStartAction(action);
1083 return true;
1084 }
1085
Jungshik Jang339227d2014-08-25 15:37:20 +09001086 private boolean canStartArcUpdateAction(int avrAddress, boolean shouldCheckArcFeatureEnabled) {
1087 HdmiDeviceInfo avr = getAvrDeviceInfo();
1088 if (avr != null
1089 && (avrAddress == avr.getLogicalAddress())
1090 && isConnectedToArcPort(avr.getPhysicalAddress())
1091 && isDirectConnectAddress(avr.getPhysicalAddress())) {
1092 if (shouldCheckArcFeatureEnabled) {
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +09001093 return isArcFeatureEnabled(avr.getPortId());
Jungshik Jang339227d2014-08-25 15:37:20 +09001094 } else {
1095 return true;
1096 }
1097 } else {
1098 return false;
1099 }
1100 }
1101
Jungshik Jang79c58a42014-06-16 16:45:36 +09001102 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001103 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001104 protected boolean handleTerminateArc(HdmiCecMessage message) {
1105 assertRunOnServiceThread();
Jungshik Jang339227d2014-08-25 15:37:20 +09001106 // In cast of termination, do not check ARC configuration in that AVR device
1107 // might be removed already.
1108
Jungshik Jang79c58a42014-06-16 16:45:36 +09001109 // In case where <Terminate Arc> is started by <Request ARC Termination>
1110 // need to clean up RequestArcInitiationAction.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001111 removeAction(RequestArcTerminationAction.class);
1112 SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this,
1113 message.getSource(), false);
1114 addAndStartAction(action);
1115 return true;
1116 }
1117
1118 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001119 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001120 protected boolean handleSetSystemAudioMode(HdmiCecMessage message) {
1121 assertRunOnServiceThread();
1122 if (!isMessageForSystemAudio(message)) {
Jinsuk Kimad1e3d72015-01-02 13:27:15 +09001123 if (getAvrDeviceInfo() == null) {
1124 // AVR may not have been discovered yet. Delay the message processing.
1125 mDelayedMessageBuffer.add(message);
1126 return true;
1127 }
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001128 HdmiLogger.warning("Invalid <Set System Audio Mode> message:" + message);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001129 mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED);
1130 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001131 }
1132 SystemAudioActionFromAvr action = new SystemAudioActionFromAvr(this,
Jungshik Jang7f0a1c52014-06-23 16:00:07 +09001133 message.getSource(), HdmiUtils.parseCommandParamSystemAudioStatus(message), null);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001134 addAndStartAction(action);
1135 return true;
1136 }
1137
1138 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001139 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001140 protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) {
1141 assertRunOnServiceThread();
1142 if (!isMessageForSystemAudio(message)) {
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001143 HdmiLogger.warning("Invalid <System Audio Mode Status> message:" + message);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001144 // Ignore this message.
1145 return true;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001146 }
Jinsuk Kim7ecfbae2014-07-11 14:16:29 +09001147 setSystemAudioMode(HdmiUtils.parseCommandParamSystemAudioStatus(message), true);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001148 return true;
1149 }
1150
Jungshik Jangb6591b82014-07-23 16:10:23 +09001151 // Seq #53
1152 @Override
1153 @ServiceThreadOnly
1154 protected boolean handleRecordTvScreen(HdmiCecMessage message) {
1155 List<OneTouchRecordAction> actions = getActions(OneTouchRecordAction.class);
1156 if (!actions.isEmpty()) {
1157 // Assumes only one OneTouchRecordAction.
1158 OneTouchRecordAction action = actions.get(0);
1159 if (action.getRecorderAddress() != message.getSource()) {
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001160 announceOneTouchRecordResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001161 message.getSource(),
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001162 HdmiControlManager.ONE_TOUCH_RECORD_PREVIOUS_RECORDING_IN_PROGRESS);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001163 }
1164 return super.handleRecordTvScreen(message);
1165 }
1166
1167 int recorderAddress = message.getSource();
1168 byte[] recordSource = mService.invokeRecordRequestListener(recorderAddress);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001169 int reason = startOneTouchRecord(recorderAddress, recordSource);
1170 if (reason != Constants.ABORT_NO_ERROR) {
1171 mService.maySendFeatureAbortCommand(message, reason);
1172 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001173 return true;
1174 }
1175
Jungshik Jange5a93372014-07-25 13:41:14 +09001176 @Override
1177 protected boolean handleTimerClearedStatus(HdmiCecMessage message) {
1178 byte[] params = message.getParams();
Jungshik Jange9e0f072014-08-05 17:48:59 +09001179 int timerClearedStatusData = params[0] & 0xFF;
Jungshik Jang326aef02014-11-05 12:50:35 +09001180 announceTimerRecordingResult(message.getSource(), timerClearedStatusData);
Jungshik Jange5a93372014-07-25 13:41:14 +09001181 return true;
1182 }
1183
Jungshik Jang326aef02014-11-05 12:50:35 +09001184 void announceOneTouchRecordResult(int recorderAddress, int result) {
1185 mService.invokeOneTouchRecordResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001186 }
1187
Jungshik Jang326aef02014-11-05 12:50:35 +09001188 void announceTimerRecordingResult(int recorderAddress, int result) {
1189 mService.invokeTimerRecordingResult(recorderAddress, result);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001190 }
1191
Jungshik Jang326aef02014-11-05 12:50:35 +09001192 void announceClearTimerRecordingResult(int recorderAddress, int result) {
1193 mService.invokeClearTimerRecordingResult(recorderAddress, result);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001194 }
1195
Jungshik Jang79c58a42014-06-16 16:45:36 +09001196 private boolean isMessageForSystemAudio(HdmiCecMessage message) {
Jungshik Jang4480efa2014-09-04 17:08:34 +09001197 return mService.isControlEnabled()
1198 && message.getSource() == Constants.ADDR_AUDIO_SYSTEM
Jungshik Jang473119f2014-08-27 16:43:22 +09001199 && (message.getDestination() == Constants.ADDR_TV
1200 || message.getDestination() == Constants.ADDR_BROADCAST)
1201 && getAvrDeviceInfo() != null;
Jungshik Jang79c58a42014-06-16 16:45:36 +09001202 }
1203
1204 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001205 * Add a new {@link HdmiDeviceInfo}. It returns old device info which has the same
Jungshik Jang79c58a42014-06-16 16:45:36 +09001206 * logical address as new device info's.
1207 *
1208 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1209 *
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001210 * @param deviceInfo a new {@link HdmiDeviceInfo} to be added.
1211 * @return {@code null} if it is new device. Otherwise, returns old {@HdmiDeviceInfo}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001212 * that has the same logical address as new one has.
1213 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001214 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001215 private HdmiDeviceInfo addDeviceInfo(HdmiDeviceInfo deviceInfo) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001216 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001217 HdmiDeviceInfo oldDeviceInfo = getCecDeviceInfo(deviceInfo.getLogicalAddress());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001218 if (oldDeviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001219 removeDeviceInfo(deviceInfo.getId());
Jungshik Jang79c58a42014-06-16 16:45:36 +09001220 }
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001221 mDeviceInfos.append(deviceInfo.getId(), deviceInfo);
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001222 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001223 return oldDeviceInfo;
1224 }
1225
1226 /**
1227 * Remove a device info corresponding to the given {@code logicalAddress}.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001228 * It returns removed {@link HdmiDeviceInfo} if exists.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001229 *
1230 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
1231 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001232 * @param id id of device to be removed
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001233 * @return removed {@link HdmiDeviceInfo} it exists. Otherwise, returns {@code null}
Jungshik Jang79c58a42014-06-16 16:45:36 +09001234 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001235 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001236 private HdmiDeviceInfo removeDeviceInfo(int id) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001237 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001238 HdmiDeviceInfo deviceInfo = mDeviceInfos.get(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001239 if (deviceInfo != null) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001240 mDeviceInfos.remove(id);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001241 }
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001242 updateSafeDeviceInfoList();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001243 return deviceInfo;
1244 }
1245
1246 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001247 * Return a list of all {@link HdmiDeviceInfo}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001248 *
1249 * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
Jungshik Jang339227d2014-08-25 15:37:20 +09001250 * This is not thread-safe. For thread safety, call {@link #getSafeExternalInputsLocked} which
Jungshik Jang8e93c842014-08-06 15:48:33 +09001251 * does not include local device.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001252 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001253 @ServiceThreadOnly
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001254 List<HdmiDeviceInfo> getDeviceInfoList(boolean includeLocalDevice) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001255 assertRunOnServiceThread();
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001256 if (includeLocalDevice) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001257 return HdmiUtils.sparseArrayToList(mDeviceInfos);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001258 } else {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001259 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001260 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001261 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001262 if (!isLocalDeviceAddress(info.getLogicalAddress())) {
1263 infoList.add(info);
1264 }
1265 }
1266 return infoList;
1267 }
1268 }
1269
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001270 /**
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001271 * Return external input devices.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001272 */
Jinsuk Kimed086452014-08-18 15:01:53 +09001273 List<HdmiDeviceInfo> getSafeExternalInputsLocked() {
1274 return mSafeExternalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001275 }
1276
Jungshik Janga5b74142014-06-23 18:03:10 +09001277 @ServiceThreadOnly
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001278 private void updateSafeDeviceInfoList() {
1279 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001280 List<HdmiDeviceInfo> copiedDevices = HdmiUtils.sparseArrayToList(mDeviceInfos);
1281 List<HdmiDeviceInfo> externalInputs = getInputDevices();
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001282 synchronized (mLock) {
1283 mSafeAllDeviceInfos = copiedDevices;
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001284 mSafeExternalInputs = externalInputs;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001285 }
1286 }
1287
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001288 /**
1289 * Return a list of external cec input (source) devices.
1290 *
1291 * <p>Note that this effectively excludes non-source devices like system audio,
1292 * secondary TV.
1293 */
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001294 private List<HdmiDeviceInfo> getInputDevices() {
1295 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001296 for (int i = 0; i < mDeviceInfos.size(); ++i) {
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001297 HdmiDeviceInfo info = mDeviceInfos.valueAt(i);
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001298 if (isLocalDeviceAddress(info.getLogicalAddress())) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001299 continue;
1300 }
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001301 if (info.isSourceType() && !hideDevicesBehindLegacySwitch(info)) {
Jinsuk Kim9c37e1f2014-07-02 08:29:26 +09001302 infoList.add(info);
1303 }
1304 }
1305 return infoList;
1306 }
1307
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001308 // Check if we are hiding CEC devices connected to a legacy (non-CEC) switch.
1309 // Returns true if the policy is set to true, and the device to check does not have
1310 // a parent CEC device (which should be the CEC-enabled switch) in the list.
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001311 private boolean hideDevicesBehindLegacySwitch(HdmiDeviceInfo info) {
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001312 return HdmiConfig.HIDE_DEVICES_BEHIND_LEGACY_SWITCH
1313 && !isConnectedToCecSwitch(info.getPhysicalAddress(), mCecSwitches);
1314 }
1315
1316 private static boolean isConnectedToCecSwitch(int path, Collection<Integer> switches) {
1317 for (int switchPath : switches) {
1318 if (isParentPath(switchPath, path)) {
1319 return true;
1320 }
1321 }
1322 return false;
1323 }
1324
1325 private static boolean isParentPath(int parentPath, int childPath) {
1326 // (A000, AB00) (AB00, ABC0), (ABC0, ABCD)
1327 // If child's last non-zero nibble is removed, the result equals to the parent.
1328 for (int i = 0; i <= 12; i += 4) {
1329 int nibble = (childPath >> i) & 0xF;
1330 if (nibble != 0) {
1331 int parentNibble = (parentPath >> i) & 0xF;
1332 return parentNibble == 0 && (childPath >> i+4) == (parentPath >> i+4);
1333 }
1334 }
1335 return false;
1336 }
1337
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001338 private void invokeDeviceEventListener(HdmiDeviceInfo info, int status) {
Jinsuk Kim98d760e2014-12-23 07:01:51 +09001339 if (!hideDevicesBehindLegacySwitch(info)) {
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001340 mService.invokeDeviceEventListeners(info, status);
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001341 }
1342 }
1343
Jungshik Jang79c58a42014-06-16 16:45:36 +09001344 private boolean isLocalDeviceAddress(int address) {
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001345 return mLocalDeviceAddresses.contains(address);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001346 }
1347
Jungshik Jange9cf1582014-06-23 17:28:58 +09001348 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001349 HdmiDeviceInfo getAvrDeviceInfo() {
Jungshik Jange9cf1582014-06-23 17:28:58 +09001350 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001351 return getCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jange9cf1582014-06-23 17:28:58 +09001352 }
1353
Jungshik Jang79c58a42014-06-16 16:45:36 +09001354 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001355 * Return a {@link HdmiDeviceInfo} corresponding to the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001356 *
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001357 * This is not thread-safe. For thread safety, call {@link #getSafeCecDeviceInfo(int)}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001358 *
Jungshik Jang339227d2014-08-25 15:37:20 +09001359 * @param logicalAddress logical address of the device to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001360 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001361 * Returns null if no logical address matched
1362 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001363 @ServiceThreadOnly
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001364 HdmiDeviceInfo getCecDeviceInfo(int logicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001365 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001366 return mDeviceInfos.get(HdmiDeviceInfo.idForCecDevice(logicalAddress));
Jungshik Jang79c58a42014-06-16 16:45:36 +09001367 }
1368
Jungshik Jange9cf1582014-06-23 17:28:58 +09001369 boolean hasSystemAudioDevice() {
1370 return getSafeAvrDeviceInfo() != null;
1371 }
1372
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001373 HdmiDeviceInfo getSafeAvrDeviceInfo() {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001374 return getSafeCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001375 }
1376
1377 /**
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001378 * Thread safe version of {@link #getCecDeviceInfo(int)}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001379 *
1380 * @param logicalAddress logical address to be retrieved
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001381 * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}.
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001382 * Returns null if no logical address matched
1383 */
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001384 HdmiDeviceInfo getSafeCecDeviceInfo(int logicalAddress) {
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001385 synchronized (mLock) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001386 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1387 if (info.isCecDevice() && info.getLogicalAddress() == logicalAddress) {
1388 return info;
1389 }
1390 }
1391 return null;
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001392 }
1393 }
1394
Jinsuk Kimbdf27fb2014-10-20 10:00:04 +09001395 List<HdmiDeviceInfo> getSafeCecDevicesLocked() {
1396 ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>();
1397 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1398 if (isLocalDeviceAddress(info.getLogicalAddress())) {
1399 continue;
1400 }
1401 infoList.add(info);
1402 }
1403 return infoList;
1404 }
1405
Jungshik Jangfa8e90d2014-06-23 14:40:32 +09001406 /**
Jungshik Jang8f2ed352014-07-07 15:02:47 +09001407 * Called when a device is newly added or a new device is detected or
1408 * existing device is updated.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001409 *
1410 * @param info device info of a new device.
1411 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001412 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001413 final void addCecDevice(HdmiDeviceInfo info) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001414 assertRunOnServiceThread();
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001415 HdmiDeviceInfo old = addDeviceInfo(info);
Jinsuk Kim13c030e2014-06-20 13:25:17 +09001416 if (info.getLogicalAddress() == mAddress) {
1417 // The addition of TV device itself should not be notified.
1418 return;
1419 }
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001420 if (old == null) {
1421 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
1422 } else if (!old.equals(info)) {
Jinsuk Kim4fcbf0b2014-12-09 06:48:13 +09001423 invokeDeviceEventListener(old, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
1424 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE);
Jinsuk Kim6102bac72014-12-09 00:11:58 +09001425 }
Jungshik Jang79c58a42014-06-16 16:45:36 +09001426 }
1427
1428 /**
1429 * Called when a device is removed or removal of device is detected.
1430 *
1431 * @param address a logical address of a device to be removed
1432 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001433 @ServiceThreadOnly
Jungshik Jang79c58a42014-06-16 16:45:36 +09001434 final void removeCecDevice(int address) {
1435 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001436 HdmiDeviceInfo info = removeDeviceInfo(HdmiDeviceInfo.idForCecDevice(address));
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001437
Jungshik Jang79c58a42014-06-16 16:45:36 +09001438 mCecMessageCache.flushMessagesFrom(address);
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001439 invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001440 }
1441
Jungshik Jang26dc71e2014-07-04 10:53:27 +09001442 @ServiceThreadOnly
1443 void handleRemoveActiveRoutingPath(int path) {
1444 assertRunOnServiceThread();
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001445 // Seq #23
1446 if (isTailOfActivePath(path, getActivePath())) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001447 int newPath = mService.portIdToPath(getActivePortId());
Jinsuk Kim546d8672014-11-24 07:30:54 +09001448 startRoutingControl(getActivePath(), newPath, true, null);
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001449 }
1450 }
1451
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001452 /**
1453 * Launch routing control process.
1454 *
1455 * @param routingForBootup true if routing control is initiated due to One Touch Play
1456 * or TV power on
1457 */
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001458 @ServiceThreadOnly
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001459 void launchRoutingControl(boolean routingForBootup) {
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001460 assertRunOnServiceThread();
1461 // Seq #24
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001462 if (getActivePortId() != Constants.INVALID_PORT_ID) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001463 if (!routingForBootup && !isProhibitMode()) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001464 int newPath = mService.portIdToPath(getActivePortId());
1465 setActivePath(newPath);
Jinsuk Kim546d8672014-11-24 07:30:54 +09001466 startRoutingControl(getActivePath(), newPath, routingForBootup, null);
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001467 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001468 } else {
1469 int activePath = mService.getPhysicalAddress();
1470 setActivePath(activePath);
Jinsuk Kim2ab6d9f2015-01-16 15:49:16 +09001471 if (!routingForBootup
1472 && !mDelayedMessageBuffer.isBuffered(Constants.MESSAGE_ACTIVE_SOURCE)) {
Jinsuk Kim5344cd92014-07-03 18:02:01 +09001473 mService.sendCecCommand(HdmiCecMessageBuilder.buildActiveSource(mAddress,
1474 activePath));
1475 }
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001476 }
1477 }
1478
Jungshik Jang79c58a42014-06-16 16:45:36 +09001479 /**
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001480 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
Jungshik Jang79c58a42014-06-16 16:45:36 +09001481 * the given routing path. CEC devices use routing path for its physical address to
1482 * describe the hierarchy of the devices in the network.
1483 *
1484 * @param path routing path or physical address
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001485 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
Jungshik Jang79c58a42014-06-16 16:45:36 +09001486 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001487 @ServiceThreadOnly
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001488 final HdmiDeviceInfo getDeviceInfoByPath(int path) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001489 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001490 for (HdmiDeviceInfo info : getDeviceInfoList(false)) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001491 if (info.getPhysicalAddress() == path) {
1492 return info;
1493 }
1494 }
1495 return null;
1496 }
1497
1498 /**
Jinsuk Kim7640d982015-01-28 16:44:07 +09001499 * Returns the {@link HdmiDeviceInfo} instance whose physical address matches
1500 * the given routing path. This is the version accessible safely from threads
1501 * other than service thread.
1502 *
1503 * @param path routing path or physical address
1504 * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null
1505 */
1506 HdmiDeviceInfo getSafeDeviceInfoByPath(int path) {
1507 synchronized (mLock) {
1508 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1509 if (info.getPhysicalAddress() == path) {
1510 return info;
1511 }
1512 }
1513 return null;
1514 }
1515 }
1516
1517 /**
Jungshik Jang79c58a42014-06-16 16:45:36 +09001518 * Whether a device of the specified physical address and logical address exists
1519 * in a device info list. However, both are minimal condition and it could
1520 * be different device from the original one.
1521 *
Jungshik Jang79c58a42014-06-16 16:45:36 +09001522 * @param logicalAddress logical address of a device to be searched
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001523 * @param physicalAddress physical address of a device to be searched
Jungshik Jang79c58a42014-06-16 16:45:36 +09001524 * @return true if exist; otherwise false
1525 */
Jungshik Janga5b74142014-06-23 18:03:10 +09001526 @ServiceThreadOnly
Jinsuk Kim7cd4a582015-01-29 09:50:25 +09001527 boolean isInDeviceList(int logicalAddress, int physicalAddress) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001528 assertRunOnServiceThread();
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001529 HdmiDeviceInfo device = getCecDeviceInfo(logicalAddress);
Jungshik Jang79c58a42014-06-16 16:45:36 +09001530 if (device == null) {
1531 return false;
1532 }
1533 return device.getPhysicalAddress() == physicalAddress;
1534 }
1535
1536 @Override
Jungshik Janga5b74142014-06-23 18:03:10 +09001537 @ServiceThreadOnly
Jinsuk Kim92b77cf2014-06-27 16:39:26 +09001538 void onHotplug(int portId, boolean connected) {
Jungshik Jang79c58a42014-06-16 16:45:36 +09001539 assertRunOnServiceThread();
Jungshik Jang79c58a42014-06-16 16:45:36 +09001540
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001541 if (!connected) {
1542 removeCecSwitches(portId);
1543 }
Jungshik Jang79c58a42014-06-16 16:45:36 +09001544 // Tv device will have permanent HotplugDetectionAction.
1545 List<HotplugDetectionAction> hotplugActions = getActions(HotplugDetectionAction.class);
1546 if (!hotplugActions.isEmpty()) {
1547 // Note that hotplug action is single action running on a machine.
1548 // "pollAllDevicesNow" cleans up timer and start poll action immediately.
Jungshik Jang24c23c12014-07-07 18:04:39 +09001549 // It covers seq #40, #43.
Jungshik Jang79c58a42014-06-16 16:45:36 +09001550 hotplugActions.get(0).pollAllDevicesNow();
1551 }
Jinsuk Kim2ee0d6f2015-01-28 17:38:28 +09001552 updateArcFeatureStatus(portId, connected);
Jungshik Jang60cffce2014-06-12 18:03:04 +09001553 }
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001554
Jinsuk Kim4fdd0f42014-08-06 07:23:32 +09001555 private void removeCecSwitches(int portId) {
1556 Iterator<Integer> it = mCecSwitches.iterator();
1557 while (!it.hasNext()) {
1558 int path = it.next();
1559 if (pathToPortId(path) == portId) {
1560 it.remove();
1561 }
1562 }
1563 }
1564
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001565 @ServiceThreadOnly
1566 void setAutoDeviceOff(boolean enabled) {
1567 assertRunOnServiceThread();
1568 mAutoDeviceOff = enabled;
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001569 }
1570
1571 @ServiceThreadOnly
1572 void setAutoWakeup(boolean enabled) {
1573 assertRunOnServiceThread();
1574 mAutoWakeup = enabled;
Jinsuk Kim160a6e52014-07-02 06:16:36 +09001575 }
Yuncheol Heo38db6292014-07-01 14:15:14 +09001576
Yuncheol Heo25c20292014-07-31 17:59:39 +09001577 @ServiceThreadOnly
1578 boolean getAutoWakeup() {
1579 assertRunOnServiceThread();
1580 return mAutoWakeup;
1581 }
1582
Yuncheol Heo38db6292014-07-01 14:15:14 +09001583 @Override
1584 @ServiceThreadOnly
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001585 protected void disableDevice(boolean initiatedByCec, PendingActionClearedCallback callback) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001586 assertRunOnServiceThread();
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001587 mService.unregisterTvInputCallback(mTvInputCallback);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001588 // Remove any repeated working actions.
1589 // HotplugDetectionAction will be reinstated during the wake up process.
1590 // HdmiControlService.onWakeUp() -> initializeLocalDevices() ->
1591 // LocalDeviceTv.onAddressAllocated() -> launchDeviceDiscovery().
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001592 removeAction(DeviceDiscoveryAction.class);
Yuncheol Heo38db6292014-07-01 14:15:14 +09001593 removeAction(HotplugDetectionAction.class);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001594 removeAction(PowerStatusMonitorAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001595 // Remove recording actions.
Jungshik Jangb6591b82014-07-23 16:10:23 +09001596 removeAction(OneTouchRecordAction.class);
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001597 removeAction(TimerRecordingAction.class);
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001598
1599 disableSystemAudioIfExist();
1600 disableArcIfExist();
Jinsuk Kima5445ce2015-03-30 17:14:58 +09001601
1602 super.disableDevice(initiatedByCec, callback);
Jinsuk Kim49b47bb2014-07-22 10:40:35 +09001603 clearDeviceInfoList();
Yuncheol Heo38db6292014-07-01 14:15:14 +09001604 checkIfPendingActionsCleared();
1605 }
1606
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001607 @ServiceThreadOnly
1608 private void disableSystemAudioIfExist() {
1609 assertRunOnServiceThread();
1610 if (getAvrDeviceInfo() == null) {
1611 return;
1612 }
1613
1614 // Seq #31.
1615 removeAction(SystemAudioActionFromAvr.class);
1616 removeAction(SystemAudioActionFromTv.class);
1617 removeAction(SystemAudioAutoInitiationAction.class);
1618 removeAction(SystemAudioStatusAction.class);
1619 removeAction(VolumeControlAction.class);
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001620 }
1621
1622 @ServiceThreadOnly
1623 private void disableArcIfExist() {
1624 assertRunOnServiceThread();
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001625 HdmiDeviceInfo avr = getAvrDeviceInfo();
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001626 if (avr == null) {
1627 return;
1628 }
1629
1630 // Seq #44.
1631 removeAction(RequestArcInitiationAction.class);
Jinsuk Kim5bcf5bf2015-04-02 07:31:21 +09001632 if (!hasAction(RequestArcTerminationAction.class) && isArcEstablished()) {
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001633 addAndStartAction(new RequestArcTerminationAction(this, avr.getLogicalAddress()));
1634 }
1635 }
1636
Yuncheol Heo38db6292014-07-01 14:15:14 +09001637 @Override
1638 @ServiceThreadOnly
Jungshik Jang4fc1d102014-07-09 19:24:50 +09001639 protected void onStandby(boolean initiatedByCec) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001640 assertRunOnServiceThread();
1641 // Seq #11
1642 if (!mService.isControlEnabled()) {
1643 return;
1644 }
Jinsuk Kim544b62b2014-07-14 14:01:23 +09001645 if (!initiatedByCec && mAutoDeviceOff) {
Yuncheol Heo38db6292014-07-01 14:15:14 +09001646 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(
Jinsuk Kimc0c20d02014-07-04 14:34:31 +09001647 mAddress, Constants.ADDR_BROADCAST));
Yuncheol Heo38db6292014-07-01 14:15:14 +09001648 }
1649 }
1650
Jinsuk Kim4d43d932014-07-03 16:43:58 +09001651 boolean isProhibitMode() {
1652 return mService.isProhibitMode();
1653 }
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001654
1655 boolean isPowerStandbyOrTransient() {
1656 return mService.isPowerStandbyOrTransient();
Jungshik Jang8866c812014-07-08 14:42:28 +09001657 }
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001658
Jungshik Jang339227d2014-08-25 15:37:20 +09001659 @ServiceThreadOnly
Jinsuk Kimc7eba0f2014-07-07 14:18:02 +09001660 void displayOsd(int messageId) {
Jungshik Jang339227d2014-08-25 15:37:20 +09001661 assertRunOnServiceThread();
1662 mService.displayOsd(messageId);
Jinsuk Kimb38cd682014-07-07 08:05:03 +09001663 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001664
Jungshik Jang2e8f1b62014-09-03 08:28:02 +09001665 @ServiceThreadOnly
1666 void displayOsd(int messageId, int extra) {
1667 assertRunOnServiceThread();
1668 mService.displayOsd(messageId, extra);
1669 }
1670
Jungshik Jangb6591b82014-07-23 16:10:23 +09001671 // Seq #54 and #55
1672 @ServiceThreadOnly
Jungshik Jang4480efa2014-09-04 17:08:34 +09001673 int startOneTouchRecord(int recorderAddress, byte[] recordSource) {
Jungshik Jangb6591b82014-07-23 16:10:23 +09001674 assertRunOnServiceThread();
1675 if (!mService.isControlEnabled()) {
1676 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001677 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001678 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001679 }
1680
1681 if (!checkRecorder(recorderAddress)) {
1682 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001683 announceOneTouchRecordResult(recorderAddress,
1684 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jang4480efa2014-09-04 17:08:34 +09001685 return Constants.ABORT_NOT_IN_CORRECT_MODE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001686 }
1687
1688 if (!checkRecordSource(recordSource)) {
1689 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001690 announceOneTouchRecordResult(recorderAddress,
1691 ONE_TOUCH_RECORD_FAIL_TO_RECORD_DISPLAYED_SCREEN);
Jinsuk Kimd47abef2015-01-17 07:38:24 +09001692 return Constants.ABORT_CANNOT_PROVIDE_SOURCE;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001693 }
1694
1695 addAndStartAction(new OneTouchRecordAction(this, recorderAddress, recordSource));
1696 Slog.i(TAG, "Start new [One Touch Record]-Target:" + recorderAddress + ", recordSource:"
1697 + Arrays.toString(recordSource));
Jungshik Jang4480efa2014-09-04 17:08:34 +09001698 return Constants.ABORT_NO_ERROR;
Jungshik Jangb6591b82014-07-23 16:10:23 +09001699 }
1700
1701 @ServiceThreadOnly
1702 void stopOneTouchRecord(int recorderAddress) {
1703 assertRunOnServiceThread();
1704 if (!mService.isControlEnabled()) {
1705 Slog.w(TAG, "Can not stop one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001706 announceOneTouchRecordResult(recorderAddress, ONE_TOUCH_RECORD_CEC_DISABLED);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001707 return;
1708 }
1709
1710 if (!checkRecorder(recorderAddress)) {
1711 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001712 announceOneTouchRecordResult(recorderAddress,
1713 ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001714 return;
1715 }
1716
1717 // Remove one touch record action so that other one touch record can be started.
1718 removeAction(OneTouchRecordAction.class);
1719 mService.sendCecCommand(HdmiCecMessageBuilder.buildRecordOff(mAddress, recorderAddress));
1720 Slog.i(TAG, "Stop [One Touch Record]-Target:" + recorderAddress);
1721 }
1722
1723 private boolean checkRecorder(int recorderAddress) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001724 HdmiDeviceInfo device = getCecDeviceInfo(recorderAddress);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001725 return (device != null)
1726 && (HdmiUtils.getTypeFromAddress(recorderAddress)
Jungshik Jang61f4fbd2014-08-06 19:21:12 +09001727 == HdmiDeviceInfo.DEVICE_RECORDER);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001728 }
1729
1730 private boolean checkRecordSource(byte[] recordSource) {
1731 return (recordSource != null) && HdmiRecordSources.checkRecordSource(recordSource);
1732 }
1733
1734 @ServiceThreadOnly
1735 void startTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1736 assertRunOnServiceThread();
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001737 if (!mService.isControlEnabled()) {
1738 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001739 announceTimerRecordingResult(recorderAddress,
1740 TIMER_RECORDING_RESULT_EXTRA_CEC_DISABLED);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001741 return;
1742 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001743
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001744 if (!checkRecorder(recorderAddress)) {
1745 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001746 announceTimerRecordingResult(recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001747 TIMER_RECORDING_RESULT_EXTRA_CHECK_RECORDER_CONNECTION);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001748 return;
1749 }
1750
1751 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1752 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
1753 announceTimerRecordingResult(
Jungshik Jang326aef02014-11-05 12:50:35 +09001754 recorderAddress,
Jungshik Jange5a93372014-07-25 13:41:14 +09001755 TIMER_RECORDING_RESULT_EXTRA_FAIL_TO_RECORD_SELECTED_SOURCE);
Jungshik Jang12e5dce2014-07-24 15:27:44 +09001756 return;
1757 }
1758
1759 addAndStartAction(
1760 new TimerRecordingAction(this, recorderAddress, sourceType, recordSource));
1761 Slog.i(TAG, "Start [Timer Recording]-Target:" + recorderAddress + ", SourceType:"
1762 + sourceType + ", RecordSource:" + Arrays.toString(recordSource));
1763 }
1764
1765 private boolean checkTimerRecordingSource(int sourceType, byte[] recordSource) {
1766 return (recordSource != null)
1767 && HdmiTimerRecordSources.checkTimerRecordSource(sourceType, recordSource);
Jungshik Jangb6591b82014-07-23 16:10:23 +09001768 }
1769
1770 @ServiceThreadOnly
1771 void clearTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) {
1772 assertRunOnServiceThread();
Jungshik Jange5a93372014-07-25 13:41:14 +09001773 if (!mService.isControlEnabled()) {
1774 Slog.w(TAG, "Can not start one touch record. CEC control is disabled.");
Jungshik Jang326aef02014-11-05 12:50:35 +09001775 announceClearTimerRecordingResult(recorderAddress, CLEAR_TIMER_STATUS_CEC_DISABLE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001776 return;
1777 }
Jungshik Jangb6591b82014-07-23 16:10:23 +09001778
Jungshik Jange5a93372014-07-25 13:41:14 +09001779 if (!checkRecorder(recorderAddress)) {
1780 Slog.w(TAG, "Invalid recorder address:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001781 announceClearTimerRecordingResult(recorderAddress,
1782 CLEAR_TIMER_STATUS_CHECK_RECORDER_CONNECTION);
Jungshik Jange5a93372014-07-25 13:41:14 +09001783 return;
1784 }
1785
1786 if (!checkTimerRecordingSource(sourceType, recordSource)) {
1787 Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource));
Jungshik Jang326aef02014-11-05 12:50:35 +09001788 announceClearTimerRecordingResult(recorderAddress,
1789 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001790 return;
1791 }
1792
1793 sendClearTimerMessage(recorderAddress, sourceType, recordSource);
1794 }
1795
Jungshik Jang326aef02014-11-05 12:50:35 +09001796 private void sendClearTimerMessage(final int recorderAddress, int sourceType,
1797 byte[] recordSource) {
Jungshik Jange5a93372014-07-25 13:41:14 +09001798 HdmiCecMessage message = null;
1799 switch (sourceType) {
1800 case TIMER_RECORDING_TYPE_DIGITAL:
1801 message = HdmiCecMessageBuilder.buildClearDigitalTimer(mAddress, recorderAddress,
1802 recordSource);
1803 break;
1804 case TIMER_RECORDING_TYPE_ANALOGUE:
1805 message = HdmiCecMessageBuilder.buildClearAnalogueTimer(mAddress, recorderAddress,
1806 recordSource);
1807 break;
1808 case TIMER_RECORDING_TYPE_EXTERNAL:
1809 message = HdmiCecMessageBuilder.buildClearExternalTimer(mAddress, recorderAddress,
1810 recordSource);
1811 break;
1812 default:
1813 Slog.w(TAG, "Invalid source type:" + recorderAddress);
Jungshik Jang326aef02014-11-05 12:50:35 +09001814 announceClearTimerRecordingResult(recorderAddress,
1815 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001816 return;
1817
1818 }
1819 mService.sendCecCommand(message, new SendMessageCallback() {
1820 @Override
1821 public void onSendCompleted(int error) {
1822 if (error != Constants.SEND_RESULT_SUCCESS) {
Jungshik Jang326aef02014-11-05 12:50:35 +09001823 announceClearTimerRecordingResult(recorderAddress,
Jungshik Jangfaa49bc2014-08-05 16:10:21 +09001824 CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE);
Jungshik Jange5a93372014-07-25 13:41:14 +09001825 }
1826 }
1827 });
Jungshik Jangb6591b82014-07-23 16:10:23 +09001828 }
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001829
1830 void updateDevicePowerStatus(int logicalAddress, int newPowerStatus) {
Jinsuk Kim8960d1b2014-08-13 10:48:30 +09001831 HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001832 if (info == null) {
1833 Slog.w(TAG, "Can not update power status of non-existing device:" + logicalAddress);
1834 return;
1835 }
1836
1837 if (info.getDevicePowerStatus() == newPowerStatus) {
1838 return;
1839 }
1840
1841 HdmiDeviceInfo newInfo = HdmiUtils.cloneHdmiDeviceInfo(info, newPowerStatus);
1842 // addDeviceInfo replaces old device info with new one if exists.
1843 addDeviceInfo(newInfo);
1844
Jungshik Jang61daf6b2014-08-08 11:38:28 +09001845 invokeDeviceEventListener(newInfo, HdmiControlManager.DEVICE_EVENT_UPDATE_DEVICE);
Jungshik Jang410ca9c2014-08-07 18:04:14 +09001846 }
Terry Heo959d2db2014-08-28 16:45:41 +09001847
1848 @Override
Yuncheol Heo184b1242014-09-12 15:09:07 +09001849 protected boolean handleMenuStatus(HdmiCecMessage message) {
1850 // Do nothing and just return true not to prevent from responding <Feature Abort>.
1851 return true;
1852 }
1853
1854 @Override
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001855 protected void sendStandby(int deviceId) {
1856 HdmiDeviceInfo targetDevice = mDeviceInfos.get(deviceId);
1857 if (targetDevice == null) {
1858 return;
1859 }
1860 int targetAddress = targetDevice.getLogicalAddress();
1861 mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby(mAddress, targetAddress));
1862 }
1863
Jinsuk Kim7fa3a662014-11-07 15:20:24 +09001864 @ServiceThreadOnly
1865 void processAllDelayedMessages() {
1866 assertRunOnServiceThread();
1867 mDelayedMessageBuffer.processAllMessages();
1868 }
1869
1870 @ServiceThreadOnly
1871 void processDelayedMessages(int address) {
1872 assertRunOnServiceThread();
1873 mDelayedMessageBuffer.processMessagesForDevice(address);
1874 }
1875
Jinsuk Kim6e26f7f2015-01-07 16:45:14 +09001876 @ServiceThreadOnly
1877 void processDelayedActiveSource(int address) {
1878 assertRunOnServiceThread();
1879 mDelayedMessageBuffer.processActiveSource(address);
1880 }
1881
Jinsuk Kimd4a94db2014-09-12 13:51:10 +09001882 @Override
Terry Heo959d2db2014-08-28 16:45:41 +09001883 protected void dump(final IndentingPrintWriter pw) {
1884 super.dump(pw);
1885 pw.println("mArcEstablished: " + mArcEstablished);
1886 pw.println("mArcFeatureEnabled: " + mArcFeatureEnabled);
1887 pw.println("mSystemAudioActivated: " + mSystemAudioActivated);
1888 pw.println("mSystemAudioMute: " + mSystemAudioMute);
1889 pw.println("mAutoDeviceOff: " + mAutoDeviceOff);
1890 pw.println("mAutoWakeup: " + mAutoWakeup);
1891 pw.println("mSkipRoutingControl: " + mSkipRoutingControl);
Jinsuk Kimcb8661c2015-01-19 12:39:06 +09001892 pw.println("mPrevPortId: " + mPrevPortId);
Jinsuk Kim4b4b9402014-09-02 10:30:10 +09001893 pw.println("CEC devices:");
1894 pw.increaseIndent();
1895 for (HdmiDeviceInfo info : mSafeAllDeviceInfos) {
1896 pw.println(info);
1897 }
1898 pw.decreaseIndent();
Terry Heo959d2db2014-08-28 16:45:41 +09001899 }
Jinsuk Kim2918e9e2014-05-20 16:45:45 +09001900}