OneTouchPlayAction.java revision c0c20d0522d7756d80f011e7a54bf3b51c78df41
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 */ 16package com.android.server.hdmi; 17 18import android.hardware.hdmi.HdmiControlManager; 19import android.hardware.hdmi.IHdmiControlCallback; 20import android.os.RemoteException; 21import android.util.Slog; 22 23/** 24 * Feature action that performs one touch play against TV/Display device. 25 * 26 * This action is initiated via {@link HdmiControlManager#oneTouchPlay()} from 27 * the Android system working as playback device to turn on the TV, and switch the input. 28 * 29 * <p>Package-private, accessed by {@link HdmiControlService} only. 30 */ 31 32final class OneTouchPlayAction extends FeatureAction { 33 private static final String TAG = "OneTouchPlayAction"; 34 35 // State in which the action is waiting for <Report Power Status>. In normal situation 36 // source device can simply send <Text|Image View On> and <Active Source> in succession 37 // since the standard requires that the TV/Display should buffer the <Active Source> 38 // if the TV is brought of out standby state. 39 // 40 // But there are TV's that fail to buffer the <Active Source> while getting out of 41 // standby mode, and do not accept the command until their power status becomes 'ON'. 42 // For a workaround, we send <Give Device Power Status> commands periodically to make sure 43 // the device switches its status to 'ON'. Then we send additional <Active Source>. 44 private static final int STATE_WAITING_FOR_REPORT_POWER_STATUS = 1; 45 46 // The maximum number of times we send <Give Device Power Status> before we give up. 47 // We wait up to RESPONSE_TIMEOUT_MS * LOOP_COUNTER_MAX = 20 seconds. 48 private static final int LOOP_COUNTER_MAX = 10; 49 50 private final int mTargetAddress; 51 private final IHdmiControlCallback mCallback; 52 53 private int mPowerStatusCounter = 0; 54 55 // Factory method. Ensures arguments are valid. 56 static OneTouchPlayAction create(HdmiCecLocalDevicePlayback source, 57 int targetAddress, IHdmiControlCallback callback) { 58 if (source == null || callback == null) { 59 Slog.e(TAG, "Wrong arguments"); 60 return null; 61 } 62 return new OneTouchPlayAction(source, targetAddress, 63 callback); 64 } 65 66 private OneTouchPlayAction(HdmiCecLocalDevice localDevice, int targetAddress, 67 IHdmiControlCallback callback) { 68 super(localDevice); 69 mTargetAddress = targetAddress; 70 mCallback = callback; 71 } 72 73 @Override 74 boolean start() { 75 sendCommand(HdmiCecMessageBuilder.buildTextViewOn(getSourceAddress(), mTargetAddress)); 76 broadcastActiveSource(); 77 queryDevicePowerStatus(); 78 mState = STATE_WAITING_FOR_REPORT_POWER_STATUS; 79 addTimer(mState, FeatureAction.TIMEOUT_MS); 80 return true; 81 } 82 83 private void broadcastActiveSource() { 84 sendCommand(HdmiCecMessageBuilder.buildActiveSource(getSourceAddress(), getSourcePath())); 85 // Because only playback device can create this action, it's safe to cast. 86 playback().markActiveSource(); 87 } 88 89 private void queryDevicePowerStatus() { 90 sendCommand(HdmiCecMessageBuilder.buildGiveDevicePowerStatus(getSourceAddress(), 91 mTargetAddress)); 92 } 93 94 @Override 95 boolean processCommand(HdmiCecMessage cmd) { 96 if (mState != STATE_WAITING_FOR_REPORT_POWER_STATUS) { 97 return false; 98 } 99 if (cmd.getOpcode() == Constants.MESSAGE_REPORT_POWER_STATUS) { 100 int status = cmd.getParams()[0]; 101 if (status == HdmiControlManager.POWER_STATUS_ON) { 102 broadcastActiveSource(); 103 invokeCallback(HdmiControlManager.RESULT_SUCCESS); 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(); 119 addTimer(mState, FeatureAction.TIMEOUT_MS); 120 } else { 121 // Couldn't wake up the TV for whatever reason. Report failure. 122 invokeCallback(HdmiControlManager.RESULT_TIMEOUT); 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} 136