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; 18 19 import android.annotation.NonNull; 20 import android.content.Context; 21 import android.content.ContextWrapper; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.content.pm.ResolveInfo; 25 import android.content.res.AssetManager; 26 import android.content.res.Resources; 27 import android.net.wifi.util.Environment; 28 import android.util.Log; 29 30 import androidx.annotation.Nullable; 31 32 import java.util.List; 33 import java.util.stream.Collectors; 34 35 /** 36 * Wrapper for context to override getResources method. Resources for wifi mainline jar needs to be 37 * fetched from the resources APK. 38 * 39 * @hide 40 */ 41 public class WifiContext extends ContextWrapper { 42 private static final String TAG = "WifiContext"; 43 /** Intent action that is used to identify ServiceWifiResources.apk */ 44 private static final String ACTION_RESOURCES_APK = 45 "com.android.server.wifi.intent.action.SERVICE_WIFI_RESOURCES_APK"; 46 /** Intent action that is used to identify WifiDialog.apk */ 47 private static final String ACTION_WIFI_DIALOG_APK = 48 "com.android.server.wifi.intent.action.WIFI_DIALOG_APK"; 49 50 /** Since service-wifi runs within system_server, its package name is "android". */ 51 private static final String SERVICE_WIFI_PACKAGE_NAME = "android"; 52 53 private String mWifiOverlayApkPkgName; 54 private String mWifiDialogApkPkgName; 55 56 // Cached resources from the resources APK. 57 private AssetManager mWifiAssetsFromApk; 58 private Resources mWifiResourcesFromApk; 59 private Resources.Theme mWifiThemeFromApk; 60 WifiContext(@onNull Context contextBase)61 public WifiContext(@NonNull Context contextBase) { 62 super(contextBase); 63 } 64 65 /** Get the package name of ServiceWifiResources.apk */ getWifiOverlayApkPkgName()66 public @Nullable String getWifiOverlayApkPkgName() { 67 if (mWifiOverlayApkPkgName != null) { 68 return mWifiOverlayApkPkgName; 69 } 70 mWifiOverlayApkPkgName = getApkPkgNameForAction(ACTION_RESOURCES_APK); 71 if (mWifiOverlayApkPkgName == null) { 72 // Resource APK not loaded yet, print a stack trace to see where this is called from 73 Log.e(TAG, "Attempted to fetch resources before Wifi Resources APK is loaded!", 74 new IllegalStateException()); 75 return null; 76 } 77 Log.i(TAG, "Found Wifi Resources APK at: " + mWifiOverlayApkPkgName); 78 return mWifiOverlayApkPkgName; 79 } 80 81 /** Get the package name of WifiDialog.apk */ getWifiDialogApkPkgName()82 public @Nullable String getWifiDialogApkPkgName() { 83 if (mWifiDialogApkPkgName != null) { 84 return mWifiDialogApkPkgName; 85 } 86 mWifiDialogApkPkgName = getApkPkgNameForAction(ACTION_WIFI_DIALOG_APK); 87 if (mWifiDialogApkPkgName == null) { 88 // WifiDialog APK not loaded yet, print a stack trace to see where this is called from 89 Log.e(TAG, "Attempted to fetch WifiDialog apk before it is loaded!", 90 new IllegalStateException()); 91 return null; 92 } 93 Log.i(TAG, "Found Wifi Dialog APK at: " + mWifiDialogApkPkgName); 94 return mWifiDialogApkPkgName; 95 } 96 97 /** Gets the package name of the apk responding to the given intent action */ getApkPkgNameForAction(@onNull String action)98 private String getApkPkgNameForAction(@NonNull String action) { 99 List<ResolveInfo> resolveInfos = getPackageManager().queryIntentActivities( 100 new Intent(action), 101 PackageManager.MATCH_SYSTEM_ONLY); 102 Log.i(TAG, "Got resolveInfos for " + action + ": " + resolveInfos); 103 104 // remove apps that don't live in the Wifi apex 105 resolveInfos.removeIf(info -> 106 !Environment.isAppInWifiApex(info.activityInfo.applicationInfo)); 107 108 if (resolveInfos.isEmpty()) { 109 return null; 110 } 111 112 if (resolveInfos.size() > 1) { 113 // multiple apps found, log a warning, but continue 114 Log.w(TAG, "Found > 1 APK that can resolve " + action + ": " 115 + resolveInfos.stream() 116 .map(info -> info.activityInfo.applicationInfo.packageName) 117 .collect(Collectors.joining(", "))); 118 } 119 120 // Assume the first ResolveInfo is the one we're looking for 121 ResolveInfo info = resolveInfos.get(0); 122 return info.activityInfo.applicationInfo.packageName; 123 } 124 getResourcesApkContext()125 private Context getResourcesApkContext() { 126 try { 127 String packageName = getWifiOverlayApkPkgName(); 128 if (packageName != null) { 129 return createPackageContext(packageName, 0); 130 } 131 } catch (PackageManager.NameNotFoundException e) { 132 Log.wtf(TAG, "Failed to load resources", e); 133 } 134 return null; 135 } 136 137 /** 138 * Retrieve assets held in the wifi resources APK. 139 */ 140 @Override getAssets()141 public AssetManager getAssets() { 142 if (mWifiAssetsFromApk == null) { 143 Context resourcesApkContext = getResourcesApkContext(); 144 if (resourcesApkContext != null) { 145 mWifiAssetsFromApk = resourcesApkContext.getAssets(); 146 } 147 } 148 return mWifiAssetsFromApk; 149 } 150 151 /** 152 * Retrieve resources held in the wifi resources APK. 153 */ 154 @Override getResources()155 public Resources getResources() { 156 if (mWifiResourcesFromApk == null) { 157 Context resourcesApkContext = getResourcesApkContext(); 158 if (resourcesApkContext != null) { 159 mWifiResourcesFromApk = resourcesApkContext.getResources(); 160 } 161 } 162 return mWifiResourcesFromApk; 163 } 164 165 /** 166 * Retrieve theme held in the wifi resources APK. 167 */ 168 @Override getTheme()169 public Resources.Theme getTheme() { 170 if (mWifiThemeFromApk == null) { 171 Context resourcesApkContext = getResourcesApkContext(); 172 if (resourcesApkContext != null) { 173 mWifiThemeFromApk = resourcesApkContext.getTheme(); 174 } 175 } 176 return mWifiThemeFromApk; 177 } 178 179 /** Get the package name that service-wifi runs under. */ getServiceWifiPackageName()180 public String getServiceWifiPackageName() { 181 return SERVICE_WIFI_PACKAGE_NAME; 182 } 183 184 /** 185 * Reset the resource cache which will cause it to be reloaded next time it is accessed. 186 */ resetResourceCache()187 public void resetResourceCache() { 188 mWifiOverlayApkPkgName = null; 189 mWifiAssetsFromApk = null; 190 mWifiResourcesFromApk = null; 191 mWifiThemeFromApk = null; 192 } 193 194 /** 195 * Returns an instance of WifiStringResourceWrapper with the given subId and carrierId. 196 */ getStringResourceWrapper(int subId, int carrierId)197 public WifiStringResourceWrapper getStringResourceWrapper(int subId, int carrierId) { 198 return new WifiStringResourceWrapper(this, subId, carrierId); 199 } 200 } 201