Android System Server进程源码分析 下

一 System Server

     上篇分析了Zygote创建SystemServer的流程,本篇则分析SystemServer的实际工作内容。

1.0 main

public static void main(String[] args) {     new SystemServer().run(); // 先new一个SystemServer对象,然后调用它的run()方法 }

Java中start方法与run方法的区别: 调用start方法会创建一个新线程,并且run方法中的代码在新的线程上运行。 调用run方法的不会创建新线程,run方法中的代码继续在当前线程上运行。

1.1 run

private void run() {
    try {
        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “InitBeforeStartServices”); // 启动Systrace追踪InitBeforeStartServices
        // If a device’s clock is before 1970 (before 0), a lot of
        // APIs crash dealing with negative numbers, notably
        // java.io.File#setLastModified, so instead we fake it and
        // hope that time from cell towers or NTP fixes it shortly.
        if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) { // 类UNIX系统时间最早从1970开始计算
            Slog.w(TAG, “System clock is before 1970; setting to 1970.”);
            SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
        }


        // If the system has “persist.sys.language” and friends set, replace them with
        // “persist.sys.locale”. Note that the default locale at this point is calculated
        // using the “-Duser.locale” command line flag. That flag is usually populated by
        // AndroidRuntime using the same set of system properties, but only the system_server
        // and system apps are allowed to set them.
        //
        // NOTE: Most changes made here will need an equivalent change to
        // core/jni/AndroidRuntime.cpp
        if (!SystemProperties.get(“persist.sys.language”).isEmpty()) {
            final String languageTag = Locale.getDefault().toLanguageTag(); // 本地化


            SystemProperties.set(“persist.sys.locale”, languageTag); // persist.sys.locale=zh-CN


            SystemProperties.set(“persist.sys.language”, “”);
            SystemProperties.set(“persist.sys.country”, “”);
            SystemProperties.set(“persist.sys.localevar”, “”);
        }


        // Here we go!
        Slog.i(TAG, “Entered the Android system server!”);
        EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, SystemClock.uptimeMillis()); // Android时间日志


        // In case the runtime switched since last boot (such as when
        // the old runtime was removed in an OTA), set the system
        // property so that it is in sync. We can’t do this in
        // libnativehelper’s JniInvocation::Init code where we already
        // had to fallback to a different runtime because it is
        // running as root and we need to be the system user to set
        // the property. http://b/11463182
        SystemProperties.set(“persist.sys.dalvik.vm.lib.2”, VMRuntime.getRuntime().vmLibrary()); // 虚拟机库文件 // VMRuntime_vmLibrary返回libart.so


        // Enable the sampling profiler.
        if (SamplingProfilerIntegration.isEnabled()) {
            SamplingProfilerIntegration.start(); // 启动SamplingProfilerIntegration进行性能统计
            mProfilerSnapshotTimer = new Timer();
            mProfilerSnapshotTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        SamplingProfilerIntegration.writeSnapshot(“system_server”, null);
                    }
                }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL); // 每隔1小时调用一次,将结果保存到/data/snapshots/system_server
        }


        // Mmmmmm… more memory!
        VMRuntime.getRuntime().clearGrowthLimit();   // 清除虚拟机运行时内存增长上限


        // The system server has to run all of the time, so it needs to be
        // as efficient as possible with its memory usage.
        VMRuntime.getRuntime().setTargetHeapUtilization(0.8f); // 设置虚拟机运行时的内存堆的利用率为0.8


        // Some devices rely on runtime fingerprint generation, so make sure
        // we’ve defined it before booting further.
        Build.ensureFingerprintProperty(); // 设置ro.build.fingerprint属性值


        // Within the system server, it is an error to access Environment paths without
        // explicitly specifying a user.
        Environment.setUserRequired(true); // 设置必须指定用户后才可以
访问环境变量


        // Ensure binder calls into the system always run at foreground priority.
        BinderInternal.disableBackgroundScheduling(true); // 确保binder系统调用,总是运行在前台优先级


        // Prepare the main looper thread (this thread).
        android.os.Process.setThreadPriority(
            android.os.Process.THREAD_PRIORITY_FOREGROUND); // 设置system_server进程的优先级为前台优先级
        android.os.Process.setCanSelfBackground(false); // 禁止system_server自行将自己设为后台进程
        Looper.prepareMainLooper(); // 创建主线程Looper


        // Initialize native services.
        System.loadLibrary(“android_servers”); //加载android_servers.so动态库


        // Check whether we failed to shut down last time we tried.
        // This call may not return.
        performPendingShutdown(); // 获取sys.shutdown.requested属性,判断是否需要关机或重启,如果是的话则执行相应操作


        // Initialize the system context.
        createSystemContext(); // 初始化系统上下文


        // Create the system service manager.
        mSystemServiceManager = new SystemServiceManager(mSystemContext); // 创建系统服务管理对象
        LocalServices.addService(SystemServiceManager.class, mSystemServiceManager); // //将mSystemServiceManager添加到本地服务的ArrayMap中
    } finally {
        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); // 停止Systrace追踪InitBeforeStartServices
    }


    // Start services.
    try {
        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “StartServices”); // 启动Systrace追踪StartServices
        startBootstrapServices(); // 启动引导服务
        startCoreServices(); // 启动核心服务
        startOtherServices(); // 启动其他服务
    } catch (Throwable ex) {
        Slog.e(“System”, “******************************************”);
        Slog.e(“System”, “************ Failure starting system services”, ex);
        throw ex;
    } finally {
        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); // 停止Systrace追踪StartServices
    }


    // For debug builds, log event loop stalls to dropbox for analysis.
    if (StrictMode.conditionallyEnableDebugLogging()) { // 严格模式,用于调试
        Slog.i(TAG, “Enabled StrictMode for system server main thread.”);
    }


    // Loop forever.
    Looper.loop(); // 一直循环,获取并处理消息
    throw new RuntimeException(“Main thread loop unexpectedly exited”);
}

