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