Add InetAddressCompat

Although the InetAddress symbols used by Connectivity are stable core
platform API, and should be usable, the core_current stubs are not yet
part of the module_current API.
Until that is fixed, add an InetAddressCompat utility that calls the
three static methods by reflection.

Test: atest FrameworksNetTests CtsNetTestCases
Bug: 183097033
Change-Id: I797009aeff1d39ae2dc06ef69d2e235689b43c89
This commit is contained in:
Remi NGUYEN VAN
2021-03-18 23:27:19 +09:00
parent eb6aa22205
commit 342ddddd9d
3 changed files with 79 additions and 3 deletions

View File

@@ -4639,7 +4639,7 @@ public class ConnectivityManager {
Log.e(TAG, "Can't set proxy properties", e); Log.e(TAG, "Can't set proxy properties", e);
} }
// Must flush DNS cache as new network may have different DNS resolutions. // Must flush DNS cache as new network may have different DNS resolutions.
InetAddress.clearDnsCache(); InetAddressCompat.clearDnsCache();
// Must flush socket pool as idle sockets will be bound to previous network and may // Must flush socket pool as idle sockets will be bound to previous network and may
// cause subsequent fetches to be performed on old network. // cause subsequent fetches to be performed on old network.
NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged(); NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();

View File

@@ -0,0 +1,76 @@
/*
* Copyright (C) 2021 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.util.Log;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Compatibility utility for InetAddress core platform APIs.
*
* Connectivity has access to such APIs, but they are not part of the module_current stubs yet
* (only core_current). Most stable core platform APIs are included manually in the connectivity
* build rules, but because InetAddress is also part of the base java SDK that is earlier on the
* classpath, the extra core platform APIs are not seen.
*
* TODO (b/183097033): remove this utility as soon as core_current is part of module_current
* @hide
*/
public class InetAddressCompat {
/**
* @see InetAddress#clearDnsCache()
*/
public static void clearDnsCache() {
try {
InetAddress.class.getMethod("clearDnsCache").invoke(null);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
Log.wtf(InetAddressCompat.class.getSimpleName(), "Error clearing DNS cache", e);
}
}
/**
* @see InetAddress#getAllByNameOnNet(String, int)
*/
public static InetAddress[] getAllByNameOnNet(String host, int netId) throws
UnknownHostException {
try {
return (InetAddress[]) InetAddress.class.getMethod("getAllByNameOnNet",
String.class, int.class).invoke(null, host, netId);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
Log.wtf(InetAddressCompat.class.getSimpleName(), "Error calling getAllByNameOnNet", e);
throw new IllegalStateException("Error querying via getAllNameOnNet", e);
}
}
/**
* @see InetAddress#getByNameOnNet(String, int)
*/
public static InetAddress getByNameOnNet(String host, int netId) throws
UnknownHostException {
try {
return (InetAddress) InetAddress.class.getMethod("getByNameOnNet",
String.class, int.class).invoke(null, host, netId);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
Log.wtf(InetAddressCompat.class.getSimpleName(), "Error calling getAllByNameOnNet", e);
throw new IllegalStateException("Error querying via getByNameOnNet", e);
}
}
}

View File

@@ -142,7 +142,7 @@ public class Network implements Parcelable {
* @throws UnknownHostException if the address lookup fails. * @throws UnknownHostException if the address lookup fails.
*/ */
public InetAddress[] getAllByName(String host) throws UnknownHostException { public InetAddress[] getAllByName(String host) throws UnknownHostException {
return InetAddress.getAllByNameOnNet(host, getNetIdForResolv()); return InetAddressCompat.getAllByNameOnNet(host, getNetIdForResolv());
} }
/** /**
@@ -155,7 +155,7 @@ public class Network implements Parcelable {
* if the address lookup fails. * if the address lookup fails.
*/ */
public InetAddress getByName(String host) throws UnknownHostException { public InetAddress getByName(String host) throws UnknownHostException {
return InetAddress.getByNameOnNet(host, getNetIdForResolv()); return InetAddressCompat.getByNameOnNet(host, getNetIdForResolv());
} }
/** /**