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.wifi.util;
18 
19 import android.content.ApexEnvironment;
20 import android.content.pm.ApplicationInfo;
21 import android.os.Build;
22 import android.os.UserHandle;
23 import android.sysprop.VndkProperties;
24 import android.util.Log;
25 
26 import java.io.File;
27 
28 /**
29  * Provides access to environment variables.
30  *
31  * Note: @hide methods copied from android.os.Environment
32  */
33 public class Environment {
34     /**
35      * Wifi apex name.
36      */
37     private static final String WIFI_APEX_NAME = "com.android.wifi";
38 
39     private static final String TAG = "Environment";
40 
41     /**
42      * The path where the Wifi apex is mounted.
43      * Current value = "/apex/com.android.wifi"
44      */
45     private static final String WIFI_APEX_PATH =
46             new File("/apex", WIFI_APEX_NAME).getAbsolutePath();
47 
48     /**
49      * Wifi shared folder.
50      */
getWifiSharedDirectory()51     public static File getWifiSharedDirectory() {
52         return ApexEnvironment.getApexEnvironment(WIFI_APEX_NAME).getDeviceProtectedDataDir();
53     }
54 
55     /**
56      * Wifi user specific folder.
57      */
getWifiUserDirectory(int userId)58     public static File getWifiUserDirectory(int userId) {
59         return ApexEnvironment.getApexEnvironment(WIFI_APEX_NAME)
60                 .getCredentialProtectedDataDirForUser(UserHandle.of(userId));
61     }
62 
63     /**
64      * Returns true if the app is in the Wifi apex, false otherwise.
65      * Checks if the app's path starts with "/apex/com.android.wifi".
66      */
isAppInWifiApex(ApplicationInfo appInfo)67     public static boolean isAppInWifiApex(ApplicationInfo appInfo) {
68         return appInfo.sourceDir.startsWith(WIFI_APEX_PATH);
69     }
70 
71     /**
72      * Return whether the VNDK version of the vendor partition is newer than the given API level.
73      * If the property is set to non-integer value, this means the vendor partition is using
74      * current API level and true is returned.
75      *
76      * Note: reference from com.android.compatibility.common.util.PropertyUtil;
77      */
isVndkApiLevelNewerThan(int apiLevel)78     public static boolean isVndkApiLevelNewerThan(int apiLevel) {
79         final String version = VndkProperties.vendor_vndk_version().orElse("");
80         int vndkApiLevel = 0;
81         if (!version.isEmpty()) {
82             try {
83                 vndkApiLevel = Integer.parseInt(version);
84             } catch (NumberFormatException ignore) {
85                 if (!("REL".equals(Build.VERSION.CODENAME))) {
86                     Log.d(TAG, "developer build, bypass the vndk version check");
87                     return true;
88                 }
89             }
90         }
91         return vndkApiLevel > apiLevel;
92     }
93 }
94