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 | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 19 | import android.hardware.hdmi.HdmiDeviceInfo; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 20 | import android.os.Handler; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 21 | import android.os.Looper; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 22 | import android.os.Message; |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 23 | import android.util.Slog; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 24 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 25 | import com.android.internal.annotations.GuardedBy; |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 26 | import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 27 | |
| 28 | import java.util.ArrayList; |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 29 | import java.util.Collections; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 30 | import java.util.Iterator; |
| 31 | import java.util.LinkedList; |
| 32 | import java.util.List; |
| 33 | |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 34 | /** |
| 35 | * Class that models a logical CEC device hosted in this system. Handles initialization, |
| 36 | * CEC commands that call for actions customized per device type. |
| 37 | */ |
| 38 | abstract class HdmiCecLocalDevice { |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 39 | private static final String TAG = "HdmiCecLocalDevice"; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 40 | |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 41 | private static final int MSG_DISABLE_DEVICE_TIMEOUT = 1; |
| 42 | // Timeout in millisecond for device clean up (5s). |
| 43 | // Normal actions timeout is 2s but some of them would have several sequence of timeout. |
| 44 | private static final int DEVICE_CLEANUP_TIMEOUT = 5000; |
| 45 | |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 46 | protected final HdmiControlService mService; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 47 | protected final int mDeviceType; |
| 48 | protected int mAddress; |
| 49 | protected int mPreferredAddress; |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 50 | protected HdmiDeviceInfo mDeviceInfo; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 51 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 52 | static class ActiveSource { |
| 53 | int logicalAddress; |
| 54 | int physicalAddress; |
| 55 | |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 56 | public ActiveSource() { |
| 57 | invalidate(); |
| 58 | } |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 59 | public ActiveSource(int logical, int physical) { |
| 60 | logicalAddress = logical; |
| 61 | physicalAddress = physical; |
| 62 | } |
| 63 | public static ActiveSource of(int logical, int physical) { |
| 64 | return new ActiveSource(logical, physical); |
| 65 | } |
| 66 | public boolean isValid() { |
| 67 | return HdmiUtils.isValidAddress(logicalAddress); |
| 68 | } |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 69 | public void invalidate() { |
| 70 | logicalAddress = Constants.ADDR_INVALID; |
| 71 | physicalAddress = Constants.INVALID_PHYSICAL_ADDRESS; |
| 72 | } |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 73 | public boolean equals(int logical, int physical) { |
| 74 | return logicalAddress == logical && physicalAddress == physical; |
| 75 | } |
| 76 | @Override |
| 77 | public boolean equals(Object obj) { |
| 78 | if (obj instanceof ActiveSource) { |
| 79 | ActiveSource that = (ActiveSource) obj; |
| 80 | return that.logicalAddress == logicalAddress && |
| 81 | that.physicalAddress == physicalAddress; |
| 82 | } |
| 83 | return false; |
| 84 | } |
| 85 | @Override |
| 86 | public int hashCode() { |
| 87 | return logicalAddress * 29 + physicalAddress; |
| 88 | } |
| 89 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 90 | // Logical address of the active source. |
| 91 | @GuardedBy("mLock") |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 92 | protected final ActiveSource mActiveSource = new ActiveSource(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 93 | |
| 94 | // Active routing path. Physical address of the active source but not all the time, such as |
| 95 | // when the new active source does not claim itself to be one. Note that we don't keep |
| 96 | // the active port id (or active input) since it can be gotten by {@link #pathToPortId(int)}. |
| 97 | @GuardedBy("mLock") |
| 98 | private int mActiveRoutingPath; |
| 99 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 100 | protected final HdmiCecMessageCache mCecMessageCache = new HdmiCecMessageCache(); |
| 101 | protected final Object mLock; |
| 102 | |
| 103 | // A collection of FeatureAction. |
| 104 | // Note that access to this collection should happen in service thread. |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 105 | private final LinkedList<HdmiCecFeatureAction> mActions = new LinkedList<>(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 106 | |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 107 | private final Handler mHandler = new Handler () { |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 108 | @Override |
| 109 | public void handleMessage(Message msg) { |
| 110 | switch (msg.what) { |
| 111 | case MSG_DISABLE_DEVICE_TIMEOUT: |
| 112 | handleDisableDeviceTimeout(); |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | /** |
| 119 | * A callback interface to get notified when all pending action is cleared. |
| 120 | * It can be called when timeout happened. |
| 121 | */ |
| 122 | interface PendingActionClearedCallback { |
| 123 | void onCleared(HdmiCecLocalDevice device); |
| 124 | } |
| 125 | |
| 126 | protected PendingActionClearedCallback mPendingActionClearedCallback; |
| 127 | |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 128 | protected HdmiCecLocalDevice(HdmiControlService service, int deviceType) { |
| 129 | mService = service; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 130 | mDeviceType = deviceType; |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 131 | mAddress = Constants.ADDR_UNREGISTERED; |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 132 | mLock = service.getServiceLock(); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Factory method that returns HdmiCecLocalDevice of corresponding type. |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 136 | static HdmiCecLocalDevice create(HdmiControlService service, int deviceType) { |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 137 | switch (deviceType) { |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 138 | case HdmiDeviceInfo.DEVICE_TV: |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 139 | return new HdmiCecLocalDeviceTv(service); |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 140 | case HdmiDeviceInfo.DEVICE_PLAYBACK: |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 141 | return new HdmiCecLocalDevicePlayback(service); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 142 | default: |
| 143 | return null; |
| 144 | } |
| 145 | } |
| 146 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 147 | @ServiceThreadOnly |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 148 | void init() { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 149 | assertRunOnServiceThread(); |
Jinsuk Kim | af2acf0 | 2014-07-11 18:43:04 +0900 | [diff] [blame] | 150 | mPreferredAddress = getPreferredAddress(); |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 151 | } |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 152 | |
Jungshik Jang | 8b308d9 | 2014-05-29 21:52:28 +0900 | [diff] [blame] | 153 | /** |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 154 | * Called once a logical address of the local device is allocated. |
Jungshik Jang | 8b308d9 | 2014-05-29 21:52:28 +0900 | [diff] [blame] | 155 | */ |
Yuncheol Heo | fc44e4e | 2014-08-04 19:41:09 +0900 | [diff] [blame] | 156 | protected abstract void onAddressAllocated(int logicalAddress, int reason); |
Jungshik Jang | 8b308d9 | 2014-05-29 21:52:28 +0900 | [diff] [blame] | 157 | |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 158 | /** |
Jinsuk Kim | af2acf0 | 2014-07-11 18:43:04 +0900 | [diff] [blame] | 159 | * Get the preferred logical address from system properties. |
| 160 | */ |
| 161 | protected abstract int getPreferredAddress(); |
| 162 | |
| 163 | /** |
| 164 | * Set the preferred logical address to system properties. |
| 165 | */ |
| 166 | protected abstract void setPreferredAddress(int addr); |
| 167 | |
| 168 | /** |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 169 | * Dispatch incoming message. |
| 170 | * |
| 171 | * @param message incoming message |
| 172 | * @return true if consumed a message; otherwise, return false. |
| 173 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 174 | @ServiceThreadOnly |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 175 | boolean dispatchMessage(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 176 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 177 | int dest = message.getDestination(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 178 | if (dest != mAddress && dest != Constants.ADDR_BROADCAST) { |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 179 | return false; |
| 180 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 181 | // Cache incoming message. Note that it caches only white-listed one. |
| 182 | mCecMessageCache.cacheMessage(message); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 183 | return onMessage(message); |
| 184 | } |
| 185 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 186 | @ServiceThreadOnly |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 187 | protected final boolean onMessage(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 188 | assertRunOnServiceThread(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 189 | if (dispatchMessageToAction(message)) { |
| 190 | return true; |
| 191 | } |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 192 | switch (message.getOpcode()) { |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 193 | case Constants.MESSAGE_ACTIVE_SOURCE: |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 194 | return handleActiveSource(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 195 | case Constants.MESSAGE_INACTIVE_SOURCE: |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 196 | return handleInactiveSource(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 197 | case Constants.MESSAGE_REQUEST_ACTIVE_SOURCE: |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 198 | return handleRequestActiveSource(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 199 | case Constants.MESSAGE_GET_MENU_LANGUAGE: |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 200 | return handleGetMenuLanguage(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 201 | case Constants.MESSAGE_GIVE_PHYSICAL_ADDRESS: |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 202 | return handleGivePhysicalAddress(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 203 | case Constants.MESSAGE_GIVE_OSD_NAME: |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 204 | return handleGiveOsdName(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 205 | case Constants.MESSAGE_GIVE_DEVICE_VENDOR_ID: |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 206 | return handleGiveDeviceVendorId(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 207 | case Constants.MESSAGE_GET_CEC_VERSION: |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 208 | return handleGetCecVersion(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 209 | case Constants.MESSAGE_REPORT_PHYSICAL_ADDRESS: |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 210 | return handleReportPhysicalAddress(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 211 | case Constants.MESSAGE_ROUTING_CHANGE: |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 212 | return handleRoutingChange(message); |
Yuncheol Heo | 64bafd9 | 2014-08-11 11:17:54 +0900 | [diff] [blame] | 213 | case Constants.MESSAGE_ROUTING_INFORMATION: |
| 214 | return handleRoutingInformation(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 215 | case Constants.MESSAGE_INITIATE_ARC: |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 216 | return handleInitiateArc(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 217 | case Constants.MESSAGE_TERMINATE_ARC: |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 218 | return handleTerminateArc(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 219 | case Constants.MESSAGE_SET_SYSTEM_AUDIO_MODE: |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 220 | return handleSetSystemAudioMode(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 221 | case Constants.MESSAGE_SYSTEM_AUDIO_MODE_STATUS: |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 222 | return handleSystemAudioModeStatus(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 223 | case Constants.MESSAGE_REPORT_AUDIO_STATUS: |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 224 | return handleReportAudioStatus(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 225 | case Constants.MESSAGE_STANDBY: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 226 | return handleStandby(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 227 | case Constants.MESSAGE_TEXT_VIEW_ON: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 228 | return handleTextViewOn(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 229 | case Constants.MESSAGE_IMAGE_VIEW_ON: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 230 | return handleImageViewOn(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 231 | case Constants.MESSAGE_USER_CONTROL_PRESSED: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 232 | return handleUserControlPressed(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 233 | case Constants.MESSAGE_SET_STREAM_PATH: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 234 | return handleSetStreamPath(message); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 235 | case Constants.MESSAGE_GIVE_DEVICE_POWER_STATUS: |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 236 | return handleGiveDevicePowerStatus(message); |
Jinsuk Kim | 119160a | 2014-07-07 18:48:10 +0900 | [diff] [blame] | 237 | case Constants.MESSAGE_VENDOR_COMMAND: |
| 238 | return handleVendorCommand(message); |
| 239 | case Constants.MESSAGE_VENDOR_COMMAND_WITH_ID: |
| 240 | return handleVendorCommandWithId(message); |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 241 | case Constants.MESSAGE_SET_OSD_NAME: |
| 242 | return handleSetOsdName(message); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 243 | case Constants.MESSAGE_RECORD_TV_SCREEN: |
| 244 | return handleRecordTvScreen(message); |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 245 | case Constants.MESSAGE_TIMER_CLEARED_STATUS: |
| 246 | return handleTimerClearedStatus(message); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 247 | default: |
| 248 | return false; |
| 249 | } |
| 250 | } |
| 251 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 252 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 253 | private boolean dispatchMessageToAction(HdmiCecMessage message) { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 254 | assertRunOnServiceThread(); |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 255 | for (HdmiCecFeatureAction action : mActions) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 256 | if (action.processCommand(message)) { |
| 257 | return true; |
| 258 | } |
| 259 | } |
| 260 | return false; |
| 261 | } |
| 262 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 263 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 264 | protected boolean handleGivePhysicalAddress() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 265 | assertRunOnServiceThread(); |
| 266 | |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 267 | int physicalAddress = mService.getPhysicalAddress(); |
| 268 | HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildReportPhysicalAddressCommand( |
| 269 | mAddress, physicalAddress, mDeviceType); |
| 270 | mService.sendCecCommand(cecMessage); |
| 271 | return true; |
| 272 | } |
| 273 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 274 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 275 | protected boolean handleGiveDeviceVendorId() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 276 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 277 | int vendorId = mService.getVendorId(); |
| 278 | HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildDeviceVendorIdCommand( |
| 279 | mAddress, vendorId); |
| 280 | mService.sendCecCommand(cecMessage); |
| 281 | return true; |
| 282 | } |
| 283 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 284 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 285 | protected boolean handleGetCecVersion(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 286 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 287 | int version = mService.getCecVersion(); |
| 288 | HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildCecVersion(message.getDestination(), |
| 289 | message.getSource(), version); |
| 290 | mService.sendCecCommand(cecMessage); |
| 291 | return true; |
| 292 | } |
| 293 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 294 | @ServiceThreadOnly |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 295 | protected boolean handleActiveSource(HdmiCecMessage message) { |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | @ServiceThreadOnly |
| 300 | protected boolean handleInactiveSource(HdmiCecMessage message) { |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | @ServiceThreadOnly |
| 305 | protected boolean handleRequestActiveSource(HdmiCecMessage message) { |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 310 | protected boolean handleGetMenuLanguage(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 311 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 312 | Slog.w(TAG, "Only TV can handle <Get Menu Language>:" + message.toString()); |
Yuncheol Heo | 6aae652 | 2014-08-05 14:48:37 +0900 | [diff] [blame] | 313 | // 'return false' will cause to reply with <Feature Abort>. |
| 314 | return false; |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 315 | } |
| 316 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 317 | @ServiceThreadOnly |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 318 | protected boolean handleGiveOsdName(HdmiCecMessage message) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 319 | assertRunOnServiceThread(); |
Jungshik Jang | 092b445 | 2014-06-11 15:19:17 +0900 | [diff] [blame] | 320 | // Note that since this method is called after logical address allocation is done, |
| 321 | // mDeviceInfo should not be null. |
| 322 | HdmiCecMessage cecMessage = HdmiCecMessageBuilder.buildSetOsdNameCommand( |
| 323 | mAddress, message.getSource(), mDeviceInfo.getDisplayName()); |
| 324 | if (cecMessage != null) { |
| 325 | mService.sendCecCommand(cecMessage); |
| 326 | } else { |
| 327 | Slog.w(TAG, "Failed to build <Get Osd Name>:" + mDeviceInfo.getDisplayName()); |
| 328 | } |
| 329 | return true; |
| 330 | } |
| 331 | |
Jinsuk Kim | 92b77cf | 2014-06-27 16:39:26 +0900 | [diff] [blame] | 332 | protected boolean handleRoutingChange(HdmiCecMessage message) { |
| 333 | return false; |
| 334 | } |
| 335 | |
Yuncheol Heo | 64bafd9 | 2014-08-11 11:17:54 +0900 | [diff] [blame] | 336 | protected boolean handleRoutingInformation(HdmiCecMessage message) { |
| 337 | return false; |
| 338 | } |
| 339 | |
Jungshik Jang | 60cffce | 2014-06-12 18:03:04 +0900 | [diff] [blame] | 340 | protected boolean handleReportPhysicalAddress(HdmiCecMessage message) { |
| 341 | return false; |
| 342 | } |
| 343 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 344 | protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) { |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | protected boolean handleSetSystemAudioMode(HdmiCecMessage message) { |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | protected boolean handleTerminateArc(HdmiCecMessage message) { |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | protected boolean handleInitiateArc(HdmiCecMessage message) { |
| 357 | return false; |
| 358 | } |
| 359 | |
Jungshik Jang | 8fa36b1 | 2014-06-25 15:51:36 +0900 | [diff] [blame] | 360 | protected boolean handleReportAudioStatus(HdmiCecMessage message) { |
| 361 | return false; |
| 362 | } |
| 363 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 364 | @ServiceThreadOnly |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 365 | protected boolean handleStandby(HdmiCecMessage message) { |
| 366 | assertRunOnServiceThread(); |
| 367 | // Seq #12 |
Jinsuk Kim | 4d43d93 | 2014-07-03 16:43:58 +0900 | [diff] [blame] | 368 | if (mService.isControlEnabled() && !mService.isProhibitMode() |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 369 | && mService.isPowerOnOrTransient()) { |
| 370 | mService.standby(); |
| 371 | return true; |
| 372 | } |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | @ServiceThreadOnly |
| 377 | protected boolean handleUserControlPressed(HdmiCecMessage message) { |
| 378 | assertRunOnServiceThread(); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 379 | if (mService.isPowerOnOrTransient() && isPowerOffOrToggleCommand(message)) { |
| 380 | mService.standby(); |
| 381 | return true; |
| 382 | } else if (mService.isPowerStandbyOrTransient() && isPowerOnOrToggleCommand(message)) { |
| 383 | mService.wakeUp(); |
| 384 | return true; |
| 385 | } |
| 386 | return false; |
| 387 | } |
| 388 | |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 389 | static boolean isPowerOnOrToggleCommand(HdmiCecMessage message) { |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 390 | byte[] params = message.getParams(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 391 | return message.getOpcode() == Constants.MESSAGE_USER_CONTROL_PRESSED |
Jungshik Jang | 210d73d | 2014-07-04 11:11:29 +0900 | [diff] [blame] | 392 | && (params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER |
| 393 | || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_ON_FUNCTION |
| 394 | || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_TOGGLE_FUNCTION); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 395 | } |
| 396 | |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 397 | static boolean isPowerOffOrToggleCommand(HdmiCecMessage message) { |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 398 | byte[] params = message.getParams(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 399 | return message.getOpcode() == Constants.MESSAGE_USER_CONTROL_PRESSED |
Jungshik Jang | 210d73d | 2014-07-04 11:11:29 +0900 | [diff] [blame] | 400 | && (params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER |
| 401 | || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_OFF_FUNCTION |
| 402 | || params[0] == HdmiCecKeycode.CEC_KEYCODE_POWER_TOGGLE_FUNCTION); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | protected boolean handleTextViewOn(HdmiCecMessage message) { |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | protected boolean handleImageViewOn(HdmiCecMessage message) { |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | protected boolean handleSetStreamPath(HdmiCecMessage message) { |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | protected boolean handleGiveDevicePowerStatus(HdmiCecMessage message) { |
| 418 | mService.sendCecCommand(HdmiCecMessageBuilder.buildReportPowerStatus( |
| 419 | mAddress, message.getSource(), mService.getPowerStatus())); |
| 420 | return true; |
| 421 | } |
| 422 | |
Jinsuk Kim | 119160a | 2014-07-07 18:48:10 +0900 | [diff] [blame] | 423 | protected boolean handleVendorCommand(HdmiCecMessage message) { |
| 424 | mService.invokeVendorCommandListeners(mDeviceType, message.getSource(), |
| 425 | message.getParams(), false); |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | protected boolean handleVendorCommandWithId(HdmiCecMessage message) { |
| 430 | byte[] params = message.getParams(); |
| 431 | int vendorId = HdmiUtils.threeBytesToInt(params); |
| 432 | if (vendorId == mService.getVendorId()) { |
| 433 | mService.invokeVendorCommandListeners(mDeviceType, message.getSource(), params, true); |
| 434 | } else if (message.getDestination() != Constants.ADDR_BROADCAST && |
| 435 | message.getSource() != Constants.ADDR_UNREGISTERED) { |
| 436 | Slog.v(TAG, "Wrong direct vendor command. Replying with <Feature Abort>"); |
Yuncheol Heo | 6aae652 | 2014-08-05 14:48:37 +0900 | [diff] [blame] | 437 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_UNRECOGNIZED_OPCODE); |
Jinsuk Kim | 119160a | 2014-07-07 18:48:10 +0900 | [diff] [blame] | 438 | } else { |
| 439 | Slog.v(TAG, "Wrong broadcast vendor command. Ignoring"); |
| 440 | } |
| 441 | return true; |
| 442 | } |
| 443 | |
Jungshik Jang | 8f2ed35 | 2014-07-07 15:02:47 +0900 | [diff] [blame] | 444 | protected boolean handleSetOsdName(HdmiCecMessage message) { |
| 445 | // The default behavior of <Set Osd Name> is doing nothing. |
| 446 | return true; |
| 447 | } |
| 448 | |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 449 | protected boolean handleRecordTvScreen(HdmiCecMessage message) { |
Yuncheol Heo | 25c2029 | 2014-07-31 17:59:39 +0900 | [diff] [blame] | 450 | // The default behavior of <Record TV Screen> is replying <Feature Abort> with |
| 451 | // "Cannot provide source". |
Yuncheol Heo | 6aae652 | 2014-08-05 14:48:37 +0900 | [diff] [blame] | 452 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_CANNOT_PROVIDE_SOURCE); |
Jungshik Jang | b6591b8 | 2014-07-23 16:10:23 +0900 | [diff] [blame] | 453 | return true; |
| 454 | } |
| 455 | |
Jungshik Jang | e5a9337 | 2014-07-25 13:41:14 +0900 | [diff] [blame] | 456 | protected boolean handleTimerClearedStatus(HdmiCecMessage message) { |
| 457 | return false; |
| 458 | } |
| 459 | |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 460 | @ServiceThreadOnly |
Yuncheol Heo | fc44e4e | 2014-08-04 19:41:09 +0900 | [diff] [blame] | 461 | final void handleAddressAllocated(int logicalAddress, int reason) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 462 | assertRunOnServiceThread(); |
Jungshik Jang | 3ee6572 | 2014-06-03 16:22:30 +0900 | [diff] [blame] | 463 | mAddress = mPreferredAddress = logicalAddress; |
Yuncheol Heo | fc44e4e | 2014-08-04 19:41:09 +0900 | [diff] [blame] | 464 | onAddressAllocated(logicalAddress, reason); |
Jinsuk Kim | af2acf0 | 2014-07-11 18:43:04 +0900 | [diff] [blame] | 465 | setPreferredAddress(logicalAddress); |
Jungshik Jang | 1a4485d | 2014-05-26 11:02:36 +0900 | [diff] [blame] | 466 | } |
| 467 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 468 | @ServiceThreadOnly |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 469 | HdmiDeviceInfo getDeviceInfo() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 470 | assertRunOnServiceThread(); |
Jungshik Jang | 1a4485d | 2014-05-26 11:02:36 +0900 | [diff] [blame] | 471 | return mDeviceInfo; |
| 472 | } |
| 473 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 474 | @ServiceThreadOnly |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 475 | void setDeviceInfo(HdmiDeviceInfo info) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 476 | assertRunOnServiceThread(); |
Jungshik Jang | 1a4485d | 2014-05-26 11:02:36 +0900 | [diff] [blame] | 477 | mDeviceInfo = info; |
| 478 | } |
| 479 | |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 480 | // Returns true if the logical address is same as the argument. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 481 | @ServiceThreadOnly |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 482 | boolean isAddressOf(int addr) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 483 | assertRunOnServiceThread(); |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 484 | return addr == mAddress; |
| 485 | } |
| 486 | |
| 487 | // Resets the logical address to unregistered(15), meaning the logical device is invalid. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 488 | @ServiceThreadOnly |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 489 | void clearAddress() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 490 | assertRunOnServiceThread(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 491 | mAddress = Constants.ADDR_UNREGISTERED; |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 492 | } |
| 493 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 494 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 495 | void addAndStartAction(final HdmiCecFeatureAction action) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 496 | assertRunOnServiceThread(); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 497 | if (mService.isPowerStandbyOrTransient()) { |
| 498 | Slog.w(TAG, "Skip the action during Standby: " + action); |
| 499 | return; |
| 500 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 501 | mActions.add(action); |
| 502 | action.start(); |
| 503 | } |
| 504 | |
| 505 | // See if we have an action of a given type in progress. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 506 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 507 | <T extends HdmiCecFeatureAction> boolean hasAction(final Class<T> clazz) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 508 | assertRunOnServiceThread(); |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 509 | for (HdmiCecFeatureAction action : mActions) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 510 | if (action.getClass().equals(clazz)) { |
| 511 | return true; |
| 512 | } |
| 513 | } |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | // Returns all actions matched with given class type. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 518 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 519 | <T extends HdmiCecFeatureAction> List<T> getActions(final Class<T> clazz) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 520 | assertRunOnServiceThread(); |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 521 | List<T> actions = Collections.<T>emptyList(); |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 522 | for (HdmiCecFeatureAction action : mActions) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 523 | if (action.getClass().equals(clazz)) { |
Jinsuk Kim | 43c23e2 | 2014-07-29 13:59:14 +0900 | [diff] [blame] | 524 | if (actions.isEmpty()) { |
| 525 | actions = new ArrayList<T>(); |
| 526 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 527 | actions.add((T) action); |
| 528 | } |
| 529 | } |
| 530 | return actions; |
| 531 | } |
| 532 | |
| 533 | /** |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 534 | * Remove the given {@link HdmiCecFeatureAction} object from the action queue. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 535 | * |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 536 | * @param action {@link HdmiCecFeatureAction} to remove |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 537 | */ |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 538 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 539 | void removeAction(final HdmiCecFeatureAction action) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 540 | assertRunOnServiceThread(); |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 541 | action.finish(false); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 542 | mActions.remove(action); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 543 | checkIfPendingActionsCleared(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | // Remove all actions matched with the given Class type. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 547 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 548 | <T extends HdmiCecFeatureAction> void removeAction(final Class<T> clazz) { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 549 | assertRunOnServiceThread(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 550 | removeActionExcept(clazz, null); |
| 551 | } |
| 552 | |
| 553 | // Remove all actions matched with the given Class type besides |exception|. |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 554 | @ServiceThreadOnly |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 555 | <T extends HdmiCecFeatureAction> void removeActionExcept(final Class<T> clazz, |
| 556 | final HdmiCecFeatureAction exception) { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 557 | assertRunOnServiceThread(); |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 558 | Iterator<HdmiCecFeatureAction> iter = mActions.iterator(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 559 | while (iter.hasNext()) { |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 560 | HdmiCecFeatureAction action = iter.next(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 561 | if (action != exception && action.getClass().equals(clazz)) { |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 562 | action.finish(false); |
| 563 | iter.remove(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 564 | } |
| 565 | } |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 566 | checkIfPendingActionsCleared(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 567 | } |
| 568 | |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 569 | protected void checkIfPendingActionsCleared() { |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 570 | if (mActions.isEmpty() && mPendingActionClearedCallback != null) { |
Yuncheol Heo | 26ba7fd | 2014-07-29 18:21:25 +0900 | [diff] [blame] | 571 | PendingActionClearedCallback callback = mPendingActionClearedCallback; |
| 572 | // To prevent from calling the callback again during handling the callback itself. |
| 573 | mPendingActionClearedCallback = null; |
| 574 | callback.onCleared(this); |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 575 | } |
| 576 | } |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 577 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 578 | protected void assertRunOnServiceThread() { |
| 579 | if (Looper.myLooper() != mService.getServiceLooper()) { |
| 580 | throw new IllegalStateException("Should run on service thread."); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Called when a hot-plug event issued. |
| 586 | * |
| 587 | * @param portId id of port where a hot-plug event happened |
| 588 | * @param connected whether to connected or not on the event |
| 589 | */ |
| 590 | void onHotplug(int portId, boolean connected) { |
| 591 | } |
| 592 | |
| 593 | final HdmiControlService getService() { |
| 594 | return mService; |
| 595 | } |
| 596 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 597 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 598 | final boolean isConnectedToArcPort(int path) { |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 599 | assertRunOnServiceThread(); |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 600 | return mService.isConnectedToArcPort(path); |
| 601 | } |
| 602 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 603 | ActiveSource getActiveSource() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 604 | synchronized (mLock) { |
| 605 | return mActiveSource; |
| 606 | } |
| 607 | } |
| 608 | |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 609 | void setActiveSource(ActiveSource newActive) { |
| 610 | setActiveSource(newActive.logicalAddress, newActive.physicalAddress); |
| 611 | } |
| 612 | |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 613 | void setActiveSource(HdmiDeviceInfo info) { |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 614 | setActiveSource(info.getLogicalAddress(), info.getPhysicalAddress()); |
| 615 | } |
| 616 | |
| 617 | void setActiveSource(int logicalAddress, int physicalAddress) { |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 618 | synchronized (mLock) { |
Jinsuk Kim | 72b7d73 | 2014-07-24 09:15:35 +0900 | [diff] [blame] | 619 | mActiveSource.logicalAddress = logicalAddress; |
| 620 | mActiveSource.physicalAddress = physicalAddress; |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 624 | int getActivePath() { |
| 625 | synchronized (mLock) { |
| 626 | return mActiveRoutingPath; |
| 627 | } |
| 628 | } |
| 629 | |
Jinsuk Kim | 8333571 | 2014-06-24 07:57:00 +0900 | [diff] [blame] | 630 | void setActivePath(int path) { |
| 631 | synchronized (mLock) { |
| 632 | mActiveRoutingPath = path; |
| 633 | } |
| 634 | } |
| 635 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 636 | /** |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 637 | * Returns the ID of the active HDMI port. The active port is the one that has the active |
| 638 | * routing path connected to it directly or indirectly under the device hierarchy. |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 639 | */ |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 640 | int getActivePortId() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 641 | synchronized (mLock) { |
| 642 | return mService.pathToPortId(mActiveRoutingPath); |
| 643 | } |
| 644 | } |
| 645 | |
Jinsuk Kim | a062a93 | 2014-06-18 10:00:39 +0900 | [diff] [blame] | 646 | /** |
| 647 | * Update the active port. |
| 648 | * |
| 649 | * @param portId the new active port id |
| 650 | */ |
| 651 | void setActivePortId(int portId) { |
| 652 | synchronized (mLock) { |
| 653 | // We update active routing path instead, since we get the active port id from |
| 654 | // the active routing path. |
| 655 | mActiveRoutingPath = mService.portIdToPath(portId); |
| 656 | } |
| 657 | } |
| 658 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 659 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 660 | HdmiCecMessageCache getCecMessageCache() { |
| 661 | assertRunOnServiceThread(); |
| 662 | return mCecMessageCache; |
| 663 | } |
| 664 | |
Jungshik Jang | a5b7414 | 2014-06-23 18:03:10 +0900 | [diff] [blame] | 665 | @ServiceThreadOnly |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 666 | int pathToPortId(int newPath) { |
| 667 | assertRunOnServiceThread(); |
| 668 | return mService.pathToPortId(newPath); |
| 669 | } |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 670 | |
| 671 | /** |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 672 | * Called when the system goes to standby mode. |
| 673 | * |
| 674 | * @param initiatedByCec true if this power sequence is initiated |
Jungshik Jang | b3e114a | 2014-07-11 17:50:05 +0900 | [diff] [blame] | 675 | * by the reception the CEC messages like <Standby> |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 676 | */ |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 677 | protected void onStandby(boolean initiatedByCec) {} |
| 678 | |
| 679 | /** |
| 680 | * Disable device. {@code callback} is used to get notified when all pending |
| 681 | * actions are completed or timeout is issued. |
| 682 | * |
| 683 | * @param initiatedByCec true if this sequence is initiated |
Jungshik Jang | b3e114a | 2014-07-11 17:50:05 +0900 | [diff] [blame] | 684 | * by the reception the CEC messages like <Standby> |
| 685 | * @param origialCallback callback interface to get notified when all pending actions are |
| 686 | * cleared |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 687 | */ |
Jungshik Jang | b3e114a | 2014-07-11 17:50:05 +0900 | [diff] [blame] | 688 | protected void disableDevice(boolean initiatedByCec, |
| 689 | final PendingActionClearedCallback origialCallback) { |
| 690 | mPendingActionClearedCallback = new PendingActionClearedCallback() { |
| 691 | @Override |
| 692 | public void onCleared(HdmiCecLocalDevice device) { |
| 693 | mHandler.removeMessages(MSG_DISABLE_DEVICE_TIMEOUT); |
| 694 | origialCallback.onCleared(device); |
| 695 | } |
| 696 | }; |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 697 | mHandler.sendMessageDelayed(Message.obtain(mHandler, MSG_DISABLE_DEVICE_TIMEOUT), |
| 698 | DEVICE_CLEANUP_TIMEOUT); |
| 699 | } |
| 700 | |
| 701 | @ServiceThreadOnly |
| 702 | private void handleDisableDeviceTimeout() { |
| 703 | assertRunOnServiceThread(); |
| 704 | |
| 705 | // If all actions are not cleared in DEVICE_CLEANUP_TIMEOUT, enforce to finish them. |
| 706 | // onCleard will be called at the last action's finish method. |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 707 | Iterator<HdmiCecFeatureAction> iter = mActions.iterator(); |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 708 | while (iter.hasNext()) { |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 709 | HdmiCecFeatureAction action = iter.next(); |
Yuncheol Heo | c516d65 | 2014-07-11 18:23:24 +0900 | [diff] [blame] | 710 | action.finish(false); |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 711 | iter.remove(); |
| 712 | } |
| 713 | } |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 714 | |
| 715 | /** |
| 716 | * Send a key event to other device. |
| 717 | * |
Jungshik Jang | 4fc1d10 | 2014-07-09 19:24:50 +0900 | [diff] [blame] | 718 | * @param keyCode key code defined in {@link android.view.KeyEvent} |
Jinsuk Kim | c068bb5 | 2014-07-07 16:59:20 +0900 | [diff] [blame] | 719 | * @param isPressed {@code true} for key down event |
| 720 | */ |
| 721 | protected void sendKeyEvent(int keyCode, boolean isPressed) { |
| 722 | Slog.w(TAG, "sendKeyEvent not implemented"); |
| 723 | } |
Jinsuk Kim | 2918e9e | 2014-05-20 16:45:45 +0900 | [diff] [blame] | 724 | } |