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