Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.hdmi; |
| 18 | |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 19 | import static android.hardware.hdmi.HdmiControlManager.CLEAR_TIMER_STATUS_CEC_DISABLE; |
| 20 | import static android.hardware.hdmi.HdmiControlManager.CLEAR_TIMER_STATUS_CHECK_RECORDER_CONNECTION; |
| 21 | import static android.hardware.hdmi.HdmiControlManager.CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE; |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 22 | import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_CEC_DISABLED; |
| 23 | import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION; |
| 24 | import static android.hardware.hdmi.HdmiControlManager.ONE_TOUCH_RECORD_FAIL_TO_RECORD_DISPLAYED_SCREEN; |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 25 | import static android.hardware.hdmi.HdmiControlManager.OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT; |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 26 | import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_RESULT_EXTRA_CEC_DISABLED; |
| 27 | import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_RESULT_EXTRA_CHECK_RECORDER_CONNECTION; |
| 28 | import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_RESULT_EXTRA_FAIL_TO_RECORD_SELECTED_SOURCE; |
| 29 | import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_TYPE_ANALOGUE; |
| 30 | import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_TYPE_DIGITAL; |
| 31 | import static android.hardware.hdmi.HdmiControlManager.TIMER_RECORDING_TYPE_EXTERNAL; |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 32 | |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 33 | import android.hardware.hdmi.HdmiControlManager; |
Jinsuk Kim | 5008486 | 2014-08-07 13:11:40 +0900 | [diff] [blame] | 34 | import android.hardware.hdmi.HdmiDeviceInfo; |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 35 | import android.hardware.hdmi.HdmiRecordSources; |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 36 | import android.hardware.hdmi.HdmiTimerRecordSources; |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 37 | import android.hardware.hdmi.IHdmiControlCallback; |
Jungshik Jang | b69aafb | 2014-07-11 16:29:06 +0900 | [diff] [blame] | 38 | import android.media.AudioManager; |
Jungshik Jang | a858d22 | 2014-06-23 17:17:47 +0900 | [diff] [blame] | 39 | import android.media.AudioSystem; |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 40 | import android.os.RemoteException; |
Jinsuk Kim | af2acf0 | 2014-07-11 18:43:04 +0900 | [diff] [blame] | 41 | import android.os.SystemProperties; |
Jinsuk Kim | 7ecfbae | 2014-07-11 14:16:29 +0900 | [diff] [blame] | 42 | import android.provider.Settings.Global; |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 43 | import android.util.ArraySet; |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 44 | import android.util.Slog; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 45 | import android.util.SparseArray; |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 46 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 47 | import com.android.internal.annotations.GuardedBy; |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame] | 48 | import com.android.internal.util.IndentingPrintWriter; |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 49 | import com.android.server.hdmi.DeviceDiscoveryAction.DeviceDiscoveryCallback; |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 50 | import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly; |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 51 | import com.android.server.hdmi.HdmiControlService.SendMessageCallback; |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 52 | |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 53 | import java.io.UnsupportedEncodingException; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 54 | import java.util.ArrayList; |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 55 | import java.util.Arrays; |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 56 | import java.util.Collection; |
Jinsuk Kim | 0a3316b | 2014-06-14 09:33:55 +0900 | [diff] [blame] | 57 | import java.util.Collections; |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 58 | import java.util.Iterator; |
Jinsuk Kim | 0a3316b | 2014-06-14 09:33:55 +0900 | [diff] [blame] | 59 | import java.util.List; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Represent a logical device of type TV residing in Android system. |
| 63 | */ |
| 64 | final class HdmiCecLocalDeviceTv extends HdmiCecLocalDevice { |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 65 | private static final String TAG = "HdmiCecLocalDeviceTv"; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 66 | |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 67 | // Whether ARC is available or not. "true" means that ARC is established between TV and |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 68 | // AVR as audio receiver. |
| 69 | @ServiceThreadOnly |
| 70 | private boolean mArcEstablished = false; |
| 71 | |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 72 | // Whether ARC feature is enabled or not. The default value is true. |
| 73 | // TODO: once adding system setting for it, read the value to it. |
| 74 | private boolean mArcFeatureEnabled = true; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 75 | |
Jungshik Jang | 377dcbd | 2014-07-15 15:49:02 +0900 | [diff] [blame] | 76 | // Whether System audio mode is activated or not. |
| 77 | // This becomes true only when all system audio sequences are finished. |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 78 | @GuardedBy("mLock") |
Jungshik Jang | 377dcbd | 2014-07-15 15:49:02 +0900 | [diff] [blame] | 79 | private boolean mSystemAudioActivated = false; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 80 | |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 81 | // The previous port id (input) before switching to the new one. This is remembered in order to |
| 82 | // be able to switch to it upon receiving <Inactive Source> from currently active source. |
| 83 | // This remains valid only when the active source was switched via one touch play operation |
| 84 | // (either by TV or source device). Manual port switching invalidates this value to |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 85 | // Constants.PORT_INVALID, for which case <Inactive Source> does not do anything. |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 86 | @GuardedBy("mLock") |
| 87 | private int mPrevPortId; |
| 88 | |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 89 | @GuardedBy("mLock") |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 90 | private int mSystemAudioVolume = Constants.UNKNOWN_VOLUME; |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 91 | |
| 92 | @GuardedBy("mLock") |
| 93 | private boolean mSystemAudioMute = false; |
| 94 | |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 95 | // Copy of mDeviceInfos to guarantee thread-safety. |
| 96 | @GuardedBy("mLock") |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 97 | private List<HdmiDeviceInfo> mSafeAllDeviceInfos = Collections.emptyList(); |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 98 | // All external cec input(source) devices. Does not include system audio device. |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 99 | @GuardedBy("mLock") |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 100 | private List<HdmiDeviceInfo> mSafeExternalInputs = Collections.emptyList(); |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 101 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 102 | // Map-like container of all cec devices including local ones. |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 103 | // device id is used as key of container. |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 104 | // This is not thread-safe. For external purpose use mSafeDeviceInfos. |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 105 | private final SparseArray<HdmiDeviceInfo> mDeviceInfos = new SparseArray<>(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 106 | |
Jinsuk Kim | 160a6e5 | 2014-07-02 06:16:36 +0900 | [diff] [blame] | 107 | // If true, TV going to standby mode puts other devices also to standby. |
| 108 | private boolean mAutoDeviceOff; |
| 109 | |
Jinsuk Kim | 544b62b | 2014-07-14 14:01:23 +0900 | [diff] [blame] | 110 | // If true, TV wakes itself up when receiving <Text/Image View On>. |
| 111 | private boolean mAutoWakeup; |
| 112 | |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 113 | private final HdmiCecStandbyModeHandler mStandbyHandler; |
| 114 | |
Jinsuk Kim | d530719 | 2014-08-22 09:07:40 +0900 | [diff] [blame] | 115 | // If true, do not do routing control/send active source for internal source. |
| 116 | // Set to true when the device was woken up by <Text/Image View On>. |
| 117 | private boolean mSkipRoutingControl; |
| 118 | |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 119 | // Set of physical addresses of CEC switches on the CEC bus. Managed independently from |
| 120 | // other CEC devices since they might not have logical address. |
| 121 | private final ArraySet<Integer> mCecSwitches = new ArraySet<Integer>(); |
| 122 | |
Jungshik Jang | 473119f | 2014-08-27 16:43:22 +0900 | [diff] [blame] | 123 | private final HdmiLogger mSafeLogger = new HdmiLogger(TAG); |
| 124 | |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 125 | HdmiCecLocalDeviceTv(HdmiControlService service) { |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 126 | super(service, HdmiDeviceInfo.DEVICE_TV); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 127 | mPrevPortId = Constants.INVALID_PORT_ID; |
Jinsuk Kim | 544b62b | 2014-07-14 14:01:23 +0900 | [diff] [blame] | 128 | mAutoDeviceOff = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED, |
| 129 | true); |
| 130 | mAutoWakeup = mService.readBooleanSetting(Global.HDMI_CONTROL_AUTO_WAKEUP_ENABLED, true); |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 131 | mStandbyHandler = new HdmiCecStandbyModeHandler(service, this); |
Jungshik Jang | 8b308d9 | 2014-05-29 21:52:28 +0900 | [diff] [blame] | 132 | } |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 133 | |
Jungshik Jang | 8b308d9 | 2014-05-29 21:52:28 +0900 | [diff] [blame] | 134 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 135 | @ServiceThreadOnly |
Yuncheol Heo | fc44e4e | 2014-08-04 19:41:09 +0900 | [diff] [blame] | 136 | protected void onAddressAllocated(int logicalAddress, int reason) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 137 | assertRunOnServiceThread(); |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 138 | mService.sendCecCommand(HdmiCecMessageBuilder.buildReportPhysicalAddressCommand( |
| 139 | mAddress, mService.getPhysicalAddress(), mDeviceType)); |
| 140 | mService.sendCecCommand(HdmiCecMessageBuilder.buildDeviceVendorIdCommand( |
| 141 | mAddress, mService.getVendorId())); |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 142 | mCecSwitches.add(mService.getPhysicalAddress()); // TV is a CEC switch too. |
Jinsuk Kim | d530719 | 2014-08-22 09:07:40 +0900 | [diff] [blame] | 143 | mSkipRoutingControl = (reason == HdmiControlService.INITIATED_BY_WAKE_UP_MESSAGE); |
Yuncheol Heo | fc44e4e | 2014-08-04 19:41:09 +0900 | [diff] [blame] | 144 | launchRoutingControl(reason != HdmiControlService.INITIATED_BY_ENABLE_CEC && |
| 145 | reason != HdmiControlService.INITIATED_BY_BOOT_UP); |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 146 | launchDeviceDiscovery(); |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 147 | } |
| 148 | |
Jinsuk Kim | af2acf0 | 2014-07-11 18:43:04 +0900 | [diff] [blame] | 149 | @Override |
| 150 | @ServiceThreadOnly |
| 151 | protected int getPreferredAddress() { |
| 152 | assertRunOnServiceThread(); |
| 153 | return SystemProperties.getInt(Constants.PROPERTY_PREFERRED_ADDRESS_TV, |
| 154 | Constants.ADDR_UNREGISTERED); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | @ServiceThreadOnly |
| 159 | protected void setPreferredAddress(int addr) { |
| 160 | assertRunOnServiceThread(); |
Jinsuk Kim | 544b62b | 2014-07-14 14:01:23 +0900 | [diff] [blame] | 161 | SystemProperties.set(Constants.PROPERTY_PREFERRED_ADDRESS_TV, String.valueOf(addr)); |
Jinsuk Kim | af2acf0 | 2014-07-11 18:43:04 +0900 | [diff] [blame] | 162 | } |
| 163 | |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 164 | @Override |
| 165 | @ServiceThreadOnly |
| 166 | boolean dispatchMessage(HdmiCecMessage message) { |
| 167 | assertRunOnServiceThread(); |
| 168 | if (mService.isPowerStandby() && mStandbyHandler.handleCommand(message)) { |
| 169 | return true; |
| 170 | } |
| 171 | return super.onMessage(message); |
| 172 | } |
| 173 | |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 174 | /** |
| 175 | * Performs the action 'device select', or 'one touch play' initiated by TV. |
| 176 | * |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 177 | * @param id id of HDMI device to select |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 178 | * @param callback callback object to report the result with |
| 179 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 180 | @ServiceThreadOnly |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 181 | void deviceSelect(int id, IHdmiControlCallback callback) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 182 | assertRunOnServiceThread(); |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 183 | HdmiDeviceInfo targetDevice = mDeviceInfos.get(id); |
| 184 | if (targetDevice == null) { |
| 185 | invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE); |
| 186 | return; |
| 187 | } |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 188 | int targetAddress = targetDevice.getLogicalAddress(); |
Jinsuk Kim | 58500f4 | 2014-08-05 12:48:35 +0900 | [diff] [blame] | 189 | ActiveSource active = getActiveSource(); |
| 190 | if (active.isValid() && targetAddress == active.logicalAddress) { |
| 191 | invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS); |
| 192 | return; |
| 193 | } |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 194 | if (targetAddress == Constants.ADDR_INTERNAL) { |
Jinsuk Kim | 5ad5716 | 2014-07-21 13:31:45 +0900 | [diff] [blame] | 195 | handleSelectInternalSource(); |
| 196 | // Switching to internal source is always successful even when CEC control is disabled. |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 197 | setActiveSource(targetAddress, mService.getPhysicalAddress()); |
Jinsuk Kim | 7e74206 | 2014-07-30 13:19:13 +0900 | [diff] [blame] | 198 | setActivePath(mService.getPhysicalAddress()); |
Jinsuk Kim | 5ad5716 | 2014-07-21 13:31:45 +0900 | [diff] [blame] | 199 | invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS); |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 200 | return; |
| 201 | } |
Jinsuk Kim | 09ffc84 | 2014-07-11 17:04:32 +0900 | [diff] [blame] | 202 | if (!mService.isControlEnabled()) { |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 203 | setActiveSource(targetDevice); |
Jinsuk Kim | 09ffc84 | 2014-07-11 17:04:32 +0900 | [diff] [blame] | 204 | invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE); |
| 205 | return; |
| 206 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 207 | removeAction(DeviceSelectAction.class); |
| 208 | addAndStartAction(new DeviceSelectAction(this, targetDevice, callback)); |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 209 | } |
| 210 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 211 | @ServiceThreadOnly |
Jinsuk Kim | 5ad5716 | 2014-07-21 13:31:45 +0900 | [diff] [blame] | 212 | private void handleSelectInternalSource() { |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 213 | assertRunOnServiceThread(); |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 214 | // Seq #18 |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 215 | if (mService.isControlEnabled() && mActiveSource.logicalAddress != mAddress) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 216 | updateActiveSource(mAddress, mService.getPhysicalAddress()); |
Jinsuk Kim | d530719 | 2014-08-22 09:07:40 +0900 | [diff] [blame] | 217 | if (mSkipRoutingControl) { |
| 218 | mSkipRoutingControl = false; |
| 219 | return; |
| 220 | } |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 221 | HdmiCecMessage activeSource = HdmiCecMessageBuilder.buildActiveSource( |
| 222 | mAddress, mService.getPhysicalAddress()); |
| 223 | mService.sendCecCommand(activeSource); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | @ServiceThreadOnly |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 228 | void updateActiveSource(int logicalAddress, int physicalAddress) { |
| 229 | assertRunOnServiceThread(); |
| 230 | updateActiveSource(ActiveSource.of(logicalAddress, physicalAddress)); |
| 231 | } |
| 232 | |
| 233 | @ServiceThreadOnly |
| 234 | void updateActiveSource(ActiveSource newActive) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 235 | assertRunOnServiceThread(); |
| 236 | // Seq #14 |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 237 | if (mActiveSource.equals(newActive)) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 238 | return; |
| 239 | } |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 240 | setActiveSource(newActive); |
| 241 | int logicalAddress = newActive.logicalAddress; |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 242 | if (getCecDeviceInfo(logicalAddress) != null && logicalAddress != mAddress) { |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 243 | if (mService.pathToPortId(newActive.physicalAddress) == getActivePortId()) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 244 | setPrevPortId(getActivePortId()); |
| 245 | } |
| 246 | // TODO: Show the OSD banner related to the new active source device. |
| 247 | } else { |
| 248 | // TODO: If displayed, remove the OSD banner related to the previous |
| 249 | // active source device. |
| 250 | } |
| 251 | } |
| 252 | |
Jinsuk Kim | 2b15201 | 2014-07-25 08:22:26 +0900 | [diff] [blame] | 253 | int getPortId(int physicalAddress) { |
| 254 | return mService.pathToPortId(physicalAddress); |
| 255 | } |
| 256 | |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 257 | /** |
| 258 | * Returns the previous port id kept to handle input switching on <Inactive Source>. |
| 259 | */ |
| 260 | int getPrevPortId() { |
| 261 | synchronized (mLock) { |
| 262 | return mPrevPortId; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Sets the previous port id. INVALID_PORT_ID invalidates it, hence no actions will be |
| 268 | * taken for <Inactive Source>. |
| 269 | */ |
| 270 | void setPrevPortId(int portId) { |
| 271 | synchronized (mLock) { |
| 272 | mPrevPortId = portId; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | @ServiceThreadOnly |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 277 | void updateActiveInput(int path, boolean notifyInputChange) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 278 | assertRunOnServiceThread(); |
| 279 | // Seq #15 |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 280 | if (path == getActivePath()) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 281 | return; |
| 282 | } |
Jinsuk Kim | 7c3a956 | 2014-08-01 11:07:42 +0900 | [diff] [blame] | 283 | setPrevPortId(getActivePortId()); |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 284 | setActivePath(path); |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 285 | // TODO: Handle PAP/PIP case. |
| 286 | // Show OSD port change banner |
| 287 | if (notifyInputChange) { |
| 288 | ActiveSource activeSource = getActiveSource(); |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 289 | HdmiDeviceInfo info = getCecDeviceInfo(activeSource.logicalAddress); |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 290 | if (info == null) { |
Jinsuk Kim | 4b4b940 | 2014-09-02 10:30:10 +0900 | [diff] [blame] | 291 | info = new HdmiDeviceInfo(Constants.ADDR_INVALID, path, getActivePortId(), |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 292 | HdmiDeviceInfo.DEVICE_RESERVED, 0, null); |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 293 | } |
| 294 | mService.invokeInputChangeListener(info); |
| 295 | } |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @ServiceThreadOnly |
| 299 | void doManualPortSwitching(int portId, IHdmiControlCallback callback) { |
| 300 | assertRunOnServiceThread(); |
| 301 | // Seq #20 |
Jinsuk Kim | 09ffc84 | 2014-07-11 17:04:32 +0900 | [diff] [blame] | 302 | if (!mService.isValidPortId(portId)) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 303 | invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE); |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 304 | return; |
| 305 | } |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 306 | if (portId == getActivePortId()) { |
| 307 | invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS); |
| 308 | return; |
| 309 | } |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 310 | mActiveSource.invalidate(); |
Jinsuk Kim | 09ffc84 | 2014-07-11 17:04:32 +0900 | [diff] [blame] | 311 | if (!mService.isControlEnabled()) { |
| 312 | setActivePortId(portId); |
| 313 | invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE); |
| 314 | return; |
| 315 | } |
Jinsuk Kim | 1f8d1c5 | 2014-08-12 15:39:53 +0900 | [diff] [blame] | 316 | int oldPath = getActivePortId() != Constants.INVALID_PORT_ID |
| 317 | ? mService.portIdToPath(getActivePortId()) : getDeviceInfo().getPhysicalAddress(); |
Jinsuk Kim | d530719 | 2014-08-22 09:07:40 +0900 | [diff] [blame] | 318 | setActivePath(oldPath); |
| 319 | if (mSkipRoutingControl) { |
| 320 | mSkipRoutingControl = false; |
| 321 | return; |
| 322 | } |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 323 | int newPath = mService.portIdToPath(portId); |
| 324 | HdmiCecMessage routingChange = |
| 325 | HdmiCecMessageBuilder.buildRoutingChange(mAddress, oldPath, newPath); |
| 326 | mService.sendCecCommand(routingChange); |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 327 | removeAction(RoutingControlAction.class); |
Jinsuk Kim | 2626958 | 2014-08-21 14:31:40 +0900 | [diff] [blame] | 328 | addAndStartAction(new RoutingControlAction(this, newPath, true, callback)); |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 329 | } |
| 330 | |
Jungshik Jang | f67113f | 2014-08-22 16:27:19 +0900 | [diff] [blame] | 331 | @ServiceThreadOnly |
Jinsuk Kim | cae6627 | 2014-07-04 13:26:44 +0900 | [diff] [blame] | 332 | int getPowerStatus() { |
Jungshik Jang | f67113f | 2014-08-22 16:27:19 +0900 | [diff] [blame] | 333 | assertRunOnServiceThread(); |
Jinsuk Kim | cae6627 | 2014-07-04 13:26:44 +0900 | [diff] [blame] | 334 | return mService.getPowerStatus(); |
| 335 | } |
| 336 | |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 337 | /** |
| 338 | * Sends key to a target CEC device. |
| 339 | * |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 340 | * @param keyCode key code to send. Defined in {@link android.view.KeyEvent}. |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 341 | * @param isPressed true if this is key press event |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 342 | */ |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 343 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 344 | @ServiceThreadOnly |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 345 | protected void sendKeyEvent(int keyCode, boolean isPressed) { |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 346 | assertRunOnServiceThread(); |
Dongil Seo | b64c2ba | 2014-08-26 19:41:39 +0900 | [diff] [blame] | 347 | if (!HdmiCecKeycode.isSupportedKeycode(keyCode)) { |
| 348 | Slog.w(TAG, "Unsupported key: " + keyCode); |
| 349 | return; |
| 350 | } |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 351 | List<SendKeyAction> action = getActions(SendKeyAction.class); |
| 352 | if (!action.isEmpty()) { |
| 353 | action.get(0).processKeyEvent(keyCode, isPressed); |
| 354 | } else { |
Jinsuk Kim | 1f8d1c5 | 2014-08-12 15:39:53 +0900 | [diff] [blame] | 355 | if (isPressed && getActiveSource().isValid()) { |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 356 | int logicalAddress = getActiveSource().logicalAddress; |
| 357 | addAndStartAction(new SendKeyAction(this, logicalAddress, keyCode)); |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 358 | } else { |
Jinsuk Kim | 1f8d1c5 | 2014-08-12 15:39:53 +0900 | [diff] [blame] | 359 | Slog.w(TAG, "Discard key event: " + keyCode + " pressed:" + isPressed); |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 364 | private static void invokeCallback(IHdmiControlCallback callback, int result) { |
Jinsuk Kim | 4893c7e | 2014-06-19 14:13:22 +0900 | [diff] [blame] | 365 | if (callback == null) { |
| 366 | return; |
| 367 | } |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 368 | try { |
| 369 | callback.onComplete(result); |
| 370 | } catch (RemoteException e) { |
| 371 | Slog.e(TAG, "Invoking callback failed:" + e); |
| 372 | } |
| 373 | } |
| 374 | |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 375 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 376 | @ServiceThreadOnly |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 377 | protected boolean handleActiveSource(HdmiCecMessage message) { |
| 378 | assertRunOnServiceThread(); |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 379 | int logicalAddress = message.getSource(); |
| 380 | int physicalAddress = HdmiUtils.twoBytesToInt(message.getParams()); |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 381 | if (getCecDeviceInfo(logicalAddress) == null) { |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 382 | handleNewDeviceAtTheTailOfActivePath(physicalAddress); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 383 | } else { |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 384 | ActiveSource activeSource = ActiveSource.of(logicalAddress, physicalAddress); |
| 385 | ActiveSourceHandler.create(this, null).process(activeSource); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 386 | } |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 387 | return true; |
| 388 | } |
| 389 | |
| 390 | @Override |
| 391 | @ServiceThreadOnly |
| 392 | protected boolean handleInactiveSource(HdmiCecMessage message) { |
| 393 | assertRunOnServiceThread(); |
| 394 | // Seq #10 |
| 395 | |
| 396 | // Ignore <Inactive Source> from non-active source device. |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 397 | if (getActiveSource().logicalAddress != message.getSource()) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 398 | return true; |
| 399 | } |
Jinsuk Kim | 4d43d93 | 2014-07-03 16:43:58 +0900 | [diff] [blame] | 400 | if (isProhibitMode()) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 401 | return true; |
| 402 | } |
| 403 | int portId = getPrevPortId(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 404 | if (portId != Constants.INVALID_PORT_ID) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 405 | // TODO: Do this only if TV is not showing multiview like PIP/PAP. |
| 406 | |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 407 | HdmiDeviceInfo inactiveSource = getCecDeviceInfo(message.getSource()); |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 408 | if (inactiveSource == null) { |
| 409 | return true; |
| 410 | } |
| 411 | if (mService.pathToPortId(inactiveSource.getPhysicalAddress()) == portId) { |
| 412 | return true; |
| 413 | } |
| 414 | // TODO: Switch the TV freeze mode off |
| 415 | |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 416 | doManualPortSwitching(portId, null); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 417 | setPrevPortId(Constants.INVALID_PORT_ID); |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 418 | } |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | @ServiceThreadOnly |
| 424 | protected boolean handleRequestActiveSource(HdmiCecMessage message) { |
| 425 | assertRunOnServiceThread(); |
| 426 | // Seq #19 |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 427 | if (mAddress == getActiveSource().logicalAddress) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 428 | mService.sendCecCommand( |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 429 | HdmiCecMessageBuilder.buildActiveSource(mAddress, getActivePath())); |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 430 | } |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 436 | protected boolean handleGetMenuLanguage(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 437 | assertRunOnServiceThread(); |
Jungshik Jang | f67113f | 2014-08-22 16:27:19 +0900 | [diff] [blame] | 438 | if (!broadcastMenuLanguage(mService.getLanguage())) { |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 439 | Slog.w(TAG, "Failed to respond to <Get Menu Language>: " + message.toString()); |
| 440 | } |
| 441 | return true; |
| 442 | } |
| 443 | |
Terry Heo | 1ca0a43 | 2014-08-18 10:30:32 +0900 | [diff] [blame] | 444 | @ServiceThreadOnly |
| 445 | boolean broadcastMenuLanguage(String language) { |
| 446 | assertRunOnServiceThread(); |
| 447 | HdmiCecMessage command = HdmiCecMessageBuilder.buildSetMenuLanguageCommand( |
| 448 | mAddress, language); |
| 449 | if (command != null) { |
| 450 | mService.sendCecCommand(command); |
| 451 | return true; |
| 452 | } |
| 453 | return false; |
| 454 | } |
| 455 | |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 456 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 457 | @ServiceThreadOnly |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 458 | protected boolean handleReportPhysicalAddress(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 459 | assertRunOnServiceThread(); |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 460 | int path = HdmiUtils.twoBytesToInt(message.getParams()); |
| 461 | int address = message.getSource(); |
Jinsuk Kim | bcfa067 | 2014-08-11 11:56:58 +0900 | [diff] [blame] | 462 | int type = message.getParams()[2]; |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 463 | |
Jinsuk Kim | bcfa067 | 2014-08-11 11:56:58 +0900 | [diff] [blame] | 464 | if (updateCecSwitchInfo(address, type, path)) return true; |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 465 | |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 466 | // Ignore if [Device Discovery Action] is going on. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 467 | if (hasAction(DeviceDiscoveryAction.class)) { |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 468 | Slog.i(TAG, "Ignored while Device Discovery Action is in progress: " + message); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 469 | return true; |
| 470 | } |
| 471 | |
Jinsuk Kim | 4b4b940 | 2014-09-02 10:30:10 +0900 | [diff] [blame] | 472 | if (!isInDeviceList(address, path)) { |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 473 | handleNewDeviceAtTheTailOfActivePath(path); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 474 | } |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 475 | startNewDeviceAction(ActiveSource.of(address, path)); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 476 | return true; |
| 477 | } |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 478 | |
Jinsuk Kim | bcfa067 | 2014-08-11 11:56:58 +0900 | [diff] [blame] | 479 | boolean updateCecSwitchInfo(int address, int type, int path) { |
| 480 | if (address == Constants.ADDR_UNREGISTERED |
| 481 | && type == HdmiDeviceInfo.DEVICE_PURE_CEC_SWITCH) { |
| 482 | mCecSwitches.add(path); |
| 483 | updateSafeDeviceInfoList(); |
| 484 | return true; // Pure switch does not need further processing. Return here. |
| 485 | } |
| 486 | if (type == HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM) { |
| 487 | mCecSwitches.add(path); |
| 488 | } |
| 489 | return false; |
| 490 | } |
| 491 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 492 | void startNewDeviceAction(ActiveSource activeSource) { |
Jungshik Jang | 97affee | 2014-07-11 17:04:43 +0900 | [diff] [blame] | 493 | for (NewDeviceAction action : getActions(NewDeviceAction.class)) { |
| 494 | // If there is new device action which has the same logical address and path |
| 495 | // ignore new request. |
| 496 | // NewDeviceAction is created whenever it receives <Report Physical Address>. |
| 497 | // And there is a chance starting NewDeviceAction for the same source. |
| 498 | // Usually, new device sends <Report Physical Address> when it's plugged |
| 499 | // in. However, TV can detect a new device from HotPlugDetectionAction, |
| 500 | // which sends <Give Physical Address> to the source for newly detected |
| 501 | // device. |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 502 | if (action.isActionOf(activeSource)) { |
Jungshik Jang | 97affee | 2014-07-11 17:04:43 +0900 | [diff] [blame] | 503 | return; |
| 504 | } |
| 505 | } |
| 506 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 507 | addAndStartAction(new NewDeviceAction(this, activeSource.logicalAddress, |
| 508 | activeSource.physicalAddress)); |
Jungshik Jang | 97affee | 2014-07-11 17:04:43 +0900 | [diff] [blame] | 509 | } |
| 510 | |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 511 | private void handleNewDeviceAtTheTailOfActivePath(int path) { |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 512 | // Seq #22 |
| 513 | if (isTailOfActivePath(path, getActivePath())) { |
| 514 | removeAction(RoutingControlAction.class); |
| 515 | int newPath = mService.portIdToPath(getActivePortId()); |
Jinsuk Kim | 7c3a956 | 2014-08-01 11:07:42 +0900 | [diff] [blame] | 516 | setActivePath(newPath); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 517 | mService.sendCecCommand(HdmiCecMessageBuilder.buildRoutingChange( |
| 518 | mAddress, getActivePath(), newPath)); |
Jinsuk Kim | 7c3a956 | 2014-08-01 11:07:42 +0900 | [diff] [blame] | 519 | addAndStartAction(new RoutingControlAction(this, newPath, false, null)); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Whether the given path is located in the tail of current active path. |
| 525 | * |
| 526 | * @param path to be tested |
| 527 | * @param activePath current active path |
| 528 | * @return true if the given path is located in the tail of current active path; otherwise, |
| 529 | * false |
| 530 | */ |
| 531 | static boolean isTailOfActivePath(int path, int activePath) { |
| 532 | // If active routing path is internal source, return false. |
| 533 | if (activePath == 0) { |
| 534 | return false; |
| 535 | } |
| 536 | for (int i = 12; i >= 0; i -= 4) { |
| 537 | int curActivePath = (activePath >> i) & 0xF; |
| 538 | if (curActivePath == 0) { |
| 539 | return true; |
| 540 | } else { |
| 541 | int curPath = (path >> i) & 0xF; |
| 542 | if (curPath != curActivePath) { |
| 543 | return false; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | @Override |
| 551 | @ServiceThreadOnly |
| 552 | protected boolean handleRoutingChange(HdmiCecMessage message) { |
| 553 | assertRunOnServiceThread(); |
| 554 | // Seq #21 |
| 555 | byte[] params = message.getParams(); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 556 | int currentPath = HdmiUtils.twoBytesToInt(params); |
| 557 | if (HdmiUtils.isAffectingActiveRoutingPath(getActivePath(), currentPath)) { |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 558 | mActiveSource.invalidate(); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 559 | removeAction(RoutingControlAction.class); |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 560 | int newPath = HdmiUtils.twoBytesToInt(params, 2); |
Jinsuk Kim | 04fd280 | 2014-07-03 14:04:02 +0900 | [diff] [blame] | 561 | addAndStartAction(new RoutingControlAction(this, newPath, true, null)); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 562 | } |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 563 | return true; |
| 564 | } |
Jinsuk Kim | 0a3316b | 2014-06-14 09:33:55 +0900 | [diff] [blame] | 565 | |
| 566 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 567 | @ServiceThreadOnly |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 568 | protected boolean handleReportAudioStatus(HdmiCecMessage message) { |
| 569 | assertRunOnServiceThread(); |
| 570 | |
| 571 | byte params[] = message.getParams(); |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 572 | int mute = params[0] & 0x80; |
| 573 | int volume = params[0] & 0x7F; |
| 574 | setAudioStatus(mute == 0x80, volume); |
| 575 | return true; |
| 576 | } |
| 577 | |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 578 | @Override |
| 579 | @ServiceThreadOnly |
| 580 | protected boolean handleTextViewOn(HdmiCecMessage message) { |
| 581 | assertRunOnServiceThread(); |
Jinsuk Kim | 544b62b | 2014-07-14 14:01:23 +0900 | [diff] [blame] | 582 | if (mService.isPowerStandbyOrTransient() && mAutoWakeup) { |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 583 | mService.wakeUp(); |
| 584 | } |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 585 | return true; |
| 586 | } |
| 587 | |
| 588 | @Override |
| 589 | @ServiceThreadOnly |
| 590 | protected boolean handleImageViewOn(HdmiCecMessage message) { |
| 591 | assertRunOnServiceThread(); |
| 592 | // Currently, it's the same as <Text View On>. |
| 593 | return handleTextViewOn(message); |
| 594 | } |
| 595 | |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 596 | @Override |
| 597 | @ServiceThreadOnly |
| 598 | protected boolean handleSetOsdName(HdmiCecMessage message) { |
| 599 | int source = message.getSource(); |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 600 | HdmiDeviceInfo deviceInfo = getCecDeviceInfo(source); |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 601 | // If the device is not in device list, ignore it. |
| 602 | if (deviceInfo == null) { |
| 603 | Slog.e(TAG, "No source device info for <Set Osd Name>." + message); |
| 604 | return true; |
| 605 | } |
| 606 | String osdName = null; |
| 607 | try { |
| 608 | osdName = new String(message.getParams(), "US-ASCII"); |
| 609 | } catch (UnsupportedEncodingException e) { |
| 610 | Slog.e(TAG, "Invalid <Set Osd Name> request:" + message, e); |
| 611 | return true; |
| 612 | } |
| 613 | |
| 614 | if (deviceInfo.getDisplayName().equals(osdName)) { |
| 615 | Slog.i(TAG, "Ignore incoming <Set Osd Name> having same osd name:" + message); |
| 616 | return true; |
| 617 | } |
| 618 | |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 619 | addCecDevice(new HdmiDeviceInfo(deviceInfo.getLogicalAddress(), |
Jinsuk Kim | 2b15201 | 2014-07-25 08:22:26 +0900 | [diff] [blame] | 620 | deviceInfo.getPhysicalAddress(), deviceInfo.getPortId(), |
| 621 | deviceInfo.getDeviceType(), deviceInfo.getVendorId(), osdName)); |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 622 | return true; |
| 623 | } |
| 624 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 625 | @ServiceThreadOnly |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 626 | private void launchDeviceDiscovery() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 627 | assertRunOnServiceThread(); |
| 628 | clearDeviceInfoList(); |
| 629 | DeviceDiscoveryAction action = new DeviceDiscoveryAction(this, |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 630 | new DeviceDiscoveryCallback() { |
| 631 | @Override |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 632 | public void onDeviceDiscoveryDone(List<HdmiDeviceInfo> deviceInfos) { |
| 633 | for (HdmiDeviceInfo info : deviceInfos) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 634 | addCecDevice(info); |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | // Since we removed all devices when it's start and |
| 638 | // device discovery action does not poll local devices, |
| 639 | // we should put device info of local device manually here |
| 640 | for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 641 | addCecDevice(device.getDeviceInfo()); |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 642 | } |
| 643 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 644 | addAndStartAction(new HotplugDetectionAction(HdmiCecLocalDeviceTv.this)); |
Jungshik Jang | 410ca9c | 2014-08-07 18:04:14 +0900 | [diff] [blame] | 645 | addAndStartAction(new PowerStatusMonitorAction(HdmiCecLocalDeviceTv.this)); |
Jungshik Jang | 187d017 | 2014-06-17 17:48:42 +0900 | [diff] [blame] | 646 | |
| 647 | // If there is AVR, initiate System Audio Auto initiation action, |
| 648 | // which turns on and off system audio according to last system |
| 649 | // audio setting. |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 650 | HdmiDeviceInfo avr = getAvrDeviceInfo(); |
| 651 | if (avr != null) { |
| 652 | onNewAvrAdded(avr); |
Jungshik Jang | 187d017 | 2014-06-17 17:48:42 +0900 | [diff] [blame] | 653 | } |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 654 | } |
| 655 | }); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 656 | addAndStartAction(action); |
| 657 | } |
| 658 | |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 659 | @ServiceThreadOnly |
| 660 | void onNewAvrAdded(HdmiDeviceInfo avr) { |
| 661 | assertRunOnServiceThread(); |
| 662 | if (getSystemAudioModeSetting()) { |
| 663 | addAndStartAction(new SystemAudioAutoInitiationAction(this, avr.getLogicalAddress())); |
| 664 | } |
| 665 | if (isArcFeatureEnabled()) { |
| 666 | startArcAction(true); |
| 667 | } |
| 668 | } |
| 669 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 670 | // Clear all device info. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 671 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 672 | private void clearDeviceInfoList() { |
| 673 | assertRunOnServiceThread(); |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 674 | for (HdmiDeviceInfo info : mSafeExternalInputs) { |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 675 | invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE); |
Jinsuk Kim | 49b47bb | 2014-07-22 10:40:35 +0900 | [diff] [blame] | 676 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 677 | mDeviceInfos.clear(); |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 678 | updateSafeDeviceInfoList(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 679 | } |
| 680 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 681 | @ServiceThreadOnly |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 682 | // Seq #32 |
Jungshik Jang | ea67c18 | 2014-06-19 22:19:20 +0900 | [diff] [blame] | 683 | void changeSystemAudioMode(boolean enabled, IHdmiControlCallback callback) { |
| 684 | assertRunOnServiceThread(); |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 685 | if (!mService.isControlEnabled() || hasAction(DeviceDiscoveryAction.class)) { |
| 686 | setSystemAudioMode(false, true); |
| 687 | invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE); |
| 688 | return; |
| 689 | } |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 690 | HdmiDeviceInfo avr = getAvrDeviceInfo(); |
Jungshik Jang | ea67c18 | 2014-06-19 22:19:20 +0900 | [diff] [blame] | 691 | if (avr == null) { |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 692 | setSystemAudioMode(false, true); |
Yuncheol Heo | d05f67f | 2014-07-11 16:06:40 +0900 | [diff] [blame] | 693 | invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE); |
Jungshik Jang | ea67c18 | 2014-06-19 22:19:20 +0900 | [diff] [blame] | 694 | return; |
| 695 | } |
| 696 | |
| 697 | addAndStartAction( |
| 698 | new SystemAudioActionFromTv(this, avr.getLogicalAddress(), enabled, callback)); |
Jungshik Jang | ea67c18 | 2014-06-19 22:19:20 +0900 | [diff] [blame] | 699 | } |
| 700 | |
Jungshik Jang | ca5be9a | 2014-07-01 18:01:26 +0900 | [diff] [blame] | 701 | // # Seq 25 |
Jinsuk Kim | 7ecfbae | 2014-07-11 14:16:29 +0900 | [diff] [blame] | 702 | void setSystemAudioMode(boolean on, boolean updateSetting) { |
Jungshik Jang | 473119f | 2014-08-27 16:43:22 +0900 | [diff] [blame] | 703 | mSafeLogger.debug(String.format("System Audio Mode change[old:%b new:%b]", |
| 704 | mSystemAudioActivated, on)); |
| 705 | |
Jungshik Jang | 377dcbd | 2014-07-15 15:49:02 +0900 | [diff] [blame] | 706 | if (updateSetting) { |
| 707 | mService.writeBooleanSetting(Global.HDMI_SYSTEM_AUDIO_ENABLED, on); |
| 708 | } |
| 709 | updateAudioManagerForSystemAudio(on); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 710 | synchronized (mLock) { |
Jungshik Jang | 377dcbd | 2014-07-15 15:49:02 +0900 | [diff] [blame] | 711 | if (mSystemAudioActivated != on) { |
| 712 | mSystemAudioActivated = on; |
Jungshik Jang | ea67c18 | 2014-06-19 22:19:20 +0900 | [diff] [blame] | 713 | mService.announceSystemAudioModeChange(on); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
Jungshik Jang | 377dcbd | 2014-07-15 15:49:02 +0900 | [diff] [blame] | 718 | private void updateAudioManagerForSystemAudio(boolean on) { |
Jungshik Jang | 377dcbd | 2014-07-15 15:49:02 +0900 | [diff] [blame] | 719 | mService.getAudioManager().setHdmiSystemAudioSupported(on); |
| 720 | } |
| 721 | |
| 722 | boolean isSystemAudioActivated() { |
| 723 | if (getAvrDeviceInfo() == null) { |
| 724 | return false; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 725 | } |
Jungshik Jang | 377dcbd | 2014-07-15 15:49:02 +0900 | [diff] [blame] | 726 | synchronized (mLock) { |
| 727 | return mSystemAudioActivated; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | boolean getSystemAudioModeSetting() { |
| 732 | return mService.readBooleanSetting(Global.HDMI_SYSTEM_AUDIO_ENABLED, false); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Change ARC status into the given {@code enabled} status. |
| 737 | * |
| 738 | * @return {@code true} if ARC was in "Enabled" status |
| 739 | */ |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 740 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 741 | boolean setArcStatus(boolean enabled) { |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 742 | assertRunOnServiceThread(); |
| 743 | boolean oldStatus = mArcEstablished; |
| 744 | // 1. Enable/disable ARC circuit. |
| 745 | mService.setAudioReturnChannel(enabled); |
| 746 | // 2. Notify arc status to audio service. |
| 747 | notifyArcStatusToAudioService(enabled); |
| 748 | // 3. Update arc status; |
| 749 | mArcEstablished = enabled; |
| 750 | return oldStatus; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 751 | } |
| 752 | |
Jungshik Jang | a858d22 | 2014-06-23 17:17:47 +0900 | [diff] [blame] | 753 | private void notifyArcStatusToAudioService(boolean enabled) { |
| 754 | // Note that we don't set any name to ARC. |
| 755 | mService.getAudioManager().setWiredDeviceConnectionState( |
| 756 | AudioSystem.DEVICE_OUT_HDMI_ARC, |
| 757 | enabled ? 1 : 0, ""); |
| 758 | } |
| 759 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 760 | /** |
| 761 | * Returns whether ARC is enabled or not. |
| 762 | */ |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 763 | @ServiceThreadOnly |
| 764 | boolean isArcEstabilished() { |
| 765 | assertRunOnServiceThread(); |
| 766 | return mArcFeatureEnabled && mArcEstablished; |
| 767 | } |
| 768 | |
| 769 | @ServiceThreadOnly |
| 770 | void changeArcFeatureEnabled(boolean enabled) { |
| 771 | assertRunOnServiceThread(); |
| 772 | |
| 773 | if (mArcFeatureEnabled != enabled) { |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 774 | mArcFeatureEnabled = enabled; |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 775 | if (enabled) { |
| 776 | if (!mArcEstablished) { |
| 777 | startArcAction(true); |
| 778 | } |
| 779 | } else { |
| 780 | if (mArcEstablished) { |
| 781 | startArcAction(false); |
| 782 | } |
| 783 | } |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
| 787 | @ServiceThreadOnly |
| 788 | boolean isArcFeatureEnabled() { |
| 789 | assertRunOnServiceThread(); |
| 790 | return mArcFeatureEnabled; |
| 791 | } |
| 792 | |
| 793 | @ServiceThreadOnly |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 794 | void startArcAction(boolean enabled) { |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 795 | assertRunOnServiceThread(); |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 796 | HdmiDeviceInfo info = getAvrDeviceInfo(); |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 797 | if (info == null) { |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 798 | Slog.w(TAG, "Failed to start arc action; No AVR device."); |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 799 | return; |
| 800 | } |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 801 | if (!canStartArcUpdateAction(info.getLogicalAddress(), enabled)) { |
| 802 | Slog.w(TAG, "Failed to start arc action; ARC configuration check failed."); |
| 803 | if (enabled && !isConnectedToArcPort(info.getPhysicalAddress())) { |
| 804 | displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT); |
| 805 | } |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 806 | return; |
| 807 | } |
| 808 | |
| 809 | // Terminate opposite action and start action if not exist. |
| 810 | if (enabled) { |
| 811 | removeAction(RequestArcTerminationAction.class); |
| 812 | if (!hasAction(RequestArcInitiationAction.class)) { |
| 813 | addAndStartAction(new RequestArcInitiationAction(this, info.getLogicalAddress())); |
| 814 | } |
| 815 | } else { |
| 816 | removeAction(RequestArcInitiationAction.class); |
| 817 | if (!hasAction(RequestArcTerminationAction.class)) { |
| 818 | addAndStartAction(new RequestArcTerminationAction(this, info.getLogicalAddress())); |
| 819 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 823 | private boolean isDirectConnectAddress(int physicalAddress) { |
| 824 | return (physicalAddress & Constants.ROUTING_PATH_TOP_MASK) == physicalAddress; |
| 825 | } |
| 826 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 827 | void setAudioStatus(boolean mute, int volume) { |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 828 | synchronized (mLock) { |
| 829 | mSystemAudioMute = mute; |
| 830 | mSystemAudioVolume = volume; |
Jungshik Jang | b69aafb | 2014-07-11 16:29:06 +0900 | [diff] [blame] | 831 | int maxVolume = mService.getAudioManager().getStreamMaxVolume( |
| 832 | AudioManager.STREAM_MUSIC); |
| 833 | mService.setAudioStatus(mute, |
| 834 | VolumeControlAction.scaleToCustomVolume(volume, maxVolume)); |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
| 838 | @ServiceThreadOnly |
| 839 | void changeVolume(int curVolume, int delta, int maxVolume) { |
| 840 | assertRunOnServiceThread(); |
Jungshik Jang | 377dcbd | 2014-07-15 15:49:02 +0900 | [diff] [blame] | 841 | if (delta == 0 || !isSystemAudioActivated()) { |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 842 | return; |
| 843 | } |
| 844 | |
| 845 | int targetVolume = curVolume + delta; |
| 846 | int cecVolume = VolumeControlAction.scaleToCecVolume(targetVolume, maxVolume); |
| 847 | synchronized (mLock) { |
| 848 | // If new volume is the same as current system audio volume, just ignore it. |
| 849 | // Note that UNKNOWN_VOLUME is not in range of cec volume scale. |
| 850 | if (cecVolume == mSystemAudioVolume) { |
| 851 | // Update tv volume with system volume value. |
| 852 | mService.setAudioStatus(false, |
| 853 | VolumeControlAction.scaleToCustomVolume(mSystemAudioVolume, maxVolume)); |
| 854 | return; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | // Remove existing volume action. |
| 859 | removeAction(VolumeControlAction.class); |
| 860 | |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 861 | HdmiDeviceInfo avr = getAvrDeviceInfo(); |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 862 | addAndStartAction(VolumeControlAction.ofVolumeChange(this, avr.getLogicalAddress(), |
| 863 | cecVolume, delta > 0)); |
| 864 | } |
| 865 | |
| 866 | @ServiceThreadOnly |
| 867 | void changeMute(boolean mute) { |
| 868 | assertRunOnServiceThread(); |
Jungshik Jang | 377dcbd | 2014-07-15 15:49:02 +0900 | [diff] [blame] | 869 | if (!isSystemAudioActivated()) { |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 870 | return; |
| 871 | } |
| 872 | |
| 873 | // Remove existing volume action. |
| 874 | removeAction(VolumeControlAction.class); |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 875 | HdmiDeviceInfo avr = getAvrDeviceInfo(); |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 876 | addAndStartAction(VolumeControlAction.ofMute(this, avr.getLogicalAddress(), mute)); |
| 877 | } |
| 878 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 879 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 880 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 881 | protected boolean handleInitiateArc(HdmiCecMessage message) { |
| 882 | assertRunOnServiceThread(); |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 883 | |
| 884 | if (!canStartArcUpdateAction(message.getSource(), true)) { |
| 885 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED); |
| 886 | if (!isConnectedToArcPort(message.getSource())) { |
| 887 | displayOsd(OSD_MESSAGE_ARC_CONNECTED_INVALID_PORT); |
| 888 | } |
| 889 | return true; |
| 890 | } |
| 891 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 892 | // In case where <Initiate Arc> is started by <Request ARC Initiation> |
| 893 | // need to clean up RequestArcInitiationAction. |
| 894 | removeAction(RequestArcInitiationAction.class); |
| 895 | SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this, |
| 896 | message.getSource(), true); |
| 897 | addAndStartAction(action); |
| 898 | return true; |
| 899 | } |
| 900 | |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 901 | private boolean canStartArcUpdateAction(int avrAddress, boolean shouldCheckArcFeatureEnabled) { |
| 902 | HdmiDeviceInfo avr = getAvrDeviceInfo(); |
| 903 | if (avr != null |
| 904 | && (avrAddress == avr.getLogicalAddress()) |
| 905 | && isConnectedToArcPort(avr.getPhysicalAddress()) |
| 906 | && isDirectConnectAddress(avr.getPhysicalAddress())) { |
| 907 | if (shouldCheckArcFeatureEnabled) { |
| 908 | return isArcFeatureEnabled(); |
| 909 | } else { |
| 910 | return true; |
| 911 | } |
| 912 | } else { |
| 913 | return false; |
| 914 | } |
| 915 | } |
| 916 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 917 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 918 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 919 | protected boolean handleTerminateArc(HdmiCecMessage message) { |
| 920 | assertRunOnServiceThread(); |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 921 | // In cast of termination, do not check ARC configuration in that AVR device |
| 922 | // might be removed already. |
| 923 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 924 | // In case where <Terminate Arc> is started by <Request ARC Termination> |
| 925 | // need to clean up RequestArcInitiationAction. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 926 | removeAction(RequestArcTerminationAction.class); |
| 927 | SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this, |
| 928 | message.getSource(), false); |
| 929 | addAndStartAction(action); |
| 930 | return true; |
| 931 | } |
| 932 | |
| 933 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 934 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 935 | protected boolean handleSetSystemAudioMode(HdmiCecMessage message) { |
| 936 | assertRunOnServiceThread(); |
| 937 | if (!isMessageForSystemAudio(message)) { |
Jungshik Jang | 473119f | 2014-08-27 16:43:22 +0900 | [diff] [blame] | 938 | mSafeLogger.warning("Invalid <Set System Audio Mode> message:" + message); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 939 | return false; |
| 940 | } |
| 941 | SystemAudioActionFromAvr action = new SystemAudioActionFromAvr(this, |
Jungshik Jang | 7f0a1c5 | 2014-06-23 16:00:07 +0900 | [diff] [blame] | 942 | message.getSource(), HdmiUtils.parseCommandParamSystemAudioStatus(message), null); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 943 | addAndStartAction(action); |
| 944 | return true; |
| 945 | } |
| 946 | |
| 947 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 948 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 949 | protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) { |
| 950 | assertRunOnServiceThread(); |
| 951 | if (!isMessageForSystemAudio(message)) { |
Jungshik Jang | 473119f | 2014-08-27 16:43:22 +0900 | [diff] [blame] | 952 | mSafeLogger.warning("Invalid <System Audio Mode Status> message:" + message); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 953 | return false; |
| 954 | } |
Jinsuk Kim | 7ecfbae | 2014-07-11 14:16:29 +0900 | [diff] [blame] | 955 | setSystemAudioMode(HdmiUtils.parseCommandParamSystemAudioStatus(message), true); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 956 | return true; |
| 957 | } |
| 958 | |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 959 | // Seq #53 |
| 960 | @Override |
| 961 | @ServiceThreadOnly |
| 962 | protected boolean handleRecordTvScreen(HdmiCecMessage message) { |
| 963 | List<OneTouchRecordAction> actions = getActions(OneTouchRecordAction.class); |
| 964 | if (!actions.isEmpty()) { |
| 965 | // Assumes only one OneTouchRecordAction. |
| 966 | OneTouchRecordAction action = actions.get(0); |
| 967 | if (action.getRecorderAddress() != message.getSource()) { |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 968 | announceOneTouchRecordResult( |
| 969 | HdmiControlManager.ONE_TOUCH_RECORD_PREVIOUS_RECORDING_IN_PROGRESS); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 970 | } |
| 971 | return super.handleRecordTvScreen(message); |
| 972 | } |
| 973 | |
| 974 | int recorderAddress = message.getSource(); |
| 975 | byte[] recordSource = mService.invokeRecordRequestListener(recorderAddress); |
| 976 | startOneTouchRecord(recorderAddress, recordSource); |
| 977 | return true; |
| 978 | } |
| 979 | |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 980 | @Override |
| 981 | protected boolean handleTimerClearedStatus(HdmiCecMessage message) { |
| 982 | byte[] params = message.getParams(); |
Jungshik Jang | e9e0f07 | 2014-08-05 17:48:59 +0900 | [diff] [blame] | 983 | int timerClearedStatusData = params[0] & 0xFF; |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 984 | announceTimerRecordingResult(timerClearedStatusData); |
| 985 | return true; |
| 986 | } |
| 987 | |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 988 | void announceOneTouchRecordResult(int result) { |
| 989 | mService.invokeOneTouchRecordResult(result); |
| 990 | } |
| 991 | |
| 992 | void announceTimerRecordingResult(int result) { |
| 993 | mService.invokeTimerRecordingResult(result); |
| 994 | } |
| 995 | |
Jungshik Jang | faa49bc | 2014-08-05 16:10:21 +0900 | [diff] [blame] | 996 | void announceClearTimerRecordingResult(int result) { |
| 997 | mService.invokeClearTimerRecordingResult(result); |
| 998 | } |
| 999 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1000 | private boolean isMessageForSystemAudio(HdmiCecMessage message) { |
Jungshik Jang | 473119f | 2014-08-27 16:43:22 +0900 | [diff] [blame] | 1001 | return message.getSource() == Constants.ADDR_AUDIO_SYSTEM |
| 1002 | && (message.getDestination() == Constants.ADDR_TV |
| 1003 | || message.getDestination() == Constants.ADDR_BROADCAST) |
| 1004 | && getAvrDeviceInfo() != null; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | /** |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1008 | * Add a new {@link HdmiDeviceInfo}. It returns old device info which has the same |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1009 | * logical address as new device info's. |
| 1010 | * |
| 1011 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 1012 | * |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1013 | * @param deviceInfo a new {@link HdmiDeviceInfo} to be added. |
| 1014 | * @return {@code null} if it is new device. Otherwise, returns old {@HdmiDeviceInfo} |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1015 | * that has the same logical address as new one has. |
| 1016 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1017 | @ServiceThreadOnly |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1018 | private HdmiDeviceInfo addDeviceInfo(HdmiDeviceInfo deviceInfo) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1019 | assertRunOnServiceThread(); |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1020 | HdmiDeviceInfo oldDeviceInfo = getCecDeviceInfo(deviceInfo.getLogicalAddress()); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1021 | if (oldDeviceInfo != null) { |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1022 | removeDeviceInfo(deviceInfo.getId()); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1023 | } |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1024 | mDeviceInfos.append(deviceInfo.getId(), deviceInfo); |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1025 | updateSafeDeviceInfoList(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1026 | return oldDeviceInfo; |
| 1027 | } |
| 1028 | |
| 1029 | /** |
| 1030 | * Remove a device info corresponding to the given {@code logicalAddress}. |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1031 | * It returns removed {@link HdmiDeviceInfo} if exists. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1032 | * |
| 1033 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 1034 | * |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1035 | * @param id id of device to be removed |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1036 | * @return removed {@link HdmiDeviceInfo} it exists. Otherwise, returns {@code null} |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1037 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1038 | @ServiceThreadOnly |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1039 | private HdmiDeviceInfo removeDeviceInfo(int id) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1040 | assertRunOnServiceThread(); |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1041 | HdmiDeviceInfo deviceInfo = mDeviceInfos.get(id); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1042 | if (deviceInfo != null) { |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1043 | mDeviceInfos.remove(id); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1044 | } |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1045 | updateSafeDeviceInfoList(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1046 | return deviceInfo; |
| 1047 | } |
| 1048 | |
| 1049 | /** |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1050 | * Return a list of all {@link HdmiDeviceInfo}. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1051 | * |
| 1052 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 1053 | * This is not thread-safe. For thread safety, call {@link #getSafeExternalInputsLocked} which |
Jungshik Jang | 8e93c84 | 2014-08-06 15:48:33 +0900 | [diff] [blame] | 1054 | * does not include local device. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1055 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1056 | @ServiceThreadOnly |
Jinsuk Kim | 4b4b940 | 2014-09-02 10:30:10 +0900 | [diff] [blame] | 1057 | List<HdmiDeviceInfo> getDeviceInfoList(boolean includeLocalDevice) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1058 | assertRunOnServiceThread(); |
Jinsuk Kim | 4b4b940 | 2014-09-02 10:30:10 +0900 | [diff] [blame] | 1059 | if (includeLocalDevice) { |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1060 | return HdmiUtils.sparseArrayToList(mDeviceInfos); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1061 | } else { |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1062 | ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1063 | for (int i = 0; i < mDeviceInfos.size(); ++i) { |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1064 | HdmiDeviceInfo info = mDeviceInfos.valueAt(i); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1065 | if (!isLocalDeviceAddress(info.getLogicalAddress())) { |
| 1066 | infoList.add(info); |
| 1067 | } |
| 1068 | } |
| 1069 | return infoList; |
| 1070 | } |
| 1071 | } |
| 1072 | |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1073 | /** |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 1074 | * Return external input devices. |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1075 | */ |
Jinsuk Kim | ed08645 | 2014-08-18 15:01:53 +0900 | [diff] [blame] | 1076 | List<HdmiDeviceInfo> getSafeExternalInputsLocked() { |
| 1077 | return mSafeExternalInputs; |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1078 | } |
| 1079 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1080 | @ServiceThreadOnly |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1081 | private void updateSafeDeviceInfoList() { |
| 1082 | assertRunOnServiceThread(); |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1083 | List<HdmiDeviceInfo> copiedDevices = HdmiUtils.sparseArrayToList(mDeviceInfos); |
| 1084 | List<HdmiDeviceInfo> externalInputs = getInputDevices(); |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1085 | synchronized (mLock) { |
| 1086 | mSafeAllDeviceInfos = copiedDevices; |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 1087 | mSafeExternalInputs = externalInputs; |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 1091 | /** |
| 1092 | * Return a list of external cec input (source) devices. |
| 1093 | * |
| 1094 | * <p>Note that this effectively excludes non-source devices like system audio, |
| 1095 | * secondary TV. |
| 1096 | */ |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1097 | private List<HdmiDeviceInfo> getInputDevices() { |
| 1098 | ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>(); |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 1099 | for (int i = 0; i < mDeviceInfos.size(); ++i) { |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1100 | HdmiDeviceInfo info = mDeviceInfos.valueAt(i); |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1101 | if (isLocalDeviceAddress(info.getLogicalAddress())) { |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 1102 | continue; |
| 1103 | } |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 1104 | if (info.isSourceType() && !hideDevicesBehindLegacySwitch(info)) { |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 1105 | infoList.add(info); |
| 1106 | } |
| 1107 | } |
| 1108 | return infoList; |
| 1109 | } |
| 1110 | |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 1111 | // Check if we are hiding CEC devices connected to a legacy (non-CEC) switch. |
| 1112 | // Returns true if the policy is set to true, and the device to check does not have |
| 1113 | // a parent CEC device (which should be the CEC-enabled switch) in the list. |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1114 | private boolean hideDevicesBehindLegacySwitch(HdmiDeviceInfo info) { |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 1115 | return HdmiConfig.HIDE_DEVICES_BEHIND_LEGACY_SWITCH |
| 1116 | && !isConnectedToCecSwitch(info.getPhysicalAddress(), mCecSwitches); |
| 1117 | } |
| 1118 | |
| 1119 | private static boolean isConnectedToCecSwitch(int path, Collection<Integer> switches) { |
| 1120 | for (int switchPath : switches) { |
| 1121 | if (isParentPath(switchPath, path)) { |
| 1122 | return true; |
| 1123 | } |
| 1124 | } |
| 1125 | return false; |
| 1126 | } |
| 1127 | |
| 1128 | private static boolean isParentPath(int parentPath, int childPath) { |
| 1129 | // (A000, AB00) (AB00, ABC0), (ABC0, ABCD) |
| 1130 | // If child's last non-zero nibble is removed, the result equals to the parent. |
| 1131 | for (int i = 0; i <= 12; i += 4) { |
| 1132 | int nibble = (childPath >> i) & 0xF; |
| 1133 | if (nibble != 0) { |
| 1134 | int parentNibble = (parentPath >> i) & 0xF; |
| 1135 | return parentNibble == 0 && (childPath >> i+4) == (parentPath >> i+4); |
| 1136 | } |
| 1137 | } |
| 1138 | return false; |
| 1139 | } |
| 1140 | |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1141 | private void invokeDeviceEventListener(HdmiDeviceInfo info, int status) { |
Jinsuk Kim | bcfa067 | 2014-08-11 11:56:58 +0900 | [diff] [blame] | 1142 | if (info.isSourceType() && !hideDevicesBehindLegacySwitch(info)) { |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1143 | mService.invokeDeviceEventListeners(info, status); |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1147 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1148 | private boolean isLocalDeviceAddress(int address) { |
| 1149 | assertRunOnServiceThread(); |
| 1150 | for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) { |
| 1151 | if (device.isAddressOf(address)) { |
| 1152 | return true; |
| 1153 | } |
| 1154 | } |
| 1155 | return false; |
| 1156 | } |
| 1157 | |
Jungshik Jang | e9cf158 | 2014-06-23 17:28:58 +0900 | [diff] [blame] | 1158 | @ServiceThreadOnly |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1159 | HdmiDeviceInfo getAvrDeviceInfo() { |
Jungshik Jang | e9cf158 | 2014-06-23 17:28:58 +0900 | [diff] [blame] | 1160 | assertRunOnServiceThread(); |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1161 | return getCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM); |
Jungshik Jang | e9cf158 | 2014-06-23 17:28:58 +0900 | [diff] [blame] | 1162 | } |
| 1163 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1164 | /** |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1165 | * Return a {@link HdmiDeviceInfo} corresponding to the given {@code logicalAddress}. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1166 | * |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1167 | * This is not thread-safe. For thread safety, call {@link #getSafeCecDeviceInfo(int)}. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1168 | * |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 1169 | * @param logicalAddress logical address of the device to be retrieved |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1170 | * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1171 | * Returns null if no logical address matched |
| 1172 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1173 | @ServiceThreadOnly |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1174 | HdmiDeviceInfo getCecDeviceInfo(int logicalAddress) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1175 | assertRunOnServiceThread(); |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1176 | return mDeviceInfos.get(HdmiDeviceInfo.idForCecDevice(logicalAddress)); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1177 | } |
| 1178 | |
Jungshik Jang | e9cf158 | 2014-06-23 17:28:58 +0900 | [diff] [blame] | 1179 | boolean hasSystemAudioDevice() { |
| 1180 | return getSafeAvrDeviceInfo() != null; |
| 1181 | } |
| 1182 | |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1183 | HdmiDeviceInfo getSafeAvrDeviceInfo() { |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1184 | return getSafeCecDeviceInfo(Constants.ADDR_AUDIO_SYSTEM); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /** |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1188 | * Thread safe version of {@link #getCecDeviceInfo(int)}. |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1189 | * |
| 1190 | * @param logicalAddress logical address to be retrieved |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1191 | * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}. |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1192 | * Returns null if no logical address matched |
| 1193 | */ |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1194 | HdmiDeviceInfo getSafeCecDeviceInfo(int logicalAddress) { |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1195 | synchronized (mLock) { |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1196 | for (HdmiDeviceInfo info : mSafeAllDeviceInfos) { |
| 1197 | if (info.isCecDevice() && info.getLogicalAddress() == logicalAddress) { |
| 1198 | return info; |
| 1199 | } |
| 1200 | } |
| 1201 | return null; |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 1205 | /** |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 1206 | * Called when a device is newly added or a new device is detected or |
| 1207 | * existing device is updated. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1208 | * |
| 1209 | * @param info device info of a new device. |
| 1210 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1211 | @ServiceThreadOnly |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1212 | final void addCecDevice(HdmiDeviceInfo info) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1213 | assertRunOnServiceThread(); |
| 1214 | addDeviceInfo(info); |
Jinsuk Kim | 13c030e | 2014-06-20 13:25:17 +0900 | [diff] [blame] | 1215 | if (info.getLogicalAddress() == mAddress) { |
| 1216 | // The addition of TV device itself should not be notified. |
| 1217 | return; |
| 1218 | } |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1219 | invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | /** |
| 1223 | * Called when a device is removed or removal of device is detected. |
| 1224 | * |
| 1225 | * @param address a logical address of a device to be removed |
| 1226 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1227 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1228 | final void removeCecDevice(int address) { |
| 1229 | assertRunOnServiceThread(); |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1230 | HdmiDeviceInfo info = removeDeviceInfo(HdmiDeviceInfo.idForCecDevice(address)); |
Jungshik Jang | 26dc71e | 2014-07-04 10:53:27 +0900 | [diff] [blame] | 1231 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1232 | mCecMessageCache.flushMessagesFrom(address); |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1233 | invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1234 | } |
| 1235 | |
Jungshik Jang | 26dc71e | 2014-07-04 10:53:27 +0900 | [diff] [blame] | 1236 | @ServiceThreadOnly |
| 1237 | void handleRemoveActiveRoutingPath(int path) { |
| 1238 | assertRunOnServiceThread(); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 1239 | // Seq #23 |
| 1240 | if (isTailOfActivePath(path, getActivePath())) { |
| 1241 | removeAction(RoutingControlAction.class); |
| 1242 | int newPath = mService.portIdToPath(getActivePortId()); |
| 1243 | mService.sendCecCommand(HdmiCecMessageBuilder.buildRoutingChange( |
| 1244 | mAddress, getActivePath(), newPath)); |
Jinsuk Kim | 4b4b940 | 2014-09-02 10:30:10 +0900 | [diff] [blame] | 1245 | mActiveSource.invalidate(); |
| 1246 | addAndStartAction(new RoutingControlAction(this, getActivePath(), true, null)); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | |
Jinsuk Kim | 5344cd9 | 2014-07-03 18:02:01 +0900 | [diff] [blame] | 1250 | /** |
| 1251 | * Launch routing control process. |
| 1252 | * |
| 1253 | * @param routingForBootup true if routing control is initiated due to One Touch Play |
| 1254 | * or TV power on |
| 1255 | */ |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 1256 | @ServiceThreadOnly |
Jinsuk Kim | 5344cd9 | 2014-07-03 18:02:01 +0900 | [diff] [blame] | 1257 | void launchRoutingControl(boolean routingForBootup) { |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 1258 | assertRunOnServiceThread(); |
| 1259 | // Seq #24 |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 1260 | if (getActivePortId() != Constants.INVALID_PORT_ID) { |
Jinsuk Kim | 5344cd9 | 2014-07-03 18:02:01 +0900 | [diff] [blame] | 1261 | if (!routingForBootup && !isProhibitMode()) { |
| 1262 | removeAction(RoutingControlAction.class); |
| 1263 | int newPath = mService.portIdToPath(getActivePortId()); |
| 1264 | setActivePath(newPath); |
| 1265 | mService.sendCecCommand(HdmiCecMessageBuilder.buildRoutingChange(mAddress, |
| 1266 | getActivePath(), newPath)); |
| 1267 | addAndStartAction(new RoutingControlAction(this, getActivePortId(), |
| 1268 | routingForBootup, null)); |
| 1269 | } |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 1270 | } else { |
| 1271 | int activePath = mService.getPhysicalAddress(); |
| 1272 | setActivePath(activePath); |
Jinsuk Kim | 5344cd9 | 2014-07-03 18:02:01 +0900 | [diff] [blame] | 1273 | if (!routingForBootup) { |
| 1274 | mService.sendCecCommand(HdmiCecMessageBuilder.buildActiveSource(mAddress, |
| 1275 | activePath)); |
| 1276 | } |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1280 | /** |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1281 | * Returns the {@link HdmiDeviceInfo} instance whose physical address matches |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1282 | * the given routing path. CEC devices use routing path for its physical address to |
| 1283 | * describe the hierarchy of the devices in the network. |
| 1284 | * |
| 1285 | * @param path routing path or physical address |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1286 | * @return {@link HdmiDeviceInfo} if the matched info is found; otherwise null |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1287 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1288 | @ServiceThreadOnly |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1289 | final HdmiDeviceInfo getDeviceInfoByPath(int path) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1290 | assertRunOnServiceThread(); |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1291 | for (HdmiDeviceInfo info : getDeviceInfoList(false)) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1292 | if (info.getPhysicalAddress() == path) { |
| 1293 | return info; |
| 1294 | } |
| 1295 | } |
| 1296 | return null; |
| 1297 | } |
| 1298 | |
| 1299 | /** |
| 1300 | * Whether a device of the specified physical address and logical address exists |
| 1301 | * in a device info list. However, both are minimal condition and it could |
| 1302 | * be different device from the original one. |
| 1303 | * |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1304 | * @param logicalAddress logical address of a device to be searched |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 1305 | * @param physicalAddress physical address of a device to be searched |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1306 | * @return true if exist; otherwise false |
| 1307 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1308 | @ServiceThreadOnly |
Jinsuk Kim | 4b4b940 | 2014-09-02 10:30:10 +0900 | [diff] [blame] | 1309 | private boolean isInDeviceList(int logicalAddress, int physicalAddress) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1310 | assertRunOnServiceThread(); |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1311 | HdmiDeviceInfo device = getCecDeviceInfo(logicalAddress); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1312 | if (device == null) { |
| 1313 | return false; |
| 1314 | } |
| 1315 | return device.getPhysicalAddress() == physicalAddress; |
| 1316 | } |
| 1317 | |
| 1318 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1319 | @ServiceThreadOnly |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 1320 | void onHotplug(int portId, boolean connected) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1321 | assertRunOnServiceThread(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1322 | |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 1323 | if (!connected) { |
| 1324 | removeCecSwitches(portId); |
| 1325 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1326 | // Tv device will have permanent HotplugDetectionAction. |
| 1327 | List<HotplugDetectionAction> hotplugActions = getActions(HotplugDetectionAction.class); |
| 1328 | if (!hotplugActions.isEmpty()) { |
| 1329 | // Note that hotplug action is single action running on a machine. |
| 1330 | // "pollAllDevicesNow" cleans up timer and start poll action immediately. |
Jungshik Jang | 24c23c1 | 2014-07-07 18:04:39 +0900 | [diff] [blame] | 1331 | // It covers seq #40, #43. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1332 | hotplugActions.get(0).pollAllDevicesNow(); |
| 1333 | } |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 1334 | } |
Jinsuk Kim | 160a6e5 | 2014-07-02 06:16:36 +0900 | [diff] [blame] | 1335 | |
Jinsuk Kim | 4fdd0f4 | 2014-08-06 07:23:32 +0900 | [diff] [blame] | 1336 | private void removeCecSwitches(int portId) { |
| 1337 | Iterator<Integer> it = mCecSwitches.iterator(); |
| 1338 | while (!it.hasNext()) { |
| 1339 | int path = it.next(); |
| 1340 | if (pathToPortId(path) == portId) { |
| 1341 | it.remove(); |
| 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | |
Jinsuk Kim | 160a6e5 | 2014-07-02 06:16:36 +0900 | [diff] [blame] | 1346 | @ServiceThreadOnly |
| 1347 | void setAutoDeviceOff(boolean enabled) { |
| 1348 | assertRunOnServiceThread(); |
| 1349 | mAutoDeviceOff = enabled; |
Jinsuk Kim | 544b62b | 2014-07-14 14:01:23 +0900 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | @ServiceThreadOnly |
| 1353 | void setAutoWakeup(boolean enabled) { |
| 1354 | assertRunOnServiceThread(); |
| 1355 | mAutoWakeup = enabled; |
Jinsuk Kim | 160a6e5 | 2014-07-02 06:16:36 +0900 | [diff] [blame] | 1356 | } |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1357 | |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 1358 | @ServiceThreadOnly |
| 1359 | boolean getAutoWakeup() { |
| 1360 | assertRunOnServiceThread(); |
| 1361 | return mAutoWakeup; |
| 1362 | } |
| 1363 | |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1364 | @Override |
| 1365 | @ServiceThreadOnly |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 1366 | protected void disableDevice(boolean initiatedByCec, PendingActionClearedCallback callback) { |
| 1367 | super.disableDevice(initiatedByCec, callback); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1368 | assertRunOnServiceThread(); |
| 1369 | // Remove any repeated working actions. |
| 1370 | // HotplugDetectionAction will be reinstated during the wake up process. |
| 1371 | // HdmiControlService.onWakeUp() -> initializeLocalDevices() -> |
| 1372 | // LocalDeviceTv.onAddressAllocated() -> launchDeviceDiscovery(). |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 1373 | removeAction(DeviceDiscoveryAction.class); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1374 | removeAction(HotplugDetectionAction.class); |
Jungshik Jang | 410ca9c | 2014-08-07 18:04:14 +0900 | [diff] [blame] | 1375 | removeAction(PowerStatusMonitorAction.class); |
Jungshik Jang | faa49bc | 2014-08-05 16:10:21 +0900 | [diff] [blame] | 1376 | // Remove recording actions. |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 1377 | removeAction(OneTouchRecordAction.class); |
Jungshik Jang | faa49bc | 2014-08-05 16:10:21 +0900 | [diff] [blame] | 1378 | removeAction(TimerRecordingAction.class); |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 1379 | |
| 1380 | disableSystemAudioIfExist(); |
| 1381 | disableArcIfExist(); |
Jinsuk Kim | 49b47bb | 2014-07-22 10:40:35 +0900 | [diff] [blame] | 1382 | clearDeviceInfoList(); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1383 | checkIfPendingActionsCleared(); |
| 1384 | } |
| 1385 | |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 1386 | @ServiceThreadOnly |
| 1387 | private void disableSystemAudioIfExist() { |
| 1388 | assertRunOnServiceThread(); |
| 1389 | if (getAvrDeviceInfo() == null) { |
| 1390 | return; |
| 1391 | } |
| 1392 | |
| 1393 | // Seq #31. |
| 1394 | removeAction(SystemAudioActionFromAvr.class); |
| 1395 | removeAction(SystemAudioActionFromTv.class); |
| 1396 | removeAction(SystemAudioAutoInitiationAction.class); |
| 1397 | removeAction(SystemAudioStatusAction.class); |
| 1398 | removeAction(VolumeControlAction.class); |
| 1399 | |
Jinsuk Kim | 7ecfbae | 2014-07-11 14:16:29 +0900 | [diff] [blame] | 1400 | // Turn off the mode but do not write it the settings, so that the next time TV powers on |
| 1401 | // the system audio mode setting can be restored automatically. |
| 1402 | setSystemAudioMode(false, false); |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | @ServiceThreadOnly |
| 1406 | private void disableArcIfExist() { |
| 1407 | assertRunOnServiceThread(); |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1408 | HdmiDeviceInfo avr = getAvrDeviceInfo(); |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 1409 | if (avr == null) { |
| 1410 | return; |
| 1411 | } |
| 1412 | |
| 1413 | // Seq #44. |
| 1414 | removeAction(RequestArcInitiationAction.class); |
| 1415 | if (!hasAction(RequestArcTerminationAction.class) && isArcEstabilished()) { |
| 1416 | addAndStartAction(new RequestArcTerminationAction(this, avr.getLogicalAddress())); |
| 1417 | } |
| 1418 | } |
| 1419 | |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1420 | @Override |
| 1421 | @ServiceThreadOnly |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 1422 | protected void onStandby(boolean initiatedByCec) { |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1423 | assertRunOnServiceThread(); |
| 1424 | // Seq #11 |
| 1425 | if (!mService.isControlEnabled()) { |
| 1426 | return; |
| 1427 | } |
Jinsuk Kim | 544b62b | 2014-07-14 14:01:23 +0900 | [diff] [blame] | 1428 | if (!initiatedByCec && mAutoDeviceOff) { |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1429 | mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby( |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 1430 | mAddress, Constants.ADDR_BROADCAST)); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | |
Jinsuk Kim | 4d43d93 | 2014-07-03 16:43:58 +0900 | [diff] [blame] | 1434 | boolean isProhibitMode() { |
| 1435 | return mService.isProhibitMode(); |
| 1436 | } |
Jinsuk Kim | b38cd68 | 2014-07-07 08:05:03 +0900 | [diff] [blame] | 1437 | |
| 1438 | boolean isPowerStandbyOrTransient() { |
| 1439 | return mService.isPowerStandbyOrTransient(); |
Jungshik Jang | 8866c81 | 2014-07-08 14:42:28 +0900 | [diff] [blame] | 1440 | } |
Jinsuk Kim | c7eba0f | 2014-07-07 14:18:02 +0900 | [diff] [blame] | 1441 | |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 1442 | @ServiceThreadOnly |
Jinsuk Kim | c7eba0f | 2014-07-07 14:18:02 +0900 | [diff] [blame] | 1443 | void displayOsd(int messageId) { |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 1444 | assertRunOnServiceThread(); |
| 1445 | mService.displayOsd(messageId); |
Jinsuk Kim | b38cd68 | 2014-07-07 08:05:03 +0900 | [diff] [blame] | 1446 | } |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 1447 | |
| 1448 | // Seq #54 and #55 |
| 1449 | @ServiceThreadOnly |
| 1450 | void startOneTouchRecord(int recorderAddress, byte[] recordSource) { |
| 1451 | assertRunOnServiceThread(); |
| 1452 | if (!mService.isControlEnabled()) { |
| 1453 | Slog.w(TAG, "Can not start one touch record. CEC control is disabled."); |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 1454 | announceOneTouchRecordResult(ONE_TOUCH_RECORD_CEC_DISABLED); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 1455 | return; |
| 1456 | } |
| 1457 | |
| 1458 | if (!checkRecorder(recorderAddress)) { |
| 1459 | Slog.w(TAG, "Invalid recorder address:" + recorderAddress); |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 1460 | announceOneTouchRecordResult(ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 1461 | return; |
| 1462 | } |
| 1463 | |
| 1464 | if (!checkRecordSource(recordSource)) { |
| 1465 | Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource)); |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 1466 | announceOneTouchRecordResult(ONE_TOUCH_RECORD_FAIL_TO_RECORD_DISPLAYED_SCREEN); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 1467 | return; |
| 1468 | } |
| 1469 | |
| 1470 | addAndStartAction(new OneTouchRecordAction(this, recorderAddress, recordSource)); |
| 1471 | Slog.i(TAG, "Start new [One Touch Record]-Target:" + recorderAddress + ", recordSource:" |
| 1472 | + Arrays.toString(recordSource)); |
| 1473 | } |
| 1474 | |
| 1475 | @ServiceThreadOnly |
| 1476 | void stopOneTouchRecord(int recorderAddress) { |
| 1477 | assertRunOnServiceThread(); |
| 1478 | if (!mService.isControlEnabled()) { |
| 1479 | Slog.w(TAG, "Can not stop one touch record. CEC control is disabled."); |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 1480 | announceOneTouchRecordResult(ONE_TOUCH_RECORD_CEC_DISABLED); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 1481 | return; |
| 1482 | } |
| 1483 | |
| 1484 | if (!checkRecorder(recorderAddress)) { |
| 1485 | Slog.w(TAG, "Invalid recorder address:" + recorderAddress); |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 1486 | announceOneTouchRecordResult(ONE_TOUCH_RECORD_CHECK_RECORDER_CONNECTION); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 1487 | return; |
| 1488 | } |
| 1489 | |
| 1490 | // Remove one touch record action so that other one touch record can be started. |
| 1491 | removeAction(OneTouchRecordAction.class); |
| 1492 | mService.sendCecCommand(HdmiCecMessageBuilder.buildRecordOff(mAddress, recorderAddress)); |
| 1493 | Slog.i(TAG, "Stop [One Touch Record]-Target:" + recorderAddress); |
| 1494 | } |
| 1495 | |
| 1496 | private boolean checkRecorder(int recorderAddress) { |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1497 | HdmiDeviceInfo device = getCecDeviceInfo(recorderAddress); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 1498 | return (device != null) |
| 1499 | && (HdmiUtils.getTypeFromAddress(recorderAddress) |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1500 | == HdmiDeviceInfo.DEVICE_RECORDER); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | private boolean checkRecordSource(byte[] recordSource) { |
| 1504 | return (recordSource != null) && HdmiRecordSources.checkRecordSource(recordSource); |
| 1505 | } |
| 1506 | |
| 1507 | @ServiceThreadOnly |
| 1508 | void startTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) { |
| 1509 | assertRunOnServiceThread(); |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 1510 | if (!mService.isControlEnabled()) { |
| 1511 | Slog.w(TAG, "Can not start one touch record. CEC control is disabled."); |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 1512 | announceTimerRecordingResult(TIMER_RECORDING_RESULT_EXTRA_CEC_DISABLED); |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 1513 | return; |
| 1514 | } |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 1515 | |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 1516 | if (!checkRecorder(recorderAddress)) { |
| 1517 | Slog.w(TAG, "Invalid recorder address:" + recorderAddress); |
| 1518 | announceTimerRecordingResult( |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 1519 | TIMER_RECORDING_RESULT_EXTRA_CHECK_RECORDER_CONNECTION); |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 1520 | return; |
| 1521 | } |
| 1522 | |
| 1523 | if (!checkTimerRecordingSource(sourceType, recordSource)) { |
| 1524 | Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource)); |
| 1525 | announceTimerRecordingResult( |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 1526 | TIMER_RECORDING_RESULT_EXTRA_FAIL_TO_RECORD_SELECTED_SOURCE); |
Jungshik Jang | 12e5dce | 2014-07-24 15:27:44 +0900 | [diff] [blame] | 1527 | return; |
| 1528 | } |
| 1529 | |
| 1530 | addAndStartAction( |
| 1531 | new TimerRecordingAction(this, recorderAddress, sourceType, recordSource)); |
| 1532 | Slog.i(TAG, "Start [Timer Recording]-Target:" + recorderAddress + ", SourceType:" |
| 1533 | + sourceType + ", RecordSource:" + Arrays.toString(recordSource)); |
| 1534 | } |
| 1535 | |
| 1536 | private boolean checkTimerRecordingSource(int sourceType, byte[] recordSource) { |
| 1537 | return (recordSource != null) |
| 1538 | && HdmiTimerRecordSources.checkTimerRecordSource(sourceType, recordSource); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | @ServiceThreadOnly |
| 1542 | void clearTimerRecording(int recorderAddress, int sourceType, byte[] recordSource) { |
| 1543 | assertRunOnServiceThread(); |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 1544 | if (!mService.isControlEnabled()) { |
| 1545 | Slog.w(TAG, "Can not start one touch record. CEC control is disabled."); |
Jungshik Jang | faa49bc | 2014-08-05 16:10:21 +0900 | [diff] [blame] | 1546 | announceClearTimerRecordingResult(CLEAR_TIMER_STATUS_CEC_DISABLE); |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 1547 | return; |
| 1548 | } |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 1549 | |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 1550 | if (!checkRecorder(recorderAddress)) { |
| 1551 | Slog.w(TAG, "Invalid recorder address:" + recorderAddress); |
Jungshik Jang | faa49bc | 2014-08-05 16:10:21 +0900 | [diff] [blame] | 1552 | announceClearTimerRecordingResult(CLEAR_TIMER_STATUS_CHECK_RECORDER_CONNECTION); |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 1553 | return; |
| 1554 | } |
| 1555 | |
| 1556 | if (!checkTimerRecordingSource(sourceType, recordSource)) { |
| 1557 | Slog.w(TAG, "Invalid record source." + Arrays.toString(recordSource)); |
Jungshik Jang | faa49bc | 2014-08-05 16:10:21 +0900 | [diff] [blame] | 1558 | announceClearTimerRecordingResult(CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE); |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 1559 | return; |
| 1560 | } |
| 1561 | |
| 1562 | sendClearTimerMessage(recorderAddress, sourceType, recordSource); |
| 1563 | } |
| 1564 | |
| 1565 | private void sendClearTimerMessage(int recorderAddress, int sourceType, byte[] recordSource) { |
| 1566 | HdmiCecMessage message = null; |
| 1567 | switch (sourceType) { |
| 1568 | case TIMER_RECORDING_TYPE_DIGITAL: |
| 1569 | message = HdmiCecMessageBuilder.buildClearDigitalTimer(mAddress, recorderAddress, |
| 1570 | recordSource); |
| 1571 | break; |
| 1572 | case TIMER_RECORDING_TYPE_ANALOGUE: |
| 1573 | message = HdmiCecMessageBuilder.buildClearAnalogueTimer(mAddress, recorderAddress, |
| 1574 | recordSource); |
| 1575 | break; |
| 1576 | case TIMER_RECORDING_TYPE_EXTERNAL: |
| 1577 | message = HdmiCecMessageBuilder.buildClearExternalTimer(mAddress, recorderAddress, |
| 1578 | recordSource); |
| 1579 | break; |
| 1580 | default: |
| 1581 | Slog.w(TAG, "Invalid source type:" + recorderAddress); |
Jungshik Jang | faa49bc | 2014-08-05 16:10:21 +0900 | [diff] [blame] | 1582 | announceClearTimerRecordingResult(CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE); |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 1583 | return; |
| 1584 | |
| 1585 | } |
| 1586 | mService.sendCecCommand(message, new SendMessageCallback() { |
| 1587 | @Override |
| 1588 | public void onSendCompleted(int error) { |
| 1589 | if (error != Constants.SEND_RESULT_SUCCESS) { |
Jungshik Jang | faa49bc | 2014-08-05 16:10:21 +0900 | [diff] [blame] | 1590 | announceClearTimerRecordingResult( |
| 1591 | CLEAR_TIMER_STATUS_FAIL_TO_CLEAR_SELECTED_SOURCE); |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | }); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 1595 | } |
Jungshik Jang | 410ca9c | 2014-08-07 18:04:14 +0900 | [diff] [blame] | 1596 | |
| 1597 | void updateDevicePowerStatus(int logicalAddress, int newPowerStatus) { |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 1598 | HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress); |
Jungshik Jang | 410ca9c | 2014-08-07 18:04:14 +0900 | [diff] [blame] | 1599 | if (info == null) { |
| 1600 | Slog.w(TAG, "Can not update power status of non-existing device:" + logicalAddress); |
| 1601 | return; |
| 1602 | } |
| 1603 | |
| 1604 | if (info.getDevicePowerStatus() == newPowerStatus) { |
| 1605 | return; |
| 1606 | } |
| 1607 | |
| 1608 | HdmiDeviceInfo newInfo = HdmiUtils.cloneHdmiDeviceInfo(info, newPowerStatus); |
| 1609 | // addDeviceInfo replaces old device info with new one if exists. |
| 1610 | addDeviceInfo(newInfo); |
| 1611 | |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1612 | invokeDeviceEventListener(newInfo, HdmiControlManager.DEVICE_EVENT_UPDATE_DEVICE); |
Jungshik Jang | 410ca9c | 2014-08-07 18:04:14 +0900 | [diff] [blame] | 1613 | } |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame] | 1614 | |
| 1615 | @Override |
| 1616 | protected void dump(final IndentingPrintWriter pw) { |
| 1617 | super.dump(pw); |
| 1618 | pw.println("mArcEstablished: " + mArcEstablished); |
| 1619 | pw.println("mArcFeatureEnabled: " + mArcFeatureEnabled); |
| 1620 | pw.println("mSystemAudioActivated: " + mSystemAudioActivated); |
| 1621 | pw.println("mSystemAudioMute: " + mSystemAudioMute); |
| 1622 | pw.println("mAutoDeviceOff: " + mAutoDeviceOff); |
| 1623 | pw.println("mAutoWakeup: " + mAutoWakeup); |
| 1624 | pw.println("mSkipRoutingControl: " + mSkipRoutingControl); |
Jinsuk Kim | 4b4b940 | 2014-09-02 10:30:10 +0900 | [diff] [blame] | 1625 | pw.println("CEC devices:"); |
| 1626 | pw.increaseIndent(); |
| 1627 | for (HdmiDeviceInfo info : mSafeAllDeviceInfos) { |
| 1628 | pw.println(info); |
| 1629 | } |
| 1630 | pw.decreaseIndent(); |
Terry Heo | 959d2db | 2014-08-28 16:45:41 +0900 | [diff] [blame] | 1631 | } |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 1632 | } |