• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 com.android.networkstack.apishim.api29;
18 
19 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
20 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN;
21 import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED;
22 
23 import android.content.Context;
24 import android.net.ConnectivityManager;
25 import android.net.ConnectivityManager.NetworkCallback;
26 import android.net.NetworkCapabilities;
27 import android.net.NetworkRequest;
28 import android.os.Build;
29 import android.os.Handler;
30 
31 import androidx.annotation.NonNull;
32 import androidx.annotation.RequiresApi;
33 
34 import com.android.networkstack.apishim.common.ConnectivityManagerShim;
35 import com.android.networkstack.apishim.common.UnsupportedApiLevelException;
36 
37 /**
38  * Implementation of {@link ConnectivityManagerShim} for API 29.
39  */
40 @RequiresApi(Build.VERSION_CODES.Q)
41 public class ConnectivityManagerShimImpl implements ConnectivityManagerShim {
42     protected final ConnectivityManager mCm;
ConnectivityManagerShimImpl(Context context)43     protected ConnectivityManagerShimImpl(Context context) {
44         mCm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
45     }
46 
47     /**
48      * Get a new instance of {@link ConnectivityManagerShim}.
49      */
newInstance(Context context)50     public static ConnectivityManagerShim newInstance(Context context) {
51         return new ConnectivityManagerShimImpl(context);
52     }
53     /**
54      * See android.net.ConnectivityManager#requestBackgroundNetwork
55      * @throws UnsupportedApiLevelException if API is not available in this API level.
56      */
57     @Override
requestBackgroundNetwork(@onNull NetworkRequest request, @NonNull NetworkCallback networkCallback, @NonNull Handler handler)58     public void requestBackgroundNetwork(@NonNull NetworkRequest request,
59             @NonNull NetworkCallback networkCallback, @NonNull Handler handler)
60             throws UnsupportedApiLevelException {
61         // Not supported for API 29.
62         throw new UnsupportedApiLevelException("Not supported in API 29.");
63     }
64 
65     /**
66      * See android.net.ConnectivityManager#registerSystemDefaultNetworkCallback
67      */
68     @Override
registerSystemDefaultNetworkCallback(@onNull NetworkCallback networkCallback, @NonNull Handler handler)69     public void registerSystemDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
70             @NonNull Handler handler) {
71         // defaultNetworkRequest is not really a "request", just a way of tracking the system
72         // default network. It's guaranteed not to actually bring up any networks because it
73         // should be the same request as the ConnectivityService default request, and thus
74         // shares fate with it.  In API <= R, registerSystemDefaultNetworkCallback is not
75         // available, and registerDefaultNetworkCallback will not track the system default when
76         // a VPN applies to the UID of this process.
77         final NetworkRequest defaultNetworkRequest = makeEmptyCapabilitiesRequest()
78                 .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
79                 .addCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED)
80                 .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
81                 .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
82                 .build();
83         mCm.requestNetwork(defaultNetworkRequest, networkCallback, handler);
84     }
85 
86     @NonNull
makeEmptyCapabilitiesRequest()87     protected NetworkRequest.Builder makeEmptyCapabilitiesRequest() {
88         // Q does not have clearCapabilities(), so assume the default capabilities are as below
89         return new NetworkRequest.Builder()
90                 .removeCapability(NET_CAPABILITY_NOT_RESTRICTED)
91                 .removeCapability(NET_CAPABILITY_TRUSTED)
92                 .removeCapability(NET_CAPABILITY_NOT_VPN);
93     }
94 }
95