blob: 354d8d1e43e07fd2e763b8c9126cd5018444c8f9 [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;
Jungshik Jang8e93c842014-08-06 15:48:33 +090019import android.hardware.hdmi.HdmiPlaybackClient.OneTouchPlayCallback;
Amy848a9f22018-08-27 17:21:26 -070020import android.hardware.hdmi.IHdmiControlCallback;
Jinsuk Kim78d695d2014-05-13 16:36:15 +090021import android.os.RemoteException;
22import android.util.Slog;
23
Jinsuk Kimcb802872015-10-13 08:22:09 +090024import java.util.ArrayList;
25import java.util.List;
26
Jinsuk Kim78d695d2014-05-13 16:36:15 +090027/**
Jungshik Jang8e93c842014-08-06 15:48:33 +090028 * 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 Kim78d695d2014-05-13 16:36:15 +090033 */
Jungshik Jangb509c2e2014-08-07 13:45:01 +090034final class OneTouchPlayAction extends HdmiCecFeatureAction {
Jinsuk Kim78d695d2014-05-13 16:36:15 +090035 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 Kim78d695d2014-05-13 16:36:15 +090052 private final int mTargetAddress;
Jinsuk Kimcb802872015-10-13 08:22:09 +090053 private final List<IHdmiControlCallback> mCallbacks = new ArrayList<>();
Jinsuk Kim78d695d2014-05-13 16:36:15 +090054
55 private int mPowerStatusCounter = 0;
56
57 // Factory method. Ensures arguments are valid.
Amy848a9f22018-08-27 17:21:26 -070058 static OneTouchPlayAction create(HdmiCecLocalDeviceSource source,
Jungshik Jang79c58a42014-06-16 16:45:36 +090059 int targetAddress, IHdmiControlCallback callback) {
60 if (source == null || callback == null) {
Jinsuk Kim78d695d2014-05-13 16:36:15 +090061 Slog.e(TAG, "Wrong arguments");
62 return null;
63 }
Jungshik Jang79c58a42014-06-16 16:45:36 +090064 return new OneTouchPlayAction(source, targetAddress,
65 callback);
Jinsuk Kim78d695d2014-05-13 16:36:15 +090066 }
67
Jungshik Jang79c58a42014-06-16 16:45:36 +090068 private OneTouchPlayAction(HdmiCecLocalDevice localDevice, int targetAddress,
69 IHdmiControlCallback callback) {
70 super(localDevice);
Jinsuk Kim78d695d2014-05-13 16:36:15 +090071 mTargetAddress = targetAddress;
Jinsuk Kimcb802872015-10-13 08:22:09 +090072 addCallback(callback);
Jinsuk Kim78d695d2014-05-13 16:36:15 +090073 }
74
75 @Override
76 boolean start() {
Jungshik Jang79c58a42014-06-16 16:45:36 +090077 sendCommand(HdmiCecMessageBuilder.buildTextViewOn(getSourceAddress(), mTargetAddress));
Jinsuk Kim78d695d2014-05-13 16:36:15 +090078 broadcastActiveSource();
79 queryDevicePowerStatus();
80 mState = STATE_WAITING_FOR_REPORT_POWER_STATUS;
Jinsuk Kim5fba96d2014-07-11 11:51:34 +090081 addTimer(mState, HdmiConfig.TIMEOUT_MS);
Jinsuk Kim78d695d2014-05-13 16:36:15 +090082 return true;
83 }
84
85 private void broadcastActiveSource() {
Amy848a9f22018-08-27 17:21:26 -070086 // Because only source device can create this action, it's safe to cast.
Amyf1baf562018-09-18 14:01:53 -070087 HdmiCecLocalDeviceSource source = source();
Amy123ec402018-09-25 10:56:31 -070088 source.mService.setAndBroadcastActiveSourceFromOneDeviceType(
89 mTargetAddress, getSourcePath());
Amyf1baf562018-09-18 14:01:53 -070090 // Set local active port to HOME when One Touch Play.
Amy03afe482018-09-18 16:57:45 -070091 // Active Port and Current Input are handled by the switch functionality device.
92 if (source.mService.audioSystem() != null) {
93 source = source.mService.audioSystem();
94 }
Amy51c6a632018-10-23 20:13:19 -070095 if (source.getLocalActivePort() != Constants.CEC_SWITCH_HOME) {
Amyec1dec72018-11-09 15:57:25 -080096 source.switchInputOnReceivingNewActivePath(getSourcePath());
Amy51c6a632018-10-23 20:13:19 -070097 }
Amy03afe482018-09-18 16:57:45 -070098 source.setRoutingPort(Constants.CEC_SWITCH_HOME);
Amyf1baf562018-09-18 14:01:53 -070099 source.setLocalActivePort(Constants.CEC_SWITCH_HOME);
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900100 }
101
102 private void queryDevicePowerStatus() {
Jungshik Jang79c58a42014-06-16 16:45:36 +0900103 sendCommand(HdmiCecMessageBuilder.buildGiveDevicePowerStatus(getSourceAddress(),
104 mTargetAddress));
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900105 }
106
107 @Override
108 boolean processCommand(HdmiCecMessage cmd) {
Jungshik Jang5352081c2014-09-22 15:14:49 +0900109 if (mState != STATE_WAITING_FOR_REPORT_POWER_STATUS
110 || mTargetAddress != cmd.getSource()) {
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900111 return false;
112 }
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900113 if (cmd.getOpcode() == Constants.MESSAGE_REPORT_POWER_STATUS) {
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900114 int status = cmd.getParams()[0];
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900115 if (status == HdmiControlManager.POWER_STATUS_ON) {
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900116 broadcastActiveSource();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900117 invokeCallback(HdmiControlManager.RESULT_SUCCESS);
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900118 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 Kim5fba96d2014-07-11 11:51:34 +0900133 addTimer(mState, HdmiConfig.TIMEOUT_MS);
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900134 } else {
135 // Couldn't wake up the TV for whatever reason. Report failure.
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900136 invokeCallback(HdmiControlManager.RESULT_TIMEOUT);
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900137 finish();
138 }
139 }
140 }
141
Jinsuk Kimcb802872015-10-13 08:22:09 +0900142 public void addCallback(IHdmiControlCallback callback) {
143 mCallbacks.add(callback);
144 }
145
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900146 private void invokeCallback(int result) {
147 try {
Jinsuk Kimcb802872015-10-13 08:22:09 +0900148 for (IHdmiControlCallback callback : mCallbacks) {
149 callback.onComplete(result);
150 }
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900151 } catch (RemoteException e) {
152 Slog.e(TAG, "Callback failed:" + e);
153 }
154 }
155}