ActiveSourceHandler.java revision bdf27fbf746bee11430c4db2ea6dfd026bae77fe
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.HdmiDeviceInfo;
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     * @param deviceType device type of the new active source
61     */
62    void process(ActiveSource newActive, int deviceType) {
63        // Seq #17
64        HdmiCecLocalDeviceTv tv = mSource;
65        ActiveSource activeSource = tv.getActiveSource();
66        if (activeSource.equals(newActive)) {
67            invokeCallback(HdmiControlManager.RESULT_SUCCESS);
68            return;
69        }
70        HdmiDeviceInfo device = mService.getDeviceInfo(newActive.logicalAddress);
71        if (device == null) {
72            tv.startNewDeviceAction(newActive, deviceType);
73        }
74
75        if (!tv.isProhibitMode()) {
76            tv.updateActiveSource(newActive);
77            boolean notifyInputChange = (mCallback == null);
78            tv.updateActiveInput(newActive.physicalAddress, notifyInputChange);
79            invokeCallback(HdmiControlManager.RESULT_SUCCESS);
80        } else {
81            // TV is in a mode that should keep its current source/input from
82            // being changed for its operation. Reclaim the active source
83            // or switch the port back to the one used for the current mode.
84            ActiveSource current = tv.getActiveSource();
85            if (current.logicalAddress == getSourceAddress()) {
86                HdmiCecMessage activeSourceCommand = HdmiCecMessageBuilder.buildActiveSource(
87                        current.logicalAddress, current.physicalAddress);
88                mService.sendCecCommand(activeSourceCommand);
89                tv.updateActiveSource(current);
90                invokeCallback(HdmiControlManager.RESULT_SUCCESS);
91            } else {
92                HdmiCecMessage routingChange = HdmiCecMessageBuilder.buildRoutingChange(
93                        getSourceAddress(), newActive.physicalAddress, current.physicalAddress);
94                mService.sendCecCommand(routingChange);
95                tv.addAndStartAction(
96                        new RoutingControlAction(tv, current.physicalAddress, true, mCallback));
97            }
98        }
99    }
100
101    private final int getSourceAddress() {
102        return mSource.getDeviceInfo().getLogicalAddress();
103    }
104
105    private void invokeCallback(int result) {
106        if (mCallback == null) {
107            return;
108        }
109        try {
110            mCallback.onComplete(result);
111        } catch (RemoteException e) {
112            Slog.e(TAG, "Callback failed:" + e);
113        }
114    }
115}
116