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 | PackageManager.MATCH_DISABLED_COMPONENTS); 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 int enabledState = getPackageManager().getApplicationEnabledSetting(mUwbOverlayApkPkgName); 90 if (enabledState != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) { 91 Log.w(TAG, "Uwb resources APK disabled, state: " + enabledState 92 + ". Trying to re-enable"); 93 getPackageManager().setApplicationEnabledSetting( 94 mUwbOverlayApkPkgName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0); 95 } 96 return mUwbOverlayApkPkgName; 97 } 98 getResourcesApkContext()99 private Context getResourcesApkContext() { 100 try { 101 return createPackageContext(getUwbOverlayApkPkgName(), 0); 102 } catch (PackageManager.NameNotFoundException e) { 103 Log.wtf(TAG, "Failed to load resources", e); 104 } 105 return null; 106 } 107 108 /** 109 * Retrieve assets held in the uwb resources APK. 110 */ 111 @Override getAssets()112 public AssetManager getAssets() { 113 if (mUwbAssetsFromApk == null) { 114 Context resourcesApkContext = getResourcesApkContext(); 115 if (resourcesApkContext != null) { 116 mUwbAssetsFromApk = resourcesApkContext.getAssets(); 117 } 118 } 119 return mUwbAssetsFromApk; 120 } 121 122 /** 123 * Retrieve resources held in the uwb resources APK. 124 */ 125 @Override getResources()126 public Resources getResources() { 127 if (mUwbResourcesFromApk == null) { 128 Context resourcesApkContext = getResourcesApkContext(); 129 if (resourcesApkContext != null) { 130 mUwbResourcesFromApk = resourcesApkContext.getResources(); 131 } 132 } 133 return mUwbResourcesFromApk; 134 } 135 136 /** 137 * Retrieve theme held in the uwb resources APK. 138 */ 139 @Override getTheme()140 public Resources.Theme getTheme() { 141 if (mUwbThemeFromApk == null) { 142 Context resourcesApkContext = getResourcesApkContext(); 143 if (resourcesApkContext != null) { 144 mUwbThemeFromApk = resourcesApkContext.getTheme(); 145 } 146 } 147 return mUwbThemeFromApk; 148 } 149 150 /** Get the package name that service-uwb runs under. */ getServiceUwbPackageName()151 public String getServiceUwbPackageName() { 152 return SERVICE_UWB_PACKAGE_NAME; 153 } 154 } 155