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