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 android.net.wifi.cts;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 import android.content.pm.PackageInfo;
22 import android.content.pm.PackageManager;
23 import android.os.Build;
24 import android.util.Log;
25 
26 import com.android.compatibility.common.util.ApiLevelUtil;
27 
28 /**
29  * Wrapper class for checking the wifi module version.
30  *
31  * Wifi CTS tests for a dessert release can be run on older dessert releases as a part of MTS.
32  * Since wifi module is optional, not all older dessert release will contain the wifi module from
33  * the provided dessert release (which means we cannot use new wifi API's on those devices).
34  *
35  * <p>
36  * This utility tries to help solve that problem by trying to check if the device is running at
37  * least
38  * <li> The provided dessert release using {@link ApiLevelUtil}, OR</li>
39  * <li> The wifi module from the provided dessert release on an older dessert release device</li>
40  *
41  * In either case above, we can somewhat safely assume that the wifi API's from the provided dessert
42  * release are present and behave the way we expect to.
43  * </p>
44  *
45  * Note: This does not check for granular wifi module version codes, only that it is some version
46  * of the module from the provided dessert release.
47  */
48 public class WifiBuildCompat {
49     private static final String TAG = "WifiBuildCompat";
50 
51     private static final String WIFI_PACKAGE_NAME_SUFFIX = ".android.wifi";
52 
53     private static final long WIFI_APEX_BASE_VERSION_CODE_FOR_S = 310000000;
54 
getWifiApexVersionCode(@onNull Context ctx)55     private static long getWifiApexVersionCode(@NonNull Context ctx) {
56         PackageManager packageManager = ctx.getPackageManager();
57         long wifiStackVersion = 0;
58         String wifiPackageName = null;
59         for (PackageInfo packageInfo : packageManager
60                 .getInstalledPackages(PackageManager.MATCH_APEX)) {
61             if (packageInfo.packageName.endsWith(WIFI_PACKAGE_NAME_SUFFIX)) {
62                 wifiPackageName = packageInfo.packageName;
63                 wifiStackVersion = packageInfo.getLongVersionCode();
64                 break;
65             }
66         }
67         Log.v(TAG, "Wifi Module package name is " + wifiPackageName
68                 + ", version is " + wifiStackVersion);
69         return wifiStackVersion;
70     }
71 
WifiBuildCompat()72     private WifiBuildCompat() { }
73 
isPlatformOrWifiModuleAtLeastS(@onNull Context ctx)74     public static boolean isPlatformOrWifiModuleAtLeastS(@NonNull Context ctx) {
75         return ApiLevelUtil.isAtLeast(Build.VERSION_CODES.S)
76                 || getWifiApexVersionCode(ctx) >= WIFI_APEX_BASE_VERSION_CODE_FOR_S;
77     }
78 }
79