Add address flags and scope to LinkAddress.
This is necessary so that the framework can know whether an IPv6
address is likely to be usable (i.e., if it's global scope and
preferred). Also, it will simplify the address notification
methods in INetworkManagementEventObserver, which currently take
the address, the flags, and the scope as separate arguments.
1. Add flags and scope to the class and update the unit test.
Use the IFA_F_* and RT_SCOPE_* constants defined by libcore.
Since most callers don't know about flags and scope, provide
constructors that default the flags to zero and determine the
scope from the address. Addresses notified by the kernel will
have these properly set. Make multicast addresses invalid.
Update the class documentation.
2. Provide an isSameAddressAs() method that compares only the
address and prefix information between two LinkAddress
objects. This is necessary because an interface can't have
two addresses with the same address/prefix but different
flags.
3. Update LinkProperties's addLinkAddress and removeLinkAddress
to identify existing addresses to add/remove using
isSameAddressAs instead of implicit equals(). Specifically:
- If addLinkAddress is called with an address that is already
present, the existing address's flags and scope are updated.
This allows, for example, an address on an interface to go
from preferred to deprecated when it expires, without it
having to be removed and re-added.
- If removeLinkAddress is called with an address that is
present but with different flags, it deletes that address
instead of failing to find a match.
4. Update the INetworkManagementEventObserver address
notification methods to take just a LinkAddress instead of
LinkAddress, flags, and scope. While I'm at it, change the
order of the arguments for consistency with the other
functions in the interface.
Change-Id: Id8fe0f09a7e8f6bee1ea3b52102178b689a9336e
diff --git a/core/java/android/net/LinkProperties.java b/core/java/android/net/LinkProperties.java
index b4d07a1..4dfd3d9 100644
--- a/core/java/android/net/LinkProperties.java
+++ b/core/java/android/net/LinkProperties.java
@@ -61,10 +61,10 @@
public class LinkProperties implements Parcelable {
// The interface described by the network link.
private String mIfaceName;
- private Collection<LinkAddress> mLinkAddresses = new ArrayList<LinkAddress>();
- private Collection<InetAddress> mDnses = new ArrayList<InetAddress>();
+ private ArrayList<LinkAddress> mLinkAddresses = new ArrayList<LinkAddress>();
+ private ArrayList<InetAddress> mDnses = new ArrayList<InetAddress>();
private String mDomains;
- private Collection<RouteInfo> mRoutes = new ArrayList<RouteInfo>();
+ private ArrayList<RouteInfo> mRoutes = new ArrayList<RouteInfo>();
private ProxyProperties mHttpProxy;
private int mMtu;
@@ -156,28 +156,52 @@
return addresses;
}
- /**
- * 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;
+ private int findLinkAddressIndex(LinkAddress address) {
+ for (int i = 0; i < mLinkAddresses.size(); i++) {
+ if (mLinkAddresses.get(i).isSameAddressAs(address)) {
+ return i;
+ }
}
- return false;
+ return -1;
}
/**
- * Removes a link address.
- * @param address The {@code LinkAddress} to remove.
+ * Adds a link address if it does not exist, or updates it if it does.
+ * @param address The {@code LinkAddress} to add.
+ * @return true if {@code address} was added or updated, false otherwise.
+ */
+ public boolean addLinkAddress(LinkAddress address) {
+ if (address == null) {
+ return false;
+ }
+ int i = findLinkAddressIndex(address);
+ if (i < 0) {
+ // Address was not present. Add it.
+ mLinkAddresses.add(address);
+ return true;
+ } else if (mLinkAddresses.get(i).equals(address)) {
+ // Address was present and has same properties. Do nothing.
+ return false;
+ } else {
+ // Address was present and has different properties. Update it.
+ mLinkAddresses.set(i, address);
+ return true;
+ }
+ }
+
+ /**
+ * Removes a link address. Specifically, removes the link address, if any, for which
+ * {@code isSameAddressAs(toRemove)} returns true.
+ * @param address A {@code LinkAddress} specifying the address to remove.
* @return true if the address was removed, false if it did not exist.
*/
public boolean removeLinkAddress(LinkAddress toRemove) {
- return mLinkAddresses.remove(toRemove);
+ int i = findLinkAddressIndex(toRemove);
+ if (i >= 0) {
+ mLinkAddresses.remove(i);
+ return true;
+ }
+ return false;
}
/**