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 com.android.server.uwb; 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.util.Log; 28 29 import java.util.List; 30 import java.util.stream.Collectors; 31 32 /** 33 * Wrapper for context to override getResources method. Resources for uwb mainline jar needs to be 34 * fetched from the resources APK. 35 */ 36 public class UwbContext extends ContextWrapper { 37 private static final String TAG = "UwbContext"; 38 /** Intent action that is used to identify ServiceUwbResources.apk */ 39 private static final String ACTION_RESOURCES_APK = 40 "com.android.server.uwb.intent.action.SERVICE_UWB_RESOURCES_APK"; 41 42 /** Since service-uwb runs within system_server, its package name is "android". */ 43 private static final String SERVICE_UWB_PACKAGE_NAME = "android"; 44 45 private String mUwbOverlayApkPkgName; 46 47 // Cached resources from the resources APK. 48 private AssetManager mUwbAssetsFromApk; 49 private Resources mUwbResourcesFromApk; 50 private Resources.Theme mUwbThemeFromApk; 51 UwbContext(@onNull Context contextBase)52 public UwbContext(@NonNull Context contextBase) { 53 super(contextBase); 54 } 55 56 /** Get the package name of ServiceUwbResources.apk */ getUwbOverlayApkPkgName()57 public String getUwbOverlayApkPkgName() { 58 if (mUwbOverlayApkPkgName != null) { 59 return mUwbOverlayApkPkgName; 60 } 61 62 List<ResolveInfo> resolveInfos = getPackageManager().queryIntentActivities( 63 new Intent(ACTION_RESOURCES_APK), 64 PackageManager.MATCH_SYSTEM_ONLY); 65 66 // remove apps that don't live in the Uwb apex 67 resolveInfos.removeIf(info -> 68 !UwbInjector.isAppInUwbApex(info.activityInfo.applicationInfo)); 69 70 if (resolveInfos.isEmpty()) { 71 // Resource APK not loaded yet, print a stack trace to see where this is called from 72 Log.e(TAG, "Attempted to fetch resources before Uwb Resources APK is loaded!", 73 new IllegalStateException()); 74 return null; 75 } 76 77 if (resolveInfos.size() > 1) { 78 // multiple apps found, log a warning, but continue 79 Log.w(TAG, "Found > 1 APK that can resolve Uwb Resources APK intent: " 80 + resolveInfos.stream() 81 .map(info -> info.activityInfo.applicationInfo.packageName) 82 .collect(Collectors.joining(", "))); 83 } 84 85 // Assume the first ResolveInfo is the one we're looking for 86 ResolveInfo info = resolveInfos.get(0); 87 mUwbOverlayApkPkgName = info.activityInfo.applicationInfo.packageName; 88 Log.i(TAG, "Found Uwb Resources APK at: " + mUwbOverlayApkPkgName); 89 return mUwbOverlayApkPkgName; 90 } 91 getResourcesApkContext()92 private Context getResourcesApkContext() { 93 try { 94 return createPackageContext(getUwbOverlayApkPkgName(), 0); 95 } catch (PackageManager.NameNotFoundException e) { 96 Log.wtf(TAG, "Failed to load resources", e); 97 } 98 return null; 99 } 100 101 /** 102 * Retrieve assets held in the uwb resources APK. 103 */ 104 @Override getAssets()105 public AssetManager getAssets() { 106 if (mUwbAssetsFromApk == null) { 107 Context resourcesApkContext = getResourcesApkContext(); 108 if (resourcesApkContext != null) { 109 mUwbAssetsFromApk = resourcesApkContext.getAssets(); 110 } 111 } 112 return mUwbAssetsFromApk; 113 } 114 115 /** 116 * Retrieve resources held in the uwb resources APK. 117 */ 118 @Override getResources()119 public Resources getResources() { 120 if (mUwbResourcesFromApk == null) { 121 Context resourcesApkContext = getResourcesApkContext(); 122 if (resourcesApkContext != null) { 123 mUwbResourcesFromApk = resourcesApkContext.getResources(); 124 } 125 } 126 return mUwbResourcesFromApk; 127 } 128 129 /** 130 * Retrieve theme held in the uwb resources APK. 131 */ 132 @Override getTheme()133 public Resources.Theme getTheme() { 134 if (mUwbThemeFromApk == null) { 135 Context resourcesApkContext = getResourcesApkContext(); 136 if (resourcesApkContext != null) { 137 mUwbThemeFromApk = resourcesApkContext.getTheme(); 138 } 139 } 140 return mUwbThemeFromApk; 141 } 142 143 /** Get the package name that service-uwb runs under. */ getServiceUwbPackageName()144 public String getServiceUwbPackageName() { 145 return SERVICE_UWB_PACKAGE_NAME; 146 } 147 } 148