Merge "CEC: Make port info unmodifiable" into lmp-dev
diff --git a/services/core/java/com/android/server/hdmi/HdmiControlService.java b/services/core/java/com/android/server/hdmi/HdmiControlService.java
index fe55b01..edadf6d 100644
--- a/services/core/java/com/android/server/hdmi/HdmiControlService.java
+++ b/services/core/java/com/android/server/hdmi/HdmiControlService.java
@@ -207,10 +207,10 @@
     private List<HdmiPortInfo> mPortInfo;
 
     // Map from path(physical address) to port ID.
-    private final SparseIntArray mPortIdMap = new SparseIntArray();
+    private UnmodifiableSparseIntArray mPortIdMap;
 
     // Map from port ID to HdmiPortInfo.
-    private final SparseArray<HdmiPortInfo> mPortInfoMap = new SparseArray<>();
+    private UnmodifiableSparseArray<HdmiPortInfo> mPortInfoMap;
 
     private HdmiCecMessageValidator mMessageValidator;
 
@@ -360,10 +360,14 @@
             return;
         }
 
+        SparseArray<HdmiPortInfo> portInfoMap = new SparseArray<>();
+        SparseIntArray portIdMap = new SparseIntArray();
         for (HdmiPortInfo info : cecPortInfo) {
-            mPortIdMap.put(info.getAddress(), info.getId());
-            mPortInfoMap.put(info.getId(), info);
+            portIdMap.put(info.getAddress(), info.getId());
+            portInfoMap.put(info.getId(), info);
         }
+        mPortIdMap = new UnmodifiableSparseIntArray(portIdMap);
+        mPortInfoMap = new UnmodifiableSparseArray<>(portInfoMap);
 
         if (mMhlController == null) {
             mPortInfo = Collections.unmodifiableList(Arrays.asList(cecPortInfo));
@@ -397,9 +401,7 @@
      * @param portId HDMI port id
      * @return {@link HdmiPortInfo} for the given port
      */
-    @ServiceThreadOnly
     HdmiPortInfo getPortInfo(int portId) {
-        assertRunOnServiceThread();
         return mPortInfoMap.get(portId, null);
     }
 
@@ -407,9 +409,7 @@
      * Returns the routing path (physical address) of the HDMI port for the given
      * port id.
      */
-    @ServiceThreadOnly
     int portIdToPath(int portId) {
-        assertRunOnServiceThread();
         HdmiPortInfo portInfo = getPortInfo(portId);
         if (portInfo == null) {
             Slog.e(TAG, "Cannot find the port info: " + portId);
@@ -424,16 +424,12 @@
      * the port id to be returned is the ID associated with the port address
      * 0x1000 (1.0.0.0) which is the topmost path of the given routing path.
      */
-    @ServiceThreadOnly
     int pathToPortId(int path) {
-        assertRunOnServiceThread();
         int portAddress = path & Constants.ROUTING_PATH_TOP_MASK;
         return mPortIdMap.get(portAddress, Constants.INVALID_PORT_ID);
     }
 
-    @ServiceThreadOnly
     boolean isValidPortId(int portId) {
-        assertRunOnServiceThread();
         return getPortInfo(portId) != null;
     }
 
@@ -490,9 +486,7 @@
     /**
      * Whether a device of the specified physical address is connected to ARC enabled port.
      */
-    @ServiceThreadOnly
     boolean isConnectedToArcPort(int physicalAddress) {
-        assertRunOnServiceThread();
         int portId = mPortIdMap.get(physicalAddress);
         if (portId != Constants.INVALID_PORT_ID) {
             return mPortInfoMap.get(portId).isArcSupported();
diff --git a/services/core/java/com/android/server/hdmi/UnmodifiableSparseArray.java b/services/core/java/com/android/server/hdmi/UnmodifiableSparseArray.java
new file mode 100644
index 0000000..5c0a360
--- /dev/null
+++ b/services/core/java/com/android/server/hdmi/UnmodifiableSparseArray.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.hdmi;
+
+import android.util.SparseArray;
+
+/**
+ * Unmodifiable version of {@link SparseArray}.
+ */
+final class UnmodifiableSparseArray<E> {
+    private static final String TAG = "ImmutableSparseArray";
+
+    private final SparseArray<E> mArray;
+
+    public UnmodifiableSparseArray(SparseArray<E> array) {
+       mArray = array;
+    }
+
+    public int size() {
+        return mArray.size();
+    }
+
+    public E get(int key) {
+        return mArray.get(key);
+    }
+
+    public E get(int key, E valueIfKeyNotFound) {
+        return mArray.get(key, valueIfKeyNotFound);
+    }
+
+    public int keyAt(int index) {
+        return mArray.keyAt(index);
+    }
+
+    public E valueAt(int index) {
+        return mArray.valueAt(index);
+    }
+
+    public int indexOfValue(E value) {
+        return mArray.indexOfValue(value);
+    }
+
+    @Override
+    public String toString() {
+        return mArray.toString();
+    }
+}
+
diff --git a/services/core/java/com/android/server/hdmi/UnmodifiableSparseIntArray.java b/services/core/java/com/android/server/hdmi/UnmodifiableSparseIntArray.java
new file mode 100644
index 0000000..cada855
--- /dev/null
+++ b/services/core/java/com/android/server/hdmi/UnmodifiableSparseIntArray.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.hdmi;
+
+import android.util.SparseIntArray;
+
+/**
+ * Unmodifiable version of {@link SparseIntArray}.
+ */
+final class UnmodifiableSparseIntArray {
+    private static final String TAG = "ImmutableSparseIntArray";
+
+    private final SparseIntArray mArray;
+
+    public UnmodifiableSparseIntArray(SparseIntArray array) {
+        mArray = array;
+    }
+
+    public int size() {
+        return mArray.size();
+    }
+
+    public int get(int key) {
+        return mArray.get(key);
+    }
+
+    public int get(int key, int valueIfKeyNotFound) {
+        return mArray.get(key, valueIfKeyNotFound);
+    }
+
+    public int keyAt(int index) {
+        return mArray.keyAt(index);
+    }
+
+    public int valueAt(int index) {
+        return mArray.valueAt(index);
+    }
+
+    public int indexOfValue(int value) {
+        return mArray.indexOfValue(value);
+    }
+
+    @Override
+    public String toString() {
+        return mArray.toString();
+    }
+}