blob: 5c66316da60c9997a8012a1e09c7668133f6d38e [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
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.
Yuncheol Heo38db6292014-07-01 14:15:14 +090058 static OneTouchPlayAction create(HdmiCecLocalDevicePlayback 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() {
Jungshik Jang79c58a42014-06-16 16:45:36 +090086 sendCommand(HdmiCecMessageBuilder.buildActiveSource(getSourceAddress(), getSourcePath()));
Yuncheol Heo38db6292014-07-01 14:15:14 +090087 // Because only playback device can create this action, it's safe to cast.
Jinsuk Kime26d8332015-01-09 08:55:41 +090088 playback().setActiveSource(true);
Jinsuk Kim78d695d2014-05-13 16:36:15 +090089 }
90
91 private void queryDevicePowerStatus() {
Jungshik Jang79c58a42014-06-16 16:45:36 +090092 sendCommand(HdmiCecMessageBuilder.buildGiveDevicePowerStatus(getSourceAddress(),
93 mTargetAddress));
Jinsuk Kim78d695d2014-05-13 16:36:15 +090094 }
95
96 @Override
97 boolean processCommand(HdmiCecMessage cmd) {
Jungshik Jang5352081c2014-09-22 15:14:49 +090098 if (mState != STATE_WAITING_FOR_REPORT_POWER_STATUS
99 || mTargetAddress != cmd.getSource()) {
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900100 return false;
101 }
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900102 if (cmd.getOpcode() == Constants.MESSAGE_REPORT_POWER_STATUS) {
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900103 int status = cmd.getParams()[0];
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900104 if (status == HdmiControlManager.POWER_STATUS_ON) {
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900105 broadcastActiveSource();
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900106 invokeCallback(HdmiControlManager.RESULT_SUCCESS);
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900107 finish();
108 }
109 return true;
110 }
111 return false;
112 }
113
114 @Override
115 void handleTimerEvent(int state) {
116 if (mState != state) {
117 return;
118 }
119 if (state == STATE_WAITING_FOR_REPORT_POWER_STATUS) {
120 if (mPowerStatusCounter++ < LOOP_COUNTER_MAX) {
121 queryDevicePowerStatus();
Jinsuk Kim5fba96d2014-07-11 11:51:34 +0900122 addTimer(mState, HdmiConfig.TIMEOUT_MS);
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900123 } else {
124 // Couldn't wake up the TV for whatever reason. Report failure.
Jinsuk Kimc0c20d02014-07-04 14:34:31 +0900125 invokeCallback(HdmiControlManager.RESULT_TIMEOUT);
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900126 finish();
127 }
128 }
129 }
130
Jinsuk Kimcb802872015-10-13 08:22:09 +0900131 public void addCallback(IHdmiControlCallback callback) {
132 mCallbacks.add(callback);
133 }
134
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900135 private void invokeCallback(int result) {
136 try {
Jinsuk Kimcb802872015-10-13 08:22:09 +0900137 for (IHdmiControlCallback callback : mCallbacks) {
138 callback.onComplete(result);
139 }
Jinsuk Kim78d695d2014-05-13 16:36:15 +0900140 } catch (RemoteException e) {
141 Slog.e(TAG, "Callback failed:" + e);
142 }
143 }
144}