Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3d9fc0f9 authored by Charles Chen's avatar Charles Chen
Browse files

Show class name in error message

Show class name instead of service name in error message
Context#getSystemService to make developers easier to debug.

Bug: 150632074
Test: manual - check error log
Change-Id: Icb00c972c3a4fd4c71383dd5da29d4864bb06379
parent 86e27477
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -1899,7 +1899,8 @@ 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
            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. "
+16 −0
Original line number Diff line number Diff line
@@ -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);
    }

    /**