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