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

Commit ada218ed authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Convert the Bluetooth NetworkAgent to the @SystemApi way."

parents 969443f8 31e1386b
Loading
Loading
Loading
Loading
+21 −20
Original line number Original line Diff line number Diff line
@@ -21,10 +21,9 @@ import android.content.Context;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.LinkProperties;
import android.net.NetworkAgent;
import android.net.NetworkAgent;
import android.net.NetworkAgentConfig;
import android.net.NetworkCapabilities;
import android.net.NetworkCapabilities;
import android.net.NetworkFactory;
import android.net.NetworkFactory;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
import android.net.ip.IIpClient;
import android.net.ip.IIpClient;
import android.net.ip.IpClientUtil;
import android.net.ip.IpClientUtil;
import android.net.ip.IpClientUtil.WaitForProvisioningCallbacks;
import android.net.ip.IpClientUtil.WaitForProvisioningCallbacks;
@@ -53,7 +52,6 @@ public class BluetoothTetheringNetworkFactory extends NetworkFactory {
    private final PanService mPanService;
    private final PanService mPanService;


    // All accesses to these must be synchronized(this).
    // All accesses to these must be synchronized(this).
    private final NetworkInfo mNetworkInfo;
    private IIpClient mIpClient;
    private IIpClient mIpClient;
    @GuardedBy("this")
    @GuardedBy("this")
    private int mIpClientStartIndex = 0;
    private int mIpClientStartIndex = 0;
@@ -66,7 +64,6 @@ public class BluetoothTetheringNetworkFactory extends NetworkFactory {
        mContext = context;
        mContext = context;
        mPanService = panService;
        mPanService = panService;


        mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_BLUETOOTH, 0, NETWORK_TYPE, "");
        mNetworkCapabilities = new NetworkCapabilities();
        mNetworkCapabilities = new NetworkCapabilities();
        initNetworkCapabilities();
        initNetworkCapabilities();
        setCapabilityFilter(mNetworkCapabilities);
        setCapabilityFilter(mNetworkCapabilities);
@@ -103,7 +100,7 @@ public class BluetoothTetheringNetworkFactory extends NetworkFactory {
        @Override
        @Override
        public void onLinkPropertiesChange(LinkProperties newLp) {
        public void onLinkPropertiesChange(LinkProperties newLp) {
            synchronized (BluetoothTetheringNetworkFactory.this) {
            synchronized (BluetoothTetheringNetworkFactory.this) {
                if (mNetworkAgent != null && mNetworkInfo.isConnected()) {
                if (mNetworkAgent != null) {
                    mNetworkAgent.sendLinkProperties(newLp);
                    mNetworkAgent.sendLinkProperties(newLp);
                }
                }
            }
            }
@@ -137,25 +134,24 @@ public class BluetoothTetheringNetworkFactory extends NetworkFactory {
    protected void startNetwork() {
    protected void startNetwork() {
        // TODO: Figure out how to replace this thread with simple invocations
        // TODO: Figure out how to replace this thread with simple invocations
        // of IpClient. This will likely necessitate a rethink about
        // of IpClient. This will likely necessitate a rethink about
        // NetworkAgent, NetworkInfo, and associated instance lifetimes.
        // NetworkAgent and associated instance lifetimes.
        Thread ipProvisioningThread = new Thread(new Runnable() {
        Thread ipProvisioningThread = new Thread(new Runnable() {
            @Override
            @Override
            public void run() {
            public void run() {
                LinkProperties linkProperties;
                final WaitForProvisioningCallbacks ipcCallback;
                final WaitForProvisioningCallbacks ipcCallback;
                final int ipClientStartIndex;


                synchronized (BluetoothTetheringNetworkFactory.this) {
                synchronized (BluetoothTetheringNetworkFactory.this) {
                    if (TextUtils.isEmpty(mInterfaceName)) {
                    if (TextUtils.isEmpty(mInterfaceName)) {
                        Log.e(TAG, "attempted to reverse tether without interface name");
                        Log.e(TAG, "attempted to reverse tether without interface name");
                        return;
                        return;
                    }
                    }
                    log("ipProvisioningThread(+" + mInterfaceName + "): " + "mNetworkInfo="
                    log("ipProvisioningThread(+" + mInterfaceName + ") start IP provisioning");
                            + mNetworkInfo);
                    ipcCallback = startIpClientLocked();
                    ipcCallback = startIpClientLocked();
                    mNetworkInfo.setDetailedState(DetailedState.OBTAINING_IPADDR, null, null);
                    ipClientStartIndex = mIpClientStartIndex;
                }
                }


                linkProperties = ipcCallback.waitForProvisioning();
                final LinkProperties linkProperties = ipcCallback.waitForProvisioning();
                if (linkProperties == null) {
                if (linkProperties == null) {
                    Log.e(TAG, "IP provisioning error.");
                    Log.e(TAG, "IP provisioning error.");
                    synchronized (BluetoothTetheringNetworkFactory.this) {
                    synchronized (BluetoothTetheringNetworkFactory.this) {
@@ -164,22 +160,27 @@ public class BluetoothTetheringNetworkFactory extends NetworkFactory {
                    }
                    }
                    return;
                    return;
                }
                }
                final NetworkAgentConfig config = new NetworkAgentConfig.Builder()
                        .setLegacyType(ConnectivityManager.TYPE_BLUETOOTH)
                        .setLegacyTypeName(NETWORK_TYPE)
                        .build();


                synchronized (BluetoothTetheringNetworkFactory.this) {
                synchronized (BluetoothTetheringNetworkFactory.this) {
                    mNetworkInfo.setIsAvailable(true);
                    // Reverse tethering has been stopped, and stopping won the race : there is
                    mNetworkInfo.setDetailedState(DetailedState.CONNECTED, null, null);
                    // no point in creating the agent (and it would be leaked), so bail.

                    if (ipClientStartIndex != mIpClientStartIndex) return;
                    // Create our NetworkAgent.
                    // Create our NetworkAgent.
                    mNetworkAgent =
                    mNetworkAgent =
                            new NetworkAgent(getLooper(), mContext, NETWORK_TYPE, mNetworkInfo,
                            new NetworkAgent(mContext, getLooper(), NETWORK_TYPE,
                                    mNetworkCapabilities, linkProperties, NETWORK_SCORE) {
                                    mNetworkCapabilities, linkProperties, NETWORK_SCORE,
                                    config, getProvider()) {
                                @Override
                                @Override
                                public void unwanted() {
                                public void unwanted() {
                                    BluetoothTetheringNetworkFactory.this.onCancelRequest();
                                    BluetoothTetheringNetworkFactory.this.onCancelRequest();
                                }
                                }

                                ;
                            };
                            };
                    mNetworkAgent.register();
                    mNetworkAgent.markConnected();
                }
                }
            }
            }
        });
        });
@@ -198,9 +199,8 @@ public class BluetoothTetheringNetworkFactory extends NetworkFactory {
        stopIpClientLocked();
        stopIpClientLocked();
        mInterfaceName = "";
        mInterfaceName = "";


        mNetworkInfo.setDetailedState(DetailedState.DISCONNECTED, null, null);
        if (mNetworkAgent != null) {
        if (mNetworkAgent != null) {
            mNetworkAgent.sendNetworkInfo(mNetworkInfo);
            mNetworkAgent.unregister();
            mNetworkAgent = null;
            mNetworkAgent = null;
        }
        }
        for (BluetoothDevice device : mPanService.getConnectedDevices()) {
        for (BluetoothDevice device : mPanService.getConnectedDevices()) {
@@ -244,6 +244,7 @@ public class BluetoothTetheringNetworkFactory extends NetworkFactory {
        mNetworkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
        mNetworkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
        mNetworkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
        mNetworkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
        mNetworkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
        mNetworkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING);
        mNetworkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED);
        // Bluetooth v3 and v4 go up to 24 Mbps.
        // Bluetooth v3 and v4 go up to 24 Mbps.
        // TODO: Adjust this to actual connection bandwidth.
        // TODO: Adjust this to actual connection bandwidth.
        mNetworkCapabilities.setLinkUpstreamBandwidthKbps(24 * 1000);
        mNetworkCapabilities.setLinkUpstreamBandwidthKbps(24 * 1000);