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