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 package com.android.compatibility.common.deviceinfo;
17 
18 import android.content.pm.PackageManager;
19 
20 import android.net.wifi.ScanResult;
21 import android.net.wifi.WifiManager;
22 
23 import android.os.Build;
24 
25 import android.util.Log;
26 
27 import com.android.compatibility.common.util.DeviceInfoStore;
28 
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /**
34  * Device connectivity info collector.
35  */
36 public final class ConnectivityDeviceInfo extends DeviceInfo {
37 
38     private static final String LOG_TAG = "ConnectivityDeviceInfo";
39 
40     private static final int WIFI_STANDARDS[] = {
41         ScanResult.WIFI_STANDARD_UNKNOWN,
42         ScanResult.WIFI_STANDARD_LEGACY,
43         ScanResult.WIFI_STANDARD_11N,
44         ScanResult.WIFI_STANDARD_11AC,
45         ScanResult.WIFI_STANDARD_11AX,
46         ScanResult.WIFI_STANDARD_11AD,
47         ScanResult.WIFI_STANDARD_11BE
48     };
49 
wifiStandardToString(int standard)50     private static String wifiStandardToString(int standard) {
51         switch (standard) {
52         case ScanResult.WIFI_STANDARD_UNKNOWN:
53             return "unknown";
54         case ScanResult.WIFI_STANDARD_LEGACY:
55             return "legacy";
56         case ScanResult.WIFI_STANDARD_11N:
57             return "11n";
58         case ScanResult.WIFI_STANDARD_11AC:
59             return "11ac";
60         case ScanResult.WIFI_STANDARD_11AX:
61             return "11ax";
62         case ScanResult.WIFI_STANDARD_11AD:
63             return "11ad";
64         case ScanResult.WIFI_STANDARD_11BE:
65             return "11be";
66         }
67         return "";
68     }
69 
hasWifi()70     private boolean hasWifi() {
71         return getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI);
72     }
73 
74     @Override
collectDeviceInfo(DeviceInfoStore store)75     protected void collectDeviceInfo(DeviceInfoStore store) throws Exception {
76         if (hasWifi()) {
77             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
78                 try {
79                     collectWifiStandards(store);
80                 } catch (IOException e) {
81                     Log.w(LOG_TAG, "Failed to collect WiFi standards", e);
82                 }
83             }
84         }
85     }
86 
collectWifiStandards(DeviceInfoStore store)87     private void collectWifiStandards(DeviceInfoStore store) throws IOException {
88       final WifiManager wifiManager = getContext().getSystemService(WifiManager.class);
89       List<String> wifiStandards = new ArrayList<String>();
90       for (int standard: WIFI_STANDARDS) {
91           if (wifiManager.isWifiStandardSupported(standard)) {
92               wifiStandards.add(wifiStandardToString(standard));
93           }
94       }
95       store.addListResult("wifi_standards", wifiStandards);
96     }
97 }
98