Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 23511a3e authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN Committed by Automerger Merge Worker
Browse files

Merge "Add InetAddressCompat" am: b648508c

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1645289

Change-Id: I6946ca309ae923a637f5e5f0ad73c91b26b3c376
parents 295bcd9b b648508c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -4661,7 +4661,7 @@ public class ConnectivityManager {
                Log.e(TAG, "Can't set proxy properties", e);
            }
            // 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
            // cause subsequent fetches to be performed on old network.
            NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
+76 −0
Original line number Diff line number Diff line
/*
 * 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);
        }
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -142,7 +142,7 @@ public class Network implements Parcelable {
     * @throws UnknownHostException if the address lookup fails.
     */
    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.
     */
    public InetAddress getByName(String host) throws UnknownHostException {
        return InetAddress.getByNameOnNet(host, getNetIdForResolv());
        return InetAddressCompat.getByNameOnNet(host, getNetIdForResolv());
    }

    /**