SystemServer.java revision 258848d2ae04f447ff1c18023fa76b139fcc0862
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
17package com.android.server;
18
19import android.accounts.AccountManagerService;
20import android.app.ActivityManagerNative;
21import android.bluetooth.BluetoothAdapter;
22import android.content.ComponentName;
23import android.content.ContentResolver;
24import android.content.ContentService;
25import android.content.Context;
26import android.content.Intent;
27import android.content.pm.IPackageManager;
28import android.content.res.Configuration;
29import android.media.AudioService;
30import android.net.wifi.p2p.WifiP2pService;
31import android.os.Looper;
32import android.os.RemoteException;
33import android.os.SchedulingPolicyService;
34import android.os.ServiceManager;
35import android.os.StrictMode;
36import android.os.SystemClock;
37import android.os.SystemProperties;
38import android.provider.Settings;
39import android.server.search.SearchManagerService;
40import android.service.dreams.DreamManagerService;
41import android.util.DisplayMetrics;
42import android.util.EventLog;
43import android.util.Log;
44import android.util.Slog;
45import android.view.Display;
46import android.view.WindowManager;
47
48import com.android.internal.os.BinderInternal;
49import com.android.internal.os.SamplingProfilerIntegration;
50import com.android.internal.widget.LockSettingsService;
51import com.android.server.accessibility.AccessibilityManagerService;
52import com.android.server.am.ActivityManagerService;
53import com.android.server.display.DisplayManagerService;
54import com.android.server.input.InputManagerService;
55import com.android.server.net.NetworkPolicyManagerService;
56import com.android.server.net.NetworkStatsService;
57import com.android.server.pm.PackageManagerService;
58import com.android.server.pm.UserManagerService;
59import com.android.server.power.PowerManagerService;
60import com.android.server.power.ShutdownThread;
61import com.android.server.usb.UsbService;
62import com.android.server.wm.WindowManagerService;
63
64import dalvik.system.VMRuntime;
65import dalvik.system.Zygote;
66
67import java.io.File;
68import java.util.Timer;
69import java.util.TimerTask;
70
71class ServerThread extends Thread {
72    private static final String TAG = "SystemServer";
73    private static final String ENCRYPTING_STATE = "trigger_restart_min_framework";
74    private static final String ENCRYPTED_STATE = "1";
75
76    ContentResolver mContentResolver;
77
78    void reportWtf(String msg, Throwable e) {
79        Slog.w(TAG, "***********************************************");
80        Log.wtf(TAG, "BOOT FAILURE " + msg, e);
81    }
82
83    @Override
84    public void run() {
85        EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN,
86            SystemClock.uptimeMillis());
87
88        Looper.prepareMainLooper();
89
90        android.os.Process.setThreadPriority(
91                android.os.Process.THREAD_PRIORITY_FOREGROUND);
92
93        BinderInternal.disableBackgroundScheduling(true);
94        android.os.Process.setCanSelfBackground(false);
95
96        // Check whether we failed to shut down last time we tried.
97        {
98            final String shutdownAction = SystemProperties.get(
99                    ShutdownThread.SHUTDOWN_ACTION_PROPERTY, "");
100            if (shutdownAction != null && shutdownAction.length() > 0) {
101                boolean reboot = (shutdownAction.charAt(0) == '1');
102
103                final String reason;
104                if (shutdownAction.length() > 1) {
105                    reason = shutdownAction.substring(1, shutdownAction.length());
106                } else {
107                    reason = null;
108                }
109
110                ShutdownThread.rebootOrShutdown(reboot, reason);
111            }
112        }
113
114        String factoryTestStr = SystemProperties.get("ro.factorytest");
115        int factoryTest = "".equals(factoryTestStr) ? SystemServer.FACTORY_TEST_OFF
116                : Integer.parseInt(factoryTestStr);
117        final boolean headless = "1".equals(SystemProperties.get("ro.config.headless", "0"));
118
119        AccountManagerService accountManager = null;
120        ContentService contentService = null;
121        LightsService lights = null;
122        PowerManagerService power = null;
123        DisplayManagerService display = null;
124        BatteryService battery = null;
125        VibratorService vibrator = null;
126        AlarmManagerService alarm = null;
127        NetworkManagementService networkManagement = null;
128        NetworkStatsService networkStats = null;
129        NetworkPolicyManagerService networkPolicy = null;
130        ConnectivityService connectivity = null;
131        WifiP2pService wifiP2p = null;
132        WifiService wifi = null;
133        NsdService serviceDiscovery= null;
134        IPackageManager pm = null;
135        Context context = null;
136        WindowManagerService wm = null;
137        BluetoothManagerService bluetooth = null;
138        DockObserver dock = null;
139        UsbService usb = null;
140        SerialService serial = null;
141        UiModeManagerService uiMode = null;
142        RecognitionManagerService recognition = null;
143        ThrottleService throttle = null;
144        NetworkTimeUpdateService networkTimeUpdater = null;
145        CommonTimeManagementService commonTimeMgmtService = null;
146        InputManagerService inputManager = null;
147
148        // Critical services...
149        try {
150            Slog.i(TAG, "Entropy Mixer");
151            ServiceManager.addService("entropy", new EntropyMixer());
152
153            Slog.i(TAG, "Power Manager");
154            power = new PowerManagerService();
155            ServiceManager.addService(Context.POWER_SERVICE, power);
156
157            Slog.i(TAG, "Display Manager");
158            display = new DisplayManagerService();
159            ServiceManager.addService(Context.DISPLAY_SERVICE, display, true);
160
161            Slog.i(TAG, "Activity Manager");
162            context = ActivityManagerService.main(factoryTest);
163            display.setContext(context);
164
165            Slog.i(TAG, "Telephony Registry");
166            ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));
167
168            Slog.i(TAG, "Scheduling Policy");
169            ServiceManager.addService(Context.SCHEDULING_POLICY_SERVICE,
170                    new SchedulingPolicyService());
171
172            AttributeCache.init(context);
173
174            Slog.i(TAG, "Package Manager");
175            // Only run "core" apps if we're encrypting the device.
176            String cryptState = SystemProperties.get("vold.decrypt");
177            boolean onlyCore = false;
178            if (ENCRYPTING_STATE.equals(cryptState)) {
179                Slog.w(TAG, "Detected encryption in progress - only parsing core apps");
180                onlyCore = true;
181            } else if (ENCRYPTED_STATE.equals(cryptState)) {
182                Slog.w(TAG, "Device encrypted - only parsing core apps");
183                onlyCore = true;
184            }
185
186            pm = PackageManagerService.main(context,
187                    factoryTest != SystemServer.FACTORY_TEST_OFF,
188                    onlyCore);
189            boolean firstBoot = false;
190            try {
191                firstBoot = pm.isFirstBoot();
192            } catch (RemoteException e) {
193            }
194
195            ActivityManagerService.setSystemProcess();
196
197            Slog.i(TAG, "User Service");
198            ServiceManager.addService(Context.USER_SERVICE,
199                    UserManagerService.getInstance(context));
200
201
202            mContentResolver = context.getContentResolver();
203
204            // The AccountManager must come before the ContentService
205            try {
206                Slog.i(TAG, "Account Manager");
207                accountManager = new AccountManagerService(context);
208                ServiceManager.addService(Context.ACCOUNT_SERVICE, accountManager);
209            } catch (Throwable e) {
210                Slog.e(TAG, "Failure starting Account Manager", e);
211            }
212
213            Slog.i(TAG, "Content Manager");
214            contentService = ContentService.main(context,
215                    factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
216
217            Slog.i(TAG, "System Content Providers");
218            ActivityManagerService.installSystemProviders();
219
220            Slog.i(TAG, "Lights Service");
221            lights = new LightsService(context);
222
223            Slog.i(TAG, "Battery Service");
224            battery = new BatteryService(context, lights);
225            ServiceManager.addService("battery", battery);
226
227            Slog.i(TAG, "Vibrator Service");
228            vibrator = new VibratorService(context);
229            ServiceManager.addService("vibrator", vibrator);
230
231            // only initialize the power service after we have started the
232            // lights service, content providers and the battery service.
233            power.init(context, lights, ActivityManagerService.self(), battery, display);
234
235            Slog.i(TAG, "Alarm Manager");
236            alarm = new AlarmManagerService(context);
237            ServiceManager.addService(Context.ALARM_SERVICE, alarm);
238
239            Slog.i(TAG, "Init Watchdog");
240            Watchdog.getInstance().init(context, battery, power, alarm,
241                    ActivityManagerService.self());
242
243            Slog.i(TAG, "Window Manager");
244            wm = WindowManagerService.main(context, power, display,
245                    factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL,
246                    !firstBoot, onlyCore);
247            ServiceManager.addService(Context.WINDOW_SERVICE, wm);
248            inputManager = wm.getInputManagerService();
249            ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
250
251            ActivityManagerService.self().setWindowManager(wm);
252
253            // Skip Bluetooth if we have an emulator kernel
254            // TODO: Use a more reliable check to see if this product should
255            // support Bluetooth - see bug 988521
256            if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
257                Slog.i(TAG, "No Bluetooh Service (emulator)");
258            } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
259                Slog.i(TAG, "No Bluetooth Service (factory test)");
260            } else {
261                Slog.i(TAG, "Bluetooth Manager Service");
262                bluetooth = new BluetoothManagerService(context);
263                ServiceManager.addService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE, bluetooth);
264            }
265
266        } catch (RuntimeException e) {
267            Slog.e("System", "******************************************");
268            Slog.e("System", "************ Failure starting core service", e);
269        }
270
271        DevicePolicyManagerService devicePolicy = null;
272        StatusBarManagerService statusBar = null;
273        InputMethodManagerService imm = null;
274        AppWidgetService appWidget = null;
275        NotificationManagerService notification = null;
276        WallpaperManagerService wallpaper = null;
277        LocationManagerService location = null;
278        CountryDetectorService countryDetector = null;
279        TextServicesManagerService tsms = null;
280        LockSettingsService lockSettings = null;
281        DreamManagerService dreamy = null;
282
283        // Bring up services needed for UI.
284        if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
285            try {
286                Slog.i(TAG, "Input Method Service");
287                imm = new InputMethodManagerService(context, wm);
288                ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
289            } catch (Throwable e) {
290                reportWtf("starting Input Manager Service", e);
291            }
292
293            try {
294                Slog.i(TAG, "Accessibility Manager");
295                ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
296                        new AccessibilityManagerService(context));
297            } catch (Throwable e) {
298                reportWtf("starting Accessibility Manager", e);
299            }
300        }
301
302        try {
303            wm.displayReady();
304        } catch (Throwable e) {
305            reportWtf("making display ready", e);
306        }
307
308        try {
309            pm.performBootDexOpt();
310        } catch (Throwable e) {
311            reportWtf("performing boot dexopt", e);
312        }
313
314        try {
315            ActivityManagerNative.getDefault().showBootMessage(
316                    context.getResources().getText(
317                            com.android.internal.R.string.android_upgrading_starting_apps),
318                            false);
319        } catch (RemoteException e) {
320        }
321
322        if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
323            MountService mountService = null;
324            if (!"0".equals(SystemProperties.get("system_init.startmountservice"))) {
325                try {
326                    /*
327                     * NotificationManagerService is dependant on MountService,
328                     * (for media / usb notifications) so we must start MountService first.
329                     */
330                    Slog.i(TAG, "Mount Service");
331                    mountService = new MountService(context);
332                    ServiceManager.addService("mount", mountService);
333                } catch (Throwable e) {
334                    reportWtf("starting Mount Service", e);
335                }
336            }
337
338            try {
339                Slog.i(TAG,  "LockSettingsService");
340                lockSettings = new LockSettingsService(context);
341                ServiceManager.addService("lock_settings", lockSettings);
342            } catch (Throwable e) {
343                reportWtf("starting LockSettingsService service", e);
344            }
345
346            try {
347                Slog.i(TAG, "Device Policy");
348                devicePolicy = new DevicePolicyManagerService(context);
349                ServiceManager.addService(Context.DEVICE_POLICY_SERVICE, devicePolicy);
350            } catch (Throwable e) {
351                reportWtf("starting DevicePolicyService", e);
352            }
353
354            try {
355                Slog.i(TAG, "Status Bar");
356                statusBar = new StatusBarManagerService(context, wm);
357                ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);
358            } catch (Throwable e) {
359                reportWtf("starting StatusBarManagerService", e);
360            }
361
362            try {
363                Slog.i(TAG, "Clipboard Service");
364                ServiceManager.addService(Context.CLIPBOARD_SERVICE,
365                        new ClipboardService(context));
366            } catch (Throwable e) {
367                reportWtf("starting Clipboard Service", e);
368            }
369
370            try {
371                Slog.i(TAG, "NetworkManagement Service");
372                networkManagement = NetworkManagementService.create(context);
373                ServiceManager.addService(Context.NETWORKMANAGEMENT_SERVICE, networkManagement);
374            } catch (Throwable e) {
375                reportWtf("starting NetworkManagement Service", e);
376            }
377
378            try {
379                Slog.i(TAG, "Text Service Manager Service");
380                tsms = new TextServicesManagerService(context);
381                ServiceManager.addService(Context.TEXT_SERVICES_MANAGER_SERVICE, tsms);
382            } catch (Throwable e) {
383                reportWtf("starting Text Service Manager Service", e);
384            }
385
386            try {
387                Slog.i(TAG, "NetworkStats Service");
388                networkStats = new NetworkStatsService(context, networkManagement, alarm);
389                ServiceManager.addService(Context.NETWORK_STATS_SERVICE, networkStats);
390            } catch (Throwable e) {
391                reportWtf("starting NetworkStats Service", e);
392            }
393
394            try {
395                Slog.i(TAG, "NetworkPolicy Service");
396                networkPolicy = new NetworkPolicyManagerService(
397                        context, ActivityManagerService.self(), power,
398                        networkStats, networkManagement);
399                ServiceManager.addService(Context.NETWORK_POLICY_SERVICE, networkPolicy);
400            } catch (Throwable e) {
401                reportWtf("starting NetworkPolicy Service", e);
402            }
403
404           try {
405                Slog.i(TAG, "Wi-Fi P2pService");
406                wifiP2p = new WifiP2pService(context);
407                ServiceManager.addService(Context.WIFI_P2P_SERVICE, wifiP2p);
408            } catch (Throwable e) {
409                reportWtf("starting Wi-Fi P2pService", e);
410            }
411
412           try {
413                Slog.i(TAG, "Wi-Fi Service");
414                wifi = new WifiService(context);
415                ServiceManager.addService(Context.WIFI_SERVICE, wifi);
416            } catch (Throwable e) {
417                reportWtf("starting Wi-Fi Service", e);
418            }
419
420            try {
421                Slog.i(TAG, "Connectivity Service");
422                connectivity = new ConnectivityService(
423                        context, networkManagement, networkStats, networkPolicy);
424                ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
425                networkStats.bindConnectivityManager(connectivity);
426                networkPolicy.bindConnectivityManager(connectivity);
427                wifi.checkAndStartWifi();
428                wifiP2p.connectivityServiceReady();
429            } catch (Throwable e) {
430                reportWtf("starting Connectivity Service", e);
431            }
432
433            try {
434                Slog.i(TAG, "Network Service Discovery Service");
435                serviceDiscovery = NsdService.create(context);
436                ServiceManager.addService(
437                        Context.NSD_SERVICE, serviceDiscovery);
438            } catch (Throwable e) {
439                reportWtf("starting Service Discovery Service", e);
440            }
441
442            try {
443                Slog.i(TAG, "Throttle Service");
444                throttle = new ThrottleService(context);
445                ServiceManager.addService(
446                        Context.THROTTLE_SERVICE, throttle);
447            } catch (Throwable e) {
448                reportWtf("starting ThrottleService", e);
449            }
450
451            try {
452                Slog.i(TAG, "UpdateLock Service");
453                ServiceManager.addService(Context.UPDATE_LOCK_SERVICE,
454                        new UpdateLockService(context));
455            } catch (Throwable e) {
456                reportWtf("starting UpdateLockService", e);
457            }
458
459            /*
460             * MountService has a few dependencies: Notification Manager and
461             * AppWidget Provider. Make sure MountService is completely started
462             * first before continuing.
463             */
464            if (mountService != null) {
465                mountService.waitForAsecScan();
466            }
467
468            try {
469                if (accountManager != null)
470                    accountManager.systemReady();
471            } catch (Throwable e) {
472                reportWtf("making Account Manager Service ready", e);
473            }
474
475            try {
476                if (contentService != null)
477                    contentService.systemReady();
478            } catch (Throwable e) {
479                reportWtf("making Content Service ready", e);
480            }
481
482            try {
483                Slog.i(TAG, "Notification Manager");
484                notification = new NotificationManagerService(context, statusBar, lights);
485                ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
486                networkPolicy.bindNotificationManager(notification);
487            } catch (Throwable e) {
488                reportWtf("starting Notification Manager", e);
489            }
490
491            try {
492                Slog.i(TAG, "Device Storage Monitor");
493                ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
494                        new DeviceStorageMonitorService(context));
495            } catch (Throwable e) {
496                reportWtf("starting DeviceStorageMonitor service", e);
497            }
498
499            try {
500                Slog.i(TAG, "Location Manager");
501                location = new LocationManagerService(context);
502                ServiceManager.addService(Context.LOCATION_SERVICE, location);
503            } catch (Throwable e) {
504                reportWtf("starting Location Manager", e);
505            }
506
507            try {
508                Slog.i(TAG, "Country Detector");
509                countryDetector = new CountryDetectorService(context);
510                ServiceManager.addService(Context.COUNTRY_DETECTOR, countryDetector);
511            } catch (Throwable e) {
512                reportWtf("starting Country Detector", e);
513            }
514
515            try {
516                Slog.i(TAG, "Search Service");
517                ServiceManager.addService(Context.SEARCH_SERVICE,
518                        new SearchManagerService(context));
519            } catch (Throwable e) {
520                reportWtf("starting Search Service", e);
521            }
522
523            try {
524                Slog.i(TAG, "DropBox Service");
525                ServiceManager.addService(Context.DROPBOX_SERVICE,
526                        new DropBoxManagerService(context, new File("/data/system/dropbox")));
527            } catch (Throwable e) {
528                reportWtf("starting DropBoxManagerService", e);
529            }
530
531            if (context.getResources().getBoolean(
532                        com.android.internal.R.bool.config_enableWallpaperService)) {
533                try {
534                    Slog.i(TAG, "Wallpaper Service");
535                    if (!headless) {
536                        wallpaper = new WallpaperManagerService(context);
537                        ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);
538                    }
539                } catch (Throwable e) {
540                    reportWtf("starting Wallpaper Service", e);
541                }
542            }
543
544            if (!"0".equals(SystemProperties.get("system_init.startaudioservice"))) {
545                try {
546                    Slog.i(TAG, "Audio Service");
547                    ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
548                } catch (Throwable e) {
549                    reportWtf("starting Audio Service", e);
550                }
551            }
552
553            try {
554                Slog.i(TAG, "Dock Observer");
555                // Listen for dock station changes
556                dock = new DockObserver(context, power);
557            } catch (Throwable e) {
558                reportWtf("starting DockObserver", e);
559            }
560
561            try {
562                Slog.i(TAG, "Wired Accessory Observer");
563                // Listen for wired headset changes
564                new WiredAccessoryObserver(context);
565            } catch (Throwable e) {
566                reportWtf("starting WiredAccessoryObserver", e);
567            }
568
569            try {
570                Slog.i(TAG, "USB Service");
571                // Manage USB host and device support
572                usb = new UsbService(context);
573                ServiceManager.addService(Context.USB_SERVICE, usb);
574            } catch (Throwable e) {
575                reportWtf("starting UsbService", e);
576            }
577
578            try {
579                Slog.i(TAG, "Serial Service");
580                // Serial port support
581                serial = new SerialService(context);
582                ServiceManager.addService(Context.SERIAL_SERVICE, serial);
583            } catch (Throwable e) {
584                Slog.e(TAG, "Failure starting SerialService", e);
585            }
586
587            try {
588                Slog.i(TAG, "UI Mode Manager Service");
589                // Listen for UI mode changes
590                uiMode = new UiModeManagerService(context);
591            } catch (Throwable e) {
592                reportWtf("starting UiModeManagerService", e);
593            }
594
595            try {
596                Slog.i(TAG, "Backup Service");
597                ServiceManager.addService(Context.BACKUP_SERVICE,
598                        new BackupManagerService(context));
599            } catch (Throwable e) {
600                Slog.e(TAG, "Failure starting Backup Service", e);
601            }
602
603            try {
604                Slog.i(TAG, "AppWidget Service");
605                appWidget = new AppWidgetService(context);
606                ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
607            } catch (Throwable e) {
608                reportWtf("starting AppWidget Service", e);
609            }
610
611            try {
612                Slog.i(TAG, "Recognition Service");
613                recognition = new RecognitionManagerService(context);
614            } catch (Throwable e) {
615                reportWtf("starting Recognition Service", e);
616            }
617
618            try {
619                Slog.i(TAG, "DiskStats Service");
620                ServiceManager.addService("diskstats", new DiskStatsService(context));
621            } catch (Throwable e) {
622                reportWtf("starting DiskStats Service", e);
623            }
624
625            try {
626                // need to add this service even if SamplingProfilerIntegration.isEnabled()
627                // is false, because it is this service that detects system property change and
628                // turns on SamplingProfilerIntegration. Plus, when sampling profiler doesn't work,
629                // there is little overhead for running this service.
630                Slog.i(TAG, "SamplingProfiler Service");
631                ServiceManager.addService("samplingprofiler",
632                            new SamplingProfilerService(context));
633            } catch (Throwable e) {
634                reportWtf("starting SamplingProfiler Service", e);
635            }
636
637            try {
638                Slog.i(TAG, "NetworkTimeUpdateService");
639                networkTimeUpdater = new NetworkTimeUpdateService(context);
640            } catch (Throwable e) {
641                reportWtf("starting NetworkTimeUpdate service", e);
642            }
643
644            try {
645                Slog.i(TAG, "CommonTimeManagementService");
646                commonTimeMgmtService = new CommonTimeManagementService(context);
647                ServiceManager.addService("commontime_management", commonTimeMgmtService);
648            } catch (Throwable e) {
649                reportWtf("starting CommonTimeManagementService service", e);
650            }
651
652            try {
653                Slog.i(TAG, "CertBlacklister");
654                CertBlacklister blacklister = new CertBlacklister(context);
655            } catch (Throwable e) {
656                reportWtf("starting CertBlacklister", e);
657            }
658
659            if (context.getResources().getBoolean(
660                    com.android.internal.R.bool.config_enableDreams)) {
661                try {
662                    Slog.i(TAG, "Dreams Service");
663                    // Dreams (interactive idle-time views, a/k/a screen savers)
664                    dreamy = new DreamManagerService(context);
665                    ServiceManager.addService("dreams", dreamy);
666                } catch (Throwable e) {
667                    reportWtf("starting DreamManagerService", e);
668                }
669            }
670        }
671
672        // Before things start rolling, be sure we have decided whether
673        // we are in safe mode.
674        final boolean safeMode = wm.detectSafeMode();
675        if (safeMode) {
676            ActivityManagerService.self().enterSafeMode();
677            // Post the safe mode state in the Zygote class
678            Zygote.systemInSafeMode = true;
679            // Disable the JIT for the system_server process
680            VMRuntime.getRuntime().disableJitCompilation();
681        } else {
682            // Enable the JIT for the system_server process
683            VMRuntime.getRuntime().startJitCompilation();
684        }
685
686        // It is now time to start up the app processes...
687
688        try {
689            vibrator.systemReady();
690        } catch (Throwable e) {
691            reportWtf("making Vibrator Service ready", e);
692        }
693
694        if (devicePolicy != null) {
695            try {
696                devicePolicy.systemReady();
697            } catch (Throwable e) {
698                reportWtf("making Device Policy Service ready", e);
699            }
700        }
701
702        if (notification != null) {
703            try {
704                notification.systemReady();
705            } catch (Throwable e) {
706                reportWtf("making Notification Service ready", e);
707            }
708        }
709
710        try {
711            lockSettings.systemReady();
712        } catch (Throwable e) {
713            reportWtf("making Lock Settings Service ready", e);
714        }
715
716        try {
717            wm.systemReady();
718        } catch (Throwable e) {
719            reportWtf("making Window Manager Service ready", e);
720        }
721
722        if (safeMode) {
723            ActivityManagerService.self().showSafeModeOverlay();
724        }
725
726        // Update the configuration for this context by hand, because we're going
727        // to start using it before the config change done in wm.systemReady() will
728        // propagate to it.
729        Configuration config = wm.computeNewConfiguration();
730        DisplayMetrics metrics = new DisplayMetrics();
731        WindowManager w = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
732        w.getDefaultDisplay().getMetrics(metrics);
733        context.getResources().updateConfiguration(config, metrics);
734
735        power.systemReady();
736        try {
737            pm.systemReady();
738        } catch (Throwable e) {
739            reportWtf("making Package Manager Service ready", e);
740        }
741
742        // These are needed to propagate to the runnable below.
743        final Context contextF = context;
744        final BatteryService batteryF = battery;
745        final NetworkManagementService networkManagementF = networkManagement;
746        final NetworkStatsService networkStatsF = networkStats;
747        final NetworkPolicyManagerService networkPolicyF = networkPolicy;
748        final ConnectivityService connectivityF = connectivity;
749        final DockObserver dockF = dock;
750        final UsbService usbF = usb;
751        final ThrottleService throttleF = throttle;
752        final UiModeManagerService uiModeF = uiMode;
753        final AppWidgetService appWidgetF = appWidget;
754        final WallpaperManagerService wallpaperF = wallpaper;
755        final InputMethodManagerService immF = imm;
756        final RecognitionManagerService recognitionF = recognition;
757        final LocationManagerService locationF = location;
758        final CountryDetectorService countryDetectorF = countryDetector;
759        final NetworkTimeUpdateService networkTimeUpdaterF = networkTimeUpdater;
760        final CommonTimeManagementService commonTimeMgmtServiceF = commonTimeMgmtService;
761        final TextServicesManagerService textServiceManagerServiceF = tsms;
762        final StatusBarManagerService statusBarF = statusBar;
763        final DreamManagerService dreamyF = dreamy;
764        final InputManagerService inputManagerF = inputManager;
765
766        // We now tell the activity manager it is okay to run third party
767        // code.  It will call back into us once it has gotten to the state
768        // where third party code can really run (but before it has actually
769        // started launching the initial applications), for us to complete our
770        // initialization.
771        ActivityManagerService.self().systemReady(new Runnable() {
772            public void run() {
773                Slog.i(TAG, "Making services ready");
774
775                if (!headless) startSystemUi(contextF);
776                try {
777                    if (batteryF != null) batteryF.systemReady();
778                } catch (Throwable e) {
779                    reportWtf("making Battery Service ready", e);
780                }
781                try {
782                    if (networkManagementF != null) networkManagementF.systemReady();
783                } catch (Throwable e) {
784                    reportWtf("making Network Managment Service ready", e);
785                }
786                try {
787                    if (networkStatsF != null) networkStatsF.systemReady();
788                } catch (Throwable e) {
789                    reportWtf("making Network Stats Service ready", e);
790                }
791                try {
792                    if (networkPolicyF != null) networkPolicyF.systemReady();
793                } catch (Throwable e) {
794                    reportWtf("making Network Policy Service ready", e);
795                }
796                try {
797                    if (connectivityF != null) connectivityF.systemReady();
798                } catch (Throwable e) {
799                    reportWtf("making Connectivity Service ready", e);
800                }
801                try {
802                    if (dockF != null) dockF.systemReady();
803                } catch (Throwable e) {
804                    reportWtf("making Dock Service ready", e);
805                }
806                try {
807                    if (usbF != null) usbF.systemReady();
808                } catch (Throwable e) {
809                    reportWtf("making USB Service ready", e);
810                }
811                try {
812                    if (uiModeF != null) uiModeF.systemReady();
813                } catch (Throwable e) {
814                    reportWtf("making UI Mode Service ready", e);
815                }
816                try {
817                    if (recognitionF != null) recognitionF.systemReady();
818                } catch (Throwable e) {
819                    reportWtf("making Recognition Service ready", e);
820                }
821                Watchdog.getInstance().start();
822
823                // It is now okay to let the various system services start their
824                // third party code...
825
826                try {
827                    if (appWidgetF != null) appWidgetF.systemReady(safeMode);
828                } catch (Throwable e) {
829                    reportWtf("making App Widget Service ready", e);
830                }
831                try {
832                    if (wallpaperF != null) wallpaperF.systemReady();
833                } catch (Throwable e) {
834                    reportWtf("making Wallpaper Service ready", e);
835                }
836                try {
837                    if (immF != null) immF.systemReady(statusBarF);
838                } catch (Throwable e) {
839                    reportWtf("making Input Method Service ready", e);
840                }
841                try {
842                    if (locationF != null) locationF.systemReady();
843                } catch (Throwable e) {
844                    reportWtf("making Location Service ready", e);
845                }
846                try {
847                    if (countryDetectorF != null) countryDetectorF.systemReady();
848                } catch (Throwable e) {
849                    reportWtf("making Country Detector Service ready", e);
850                }
851                try {
852                    if (throttleF != null) throttleF.systemReady();
853                } catch (Throwable e) {
854                    reportWtf("making Throttle Service ready", e);
855                }
856                try {
857                    if (networkTimeUpdaterF != null) networkTimeUpdaterF.systemReady();
858                } catch (Throwable e) {
859                    reportWtf("making Network Time Service ready", e);
860                }
861                try {
862                    if (commonTimeMgmtServiceF != null) commonTimeMgmtServiceF.systemReady();
863                } catch (Throwable e) {
864                    reportWtf("making Common time management service ready", e);
865                }
866                try {
867                    if (textServiceManagerServiceF != null) textServiceManagerServiceF.systemReady();
868                } catch (Throwable e) {
869                    reportWtf("making Text Services Manager Service ready", e);
870                }
871                try {
872                    if (dreamyF != null) dreamyF.systemReady();
873                } catch (Throwable e) {
874                    reportWtf("making DreamManagerService ready", e);
875                }
876                try {
877                    // TODO(BT) Pass parameter to input manager
878                    if (inputManagerF != null) inputManagerF.systemReady();
879                } catch (Throwable e) {
880                    reportWtf("making InputManagerService ready", e);
881                }
882            }
883        });
884
885        // For debug builds, log event loop stalls to dropbox for analysis.
886        if (StrictMode.conditionallyEnableDebugLogging()) {
887            Slog.i(TAG, "Enabled StrictMode for system server main thread.");
888        }
889
890        Looper.loop();
891        Slog.d(TAG, "System ServerThread is exiting!");
892    }
893
894    static final void startSystemUi(Context context) {
895        Intent intent = new Intent();
896        intent.setComponent(new ComponentName("com.android.systemui",
897                    "com.android.systemui.SystemUIService"));
898        Slog.d(TAG, "Starting service: " + intent);
899        context.startService(intent);
900    }
901}
902
903public class SystemServer {
904    private static final String TAG = "SystemServer";
905
906    public static final int FACTORY_TEST_OFF = 0;
907    public static final int FACTORY_TEST_LOW_LEVEL = 1;
908    public static final int FACTORY_TEST_HIGH_LEVEL = 2;
909
910    static Timer timer;
911    static final long SNAPSHOT_INTERVAL = 60 * 60 * 1000; // 1hr
912
913    // The earliest supported time.  We pick one day into 1970, to
914    // give any timezone code room without going into negative time.
915    private static final long EARLIEST_SUPPORTED_TIME = 86400 * 1000;
916
917    /**
918     * This method is called from Zygote to initialize the system. This will cause the native
919     * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
920     * up into init2() to start the Android services.
921     */
922    native public static void init1(String[] args);
923
924    public static void main(String[] args) {
925        if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
926            // If a device's clock is before 1970 (before 0), a lot of
927            // APIs crash dealing with negative numbers, notably
928            // java.io.File#setLastModified, so instead we fake it and
929            // hope that time from cell towers or NTP fixes it
930            // shortly.
931            Slog.w(TAG, "System clock is before 1970; setting to 1970.");
932            SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
933        }
934
935        if (SamplingProfilerIntegration.isEnabled()) {
936            SamplingProfilerIntegration.start();
937            timer = new Timer();
938            timer.schedule(new TimerTask() {
939                @Override
940                public void run() {
941                    SamplingProfilerIntegration.writeSnapshot("system_server", null);
942                }
943            }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
944        }
945
946        // Mmmmmm... more memory!
947        dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
948
949        // The system server has to run all of the time, so it needs to be
950        // as efficient as possible with its memory usage.
951        VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
952
953        System.loadLibrary("android_servers");
954        init1(args);
955    }
956
957    public static final void init2() {
958        Slog.i(TAG, "Entered the Android system server!");
959        Thread thr = new ServerThread();
960        thr.setName("android.server.ServerThread");
961        thr.start();
962    }
963}
964