• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.net.connectivity;
18 
19 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED;
20 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
21 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING;
22 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED;
23 
24 import android.annotation.SuppressLint;
25 import android.content.Context;
26 import android.net.ConnectivityManager;
27 import android.net.LinkProperties;
28 import android.net.LocalNetworkConfig;
29 import android.net.NetworkAgent;
30 import android.net.NetworkAgentConfig;
31 import android.net.NetworkCapabilities;
32 import android.net.NetworkScore;
33 import android.os.Build;
34 import android.os.IBinder;
35 import android.os.Looper;
36 
37 import androidx.annotation.NonNull;
38 import androidx.annotation.RequiresApi;
39 
40 /**
41  * Utility providing limited access to module-internal APIs which are only available on Android S+,
42  * as this class is only in the bootclasspath on S+ as part of framework-connectivity.
43  *
44  * R+ module components like Tethering cannot depend on all hidden symbols from
45  * framework-connectivity. They only have access to stable API stubs where newer APIs can be
46  * accessed after an API level check (enforced by the linter), or to limited hidden symbols in this
47  * class which is also annotated with @RequiresApi (so API level checks are also enforced by the
48  * linter).
49  * @hide
50  */
51 @RequiresApi(Build.VERSION_CODES.S)
52 public class ConnectivityInternalApiUtil {
53 
54     /**
55      * Get a service binder token for
56      * {@link com.android.server.connectivity.wear.CompanionDeviceManagerProxyService}.
57      */
58     @RequiresApi(Build.VERSION_CODES.TIRAMISU)
getCompanionDeviceManagerProxyService(Context ctx)59     public static IBinder getCompanionDeviceManagerProxyService(Context ctx) {
60         final ConnectivityManager cm = ctx.getSystemService(ConnectivityManager.class);
61         return cm.getCompanionDeviceManagerProxyService();
62     }
63 
64     /**
65      * Obtain a routing coordinator manager from a context, possibly cross-module.
66      * @param ctx the context
67      * @return an instance of the coordinator manager
68      */
69     @RequiresApi(Build.VERSION_CODES.S)
getRoutingCoordinator(Context ctx)70     public static IBinder getRoutingCoordinator(Context ctx) {
71         final ConnectivityManager cm = ctx.getSystemService(ConnectivityManager.class);
72         return cm.getRoutingCoordinatorService();
73     }
74 
75     /**
76      * Create a NetworkAgent instance to be used by Tethering.
77      * @param ctx the context
78      * @return an instance of the {@code NetworkAgent}
79      */
80     // TODO: Expose LocalNetworkConfig related APIs and delete this method. This method is
81     //  only here because on R Tethering is installed and not Connectivity, requiring all
82     //  shared classes to be public API. LocalNetworkConfig is not public yet, but it will
83     //  only be used by Tethering on V+ so it's fine.
84     @SuppressLint("WrongConstant")
85     @NonNull
buildTetheringNetworkAgent(@onNull Context ctx, @NonNull Looper looper, @NonNull String logTag, int transportType, @NonNull LinkProperties lp)86     public static NetworkAgent buildTetheringNetworkAgent(@NonNull Context ctx,
87             @NonNull Looper looper, @NonNull String logTag, int transportType,
88             @NonNull LinkProperties lp) {
89         final NetworkCapabilities.Builder builder = new NetworkCapabilities.Builder()
90                 .addCapability(NET_CAPABILITY_NOT_METERED)
91                 .addCapability(NET_CAPABILITY_NOT_ROAMING)
92                 .addCapability(NET_CAPABILITY_NOT_CONGESTED)
93                 .addCapability(NET_CAPABILITY_NOT_SUSPENDED)
94                 .addTransportType(transportType);
95         // TODO: Change to use the constant definition. Flags.netCapabilityLocalNetwork() was not
96         //  fully rolled out but the service will still process this capability, set it anyway.
97         builder.addCapability(36 /* NET_CAPABILITY_LOCAL_NETWORK */);
98         final NetworkCapabilities caps = builder.build();
99         final NetworkAgentConfig nac = new NetworkAgentConfig.Builder().build();
100         return new NetworkAgent(ctx, looper, logTag, caps, lp,
101                 new LocalNetworkConfig.Builder().build(), new NetworkScore.Builder()
102                 .setKeepConnectedReason(NetworkScore.KEEP_CONNECTED_LOCAL_NETWORK)
103                 .build(), nac, null /* provider */) {
104         };
105     }
106 }
107