CEC: Make port info unmodifiable

Puts HDMI port information behind unmodifiable type. This helps API make use
of port-related information without having to switch to service thread.
See getActiveSource().

Added the unmodifiable version of SparseArray/SparseIntArray.

Change-Id: Idcf05d1d6e19b47c91a7e89a920abccc607a3eb5
This commit is contained in:
Jinsuk Kim
2014-08-05 17:30:09 +09:00
parent 58500f43ec
commit 30c74d9ba6
3 changed files with 131 additions and 14 deletions

View File

@@ -207,10 +207,10 @@ public final class HdmiControlService extends SystemService {
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 @@ public final class HdmiControlService extends SystemService {
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 @@ public final class HdmiControlService extends SystemService {
* @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 @@ public final class HdmiControlService extends SystemService {
* 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 @@ public final class HdmiControlService extends SystemService {
* 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 @@ public final class HdmiControlService extends SystemService {
/**
* 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();

View File

@@ -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();
}
}

View File

@@ -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();
}
}