1 /* 2 * Copyright (C) 2023 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 static org.junit.Assert.assertEquals; 20 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.content.pm.ResolveInfo; 25 import android.content.res.Resources; 26 27 import java.io.File; 28 import java.util.List; 29 30 public class WifiResourceUtil { 31 private static final String ACTION_RESOURCES_APK = 32 "com.android.server.wifi.intent.action.SERVICE_WIFI_RESOURCES_APK"; 33 34 private static final String WIFI_APEX_NAME = "com.android.wifi"; 35 36 private static final String WIFI_APEX_PATH = 37 new File("/apex", WIFI_APEX_NAME).getAbsolutePath(); 38 39 private final Resources sWifiResources; 40 private final Context sContext; 41 private String sWifiResourcesPackageName; 42 WifiResourceUtil(Context context)43 public WifiResourceUtil(Context context) { 44 sContext = context; 45 sWifiResources = getWifiResources(); 46 } 47 getWifiResources()48 private Resources getWifiResources() { 49 final PackageManager pm = sContext.getPackageManager(); 50 List<ResolveInfo> resolveInfos; 51 resolveInfos = pm.queryIntentActivities(new Intent(ACTION_RESOURCES_APK), 52 PackageManager.MATCH_SYSTEM_ONLY); 53 // remove apps that don't live in the Wifi apex 54 resolveInfos.removeIf(info -> 55 !info.activityInfo.applicationInfo.sourceDir.startsWith(WIFI_APEX_PATH)); 56 57 assertEquals(1, resolveInfos.size()); 58 sWifiResourcesPackageName = resolveInfos.get(0).activityInfo.applicationInfo.packageName; 59 try { 60 return sContext.createPackageContext(sWifiResourcesPackageName, 0).getResources(); 61 } catch (PackageManager.NameNotFoundException e) { 62 throw new RuntimeException(e); 63 } 64 } 65 getWifiBoolean(String name)66 public boolean getWifiBoolean(String name) throws PackageManager.NameNotFoundException { 67 return sWifiResources.getBoolean(sWifiResources.getIdentifier(name, "bool", 68 sWifiResourcesPackageName)); 69 } 70 } 71