OneTouchPlayAction.java revision 38db629d897e9d7c8e31ce0a7e985981e3e12996
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.HdmiCec;
19import android.hardware.hdmi.HdmiCecMessage;
20import android.hardware.hdmi.HdmiControlManager;
21import android.hardware.hdmi.IHdmiControlCallback;
22import android.os.RemoteException;
23import android.util.Slog;
24
25/**
26 * Feature action that performs one touch play against TV/Display device.
27 *
28 * This action is initiated via {@link HdmiControlManager#oneTouchPlay()} from
29 * the Android system working as playback device to turn on the TV, and switch the input.
30 *
31 * <p>Package-private, accessed by {@link HdmiControlService} only.
32 */
33
34final class OneTouchPlayAction extends FeatureAction {
35    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
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(HdmiCecLocalDevicePlayback source,
59            int targetAddress, IHdmiControlCallback callback) {
60        if (source == null || callback == null) {
61            Slog.e(TAG, "Wrong arguments");
62            return null;
63        }
64        return new OneTouchPlayAction(source, targetAddress,
65                callback);
66    }
67
68    private OneTouchPlayAction(HdmiCecLocalDevice localDevice, int targetAddress,
69            IHdmiControlCallback callback) {
70        super(localDevice);
71        mTargetAddress = targetAddress;
72        mCallback = callback;
73    }
74
75    @Override
76    boolean start() {
77        sendCommand(HdmiCecMessageBuilder.buildTextViewOn(getSourceAddress(), mTargetAddress));
78        broadcastActiveSource();
79        queryDevicePowerStatus();
80        mState = STATE_WAITING_FOR_REPORT_POWER_STATUS;
81        addTimer(mState, FeatureAction.TIMEOUT_MS);
82        return true;
83    }
84
85    private void broadcastActiveSource() {
86        sendCommand(HdmiCecMessageBuilder.buildActiveSource(getSourceAddress(), getSourcePath()));
87        // Because only playback device can create this action, it's safe to cast.
88        playback().markActiveSource();
89    }
90
91    private void queryDevicePowerStatus() {
92        sendCommand(HdmiCecMessageBuilder.buildGiveDevicePowerStatus(getSourceAddress(),
93                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