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

Commit 50a228a2 authored by junyulai's avatar junyulai
Browse files

Implement registerSystemDefaultNetworkCallback replacement in api30

In R framework, while registerSystemDefaultNetworkCallback was
not available, Tethering module used a hacky way to track system
default network, which is to create a NetworkRequest that is
exact the same with the one in ConnectivityService.

In S framework, although we have the new API to track system
default network, but still need to provide backward compatibility
for R devices. Thus, implement the workaround in api30 and
provide unified interface to tethering.

This change also use service name string to get system service,
given that getSystemService(ConnectivityManager.class) cannot be
mocked.

Test: atest TetheringCoverageTests
Bug: 185952829
Change-Id: Iaf21b6b662aa6aba79c2b75379128b8523f81f02
parent 01e4fa96
Loading
Loading
Loading
Loading
+21 −3
Original line number Diff line number Diff line
@@ -17,12 +17,15 @@
package com.android.networkstack.apishim.api30;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Build;
import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.android.networkstack.apishim.common.ConnectivityManagerShim;
import com.android.networkstack.apishim.common.ShimUtils;
@@ -33,8 +36,10 @@ import com.android.networkstack.apishim.common.UnsupportedApiLevelException;
 */
public class ConnectivityManagerShimImpl
        extends com.android.networkstack.apishim.api29.ConnectivityManagerShimImpl {
    protected final ConnectivityManager mCm;
    protected ConnectivityManagerShimImpl(Context context) {
        super(context);
        mCm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    }

    /**
@@ -65,9 +70,22 @@ public class ConnectivityManagerShimImpl
     * @throws UnsupportedApiLevelException if API is not available in this API level.
     */
    @Override
    @RequiresApi(Build.VERSION_CODES.R)
    public void registerSystemDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
            @NonNull Handler handler) throws UnsupportedApiLevelException {
        // Not supported for API 30.
        throw new UnsupportedApiLevelException("Not supported in API 30.");
            @NonNull Handler handler) {
        // defaultNetworkRequest is not really a "request", just a way of tracking the system
        // default network. It's guaranteed not to actually bring up any networks because it
        // should be the same request as the ConnectivityService default request, and thus
        // shares fate with it.  In API <= R, registerSystemDefaultNetworkCallback is not
        // available, and registerDefaultNetworkCallback will not track the system default when
        // a VPN applies to the UID of this process.
        final NetworkRequest defaultNetworkRequest = new NetworkRequest.Builder()
                .clearCapabilities()
                .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
                .addCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED)
                .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
                .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                .build();
        mCm.requestNetwork(defaultNetworkRequest, networkCallback, handler);
    }
}
+0 −3
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.networkstack.apishim;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback;
import android.net.NetworkRequest;
import android.os.Build;
@@ -36,11 +35,9 @@ import java.util.Collection;
 */
public class ConnectivityManagerShimImpl
        extends com.android.networkstack.apishim.api30.ConnectivityManagerShimImpl  {
    private final ConnectivityManager mCm;

    protected ConnectivityManagerShimImpl(Context context) {
        super(context);
        mCm = context.getSystemService(ConnectivityManager.class);
    }

    /**