forked from upstream-mirrors/quik
move old ReceiveSms interactor code to receive sms worker
move old ReceiveMms interactor code to receive mms worker move mms processing code from mmsreceivedreceiver to receive mms worker fix bug of non-unique pending intent for mms ack/notify send removed some unused android-smsmms files and code that was causing build issues
This commit is contained in:
@@ -1,168 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2010 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 android.net;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import timber.log.Timber; import android.util.Log; import static com.klinker.android.timberworkarounds.TimberExtensionsKt.Timber_isLoggable; // inserted with sed
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* A simple object for retrieving the results of a DHCP request.
|
||||
* Replaces (internally) the IPv4-only DhcpInfo class.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public class DhcpInfoInternal {
|
||||
private final static String TAG = "DhcpInfoInternal";
|
||||
public String ipAddress;
|
||||
public int prefixLength;
|
||||
|
||||
public String dns1;
|
||||
public String dns2;
|
||||
|
||||
public String serverAddress;
|
||||
public int leaseDuration;
|
||||
|
||||
/**
|
||||
* Vendor specific information (from RFC 2132).
|
||||
*/
|
||||
public String vendorInfo;
|
||||
|
||||
private Collection<RouteInfo> mRoutes;
|
||||
|
||||
public DhcpInfoInternal() {
|
||||
mRoutes = new ArrayList<RouteInfo>();
|
||||
}
|
||||
|
||||
public void addRoute(RouteInfo routeInfo) {
|
||||
mRoutes.add(routeInfo);
|
||||
}
|
||||
|
||||
public Collection<RouteInfo> getRoutes() {
|
||||
return Collections.unmodifiableCollection(mRoutes);
|
||||
}
|
||||
|
||||
private int convertToInt(String addr) {
|
||||
if (addr != null) {
|
||||
try {
|
||||
InetAddress inetAddress = NetworkUtilsHelper.numericToInetAddress(addr);
|
||||
if (inetAddress instanceof Inet4Address) {
|
||||
return NetworkUtilsHelper.inetAddressToInt(inetAddress);
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public DhcpInfo makeDhcpInfo() {
|
||||
DhcpInfo info = new DhcpInfo();
|
||||
info.ipAddress = convertToInt(ipAddress);
|
||||
for (RouteInfo route : mRoutes) {
|
||||
if (route.isDefaultRoute()) {
|
||||
info.gateway = convertToInt(route.getGateway().getHostAddress());
|
||||
break;
|
||||
}
|
||||
}
|
||||
try {
|
||||
info.netmask = NetworkUtilsHelper.prefixLengthToNetmaskInt(prefixLength);
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
info.dns1 = convertToInt(dns1);
|
||||
info.dns2 = convertToInt(dns2);
|
||||
info.serverAddress = convertToInt(serverAddress);
|
||||
info.leaseDuration = leaseDuration;
|
||||
return info;
|
||||
}
|
||||
|
||||
public LinkAddress makeLinkAddress() {
|
||||
if (TextUtils.isEmpty(ipAddress)) {
|
||||
Log.e(TAG, "makeLinkAddress with empty ipAddress");
|
||||
return null;
|
||||
}
|
||||
return new LinkAddress(NetworkUtilsHelper.numericToInetAddress(ipAddress), prefixLength);
|
||||
}
|
||||
|
||||
public LinkProperties makeLinkProperties() {
|
||||
LinkProperties p = new LinkProperties();
|
||||
p.addLinkAddress(makeLinkAddress());
|
||||
for (RouteInfo route : mRoutes) {
|
||||
p.addRoute(route);
|
||||
}
|
||||
//if empty, connectivity configures default DNS
|
||||
if (TextUtils.isEmpty(dns1) == false) {
|
||||
p.addDns(NetworkUtilsHelper.numericToInetAddress(dns1));
|
||||
} else {
|
||||
Log.d(TAG, "makeLinkProperties with empty dns1!");
|
||||
}
|
||||
if (TextUtils.isEmpty(dns2) == false) {
|
||||
p.addDns(NetworkUtilsHelper.numericToInetAddress(dns2));
|
||||
} else {
|
||||
Log.d(TAG, "makeLinkProperties with empty dns2!");
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
/* Updates the DHCP fields that need to be retained from
|
||||
* original DHCP request if the DHCP renewal shows them as
|
||||
* being empty
|
||||
*/
|
||||
public void updateFromDhcpRequest(DhcpInfoInternal orig) {
|
||||
if (orig == null) return;
|
||||
|
||||
if (TextUtils.isEmpty(dns1)) {
|
||||
dns1 = orig.dns1;
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(dns2)) {
|
||||
dns2 = orig.dns2;
|
||||
}
|
||||
|
||||
if (mRoutes.size() == 0) {
|
||||
for (RouteInfo route : orig.getRoutes()) {
|
||||
addRoute(route);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if this DHCP lease includes vendor hint that network link is
|
||||
* metered, and sensitive to heavy data transfers.
|
||||
*/
|
||||
public boolean hasMeteredHint() {
|
||||
if (vendorInfo != null) {
|
||||
return vendorInfo.contains("ANDROID_METERED");
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
String routeString = "";
|
||||
for (RouteInfo route : mRoutes) routeString += route.toString() + " | ";
|
||||
return "addr: " + ipAddress + "/" + prefixLength +
|
||||
" mRoutes: " + routeString +
|
||||
" dns: " + dns1 + "," + dns2 +
|
||||
" dhcpServer: " + serverAddress +
|
||||
" leaseDuration: " + leaseDuration;
|
||||
}
|
||||
}
|
||||
@@ -58,223 +58,6 @@ public class NetworkUtilsHelper {
|
||||
*/
|
||||
public static final int RESET_ALL_ADDRESSES = RESET_IPV4_ADDRESSES | RESET_IPV6_ADDRESSES;
|
||||
|
||||
/**
|
||||
* Reset IPv6 or IPv4 sockets that are connected via the named interface.
|
||||
*
|
||||
* @param interfaceName is the interface to reset
|
||||
* @param mask {@see #RESET_IPV4_ADDRESSES} and {@see #RESET_IPV6_ADDRESSES}
|
||||
*/
|
||||
public native static int resetConnections(String interfaceName, int mask);
|
||||
|
||||
/**
|
||||
* Start the DHCP client daemon, in order to have it request addresses
|
||||
* for the named interface, and then configure the interface with those
|
||||
* addresses. This call blocks until it obtains a result (either success
|
||||
* or failure) from the daemon.
|
||||
*
|
||||
* @param interfaceName the name of the interface to configure
|
||||
* @param ipInfo if the request succeeds, this object is filled in with
|
||||
* the IP address information.
|
||||
* @return {@code true} for success, {@code false} for failure
|
||||
*/
|
||||
public native static boolean runDhcp(String interfaceName, DhcpInfoInternal ipInfo);
|
||||
|
||||
/**
|
||||
* Initiate renewal on the Dhcp client daemon. This call blocks until it obtains
|
||||
* a result (either success or failure) from the daemon.
|
||||
*
|
||||
* @param interfaceName the name of the interface to configure
|
||||
* @param ipInfo if the request succeeds, this object is filled in with
|
||||
* the IP address information.
|
||||
* @return {@code true} for success, {@code false} for failure
|
||||
*/
|
||||
public native static boolean runDhcpRenew(String interfaceName, DhcpInfoInternal ipInfo);
|
||||
|
||||
/**
|
||||
* Shut down the DHCP client daemon.
|
||||
*
|
||||
* @param interfaceName the name of the interface for which the daemon
|
||||
* should be stopped
|
||||
* @return {@code true} for success, {@code false} for failure
|
||||
*/
|
||||
public native static boolean stopDhcp(String interfaceName);
|
||||
|
||||
/**
|
||||
* Release the current DHCP lease.
|
||||
*
|
||||
* @param interfaceName the name of the interface for which the lease should
|
||||
* be released
|
||||
* @return {@code true} for success, {@code false} for failure
|
||||
*/
|
||||
public native static boolean releaseDhcpLease(String interfaceName);
|
||||
|
||||
/**
|
||||
* Return the last DHCP-related error message that was recorded.
|
||||
* <p/>NOTE: This string is not localized, but currently it is only
|
||||
* used in logging.
|
||||
*
|
||||
* @return the most recent error message, if any
|
||||
*/
|
||||
public native static String getDhcpError();
|
||||
|
||||
/**
|
||||
* Convert a IPv4 address from an integer to an InetAddress.
|
||||
*
|
||||
* @param hostAddress an int corresponding to the IPv4 address in network byte order
|
||||
*/
|
||||
public static InetAddress intToInetAddress(int hostAddress) {
|
||||
byte[] addressBytes = {(byte) (0xff & hostAddress),
|
||||
(byte) (0xff & (hostAddress >> 8)),
|
||||
(byte) (0xff & (hostAddress >> 16)),
|
||||
(byte) (0xff & (hostAddress >> 24))};
|
||||
|
||||
try {
|
||||
return InetAddress.getByAddress(addressBytes);
|
||||
} catch (UnknownHostException e) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a IPv4 address from an InetAddress to an integer
|
||||
*
|
||||
* @param inetAddr is an InetAddress corresponding to the IPv4 address
|
||||
* @return the IP address as an integer in network byte order
|
||||
*/
|
||||
public static int inetAddressToInt(InetAddress inetAddr)
|
||||
throws IllegalArgumentException {
|
||||
byte[] addr = inetAddr.getAddress();
|
||||
if (addr.length != 4) {
|
||||
throw new IllegalArgumentException("Not an IPv4 address");
|
||||
}
|
||||
return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) |
|
||||
((addr[1] & 0xff) << 8) | (addr[0] & 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a network prefix length to an IPv4 netmask integer
|
||||
*
|
||||
* @param prefixLength
|
||||
* @return the IPv4 netmask as an integer in network byte order
|
||||
*/
|
||||
public static int prefixLengthToNetmaskInt(int prefixLength)
|
||||
throws IllegalArgumentException {
|
||||
if (prefixLength < 0 || prefixLength > 32) {
|
||||
throw new IllegalArgumentException("Invalid prefix length (0 <= prefix <= 32)");
|
||||
}
|
||||
int value = 0xffffffff << (32 - prefixLength);
|
||||
return Integer.reverseBytes(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a IPv4 netmask integer to a prefix length
|
||||
*
|
||||
* @param netmask as an integer in network byte order
|
||||
* @return the network prefix length
|
||||
*/
|
||||
public static int netmaskIntToPrefixLength(int netmask) {
|
||||
return Integer.bitCount(netmask);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an InetAddress from a string where the string must be a standard
|
||||
* representation of a V4 or V6 address. Avoids doing a DNS lookup on failure
|
||||
* but it will throw an IllegalArgumentException in that case.
|
||||
*
|
||||
* @param addrString
|
||||
* @return the InetAddress
|
||||
* @hide
|
||||
*/
|
||||
public static InetAddress numericToInetAddress(String addrString)
|
||||
throws IllegalArgumentException {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get InetAddress masked with prefixLength. Will never return null.
|
||||
*
|
||||
* @param IP address which will be masked with specified prefixLength
|
||||
* @param prefixLength the prefixLength used to mask the IP
|
||||
*/
|
||||
public static InetAddress getNetworkPart(InetAddress address, int prefixLength) {
|
||||
if (address == null) {
|
||||
throw new RuntimeException("getNetworkPart doesn't accept null address");
|
||||
}
|
||||
|
||||
byte[] array = address.getAddress();
|
||||
|
||||
if (prefixLength < 0 || prefixLength > array.length * 8) {
|
||||
throw new RuntimeException("getNetworkPart - bad prefixLength");
|
||||
}
|
||||
|
||||
int offset = prefixLength / 8;
|
||||
int reminder = prefixLength % 8;
|
||||
byte mask = (byte) (0xFF << (8 - reminder));
|
||||
|
||||
if (offset < array.length) array[offset] = (byte) (array[offset] & mask);
|
||||
|
||||
offset++;
|
||||
|
||||
for (; offset < array.length; offset++) {
|
||||
array[offset] = 0;
|
||||
}
|
||||
|
||||
InetAddress netPart = null;
|
||||
try {
|
||||
netPart = InetAddress.getByAddress(array);
|
||||
} catch (UnknownHostException e) {
|
||||
throw new RuntimeException("getNetworkPart error - " + e.toString());
|
||||
}
|
||||
return netPart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if IP address type is consistent between two InetAddress.
|
||||
*
|
||||
* @return true if both are the same type. False otherwise.
|
||||
*/
|
||||
public static boolean addressTypeMatches(InetAddress left, InetAddress right) {
|
||||
return (((left instanceof Inet4Address) && (right instanceof Inet4Address)) ||
|
||||
((left instanceof Inet6Address) && (right instanceof Inet6Address)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a 32 char hex string into a Inet6Address.
|
||||
* throws a runtime exception if the string isn't 32 chars, isn't hex or can't be
|
||||
* made into an Inet6Address
|
||||
*
|
||||
* @param addrHexString a 32 character hex string representing an IPv6 addr
|
||||
* @return addr an InetAddress representation for the string
|
||||
*/
|
||||
public static InetAddress hexToInet6Address(String addrHexString)
|
||||
throws IllegalArgumentException {
|
||||
try {
|
||||
return numericToInetAddress(String.format("%s:%s:%s:%s:%s:%s:%s:%s",
|
||||
addrHexString.substring(0, 4), addrHexString.substring(4, 8),
|
||||
addrHexString.substring(8, 12), addrHexString.substring(12, 16),
|
||||
addrHexString.substring(16, 20), addrHexString.substring(20, 24),
|
||||
addrHexString.substring(24, 28), addrHexString.substring(28, 32)));
|
||||
} catch (Exception e) {
|
||||
Log.e("NetworkUtils", "error in hexToInet6Address(" + addrHexString + "): " + e);
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a string array of host addresses from a collection of InetAddresses
|
||||
*
|
||||
* @param addrs a Collection of InetAddresses
|
||||
* @return an array of Strings containing their host addresses
|
||||
*/
|
||||
public static String[] makeStrings(Collection<InetAddress> addrs) {
|
||||
String[] result = new String[addrs.size()];
|
||||
int i = 0;
|
||||
for (InetAddress addr : addrs) {
|
||||
result[i++] = addr.getHostAddress();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trim leading zeros from IPv4 address strings
|
||||
* Our base libraries will interpret that as octel..
|
||||
@@ -303,4 +86,5 @@ public class NetworkUtilsHelper {
|
||||
result = builder.toString();
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,286 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2011 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 android.net;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import timber.log.Timber; import android.util.Log; import static com.klinker.android.timberworkarounds.TimberExtensionsKt.Timber_isLoggable; // inserted with sed
|
||||
|
||||
import java.net.Inet4Address;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* A simple container for route information.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public class RouteInfo implements Parcelable {
|
||||
private static final String TAG = "RouteInfo";
|
||||
/**
|
||||
* The IP destination address for this route.
|
||||
*/
|
||||
private final LinkAddress mDestination;
|
||||
|
||||
/**
|
||||
* The gateway address for this route.
|
||||
*/
|
||||
private final InetAddress mGateway;
|
||||
|
||||
private final boolean mIsDefault;
|
||||
private final boolean mIsHost;
|
||||
|
||||
public RouteInfo(LinkAddress destination, InetAddress gateway) {
|
||||
if (destination == null) {
|
||||
if (gateway != null) {
|
||||
if (gateway instanceof Inet4Address) {
|
||||
try {
|
||||
destination = new LinkAddress(Inet4Address.getLocalHost(), 0);
|
||||
} catch (UnknownHostException e) {
|
||||
// TODO Auto-generated catch block
|
||||
Log.e(TAG, "exception thrown", e);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
destination = new LinkAddress(Inet6Address.getLocalHost(), 0);
|
||||
} catch (UnknownHostException e) {
|
||||
// TODO Auto-generated catch block
|
||||
Log.e(TAG, "exception thrown", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// no destination, no gateway. invalid.
|
||||
throw new RuntimeException("Invalid arguments passed in.");
|
||||
}
|
||||
}
|
||||
if (gateway == null) {
|
||||
if (destination.getAddress() instanceof Inet4Address) {
|
||||
try {
|
||||
gateway = Inet4Address.getLocalHost();
|
||||
} catch (UnknownHostException e) {
|
||||
// TODO Auto-generated catch block
|
||||
Log.e(TAG, "exception thrown", e);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
gateway = Inet6Address.getLocalHost();
|
||||
} catch (UnknownHostException e) {
|
||||
// TODO Auto-generated catch block
|
||||
Log.e(TAG, "exception thrown", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
mDestination = new LinkAddress(NetworkUtilsHelper.getNetworkPart(destination.getAddress(),
|
||||
destination.getNetworkPrefixLength()), destination.getNetworkPrefixLength());
|
||||
mGateway = gateway;
|
||||
mIsDefault = isDefault();
|
||||
mIsHost = isHost();
|
||||
}
|
||||
|
||||
public RouteInfo(InetAddress gateway) {
|
||||
this(null, gateway);
|
||||
}
|
||||
|
||||
public static RouteInfo makeHostRoute(InetAddress host) {
|
||||
return makeHostRoute(host, null);
|
||||
}
|
||||
|
||||
public static RouteInfo makeHostRoute(InetAddress host, InetAddress gateway) {
|
||||
if (host == null) return null;
|
||||
|
||||
if (host instanceof Inet4Address) {
|
||||
return new RouteInfo(new LinkAddress(host, 32), gateway);
|
||||
} else {
|
||||
return new RouteInfo(new LinkAddress(host, 128), gateway);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isHost() {
|
||||
try {
|
||||
return (mGateway.equals(Inet4Address.getLocalHost()) || mGateway.equals(Inet6Address.getLocalHost()));
|
||||
} catch (UnknownHostException e) {
|
||||
// TODO Auto-generated catch block
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isDefault() {
|
||||
boolean val = false;
|
||||
if (mGateway != null) {
|
||||
if (mGateway instanceof Inet4Address) {
|
||||
val = (mDestination == null || mDestination.getNetworkPrefixLength() == 0);
|
||||
} else {
|
||||
val = (mDestination == null || mDestination.getNetworkPrefixLength() == 0);
|
||||
}
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
public LinkAddress getDestination() {
|
||||
return mDestination;
|
||||
}
|
||||
|
||||
public InetAddress getGateway() {
|
||||
return mGateway;
|
||||
}
|
||||
|
||||
public boolean isDefaultRoute() {
|
||||
return mIsDefault;
|
||||
}
|
||||
|
||||
public boolean isHostRoute() {
|
||||
return mIsHost;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
String val = "";
|
||||
if (mDestination != null) val = mDestination.toString();
|
||||
if (mGateway != null) val += " -> " + mGateway.getHostAddress();
|
||||
return val;
|
||||
}
|
||||
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
if (mDestination == null) {
|
||||
dest.writeByte((byte) 0);
|
||||
} else {
|
||||
dest.writeByte((byte) 1);
|
||||
dest.writeByteArray(mDestination.getAddress().getAddress());
|
||||
dest.writeInt(mDestination.getNetworkPrefixLength());
|
||||
}
|
||||
|
||||
if (mGateway == null) {
|
||||
dest.writeByte((byte) 0);
|
||||
} else {
|
||||
dest.writeByte((byte) 1);
|
||||
dest.writeByteArray(mGateway.getAddress());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
|
||||
if (!(obj instanceof RouteInfo)) return false;
|
||||
|
||||
RouteInfo target = (RouteInfo) obj;
|
||||
|
||||
boolean sameDestination = (mDestination == null) ?
|
||||
target.getDestination() == null
|
||||
: mDestination.equals(target.getDestination());
|
||||
|
||||
boolean sameAddress = (mGateway == null) ?
|
||||
target.getGateway() == null
|
||||
: mGateway.equals(target.getGateway());
|
||||
|
||||
return sameDestination && sameAddress
|
||||
&& mIsDefault == target.mIsDefault;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return (mDestination == null ? 0 : mDestination.hashCode())
|
||||
+ (mGateway == null ? 0 : mGateway.hashCode())
|
||||
+ (mIsDefault ? 3 : 7);
|
||||
}
|
||||
|
||||
public static final Creator<RouteInfo> CREATOR =
|
||||
new Creator<RouteInfo>() {
|
||||
public RouteInfo createFromParcel(Parcel in) {
|
||||
InetAddress destAddr = null;
|
||||
int prefix = 0;
|
||||
InetAddress gateway = null;
|
||||
|
||||
if (in.readByte() == 1) {
|
||||
byte[] addr = in.createByteArray();
|
||||
prefix = in.readInt();
|
||||
|
||||
try {
|
||||
destAddr = InetAddress.getByAddress(addr);
|
||||
} catch (UnknownHostException e) {
|
||||
}
|
||||
}
|
||||
|
||||
if (in.readByte() == 1) {
|
||||
byte[] addr = in.createByteArray();
|
||||
|
||||
try {
|
||||
gateway = InetAddress.getByAddress(addr);
|
||||
} catch (UnknownHostException e) {
|
||||
}
|
||||
}
|
||||
|
||||
LinkAddress dest = null;
|
||||
|
||||
if (destAddr != null) {
|
||||
dest = new LinkAddress(destAddr, prefix);
|
||||
}
|
||||
|
||||
return new RouteInfo(dest, gateway);
|
||||
}
|
||||
|
||||
public RouteInfo[] newArray(int size) {
|
||||
return new RouteInfo[size];
|
||||
}
|
||||
};
|
||||
|
||||
private boolean matches(InetAddress destination) {
|
||||
if (destination == null) return false;
|
||||
|
||||
// if the destination is present and the route is default.
|
||||
// return true
|
||||
if (isDefault()) return true;
|
||||
|
||||
// match the route destination and destination with prefix length
|
||||
InetAddress dstNet = NetworkUtilsHelper.getNetworkPart(destination,
|
||||
mDestination.getNetworkPrefixLength());
|
||||
|
||||
return mDestination.getAddress().equals(dstNet);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the route from a Collection of routes that best matches a given address.
|
||||
* May return null if no routes are applicable.
|
||||
*
|
||||
* @param routes a Collection of RouteInfos to chose from
|
||||
* @param dest the InetAddress your trying to get to
|
||||
* @return the RouteInfo from the Collection that best fits the given address
|
||||
*/
|
||||
public static RouteInfo selectBestRoute(Collection<RouteInfo> routes, InetAddress dest) {
|
||||
if ((routes == null) || (dest == null)) return null;
|
||||
|
||||
RouteInfo bestRoute = null;
|
||||
// pick a longest prefix match under same address type
|
||||
for (RouteInfo route : routes) {
|
||||
if (NetworkUtilsHelper.addressTypeMatches(route.mDestination.getAddress(), dest)) {
|
||||
if ((bestRoute != null) &&
|
||||
(bestRoute.mDestination.getNetworkPrefixLength() >=
|
||||
route.mDestination.getNetworkPrefixLength())) {
|
||||
continue;
|
||||
}
|
||||
if (route.matches(dest)) bestRoute = route;
|
||||
}
|
||||
}
|
||||
return bestRoute;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user