Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +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 | package com.android.server.hdmi; |
| 17 | |
Yuncheol Heo | 38db629 | 2014-07-01 14:15:14 +0900 | [diff] [blame] | 18 | import android.hardware.hdmi.HdmiControlManager; |
Jungshik Jang | 8e93c84 | 2014-08-06 15:48:33 +0900 | [diff] [blame] | 19 | import android.hardware.hdmi.HdmiPlaybackClient.OneTouchPlayCallback; |
Amy | 848a9f2 | 2018-08-27 17:21:26 -0700 | [diff] [blame] | 20 | import android.hardware.hdmi.IHdmiControlCallback; |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 21 | import android.os.RemoteException; |
| 22 | import android.util.Slog; |
| 23 | |
Jinsuk Kim | cb80287 | 2015-10-13 08:22:09 +0900 | [diff] [blame] | 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 27 | /** |
Jungshik Jang | 8e93c84 | 2014-08-06 15:48:33 +0900 | [diff] [blame] | 28 | * Feature action that performs one touch play against TV/Display device. This action is initiated |
| 29 | * via {@link android.hardware.hdmi.HdmiPlaybackClient#oneTouchPlay(OneTouchPlayCallback)} from the |
| 30 | * Android system working as playback device to turn on the TV, and switch the input. |
| 31 | * <p> |
| 32 | * Package-private, accessed by {@link HdmiControlService} only. |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 33 | */ |
Jungshik Jang | b509c2e | 2014-08-07 13:45:01 +0900 | [diff] [blame] | 34 | final class OneTouchPlayAction extends HdmiCecFeatureAction { |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 35 | private static final String TAG = "OneTouchPlayAction"; |
| 36 | |
| 37 | // State in which the action is waiting for <Report Power Status>. In normal situation |
| 38 | // source device can simply send <Text|Image View On> and <Active Source> in succession |
| 39 | // since the standard requires that the TV/Display should buffer the <Active Source> |
| 40 | // if the TV is brought of out standby state. |
| 41 | // |
| 42 | // But there are TV's that fail to buffer the <Active Source> while getting out of |
| 43 | // standby mode, and do not accept the command until their power status becomes 'ON'. |
| 44 | // For a workaround, we send <Give Device Power Status> commands periodically to make sure |
| 45 | // the device switches its status to 'ON'. Then we send additional <Active Source>. |
| 46 | private static final int STATE_WAITING_FOR_REPORT_POWER_STATUS = 1; |
| 47 | |
| 48 | // The maximum number of times we send <Give Device Power Status> before we give up. |
| 49 | // We wait up to RESPONSE_TIMEOUT_MS * LOOP_COUNTER_MAX = 20 seconds. |
| 50 | private static final int LOOP_COUNTER_MAX = 10; |
| 51 | |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 52 | private final int mTargetAddress; |
Jinsuk Kim | cb80287 | 2015-10-13 08:22:09 +0900 | [diff] [blame] | 53 | private final List<IHdmiControlCallback> mCallbacks = new ArrayList<>(); |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 54 | |
| 55 | private int mPowerStatusCounter = 0; |
| 56 | |
| 57 | // Factory method. Ensures arguments are valid. |
Amy | 848a9f2 | 2018-08-27 17:21:26 -0700 | [diff] [blame] | 58 | static OneTouchPlayAction create(HdmiCecLocalDeviceSource source, |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 59 | int targetAddress, IHdmiControlCallback callback) { |
| 60 | if (source == null || callback == null) { |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 61 | Slog.e(TAG, "Wrong arguments"); |
| 62 | return null; |
| 63 | } |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 64 | return new OneTouchPlayAction(source, targetAddress, |
| 65 | callback); |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 66 | } |
| 67 | |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 68 | private OneTouchPlayAction(HdmiCecLocalDevice localDevice, int targetAddress, |
| 69 | IHdmiControlCallback callback) { |
| 70 | super(localDevice); |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 71 | mTargetAddress = targetAddress; |
Jinsuk Kim | cb80287 | 2015-10-13 08:22:09 +0900 | [diff] [blame] | 72 | addCallback(callback); |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | boolean start() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 77 | sendCommand(HdmiCecMessageBuilder.buildTextViewOn(getSourceAddress(), mTargetAddress)); |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 78 | broadcastActiveSource(); |
| 79 | queryDevicePowerStatus(); |
| 80 | mState = STATE_WAITING_FOR_REPORT_POWER_STATUS; |
Jinsuk Kim | 5fba96d | 2014-07-11 11:51:34 +0900 | [diff] [blame] | 81 | addTimer(mState, HdmiConfig.TIMEOUT_MS); |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
| 85 | private void broadcastActiveSource() { |
Amy | 848a9f2 | 2018-08-27 17:21:26 -0700 | [diff] [blame] | 86 | // Because only source device can create this action, it's safe to cast. |
Amy | f1baf56 | 2018-09-18 14:01:53 -0700 | [diff] [blame] | 87 | HdmiCecLocalDeviceSource source = source(); |
Amy | 123ec40 | 2018-09-25 10:56:31 -0700 | [diff] [blame] | 88 | source.mService.setAndBroadcastActiveSourceFromOneDeviceType( |
| 89 | mTargetAddress, getSourcePath()); |
Amy | f1baf56 | 2018-09-18 14:01:53 -0700 | [diff] [blame] | 90 | // Set local active port to HOME when One Touch Play. |
Amy | 03afe48 | 2018-09-18 16:57:45 -0700 | [diff] [blame] | 91 | // Active Port and Current Input are handled by the switch functionality device. |
| 92 | if (source.mService.audioSystem() != null) { |
| 93 | source = source.mService.audioSystem(); |
| 94 | } |
Amy | 51c6a63 | 2018-10-23 20:13:19 -0700 | [diff] [blame] | 95 | if (source.getLocalActivePort() != Constants.CEC_SWITCH_HOME) { |
Amy | ec1dec7 | 2018-11-09 15:57:25 -0800 | [diff] [blame] | 96 | source.switchInputOnReceivingNewActivePath(getSourcePath()); |
Amy | 51c6a63 | 2018-10-23 20:13:19 -0700 | [diff] [blame] | 97 | } |
Amy | 03afe48 | 2018-09-18 16:57:45 -0700 | [diff] [blame] | 98 | source.setRoutingPort(Constants.CEC_SWITCH_HOME); |
Amy | f1baf56 | 2018-09-18 14:01:53 -0700 | [diff] [blame] | 99 | source.setLocalActivePort(Constants.CEC_SWITCH_HOME); |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private void queryDevicePowerStatus() { |
Jungshik Jang | 79c58a4 | 2014-06-16 16:45:36 +0900 | [diff] [blame] | 103 | sendCommand(HdmiCecMessageBuilder.buildGiveDevicePowerStatus(getSourceAddress(), |
| 104 | mTargetAddress)); |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | boolean processCommand(HdmiCecMessage cmd) { |
Jungshik Jang | 5352081c | 2014-09-22 15:14:49 +0900 | [diff] [blame] | 109 | if (mState != STATE_WAITING_FOR_REPORT_POWER_STATUS |
| 110 | || mTargetAddress != cmd.getSource()) { |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 111 | return false; |
| 112 | } |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 113 | if (cmd.getOpcode() == Constants.MESSAGE_REPORT_POWER_STATUS) { |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 114 | int status = cmd.getParams()[0]; |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 115 | if (status == HdmiControlManager.POWER_STATUS_ON) { |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 116 | broadcastActiveSource(); |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 117 | invokeCallback(HdmiControlManager.RESULT_SUCCESS); |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 118 | finish(); |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | void handleTimerEvent(int state) { |
| 127 | if (mState != state) { |
| 128 | return; |
| 129 | } |
| 130 | if (state == STATE_WAITING_FOR_REPORT_POWER_STATUS) { |
| 131 | if (mPowerStatusCounter++ < LOOP_COUNTER_MAX) { |
| 132 | queryDevicePowerStatus(); |
Jinsuk Kim | 5fba96d | 2014-07-11 11:51:34 +0900 | [diff] [blame] | 133 | addTimer(mState, HdmiConfig.TIMEOUT_MS); |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 134 | } else { |
| 135 | // Couldn't wake up the TV for whatever reason. Report failure. |
Jinsuk Kim | c0c20d0 | 2014-07-04 14:34:31 +0900 | [diff] [blame] | 136 | invokeCallback(HdmiControlManager.RESULT_TIMEOUT); |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 137 | finish(); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
Jinsuk Kim | cb80287 | 2015-10-13 08:22:09 +0900 | [diff] [blame] | 142 | public void addCallback(IHdmiControlCallback callback) { |
| 143 | mCallbacks.add(callback); |
| 144 | } |
| 145 | |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 146 | private void invokeCallback(int result) { |
| 147 | try { |
Jinsuk Kim | cb80287 | 2015-10-13 08:22:09 +0900 | [diff] [blame] | 148 | for (IHdmiControlCallback callback : mCallbacks) { |
| 149 | callback.onComplete(result); |
| 150 | } |
Jinsuk Kim | 78d695d | 2014-05-13 16:36:15 +0900 | [diff] [blame] | 151 | } catch (RemoteException e) { |
| 152 | Slog.e(TAG, "Callback failed:" + e); |
| 153 | } |
| 154 | } |
| 155 | } |