Merge "Show class name in error message" into rvc-dev

This commit is contained in:
Charles Chen
2020-03-16 11:44:03 +00:00
committed by Android (Google) Code Review
2 changed files with 19 additions and 2 deletions

View File

@@ -1899,8 +1899,9 @@ class ContextImpl extends Context {
public Object getSystemService(String name) {
// Check incorrect Context usage.
if (isUiComponent(name) && !isUiContext() && vmIncorrectContextUseEnabled()) {
final String errorMessage = "Tried to access visual service " + name
+ " from a non-visual Context.";
final String errorMessage = "Tried to access visual service "
+ SystemServiceRegistry.getSystemServiceClassName(name)
+ " from a non-visual Context. ";
final String message = "Visual services, such as WindowManager, WallpaperService or "
+ "LayoutInflater should be accessed from Activity or other visual Context. "
+ "Use an Activity or a Context created with "

View File

@@ -19,6 +19,7 @@ package android.app;
import android.accounts.AccountManager;
import android.accounts.IAccountManager;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.app.ContextImpl.ServiceInitializationState;
import android.app.admin.DevicePolicyManager;
@@ -227,6 +228,8 @@ public final class SystemServiceRegistry {
new ArrayMap<Class<?>, String>();
private static final Map<String, ServiceFetcher<?>> SYSTEM_SERVICE_FETCHERS =
new ArrayMap<String, ServiceFetcher<?>>();
private static final Map<String, String> SYSTEM_SERVICE_CLASS_NAMES = new ArrayMap<>();
private static int sServiceCacheSize;
private static volatile boolean sInitializing;
@@ -1389,6 +1392,19 @@ public final class SystemServiceRegistry {
@NonNull Class<T> serviceClass, @NonNull ServiceFetcher<T> serviceFetcher) {
SYSTEM_SERVICE_NAMES.put(serviceClass, serviceName);
SYSTEM_SERVICE_FETCHERS.put(serviceName, serviceFetcher);
SYSTEM_SERVICE_CLASS_NAMES.put(serviceName, serviceClass.getSimpleName());
}
/**
* Returns system service class name by system service name. This method is mostly an inverse of
* {@link #getSystemServiceName(Class)}
*
* @return system service class name. {@code null} if service name is invalid.
* @hide
*/
@Nullable
public static String getSystemServiceClassName(@NonNull String name) {
return SYSTEM_SERVICE_CLASS_NAMES.get(name);
}
/**