ActiveSourceHandler.java revision 8333571bd5e0a08773a1679964f8d96227af3356
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.HdmiCec;
22import android.hardware.hdmi.HdmiCecDeviceInfo;
23import android.hardware.hdmi.HdmiCecMessage;
24import android.os.RemoteException;
25import android.util.Slog;
26
27/**
28 * Handles CEC command <Active Source>.
29 * <p>
30 * Used by feature actions that need to handle the command in their flow. Only for TV
31 * local device.
32 */
33final class ActiveSourceHandler {
34    private static final String TAG = "ActiveSourceHandler";
35
36    private final HdmiCecLocalDeviceTv mSource;
37    private final HdmiControlService mService;
38    @Nullable
39    private final IHdmiControlCallback mCallback;
40
41    static ActiveSourceHandler create(HdmiCecLocalDeviceTv source, IHdmiControlCallback callback) {
42        if (source == null) {
43            Slog.e(TAG, "Wrong arguments");
44            return null;
45        }
46        return new ActiveSourceHandler(source, callback);
47    }
48
49    private ActiveSourceHandler(HdmiCecLocalDeviceTv source, IHdmiControlCallback callback) {
50        mSource = source;
51        mService = mSource.getService();
52        mCallback = callback;
53    }
54
55    /**
56     * Handles the incoming active source command.
57     *
58     * @param activeAddress logical address of the device to be the active source
59     * @param activePath routing path of the device to be the active source
60     */
61    void process(int activeAddress, int activePath) {
62        // Seq #17
63        HdmiCecLocalDeviceTv tv = mSource;
64        if (getSourcePath() == activePath && tv.getActiveSource() == getSourceAddress()) {
65            invokeCallback(HdmiCec.RESULT_SUCCESS);
66            return;
67        }
68        HdmiCecDeviceInfo device = mService.getDeviceInfo(activeAddress);
69        if (device == null) {
70            // "New device action" initiated by <Active Source> does not require
71            // "Routing change action".
72            tv.addAndStartAction(new NewDeviceAction(tv, activeAddress, activePath, false));
73        }
74
75        int currentActive = tv.getActiveSource();
76        int currentPath = tv.getActivePath();
77        if (!tv.isInPresetInstallationMode()) {
78            tv.updateActiveSource(activeAddress, activePath);
79            if (currentActive != activeAddress && currentPath != activePath) {
80                tv.updateActivePortId(mService.pathToPortId(activePath));
81            }
82            invokeCallback(HdmiCec.RESULT_SUCCESS);
83        } else {
84            // TV is in a mode that should keep its current source/input from
85            // being changed for its operation. Reclaim the active source
86            // or switch the port back to the one used for the current mode.
87            if (currentActive == getSourceAddress()) {
88                HdmiCecMessage activeSource =
89                        HdmiCecMessageBuilder.buildActiveSource(currentActive, currentPath);
90                mService.sendCecCommand(activeSource);
91                tv.updateActiveSource(currentActive, currentPath);
92                invokeCallback(HdmiCec.RESULT_SUCCESS);
93            } else {
94                HdmiCecMessage routingChange = HdmiCecMessageBuilder.buildRoutingChange(
95                        getSourceAddress(), activePath, currentPath);
96                mService.sendCecCommand(routingChange);
97                tv.addAndStartAction(new RoutingControlAction(tv, currentPath, 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