Modify LinkProperties address update methods.
1. Make addLinkAddress a no-op if the address already exists. 2. Make addLinkAddress, addStackedLink and removeStackedLink return a boolean indicating whether something changed. 3. Add a removeLinkAddress method (currently there is no way of removing an address). 3. Move hasIPv6Address from ConnectivityService to LinkProperties, where it belongs. Bug: 9625448 Bug: 10232006 Change-Id: If641d0198432a7a505e358c059171f25bc9f13d5
This commit is contained in:
@@ -23,6 +23,7 @@ import android.text.TextUtils;
|
|||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
|
import java.net.Inet6Address;
|
||||||
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -153,8 +154,28 @@ public class LinkProperties implements Parcelable {
|
|||||||
return addresses;
|
return addresses;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addLinkAddress(LinkAddress address) {
|
/**
|
||||||
if (address != null) mLinkAddresses.add(address);
|
* Adds a link address if it does not exist, or update it if it does.
|
||||||
|
* @param address The {@code LinkAddress} to add.
|
||||||
|
* @return true if the address was added, false if it already existed.
|
||||||
|
*/
|
||||||
|
public boolean addLinkAddress(LinkAddress address) {
|
||||||
|
// TODO: when the LinkAddress has other attributes beyond the
|
||||||
|
// address and the prefix length, update them here.
|
||||||
|
if (address != null && !mLinkAddresses.contains(address)) {
|
||||||
|
mLinkAddresses.add(address);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a link address.
|
||||||
|
* @param address The {@code LinkAddress} to remove.
|
||||||
|
* @return true if the address was removed, false if it did not exist.
|
||||||
|
*/
|
||||||
|
public boolean removeLinkAddress(LinkAddress toRemove) {
|
||||||
|
return mLinkAddresses.remove(toRemove);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -245,11 +266,14 @@ public class LinkProperties implements Parcelable {
|
|||||||
* of stacked links. If link is null, nothing changes.
|
* of stacked links. If link is null, nothing changes.
|
||||||
*
|
*
|
||||||
* @param link The link to add.
|
* @param link The link to add.
|
||||||
|
* @return true if the link was stacked, false otherwise.
|
||||||
*/
|
*/
|
||||||
public void addStackedLink(LinkProperties link) {
|
public boolean addStackedLink(LinkProperties link) {
|
||||||
if (link != null && link.getInterfaceName() != null) {
|
if (link != null && link.getInterfaceName() != null) {
|
||||||
mStackedLinks.put(link.getInterfaceName(), link);
|
mStackedLinks.put(link.getInterfaceName(), link);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -258,12 +282,15 @@ public class LinkProperties implements Parcelable {
|
|||||||
* If there a stacked link with the same interfacename as link, it is
|
* If there a stacked link with the same interfacename as link, it is
|
||||||
* removed. Otherwise, nothing changes.
|
* removed. Otherwise, nothing changes.
|
||||||
*
|
*
|
||||||
* @param link The link to add.
|
* @param link The link to remove.
|
||||||
|
* @return true if the link was removed, false otherwise.
|
||||||
*/
|
*/
|
||||||
public void removeStackedLink(LinkProperties link) {
|
public boolean removeStackedLink(LinkProperties link) {
|
||||||
if (link != null && link.getInterfaceName() != null) {
|
if (link != null && link.getInterfaceName() != null) {
|
||||||
mStackedLinks.remove(link.getInterfaceName());
|
LinkProperties removed = mStackedLinks.remove(link.getInterfaceName());
|
||||||
|
return removed != null;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -339,6 +366,20 @@ public class LinkProperties implements Parcelable {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if this link has an IPv6 address.
|
||||||
|
*
|
||||||
|
* @return {@code true} if there is an IPv6 address, {@code false} otherwise.
|
||||||
|
*/
|
||||||
|
public boolean hasIPv6Address() {
|
||||||
|
for (LinkAddress address : mLinkAddresses) {
|
||||||
|
if (address.getAddress() instanceof Inet6Address) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compares this {@code LinkProperties} interface name against the target
|
* Compares this {@code LinkProperties} interface name against the target
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -306,5 +306,61 @@ public class LinkPropertiesTest extends TestCase {
|
|||||||
for (LinkProperties link : rmnet0.getStackedLinks()) {
|
for (LinkProperties link : rmnet0.getStackedLinks()) {
|
||||||
assertFalse("newname".equals(link.getInterfaceName()));
|
assertFalse("newname".equals(link.getInterfaceName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assertTrue(rmnet0.removeStackedLink(clat4));
|
||||||
|
assertEquals(0, rmnet0.getStackedLinks().size());
|
||||||
|
assertEquals(1, rmnet0.getAddresses().size());
|
||||||
|
assertEquals(1, rmnet0.getLinkAddresses().size());
|
||||||
|
assertEquals(1, rmnet0.getAllAddresses().size());
|
||||||
|
assertEquals(1, rmnet0.getAllLinkAddresses().size());
|
||||||
|
|
||||||
|
assertFalse(rmnet0.removeStackedLink(clat4));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SmallTest
|
||||||
|
public void testAddressMethods() {
|
||||||
|
LinkProperties lp = new LinkProperties();
|
||||||
|
|
||||||
|
// No addresses.
|
||||||
|
assertFalse(lp.hasIPv4Address());
|
||||||
|
assertFalse(lp.hasIPv6Address());
|
||||||
|
|
||||||
|
// Addresses on stacked links don't count.
|
||||||
|
LinkProperties stacked = new LinkProperties();
|
||||||
|
stacked.setInterfaceName("stacked");
|
||||||
|
lp.addStackedLink(stacked);
|
||||||
|
stacked.addLinkAddress(LINKADDRV4);
|
||||||
|
stacked.addLinkAddress(LINKADDRV6);
|
||||||
|
assertTrue(stacked.hasIPv4Address());
|
||||||
|
assertTrue(stacked.hasIPv6Address());
|
||||||
|
assertFalse(lp.hasIPv4Address());
|
||||||
|
assertFalse(lp.hasIPv6Address());
|
||||||
|
lp.removeStackedLink(stacked);
|
||||||
|
assertFalse(lp.hasIPv4Address());
|
||||||
|
assertFalse(lp.hasIPv6Address());
|
||||||
|
|
||||||
|
// Addresses on the base link.
|
||||||
|
// Check the return values of hasIPvXAddress and ensure the add/remove methods return true
|
||||||
|
// iff something changes.
|
||||||
|
assertTrue(lp.addLinkAddress(LINKADDRV6));
|
||||||
|
assertFalse(lp.hasIPv4Address());
|
||||||
|
assertTrue(lp.hasIPv6Address());
|
||||||
|
|
||||||
|
assertTrue(lp.removeLinkAddress(LINKADDRV6));
|
||||||
|
assertTrue(lp.addLinkAddress(LINKADDRV4));
|
||||||
|
assertTrue(lp.hasIPv4Address());
|
||||||
|
assertFalse(lp.hasIPv6Address());
|
||||||
|
|
||||||
|
assertTrue(lp.addLinkAddress(LINKADDRV6));
|
||||||
|
assertTrue(lp.hasIPv4Address());
|
||||||
|
assertTrue(lp.hasIPv6Address());
|
||||||
|
|
||||||
|
// Adding an address twice has no effect.
|
||||||
|
// Removing an address that's not present has no effect.
|
||||||
|
assertFalse(lp.addLinkAddress(LINKADDRV4));
|
||||||
|
assertTrue(lp.hasIPv4Address());
|
||||||
|
assertTrue(lp.removeLinkAddress(LINKADDRV4));
|
||||||
|
assertFalse(lp.hasIPv4Address());
|
||||||
|
assertFalse(lp.removeLinkAddress(LINKADDRV4));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3982,8 +3982,8 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
// Get the type of addresses supported by this link
|
// Get the type of addresses supported by this link
|
||||||
LinkProperties lp = mCs.getLinkProperties(
|
LinkProperties lp = mCs.getLinkProperties(
|
||||||
ConnectivityManager.TYPE_MOBILE_HIPRI);
|
ConnectivityManager.TYPE_MOBILE_HIPRI);
|
||||||
boolean linkHasIpv4 = hasIPv4Address(lp);
|
boolean linkHasIpv4 = lp.hasIPv4Address();
|
||||||
boolean linkHasIpv6 = hasIPv6Address(lp);
|
boolean linkHasIpv6 = lp.hasIPv6Address();
|
||||||
log("isMobileOk: linkHasIpv4=" + linkHasIpv4
|
log("isMobileOk: linkHasIpv4=" + linkHasIpv4
|
||||||
+ " linkHasIpv6=" + linkHasIpv6);
|
+ " linkHasIpv6=" + linkHasIpv6);
|
||||||
|
|
||||||
@@ -4129,20 +4129,6 @@ public class ConnectivityService extends IConnectivityManager.Stub {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasIPv4Address(LinkProperties lp) {
|
|
||||||
return lp.hasIPv4Address();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not implemented in LinkProperties, do it here.
|
|
||||||
public boolean hasIPv6Address(LinkProperties lp) {
|
|
||||||
for (LinkAddress address : lp.getLinkAddresses()) {
|
|
||||||
if (address.getAddress() instanceof Inet6Address) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void log(String s) {
|
private void log(String s) {
|
||||||
Slog.d(ConnectivityService.TAG, "[" + CHECKMP_TAG + "] " + s);
|
Slog.d(ConnectivityService.TAG, "[" + CHECKMP_TAG + "] " + s);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user