1 /*
2  * Copyright (C) 2019 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.server.wifi.util;
18 
19 import android.content.ApexEnvironment;
20 import android.content.pm.ApplicationInfo;
21 import android.os.UserHandle;
22 
23 import java.io.File;
24 
25 /**
26  * Provides access to environment variables.
27  *
28  * Note: @hide methods copied from android.os.Environment
29  */
30 public class Environment {
31     /**
32      * Wifi apex name.
33      */
34     private static final String WIFI_APEX_NAME = "com.android.wifi";
35 
36     /**
37      * The path where the Wifi apex is mounted.
38      * Current value = "/apex/com.android.wifi"
39      */
40     private static final String WIFI_APEX_PATH =
41             new File("/apex", WIFI_APEX_NAME).getAbsolutePath();
42 
43     /**
44      * Wifi shared folder.
45      */
getWifiSharedDirectory()46     public static File getWifiSharedDirectory() {
47         return ApexEnvironment.getApexEnvironment(WIFI_APEX_NAME).getDeviceProtectedDataDir();
48     }
49 
50     /**
51      * Wifi user specific folder.
52      */
getWifiUserDirectory(int userId)53     public static File getWifiUserDirectory(int userId) {
54         return ApexEnvironment.getApexEnvironment(WIFI_APEX_NAME)
55                 .getCredentialProtectedDataDirForUser(UserHandle.of(userId));
56     }
57 
58     /**
59      * Returns true if the app is in the Wifi apex, false otherwise.
60      * Checks if the app's path starts with "/apex/com.android.wifi".
61      */
isAppInWifiApex(ApplicationInfo appInfo)62     public static boolean isAppInWifiApex(ApplicationInfo appInfo) {
63         return appInfo.sourceDir.startsWith(WIFI_APEX_PATH);
64     }
65 }
66