Return copies for get methods

NetworkRegistrationInfo and ServiceState are not
immutable class. Their get methods should return a new
copy of object so the caller won't modify its state.

Bug: 130538118
Test: Unit tests + manual

Merged-In: I51662a92b0b6189a8c8aa017085affedac417190
Change-Id: I51662a92b0b6189a8c8aa017085affedac417190
(cherry picked from commit 37538594e6)
This commit is contained in:
Jack Yu
2019-04-16 08:20:18 -07:00
parent dc4d14b37a
commit b79342f288
4 changed files with 98 additions and 33 deletions

View File

@@ -95,6 +95,21 @@ public final class DataSpecificRegistrationInfo implements Parcelable {
this.mIsUsingCarrierAggregation = isUsingCarrierAggregation;
}
/**
* Constructor from another data specific registration info
*
* @param dsri another data specific registration info
* @hide
*/
DataSpecificRegistrationInfo(DataSpecificRegistrationInfo dsri) {
maxDataCalls = dsri.maxDataCalls;
isDcNrRestricted = dsri.isDcNrRestricted;
isNrAvailable = dsri.isNrAvailable;
isEnDcAvailable = dsri.isEnDcAvailable;
mLteVopsSupportInfo = dsri.mLteVopsSupportInfo;
mIsUsingCarrierAggregation = dsri.mIsUsingCarrierAggregation;
}
private DataSpecificRegistrationInfo(Parcel source) {
maxDataCalls = source.readInt();
isDcNrRestricted = source.readBoolean();

View File

@@ -279,6 +279,39 @@ public final class NetworkRegistrationInfo implements Parcelable {
mNrState = source.readInt();
}
/**
* Constructor from another network registration info
*
* @param nri Another network registration info
* @hide
*/
public NetworkRegistrationInfo(NetworkRegistrationInfo nri) {
mDomain = nri.mDomain;
mTransportType = nri.mTransportType;
mRegistrationState = nri.mRegistrationState;
mRoamingType = nri.mRoamingType;
mAccessNetworkTechnology = nri.mAccessNetworkTechnology;
mRejectCause = nri.mRejectCause;
mEmergencyOnly = nri.mEmergencyOnly;
mAvailableServices = new ArrayList<>(nri.mAvailableServices);
if (nri.mCellIdentity != null) {
Parcel p = Parcel.obtain();
nri.mCellIdentity.writeToParcel(p, 0);
p.setDataPosition(0);
// TODO: Instead of doing this, we should create a formal way for cloning cell identity.
// Cell identity is not an immutable object so we have to deep copy it.
mCellIdentity = CellIdentity.CREATOR.createFromParcel(p);
}
if (nri.mVoiceSpecificInfo != null) {
mVoiceSpecificInfo = new VoiceSpecificRegistrationInfo(nri.mVoiceSpecificInfo);
}
if (nri.mDataSpecificInfo != null) {
mDataSpecificInfo = new DataSpecificRegistrationInfo(nri.mDataSpecificInfo);
}
mNrState = nri.mNrState;
}
/**
* @return The transport type.
*/

View File

@@ -418,7 +418,7 @@ public class ServiceState implements Parcelable {
Arrays.copyOf(s.mCellBandwidths, s.mCellBandwidths.length);
mLteEarfcnRsrpBoost = s.mLteEarfcnRsrpBoost;
mNetworkRegistrationInfos = s.mNetworkRegistrationInfos == null ? null :
new ArrayList<>(s.mNetworkRegistrationInfos);
s.getNetworkRegistrationInfoList();
mNrFrequencyRange = s.mNrFrequencyRange;
}
@@ -1113,16 +1113,16 @@ public class ServiceState implements Parcelable {
/** @hide */
@TestApi
public void setVoiceRoamingType(@RoamingType int type) {
NetworkRegistrationInfo regState = getNetworkRegistrationInfo(
NetworkRegistrationInfo regInfo = getNetworkRegistrationInfo(
NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
if (regState == null) {
regState = new NetworkRegistrationInfo.Builder()
if (regInfo == null) {
regInfo = new NetworkRegistrationInfo.Builder()
.setDomain(NetworkRegistrationInfo.DOMAIN_CS)
.setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
.build();
addNetworkRegistrationInfo(regState);
}
regState.setRoamingType(type);
regInfo.setRoamingType(type);
addNetworkRegistrationInfo(regInfo);
}
/** @hide */
@@ -1134,16 +1134,16 @@ public class ServiceState implements Parcelable {
/** @hide */
@TestApi
public void setDataRoamingType(@RoamingType int type) {
NetworkRegistrationInfo regState = getNetworkRegistrationInfo(
NetworkRegistrationInfo regInfo = getNetworkRegistrationInfo(
NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
if (regState == null) {
regState = new NetworkRegistrationInfo.Builder()
if (regInfo == null) {
regInfo = new NetworkRegistrationInfo.Builder()
.setDomain(NetworkRegistrationInfo.DOMAIN_PS)
.setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
.build();
addNetworkRegistrationInfo(regState);
}
regState.setRoamingType(type);
regInfo.setRoamingType(type);
addNetworkRegistrationInfo(regInfo);
}
/**
@@ -1305,16 +1305,16 @@ public class ServiceState implements Parcelable {
Rlog.e(LOG_TAG, "ServiceState.setRilVoiceRadioTechnology() called. It's encouraged to "
+ "use addNetworkRegistrationInfo() instead *******");
// Sync to network registration state
NetworkRegistrationInfo regState = getNetworkRegistrationInfo(
NetworkRegistrationInfo regInfo = getNetworkRegistrationInfo(
NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
if (regState == null) {
regState = new NetworkRegistrationInfo.Builder()
if (regInfo == null) {
regInfo = new NetworkRegistrationInfo.Builder()
.setDomain(NetworkRegistrationInfo.DOMAIN_CS)
.setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
.build();
addNetworkRegistrationInfo(regState);
}
regState.setAccessNetworkTechnology(rilRadioTechnologyToNetworkType(rt));
regInfo.setAccessNetworkTechnology(rilRadioTechnologyToNetworkType(rt));
addNetworkRegistrationInfo(regInfo);
}
@@ -1326,17 +1326,17 @@ public class ServiceState implements Parcelable {
// Sync to network registration state. Always write down the WWAN transport. For AP-assisted
// mode device, use addNetworkRegistrationInfo() to set the correct transport if RAT
// is IWLAN.
NetworkRegistrationInfo regState = getNetworkRegistrationInfo(
NetworkRegistrationInfo regInfo = getNetworkRegistrationInfo(
NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
if (regState == null) {
regState = new NetworkRegistrationInfo.Builder()
if (regInfo == null) {
regInfo = new NetworkRegistrationInfo.Builder()
.setDomain(NetworkRegistrationInfo.DOMAIN_PS)
.setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
.build();
addNetworkRegistrationInfo(regState);
}
regState.setAccessNetworkTechnology(rilRadioTechnologyToNetworkType(rt));
regInfo.setAccessNetworkTechnology(rilRadioTechnologyToNetworkType(rt));
addNetworkRegistrationInfo(regInfo);
}
/** @hide */
@@ -1378,10 +1378,10 @@ public class ServiceState implements Parcelable {
* @hide
*/
public @NRState int getNrState() {
final NetworkRegistrationInfo regState = getNetworkRegistrationInfo(
final NetworkRegistrationInfo regInfo = getNetworkRegistrationInfo(
NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
if (regState == null) return NetworkRegistrationInfo.NR_STATE_NONE;
return regState.getNrState();
if (regInfo == null) return NetworkRegistrationInfo.NR_STATE_NONE;
return regInfo.getNrState();
}
/**
@@ -1775,7 +1775,11 @@ public class ServiceState implements Parcelable {
@SystemApi
public List<NetworkRegistrationInfo> getNetworkRegistrationInfoList() {
synchronized (mNetworkRegistrationInfos) {
return new ArrayList<>(mNetworkRegistrationInfos);
List<NetworkRegistrationInfo> newList = new ArrayList<>();
for (NetworkRegistrationInfo nri : mNetworkRegistrationInfos) {
newList.add(new NetworkRegistrationInfo(nri));
}
return newList;
}
}
@@ -1795,7 +1799,7 @@ public class ServiceState implements Parcelable {
synchronized (mNetworkRegistrationInfos) {
for (NetworkRegistrationInfo networkRegistrationInfo : mNetworkRegistrationInfos) {
if (networkRegistrationInfo.getTransportType() == transportType) {
list.add(networkRegistrationInfo);
list.add(new NetworkRegistrationInfo(networkRegistrationInfo));
}
}
}
@@ -1819,7 +1823,7 @@ public class ServiceState implements Parcelable {
synchronized (mNetworkRegistrationInfos) {
for (NetworkRegistrationInfo networkRegistrationInfo : mNetworkRegistrationInfos) {
if (networkRegistrationInfo.getDomain() == domain) {
list.add(networkRegistrationInfo);
list.add(new NetworkRegistrationInfo(networkRegistrationInfo));
}
}
}
@@ -1844,7 +1848,7 @@ public class ServiceState implements Parcelable {
for (NetworkRegistrationInfo networkRegistrationInfo : mNetworkRegistrationInfos) {
if (networkRegistrationInfo.getTransportType() == transportType
&& networkRegistrationInfo.getDomain() == domain) {
return networkRegistrationInfo;
return new NetworkRegistrationInfo(networkRegistrationInfo);
}
}
}
@@ -1856,20 +1860,20 @@ public class ServiceState implements Parcelable {
* @hide
*/
@TestApi
public void addNetworkRegistrationInfo(NetworkRegistrationInfo regState) {
if (regState == null) return;
public void addNetworkRegistrationInfo(NetworkRegistrationInfo nri) {
if (nri == null) return;
synchronized (mNetworkRegistrationInfos) {
for (int i = 0; i < mNetworkRegistrationInfos.size(); i++) {
NetworkRegistrationInfo curRegState = mNetworkRegistrationInfos.get(i);
if (curRegState.getTransportType() == regState.getTransportType()
&& curRegState.getDomain() == regState.getDomain()) {
if (curRegState.getTransportType() == nri.getTransportType()
&& curRegState.getDomain() == nri.getDomain()) {
mNetworkRegistrationInfos.remove(i);
break;
}
}
mNetworkRegistrationInfos.add(regState);
mNetworkRegistrationInfos.add(new NetworkRegistrationInfo(nri));
}
}

View File

@@ -65,6 +65,19 @@ public class VoiceSpecificRegistrationInfo implements Parcelable{
this.defaultRoamingIndicator = defaultRoamingIndicator;
}
/**
* Constructor from another voice specific registration info
*
* @param vsri another voice specific registration info
* @hide
*/
VoiceSpecificRegistrationInfo(VoiceSpecificRegistrationInfo vsri) {
cssSupported = vsri.cssSupported;
roamingIndicator = vsri.roamingIndicator;
systemIsInPrl = vsri.systemIsInPrl;
defaultRoamingIndicator = vsri.defaultRoamingIndicator;
}
private VoiceSpecificRegistrationInfo(Parcel source) {
this.cssSupported = source.readBoolean();
this.roamingIndicator = source.readInt();