1 /*
2  * Copyright (C) 2018 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.shared;
18 
19 import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
20 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
21 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN;
22 import static android.net.NetworkCapabilities.NET_CAPABILITY_TRUSTED;
23 
24 import android.net.NetworkCapabilities;
25 
26 /** @hide */
27 public class NetworkMonitorUtils {
28 
29     // Network conditions broadcast constants
30     public static final String ACTION_NETWORK_CONDITIONS_MEASURED =
31             "android.net.conn.NETWORK_CONDITIONS_MEASURED";
32     public static final String EXTRA_CONNECTIVITY_TYPE = "extra_connectivity_type";
33     public static final String EXTRA_NETWORK_TYPE = "extra_network_type";
34     public static final String EXTRA_RESPONSE_RECEIVED = "extra_response_received";
35     public static final String EXTRA_IS_CAPTIVE_PORTAL = "extra_is_captive_portal";
36     public static final String EXTRA_CELL_ID = "extra_cellid";
37     public static final String EXTRA_SSID = "extra_ssid";
38     public static final String EXTRA_BSSID = "extra_bssid";
39     /** real time since boot */
40     public static final String EXTRA_REQUEST_TIMESTAMP_MS = "extra_request_timestamp_ms";
41     public static final String EXTRA_RESPONSE_TIMESTAMP_MS = "extra_response_timestamp_ms";
42     public static final String PERMISSION_ACCESS_NETWORK_CONDITIONS =
43             "android.permission.ACCESS_NETWORK_CONDITIONS";
44 
45     /**
46      * Return whether validation is required for private DNS in strict mode.
47      * @param nc Network capabilities of the network to test.
48      */
isPrivateDnsValidationRequired(NetworkCapabilities nc)49     public static boolean isPrivateDnsValidationRequired(NetworkCapabilities nc) {
50         // TODO: Consider requiring validation for DUN networks.
51         return nc != null
52                 && nc.hasCapability(NET_CAPABILITY_INTERNET)
53                 && nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)
54                 && nc.hasCapability(NET_CAPABILITY_TRUSTED);
55     }
56 
57     /**
58      * Return whether validation is required for a network.
59      * @param nc Network capabilities of the network to test.
60      */
isValidationRequired(NetworkCapabilities nc)61     public static boolean isValidationRequired(NetworkCapabilities nc) {
62         // TODO: Consider requiring validation for DUN networks.
63         return isPrivateDnsValidationRequired(nc) && nc.hasCapability(NET_CAPABILITY_NOT_VPN);
64     }
65 }
66