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 | |
Jinsuk Kim | c7eba0f | 2014-07-07 14:18:02 +0900 | [diff] [blame] | 19 | import android.content.Intent; |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 20 | import android.hardware.hdmi.HdmiCecDeviceInfo; |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 21 | import android.hardware.hdmi.HdmiControlManager; |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 22 | import android.hardware.hdmi.IHdmiControlCallback; |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 23 | import android.media.AudioManager.OnAudioPortUpdateListener; |
| 24 | import android.media.AudioPatch; |
| 25 | import android.media.AudioPort; |
Jungshik Jang | a858d22 | 2014-06-23 17:17:47 +0900 | [diff] [blame] | 26 | import android.media.AudioSystem; |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 27 | import android.os.RemoteException; |
Jinsuk Kim | c7eba0f | 2014-07-07 14:18:02 +0900 | [diff] [blame] | 28 | import android.os.UserHandle; |
Jinsuk Kim | 7ecfbae | 2014-07-11 14:16:29 +0900 | [diff] [blame] | 29 | import android.provider.Settings.Global; |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 30 | import android.util.Slog; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 31 | import android.util.SparseArray; |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 32 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 33 | import com.android.internal.annotations.GuardedBy; |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 34 | import com.android.server.hdmi.DeviceDiscoveryAction.DeviceDiscoveryCallback; |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 35 | import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly; |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 36 | |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 37 | import java.io.UnsupportedEncodingException; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 38 | import java.util.ArrayList; |
Jinsuk Kim | 0a3316b | 2014-06-14 09:33:55 +0900 | [diff] [blame] | 39 | import java.util.Collections; |
| 40 | import java.util.List; |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 41 | import java.util.Locale; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Represent a logical device of type TV residing in Android system. |
| 45 | */ |
| 46 | final class HdmiCecLocalDeviceTv extends HdmiCecLocalDevice { |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 47 | private static final String TAG = "HdmiCecLocalDeviceTv"; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 48 | |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 49 | // Whether ARC is available or not. "true" means that ARC is estabilished between TV and |
| 50 | // AVR as audio receiver. |
| 51 | @ServiceThreadOnly |
| 52 | private boolean mArcEstablished = false; |
| 53 | |
| 54 | // Whether ARC feature is enabled or not. |
| 55 | private boolean mArcFeatureEnabled = false; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 56 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 57 | // Whether SystemAudioMode is "On" or not. |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 58 | @GuardedBy("mLock") |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 59 | private boolean mSystemAudioMode; |
| 60 | |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 61 | // The previous port id (input) before switching to the new one. This is remembered in order to |
| 62 | // be able to switch to it upon receiving <Inactive Source> from currently active source. |
| 63 | // This remains valid only when the active source was switched via one touch play operation |
| 64 | // (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] | 65 | // Constants.PORT_INVALID, for which case <Inactive Source> does not do anything. |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 66 | @GuardedBy("mLock") |
| 67 | private int mPrevPortId; |
| 68 | |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 69 | @GuardedBy("mLock") |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 70 | private int mSystemAudioVolume = Constants.UNKNOWN_VOLUME; |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 71 | |
| 72 | @GuardedBy("mLock") |
| 73 | private boolean mSystemAudioMute = false; |
| 74 | |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 75 | // Copy of mDeviceInfos to guarantee thread-safety. |
| 76 | @GuardedBy("mLock") |
| 77 | private List<HdmiCecDeviceInfo> mSafeAllDeviceInfos = Collections.emptyList(); |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 78 | // All external cec input(source) devices. Does not include system audio device. |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 79 | @GuardedBy("mLock") |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 80 | private List<HdmiCecDeviceInfo> mSafeExternalInputs = Collections.emptyList(); |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 81 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 82 | // Map-like container of all cec devices including local ones. |
| 83 | // A logical address of device is used as key of container. |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 84 | // This is not thread-safe. For external purpose use mSafeDeviceInfos. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 85 | private final SparseArray<HdmiCecDeviceInfo> mDeviceInfos = new SparseArray<>(); |
| 86 | |
Jinsuk Kim | 160a6e5 | 2014-07-02 06:16:36 +0900 | [diff] [blame] | 87 | // If true, TV going to standby mode puts other devices also to standby. |
| 88 | private boolean mAutoDeviceOff; |
| 89 | |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 90 | HdmiCecLocalDeviceTv(HdmiControlService service) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 91 | super(service, HdmiCecDeviceInfo.DEVICE_TV); |
| 92 | mPrevPortId = Constants.INVALID_PORT_ID; |
Jungshik Jang | 8b308d9 | 2014-05-29 21:52:28 +0900 | [diff] [blame] | 93 | } |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 94 | |
Jungshik Jang | 8b308d9 | 2014-05-29 21:52:28 +0900 | [diff] [blame] | 95 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 96 | @ServiceThreadOnly |
Jungshik Jang | a9f1062 | 2014-07-11 15:36:39 +0900 | [diff] [blame] | 97 | protected void onAddressAllocated(int logicalAddress, boolean fromBootup) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 98 | assertRunOnServiceThread(); |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 99 | mService.sendCecCommand(HdmiCecMessageBuilder.buildReportPhysicalAddressCommand( |
| 100 | mAddress, mService.getPhysicalAddress(), mDeviceType)); |
| 101 | mService.sendCecCommand(HdmiCecMessageBuilder.buildDeviceVendorIdCommand( |
| 102 | mAddress, mService.getVendorId())); |
Jinsuk Kim | 7ecfbae | 2014-07-11 14:16:29 +0900 | [diff] [blame] | 103 | mSystemAudioMode = mService.readBooleanSetting(Global.HDMI_SYSTEM_AUDIO_ENABLED, false); |
Jungshik Jang | a9f1062 | 2014-07-11 15:36:39 +0900 | [diff] [blame] | 104 | launchRoutingControl(fromBootup); |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 105 | launchDeviceDiscovery(); |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 106 | registerAudioPortUpdateListener(); |
| 107 | // TODO: unregister audio port update listener if local device is released. |
| 108 | } |
| 109 | |
| 110 | private void registerAudioPortUpdateListener() { |
| 111 | mService.getAudioManager().registerAudioPortUpdateListener( |
| 112 | new OnAudioPortUpdateListener() { |
| 113 | @Override |
| 114 | public void OnAudioPatchListUpdate(AudioPatch[] patchList) {} |
| 115 | |
| 116 | @Override |
| 117 | public void OnAudioPortListUpdate(AudioPort[] portList) { |
| 118 | if (!mSystemAudioMode) { |
| 119 | return; |
| 120 | } |
| 121 | int devices = mService.getAudioManager().getDevicesForStream( |
| 122 | AudioSystem.STREAM_MUSIC); |
| 123 | if ((devices & ~AudioSystem.DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER) |
| 124 | != 0) { |
| 125 | // TODO: release system audio here. |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void OnServiceDied() {} |
| 131 | }); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 132 | } |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 133 | |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 134 | /** |
| 135 | * Performs the action 'device select', or 'one touch play' initiated by TV. |
| 136 | * |
| 137 | * @param targetAddress logical address of the device to select |
| 138 | * @param callback callback object to report the result with |
| 139 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 140 | @ServiceThreadOnly |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 141 | void deviceSelect(int targetAddress, IHdmiControlCallback callback) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 142 | assertRunOnServiceThread(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 143 | if (targetAddress == Constants.ADDR_INTERNAL) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 144 | handleSelectInternalSource(callback); |
| 145 | return; |
| 146 | } |
Jinsuk Kim | 13c030e | 2014-06-20 13:25:17 +0900 | [diff] [blame] | 147 | HdmiCecDeviceInfo targetDevice = getDeviceInfo(targetAddress); |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 148 | if (targetDevice == null) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 149 | invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE); |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 150 | return; |
| 151 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 152 | removeAction(DeviceSelectAction.class); |
| 153 | addAndStartAction(new DeviceSelectAction(this, targetDevice, callback)); |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 154 | } |
| 155 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 156 | @ServiceThreadOnly |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 157 | private void handleSelectInternalSource(IHdmiControlCallback callback) { |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 158 | assertRunOnServiceThread(); |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 159 | // Seq #18 |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 160 | if (mService.isControlEnabled() && getActiveSource() != mAddress) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 161 | updateActiveSource(mAddress, mService.getPhysicalAddress()); |
| 162 | // TODO: Check if this comes from <Text/Image View On> - if true, do nothing. |
| 163 | HdmiCecMessage activeSource = HdmiCecMessageBuilder.buildActiveSource( |
| 164 | mAddress, mService.getPhysicalAddress()); |
| 165 | mService.sendCecCommand(activeSource); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | @ServiceThreadOnly |
| 170 | void updateActiveSource(int activeSource, int activePath) { |
| 171 | assertRunOnServiceThread(); |
| 172 | // Seq #14 |
| 173 | if (activeSource == getActiveSource() && activePath == getActivePath()) { |
| 174 | return; |
| 175 | } |
| 176 | setActiveSource(activeSource); |
| 177 | setActivePath(activePath); |
| 178 | if (getDeviceInfo(activeSource) != null && activeSource != mAddress) { |
| 179 | if (mService.pathToPortId(activePath) == getActivePortId()) { |
| 180 | setPrevPortId(getActivePortId()); |
| 181 | } |
| 182 | // TODO: Show the OSD banner related to the new active source device. |
| 183 | } else { |
| 184 | // TODO: If displayed, remove the OSD banner related to the previous |
| 185 | // active source device. |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Returns the previous port id kept to handle input switching on <Inactive Source>. |
| 191 | */ |
| 192 | int getPrevPortId() { |
| 193 | synchronized (mLock) { |
| 194 | return mPrevPortId; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Sets the previous port id. INVALID_PORT_ID invalidates it, hence no actions will be |
| 200 | * taken for <Inactive Source>. |
| 201 | */ |
| 202 | void setPrevPortId(int portId) { |
| 203 | synchronized (mLock) { |
| 204 | mPrevPortId = portId; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | @ServiceThreadOnly |
| 209 | void updateActivePortId(int portId) { |
| 210 | assertRunOnServiceThread(); |
| 211 | // Seq #15 |
| 212 | if (portId == getActivePortId()) { |
| 213 | return; |
| 214 | } |
| 215 | setPrevPortId(portId); |
| 216 | // TODO: Actually switch the physical port here. Handle PAP/PIP as well. |
| 217 | // Show OSD port change banner |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 218 | mService.invokeInputChangeListener(getActiveSource()); |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @ServiceThreadOnly |
| 222 | void doManualPortSwitching(int portId, IHdmiControlCallback callback) { |
| 223 | assertRunOnServiceThread(); |
| 224 | // Seq #20 |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 225 | if (!mService.isControlEnabled() || portId == getActivePortId()) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 226 | invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE); |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 227 | return; |
| 228 | } |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 229 | // TODO: Make sure this call does not stem from <Active Source> message reception. |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 230 | |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 231 | setActivePortId(portId); |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 232 | // TODO: Return immediately if the operation is triggered by <Text/Image View On> |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 233 | // and this is the first notification about the active input after power-on. |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 234 | // TODO: Handle invalid port id / active input which should be treated as an |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 235 | // internal tuner. |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 236 | |
| 237 | removeAction(RoutingControlAction.class); |
| 238 | |
| 239 | int oldPath = mService.portIdToPath(mService.portIdToPath(getActivePortId())); |
| 240 | int newPath = mService.portIdToPath(portId); |
| 241 | HdmiCecMessage routingChange = |
| 242 | HdmiCecMessageBuilder.buildRoutingChange(mAddress, oldPath, newPath); |
| 243 | mService.sendCecCommand(routingChange); |
Jinsuk Kim | 04fd280 | 2014-07-03 14:04:02 +0900 | [diff] [blame] | 244 | addAndStartAction(new RoutingControlAction(this, newPath, false, callback)); |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 245 | } |
| 246 | |
Jinsuk Kim | cae6627 | 2014-07-04 13:26:44 +0900 | [diff] [blame] | 247 | int getPowerStatus() { |
| 248 | return mService.getPowerStatus(); |
| 249 | } |
| 250 | |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 251 | /** |
| 252 | * Sends key to a target CEC device. |
| 253 | * |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 254 | * @param keyCode key code to send. Defined in {@link android.view.KeyEvent}. |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 255 | * @param isPressed true if this is key press event |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 256 | */ |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 257 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 258 | @ServiceThreadOnly |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 259 | protected void sendKeyEvent(int keyCode, boolean isPressed) { |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 260 | assertRunOnServiceThread(); |
| 261 | List<SendKeyAction> action = getActions(SendKeyAction.class); |
| 262 | if (!action.isEmpty()) { |
| 263 | action.get(0).processKeyEvent(keyCode, isPressed); |
| 264 | } else { |
| 265 | if (isPressed) { |
| 266 | addAndStartAction(new SendKeyAction(this, getActiveSource(), keyCode)); |
| 267 | } else { |
| 268 | Slog.w(TAG, "Discard key release event"); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 273 | private static void invokeCallback(IHdmiControlCallback callback, int result) { |
Jinsuk Kim | 4893c7e | 2014-06-19 14:13:22 +0900 | [diff] [blame] | 274 | if (callback == null) { |
| 275 | return; |
| 276 | } |
Jinsuk Kim | a6ce770 | 2014-05-11 06:54:49 +0900 | [diff] [blame] | 277 | try { |
| 278 | callback.onComplete(result); |
| 279 | } catch (RemoteException e) { |
| 280 | Slog.e(TAG, "Invoking callback failed:" + e); |
| 281 | } |
| 282 | } |
| 283 | |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 284 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 285 | @ServiceThreadOnly |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 286 | protected boolean handleActiveSource(HdmiCecMessage message) { |
| 287 | assertRunOnServiceThread(); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 288 | int address = message.getSource(); |
| 289 | int path = HdmiUtils.twoBytesToInt(message.getParams()); |
| 290 | if (getDeviceInfo(address) == null) { |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 291 | handleNewDeviceAtTheTailOfActivePath(path); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 292 | } else { |
| 293 | ActiveSourceHandler.create(this, null).process(address, path); |
| 294 | } |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 295 | return true; |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | @ServiceThreadOnly |
| 300 | protected boolean handleInactiveSource(HdmiCecMessage message) { |
| 301 | assertRunOnServiceThread(); |
| 302 | // Seq #10 |
| 303 | |
| 304 | // Ignore <Inactive Source> from non-active source device. |
| 305 | if (getActiveSource() != message.getSource()) { |
| 306 | return true; |
| 307 | } |
Jinsuk Kim | 4d43d93 | 2014-07-03 16:43:58 +0900 | [diff] [blame] | 308 | if (isProhibitMode()) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 309 | return true; |
| 310 | } |
| 311 | int portId = getPrevPortId(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 312 | if (portId != Constants.INVALID_PORT_ID) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 313 | // TODO: Do this only if TV is not showing multiview like PIP/PAP. |
| 314 | |
| 315 | HdmiCecDeviceInfo inactiveSource = getDeviceInfo(message.getSource()); |
| 316 | if (inactiveSource == null) { |
| 317 | return true; |
| 318 | } |
| 319 | if (mService.pathToPortId(inactiveSource.getPhysicalAddress()) == portId) { |
| 320 | return true; |
| 321 | } |
| 322 | // TODO: Switch the TV freeze mode off |
| 323 | |
| 324 | setActivePortId(portId); |
| 325 | doManualPortSwitching(portId, null); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 326 | setPrevPortId(Constants.INVALID_PORT_ID); |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 327 | } |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | @ServiceThreadOnly |
| 333 | protected boolean handleRequestActiveSource(HdmiCecMessage message) { |
| 334 | assertRunOnServiceThread(); |
| 335 | // Seq #19 |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 336 | if (mAddress == getActiveSource()) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 337 | mService.sendCecCommand( |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 338 | HdmiCecMessageBuilder.buildActiveSource(mAddress, getActivePath())); |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 339 | } |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 345 | protected boolean handleGetMenuLanguage(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 346 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 347 | HdmiCecMessage command = HdmiCecMessageBuilder.buildSetMenuLanguageCommand( |
| 348 | mAddress, Locale.getDefault().getISO3Language()); |
| 349 | // TODO: figure out how to handle failed to get language code. |
| 350 | if (command != null) { |
| 351 | mService.sendCecCommand(command); |
| 352 | } else { |
| 353 | Slog.w(TAG, "Failed to respond to <Get Menu Language>: " + message.toString()); |
| 354 | } |
| 355 | return true; |
| 356 | } |
| 357 | |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 358 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 359 | @ServiceThreadOnly |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 360 | protected boolean handleReportPhysicalAddress(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 361 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 362 | // Ignore if [Device Discovery Action] is going on. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 363 | if (hasAction(DeviceDiscoveryAction.class)) { |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 364 | Slog.i(TAG, "Ignore unrecognizable <Report Physical Address> " |
| 365 | + "because Device Discovery Action is on-going:" + message); |
| 366 | return true; |
| 367 | } |
| 368 | |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 369 | int path = HdmiUtils.twoBytesToInt(message.getParams()); |
| 370 | int address = message.getSource(); |
| 371 | if (!isInDeviceList(path, address)) { |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 372 | handleNewDeviceAtTheTailOfActivePath(path); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 373 | } |
| 374 | addAndStartAction(new NewDeviceAction(this, address, path)); |
| 375 | return true; |
| 376 | } |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 377 | |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 378 | private void handleNewDeviceAtTheTailOfActivePath(int path) { |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 379 | // Seq #22 |
| 380 | if (isTailOfActivePath(path, getActivePath())) { |
| 381 | removeAction(RoutingControlAction.class); |
| 382 | int newPath = mService.portIdToPath(getActivePortId()); |
| 383 | mService.sendCecCommand(HdmiCecMessageBuilder.buildRoutingChange( |
| 384 | mAddress, getActivePath(), newPath)); |
Jinsuk Kim | 04fd280 | 2014-07-03 14:04:02 +0900 | [diff] [blame] | 385 | addAndStartAction(new RoutingControlAction(this, getActivePortId(), false, null)); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Whether the given path is located in the tail of current active path. |
| 391 | * |
| 392 | * @param path to be tested |
| 393 | * @param activePath current active path |
| 394 | * @return true if the given path is located in the tail of current active path; otherwise, |
| 395 | * false |
| 396 | */ |
| 397 | static boolean isTailOfActivePath(int path, int activePath) { |
| 398 | // If active routing path is internal source, return false. |
| 399 | if (activePath == 0) { |
| 400 | return false; |
| 401 | } |
| 402 | for (int i = 12; i >= 0; i -= 4) { |
| 403 | int curActivePath = (activePath >> i) & 0xF; |
| 404 | if (curActivePath == 0) { |
| 405 | return true; |
| 406 | } else { |
| 407 | int curPath = (path >> i) & 0xF; |
| 408 | if (curPath != curActivePath) { |
| 409 | return false; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | @ServiceThreadOnly |
| 418 | protected boolean handleRoutingChange(HdmiCecMessage message) { |
| 419 | assertRunOnServiceThread(); |
| 420 | // Seq #21 |
| 421 | byte[] params = message.getParams(); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 422 | int currentPath = HdmiUtils.twoBytesToInt(params); |
| 423 | if (HdmiUtils.isAffectingActiveRoutingPath(getActivePath(), currentPath)) { |
| 424 | int newPath = HdmiUtils.twoBytesToInt(params, 2); |
| 425 | setActivePath(newPath); |
| 426 | removeAction(RoutingControlAction.class); |
Jinsuk Kim | 04fd280 | 2014-07-03 14:04:02 +0900 | [diff] [blame] | 427 | addAndStartAction(new RoutingControlAction(this, newPath, true, null)); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 428 | } |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 429 | return true; |
| 430 | } |
Jinsuk Kim | 0a3316b | 2014-06-14 09:33:55 +0900 | [diff] [blame] | 431 | |
| 432 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 433 | @ServiceThreadOnly |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 434 | protected boolean handleReportAudioStatus(HdmiCecMessage message) { |
| 435 | assertRunOnServiceThread(); |
| 436 | |
| 437 | byte params[] = message.getParams(); |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 438 | int mute = params[0] & 0x80; |
| 439 | int volume = params[0] & 0x7F; |
| 440 | setAudioStatus(mute == 0x80, volume); |
| 441 | return true; |
| 442 | } |
| 443 | |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 444 | @Override |
| 445 | @ServiceThreadOnly |
| 446 | protected boolean handleTextViewOn(HdmiCecMessage message) { |
| 447 | assertRunOnServiceThread(); |
| 448 | if (mService.isPowerStandbyOrTransient()) { |
| 449 | mService.wakeUp(); |
| 450 | } |
| 451 | // TODO: Connect to Hardware input manager to invoke TV App with the appropriate channel |
| 452 | // that represents the source device. |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | @Override |
| 457 | @ServiceThreadOnly |
| 458 | protected boolean handleImageViewOn(HdmiCecMessage message) { |
| 459 | assertRunOnServiceThread(); |
| 460 | // Currently, it's the same as <Text View On>. |
| 461 | return handleTextViewOn(message); |
| 462 | } |
| 463 | |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 464 | @Override |
| 465 | @ServiceThreadOnly |
| 466 | protected boolean handleSetOsdName(HdmiCecMessage message) { |
| 467 | int source = message.getSource(); |
| 468 | HdmiCecDeviceInfo deviceInfo = getDeviceInfo(source); |
| 469 | // If the device is not in device list, ignore it. |
| 470 | if (deviceInfo == null) { |
| 471 | Slog.e(TAG, "No source device info for <Set Osd Name>." + message); |
| 472 | return true; |
| 473 | } |
| 474 | String osdName = null; |
| 475 | try { |
| 476 | osdName = new String(message.getParams(), "US-ASCII"); |
| 477 | } catch (UnsupportedEncodingException e) { |
| 478 | Slog.e(TAG, "Invalid <Set Osd Name> request:" + message, e); |
| 479 | return true; |
| 480 | } |
| 481 | |
| 482 | if (deviceInfo.getDisplayName().equals(osdName)) { |
| 483 | Slog.i(TAG, "Ignore incoming <Set Osd Name> having same osd name:" + message); |
| 484 | return true; |
| 485 | } |
| 486 | |
| 487 | addCecDevice(new HdmiCecDeviceInfo(deviceInfo.getLogicalAddress(), |
| 488 | deviceInfo.getPhysicalAddress(), deviceInfo.getDeviceType(), |
| 489 | deviceInfo.getVendorId(), osdName)); |
| 490 | return true; |
| 491 | } |
| 492 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 493 | @ServiceThreadOnly |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 494 | private void launchDeviceDiscovery() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 495 | assertRunOnServiceThread(); |
| 496 | clearDeviceInfoList(); |
| 497 | DeviceDiscoveryAction action = new DeviceDiscoveryAction(this, |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 498 | new DeviceDiscoveryCallback() { |
| 499 | @Override |
| 500 | public void onDeviceDiscoveryDone(List<HdmiCecDeviceInfo> deviceInfos) { |
| 501 | for (HdmiCecDeviceInfo info : deviceInfos) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 502 | addCecDevice(info); |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | // Since we removed all devices when it's start and |
| 506 | // device discovery action does not poll local devices, |
| 507 | // we should put device info of local device manually here |
| 508 | for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 509 | addCecDevice(device.getDeviceInfo()); |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 510 | } |
| 511 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 512 | addAndStartAction(new HotplugDetectionAction(HdmiCecLocalDeviceTv.this)); |
Jungshik Jang | 187d017 | 2014-06-17 17:48:42 +0900 | [diff] [blame] | 513 | |
| 514 | // If there is AVR, initiate System Audio Auto initiation action, |
| 515 | // which turns on and off system audio according to last system |
| 516 | // audio setting. |
Jinsuk Kim | 7ecfbae | 2014-07-11 14:16:29 +0900 | [diff] [blame] | 517 | if (mSystemAudioMode && getAvrDeviceInfo() != null) { |
Jungshik Jang | 187d017 | 2014-06-17 17:48:42 +0900 | [diff] [blame] | 518 | addAndStartAction(new SystemAudioAutoInitiationAction( |
Jinsuk Kim | 7ecfbae | 2014-07-11 14:16:29 +0900 | [diff] [blame] | 519 | HdmiCecLocalDeviceTv.this, |
| 520 | getAvrDeviceInfo().getLogicalAddress())); |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 521 | if (mArcEstablished) { |
| 522 | startArcAction(true); |
Jungshik Jang | 5e3916a | 2014-06-30 14:59:21 +0900 | [diff] [blame] | 523 | } |
Jungshik Jang | 187d017 | 2014-06-17 17:48:42 +0900 | [diff] [blame] | 524 | } |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 525 | } |
| 526 | }); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 527 | addAndStartAction(action); |
| 528 | } |
| 529 | |
| 530 | // Clear all device info. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 531 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 532 | private void clearDeviceInfoList() { |
| 533 | assertRunOnServiceThread(); |
| 534 | mDeviceInfos.clear(); |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 535 | updateSafeDeviceInfoList(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 536 | } |
| 537 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 538 | @ServiceThreadOnly |
Jungshik Jang | ea67c18 | 2014-06-19 22:19:20 +0900 | [diff] [blame] | 539 | void changeSystemAudioMode(boolean enabled, IHdmiControlCallback callback) { |
| 540 | assertRunOnServiceThread(); |
| 541 | HdmiCecDeviceInfo avr = getAvrDeviceInfo(); |
| 542 | if (avr == null) { |
Yuncheol Heo | d05f67f | 2014-07-11 16:06:40 +0900 | [diff] [blame^] | 543 | invokeCallback(callback, HdmiControlManager.RESULT_TARGET_NOT_AVAILABLE); |
Jungshik Jang | ea67c18 | 2014-06-19 22:19:20 +0900 | [diff] [blame] | 544 | return; |
| 545 | } |
| 546 | |
| 547 | addAndStartAction( |
| 548 | new SystemAudioActionFromTv(this, avr.getLogicalAddress(), enabled, callback)); |
Jungshik Jang | ea67c18 | 2014-06-19 22:19:20 +0900 | [diff] [blame] | 549 | } |
| 550 | |
Jungshik Jang | ca5be9a | 2014-07-01 18:01:26 +0900 | [diff] [blame] | 551 | // # Seq 25 |
Jinsuk Kim | 7ecfbae | 2014-07-11 14:16:29 +0900 | [diff] [blame] | 552 | void setSystemAudioMode(boolean on, boolean updateSetting) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 553 | synchronized (mLock) { |
| 554 | if (on != mSystemAudioMode) { |
| 555 | mSystemAudioMode = on; |
Jinsuk Kim | 7ecfbae | 2014-07-11 14:16:29 +0900 | [diff] [blame] | 556 | if (updateSetting) { |
| 557 | mService.writeBooleanSetting(Global.HDMI_SYSTEM_AUDIO_ENABLED, on); |
| 558 | } |
Jungshik Jang | ea67c18 | 2014-06-19 22:19:20 +0900 | [diff] [blame] | 559 | mService.announceSystemAudioModeChange(on); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | boolean getSystemAudioMode() { |
| 565 | synchronized (mLock) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 566 | return mSystemAudioMode; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Change ARC status into the given {@code enabled} status. |
| 572 | * |
| 573 | * @return {@code true} if ARC was in "Enabled" status |
| 574 | */ |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 575 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 576 | boolean setArcStatus(boolean enabled) { |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 577 | assertRunOnServiceThread(); |
| 578 | boolean oldStatus = mArcEstablished; |
| 579 | // 1. Enable/disable ARC circuit. |
| 580 | mService.setAudioReturnChannel(enabled); |
| 581 | // 2. Notify arc status to audio service. |
| 582 | notifyArcStatusToAudioService(enabled); |
| 583 | // 3. Update arc status; |
| 584 | mArcEstablished = enabled; |
| 585 | return oldStatus; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 586 | } |
| 587 | |
Jungshik Jang | a858d22 | 2014-06-23 17:17:47 +0900 | [diff] [blame] | 588 | private void notifyArcStatusToAudioService(boolean enabled) { |
| 589 | // Note that we don't set any name to ARC. |
| 590 | mService.getAudioManager().setWiredDeviceConnectionState( |
| 591 | AudioSystem.DEVICE_OUT_HDMI_ARC, |
| 592 | enabled ? 1 : 0, ""); |
| 593 | } |
| 594 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 595 | /** |
| 596 | * Returns whether ARC is enabled or not. |
| 597 | */ |
Jungshik Jang | a13da0d | 2014-06-30 16:26:06 +0900 | [diff] [blame] | 598 | @ServiceThreadOnly |
| 599 | boolean isArcEstabilished() { |
| 600 | assertRunOnServiceThread(); |
| 601 | return mArcFeatureEnabled && mArcEstablished; |
| 602 | } |
| 603 | |
| 604 | @ServiceThreadOnly |
| 605 | void changeArcFeatureEnabled(boolean enabled) { |
| 606 | assertRunOnServiceThread(); |
| 607 | |
| 608 | if (mArcFeatureEnabled != enabled) { |
| 609 | if (enabled) { |
| 610 | if (!mArcEstablished) { |
| 611 | startArcAction(true); |
| 612 | } |
| 613 | } else { |
| 614 | if (mArcEstablished) { |
| 615 | startArcAction(false); |
| 616 | } |
| 617 | } |
| 618 | mArcFeatureEnabled = enabled; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | @ServiceThreadOnly |
| 623 | boolean isArcFeatureEnabled() { |
| 624 | assertRunOnServiceThread(); |
| 625 | return mArcFeatureEnabled; |
| 626 | } |
| 627 | |
| 628 | @ServiceThreadOnly |
| 629 | private void startArcAction(boolean enabled) { |
| 630 | assertRunOnServiceThread(); |
| 631 | HdmiCecDeviceInfo info = getAvrDeviceInfo(); |
| 632 | if (info == null) { |
| 633 | return; |
| 634 | } |
| 635 | if (!isConnectedToArcPort(info.getPhysicalAddress())) { |
| 636 | return; |
| 637 | } |
| 638 | |
| 639 | // Terminate opposite action and start action if not exist. |
| 640 | if (enabled) { |
| 641 | removeAction(RequestArcTerminationAction.class); |
| 642 | if (!hasAction(RequestArcInitiationAction.class)) { |
| 643 | addAndStartAction(new RequestArcInitiationAction(this, info.getLogicalAddress())); |
| 644 | } |
| 645 | } else { |
| 646 | removeAction(RequestArcInitiationAction.class); |
| 647 | if (!hasAction(RequestArcTerminationAction.class)) { |
| 648 | addAndStartAction(new RequestArcTerminationAction(this, info.getLogicalAddress())); |
| 649 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
| 653 | void setAudioStatus(boolean mute, int volume) { |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 654 | synchronized (mLock) { |
| 655 | mSystemAudioMute = mute; |
| 656 | mSystemAudioVolume = volume; |
| 657 | // TODO: pass volume to service (audio service) after scale it to local volume level. |
| 658 | mService.setAudioStatus(mute, volume); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | @ServiceThreadOnly |
| 663 | void changeVolume(int curVolume, int delta, int maxVolume) { |
| 664 | assertRunOnServiceThread(); |
| 665 | if (delta == 0 || !isSystemAudioOn()) { |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | int targetVolume = curVolume + delta; |
| 670 | int cecVolume = VolumeControlAction.scaleToCecVolume(targetVolume, maxVolume); |
| 671 | synchronized (mLock) { |
| 672 | // If new volume is the same as current system audio volume, just ignore it. |
| 673 | // Note that UNKNOWN_VOLUME is not in range of cec volume scale. |
| 674 | if (cecVolume == mSystemAudioVolume) { |
| 675 | // Update tv volume with system volume value. |
| 676 | mService.setAudioStatus(false, |
| 677 | VolumeControlAction.scaleToCustomVolume(mSystemAudioVolume, maxVolume)); |
| 678 | return; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | // Remove existing volume action. |
| 683 | removeAction(VolumeControlAction.class); |
| 684 | |
| 685 | HdmiCecDeviceInfo avr = getAvrDeviceInfo(); |
| 686 | addAndStartAction(VolumeControlAction.ofVolumeChange(this, avr.getLogicalAddress(), |
| 687 | cecVolume, delta > 0)); |
| 688 | } |
| 689 | |
| 690 | @ServiceThreadOnly |
| 691 | void changeMute(boolean mute) { |
| 692 | assertRunOnServiceThread(); |
| 693 | if (!isSystemAudioOn()) { |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | // Remove existing volume action. |
| 698 | removeAction(VolumeControlAction.class); |
| 699 | HdmiCecDeviceInfo avr = getAvrDeviceInfo(); |
| 700 | addAndStartAction(VolumeControlAction.ofMute(this, avr.getLogicalAddress(), mute)); |
| 701 | } |
| 702 | |
| 703 | private boolean isSystemAudioOn() { |
| 704 | if (getAvrDeviceInfo() == null) { |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | synchronized (mLock) { |
| 709 | return mSystemAudioMode; |
| 710 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 714 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 715 | protected boolean handleInitiateArc(HdmiCecMessage message) { |
| 716 | assertRunOnServiceThread(); |
| 717 | // In case where <Initiate Arc> is started by <Request ARC Initiation> |
| 718 | // need to clean up RequestArcInitiationAction. |
| 719 | removeAction(RequestArcInitiationAction.class); |
| 720 | SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this, |
| 721 | message.getSource(), true); |
| 722 | addAndStartAction(action); |
| 723 | return true; |
| 724 | } |
| 725 | |
| 726 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 727 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 728 | protected boolean handleTerminateArc(HdmiCecMessage message) { |
| 729 | assertRunOnServiceThread(); |
| 730 | // In case where <Terminate Arc> is started by <Request ARC Termination> |
| 731 | // need to clean up RequestArcInitiationAction. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 732 | removeAction(RequestArcTerminationAction.class); |
| 733 | SetArcTransmissionStateAction action = new SetArcTransmissionStateAction(this, |
| 734 | message.getSource(), false); |
| 735 | addAndStartAction(action); |
| 736 | return true; |
| 737 | } |
| 738 | |
| 739 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 740 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 741 | protected boolean handleSetSystemAudioMode(HdmiCecMessage message) { |
| 742 | assertRunOnServiceThread(); |
| 743 | if (!isMessageForSystemAudio(message)) { |
| 744 | return false; |
| 745 | } |
| 746 | SystemAudioActionFromAvr action = new SystemAudioActionFromAvr(this, |
Jungshik Jang | 7f0a1c5 | 2014-06-23 16:00:07 +0900 | [diff] [blame] | 747 | message.getSource(), HdmiUtils.parseCommandParamSystemAudioStatus(message), null); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 748 | addAndStartAction(action); |
| 749 | return true; |
| 750 | } |
| 751 | |
| 752 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 753 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 754 | protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) { |
| 755 | assertRunOnServiceThread(); |
| 756 | if (!isMessageForSystemAudio(message)) { |
| 757 | return false; |
| 758 | } |
Jinsuk Kim | 7ecfbae | 2014-07-11 14:16:29 +0900 | [diff] [blame] | 759 | setSystemAudioMode(HdmiUtils.parseCommandParamSystemAudioStatus(message), true); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 760 | return true; |
| 761 | } |
| 762 | |
| 763 | private boolean isMessageForSystemAudio(HdmiCecMessage message) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 764 | if (message.getSource() != Constants.ADDR_AUDIO_SYSTEM |
| 765 | || message.getDestination() != Constants.ADDR_TV |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 766 | || getAvrDeviceInfo() == null) { |
| 767 | Slog.w(TAG, "Skip abnormal CecMessage: " + message); |
| 768 | return false; |
| 769 | } |
| 770 | return true; |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * Add a new {@link HdmiCecDeviceInfo}. It returns old device info which has the same |
| 775 | * logical address as new device info's. |
| 776 | * |
| 777 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 778 | * |
| 779 | * @param deviceInfo a new {@link HdmiCecDeviceInfo} to be added. |
| 780 | * @return {@code null} if it is new device. Otherwise, returns old {@HdmiCecDeviceInfo} |
| 781 | * that has the same logical address as new one has. |
| 782 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 783 | @ServiceThreadOnly |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 784 | private HdmiCecDeviceInfo addDeviceInfo(HdmiCecDeviceInfo deviceInfo) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 785 | assertRunOnServiceThread(); |
| 786 | HdmiCecDeviceInfo oldDeviceInfo = getDeviceInfo(deviceInfo.getLogicalAddress()); |
| 787 | if (oldDeviceInfo != null) { |
| 788 | removeDeviceInfo(deviceInfo.getLogicalAddress()); |
| 789 | } |
| 790 | mDeviceInfos.append(deviceInfo.getLogicalAddress(), deviceInfo); |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 791 | updateSafeDeviceInfoList(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 792 | return oldDeviceInfo; |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * Remove a device info corresponding to the given {@code logicalAddress}. |
| 797 | * It returns removed {@link HdmiCecDeviceInfo} if exists. |
| 798 | * |
| 799 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
| 800 | * |
| 801 | * @param logicalAddress logical address of device to be removed |
| 802 | * @return removed {@link HdmiCecDeviceInfo} it exists. Otherwise, returns {@code null} |
| 803 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 804 | @ServiceThreadOnly |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 805 | private HdmiCecDeviceInfo removeDeviceInfo(int logicalAddress) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 806 | assertRunOnServiceThread(); |
| 807 | HdmiCecDeviceInfo deviceInfo = mDeviceInfos.get(logicalAddress); |
| 808 | if (deviceInfo != null) { |
| 809 | mDeviceInfos.remove(logicalAddress); |
| 810 | } |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 811 | updateSafeDeviceInfoList(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 812 | return deviceInfo; |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * Return a list of all {@link HdmiCecDeviceInfo}. |
| 817 | * |
| 818 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 819 | * This is not thread-safe. For thread safety, call {@link #getSafeDeviceInfoList(boolean)}. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 820 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 821 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 822 | List<HdmiCecDeviceInfo> getDeviceInfoList(boolean includelLocalDevice) { |
| 823 | assertRunOnServiceThread(); |
| 824 | if (includelLocalDevice) { |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 825 | return HdmiUtils.sparseArrayToList(mDeviceInfos); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 826 | } else { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 827 | ArrayList<HdmiCecDeviceInfo> infoList = new ArrayList<>(); |
| 828 | for (int i = 0; i < mDeviceInfos.size(); ++i) { |
| 829 | HdmiCecDeviceInfo info = mDeviceInfos.valueAt(i); |
| 830 | if (!isLocalDeviceAddress(info.getLogicalAddress())) { |
| 831 | infoList.add(info); |
| 832 | } |
| 833 | } |
| 834 | return infoList; |
| 835 | } |
| 836 | } |
| 837 | |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 838 | /** |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 839 | * Return external input devices. |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 840 | */ |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 841 | List<HdmiCecDeviceInfo> getSafeExternalInputs() { |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 842 | synchronized (mLock) { |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 843 | return mSafeExternalInputs; |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 847 | @ServiceThreadOnly |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 848 | private void updateSafeDeviceInfoList() { |
| 849 | assertRunOnServiceThread(); |
| 850 | List<HdmiCecDeviceInfo> copiedDevices = HdmiUtils.sparseArrayToList(mDeviceInfos); |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 851 | List<HdmiCecDeviceInfo> externalInputs = getInputDevices(); |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 852 | synchronized (mLock) { |
| 853 | mSafeAllDeviceInfos = copiedDevices; |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 854 | mSafeExternalInputs = externalInputs; |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | |
Jinsuk Kim | 9c37e1f | 2014-07-02 08:29:26 +0900 | [diff] [blame] | 858 | /** |
| 859 | * Return a list of external cec input (source) devices. |
| 860 | * |
| 861 | * <p>Note that this effectively excludes non-source devices like system audio, |
| 862 | * secondary TV. |
| 863 | */ |
| 864 | private List<HdmiCecDeviceInfo> getInputDevices() { |
| 865 | ArrayList<HdmiCecDeviceInfo> infoList = new ArrayList<>(); |
| 866 | for (int i = 0; i < mDeviceInfos.size(); ++i) { |
| 867 | HdmiCecDeviceInfo info = mDeviceInfos.valueAt(i); |
| 868 | if (isLocalDeviceAddress(i)) { |
| 869 | continue; |
| 870 | } |
| 871 | if (info.isSourceType()) { |
| 872 | infoList.add(info); |
| 873 | } |
| 874 | } |
| 875 | return infoList; |
| 876 | } |
| 877 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 878 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 879 | private boolean isLocalDeviceAddress(int address) { |
| 880 | assertRunOnServiceThread(); |
| 881 | for (HdmiCecLocalDevice device : mService.getAllLocalDevices()) { |
| 882 | if (device.isAddressOf(address)) { |
| 883 | return true; |
| 884 | } |
| 885 | } |
| 886 | return false; |
| 887 | } |
| 888 | |
Jungshik Jang | e9cf158 | 2014-06-23 17:28:58 +0900 | [diff] [blame] | 889 | @ServiceThreadOnly |
| 890 | HdmiCecDeviceInfo getAvrDeviceInfo() { |
| 891 | assertRunOnServiceThread(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 892 | return getDeviceInfo(Constants.ADDR_AUDIO_SYSTEM); |
Jungshik Jang | e9cf158 | 2014-06-23 17:28:58 +0900 | [diff] [blame] | 893 | } |
| 894 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 895 | /** |
| 896 | * Return a {@link HdmiCecDeviceInfo} corresponding to the given {@code logicalAddress}. |
| 897 | * |
| 898 | * <p>Declared as package-private. accessed by {@link HdmiControlService} only. |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 899 | * This is not thread-safe. For thread safety, call {@link #getSafeDeviceInfo(int)}. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 900 | * |
| 901 | * @param logicalAddress logical address to be retrieved |
| 902 | * @return {@link HdmiCecDeviceInfo} matched with the given {@code logicalAddress}. |
| 903 | * Returns null if no logical address matched |
| 904 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 905 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 906 | HdmiCecDeviceInfo getDeviceInfo(int logicalAddress) { |
| 907 | assertRunOnServiceThread(); |
| 908 | return mDeviceInfos.get(logicalAddress); |
| 909 | } |
| 910 | |
Jungshik Jang | e9cf158 | 2014-06-23 17:28:58 +0900 | [diff] [blame] | 911 | boolean hasSystemAudioDevice() { |
| 912 | return getSafeAvrDeviceInfo() != null; |
| 913 | } |
| 914 | |
| 915 | HdmiCecDeviceInfo getSafeAvrDeviceInfo() { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 916 | return getSafeDeviceInfo(Constants.ADDR_AUDIO_SYSTEM); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /** |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 920 | * Thread safe version of {@link #getDeviceInfo(int)}. |
| 921 | * |
| 922 | * @param logicalAddress logical address to be retrieved |
| 923 | * @return {@link HdmiCecDeviceInfo} matched with the given {@code logicalAddress}. |
| 924 | * Returns null if no logical address matched |
| 925 | */ |
| 926 | HdmiCecDeviceInfo getSafeDeviceInfo(int logicalAddress) { |
| 927 | synchronized (mLock) { |
| 928 | return mSafeAllDeviceInfos.get(logicalAddress); |
| 929 | } |
| 930 | } |
| 931 | |
Jungshik Jang | fa8e90d | 2014-06-23 14:40:32 +0900 | [diff] [blame] | 932 | /** |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 933 | * Called when a device is newly added or a new device is detected or |
| 934 | * existing device is updated. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 935 | * |
| 936 | * @param info device info of a new device. |
| 937 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 938 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 939 | final void addCecDevice(HdmiCecDeviceInfo info) { |
| 940 | assertRunOnServiceThread(); |
| 941 | addDeviceInfo(info); |
Jinsuk Kim | 13c030e | 2014-06-20 13:25:17 +0900 | [diff] [blame] | 942 | if (info.getLogicalAddress() == mAddress) { |
| 943 | // The addition of TV device itself should not be notified. |
| 944 | return; |
| 945 | } |
Jinsuk Kim | 4893c7e | 2014-06-19 14:13:22 +0900 | [diff] [blame] | 946 | mService.invokeDeviceEventListeners(info, true); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | /** |
| 950 | * Called when a device is removed or removal of device is detected. |
| 951 | * |
| 952 | * @param address a logical address of a device to be removed |
| 953 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 954 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 955 | final void removeCecDevice(int address) { |
| 956 | assertRunOnServiceThread(); |
Jinsuk Kim | 4893c7e | 2014-06-19 14:13:22 +0900 | [diff] [blame] | 957 | HdmiCecDeviceInfo info = removeDeviceInfo(address); |
Jungshik Jang | 26dc71e | 2014-07-04 10:53:27 +0900 | [diff] [blame] | 958 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 959 | mCecMessageCache.flushMessagesFrom(address); |
Jinsuk Kim | 4893c7e | 2014-06-19 14:13:22 +0900 | [diff] [blame] | 960 | mService.invokeDeviceEventListeners(info, false); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 961 | } |
| 962 | |
Jungshik Jang | 26dc71e | 2014-07-04 10:53:27 +0900 | [diff] [blame] | 963 | @ServiceThreadOnly |
| 964 | void handleRemoveActiveRoutingPath(int path) { |
| 965 | assertRunOnServiceThread(); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 966 | // Seq #23 |
| 967 | if (isTailOfActivePath(path, getActivePath())) { |
| 968 | removeAction(RoutingControlAction.class); |
| 969 | int newPath = mService.portIdToPath(getActivePortId()); |
| 970 | mService.sendCecCommand(HdmiCecMessageBuilder.buildRoutingChange( |
| 971 | mAddress, getActivePath(), newPath)); |
Jinsuk Kim | 04fd280 | 2014-07-03 14:04:02 +0900 | [diff] [blame] | 972 | addAndStartAction(new RoutingControlAction(this, getActivePortId(), true, null)); |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
Jinsuk Kim | 5344cd9 | 2014-07-03 18:02:01 +0900 | [diff] [blame] | 976 | /** |
| 977 | * Launch routing control process. |
| 978 | * |
| 979 | * @param routingForBootup true if routing control is initiated due to One Touch Play |
| 980 | * or TV power on |
| 981 | */ |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 982 | @ServiceThreadOnly |
Jinsuk Kim | 5344cd9 | 2014-07-03 18:02:01 +0900 | [diff] [blame] | 983 | void launchRoutingControl(boolean routingForBootup) { |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 984 | assertRunOnServiceThread(); |
| 985 | // Seq #24 |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 986 | if (getActivePortId() != Constants.INVALID_PORT_ID) { |
Jinsuk Kim | 5344cd9 | 2014-07-03 18:02:01 +0900 | [diff] [blame] | 987 | if (!routingForBootup && !isProhibitMode()) { |
| 988 | removeAction(RoutingControlAction.class); |
| 989 | int newPath = mService.portIdToPath(getActivePortId()); |
| 990 | setActivePath(newPath); |
| 991 | mService.sendCecCommand(HdmiCecMessageBuilder.buildRoutingChange(mAddress, |
| 992 | getActivePath(), newPath)); |
| 993 | addAndStartAction(new RoutingControlAction(this, getActivePortId(), |
| 994 | routingForBootup, null)); |
| 995 | } |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 996 | } else { |
| 997 | int activePath = mService.getPhysicalAddress(); |
| 998 | setActivePath(activePath); |
Jinsuk Kim | 5344cd9 | 2014-07-03 18:02:01 +0900 | [diff] [blame] | 999 | if (!routingForBootup) { |
| 1000 | mService.sendCecCommand(HdmiCecMessageBuilder.buildActiveSource(mAddress, |
| 1001 | activePath)); |
| 1002 | } |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1006 | /** |
| 1007 | * Returns the {@link HdmiCecDeviceInfo} instance whose physical address matches |
| 1008 | * the given routing path. CEC devices use routing path for its physical address to |
| 1009 | * describe the hierarchy of the devices in the network. |
| 1010 | * |
| 1011 | * @param path routing path or physical address |
| 1012 | * @return {@link HdmiCecDeviceInfo} if the matched info is found; otherwise null |
| 1013 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1014 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1015 | final HdmiCecDeviceInfo getDeviceInfoByPath(int path) { |
| 1016 | assertRunOnServiceThread(); |
| 1017 | for (HdmiCecDeviceInfo info : getDeviceInfoList(false)) { |
| 1018 | if (info.getPhysicalAddress() == path) { |
| 1019 | return info; |
| 1020 | } |
| 1021 | } |
| 1022 | return null; |
| 1023 | } |
| 1024 | |
| 1025 | /** |
| 1026 | * Whether a device of the specified physical address and logical address exists |
| 1027 | * in a device info list. However, both are minimal condition and it could |
| 1028 | * be different device from the original one. |
| 1029 | * |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1030 | * @param logicalAddress logical address of a device to be searched |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 1031 | * @param physicalAddress physical address of a device to be searched |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1032 | * @return true if exist; otherwise false |
| 1033 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1034 | @ServiceThreadOnly |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 1035 | boolean isInDeviceList(int logicalAddress, int physicalAddress) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1036 | assertRunOnServiceThread(); |
| 1037 | HdmiCecDeviceInfo device = getDeviceInfo(logicalAddress); |
| 1038 | if (device == null) { |
| 1039 | return false; |
| 1040 | } |
| 1041 | return device.getPhysicalAddress() == physicalAddress; |
| 1042 | } |
| 1043 | |
| 1044 | @Override |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 1045 | @ServiceThreadOnly |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 1046 | void onHotplug(int portId, boolean connected) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1047 | assertRunOnServiceThread(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1048 | |
| 1049 | // Tv device will have permanent HotplugDetectionAction. |
| 1050 | List<HotplugDetectionAction> hotplugActions = getActions(HotplugDetectionAction.class); |
| 1051 | if (!hotplugActions.isEmpty()) { |
| 1052 | // Note that hotplug action is single action running on a machine. |
| 1053 | // "pollAllDevicesNow" cleans up timer and start poll action immediately. |
Jungshik Jang | 24c23c1 | 2014-07-07 18:04:39 +0900 | [diff] [blame] | 1054 | // It covers seq #40, #43. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 1055 | hotplugActions.get(0).pollAllDevicesNow(); |
| 1056 | } |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 1057 | } |
Jinsuk Kim | 160a6e5 | 2014-07-02 06:16:36 +0900 | [diff] [blame] | 1058 | |
| 1059 | @ServiceThreadOnly |
| 1060 | void setAutoDeviceOff(boolean enabled) { |
| 1061 | assertRunOnServiceThread(); |
| 1062 | mAutoDeviceOff = enabled; |
| 1063 | } |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1064 | |
| 1065 | @Override |
| 1066 | @ServiceThreadOnly |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 1067 | protected void disableDevice(boolean initiatedByCec, PendingActionClearedCallback callback) { |
| 1068 | super.disableDevice(initiatedByCec, callback); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1069 | assertRunOnServiceThread(); |
| 1070 | // Remove any repeated working actions. |
| 1071 | // HotplugDetectionAction will be reinstated during the wake up process. |
| 1072 | // HdmiControlService.onWakeUp() -> initializeLocalDevices() -> |
| 1073 | // LocalDeviceTv.onAddressAllocated() -> launchDeviceDiscovery(). |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 1074 | removeAction(DeviceDiscoveryAction.class); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1075 | removeAction(HotplugDetectionAction.class); |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 1076 | |
| 1077 | disableSystemAudioIfExist(); |
| 1078 | disableArcIfExist(); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1079 | checkIfPendingActionsCleared(); |
| 1080 | } |
| 1081 | |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 1082 | @ServiceThreadOnly |
| 1083 | private void disableSystemAudioIfExist() { |
| 1084 | assertRunOnServiceThread(); |
| 1085 | if (getAvrDeviceInfo() == null) { |
| 1086 | return; |
| 1087 | } |
| 1088 | |
| 1089 | // Seq #31. |
| 1090 | removeAction(SystemAudioActionFromAvr.class); |
| 1091 | removeAction(SystemAudioActionFromTv.class); |
| 1092 | removeAction(SystemAudioAutoInitiationAction.class); |
| 1093 | removeAction(SystemAudioStatusAction.class); |
| 1094 | removeAction(VolumeControlAction.class); |
| 1095 | |
Jinsuk Kim | 7ecfbae | 2014-07-11 14:16:29 +0900 | [diff] [blame] | 1096 | // Turn off the mode but do not write it the settings, so that the next time TV powers on |
| 1097 | // the system audio mode setting can be restored automatically. |
| 1098 | setSystemAudioMode(false, false); |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | @ServiceThreadOnly |
| 1102 | private void disableArcIfExist() { |
| 1103 | assertRunOnServiceThread(); |
| 1104 | HdmiCecDeviceInfo avr = getAvrDeviceInfo(); |
| 1105 | if (avr == null) { |
| 1106 | return; |
| 1107 | } |
| 1108 | |
| 1109 | // Seq #44. |
| 1110 | removeAction(RequestArcInitiationAction.class); |
| 1111 | if (!hasAction(RequestArcTerminationAction.class) && isArcEstabilished()) { |
| 1112 | addAndStartAction(new RequestArcTerminationAction(this, avr.getLogicalAddress())); |
| 1113 | } |
| 1114 | } |
| 1115 | |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1116 | @Override |
| 1117 | @ServiceThreadOnly |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 1118 | protected void onStandby(boolean initiatedByCec) { |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1119 | assertRunOnServiceThread(); |
| 1120 | // Seq #11 |
| 1121 | if (!mService.isControlEnabled()) { |
| 1122 | return; |
| 1123 | } |
| 1124 | if (!initiatedByCec) { |
| 1125 | mService.sendCecCommand(HdmiCecMessageBuilder.buildStandby( |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 1126 | mAddress, Constants.ADDR_BROADCAST)); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | @Override |
| 1131 | @ServiceThreadOnly |
| 1132 | protected boolean handleStandby(HdmiCecMessage message) { |
| 1133 | assertRunOnServiceThread(); |
| 1134 | // Seq #12 |
| 1135 | // Tv accepts directly addressed <Standby> only. |
| 1136 | if (message.getDestination() == mAddress) { |
| 1137 | super.handleStandby(message); |
| 1138 | } |
| 1139 | return false; |
| 1140 | } |
Jinsuk Kim | 4d43d93 | 2014-07-03 16:43:58 +0900 | [diff] [blame] | 1141 | |
| 1142 | boolean isProhibitMode() { |
| 1143 | return mService.isProhibitMode(); |
| 1144 | } |
Jinsuk Kim | b38cd68 | 2014-07-07 08:05:03 +0900 | [diff] [blame] | 1145 | |
| 1146 | boolean isPowerStandbyOrTransient() { |
| 1147 | return mService.isPowerStandbyOrTransient(); |
Jungshik Jang | 8866c81 | 2014-07-08 14:42:28 +0900 | [diff] [blame] | 1148 | } |
Jinsuk Kim | c7eba0f | 2014-07-07 14:18:02 +0900 | [diff] [blame] | 1149 | |
| 1150 | void displayOsd(int messageId) { |
| 1151 | Intent intent = new Intent(HdmiControlManager.ACTION_OSD_MESSAGE); |
| 1152 | intent.putExtra(HdmiControlManager.EXTRA_MESSAGE_ID, messageId); |
| 1153 | mService.getContext().sendBroadcastAsUser(intent, UserHandle.ALL, |
| 1154 | HdmiControlService.PERMISSION); |
Jinsuk Kim | b38cd68 | 2014-07-07 08:05:03 +0900 | [diff] [blame] | 1155 | } |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 1156 | } |