ActiveSourceHandler.java revision 7c3a95633d307c4be30c9dbbf1071063aa7a3c64
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        if (!tv.isProhibitMode()) {
75            tv.updateActiveSource(newActive);
76            boolean notifyInputChange = (mCallback == null);
77            tv.updateActiveInput(newActive.physicalAddress, notifyInputChange);
78            invokeCallback(HdmiControlManager.RESULT_SUCCESS);
79        } else {
80            // TV is in a mode that should keep its current source/input from
81            // being changed for its operation. Reclaim the active source
82            // or switch the port back to the one used for the current mode.
83            ActiveSource current = tv.getActiveSource();
84            if (current.logicalAddress == getSourceAddress()) {
85                HdmiCecMessage activeSourceCommand = HdmiCecMessageBuilder.buildActiveSource(
86                        current.logicalAddress, current.physicalAddress);
87                mService.sendCecCommand(activeSourceCommand);
88                tv.updateActiveSource(current);
89                invokeCallback(HdmiControlManager.RESULT_SUCCESS);
90            } else {
91                HdmiCecMessage routingChange = HdmiCecMessageBuilder.buildRoutingChange(
92                        getSourceAddress(), newActive.physicalAddress, current.physicalAddress);
93                mService.sendCecCommand(routingChange);
94                tv.addAndStartAction(
95                        new RoutingControlAction(tv, current.physicalAddress, true, mCallback));
96            }
97        }
98    }
99
100    private final int getSourceAddress() {
101        return mSource.getDeviceInfo().getLogicalAddress();
102    }
103
104    private final int getSourcePath() {
105        return mSource.getDeviceInfo().getPhysicalAddress();
106    }
107
108    private void invokeCallback(int result) {
109        if (mCallback == null) {
110            return;
111        }
112        try {
113            mCallback.onComplete(result);
114        } catch (RemoteException e) {
115            Slog.e(TAG, "Callback failed:" + e);
116        }
117    }
118}
119