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

Commit cee9b51c authored by Paul Jensen's avatar Paul Jensen
Browse files

Query HTTP proxy for network via a new API to avoid permissions exceptions

Add @hidden ConnectivityManager.getProxyForNetwork() API.

Bug:20470604
Change-Id: I6a9bc4afc8273bc43b14cdeccfedbbf3ff66be40
parent 424681e4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -4552,7 +4552,7 @@ public final class ActivityThread {
            // crash if we can't get it.
            IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
            try {
                final ProxyInfo proxyInfo = service.getDefaultProxy();
                final ProxyInfo proxyInfo = service.getProxyForNetwork(null);
                Proxy.setHttpProxySystemProperty(proxyInfo);
            } catch (RemoteException e) {}
        }
+23 −16
Original line number Diff line number Diff line
@@ -1904,9 +1904,6 @@ public class ConnectivityManager {
     *
     * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
     *        if no global HTTP proxy is set.
     *
     * <p>This method requires the caller to hold the permission
     * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
     * @hide
     */
    public ProxyInfo getGlobalProxy() {
@@ -1917,6 +1914,28 @@ public class ConnectivityManager {
        }
    }

    /**
     * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
     * network-specific HTTP proxy.  If {@code network} is null, the
     * network-specific proxy returned is the proxy of the default active
     * network.
     *
     * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
     *         global HTTP proxy is set, {@code ProxyInfo} for {@code network},
     *         or when {@code network} is {@code null},
     *         the {@code ProxyInfo} for the default active network.  Returns
     *         {@code null} when no proxy applies or the caller doesn't have
     *         permission to use {@code network}.
     * @hide
     */
    public ProxyInfo getProxyForNetwork(Network network) {
        try {
            return mService.getProxyForNetwork(network);
        } catch (RemoteException e) {
            return null;
        }
    }

    /**
     * Get the current default HTTP proxy settings.  If a global proxy is set it will be returned,
     * otherwise if this process is bound to a {@link Network} using
@@ -1927,19 +1946,7 @@ public class ConnectivityManager {
     *        HTTP proxy is active.
     */
    public ProxyInfo getDefaultProxy() {
        final Network network = getBoundNetworkForProcess();
        if (network != null) {
            final ProxyInfo globalProxy = getGlobalProxy();
            if (globalProxy != null) return globalProxy;
            final LinkProperties lp = getLinkProperties(network);
            if (lp != null) return lp.getHttpProxy();
            return null;
        }
        try {
            return mService.getDefaultProxy();
        } catch (RemoteException e) {
            return null;
        }
        return getProxyForNetwork(getBoundNetworkForProcess());
    }

    /**
+1 −1
Original line number Diff line number Diff line
@@ -104,7 +104,7 @@ interface IConnectivityManager

    void setGlobalProxy(in ProxyInfo p);

    ProxyInfo getDefaultProxy();
    ProxyInfo getProxyForNetwork(in Network nework);

    boolean prepareVpn(String oldPackage, String newPackage);

+1 −6
Original line number Diff line number Diff line
@@ -247,12 +247,7 @@ public class Network implements Parcelable {
            throw new IOException("No ConnectivityManager yet constructed, please construct one");
        }
        // TODO: Should this be optimized to avoid fetching the global proxy for every request?
        ProxyInfo proxyInfo = cm.getGlobalProxy();
        if (proxyInfo == null) {
            // TODO: Should this be optimized to avoid fetching LinkProperties for every request?
            final LinkProperties lp = cm.getLinkProperties(this);
            if (lp != null) proxyInfo = lp.getHttpProxy();
        }
        final ProxyInfo proxyInfo = cm.getProxyForNetwork(this);
        java.net.Proxy proxy = null;
        if (proxyInfo != null) {
            proxy = proxyInfo.makeProxy();
+6 −0
Original line number Diff line number Diff line
@@ -193,6 +193,12 @@ public class NetworkUtils {
     */
    public native static boolean protectFromVpn(int socketfd);

    /**
     * Determine if {@code uid} can access network designated by {@code netId}.
     * @return {@code true} if {@code uid} can access network, {@code false} otherwise.
     */
    public native static boolean queryUserAccess(int uid, int netId);

    /**
     * Convert a IPv4 address from an integer to an InetAddress.
     * @param hostAddress an int corresponding to the IPv4 address in network byte order
Loading