Search code examples
androidandroid-source

What code is running when I call getSystemService() from my activity?


I'm trying to trace AOSP code from the grepcode site.

When I call getSystemService(Context.WIFI_P2P_SERVICE), it gets to the following code:

 @Override public Object getSystemService(String name) {
    if (getBaseContext() == null) {
        throw new IllegalStateException(
                "System services not available to Activities before onCreate()");
    }
    if (WINDOW_SERVICE.equals(name)) {
        return mWindowManager;
    } else if (SEARCH_SERVICE.equals(name)) {
        ensureSearchManager();
        return mSearchManager;
    }
    return super.getSystemService(name);
}

And since WIFI_P2P_SERVICE declared as public static final String WIFI_P2P_SERVICE = "wifip2p";, if will not fall in one of the conditions and will go to the super.getSystemService(name);

Activity extends ContextThemeWrapper, the code there is:
 @Override public Object getSystemService(String name) {
        if (LAYOUT_INFLATER_SERVICE.equals(name)) {
            if (mInflater == null) {
                mInflater = LayoutInflater.from(mBase).cloneInContext(this);
            }
            return mInflater;
        }
        return mBase.getSystemService(name);
    }

Here also, the required service name will not match, mBase is an instance of Context so the code in Context is:

public abstract Object getSystemService(String name);

which means that classes which extends from it must handle that functionality. Well, Where my request is being treated?


Solution

  • As far as i know the implementation code of Context is under the package android.app with class name ContextImpl

    Here is getSystemService from that class -

    @Override
    public Object getSystemService(String name) {
        ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
        return fetcher == null ? null : fetcher.getService(this);
    }
    

    Edit - The entry point for WIFI_P2P_SERVICE -

    registerService(WIFI_P2P_SERVICE, new ServiceFetcher() {
        public Object createService(ContextImpl ctx) {
              IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE);
              IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
              return new WifiP2pManager(service);
            }});