SystemServer.java revision 6ec6f79e1ac1714e3b837796e99f07ff88f66601
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 airplaneModeOn = Settings.System.getInt(mContentResolver,
255                        Settings.System.AIRPLANE_MODE_ON, 0);
256                int bluetoothOn = Settings.Secure.getInt(mContentResolver,
257                    Settings.Secure.BLUETOOTH_ON, 0);
258                if (airplaneModeOn == 0 && bluetoothOn != 0) {
259                    bluetooth.enable();
260                }
261            }
262
263        } catch (RuntimeException e) {
264            Slog.e("System", "******************************************");
265            Slog.e("System", "************ Failure starting core service", e);
266        }
267
268        DevicePolicyManagerService devicePolicy = null;
269        StatusBarManagerService statusBar = null;
270        InputMethodManagerService imm = null;
271        AppWidgetService appWidget = null;
272        NotificationManagerService notification = null;
273        WallpaperManagerService wallpaper = null;
274        LocationManagerService location = null;
275        CountryDetectorService countryDetector = null;
276        TextServicesManagerService tsms = null;
277        LockSettingsService lockSettings = null;
278        DreamManagerService dreamy = null;
279
280        // Bring up services needed for UI.
281        if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
282            try {
283                Slog.i(TAG, "Input Method Service");
284                imm = new InputMethodManagerService(context, wm);
285                ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
286            } catch (Throwable e) {
287                reportWtf("starting Input Manager Service", e);
288            }
289
290            try {
291                Slog.i(TAG, "Accessibility Manager");
292                ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
293                        new AccessibilityManagerService(context));
294            } catch (Throwable e) {
295                reportWtf("starting Accessibility Manager", e);
296            }
297        }
298
299        try {
300            wm.displayReady();
301        } catch (Throwable e) {
302            reportWtf("making display ready", e);
303        }
304
305        try {
306            pm.performBootDexOpt();
307        } catch (Throwable e) {
308            reportWtf("performing boot dexopt", e);
309        }
310
311        try {
312            ActivityManagerNative.getDefault().showBootMessage(
313                    context.getResources().getText(
314                            com.android.internal.R.string.android_upgrading_starting_apps),
315                            false);
316        } catch (RemoteException e) {
317        }
318
319        if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
320            try {
321                Slog.i(TAG,  "LockSettingsService");
322                lockSettings = new LockSettingsService(context);
323                ServiceManager.addService("lock_settings", lockSettings);
324            } catch (Throwable e) {
325                reportWtf("starting LockSettingsService service", e);
326            }
327
328            try {
329                Slog.i(TAG, "Device Policy");
330                devicePolicy = new DevicePolicyManagerService(context);
331                ServiceManager.addService(Context.DEVICE_POLICY_SERVICE, devicePolicy);
332            } catch (Throwable e) {
333                reportWtf("starting DevicePolicyService", e);
334            }
335
336            try {
337                Slog.i(TAG, "Status Bar");
338                statusBar = new StatusBarManagerService(context, wm);
339                ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);
340            } catch (Throwable e) {
341                reportWtf("starting StatusBarManagerService", e);
342            }
343
344            try {
345                Slog.i(TAG, "Clipboard Service");
346                ServiceManager.addService(Context.CLIPBOARD_SERVICE,
347                        new ClipboardService(context));
348            } catch (Throwable e) {
349                reportWtf("starting Clipboard Service", e);
350            }
351
352            try {
353                Slog.i(TAG, "NetworkManagement Service");
354                networkManagement = NetworkManagementService.create(context);
355                ServiceManager.addService(Context.NETWORKMANAGEMENT_SERVICE, networkManagement);
356            } catch (Throwable e) {
357                reportWtf("starting NetworkManagement Service", e);
358            }
359
360            try {
361                Slog.i(TAG, "Text Service Manager Service");
362                tsms = new TextServicesManagerService(context);
363                ServiceManager.addService(Context.TEXT_SERVICES_MANAGER_SERVICE, tsms);
364            } catch (Throwable e) {
365                reportWtf("starting Text Service Manager Service", e);
366            }
367
368            try {
369                Slog.i(TAG, "NetworkStats Service");
370                networkStats = new NetworkStatsService(context, networkManagement, alarm);
371                ServiceManager.addService(Context.NETWORK_STATS_SERVICE, networkStats);
372            } catch (Throwable e) {
373                reportWtf("starting NetworkStats Service", e);
374            }
375
376            try {
377                Slog.i(TAG, "NetworkPolicy Service");
378                networkPolicy = new NetworkPolicyManagerService(
379                        context, ActivityManagerService.self(), power,
380                        networkStats, networkManagement);
381                ServiceManager.addService(Context.NETWORK_POLICY_SERVICE, networkPolicy);
382            } catch (Throwable e) {
383                reportWtf("starting NetworkPolicy Service", e);
384            }
385
386           try {
387                Slog.i(TAG, "Wi-Fi P2pService");
388                wifiP2p = new WifiP2pService(context);
389                ServiceManager.addService(Context.WIFI_P2P_SERVICE, wifiP2p);
390            } catch (Throwable e) {
391                reportWtf("starting Wi-Fi P2pService", e);
392            }
393
394           try {
395                Slog.i(TAG, "Wi-Fi Service");
396                wifi = new WifiService(context);
397                ServiceManager.addService(Context.WIFI_SERVICE, wifi);
398            } catch (Throwable e) {
399                reportWtf("starting Wi-Fi Service", e);
400            }
401
402            try {
403                Slog.i(TAG, "Connectivity Service");
404                connectivity = new ConnectivityService(
405                        context, networkManagement, networkStats, networkPolicy);
406                ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
407                networkStats.bindConnectivityManager(connectivity);
408                networkPolicy.bindConnectivityManager(connectivity);
409                wifi.checkAndStartWifi();
410                wifiP2p.connectivityServiceReady();
411            } catch (Throwable e) {
412                reportWtf("starting Connectivity Service", e);
413            }
414
415            try {
416                Slog.i(TAG, "Network Service Discovery Service");
417                serviceDiscovery = NsdService.create(context);
418                ServiceManager.addService(
419                        Context.NSD_SERVICE, serviceDiscovery);
420            } catch (Throwable e) {
421                reportWtf("starting Service Discovery Service", e);
422            }
423
424            try {
425                Slog.i(TAG, "Throttle Service");
426                throttle = new ThrottleService(context);
427                ServiceManager.addService(
428                        Context.THROTTLE_SERVICE, throttle);
429            } catch (Throwable e) {
430                reportWtf("starting ThrottleService", e);
431            }
432
433            try {
434                Slog.i(TAG, "UpdateLock Service");
435                ServiceManager.addService(Context.UPDATE_LOCK_SERVICE,
436                        new UpdateLockService(context));
437            } catch (Throwable e) {
438                reportWtf("starting UpdateLockService", e);
439            }
440
441            if (!"0".equals(SystemProperties.get("system_init.startmountservice"))) {
442                try {
443                    /*
444                     * NotificationManagerService is dependant on MountService,
445                     * (for media / usb notifications) so we must start MountService first.
446                     */
447                    Slog.i(TAG, "Mount Service");
448                    ServiceManager.addService("mount", new MountService(context));
449                } catch (Throwable e) {
450                    reportWtf("starting Mount Service", e);
451                }
452            }
453
454            try {
455                Slog.i(TAG, "Notification Manager");
456                notification = new NotificationManagerService(context, statusBar, lights);
457                ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
458                networkPolicy.bindNotificationManager(notification);
459            } catch (Throwable e) {
460                reportWtf("starting Notification Manager", e);
461            }
462
463            try {
464                Slog.i(TAG, "Device Storage Monitor");
465                ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
466                        new DeviceStorageMonitorService(context));
467            } catch (Throwable e) {
468                reportWtf("starting DeviceStorageMonitor service", e);
469            }
470
471            try {
472                Slog.i(TAG, "Location Manager");
473                location = new LocationManagerService(context);
474                ServiceManager.addService(Context.LOCATION_SERVICE, location);
475            } catch (Throwable e) {
476                reportWtf("starting Location Manager", e);
477            }
478
479            try {
480                Slog.i(TAG, "Country Detector");
481                countryDetector = new CountryDetectorService(context);
482                ServiceManager.addService(Context.COUNTRY_DETECTOR, countryDetector);
483            } catch (Throwable e) {
484                reportWtf("starting Country Detector", e);
485            }
486
487            try {
488                Slog.i(TAG, "Search Service");
489                ServiceManager.addService(Context.SEARCH_SERVICE,
490                        new SearchManagerService(context));
491            } catch (Throwable e) {
492                reportWtf("starting Search Service", e);
493            }
494
495            try {
496                Slog.i(TAG, "DropBox Service");
497                ServiceManager.addService(Context.DROPBOX_SERVICE,
498                        new DropBoxManagerService(context, new File("/data/system/dropbox")));
499            } catch (Throwable e) {
500                reportWtf("starting DropBoxManagerService", e);
501            }
502
503            if (context.getResources().getBoolean(
504                        com.android.internal.R.bool.config_enableWallpaperService)) {
505                try {
506                    Slog.i(TAG, "Wallpaper Service");
507                    if (!headless) {
508                        wallpaper = new WallpaperManagerService(context);
509                        ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);
510                    }
511                } catch (Throwable e) {
512                    reportWtf("starting Wallpaper Service", e);
513                }
514            }
515
516            if (!"0".equals(SystemProperties.get("system_init.startaudioservice"))) {
517                try {
518                    Slog.i(TAG, "Audio Service");
519                    ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
520                } catch (Throwable e) {
521                    reportWtf("starting Audio Service", e);
522                }
523            }
524
525            try {
526                Slog.i(TAG, "Dock Observer");
527                // Listen for dock station changes
528                dock = new DockObserver(context, power);
529            } catch (Throwable e) {
530                reportWtf("starting DockObserver", e);
531            }
532
533            try {
534                Slog.i(TAG, "Wired Accessory Observer");
535                // Listen for wired headset changes
536                new WiredAccessoryObserver(context);
537            } catch (Throwable e) {
538                reportWtf("starting WiredAccessoryObserver", e);
539            }
540
541            try {
542                Slog.i(TAG, "USB Service");
543                // Manage USB host and device support
544                usb = new UsbService(context);
545                ServiceManager.addService(Context.USB_SERVICE, usb);
546            } catch (Throwable e) {
547                reportWtf("starting UsbService", e);
548            }
549
550            try {
551                Slog.i(TAG, "Serial Service");
552                // Serial port support
553                serial = new SerialService(context);
554                ServiceManager.addService(Context.SERIAL_SERVICE, serial);
555            } catch (Throwable e) {
556                Slog.e(TAG, "Failure starting SerialService", e);
557            }
558
559            try {
560                Slog.i(TAG, "UI Mode Manager Service");
561                // Listen for UI mode changes
562                uiMode = new UiModeManagerService(context);
563            } catch (Throwable e) {
564                reportWtf("starting UiModeManagerService", e);
565            }
566
567            try {
568                Slog.i(TAG, "Backup Service");
569                ServiceManager.addService(Context.BACKUP_SERVICE,
570                        new BackupManagerService(context));
571            } catch (Throwable e) {
572                Slog.e(TAG, "Failure starting Backup Service", e);
573            }
574
575            try {
576                Slog.i(TAG, "AppWidget Service");
577                appWidget = new AppWidgetService(context);
578                ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
579            } catch (Throwable e) {
580                reportWtf("starting AppWidget Service", e);
581            }
582
583            try {
584                Slog.i(TAG, "Recognition Service");
585                recognition = new RecognitionManagerService(context);
586            } catch (Throwable e) {
587                reportWtf("starting Recognition Service", e);
588            }
589
590            try {
591                Slog.i(TAG, "DiskStats Service");
592                ServiceManager.addService("diskstats", new DiskStatsService(context));
593            } catch (Throwable e) {
594                reportWtf("starting DiskStats Service", e);
595            }
596
597            try {
598                // need to add this service even if SamplingProfilerIntegration.isEnabled()
599                // is false, because it is this service that detects system property change and
600                // turns on SamplingProfilerIntegration. Plus, when sampling profiler doesn't work,
601                // there is little overhead for running this service.
602                Slog.i(TAG, "SamplingProfiler Service");
603                ServiceManager.addService("samplingprofiler",
604                            new SamplingProfilerService(context));
605            } catch (Throwable e) {
606                reportWtf("starting SamplingProfiler Service", e);
607            }
608
609            try {
610                Slog.i(TAG, "NetworkTimeUpdateService");
611                networkTimeUpdater = new NetworkTimeUpdateService(context);
612            } catch (Throwable e) {
613                reportWtf("starting NetworkTimeUpdate service", e);
614            }
615
616            try {
617                Slog.i(TAG, "CommonTimeManagementService");
618                commonTimeMgmtService = new CommonTimeManagementService(context);
619                ServiceManager.addService("commontime_management", commonTimeMgmtService);
620            } catch (Throwable e) {
621                reportWtf("starting CommonTimeManagementService service", e);
622            }
623
624            if (context.getResources().getBoolean(
625                    com.android.internal.R.bool.config_enableDreams)) {
626                try {
627                    Slog.i(TAG, "Dreams Service");
628                    // Dreams (interactive idle-time views, a/k/a screen savers)
629                    dreamy = new DreamManagerService(context);
630                    ServiceManager.addService("dreams", dreamy);
631                } catch (Throwable e) {
632                    reportWtf("starting DreamManagerService", e);
633                }
634            }
635        }
636
637        // Before things start rolling, be sure we have decided whether
638        // we are in safe mode.
639        final boolean safeMode = wm.detectSafeMode();
640        if (safeMode) {
641            ActivityManagerService.self().enterSafeMode();
642            // Post the safe mode state in the Zygote class
643            Zygote.systemInSafeMode = true;
644            // Disable the JIT for the system_server process
645            VMRuntime.getRuntime().disableJitCompilation();
646        } else {
647            // Enable the JIT for the system_server process
648            VMRuntime.getRuntime().startJitCompilation();
649        }
650
651        // It is now time to start up the app processes...
652
653        try {
654            vibrator.systemReady();
655        } catch (Throwable e) {
656            reportWtf("making Vibrator Service ready", e);
657        }
658
659        if (devicePolicy != null) {
660            try {
661                devicePolicy.systemReady();
662            } catch (Throwable e) {
663                reportWtf("making Device Policy Service ready", e);
664            }
665        }
666
667        if (notification != null) {
668            try {
669                notification.systemReady();
670            } catch (Throwable e) {
671                reportWtf("making Notification Service ready", e);
672            }
673        }
674
675        try {
676            wm.systemReady();
677        } catch (Throwable e) {
678            reportWtf("making Window Manager Service ready", e);
679        }
680
681        if (safeMode) {
682            ActivityManagerService.self().showSafeModeOverlay();
683        }
684
685        // Update the configuration for this context by hand, because we're going
686        // to start using it before the config change done in wm.systemReady() will
687        // propagate to it.
688        Configuration config = wm.computeNewConfiguration();
689        DisplayMetrics metrics = new DisplayMetrics();
690        WindowManager w = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
691        w.getDefaultDisplay().getMetrics(metrics);
692        context.getResources().updateConfiguration(config, metrics);
693
694        power.systemReady();
695        try {
696            pm.systemReady();
697        } catch (Throwable e) {
698            reportWtf("making Package Manager Service ready", e);
699        }
700        try {
701            lockSettings.systemReady();
702        } catch (Throwable e) {
703            reportWtf("making Lock Settings Service ready", e);
704        }
705
706        // These are needed to propagate to the runnable below.
707        final Context contextF = context;
708        final BatteryService batteryF = battery;
709        final NetworkManagementService networkManagementF = networkManagement;
710        final NetworkStatsService networkStatsF = networkStats;
711        final NetworkPolicyManagerService networkPolicyF = networkPolicy;
712        final ConnectivityService connectivityF = connectivity;
713        final DockObserver dockF = dock;
714        final UsbService usbF = usb;
715        final ThrottleService throttleF = throttle;
716        final UiModeManagerService uiModeF = uiMode;
717        final AppWidgetService appWidgetF = appWidget;
718        final WallpaperManagerService wallpaperF = wallpaper;
719        final InputMethodManagerService immF = imm;
720        final RecognitionManagerService recognitionF = recognition;
721        final LocationManagerService locationF = location;
722        final CountryDetectorService countryDetectorF = countryDetector;
723        final NetworkTimeUpdateService networkTimeUpdaterF = networkTimeUpdater;
724        final CommonTimeManagementService commonTimeMgmtServiceF = commonTimeMgmtService;
725        final TextServicesManagerService textServiceManagerServiceF = tsms;
726        final StatusBarManagerService statusBarF = statusBar;
727        final DreamManagerService dreamyF = dreamy;
728        final InputManagerService inputManagerF = inputManager;
729
730        // We now tell the activity manager it is okay to run third party
731        // code.  It will call back into us once it has gotten to the state
732        // where third party code can really run (but before it has actually
733        // started launching the initial applications), for us to complete our
734        // initialization.
735        ActivityManagerService.self().systemReady(new Runnable() {
736            public void run() {
737                Slog.i(TAG, "Making services ready");
738
739                if (!headless) startSystemUi(contextF);
740                try {
741                    if (batteryF != null) batteryF.systemReady();
742                } catch (Throwable e) {
743                    reportWtf("making Battery Service ready", e);
744                }
745                try {
746                    if (networkManagementF != null) networkManagementF.systemReady();
747                } catch (Throwable e) {
748                    reportWtf("making Network Managment Service ready", e);
749                }
750                try {
751                    if (networkStatsF != null) networkStatsF.systemReady();
752                } catch (Throwable e) {
753                    reportWtf("making Network Stats Service ready", e);
754                }
755                try {
756                    if (networkPolicyF != null) networkPolicyF.systemReady();
757                } catch (Throwable e) {
758                    reportWtf("making Network Policy Service ready", e);
759                }
760                try {
761                    if (connectivityF != null) connectivityF.systemReady();
762                } catch (Throwable e) {
763                    reportWtf("making Connectivity Service ready", e);
764                }
765                try {
766                    if (dockF != null) dockF.systemReady();
767                } catch (Throwable e) {
768                    reportWtf("making Dock Service ready", e);
769                }
770                try {
771                    if (usbF != null) usbF.systemReady();
772                } catch (Throwable e) {
773                    reportWtf("making USB Service ready", e);
774                }
775                try {
776                    if (uiModeF != null) uiModeF.systemReady();
777                } catch (Throwable e) {
778                    reportWtf("making UI Mode Service ready", e);
779                }
780                try {
781                    if (recognitionF != null) recognitionF.systemReady();
782                } catch (Throwable e) {
783                    reportWtf("making Recognition Service ready", e);
784                }
785                Watchdog.getInstance().start();
786
787                // It is now okay to let the various system services start their
788                // third party code...
789
790                try {
791                    if (appWidgetF != null) appWidgetF.systemReady(safeMode);
792                } catch (Throwable e) {
793                    reportWtf("making App Widget Service ready", e);
794                }
795                try {
796                    if (wallpaperF != null) wallpaperF.systemReady();
797                } catch (Throwable e) {
798                    reportWtf("making Wallpaper Service ready", e);
799                }
800                try {
801                    if (immF != null) immF.systemReady(statusBarF);
802                } catch (Throwable e) {
803                    reportWtf("making Input Method Service ready", e);
804                }
805                try {
806                    if (locationF != null) locationF.systemReady();
807                } catch (Throwable e) {
808                    reportWtf("making Location Service ready", e);
809                }
810                try {
811                    if (countryDetectorF != null) countryDetectorF.systemReady();
812                } catch (Throwable e) {
813                    reportWtf("making Country Detector Service ready", e);
814                }
815                try {
816                    if (throttleF != null) throttleF.systemReady();
817                } catch (Throwable e) {
818                    reportWtf("making Throttle Service ready", e);
819                }
820                try {
821                    if (networkTimeUpdaterF != null) networkTimeUpdaterF.systemReady();
822                } catch (Throwable e) {
823                    reportWtf("making Network Time Service ready", e);
824                }
825                try {
826                    if (commonTimeMgmtServiceF != null) commonTimeMgmtServiceF.systemReady();
827                } catch (Throwable e) {
828                    reportWtf("making Common time management service ready", e);
829                }
830                try {
831                    if (textServiceManagerServiceF != null) textServiceManagerServiceF.systemReady();
832                } catch (Throwable e) {
833                    reportWtf("making Text Services Manager Service ready", e);
834                }
835                try {
836                    if (dreamyF != null) dreamyF.systemReady();
837                } catch (Throwable e) {
838                    reportWtf("making DreamManagerService ready", e);
839                }
840                try {
841                    if (inputManagerF != null) inputManagerF.systemReady();
842                } catch (Throwable e) {
843                    reportWtf("making InputManagerService ready", e);
844                }
845            }
846        });
847
848        // For debug builds, log event loop stalls to dropbox for analysis.
849        if (StrictMode.conditionallyEnableDebugLogging()) {
850            Slog.i(TAG, "Enabled StrictMode for system server main thread.");
851        }
852
853        Looper.loop();
854        Slog.d(TAG, "System ServerThread is exiting!");
855    }
856
857    static final void startSystemUi(Context context) {
858        Intent intent = new Intent();
859        intent.setComponent(new ComponentName("com.android.systemui",
860                    "com.android.systemui.SystemUIService"));
861        Slog.d(TAG, "Starting service: " + intent);
862        context.startService(intent);
863    }
864}
865
866public class SystemServer {
867    private static final String TAG = "SystemServer";
868
869    public static final int FACTORY_TEST_OFF = 0;
870    public static final int FACTORY_TEST_LOW_LEVEL = 1;
871    public static final int FACTORY_TEST_HIGH_LEVEL = 2;
872
873    static Timer timer;
874    static final long SNAPSHOT_INTERVAL = 60 * 60 * 1000; // 1hr
875
876    // The earliest supported time.  We pick one day into 1970, to
877    // give any timezone code room without going into negative time.
878    private static final long EARLIEST_SUPPORTED_TIME = 86400 * 1000;
879
880    /**
881     * This method is called from Zygote to initialize the system. This will cause the native
882     * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
883     * up into init2() to start the Android services.
884     */
885    native public static void init1(String[] args);
886
887    public static void main(String[] args) {
888        if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
889            // If a device's clock is before 1970 (before 0), a lot of
890            // APIs crash dealing with negative numbers, notably
891            // java.io.File#setLastModified, so instead we fake it and
892            // hope that time from cell towers or NTP fixes it
893            // shortly.
894            Slog.w(TAG, "System clock is before 1970; setting to 1970.");
895            SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
896        }
897
898        if (SamplingProfilerIntegration.isEnabled()) {
899            SamplingProfilerIntegration.start();
900            timer = new Timer();
901            timer.schedule(new TimerTask() {
902                @Override
903                public void run() {
904                    SamplingProfilerIntegration.writeSnapshot("system_server", null);
905                }
906            }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
907        }
908
909        // Mmmmmm... more memory!
910        dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
911
912        // The system server has to run all of the time, so it needs to be
913        // as efficient as possible with its memory usage.
914        VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
915
916        System.loadLibrary("android_servers");
917        init1(args);
918    }
919
920    public static final void init2() {
921        Slog.i(TAG, "Entered the Android system server!");
922        Thread thr = new ServerThread();
923        thr.setName("android.server.ServerThread");
924        thr.start();
925    }
926}
927