1.3 createSystemContext

private void createSystemContext() {     ActivityThread activityThread = ActivityThread.systemMain(); // 创建ActivityThread对象     mSystemContext = activityThread.getSystemContext(); // 创建并获取ContextImpl     mSystemContext.setTheme(android.R.style.Theme_DeviceDefault_Light_DarkActionBar); // 设置系统主题 }

1.4 startBootstrapServices

private void startBootstrapServices() {     // Wait for installd to finish starting up so that it has a chance to     // create critical directories such as /data/user with the appropriate     // permissions.  We need this to complete before we initialize other services.     Installer installer = mSystemServiceManager.startService(Installer.class); // 启动Installer系统服务,并等待和installd建立socket连接

    // Activity manager runs the show.     mActivityManagerService = mSystemServiceManager.startService(             ActivityManagerService.Lifecycle.class).getService(); //启动ActivityManagerService系统服务     mActivityManagerService.setSystemServiceManager(mSystemServiceManager); // 设置ActivityManagerService的SystemServiceManager     mActivityManagerService.setInstaller(installer); // 设置ActivityManagerService的Installer

    // Power manager needs to be started early because other services need it.     // Native daemons may be watching for it to be registered so it must be ready     // to handle incoming binder calls immediately (including being able to verify     // the permissions for those calls).     mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class); // 启动PowerManagerService系统服务

    // Now that the power manager has been started, let the activity manager     // initialize power management features.     Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “InitPowerManagement”);     mActivityManagerService.initPowerManagement(); // 初始化PowerManagement     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

    // Manages LEDs and display backlight so we need it to bring up the display.     mSystemServiceManager.startService(LightsService.class); // 启动LightsService系统服务

    // Display manager is needed to provide display metrics before package manager     // starts up.     mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class); // 启动DisplayManagerService系统服务

    // We need the default display before we can initialize the package manager.     mSystemServiceManager.startBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY); // 等待Display有默认显示 // 调用DisplayManagerService的onBootPhase方法

    // Only run “core” apps if we’re encrypting the device.     String cryptState = SystemProperties.get(“vold.decrypt”); // 当设备正在加密时,仅运行核心应用程序     if (ENCRYPTING_STATE.equals(cryptState)) {         Slog.w(TAG, “Detected encryption in progress – only parsing core apps”);         mOnlyCore = true;     } else if (ENCRYPTED_STATE.equals(cryptState)) {         Slog.w(TAG, “Device encrypted – only parsing core apps”);         mOnlyCore = true;     }

    // Start the package manager.     traceBeginAndSlog(“StartPackageManagerService”);     mPackageManagerService = PackageManagerService.main(mSystemContext, installer,             mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore); // 启动PackageManagerService系统服务     mFirstBoot = mPackageManagerService.isFirstBoot(); // 是否是第一次开机     mPackageManager = mSystemContext.getPackageManager();     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

    traceBeginAndSlog(“StartUserManagerService”);     ServiceManager.addService(Context.USER_SERVICE, UserManagerService.getInstance());  // 启动UserManagerService系统服务,并将其注册到ServiceManager     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

    // Initialize attribute cache used to cache resources from packages.     AttributeCache.init(mSystemContext); // 初始化属性缓存

    // Set up the Application instance for the system process and get started.     mActivityManagerService.setSystemProcess(); // 先将ActivityManagerService注册到ServiceManager,然后向SystemManager注册几个其他服务,最后创建了一个代表system_server的ProcessRecord对象

    // The sensor service needs access to package manager service, app ops     // service, and permissions service, therefore we start it after them.     startSensorService();  // 启动传感器系统服务 }

1.5 startCoreServices

private void startCoreServices() {     // Tracks the battery level.  Requires LightService.     mSystemServiceManager.startService(BatteryService.class); // 启动BatteryService系统服务,用于统计电池电量信息

    // Tracks application usage stats.     mSystemServiceManager.startService(UsageStatsService.class); // 启动UsageStatsService系统服务,用于统计应用使用情况     mActivityManagerService.setUsageStatsManager(             LocalServices.getService(UsageStatsManagerInternal.class));     // Update after UsageStatsService is available, needed before performBootDexOpt.     mPackageManagerService.getUsageStatsIfNoPackageUsageInfo();

    // Tracks whether the updatable WebView is in a ready state and watches for update installs.     mSystemServiceManager.startService(WebViewUpdateService.class); // 启动WebViewUpdateService系统服务 }

1.6 startOtherServices

启动一系列的服务,过于繁杂就不分析了。 private void startOtherServices() {     final Context context = mSystemContext;     AccountManagerService accountManager = null;     ContentService contentService = null;     VibratorService vibrator = null;     IAlarmManager alarm = null;     IMountService mountService = null;     NetworkManagementService networkManagement = null;     NetworkStatsService networkStats = null;     NetworkPolicyManagerService networkPolicy = null;     ConnectivityService connectivity = null;     NetworkScoreService networkScore = null;     NsdService serviceDiscovery= null;     WindowManagerService wm = null;     UsbService usb = null;     SerialService serial = null;     NetworkTimeUpdateService networkTimeUpdater = null;     CommonTimeManagementService commonTimeMgmtService = null;     InputManagerService inputManager = null;     TelephonyRegistry telephonyRegistry = null;     ConsumerIrService consumerIr = null;     AudioService audioService = null;     MmsServiceBroker mmsService = null;     EntropyMixer entropyMixer = null;     CameraService cameraService = null;

    boolean disableStorage = SystemProperties.getBoolean(“config.disable_storage”, false);     boolean disableBluetooth = SystemProperties.getBoolean(“config.disable_bluetooth”, false);     boolean disableLocation = SystemProperties.getBoolean(“config.disable_location”, false);     boolean disableSystemUI = SystemProperties.getBoolean(“config.disable_systemui”, false);     boolean disableNonCoreServices = SystemProperties.getBoolean(“config.disable_noncore”, false);     boolean disableNetwork = SystemProperties.getBoolean(“config.disable_network”, false);     boolean disableNetworkTime = SystemProperties.getBoolean(“config.disable_networktime”, false);     boolean isEmulator = SystemProperties.get(“ro.kernel.qemu”).equals(“1”);

    try {         Slog.i(TAG, “Reading configuration…”);         SystemConfig.getInstance();

        traceBeginAndSlog(“StartSchedulingPolicyService”);         ServiceManager.addService(“scheduling_policy”, new SchedulingPolicyService());         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        mSystemServiceManager.startService(TelecomLoaderService.class);

        traceBeginAndSlog(“StartTelephonyRegistry”);         telephonyRegistry = new TelephonyRegistry(context);         ServiceManager.addService(“telephony.registry”, telephonyRegistry);         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        traceBeginAndSlog(“StartEntropyMixer”);         entropyMixer = new EntropyMixer(context);         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        mContentResolver = context.getContentResolver();

        Slog.i(TAG, “Camera Service”);         mSystemServiceManager.startService(CameraService.class);

        // The AccountManager must come before the ContentService         traceBeginAndSlog(“StartAccountManagerService”);         try {             // TODO: seems like this should be disable-able, but req’d by ContentService             accountManager = new AccountManagerService(context);             ServiceManager.addService(Context.ACCOUNT_SERVICE, accountManager);         } catch (Throwable e) {             Slog.e(TAG, “Failure starting Account Manager”, e);         }         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        traceBeginAndSlog(“StartContentService”);         contentService = ContentService.main(context,                 mFactoryTestMode == FactoryTest.FACTORY_TEST_LOW_LEVEL);         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        traceBeginAndSlog(“InstallSystemProviders”);         mActivityManagerService.installSystemProviders();         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        traceBeginAndSlog(“StartVibratorService”);         vibrator = new VibratorService(context);         ServiceManager.addService(“vibrator”, vibrator);         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        traceBeginAndSlog(“StartConsumerIrService”);         consumerIr = new ConsumerIrService(context);         ServiceManager.addService(Context.CONSUMER_IR_SERVICE, consumerIr);         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        mSystemServiceManager.startService(AlarmManagerService.class);         alarm = IAlarmManager.Stub.asInterface(                 ServiceManager.getService(Context.ALARM_SERVICE));

        traceBeginAndSlog(“InitWatchdog”);         final Watchdog watchdog = Watchdog.getInstance();         watchdog.init(context, mActivityManagerService);         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        traceBeginAndSlog(“StartInputManagerService”);         inputManager = new InputManagerService(context);         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        traceBeginAndSlog(“StartWindowManagerService”);         wm = WindowManagerService.main(context, inputManager,                 mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,                 !mFirstBoot, mOnlyCore);         ServiceManager.addService(Context.WINDOW_SERVICE, wm);         ServiceManager.addService(Context.INPUT_SERVICE, inputManager);         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        mActivityManagerService.setWindowManager(wm);

        inputManager.setWindowManagerCallbacks(wm.getInputMonitor());         inputManager.start();

        // TODO: Use service dependencies instead.         mDisplayManagerService.windowManagerAndInputReady();

        // Skip Bluetooth if we have an emulator kernel         // TODO: Use a more reliable check to see if this product should         // support Bluetooth – see bug 988521         if (isEmulator) {             Slog.i(TAG, “No Bluetooth Service (emulator)”);         } else if (mFactoryTestMode == FactoryTest.FACTORY_TEST_LOW_LEVEL) {             Slog.i(TAG, “No Bluetooth Service (factory test)”);         } else if (!context.getPackageManager().hasSystemFeature                    (PackageManager.FEATURE_BLUETOOTH)) {             Slog.i(TAG, “No Bluetooth Service (Bluetooth Hardware Not Present)”);         } else if (disableBluetooth) {             Slog.i(TAG, “Bluetooth Service disabled by config”);         } else {             mSystemServiceManager.startService(BluetoothService.class);         }     } catch (RuntimeException e) {         Slog.e(“System”, “******************************************”);         Slog.e(“System”, “************ Failure starting core service”, e);     }

    StatusBarManagerService statusBar = null;     INotificationManager notification = null;     InputMethodManagerService imm = null;     WallpaperManagerService wallpaper = null;     LocationManagerService location = null;     CountryDetectorService countryDetector = null;     TextServicesManagerService tsms = null;     LockSettingsService lockSettings = null;     AssetAtlasService atlas = null;     MediaRouterService mediaRouter = null;

    // Bring up services needed for UI.     if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {         traceBeginAndSlog(“StartInputMethodManagerService”);         try {             imm = new InputMethodManagerService(context, wm);             ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);         } catch (Throwable e) {             reportWtf(“starting Input Manager Service”, e);         }         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        traceBeginAndSlog(“StartAccessibilityManagerService”);         try {             ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,                     new AccessibilityManagerService(context));         } catch (Throwable e) {             reportWtf(“starting Accessibility Manager”, e);         }         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);     }

    try {         wm.displayReady();     } catch (Throwable e) {         reportWtf(“making display ready”, e);     }

    if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {         if (!disableStorage &&             !”0″.equals(SystemProperties.get(“system_init.startmountservice”))) {             try {                 /*                  * NotificationManagerService is dependant on MountService,                  * (for media / usb notifications) so we must start MountService first.                  */                 mSystemServiceManager.startService(MOUNT_SERVICE_CLASS);                 mountService = IMountService.Stub.asInterface(                         ServiceManager.getService(“mount”));             } catch (Throwable e) {                 reportWtf(“starting Mount Service”, e);             }         }     }

    // We start this here so that we update our configuration to set watch or television     // as appropriate.     mSystemServiceManager.startService(UiModeManagerService.class);

    Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “PerformBootDexOpt”);     try {         mPackageManagerService.performBootDexOpt();     } catch (Throwable e) {         reportWtf(“performing boot dexopt”, e);     }     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

    try {         ActivityManagerNative.getDefault().showBootMessage(                 context.getResources().getText(                         com.android.internal.R.string.android_upgrading_starting_apps),                 false);     } catch (RemoteException e) {     }

    if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {         if (!disableNonCoreServices) {             traceBeginAndSlog(“StartLockSettingsService”);             try {                 lockSettings = new LockSettingsService(context);                 ServiceManager.addService(“lock_settings”, lockSettings);             } catch (Throwable e) {                 reportWtf(“starting LockSettingsService service”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

            if (!SystemProperties.get(PERSISTENT_DATA_BLOCK_PROP).equals(“”)) {                 mSystemServiceManager.startService(PersistentDataBlockService.class);             }

            mSystemServiceManager.startService(DeviceIdleController.class);

            // Always start the Device Policy Manager, so that the API is compatible with             // API8.             mSystemServiceManager.startService(DevicePolicyManagerService.Lifecycle.class);         }

        if (!disableSystemUI) {             traceBeginAndSlog(“StartStatusBarManagerService”);             try {                 statusBar = new StatusBarManagerService(context, wm);                 ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);             } catch (Throwable e) {                 reportWtf(“starting StatusBarManagerService”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        if (!disableNonCoreServices) {             traceBeginAndSlog(“StartClipboardService”);             try {                 ServiceManager.addService(Context.CLIPBOARD_SERVICE,                         new ClipboardService(context));             } catch (Throwable e) {                 reportWtf(“starting Clipboard Service”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        if (!disableNetwork) {             traceBeginAndSlog(“StartNetworkManagementService”);             try {                 networkManagement = NetworkManagementService.create(context);                 ServiceManager.addService(Context.NETWORKMANAGEMENT_SERVICE, networkManagement);             } catch (Throwable e) {                 reportWtf(“starting NetworkManagement Service”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        if (!disableNonCoreServices) {             traceBeginAndSlog(“StartTextServicesManagerService”);             try {                 tsms = new TextServicesManagerService(context);                 ServiceManager.addService(Context.TEXT_SERVICES_MANAGER_SERVICE, tsms);             } catch (Throwable e) {                 reportWtf(“starting Text Service Manager Service”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        if (!disableNetwork) {             traceBeginAndSlog(“StartNetworkScoreService”);             try {                 networkScore = new NetworkScoreService(context);                 ServiceManager.addService(Context.NETWORK_SCORE_SERVICE, networkScore);             } catch (Throwable e) {                 reportWtf(“starting Network Score Service”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

            traceBeginAndSlog(“StartNetworkStatsService”);             try {                 networkStats = new NetworkStatsService(context, networkManagement, alarm);                 ServiceManager.addService(Context.NETWORK_STATS_SERVICE, networkStats);             } catch (Throwable e) {                 reportWtf(“starting NetworkStats Service”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

            traceBeginAndSlog(“StartNetworkPolicyManagerService”);             try {                 networkPolicy = new NetworkPolicyManagerService(                         context, mActivityManagerService,                         (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE),                         networkStats, networkManagement);                 ServiceManager.addService(Context.NETWORK_POLICY_SERVICE, networkPolicy);             } catch (Throwable e) {                 reportWtf(“starting NetworkPolicy Service”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

            mSystemServiceManager.startService(WIFI_P2P_SERVICE_CLASS);             mSystemServiceManager.startService(WIFI_SERVICE_CLASS);             mSystemServiceManager.startService(                         “com.android.server.wifi.WifiScanningService”);

            mSystemServiceManager.startService(“com.android.server.wifi.RttService”);

            if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_ETHERNET) ||                 mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)) {                 mSystemServiceManager.startService(ETHERNET_SERVICE_CLASS);             }

            traceBeginAndSlog(“StartConnectivityService”);             try {                 connectivity = new ConnectivityService(                         context, networkManagement, networkStats, networkPolicy);                 ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);                 networkStats.bindConnectivityManager(connectivity);                 networkPolicy.bindConnectivityManager(connectivity);             } catch (Throwable e) {                 reportWtf(“starting Connectivity Service”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

            traceBeginAndSlog(“StartNsdService”);             try {                 serviceDiscovery = NsdService.create(context);                 ServiceManager.addService(                         Context.NSD_SERVICE, serviceDiscovery);             } catch (Throwable e) {                 reportWtf(“starting Service Discovery Service”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        if (!disableNonCoreServices) {             traceBeginAndSlog(“StartUpdateLockService”);             try {                 ServiceManager.addService(Context.UPDATE_LOCK_SERVICE,                         new UpdateLockService(context));             } catch (Throwable e) {                 reportWtf(“starting UpdateLockService”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        /*          * MountService has a few dependencies: Notification Manager and          * AppWidget Provider. Make sure MountService is completely started          * first before continuing.          */         if (mountService != null && !mOnlyCore) {             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “WaitForAsecScan”);             try {                 mountService.waitForAsecScan();             } catch (RemoteException ignored) {             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakeAccountManagerServiceReady”);         try {             if (accountManager != null)                 accountManager.systemReady();         } catch (Throwable e) {             reportWtf(“making Account Manager Service ready”, e);         }         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakeContentServiceReady”);         try {             if (contentService != null)                 contentService.systemReady();         } catch (Throwable e) {             reportWtf(“making Content Service ready”, e);         }         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        mSystemServiceManager.startService(NotificationManagerService.class);         notification = INotificationManager.Stub.asInterface(                 ServiceManager.getService(Context.NOTIFICATION_SERVICE));         networkPolicy.bindNotificationManager(notification);

        mSystemServiceManager.startService(DeviceStorageMonitorService.class);

        if (!disableLocation) {             traceBeginAndSlog(“StartLocationManagerService”);             try {                 location = new LocationManagerService(context);                 ServiceManager.addService(Context.LOCATION_SERVICE, location);             } catch (Throwable e) {                 reportWtf(“starting Location Manager”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

            traceBeginAndSlog(“StartCountryDetectorService”);             try {                 countryDetector = new CountryDetectorService(context);                 ServiceManager.addService(Context.COUNTRY_DETECTOR, countryDetector);             } catch (Throwable e) {                 reportWtf(“starting Country Detector”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        if (!disableNonCoreServices) {             traceBeginAndSlog(“StartSearchManagerService”);             try {                 ServiceManager.addService(Context.SEARCH_SERVICE,                         new SearchManagerService(context));             } catch (Throwable e) {                 reportWtf(“starting Search Service”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        traceBeginAndSlog(“StartDropBoxManagerService”);         try {             ServiceManager.addService(Context.DROPBOX_SERVICE,                     new DropBoxManagerService(context, new File(“/data/system/dropbox”)));         } catch (Throwable e) {             reportWtf(“starting DropBoxManagerService”, e);         }         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        if (!disableNonCoreServices && context.getResources().getBoolean(                     R.bool.config_enableWallpaperService)) {             traceBeginAndSlog(“StartWallpaperManagerService”);             try {                 wallpaper = new WallpaperManagerService(context);                 ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);             } catch (Throwable e) {                 reportWtf(“starting Wallpaper Service”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        traceBeginAndSlog(“StartAudioService”);         try {             audioService = new AudioService(context);             ServiceManager.addService(Context.AUDIO_SERVICE, audioService);         } catch (Throwable e) {             reportWtf(“starting Audio Service”, e);         }         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        if (!disableNonCoreServices) {             mSystemServiceManager.startService(DockObserver.class);

            if (context.getPackageManager().hasSystemFeature                     (PackageManager.FEATURE_WATCH)) {                 mSystemServiceManager.startService(ThermalObserver.class);             }         }

        traceBeginAndSlog(“StartWiredAccessoryManager”);         try {             // Listen for wired headset changes             inputManager.setWiredAccessoryCallbacks(                     new WiredAccessoryManager(context, inputManager));         } catch (Throwable e) {             reportWtf(“starting WiredAccessoryManager”, e);         }         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        if (!disableNonCoreServices) {             if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_MIDI)) {                 // Start MIDI Manager service                 mSystemServiceManager.startService(MIDI_SERVICE_CLASS);             }

            if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)                     || mPackageManager.hasSystemFeature(                             PackageManager.FEATURE_USB_ACCESSORY)) {                 // Manage USB host and device support                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “StartUsbService”);                 mSystemServiceManager.startService(USB_SERVICE_CLASS);                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);             }

            traceBeginAndSlog(“StartSerialService”);             try {                 // Serial port support                 serial = new SerialService(context);                 ServiceManager.addService(Context.SERIAL_SERVICE, serial);             } catch (Throwable e) {                 Slog.e(TAG, “Failure starting SerialService”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        mSystemServiceManager.startService(TwilightService.class);

        mSystemServiceManager.startService(JobSchedulerService.class);

        if (!disableNonCoreServices) {             if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_BACKUP)) {                 mSystemServiceManager.startService(BACKUP_MANAGER_SERVICE_CLASS);             }

            if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_APP_WIDGETS)) {                 mSystemServiceManager.startService(APPWIDGET_SERVICE_CLASS);             }

            if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_VOICE_RECOGNIZERS)) {                 mSystemServiceManager.startService(VOICE_RECOGNITION_MANAGER_SERVICE_CLASS);             }

            if (GestureLauncherService.isGestureLauncherEnabled(context.getResources())) {                 Slog.i(TAG, “Gesture Launcher Service”);                 mSystemServiceManager.startService(GestureLauncherService.class);             }         }

        traceBeginAndSlog(“StartDiskStatsService”);         try {             ServiceManager.addService(“diskstats”, new DiskStatsService(context));         } catch (Throwable e) {             reportWtf(“starting DiskStats Service”, e);         }         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        traceBeginAndSlog(“StartSamplingProfilerService”);         try {             // need to add this service even if SamplingProfilerIntegration.isEnabled()             // is false, because it is this service that detects system property change and             // turns on SamplingProfilerIntegration. Plus, when sampling profiler doesn’t work,             // there is little overhead for running this service.             ServiceManager.addService(“samplingprofiler”,                         new SamplingProfilerService(context));         } catch (Throwable e) {             reportWtf(“starting SamplingProfiler Service”, e);         }         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        if (!disableNetwork && !disableNetworkTime) {             traceBeginAndSlog(“StartNetworkTimeUpdateService”);             try {                 networkTimeUpdater = new NetworkTimeUpdateService(context);             } catch (Throwable e) {                 reportWtf(“starting NetworkTimeUpdate service”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        traceBeginAndSlog(“StartCommonTimeManagementService”);         try {             commonTimeMgmtService = new CommonTimeManagementService(context);             ServiceManager.addService(“commontime_management”, commonTimeMgmtService);         } catch (Throwable e) {             reportWtf(“starting CommonTimeManagementService service”, e);         }         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        if (!disableNetwork) {             traceBeginAndSlog(“CertBlacklister”);             try {                 CertBlacklister blacklister = new CertBlacklister(context);             } catch (Throwable e) {                 reportWtf(“starting CertBlacklister”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        if (!disableNonCoreServices) {             // Dreams (interactive idle-time views, a/k/a screen savers, and doze mode)             mSystemServiceManager.startService(DreamManagerService.class);         }

        if (!disableNonCoreServices) {             traceBeginAndSlog(“StartAssetAtlasService”);             try {                 atlas = new AssetAtlasService(context);                 ServiceManager.addService(AssetAtlasService.ASSET_ATLAS_SERVICE, atlas);             } catch (Throwable e) {                 reportWtf(“starting AssetAtlasService”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }

        if (!disableNonCoreServices) {             ServiceManager.addService(GraphicsStatsService.GRAPHICS_STATS_SERVICE,                     new GraphicsStatsService(context));         }

        if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING)) {             mSystemServiceManager.startService(PRINT_MANAGER_SERVICE_CLASS);         }

        mSystemServiceManager.startService(RestrictionsManagerService.class);

        mSystemServiceManager.startService(MediaSessionService.class);

        if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_HDMI_CEC)) {             mSystemServiceManager.startService(HdmiControlService.class);         }

        if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_LIVE_TV)) {             mSystemServiceManager.startService(TvInputManagerService.class);         }

        if (!disableNonCoreServices) {             traceBeginAndSlog(“StartMediaRouterService”);             try {                 mediaRouter = new MediaRouterService(context);                 ServiceManager.addService(Context.MEDIA_ROUTER_SERVICE, mediaRouter);             } catch (Throwable e) {                 reportWtf(“starting MediaRouterService”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

            mSystemServiceManager.startService(TrustManagerService.class);

            mSystemServiceManager.startService(FingerprintService.class);

            traceBeginAndSlog(“StartBackgroundDexOptService”);             try {                 BackgroundDexOptService.schedule(context, 0);             } catch (Throwable e) {                 reportWtf(“starting BackgroundDexOptService”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

        }

        mSystemServiceManager.startService(LauncherAppsService.class);     }

    if (!disableNonCoreServices) {         mSystemServiceManager.startService(MediaProjectionManagerService.class);     }

    // Before things start rolling, be sure we have decided whether     // we are in safe mode.     final boolean safeMode = wm.detectSafeMode();     if (safeMode) {         mActivityManagerService.enterSafeMode();         // Disable the JIT for the system_server process         VMRuntime.getRuntime().disableJitCompilation();     } else {         // Enable the JIT for the system_server process         VMRuntime.getRuntime().startJitCompilation();     }

    // MMS service broker     mmsService = mSystemServiceManager.startService(MmsServiceBroker.class);

    // It is now time to start up the app processes…

    Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakeVibratorServiceReady”);     try {         vibrator.systemReady();     } catch (Throwable e) {         reportWtf(“making Vibrator Service ready”, e);     }     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

    Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakeLockSettingsServiceReady”);     if (lockSettings != null) {         try {             lockSettings.systemReady();         } catch (Throwable e) {             reportWtf(“making Lock Settings Service ready”, e);         }     }     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

    // Needed by DevicePolicyManager for initialization     mSystemServiceManager.startBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY); // 该阶段完成之后,service能获取锁屏设置的数据       mSystemServiceManager.startBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); // 该阶段完成之后,service能安全地调用核心系统服务

    Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakeWindowManagerServiceReady”);     try {         wm.systemReady();     } catch (Throwable e) {         reportWtf(“making Window Manager Service ready”, e);     }     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

    if (safeMode) {         mActivityManagerService.showSafeModeOverlay();     }

    // Update the configuration for this context by hand, because we’re going     // to start using it before the config change done in wm.systemReady() will     // propagate to it.     Configuration config = wm.computeNewConfiguration();     DisplayMetrics metrics = new DisplayMetrics();     WindowManager w = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);     w.getDefaultDisplay().getMetrics(metrics);     context.getResources().updateConfiguration(config, metrics);

    // The system context’s theme may be configuration-dependent.     final Theme systemTheme = context.getTheme();     if (systemTheme.getChangingConfigurations() != 0) {         systemTheme.rebase();     }

    Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakePowerManagerServiceReady”);     try {         // TODO: use boot phase         mPowerManagerService.systemReady(mActivityManagerService.getAppOpsService());         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);     } catch (Throwable e) {         reportWtf(“making Power Manager Service ready”, e);     }     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

    Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakePackageManagerServiceReady”);     try {         mPackageManagerService.systemReady();     } catch (Throwable e) {         reportWtf(“making Package Manager Service ready”, e);     }     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

    Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakeDisplayManagerServiceReady”);     try {         // TODO: use boot phase and communicate these flags some other way         mDisplayManagerService.systemReady(safeMode, mOnlyCore);     } catch (Throwable e) {         reportWtf(“making Display Manager Service ready”, e);     }     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

    // These are needed to propagate to the runnable below.     final NetworkManagementService networkManagementF = networkManagement;     final NetworkStatsService networkStatsF = networkStats;     final NetworkPolicyManagerService networkPolicyF = networkPolicy;     final ConnectivityService connectivityF = connectivity;     final NetworkScoreService networkScoreF = networkScore;     final WallpaperManagerService wallpaperF = wallpaper;     final InputMethodManagerService immF = imm;     final LocationManagerService locationF = location;     final CountryDetectorService countryDetectorF = countryDetector;     final NetworkTimeUpdateService networkTimeUpdaterF = networkTimeUpdater;     final CommonTimeManagementService commonTimeMgmtServiceF = commonTimeMgmtService;     final TextServicesManagerService textServiceManagerServiceF = tsms;     final StatusBarManagerService statusBarF = statusBar;     final AssetAtlasService atlasF = atlas;     final InputManagerService inputManagerF = inputManager;     final TelephonyRegistry telephonyRegistryF = telephonyRegistry;     final MediaRouterService mediaRouterF = mediaRouter;     final AudioService audioServiceF = audioService;     final MmsServiceBroker mmsServiceF = mmsService;

    // We now tell the activity manager it is okay to run third party     // code.  It will call back into us once it has gotten to the state     // where third party code can really run (but before it has actually     // started launching the initial applications), for us to complete our     // initialization.     mActivityManagerService.systemReady(new Runnable() {         @Override         public void run() {             Slog.i(TAG, “Making services ready”);             mSystemServiceManager.startBootPhase(                     SystemService.PHASE_ACTIVITY_MANAGER_READY); // 该阶段完成之后,service能广播Intent             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “PhaseActivityManagerReady”);

            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “StartObservingNativeCrashes”);             try {                 mActivityManagerService.startObservingNativeCrashes();             } catch (Throwable e) {                 reportWtf(“observing native crashes”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

            Slog.i(TAG, “WebViewFactory preparation”);             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “WebViewFactoryPreparation”);             WebViewFactory.prepareWebViewInSystemServer();             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);

            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “StartSystemUI”);             try {                 startSystemUi(context);             } catch (Throwable e) {                 reportWtf(“starting System UI”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakeMountServiceReady”);             try {                 if (networkScoreF != null) networkScoreF.systemReady();             } catch (Throwable e) {                 reportWtf(“making Network Score Service ready”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakeNetworkManagementServiceReady”);             try {                 if (networkManagementF != null) networkManagementF.systemReady();             } catch (Throwable e) {                 reportWtf(“making Network Managment Service ready”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakeNetworkStatsServiceReady”);             try {                 if (networkStatsF != null) networkStatsF.systemReady();             } catch (Throwable e) {                 reportWtf(“making Network Stats Service ready”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakeNetworkPolicyServiceReady”);             try {                 if (networkPolicyF != null) networkPolicyF.systemReady();             } catch (Throwable e) {                 reportWtf(“making Network Policy Service ready”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakeConnectivityServiceReady”);             try {                 if (connectivityF != null) connectivityF.systemReady();             } catch (Throwable e) {                 reportWtf(“making Connectivity Service ready”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “MakeAudioServiceReady”);             try {                 if (audioServiceF != null) audioServiceF.systemReady();             } catch (Throwable e) {                 reportWtf(“Notifying AudioService running”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);             Watchdog.getInstance().start(); // 启动Android Watchdog

            // It is now okay to let the various system services start their             // third party code…             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, “PhaseThirdPartyAppsCanStart”);             mSystemServiceManager.startBootPhase(                     SystemService.PHASE_THIRD_PARTY_APPS_CAN_START); // 该阶段完成之后,service能start/bind第三方应用程序

            try {                 if (wallpaperF != null) wallpaperF.systemRunning();             } catch (Throwable e) {                 reportWtf(“Notifying WallpaperService running”, e);             }             try {                 if (immF != null) immF.systemRunning(statusBarF);             } catch (Throwable e) {                 reportWtf(“Notifying InputMethodService running”, e);             }             try {                 if (locationF != null) locationF.systemRunning();             } catch (Throwable e) {                 reportWtf(“Notifying Location Service running”, e);             }             try {                 if (countryDetectorF != null) countryDetectorF.systemRunning();             } catch (Throwable e) {                 reportWtf(“Notifying CountryDetectorService running”, e);             }             try {                 if (networkTimeUpdaterF != null) networkTimeUpdaterF.systemRunning();             } catch (Throwable e) {                 reportWtf(“Notifying NetworkTimeService running”, e);             }             try {                 if (commonTimeMgmtServiceF != null) {                     commonTimeMgmtServiceF.systemRunning();                 }             } catch (Throwable e) {                 reportWtf(“Notifying CommonTimeManagementService running”, e);             }             try {                 if (textServiceManagerServiceF != null)                     textServiceManagerServiceF.systemRunning();             } catch (Throwable e) {                 reportWtf(“Notifying TextServicesManagerService running”, e);             }             try {                 if (atlasF != null) atlasF.systemRunning();             } catch (Throwable e) {                 reportWtf(“Notifying AssetAtlasService running”, e);             }             try {                 // TODO(BT) Pass parameter to input manager                 if (inputManagerF != null) inputManagerF.systemRunning();             } catch (Throwable e) {                 reportWtf(“Notifying InputManagerService running”, e);             }             try {                 if (telephonyRegistryF != null) telephonyRegistryF.systemRunning();             } catch (Throwable e) {                 reportWtf(“Notifying TelephonyRegistry running”, e);             }             try {                 if (mediaRouterF != null) mediaRouterF.systemRunning();             } catch (Throwable e) {                 reportWtf(“Notifying MediaRouterService running”, e);             }

            try {                 if (mmsServiceF != null) mmsServiceF.systemRunning();             } catch (Throwable e) {                 reportWtf(“Notifying MmsService running”, e);             }             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);         }     }); }

    原文作者:Android源码分析
    原文地址: https://blog.csdn.net/xichangbao/article/details/53176106
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