本文介绍Android 5.1和Android6.0平台下,框架层实现盒子与支持CEC的电视控制和交互逻辑:
(1)盒子待机,控制电视待机
(2)电视待机,控制盒子待机
(3)盒子唤醒,控制电视唤醒
(4)获取盒子,电视的电源状态
其中(2)Android框架层已经实现
关于HDMI-CEC的介绍,请移步https://source.android.com/devices/tv/hdmi-cec
HDMI-CEC框架层控制部分别在两个部分:
frameworks/base/core/java/android/hardware/hdmi
frameworks/base/services/core/java/com/android/server/hdmi
1.修改盒子名称在电视显示的名字
diff --git a/core/java/android/hardware/hdmi/HdmiDeviceInfo.java b/core/java/android/hardware/hdmi/HdmiDeviceInfo.java
index 48ea9a6..a921ebb 100644
--- a/core/java/android/hardware/hdmi/HdmiDeviceInfo.java
+++ b/core/java/android/hardware/hdmi/HdmiDeviceInfo.java
@@ -19,6 +19,7 @@ package android.hardware.hdmi;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;
+import android.os.SystemProperties;
/**
* A class to encapsulate device information for HDMI devices including CEC and MHL. In terms of
@@ -407,7 +408,10 @@ public class HdmiDeviceInfo implements Parcelable {
* Returns display (OSD) name of the device.
*/
public String getDisplayName() {
- return mDisplayName;
+
+ mDisplayName = SystemProperties.get("persist.sys.exdevicename", "box");
+ return mDisplayName;
}
2.在HdmiControlManager.java添加广播动作,主要用于发送广播获取电视,盒子的待机状态,第三方应用可以通过接受广播获取盒子和电视的待机状态
diff --git a/core/java/android/hardware/hdmi/HdmiControlManager.java b/core/java/android/hardware/hdmi/HdmiControlManager.java
old mode 100644
new mode 100755
index 308a219..4043e2e
--- a/core/java/android/hardware/hdmi/HdmiControlManager.java
+++ b/core/java/android/hardware/hdmi/HdmiControlManager.java
@@ -254,6 +254,16 @@ public final class HdmiControlManager {
// True if we have a logical device of type TV hosted in the system.
private final boolean mHasTvDevice;
+ //Broadcast action for power status changed
+ public static final String POWER_STATUS_CHANGED = "com.rockchip.hdmicec.power_status_changed";
+ //The key for box power status
+ public static final String HDMI_CEC_POWER_STATUS = "hdmi_cec_power_status";
+ //The key for hdmi cec device type
+ public static final String HDMI_CEC_DEVICE_TYPE = "hdmi_cec_device_type";
+ //box type
+ public static final int TYPE_HDMI_CEC_BOX = 0;
+ //tv type
+ public static final int TYPE_HDMI_CEC_TV= 1;
3.IHdmiControlService.aidl添加发送CEC命令,获取电视或盒子的逻辑地址,获取盒子电源状态等接口,这些接口在service端的HdmiControlService.java文件实现,第三方应用可以通过Binder机制去发送CEC控制命令,获取盒子电源状态等
diff --git a/core/java/android/hardware/hdmi/IHdmiControlService.aidl b/core/java/android/hardware/hdmi/IHdmiControlService.aidl
old mode 100644
new mode 100755
index c1e924e..62dea8a
--- a/core/java/android/hardware/hdmi/IHdmiControlService.aidl
+++ b/core/java/android/hardware/hdmi/IHdmiControlService.aidl
@@ -71,4 +71,10 @@ interface IHdmiControlService {
void clearTimerRecording(int recorderAddress, int sourceType, in byte[] recordSource);
void sendMhlVendorCommand(int portId, int offset, int length, in byte[] data);
void addHdmiMhlVendorCommandListener(IHdmiMhlVendorCommandListener listener);
+ //send cec command
+ void sendControlCommand(int src, int targetAddress, int opCode, in byte[] params);
+ //get local address for specific type
+ int getLocalAddress(int type);
+ //get power status
+ int getPowerStatus();
}
4.在HdmiCecLocalDevice.java广播电视的待机状态,激活盒子作为输入源,控制盒子待机
diff --git a/services/core/java/com/android/server/hdmi/HdmiCecLocalDevice.java b/services/core/java/com/android/server/hdmi/HdmiCecLocalDevice.java
old mode 100644
new mode 100755
index 7999321..e66d0d1
--- a/services/core/java/com/android/server/hdmi/HdmiCecLocalDevice.java
+++ b/services/core/java/com/android/server/hdmi/HdmiCecLocalDevice.java
@@ -22,7 +22,9 @@ import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
+import android.os.SystemProperties;
import android.util.Slog;
+import android.util.L