OSDN Git Service

19c073ccf4dcad58e1dc44efa8c4dde8fa03cffc
[android-x86/frameworks-base.git] / services / java / com / android / server / SystemServer.java
1 /*
2  * Copyright (C) 2006 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;
18
19 import android.app.ActivityManagerNative;
20 import android.app.ActivityThread;
21 import android.app.IAlarmManager;
22 import android.app.INotificationManager;
23 import android.app.usage.UsageStatsManagerInternal;
24 import android.content.ComponentName;
25 import android.content.ContentResolver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.pm.PackageManager;
29 import android.content.res.Configuration;
30 import android.content.res.Resources.Theme;
31 import android.os.Build;
32 import android.os.Environment;
33 import android.os.FactoryTest;
34 import android.os.FileUtils;
35 import android.os.IPowerManager;
36 import android.os.Looper;
37 import android.os.PowerManager;
38 import android.os.RecoverySystem;
39 import android.os.RemoteException;
40 import android.os.ServiceManager;
41 import android.os.StrictMode;
42 import android.os.SystemClock;
43 import android.os.SystemProperties;
44 import android.os.Trace;
45 import android.os.UserHandle;
46 import android.os.storage.IMountService;
47 import android.util.DisplayMetrics;
48 import android.util.EventLog;
49 import android.util.Slog;
50 import android.view.WindowManager;
51
52 import com.android.internal.R;
53 import com.android.internal.os.BinderInternal;
54 import com.android.internal.os.SamplingProfilerIntegration;
55 import com.android.internal.os.ZygoteInit;
56 import com.android.server.accessibility.AccessibilityManagerService;
57 import com.android.server.accounts.AccountManagerService;
58 import com.android.server.am.ActivityManagerService;
59 import com.android.server.audio.AudioService;
60 import com.android.server.camera.CameraService;
61 import com.android.server.clipboard.ClipboardService;
62 import com.android.server.connectivity.MetricsLoggerService;
63 import com.android.server.content.ContentService;
64 import com.android.server.devicepolicy.DevicePolicyManagerService;
65 import com.android.server.display.DisplayManagerService;
66 import com.android.server.dreams.DreamManagerService;
67 import com.android.server.fingerprint.FingerprintService;
68 import com.android.server.hdmi.HdmiControlService;
69 import com.android.server.input.InputManagerService;
70 import com.android.server.job.JobSchedulerService;
71 import com.android.server.lights.LightsService;
72 import com.android.internal.widget.ILockSettings;
73 import com.android.server.media.MediaRouterService;
74 import com.android.server.media.MediaSessionService;
75 import com.android.server.media.MediaResourceMonitorService;
76 import com.android.server.media.projection.MediaProjectionManagerService;
77 import com.android.server.net.NetworkPolicyManagerService;
78 import com.android.server.net.NetworkStatsService;
79 import com.android.server.notification.NotificationManagerService;
80 import com.android.server.os.SchedulingPolicyService;
81 import com.android.server.pm.BackgroundDexOptService;
82 import com.android.server.pm.Installer;
83 import com.android.server.pm.LauncherAppsService;
84 import com.android.server.pm.OtaDexoptService;
85 import com.android.server.pm.PackageManagerService;
86 import com.android.server.pm.ShortcutService;
87 import com.android.server.pm.UserManagerService;
88 import com.android.server.power.PowerManagerService;
89 import com.android.server.power.ShutdownThread;
90 import com.android.server.restrictions.RestrictionsManagerService;
91 import com.android.server.soundtrigger.SoundTriggerService;
92 import com.android.server.statusbar.StatusBarManagerService;
93 import com.android.server.storage.DeviceStorageMonitorService;
94 import com.android.server.telecom.TelecomLoaderService;
95 import com.android.server.trust.TrustManagerService;
96 import com.android.server.tv.TvInputManagerService;
97 import com.android.server.twilight.TwilightService;
98 import com.android.server.usage.UsageStatsService;
99 import com.android.server.vr.VrManagerService;
100 import com.android.server.wallpaper.WallpaperManagerService;
101 import com.android.server.webkit.WebViewUpdateService;
102 import com.android.server.wm.WindowManagerService;
103
104 import dalvik.system.VMRuntime;
105
106 import java.io.File;
107 import java.io.IOException;
108 import java.util.Locale;
109 import java.util.Timer;
110 import java.util.TimerTask;
111
112 public final class SystemServer {
113     private static final String TAG = "SystemServer";
114
115     private static final String ENCRYPTING_STATE = "trigger_restart_min_framework";
116     private static final String ENCRYPTED_STATE = "1";
117
118     private static final long SNAPSHOT_INTERVAL = 60 * 60 * 1000; // 1hr
119
120     // The earliest supported time.  We pick one day into 1970, to
121     // give any timezone code room without going into negative time.
122     private static final long EARLIEST_SUPPORTED_TIME = 86400 * 1000;
123
124     /*
125      * Implementation class names. TODO: Move them to a codegen class or load
126      * them from the build system somehow.
127      */
128     private static final String BACKUP_MANAGER_SERVICE_CLASS =
129             "com.android.server.backup.BackupManagerService$Lifecycle";
130     private static final String APPWIDGET_SERVICE_CLASS =
131             "com.android.server.appwidget.AppWidgetService";
132     private static final String VOICE_RECOGNITION_MANAGER_SERVICE_CLASS =
133             "com.android.server.voiceinteraction.VoiceInteractionManagerService";
134     private static final String PRINT_MANAGER_SERVICE_CLASS =
135             "com.android.server.print.PrintManagerService";
136     private static final String USB_SERVICE_CLASS =
137             "com.android.server.usb.UsbService$Lifecycle";
138     private static final String MIDI_SERVICE_CLASS =
139             "com.android.server.midi.MidiService$Lifecycle";
140     private static final String WIFI_SERVICE_CLASS =
141             "com.android.server.wifi.WifiService";
142     private static final String WIFI_NAN_SERVICE_CLASS =
143             "com.android.server.wifi.nan.WifiNanService";
144     private static final String WIFI_P2P_SERVICE_CLASS =
145             "com.android.server.wifi.p2p.WifiP2pService";
146     private static final String ETHERNET_SERVICE_CLASS =
147             "com.android.server.ethernet.EthernetService";
148     private static final String JOB_SCHEDULER_SERVICE_CLASS =
149             "com.android.server.job.JobSchedulerService";
150     private static final String LOCK_SETTINGS_SERVICE_CLASS =
151             "com.android.server.LockSettingsService$Lifecycle";
152     private static final String MOUNT_SERVICE_CLASS =
153             "com.android.server.MountService$Lifecycle";
154     private static final String SEARCH_MANAGER_SERVICE_CLASS =
155             "com.android.server.search.SearchManagerService$Lifecycle";
156
157     private static final String PERSISTENT_DATA_BLOCK_PROP = "ro.frp.pst";
158
159     private static final String UNCRYPT_PACKAGE_FILE = "/cache/recovery/uncrypt_file";
160     private static final String BLOCK_MAP_FILE = "/cache/recovery/block.map";
161
162     /**
163      * Default theme used by the system context. This is used to style
164      * system-provided dialogs, such as the Power Off dialog, and other
165      * visual content.
166      */
167     private static final int DEFAULT_SYSTEM_THEME =
168             com.android.internal.R.style.Theme_Material_DayNight_DarkActionBar;
169
170     private final int mFactoryTestMode;
171     private Timer mProfilerSnapshotTimer;
172
173     private Context mSystemContext;
174     private SystemServiceManager mSystemServiceManager;
175
176     // TODO: remove all of these references by improving dependency resolution and boot phases
177     private PowerManagerService mPowerManagerService;
178     private ActivityManagerService mActivityManagerService;
179     private WebViewUpdateService mWebViewUpdateService;
180     private DisplayManagerService mDisplayManagerService;
181     private PackageManagerService mPackageManagerService;
182     private PackageManager mPackageManager;
183     private ContentResolver mContentResolver;
184
185     private boolean mOnlyCore;
186     private boolean mFirstBoot;
187
188     /**
189      * Start the sensor service.
190      */
191     private static native void startSensorService();
192
193     /**
194      * The main entry point from zygote.
195      */
196     public static void main(String[] args) {
197         new SystemServer().run();
198     }
199
200     public SystemServer() {
201         // Check for factory test mode.
202         mFactoryTestMode = FactoryTest.getMode();
203     }
204
205     private void run() {
206         try {
207             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "InitBeforeStartServices");
208             // If a device's clock is before 1970 (before 0), a lot of
209             // APIs crash dealing with negative numbers, notably
210             // java.io.File#setLastModified, so instead we fake it and
211             // hope that time from cell towers or NTP fixes it shortly.
212             if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
213                 Slog.w(TAG, "System clock is before 1970; setting to 1970.");
214                 SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
215             }
216
217             // If the system has "persist.sys.language" and friends set, replace them with
218             // "persist.sys.locale". Note that the default locale at this point is calculated
219             // using the "-Duser.locale" command line flag. That flag is usually populated by
220             // AndroidRuntime using the same set of system properties, but only the system_server
221             // and system apps are allowed to set them.
222             //
223             // NOTE: Most changes made here will need an equivalent change to
224             // core/jni/AndroidRuntime.cpp
225             if (!SystemProperties.get("persist.sys.language").isEmpty()) {
226                 final String languageTag = Locale.getDefault().toLanguageTag();
227
228                 SystemProperties.set("persist.sys.locale", languageTag);
229                 SystemProperties.set("persist.sys.language", "");
230                 SystemProperties.set("persist.sys.country", "");
231                 SystemProperties.set("persist.sys.localevar", "");
232             }
233
234             // Here we go!
235             Slog.i(TAG, "Entered the Android system server!");
236             EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, SystemClock.uptimeMillis());
237
238             // In case the runtime switched since last boot (such as when
239             // the old runtime was removed in an OTA), set the system
240             // property so that it is in sync. We can't do this in
241             // libnativehelper's JniInvocation::Init code where we already
242             // had to fallback to a different runtime because it is
243             // running as root and we need to be the system user to set
244             // the property. http://b/11463182
245             SystemProperties.set("persist.sys.dalvik.vm.lib.2", VMRuntime.getRuntime().vmLibrary());
246
247             // Enable the sampling profiler.
248             if (SamplingProfilerIntegration.isEnabled()) {
249                 SamplingProfilerIntegration.start();
250                 mProfilerSnapshotTimer = new Timer();
251                 mProfilerSnapshotTimer.schedule(new TimerTask() {
252                         @Override
253                         public void run() {
254                             SamplingProfilerIntegration.writeSnapshot("system_server", null);
255                         }
256                     }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
257             }
258
259             // Mmmmmm... more memory!
260             VMRuntime.getRuntime().clearGrowthLimit();
261
262             // The system server has to run all of the time, so it needs to be
263             // as efficient as possible with its memory usage.
264             VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
265
266             // Some devices rely on runtime fingerprint generation, so make sure
267             // we've defined it before booting further.
268             Build.ensureFingerprintProperty();
269
270             // Within the system server, it is an error to access Environment paths without
271             // explicitly specifying a user.
272             Environment.setUserRequired(true);
273
274             // Ensure binder calls into the system always run at foreground priority.
275             BinderInternal.disableBackgroundScheduling(true);
276
277             // Prepare the main looper thread (this thread).
278             android.os.Process.setThreadPriority(
279                 android.os.Process.THREAD_PRIORITY_FOREGROUND);
280             android.os.Process.setCanSelfBackground(false);
281             Looper.prepareMainLooper();
282
283             // Initialize native services.
284             System.loadLibrary("android_servers");
285
286             // Check whether we failed to shut down last time we tried.
287             // This call may not return.
288             performPendingShutdown();
289
290             // Initialize the system context.
291             createSystemContext();
292
293             // Create the system service manager.
294             mSystemServiceManager = new SystemServiceManager(mSystemContext);
295             LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
296         } finally {
297             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
298         }
299
300         // Start services.
301         try {
302             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartServices");
303             startBootstrapServices();
304             startCoreServices();
305             startOtherServices();
306         } catch (Throwable ex) {
307             Slog.e("System", "******************************************");
308             Slog.e("System", "************ Failure starting system services", ex);
309             throw ex;
310         } finally {
311             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
312         }
313
314         // For debug builds, log event loop stalls to dropbox for analysis.
315         if (StrictMode.conditionallyEnableDebugLogging()) {
316             Slog.i(TAG, "Enabled StrictMode for system server main thread.");
317         }
318
319         // Loop forever.
320         Looper.loop();
321         throw new RuntimeException("Main thread loop unexpectedly exited");
322     }
323
324     private void reportWtf(String msg, Throwable e) {
325         Slog.w(TAG, "***********************************************");
326         Slog.wtf(TAG, "BOOT FAILURE " + msg, e);
327     }
328
329     private void performPendingShutdown() {
330         final String shutdownAction = SystemProperties.get(
331                 ShutdownThread.SHUTDOWN_ACTION_PROPERTY, "");
332         if (shutdownAction != null && shutdownAction.length() > 0) {
333             boolean reboot = (shutdownAction.charAt(0) == '1');
334
335             final String reason;
336             if (shutdownAction.length() > 1) {
337                 reason = shutdownAction.substring(1, shutdownAction.length());
338             } else {
339                 reason = null;
340             }
341
342             // If it's a pending reboot into recovery to apply an update,
343             // always make sure uncrypt gets executed properly when needed.
344             // If '/cache/recovery/block.map' hasn't been created, stop the
345             // reboot which will fail for sure, and get a chance to capture a
346             // bugreport when that's still feasible. (Bug: 26444951)
347             if (PowerManager.REBOOT_RECOVERY_UPDATE.equals(reason)) {
348                 File packageFile = new File(UNCRYPT_PACKAGE_FILE);
349                 if (packageFile.exists()) {
350                     String filename = null;
351                     try {
352                         filename = FileUtils.readTextFile(packageFile, 0, null);
353                     } catch (IOException e) {
354                         Slog.e(TAG, "Error reading uncrypt package file", e);
355                     }
356
357                     if (filename != null && filename.startsWith("/data")) {
358                         if (!new File(BLOCK_MAP_FILE).exists()) {
359                             Slog.e(TAG, "Can't find block map file, uncrypt failed or " +
360                                        "unexpected runtime restart?");
361                             return;
362                         }
363                     }
364                 }
365             }
366             ShutdownThread.rebootOrShutdown(null, reboot, reason);
367         }
368     }
369
370     private void createSystemContext() {
371         ActivityThread activityThread = ActivityThread.systemMain();
372         mSystemContext = activityThread.getSystemContext();
373         mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
374     }
375
376     /**
377      * Starts the small tangle of critical services that are needed to get
378      * the system off the ground.  These services have complex mutual dependencies
379      * which is why we initialize them all in one place here.  Unless your service
380      * is also entwined in these dependencies, it should be initialized in one of
381      * the other functions.
382      */
383     private void startBootstrapServices() {
384         // Wait for installd to finish starting up so that it has a chance to
385         // create critical directories such as /data/user with the appropriate
386         // permissions.  We need this to complete before we initialize other services.
387         Installer installer = mSystemServiceManager.startService(Installer.class);
388
389         // Activity manager runs the show.
390         mActivityManagerService = mSystemServiceManager.startService(
391                 ActivityManagerService.Lifecycle.class).getService();
392         mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
393         mActivityManagerService.setInstaller(installer);
394
395         // Power manager needs to be started early because other services need it.
396         // Native daemons may be watching for it to be registered so it must be ready
397         // to handle incoming binder calls immediately (including being able to verify
398         // the permissions for those calls).
399         mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
400
401         // Now that the power manager has been started, let the activity manager
402         // initialize power management features.
403         Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "InitPowerManagement");
404         mActivityManagerService.initPowerManagement();
405         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
406
407         // Manages LEDs and display backlight so we need it to bring up the display.
408         mSystemServiceManager.startService(LightsService.class);
409
410         // Display manager is needed to provide display metrics before package manager
411         // starts up.
412         mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);
413
414         // We need the default display before we can initialize the package manager.
415         mSystemServiceManager.startBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);
416
417         // Only run "core" apps if we're encrypting the device.
418         String cryptState = SystemProperties.get("vold.decrypt");
419         if (ENCRYPTING_STATE.equals(cryptState)) {
420             Slog.w(TAG, "Detected encryption in progress - only parsing core apps");
421             mOnlyCore = true;
422         } else if (ENCRYPTED_STATE.equals(cryptState)) {
423             Slog.w(TAG, "Device encrypted - only parsing core apps");
424             mOnlyCore = true;
425         }
426
427         // Start the package manager.
428         traceBeginAndSlog("StartPackageManagerService");
429         mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
430                 mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
431         mFirstBoot = mPackageManagerService.isFirstBoot();
432         mPackageManager = mSystemContext.getPackageManager();
433         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
434
435         // Manages A/B OTA dexopting. This is a bootstrap service as we need it to rename
436         // A/B artifacts after boot, before anything else might touch/need them.
437         // Note: this isn't needed during decryption (we don't have /data anyways).
438         if (!mOnlyCore) {
439             boolean disableOtaDexopt = SystemProperties.getBoolean("config.disable_otadexopt",
440                     false);
441             if (!disableOtaDexopt) {
442                 traceBeginAndSlog("StartOtaDexOptService");
443                 try {
444                     OtaDexoptService.main(mSystemContext, mPackageManagerService);
445                 } catch (Throwable e) {
446                     reportWtf("starting OtaDexOptService", e);
447                 } finally {
448                     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
449                 }
450             }
451         }
452
453         traceBeginAndSlog("StartUserManagerService");
454         ServiceManager.addService(Context.USER_SERVICE, UserManagerService.getInstance());
455         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
456
457         // Initialize attribute cache used to cache resources from packages.
458         AttributeCache.init(mSystemContext);
459
460         // Set up the Application instance for the system process and get started.
461         mActivityManagerService.setSystemProcess();
462
463         // The sensor service needs access to package manager service, app ops
464         // service, and permissions service, therefore we start it after them.
465         startSensorService();
466     }
467
468     /**
469      * Starts some essential services that are not tangled up in the bootstrap process.
470      */
471     private void startCoreServices() {
472         // Tracks the battery level.  Requires LightService.
473         mSystemServiceManager.startService(BatteryService.class);
474
475         // Tracks application usage stats.
476         mSystemServiceManager.startService(UsageStatsService.class);
477         mActivityManagerService.setUsageStatsManager(
478                 LocalServices.getService(UsageStatsManagerInternal.class));
479
480         // Tracks whether the updatable WebView is in a ready state and watches for update installs.
481         mWebViewUpdateService = mSystemServiceManager.startService(WebViewUpdateService.class);
482     }
483
484     /**
485      * Starts a miscellaneous grab bag of stuff that has yet to be refactored
486      * and organized.
487      */
488     private void startOtherServices() {
489         final Context context = mSystemContext;
490         AccountManagerService accountManager = null;
491         ContentService contentService = null;
492         VibratorService vibrator = null;
493         IAlarmManager alarm = null;
494         IMountService mountService = null;
495         NetworkManagementService networkManagement = null;
496         NetworkStatsService networkStats = null;
497         NetworkPolicyManagerService networkPolicy = null;
498         ConnectivityService connectivity = null;
499         NetworkScoreService networkScore = null;
500         NsdService serviceDiscovery= null;
501         WindowManagerService wm = null;
502         SerialService serial = null;
503         NetworkTimeUpdateService networkTimeUpdater = null;
504         CommonTimeManagementService commonTimeMgmtService = null;
505         InputManagerService inputManager = null;
506         TelephonyRegistry telephonyRegistry = null;
507         ConsumerIrService consumerIr = null;
508         MmsServiceBroker mmsService = null;
509         EntropyMixer entropyMixer = null;
510         VrManagerService vrManagerService = null;
511         HardwarePropertiesManagerService hardwarePropertiesService = null;
512
513         boolean disableStorage = SystemProperties.getBoolean("config.disable_storage", false);
514         boolean disableBluetooth = SystemProperties.getBoolean("config.disable_bluetooth", false);
515         boolean disableLocation = SystemProperties.getBoolean("config.disable_location", false);
516         boolean disableSystemUI = SystemProperties.getBoolean("config.disable_systemui", false);
517         boolean disableNonCoreServices = SystemProperties.getBoolean("config.disable_noncore", false);
518         boolean disableNetwork = SystemProperties.getBoolean("config.disable_network", false);
519         boolean disableNetworkTime = SystemProperties.getBoolean("config.disable_networktime", false);
520         boolean disableRtt = SystemProperties.getBoolean("config.disable_rtt", false);
521         boolean disableMediaProjection = SystemProperties.getBoolean("config.disable_mediaproj",
522                 false);
523         boolean disableSerial = SystemProperties.getBoolean("config.disable_serial", false);
524         boolean disableSearchManager = SystemProperties.getBoolean("config.disable_searchmanager",
525                 false);
526         boolean disableTrustManager = SystemProperties.getBoolean("config.disable_trustmanager",
527                 false);
528         boolean disableTextServices = SystemProperties.getBoolean("config.disable_textservices", false);
529         boolean disableSamplingProfiler = SystemProperties.getBoolean("config.disable_samplingprof",
530                 false);
531         boolean isEmulator = SystemProperties.get("ro.kernel.qemu").equals("1");
532
533         try {
534             Slog.i(TAG, "Reading configuration...");
535             SystemConfig.getInstance();
536
537             traceBeginAndSlog("StartSchedulingPolicyService");
538             ServiceManager.addService("scheduling_policy", new SchedulingPolicyService());
539             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
540
541             mSystemServiceManager.startService(TelecomLoaderService.class);
542
543             traceBeginAndSlog("StartTelephonyRegistry");
544             telephonyRegistry = new TelephonyRegistry(context);
545             ServiceManager.addService("telephony.registry", telephonyRegistry);
546             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
547
548             traceBeginAndSlog("StartEntropyMixer");
549             entropyMixer = new EntropyMixer(context);
550             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
551
552             mContentResolver = context.getContentResolver();
553
554             Slog.i(TAG, "Camera Service");
555             mSystemServiceManager.startService(CameraService.class);
556
557             // The AccountManager must come before the ContentService
558             traceBeginAndSlog("StartAccountManagerService");
559             try {
560                 // TODO: seems like this should be disable-able, but req'd by ContentService
561                 accountManager = new AccountManagerService(context);
562                 ServiceManager.addService(Context.ACCOUNT_SERVICE, accountManager);
563             } catch (Throwable e) {
564                 Slog.e(TAG, "Failure starting Account Manager", e);
565             }
566             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
567
568             traceBeginAndSlog("StartContentService");
569             contentService = ContentService.main(context,
570                     mFactoryTestMode == FactoryTest.FACTORY_TEST_LOW_LEVEL);
571             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
572
573             traceBeginAndSlog("InstallSystemProviders");
574             mActivityManagerService.installSystemProviders();
575             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
576
577             traceBeginAndSlog("StartVibratorService");
578             vibrator = new VibratorService(context);
579             ServiceManager.addService("vibrator", vibrator);
580             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
581
582             traceBeginAndSlog("StartConsumerIrService");
583             consumerIr = new ConsumerIrService(context);
584             ServiceManager.addService(Context.CONSUMER_IR_SERVICE, consumerIr);
585             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
586
587             mSystemServiceManager.startService(AlarmManagerService.class);
588             alarm = IAlarmManager.Stub.asInterface(
589                     ServiceManager.getService(Context.ALARM_SERVICE));
590
591             traceBeginAndSlog("InitWatchdog");
592             final Watchdog watchdog = Watchdog.getInstance();
593             watchdog.init(context, mActivityManagerService);
594             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
595
596             traceBeginAndSlog("StartInputManagerService");
597             inputManager = new InputManagerService(context);
598             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
599
600             traceBeginAndSlog("StartWindowManagerService");
601             wm = WindowManagerService.main(context, inputManager,
602                     mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
603                     !mFirstBoot, mOnlyCore);
604             ServiceManager.addService(Context.WINDOW_SERVICE, wm);
605             ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
606             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
607
608             traceBeginAndSlog("StartVrManagerService");
609             mSystemServiceManager.startService(VrManagerService.class);
610             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
611
612             mActivityManagerService.setWindowManager(wm);
613
614             inputManager.setWindowManagerCallbacks(wm.getInputMonitor());
615             inputManager.start();
616
617             // TODO: Use service dependencies instead.
618             mDisplayManagerService.windowManagerAndInputReady();
619
620             // Skip Bluetooth if we have an emulator kernel
621             // TODO: Use a more reliable check to see if this product should
622             // support Bluetooth - see bug 988521
623             if (isEmulator) {
624                 Slog.i(TAG, "No Bluetooth Service (emulator)");
625             } else if (mFactoryTestMode == FactoryTest.FACTORY_TEST_LOW_LEVEL) {
626                 Slog.i(TAG, "No Bluetooth Service (factory test)");
627             } else if (!context.getPackageManager().hasSystemFeature
628                        (PackageManager.FEATURE_BLUETOOTH)) {
629                 Slog.i(TAG, "No Bluetooth Service (Bluetooth Hardware Not Present)");
630             } else if (disableBluetooth) {
631                 Slog.i(TAG, "Bluetooth Service disabled by config");
632             } else {
633                 mSystemServiceManager.startService(BluetoothService.class);
634             }
635
636             traceBeginAndSlog("ConnectivityMetricsLoggerService");
637             mSystemServiceManager.startService(MetricsLoggerService.class);
638             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
639         } catch (RuntimeException e) {
640             Slog.e("System", "******************************************");
641             Slog.e("System", "************ Failure starting core service", e);
642         }
643
644         StatusBarManagerService statusBar = null;
645         INotificationManager notification = null;
646         WallpaperManagerService wallpaper = null;
647         LocationManagerService location = null;
648         CountryDetectorService countryDetector = null;
649         ILockSettings lockSettings = null;
650         AssetAtlasService atlas = null;
651         MediaRouterService mediaRouter = null;
652
653         // Bring up services needed for UI.
654         if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
655             mSystemServiceManager.startService(InputMethodManagerService.Lifecycle.class);
656
657             traceBeginAndSlog("StartAccessibilityManagerService");
658             try {
659                 ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
660                         new AccessibilityManagerService(context));
661             } catch (Throwable e) {
662                 reportWtf("starting Accessibility Manager", e);
663             }
664             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
665         }
666
667         try {
668             wm.displayReady();
669         } catch (Throwable e) {
670             reportWtf("making display ready", e);
671         }
672
673         if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
674             if (!disableStorage &&
675                 !"0".equals(SystemProperties.get("system_init.startmountservice"))) {
676                 try {
677                     /*
678                      * NotificationManagerService is dependant on MountService,
679                      * (for media / usb notifications) so we must start MountService first.
680                      */
681                     mSystemServiceManager.startService(MOUNT_SERVICE_CLASS);
682                     mountService = IMountService.Stub.asInterface(
683                             ServiceManager.getService("mount"));
684                 } catch (Throwable e) {
685                     reportWtf("starting Mount Service", e);
686                 }
687             }
688         }
689
690         // We start this here so that we update our configuration to set watch or television
691         // as appropriate.
692         mSystemServiceManager.startService(UiModeManagerService.class);
693
694         Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "PerformFstrimIfNeeded");
695         try {
696             mPackageManagerService.performFstrimIfNeeded();
697         } catch (Throwable e) {
698             reportWtf("performing fstrim", e);
699         }
700         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
701
702         Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "ExtractPackagesIfNeeded");
703         try {
704             mPackageManagerService.extractPackagesIfNeeded();
705         } catch (Throwable e) {
706             reportWtf("extract packages", e);
707         }
708         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
709
710         try {
711             ActivityManagerNative.getDefault().showBootMessage(
712                     context.getResources().getText(
713                             com.android.internal.R.string.android_upgrading_starting_apps),
714                     false);
715         } catch (RemoteException e) {
716         }
717
718         if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
719             if (!disableNonCoreServices) {
720                 traceBeginAndSlog("StartLockSettingsService");
721                 try {
722                     mSystemServiceManager.startService(LOCK_SETTINGS_SERVICE_CLASS);
723                     lockSettings = ILockSettings.Stub.asInterface(
724                             ServiceManager.getService("lock_settings"));
725                 } catch (Throwable e) {
726                     reportWtf("starting LockSettingsService service", e);
727                 }
728                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
729
730                 if (!SystemProperties.get(PERSISTENT_DATA_BLOCK_PROP).equals("")) {
731                     mSystemServiceManager.startService(PersistentDataBlockService.class);
732                 }
733
734                 mSystemServiceManager.startService(DeviceIdleController.class);
735
736                 // Always start the Device Policy Manager, so that the API is compatible with
737                 // API8.
738                 mSystemServiceManager.startService(DevicePolicyManagerService.Lifecycle.class);
739             }
740
741             if (!disableSystemUI) {
742                 traceBeginAndSlog("StartStatusBarManagerService");
743                 try {
744                     statusBar = new StatusBarManagerService(context, wm);
745                     ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);
746                 } catch (Throwable e) {
747                     reportWtf("starting StatusBarManagerService", e);
748                 }
749                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
750             }
751
752             if (!disableNonCoreServices) {
753                 traceBeginAndSlog("StartClipboardService");
754                 try {
755                     ServiceManager.addService(Context.CLIPBOARD_SERVICE,
756                             new ClipboardService(context));
757                 } catch (Throwable e) {
758                     reportWtf("starting Clipboard Service", e);
759                 }
760                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
761             }
762
763             if (!disableNetwork) {
764                 traceBeginAndSlog("StartNetworkManagementService");
765                 try {
766                     networkManagement = NetworkManagementService.create(context);
767                     ServiceManager.addService(Context.NETWORKMANAGEMENT_SERVICE, networkManagement);
768                 } catch (Throwable e) {
769                     reportWtf("starting NetworkManagement Service", e);
770                 }
771                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
772             }
773
774             if (!disableNonCoreServices && !disableTextServices) {
775                 mSystemServiceManager.startService(TextServicesManagerService.Lifecycle.class);
776             }
777
778             if (!disableNetwork) {
779                 traceBeginAndSlog("StartNetworkScoreService");
780                 try {
781                     networkScore = new NetworkScoreService(context);
782                     ServiceManager.addService(Context.NETWORK_SCORE_SERVICE, networkScore);
783                 } catch (Throwable e) {
784                     reportWtf("starting Network Score Service", e);
785                 }
786                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
787
788                 traceBeginAndSlog("StartNetworkStatsService");
789                 try {
790                     networkStats = NetworkStatsService.create(context, networkManagement);
791                     ServiceManager.addService(Context.NETWORK_STATS_SERVICE, networkStats);
792                 } catch (Throwable e) {
793                     reportWtf("starting NetworkStats Service", e);
794                 }
795                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
796
797                 traceBeginAndSlog("StartNetworkPolicyManagerService");
798                 try {
799                     networkPolicy = new NetworkPolicyManagerService(
800                             context, mActivityManagerService,
801                             (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE),
802                             networkStats, networkManagement);
803                     ServiceManager.addService(Context.NETWORK_POLICY_SERVICE, networkPolicy);
804                 } catch (Throwable e) {
805                     reportWtf("starting NetworkPolicy Service", e);
806                 }
807                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
808
809                 if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_NAN)) {
810                     mSystemServiceManager.startService(WIFI_NAN_SERVICE_CLASS);
811                 } else {
812                     Slog.i(TAG, "No Wi-Fi NAN Service (NAN support Not Present)");
813                 }
814                 mSystemServiceManager.startService(WIFI_P2P_SERVICE_CLASS);
815                 mSystemServiceManager.startService(WIFI_SERVICE_CLASS);
816                 mSystemServiceManager.startService(
817                             "com.android.server.wifi.WifiScanningService");
818
819                 if (!disableRtt) {
820                     mSystemServiceManager.startService("com.android.server.wifi.RttService");
821                 }
822
823                 if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_ETHERNET) ||
824                     mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)) {
825                     mSystemServiceManager.startService(ETHERNET_SERVICE_CLASS);
826                 }
827
828                 traceBeginAndSlog("StartConnectivityService");
829                 try {
830                     connectivity = new ConnectivityService(
831                             context, networkManagement, networkStats, networkPolicy);
832                     ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
833                     networkStats.bindConnectivityManager(connectivity);
834                     networkPolicy.bindConnectivityManager(connectivity);
835                 } catch (Throwable e) {
836                     reportWtf("starting Connectivity Service", e);
837                 }
838                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
839
840                 traceBeginAndSlog("StartNsdService");
841                 try {
842                     serviceDiscovery = NsdService.create(context);
843                     ServiceManager.addService(
844                             Context.NSD_SERVICE, serviceDiscovery);
845                 } catch (Throwable e) {
846                     reportWtf("starting Service Discovery Service", e);
847                 }
848                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
849             }
850
851             if (!disableNonCoreServices) {
852                 traceBeginAndSlog("StartUpdateLockService");
853                 try {
854                     ServiceManager.addService(Context.UPDATE_LOCK_SERVICE,
855                             new UpdateLockService(context));
856                 } catch (Throwable e) {
857                     reportWtf("starting UpdateLockService", e);
858                 }
859                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
860             }
861
862             if (!disableNonCoreServices) {
863                 mSystemServiceManager.startService(RecoverySystemService.class);
864             }
865
866             /*
867              * MountService has a few dependencies: Notification Manager and
868              * AppWidget Provider. Make sure MountService is completely started
869              * first before continuing.
870              */
871             if (mountService != null && !mOnlyCore) {
872                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "WaitForAsecScan");
873                 try {
874                     mountService.waitForAsecScan();
875                 } catch (RemoteException ignored) {
876                 }
877                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
878             }
879
880             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeAccountManagerServiceReady");
881             try {
882                 if (accountManager != null)
883                     accountManager.systemReady();
884             } catch (Throwable e) {
885                 reportWtf("making Account Manager Service ready", e);
886             }
887             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
888
889             Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeContentServiceReady");
890             try {
891                 if (contentService != null)
892                     contentService.systemReady();
893             } catch (Throwable e) {
894                 reportWtf("making Content Service ready", e);
895             }
896             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
897
898             mSystemServiceManager.startService(NotificationManagerService.class);
899             notification = INotificationManager.Stub.asInterface(
900                     ServiceManager.getService(Context.NOTIFICATION_SERVICE));
901             networkPolicy.bindNotificationManager(notification);
902
903             mSystemServiceManager.startService(DeviceStorageMonitorService.class);
904
905             if (!disableLocation) {
906                 traceBeginAndSlog("StartLocationManagerService");
907                 try {
908                     location = new LocationManagerService(context);
909                     ServiceManager.addService(Context.LOCATION_SERVICE, location);
910                 } catch (Throwable e) {
911                     reportWtf("starting Location Manager", e);
912                 }
913                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
914
915                 traceBeginAndSlog("StartCountryDetectorService");
916                 try {
917                     countryDetector = new CountryDetectorService(context);
918                     ServiceManager.addService(Context.COUNTRY_DETECTOR, countryDetector);
919                 } catch (Throwable e) {
920                     reportWtf("starting Country Detector", e);
921                 }
922                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
923             }
924
925             if (!disableNonCoreServices && !disableSearchManager) {
926                 traceBeginAndSlog("StartSearchManagerService");
927                 try {
928                     mSystemServiceManager.startService(SEARCH_MANAGER_SERVICE_CLASS);
929                 } catch (Throwable e) {
930                     reportWtf("starting Search Service", e);
931                 }
932                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
933             }
934
935             mSystemServiceManager.startService(DropBoxManagerService.class);
936
937             if (!disableNonCoreServices && context.getResources().getBoolean(
938                         R.bool.config_enableWallpaperService)) {
939                 traceBeginAndSlog("StartWallpaperManagerService");
940                 try {
941                     wallpaper = new WallpaperManagerService(context);
942                     ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);
943                 } catch (Throwable e) {
944                     reportWtf("starting Wallpaper Service", e);
945                 }
946                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
947             }
948
949             traceBeginAndSlog("StartAudioService");
950             mSystemServiceManager.startService(AudioService.Lifecycle.class);
951             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
952
953             if (!disableNonCoreServices) {
954                 mSystemServiceManager.startService(DockObserver.class);
955
956                 if (context.getPackageManager().hasSystemFeature
957                         (PackageManager.FEATURE_WATCH)) {
958                     mSystemServiceManager.startService(ThermalObserver.class);
959                 }
960             }
961
962             traceBeginAndSlog("StartWiredAccessoryManager");
963             try {
964                 // Listen for wired headset changes
965                 inputManager.setWiredAccessoryCallbacks(
966                         new WiredAccessoryManager(context, inputManager));
967             } catch (Throwable e) {
968                 reportWtf("starting WiredAccessoryManager", e);
969             }
970             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
971
972             if (!disableNonCoreServices) {
973                 if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_MIDI)) {
974                     // Start MIDI Manager service
975                     mSystemServiceManager.startService(MIDI_SERVICE_CLASS);
976                 }
977
978                 if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)
979                         || mPackageManager.hasSystemFeature(
980                                 PackageManager.FEATURE_USB_ACCESSORY)) {
981                     // Manage USB host and device support
982                     Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartUsbService");
983                     mSystemServiceManager.startService(USB_SERVICE_CLASS);
984                     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
985                 }
986
987                 if (!disableSerial) {
988                     traceBeginAndSlog("StartSerialService");
989                     try {
990                         // Serial port support
991                         serial = new SerialService(context);
992                         ServiceManager.addService(Context.SERIAL_SERVICE, serial);
993                     } catch (Throwable e) {
994                         Slog.e(TAG, "Failure starting SerialService", e);
995                     }
996                     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
997                 }
998
999                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER,
1000                         "StartHardwarePropertiesManagerService");
1001                 try {
1002                     hardwarePropertiesService = new HardwarePropertiesManagerService(context);
1003                     ServiceManager.addService(Context.HARDWARE_PROPERTIES_SERVICE,
1004                             hardwarePropertiesService);
1005                 } catch (Throwable e) {
1006                     Slog.e(TAG, "Failure starting HardwarePropertiesManagerService", e);
1007                 }
1008                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1009             }
1010
1011             mSystemServiceManager.startService(TwilightService.class);
1012
1013             mSystemServiceManager.startService(JobSchedulerService.class);
1014
1015             mSystemServiceManager.startService(SoundTriggerService.class);
1016
1017             if (!disableNonCoreServices) {
1018                 if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_BACKUP)) {
1019                     mSystemServiceManager.startService(BACKUP_MANAGER_SERVICE_CLASS);
1020                 }
1021
1022                 if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_APP_WIDGETS)) {
1023                     mSystemServiceManager.startService(APPWIDGET_SERVICE_CLASS);
1024                 }
1025
1026                 if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_VOICE_RECOGNIZERS)) {
1027                     mSystemServiceManager.startService(VOICE_RECOGNITION_MANAGER_SERVICE_CLASS);
1028                 }
1029
1030                 if (GestureLauncherService.isGestureLauncherEnabled(context.getResources())) {
1031                     Slog.i(TAG, "Gesture Launcher Service");
1032                     mSystemServiceManager.startService(GestureLauncherService.class);
1033                 }
1034                 mSystemServiceManager.startService(SensorNotificationService.class);
1035                 mSystemServiceManager.startService(ContextHubSystemService.class);
1036             }
1037
1038             traceBeginAndSlog("StartDiskStatsService");
1039             try {
1040                 ServiceManager.addService("diskstats", new DiskStatsService(context));
1041             } catch (Throwable e) {
1042                 reportWtf("starting DiskStats Service", e);
1043             }
1044             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1045
1046             if (!disableSamplingProfiler) {
1047                 traceBeginAndSlog("StartSamplingProfilerService");
1048                 try {
1049                     // need to add this service even if SamplingProfilerIntegration.isEnabled()
1050                     // is false, because it is this service that detects system property change and
1051                     // turns on SamplingProfilerIntegration. Plus, when sampling profiler doesn't work,
1052                     // there is little overhead for running this service.
1053                     ServiceManager.addService("samplingprofiler",
1054                                 new SamplingProfilerService(context));
1055                 } catch (Throwable e) {
1056                     reportWtf("starting SamplingProfiler Service", e);
1057                 }
1058                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1059             }
1060
1061             if (!disableNetwork && !disableNetworkTime) {
1062                 traceBeginAndSlog("StartNetworkTimeUpdateService");
1063                 try {
1064                     networkTimeUpdater = new NetworkTimeUpdateService(context);
1065                     ServiceManager.addService("network_time_update_service", networkTimeUpdater);
1066                 } catch (Throwable e) {
1067                     reportWtf("starting NetworkTimeUpdate service", e);
1068                 }
1069                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1070             }
1071
1072             traceBeginAndSlog("StartCommonTimeManagementService");
1073             try {
1074                 commonTimeMgmtService = new CommonTimeManagementService(context);
1075                 ServiceManager.addService("commontime_management", commonTimeMgmtService);
1076             } catch (Throwable e) {
1077                 reportWtf("starting CommonTimeManagementService service", e);
1078             }
1079             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1080
1081             if (!disableNetwork) {
1082                 traceBeginAndSlog("CertBlacklister");
1083                 try {
1084                     CertBlacklister blacklister = new CertBlacklister(context);
1085                 } catch (Throwable e) {
1086                     reportWtf("starting CertBlacklister", e);
1087                 }
1088                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1089             }
1090
1091             if (!disableNonCoreServices) {
1092                 // Dreams (interactive idle-time views, a/k/a screen savers, and doze mode)
1093                 mSystemServiceManager.startService(DreamManagerService.class);
1094             }
1095
1096             if (!disableNonCoreServices && ZygoteInit.PRELOAD_RESOURCES) {
1097                 traceBeginAndSlog("StartAssetAtlasService");
1098                 try {
1099                     atlas = new AssetAtlasService(context);
1100                     ServiceManager.addService(AssetAtlasService.ASSET_ATLAS_SERVICE, atlas);
1101                 } catch (Throwable e) {
1102                     reportWtf("starting AssetAtlasService", e);
1103                 }
1104                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1105             }
1106
1107             if (!disableNonCoreServices) {
1108                 ServiceManager.addService(GraphicsStatsService.GRAPHICS_STATS_SERVICE,
1109                         new GraphicsStatsService(context));
1110             }
1111
1112             if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_PRINTING)) {
1113                 mSystemServiceManager.startService(PRINT_MANAGER_SERVICE_CLASS);
1114             }
1115
1116             mSystemServiceManager.startService(RestrictionsManagerService.class);
1117
1118             mSystemServiceManager.startService(MediaSessionService.class);
1119
1120             if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_HDMI_CEC)) {
1121                 mSystemServiceManager.startService(HdmiControlService.class);
1122             }
1123
1124             if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_LIVE_TV)) {
1125                 mSystemServiceManager.startService(TvInputManagerService.class);
1126             }
1127
1128             mSystemServiceManager.startService(MediaResourceMonitorService.class);
1129
1130             if (!disableNonCoreServices) {
1131                 traceBeginAndSlog("StartMediaRouterService");
1132                 try {
1133                     mediaRouter = new MediaRouterService(context);
1134                     ServiceManager.addService(Context.MEDIA_ROUTER_SERVICE, mediaRouter);
1135                 } catch (Throwable e) {
1136                     reportWtf("starting MediaRouterService", e);
1137                 }
1138                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1139
1140                 if (!disableTrustManager) {
1141                     mSystemServiceManager.startService(TrustManagerService.class);
1142                 }
1143
1144                 if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
1145                     mSystemServiceManager.startService(FingerprintService.class);
1146                 }
1147
1148                 traceBeginAndSlog("StartBackgroundDexOptService");
1149                 try {
1150                     BackgroundDexOptService.schedule(context);
1151                 } catch (Throwable e) {
1152                     reportWtf("starting BackgroundDexOptService", e);
1153                 }
1154                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1155             }
1156             // LauncherAppsService uses ShortcutService.
1157             mSystemServiceManager.startService(ShortcutService.Lifecycle.class);
1158
1159             mSystemServiceManager.startService(LauncherAppsService.class);
1160         }
1161
1162         if (!disableNonCoreServices && !disableMediaProjection) {
1163             mSystemServiceManager.startService(MediaProjectionManagerService.class);
1164         }
1165
1166         // Before things start rolling, be sure we have decided whether
1167         // we are in safe mode.
1168         final boolean safeMode = wm.detectSafeMode();
1169         if (safeMode) {
1170             mActivityManagerService.enterSafeMode();
1171             // Disable the JIT for the system_server process
1172             VMRuntime.getRuntime().disableJitCompilation();
1173         } else {
1174             // Enable the JIT for the system_server process
1175             VMRuntime.getRuntime().startJitCompilation();
1176         }
1177
1178         // MMS service broker
1179         mmsService = mSystemServiceManager.startService(MmsServiceBroker.class);
1180
1181         // It is now time to start up the app processes...
1182
1183         Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeVibratorServiceReady");
1184         try {
1185             vibrator.systemReady();
1186         } catch (Throwable e) {
1187             reportWtf("making Vibrator Service ready", e);
1188         }
1189         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1190
1191         Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeLockSettingsServiceReady");
1192         if (lockSettings != null) {
1193             try {
1194                 lockSettings.systemReady();
1195             } catch (Throwable e) {
1196                 reportWtf("making Lock Settings Service ready", e);
1197             }
1198         }
1199         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1200
1201         // Needed by DevicePolicyManager for initialization
1202         mSystemServiceManager.startBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY);
1203
1204         mSystemServiceManager.startBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
1205
1206         Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeWindowManagerServiceReady");
1207         try {
1208             wm.systemReady();
1209         } catch (Throwable e) {
1210             reportWtf("making Window Manager Service ready", e);
1211         }
1212         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1213
1214         if (safeMode) {
1215             mActivityManagerService.showSafeModeOverlay();
1216         }
1217
1218         // Update the configuration for this context by hand, because we're going
1219         // to start using it before the config change done in wm.systemReady() will
1220         // propagate to it.
1221         Configuration config = wm.computeNewConfiguration();
1222         DisplayMetrics metrics = new DisplayMetrics();
1223         WindowManager w = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
1224         w.getDefaultDisplay().getMetrics(metrics);
1225         context.getResources().updateConfiguration(config, metrics);
1226
1227         // The system context's theme may be configuration-dependent.
1228         final Theme systemTheme = context.getTheme();
1229         if (systemTheme.getChangingConfigurations() != 0) {
1230             systemTheme.rebase();
1231         }
1232
1233         Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakePowerManagerServiceReady");
1234         try {
1235             // TODO: use boot phase
1236             mPowerManagerService.systemReady(mActivityManagerService.getAppOpsService());
1237             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1238         } catch (Throwable e) {
1239             reportWtf("making Power Manager Service ready", e);
1240         }
1241         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1242
1243         Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakePackageManagerServiceReady");
1244         try {
1245             mPackageManagerService.systemReady();
1246         } catch (Throwable e) {
1247             reportWtf("making Package Manager Service ready", e);
1248         }
1249         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1250
1251         Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeDisplayManagerServiceReady");
1252         try {
1253             // TODO: use boot phase and communicate these flags some other way
1254             mDisplayManagerService.systemReady(safeMode, mOnlyCore);
1255         } catch (Throwable e) {
1256             reportWtf("making Display Manager Service ready", e);
1257         }
1258         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1259
1260         // These are needed to propagate to the runnable below.
1261         final NetworkManagementService networkManagementF = networkManagement;
1262         final NetworkStatsService networkStatsF = networkStats;
1263         final NetworkPolicyManagerService networkPolicyF = networkPolicy;
1264         final ConnectivityService connectivityF = connectivity;
1265         final NetworkScoreService networkScoreF = networkScore;
1266         final WallpaperManagerService wallpaperF = wallpaper;
1267         final LocationManagerService locationF = location;
1268         final CountryDetectorService countryDetectorF = countryDetector;
1269         final NetworkTimeUpdateService networkTimeUpdaterF = networkTimeUpdater;
1270         final CommonTimeManagementService commonTimeMgmtServiceF = commonTimeMgmtService;
1271         final StatusBarManagerService statusBarF = statusBar;
1272         final AssetAtlasService atlasF = atlas;
1273         final InputManagerService inputManagerF = inputManager;
1274         final TelephonyRegistry telephonyRegistryF = telephonyRegistry;
1275         final MediaRouterService mediaRouterF = mediaRouter;
1276         final MmsServiceBroker mmsServiceF = mmsService;
1277
1278         // We now tell the activity manager it is okay to run third party
1279         // code.  It will call back into us once it has gotten to the state
1280         // where third party code can really run (but before it has actually
1281         // started launching the initial applications), for us to complete our
1282         // initialization.
1283         mActivityManagerService.systemReady(new Runnable() {
1284             @Override
1285             public void run() {
1286                 Slog.i(TAG, "Making services ready");
1287                 mSystemServiceManager.startBootPhase(
1288                         SystemService.PHASE_ACTIVITY_MANAGER_READY);
1289                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "PhaseActivityManagerReady");
1290
1291                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartObservingNativeCrashes");
1292                 try {
1293                     mActivityManagerService.startObservingNativeCrashes();
1294                 } catch (Throwable e) {
1295                     reportWtf("observing native crashes", e);
1296                 }
1297                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1298
1299                 if (!mOnlyCore) {
1300                     Slog.i(TAG, "WebViewFactory preparation");
1301                     Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "WebViewFactoryPreparation");
1302                     mWebViewUpdateService.prepareWebViewInSystemServer();
1303                     Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1304                 }
1305
1306                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartSystemUI");
1307                 try {
1308                     startSystemUi(context);
1309                 } catch (Throwable e) {
1310                     reportWtf("starting System UI", e);
1311                 }
1312                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1313                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeMountServiceReady");
1314                 try {
1315                     if (networkScoreF != null) networkScoreF.systemReady();
1316                 } catch (Throwable e) {
1317                     reportWtf("making Network Score Service ready", e);
1318                 }
1319                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1320                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeNetworkManagementServiceReady");
1321                 try {
1322                     if (networkManagementF != null) networkManagementF.systemReady();
1323                 } catch (Throwable e) {
1324                     reportWtf("making Network Managment Service ready", e);
1325                 }
1326                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1327                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeNetworkStatsServiceReady");
1328                 try {
1329                     if (networkStatsF != null) networkStatsF.systemReady();
1330                 } catch (Throwable e) {
1331                     reportWtf("making Network Stats Service ready", e);
1332                 }
1333                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1334                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeNetworkPolicyServiceReady");
1335                 try {
1336                     if (networkPolicyF != null) networkPolicyF.systemReady();
1337                 } catch (Throwable e) {
1338                     reportWtf("making Network Policy Service ready", e);
1339                 }
1340                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1341                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeConnectivityServiceReady");
1342                 try {
1343                     if (connectivityF != null) connectivityF.systemReady();
1344                 } catch (Throwable e) {
1345                     reportWtf("making Connectivity Service ready", e);
1346                 }
1347                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1348
1349                 Watchdog.getInstance().start();
1350
1351                 // It is now okay to let the various system services start their
1352                 // third party code...
1353                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1354                 Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "PhaseThirdPartyAppsCanStart");
1355                 mSystemServiceManager.startBootPhase(
1356                         SystemService.PHASE_THIRD_PARTY_APPS_CAN_START);
1357
1358                 try {
1359                     if (wallpaperF != null) wallpaperF.systemRunning();
1360                 } catch (Throwable e) {
1361                     reportWtf("Notifying WallpaperService running", e);
1362                 }
1363                 try {
1364                     if (locationF != null) locationF.systemRunning();
1365                 } catch (Throwable e) {
1366                     reportWtf("Notifying Location Service running", e);
1367                 }
1368                 try {
1369                     if (countryDetectorF != null) countryDetectorF.systemRunning();
1370                 } catch (Throwable e) {
1371                     reportWtf("Notifying CountryDetectorService running", e);
1372                 }
1373                 try {
1374                     if (networkTimeUpdaterF != null) networkTimeUpdaterF.systemRunning();
1375                 } catch (Throwable e) {
1376                     reportWtf("Notifying NetworkTimeService running", e);
1377                 }
1378                 try {
1379                     if (commonTimeMgmtServiceF != null) {
1380                         commonTimeMgmtServiceF.systemRunning();
1381                     }
1382                 } catch (Throwable e) {
1383                     reportWtf("Notifying CommonTimeManagementService running", e);
1384                 }
1385                 try {
1386                     if (atlasF != null) atlasF.systemRunning();
1387                 } catch (Throwable e) {
1388                     reportWtf("Notifying AssetAtlasService running", e);
1389                 }
1390                 try {
1391                     // TODO(BT) Pass parameter to input manager
1392                     if (inputManagerF != null) inputManagerF.systemRunning();
1393                 } catch (Throwable e) {
1394                     reportWtf("Notifying InputManagerService running", e);
1395                 }
1396                 try {
1397                     if (telephonyRegistryF != null) telephonyRegistryF.systemRunning();
1398                 } catch (Throwable e) {
1399                     reportWtf("Notifying TelephonyRegistry running", e);
1400                 }
1401                 try {
1402                     if (mediaRouterF != null) mediaRouterF.systemRunning();
1403                 } catch (Throwable e) {
1404                     reportWtf("Notifying MediaRouterService running", e);
1405                 }
1406
1407                 try {
1408                     if (mmsServiceF != null) mmsServiceF.systemRunning();
1409                 } catch (Throwable e) {
1410                     reportWtf("Notifying MmsService running", e);
1411                 }
1412                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
1413             }
1414         });
1415     }
1416
1417     static final void startSystemUi(Context context) {
1418         Intent intent = new Intent();
1419         intent.setComponent(new ComponentName("com.android.systemui",
1420                     "com.android.systemui.SystemUIService"));
1421         intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
1422         //Slog.d(TAG, "Starting service: " + intent);
1423         context.startServiceAsUser(intent, UserHandle.SYSTEM);
1424     }
1425
1426     private static void traceBeginAndSlog(String name) {
1427         Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, name);
1428         Slog.i(TAG, name);
1429     }
1430 }