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

Commit c473dc4e authored by Lorenzo Colitti's avatar Lorenzo Colitti
Browse files

Add a getBoundURL method that returns a network-specific URL.

Change-Id: I4b57e675bb87064ab75dcc36b00fdc7a2987b86e
parent d65e8f4b
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -17438,6 +17438,7 @@ package android.net {
  public class Network implements android.os.Parcelable {
  public class Network implements android.os.Parcelable {
    method public int describeContents();
    method public int describeContents();
    method public java.net.InetAddress[] getAllByName(java.lang.String) throws java.net.UnknownHostException;
    method public java.net.InetAddress[] getAllByName(java.lang.String) throws java.net.UnknownHostException;
    method public java.net.URL getBoundURL(java.net.URL) throws java.net.MalformedURLException;
    method public java.net.InetAddress getByName(java.lang.String) throws java.net.UnknownHostException;
    method public java.net.InetAddress getByName(java.lang.String) throws java.net.UnknownHostException;
    method public javax.net.SocketFactory getSocketFactory();
    method public javax.net.SocketFactory getSocketFactory();
    method public void writeToParcel(android.os.Parcel, int);
    method public void writeToParcel(android.os.Parcel, int);
+37 −2
Original line number Original line Diff line number Diff line
@@ -23,11 +23,16 @@ import android.os.Parcel;
import java.io.IOException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.net.UnknownHostException;
import java.net.URL;
import javax.net.SocketFactory;
import javax.net.SocketFactory;


import com.android.okhttp.HostResolver;
import com.android.okhttp.OkHttpClient;

/**
/**
 * Identifies a {@code Network}.  This is supplied to applications via
 * Identifies a {@code Network}.  This is supplied to applications via
 * {@link ConnectivityManager.NetworkCallback} in response to the active
 * {@link ConnectivityManager.NetworkCallback} in response to the active
@@ -44,7 +49,11 @@ public class Network implements Parcelable {
     */
     */
    public final int netId;
    public final int netId;


    // Objects used to perform per-network operations such as getSocketFactory
    // and getBoundURL, and a lock to protect access to them.
    private NetworkBoundSocketFactory mNetworkBoundSocketFactory = null;
    private NetworkBoundSocketFactory mNetworkBoundSocketFactory = null;
    private OkHttpClient mOkHttpClient = null;
    private Object mLock = new Object();


    /**
    /**
     * @hide
     * @hide
@@ -166,12 +175,38 @@ public class Network implements Parcelable {
     *         {@code Network}.
     *         {@code Network}.
     */
     */
    public SocketFactory getSocketFactory() {
    public SocketFactory getSocketFactory() {
        synchronized (mLock) {
            if (mNetworkBoundSocketFactory == null) {
            if (mNetworkBoundSocketFactory == null) {
                mNetworkBoundSocketFactory = new NetworkBoundSocketFactory(netId);
                mNetworkBoundSocketFactory = new NetworkBoundSocketFactory(netId);
            }
            }
        }
        return mNetworkBoundSocketFactory;
        return mNetworkBoundSocketFactory;
    }
    }


    /**
     * Returns a {@link URL} based on the given URL but bound to this {@code Network}.
     * Note that if this {@code Network} ever disconnects, this factory and any URL object it
     * produced in the past or future will cease to work.
     *
     * @return a {@link URL} bound to this {@code Network}.
     */
    public URL getBoundURL(URL url) throws MalformedURLException {
        synchronized (mLock) {
            if (mOkHttpClient == null) {
                HostResolver hostResolver = new HostResolver() {
                    @Override
                    public InetAddress[] getAllByName(String host) throws UnknownHostException {
                        return Network.this.getAllByName(host);
                    }
                };
                mOkHttpClient = new OkHttpClient()
                        .setSocketFactory(getSocketFactory())
                        .setHostResolver(hostResolver);
            }
        }
        return new URL(url, "", mOkHttpClient.createURLStreamHandler(url.getProtocol()));
    }

    // implement the Parcelable interface
    // implement the Parcelable interface
    public int describeContents() {
    public int describeContents() {
        return 0;
        return 0;