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