blob: a711102f84fd7b7988a57a3189eb3022d5831a6f [file] [log] [blame]
Jinsuk Kim78d695d2014-05-13 16:36:15 +09001/*
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
Yuncheol Heo38db6292014-07-01 14:15:14 +090018import android.hardware.hdmi.HdmiControlManager;
19import android.hardware.hdmi.IHdmiControlCallback;
Jungshik Jang8e93c842014-08-06 15:48:33 +090020import android.hardware.hdmi.HdmiPlaybackClient.OneTouchPlayCallback;
Jinsuk Kim78d695d2014-05-13 16:36:15 +090021import android.os.RemoteException;
22import android.util.Slog;
23
24/**
Jungshik Jang8e93c842014-08-06 15:48:33 +090025 * 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 Kim78d695d2014-05-13 16:36:15 +090030 */
Jungshik Jangb509c2e2014-08-07 13:45:01 +090031final class OneTouchPlayAction extends HdmiCecFeatureAction {
Jinsuk Kim78d695d2014-05-13 16:36:15 +090032 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 Kim78d695d2014-05-13 16:36:15 +090049 private final int mTargetAddress;
50 private final IHdmiControlCallback mCallback;
51
52 private int mPowerStatusCounter = 0;
53
54 // Factory method. Ensures arguments are valid.
Yuncheol Heo38db6292014-07-01 14:15:14 +090055 static OneTouchPlayAction create(HdmiCecLocalDevicePlayback source,
Jungshik Jang79c58a42014-06-16 16:45:36 +090056 int targetAddress, IHdmiControlCallback callback) {
57 if (source == null || callback == null) {
Jinsuk Kim78d695d2014-05-13 16:36:15 +090058 Slog.e(TAG, "Wrong arguments");
59 return null;
60 }
Jungshik Jang79c58a42014-06-16 16:45:36 +090061 return new OneTouchPlayAction(source, targetAddress,
62 callback);
Jinsuk Kim78d695d2014-05-13 16:36:15 +090063 }
64
Jungshik Jang79c58a42014-06-16 16:45:36 +090065 private OneTouchPlayAction(HdmiCecLocalDevice localDevice, int targetAddress,
66 IHdmiControlCallback callback) {
67 super(localDevice);
Jinsuk Kim78d695d2014-05-13 16:36:15 +090068 mTargetAddress = targetAddress;
69 mCallback = callback;
70 }
71
72 @Override
73 boolean start() {
Jungshik Jang79c58a42014-06-16 16:45:36 +090074 sendCommand(HdmiCecMessageBuilder.buildTextViewOn(getSourceAddress(), mTargetAddress));
Jinsuk Kim78d695d2014-05-13 16:36:15 +090075 broadcastActiveSource();
76 queryDevicePowerStatus();
77 mState = STATE_WAITING_FOR_REPORT_POWER_STATUS;
Jinsuk Kim5fba96d2014-07-11 11:51:34 +090078 addTimer(mState, HdmiConfig.TIMEOUT_MS);
Jinsuk Kim78d695d2014-05-13 16:36:15 +090079 return true;
80 }
81
82 private void broadcastActiveSource() {
Jungshik Jang79c58a42014-06-16 16:45:36 +090083 sendCommand(HdmiCecMessageBuilder.buildActiveSource(getSourceAddress(), getSourcePath()));
Yuncheol Heo38db6292014-07-01 14:15:14 +090084 // Because only playback device can create this action, it's safe to cast.
Jinsuk Kime26d8332015-01-09 08:55:41 +090085 playback().setActiveSource(true);
Jinsuk Kim78d695d2014-05-13 16:36:15 +090086 }
87
88 private void queryDevicePowerStatus() {
Jungshik Jang79c58a42014-06-16 16:45:36 +090089 sendCommand(HdmiCecMessageBuilder.buildGiveDevicePowerStatus(getSourceAddress(),
90 mTargetAddress));
Jinsuk Kim78d695d2014-05-13 16:36:15 +090091 }
92
93 @Override
94 boolean processCommand(HdmiCecMessage cmd) {
Jungshik Jang5352081c2014-09-22 15:14:49 +090095 if (mState != STATE_WAITING_FOR_REPORT_POWER_STATUS
96 || mTargetAddress != cmd.getSource()) {
Jinsuk Kim78d695d2014-05-13 16:36:15 +090097 return false;
98 }
Jinsuk Kimc0c20d02014-07-04 14:34:31 +090099 if (cmd.getOpcode() == Constants.MESSAGE_REPORT_POWER_STATUS) {
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900100 int status = cmd.getParams()[0];
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900101 if (status == HdmiControlManager.POWER_STATUS_ON) {
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900102 broadcastActiveSource();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900103 invokeCallback(HdmiControlManager.RESULT_SUCCESS);
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900104 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 Kim5fba96d2014-07-11 11:51:34 +0900119 addTimer(mState, HdmiConfig.TIMEOUT_MS);
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900120 } else {
121 // Couldn't wake up the TV for whatever reason. Report failure.
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900122 invokeCallback(HdmiControlManager.RESULT_TIMEOUT);
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900123 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}