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