Saturday, August 29, 2015

TvInputService Implementation : Creating TvInputInfo

TvInputService Implementation : Creating TvInputInfo 


As we know Android Tv Input Framework, a data source or Tv Input like IP-TV, Tuner, HDMI etc. is nothing else but the implementation of TvInputService.

TvInputManagerService provides the info of Hardware/HDMI devices available in the system, to all concrete classes of TvInputService by following callbcaks,

    @SystemApi
    public TvInputInfo onHardwareAdded(TvInputHardwareInfo hardwareInfo) {
        return null;
    }

    @SystemApi
    public TvInputInfo onHdmiDeviceAdded(HdmiDeviceInfo deviceInfo) {
        return null;
    }

While implementing TvInputService for any Hardware or HDMI-CEC, we need to override these methods carefuly, so a create correct TvInputInfo which will be returned back to TvInputManagerService for storing it.

TvInputInfo.java
private final String mId; // a string representation(example at end) 
private final String mParentId; //relevant in case of HDMI-CEC

private int mType = TYPE_TUNER;
private HdmiDeviceInfo mHdmiDeviceInfo; //relevant in case of HDMI-CEC

For creating TvInputInfo we will use one of the overloaded static methods createTvInputInfo::createTvInputInfo,

1. For Built-in Tuner, createTvInputInfo(Context context, ResolveInfo service)
2. For HDMI-CEC, createTvInputInfo(Context, ResolveInfo service, HdmiDeviceInfo, String parentId, label, iconUri)
3. For other hardware like HDMI-Input, createTvInputInfo(Context, ResolveInfo service, TvInputHardwareInfo, label, iconUri)

Here is an example of method implementation,
public class MyTvInputService extends TvInputService
{
    private final Context context;
    private final ResolveInfo service;

    public void onCreate( 
    {
context = getApplicationContext();
service = context.getPackageManager().
resolveService(new Intent(TvInputService.SERVICE_INTERFACE), PackageManager.GET_INTENT_FILTERS | PackageManager.GET_META_DATA);

    }

    public TvInputInfo onHardwareAdded(TvInputHardwareInfo hardwareInfo)
    {

        TvInputInfo info;
        try    
        {
        String lebel = new StringBuilder("HDMI").append(hardwareInfo.getDeviceId()).toString();
        info = TvInputInfo.createTvInputInfo(context, service, hardwareInfo, lebel, iconUri);
        } catch (Exception ex)   {        }
        return info;
    }

    public TvInputInfo onHdmiDeviceAdded(HdmiDeviceInfo deviceInfo) 
    {
        TvInputInfo info;
        try    
        {
String lebel = deviceInfo.getDisplayName();
info = TvInputInfo.createTvInputInfo(context, service, deviceInfo, parentId, lebel, iconUri);
} catch (Exception ex)   {        }
        return info;
    }
}

In case of onHdmiDeviceAdded, parentId is ID of this TV input's parent input, this is the Id of previously registered TvInput's, on same port-id as this HdmiDeviceInfo's port.

In "adb shell dumpsys tv_input" we can see all the TvInputs available in system,
For example,
Id will be "com.xyz.android.tv/.MyTvInputService/HW1" for 1st HW of MyTvInputService in package com.xyz.android.tv



No comments:

Post a Comment