HdmiCecController.java revision 0792d37385e60aa8d73f8df174d0a32f4f618bc4
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.os.Handler;
20import android.os.Looper;
21import android.os.Message;
22
23/**
24 * Manages HDMI-CEC command and behaviors. It converts user's command into CEC command
25 * and pass it to CEC HAL so that it sends message to other device. For incoming
26 * message it translates the message and delegates it to proper module.
27 *
28 * <p>It can be created only by {@link HdmiCecController#create}
29 *
30 * <p>Declared as package-private, accessed by {@link HdmiControlService} only.
31 */
32class HdmiCecController {
33    private static final String TAG = "HdmiCecController";
34
35    // Handler instance to process synchronous I/O (mainly send) message.
36    private Handler mIoHandler;
37
38    // Handler instance to process various messages coming from other CEC
39    // device or issued by internal state change.
40    private Handler mMessageHandler;
41
42    // Stores the pointer to the native implementation of the service that
43    // interacts with HAL.
44    private long mNativePtr;
45
46    // Private constructor.  Use HdmiCecController.create().
47    private HdmiCecController() {
48    }
49
50    /**
51     * A factory method to get {@link HdmiCecController}. If it fails to initialize
52     * inner device or has no device it will return {@code null}.
53     *
54     * <p>Declared as package-private, accessed by {@link HdmiControlService} only.
55     *
56     * @param ioLooper a Looper instance to handle IO (mainly send message) operation.
57     * @param messageHandler a message handler that processes a message coming from other
58     *                       CEC compatible device or callback of internal state change.
59     * @return {@link HdmiCecController} if device is initialized successfully. Otherwise,
60     *         returns {@code null}.
61     */
62    static HdmiCecController create(Looper ioLooper, Handler messageHandler) {
63        HdmiCecController handler = new HdmiCecController();
64        long nativePtr = nativeInit(handler);
65        if (nativePtr == 0L) {
66            handler = null;
67            return null;
68        }
69
70        handler.init(ioLooper, messageHandler, nativePtr);
71        return handler;
72    }
73
74    private void init(Looper ioLooper, Handler messageHandler, long nativePtr) {
75        mIoHandler = new Handler(ioLooper) {
76                @Override
77            public void handleMessage(Message msg) {
78                // TODO: Call native sendMessage.
79            }
80        };
81
82        mMessageHandler = messageHandler;
83        mNativePtr = nativePtr;
84    }
85
86    /**
87     * Called by native when an HDMI-CEC message arrived.
88     */
89    private void handleMessage(int srcAddress, int dstAddres, int opcode, byte[] params) {
90        // TODO: Translate message and delegate it to main message handler.
91    }
92
93    private static native long nativeInit(HdmiCecController handler);
94}
95