Implement add/remove device info api for Hdmi Cec device.

In order to manage info of all cec devices connected hdmi bus,
HdmiCecController should have data structure for them.
This change includes two major pieces.

1. HdmiCecDeviceInfo
 It's data structure containing basic device information such as
 logical address, physicall address, device type and vendor id.
 It will not be available to thirdparty but some system component
 like TIF needs this to update device information connected to
 its hdmi ports.

2. Managing device list in HdmiCecController.
 HdmiCecController is a host to manage all CEC device.
 and we need to have logic add or remove as well as get it.
 All cec devices are managed as sparsearray which uses logical address as key.

This change introduces internal api and the later change will have logic
to call these apis.

Change-Id: Idc2f189ac0bffe904e011ced0ac991f16da07db1
diff --git a/services/core/java/com/android/server/hdmi/HdmiCecController.java b/services/core/java/com/android/server/hdmi/HdmiCecController.java
index 8d4341d..f42c212 100644
--- a/services/core/java/com/android/server/hdmi/HdmiCecController.java
+++ b/services/core/java/com/android/server/hdmi/HdmiCecController.java
@@ -17,13 +17,17 @@
 package com.android.server.hdmi;
 
 import android.hardware.hdmi.HdmiCec;
+import android.hardware.hdmi.HdmiCecDeviceInfo;
 import android.hardware.hdmi.HdmiCecMessage;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
 import android.util.Slog;
+import android.util.SparseArray;
 
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 
 /**
  * Manages HDMI-CEC command and behaviors. It converts user's command into CEC command
@@ -63,6 +67,11 @@
     // interacts with HAL.
     private long mNativePtr;
 
+    // Map-like container of all cec devices. A logical address of device is
+    // used as key of container.
+    private final SparseArray<HdmiCecDeviceInfo> mDeviceInfos =
+            new SparseArray<HdmiCecDeviceInfo>();
+
     // Private constructor.  Use HdmiCecController.create().
     private HdmiCecController() {
     }
@@ -136,6 +145,69 @@
         }
     }
 
+    /**
+     * Add a new {@link HdmiCecDeviceInfo}. It returns old device info which has the same
+     * logical address as new device info's.
+     *
+     * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
+     *
+     * @param deviceInfo a new {@link HdmiCecDeviceInfo} to be added.
+     * @return {@code null} if it is new device. Otherwise, returns old {@HdmiCecDeviceInfo}
+     *         that has the same logical address as new one has.
+     */
+    HdmiCecDeviceInfo addDeviceInfo(HdmiCecDeviceInfo deviceInfo) {
+        HdmiCecDeviceInfo oldDeviceInfo = getDeviceInfo(deviceInfo.getLogicalAddress());
+        if (oldDeviceInfo != null) {
+            removeDeviceInfo(deviceInfo.getLogicalAddress());
+        }
+        mDeviceInfos.append(deviceInfo.getLogicalAddress(), deviceInfo);
+        return oldDeviceInfo;
+    }
+
+    /**
+     * Remove a device info corresponding to the given {@code logicalAddress}.
+     * It returns removed {@link HdmiCecDeviceInfo} if exists.
+     *
+     * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
+     *
+     * @param logicalAddress logical address of device to be removed
+     * @return removed {@link HdmiCecDeviceInfo} it exists. Otherwise, returns {@code null}
+     */
+    HdmiCecDeviceInfo removeDeviceInfo(int logicalAddress) {
+        HdmiCecDeviceInfo deviceInfo = mDeviceInfos.get(logicalAddress);
+        if (deviceInfo != null) {
+            mDeviceInfos.remove(logicalAddress);
+        }
+        return deviceInfo;
+    }
+
+    /**
+     * Return a list of all {@HdmiCecDeviceInfo}.
+     *
+     * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
+     */
+    List<HdmiCecDeviceInfo> getDeviceInfoList() {
+        List<HdmiCecDeviceInfo> deviceInfoList = new ArrayList<HdmiCecDeviceInfo>(
+                mDeviceInfos.size());
+        for (int i = 0; i < mDeviceInfos.size(); ++i) {
+            deviceInfoList.add(mDeviceInfos.valueAt(i));
+        }
+        return deviceInfoList;
+    }
+
+    /**
+     * Return a {@link HdmiCecDeviceInfo} corresponding to the given {@code logicalAddress}.
+     *
+     * <p>Declared as package-private. accessed by {@link HdmiControlService} only.
+     *
+     * @param logicalAddress logical address to be retrieved
+     * @return {@link HdmiCecDeviceInfo} matched with the given {@code logicalAddress}.
+     *         Returns null if no logical address matched
+     */
+    HdmiCecDeviceInfo getDeviceInfo(int logicalAddress) {
+        return mDeviceInfos.get(logicalAddress);
+    }
+
     private void init(HdmiControlService service, long nativePtr) {
         mIoHandler = new IoHandler(service.getServiceLooper());
         mControlHandler = new ControlHandler(service.getServiceLooper());