ActiveSourceHandler.java revision 72b7d738d5b9254594726304cdb1777b54d95631
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 */
16
17package com.android.server.hdmi;
18
19import android.annotation.Nullable;
20import android.hardware.hdmi.IHdmiControlCallback;
21import android.hardware.hdmi.HdmiCecDeviceInfo;
22import android.hardware.hdmi.HdmiControlManager;
23import android.os.RemoteException;
24import android.util.Slog;
25
26import com.android.server.hdmi.HdmiCecLocalDevice.ActiveSource;
27
28/**
29 * Handles CEC command <Active Source>.
30 * <p>
31 * Used by feature actions that need to handle the command in their flow. Only for TV
32 * local device.
33 */
34final class ActiveSourceHandler {
35    private static final String TAG = "ActiveSourceHandler";
36
37    private final HdmiCecLocalDeviceTv mSource;
38    private final HdmiControlService mService;
39    @Nullable
40    private final IHdmiControlCallback mCallback;
41
42    static ActiveSourceHandler create(HdmiCecLocalDeviceTv source, IHdmiControlCallback callback) {
43        if (source == null) {
44            Slog.e(TAG, "Wrong arguments");
45            return null;
46        }
47        return new ActiveSourceHandler(source, callback);
48    }
49
50    private ActiveSourceHandler(HdmiCecLocalDeviceTv source, IHdmiControlCallback callback) {
51        mSource = source;
52        mService = mSource.getService();
53        mCallback = callback;
54    }
55
56    /**
57     * Handles the incoming active source command.
58     *
59     * @param newActive new active source information
60     */
61    void process(ActiveSource newActive) {
62        // Seq #17
63        HdmiCecLocalDeviceTv tv = mSource;
64        ActiveSource activeSource = tv.getActiveSource();
65        if (activeSource.equals(newActive)) {
66            invokeCallback(HdmiControlManager.RESULT_SUCCESS);
67            return;
68        }
69        HdmiCecDeviceInfo device = mService.getDeviceInfo(newActive.logicalAddress);
70        if (device == null) {
71            tv.startNewDeviceAction(newActive);
72        }
73
74        ActiveSource current = tv.getActiveSource();
75        if (!tv.isProhibitMode()) {
76            tv.updateActiveSource(newActive);
77            if (!current.equals(newActive)) {
78                boolean notifyInputChange = (mCallback == null);
79                tv.updateActiveInput(newActive.physicalAddress, notifyInputChange);
80            }
81            invokeCallback(HdmiControlManager.RESULT_SUCCESS);
82        } else {
83            // TV is in a mode that should keep its current source/input from
84            // being changed for its operation. Reclaim the active source
85            // or switch the port back to the one used for the current mode.
86            if (current.logicalAddress == getSourceAddress()) {
87                HdmiCecMessage activeSourceCommand = HdmiCecMessageBuilder.buildActiveSource(
88                        current.logicalAddress, current.physicalAddress);
89                mService.sendCecCommand(activeSourceCommand);
90                tv.updateActiveSource(current);
91                invokeCallback(HdmiControlManager.RESULT_SUCCESS);
92            } else {
93                HdmiCecMessage routingChange = HdmiCecMessageBuilder.buildRoutingChange(
94                        getSourceAddress(), newActive.physicalAddress, current.physicalAddress);
95                mService.sendCecCommand(routingChange);
96                tv.addAndStartAction(
97                        new RoutingControlAction(tv, current.physicalAddress, true, mCallback));
98            }
99        }
100    }
101
102    private final int getSourceAddress() {
103        return mSource.getDeviceInfo().getLogicalAddress();
104    }
105
106    private final int getSourcePath() {
107        return mSource.getDeviceInfo().getPhysicalAddress();
108    }
109
110    private void invokeCallback(int result) {
111        if (mCallback == null) {
112            return;
113        }
114        try {
115            mCallback.onComplete(result);
116        } catch (RemoteException e) {
117            Slog.e(TAG, "Callback failed:" + e);
118        }
119    }
120}
121