SystemServer.java revision 038cabe0247ee46df62f9363f1a303bc5b9c1028
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 com.android.server.am.ActivityManagerService; 20import com.android.internal.os.BinderInternal; 21import com.android.internal.os.SamplingProfilerIntegration; 22import com.trustedlogic.trustednfc.android.server.NfcService; 23 24import dalvik.system.VMRuntime; 25import dalvik.system.Zygote; 26 27import android.app.ActivityManagerNative; 28import android.bluetooth.BluetoothAdapter; 29import android.content.ComponentName; 30import android.content.ContentResolver; 31import android.content.ContentService; 32import android.content.Context; 33import android.content.Intent; 34import android.content.pm.IPackageManager; 35import android.database.ContentObserver; 36import android.database.Cursor; 37import android.media.AudioService; 38import android.os.*; 39import android.provider.Contacts.People; 40import android.provider.Settings; 41import android.server.BluetoothA2dpService; 42import android.server.BluetoothService; 43import android.server.search.SearchManagerService; 44import android.util.EventLog; 45import android.util.Log; 46import android.util.Slog; 47import android.accounts.AccountManagerService; 48 49import java.io.File; 50import java.util.Timer; 51import java.util.TimerTask; 52 53class ServerThread extends Thread { 54 private static final String TAG = "SystemServer"; 55 private final static boolean INCLUDE_DEMO = false; 56 57 private static final int LOG_BOOT_PROGRESS_SYSTEM_RUN = 3010; 58 59 private ContentResolver mContentResolver; 60 61 private class AdbSettingsObserver extends ContentObserver { 62 public AdbSettingsObserver() { 63 super(null); 64 } 65 @Override 66 public void onChange(boolean selfChange) { 67 boolean enableAdb = (Settings.Secure.getInt(mContentResolver, 68 Settings.Secure.ADB_ENABLED, 0) > 0); 69 // setting this secure property will start or stop adbd 70 SystemProperties.set("persist.service.adb.enable", enableAdb ? "1" : "0"); 71 } 72 } 73 74 @Override 75 public void run() { 76 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, 77 SystemClock.uptimeMillis()); 78 79 Looper.prepare(); 80 81 android.os.Process.setThreadPriority( 82 android.os.Process.THREAD_PRIORITY_FOREGROUND); 83 84 BinderInternal.disableBackgroundScheduling(true); 85 android.os.Process.setCanSelfBackground(false); 86 87 String factoryTestStr = SystemProperties.get("ro.factorytest"); 88 int factoryTest = "".equals(factoryTestStr) ? SystemServer.FACTORY_TEST_OFF 89 : Integer.parseInt(factoryTestStr); 90 91 LightsService lights = null; 92 PowerManagerService power = null; 93 BatteryService battery = null; 94 ConnectivityService connectivity = null; 95 IPackageManager pm = null; 96 Context context = null; 97 WindowManagerService wm = null; 98 BluetoothService bluetooth = null; 99 BluetoothA2dpService bluetoothA2dp = null; 100 HeadsetObserver headset = null; 101 DockObserver dock = null; 102 UsbObserver usb = null; 103 UiModeManagerService uiMode = null; 104 RecognitionManagerService recognition = null; 105 ThrottleService throttle = null; 106 107 // Critical services... 108 try { 109 Slog.i(TAG, "Entropy Service"); 110 ServiceManager.addService("entropy", new EntropyService()); 111 112 Slog.i(TAG, "Power Manager"); 113 power = new PowerManagerService(); 114 ServiceManager.addService(Context.POWER_SERVICE, power); 115 116 Slog.i(TAG, "Activity Manager"); 117 context = ActivityManagerService.main(factoryTest); 118 119 Slog.i(TAG, "Telephony Registry"); 120 ServiceManager.addService("telephony.registry", new TelephonyRegistry(context)); 121 122 AttributeCache.init(context); 123 124 Slog.i(TAG, "Package Manager"); 125 pm = PackageManagerService.main(context, 126 factoryTest != SystemServer.FACTORY_TEST_OFF); 127 128 ActivityManagerService.setSystemProcess(); 129 130 mContentResolver = context.getContentResolver(); 131 132 // The AccountManager must come before the ContentService 133 try { 134 Slog.i(TAG, "Account Manager"); 135 ServiceManager.addService(Context.ACCOUNT_SERVICE, 136 new AccountManagerService(context)); 137 } catch (Throwable e) { 138 Slog.e(TAG, "Failure starting Account Manager", e); 139 } 140 141 Slog.i(TAG, "Content Manager"); 142 ContentService.main(context, 143 factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL); 144 145 Slog.i(TAG, "System Content Providers"); 146 ActivityManagerService.installSystemProviders(); 147 148 Slog.i(TAG, "Battery Service"); 149 battery = new BatteryService(context); 150 ServiceManager.addService("battery", battery); 151 152 Slog.i(TAG, "Lights Service"); 153 lights = new LightsService(context); 154 155 Slog.i(TAG, "Vibrator Service"); 156 ServiceManager.addService("vibrator", new VibratorService(context)); 157 158 // only initialize the power service after we have started the 159 // lights service, content providers and the battery service. 160 power.init(context, lights, ActivityManagerService.getDefault(), battery); 161 162 Slog.i(TAG, "Alarm Manager"); 163 AlarmManagerService alarm = new AlarmManagerService(context); 164 ServiceManager.addService(Context.ALARM_SERVICE, alarm); 165 166 Slog.i(TAG, "Init Watchdog"); 167 Watchdog.getInstance().init(context, battery, power, alarm, 168 ActivityManagerService.self()); 169 170 Slog.i(TAG, "Window Manager"); 171 wm = WindowManagerService.main(context, power, 172 factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL); 173 ServiceManager.addService(Context.WINDOW_SERVICE, wm); 174 175 ((ActivityManagerService)ServiceManager.getService("activity")) 176 .setWindowManager(wm); 177 178 // Skip Bluetooth if we have an emulator kernel 179 // TODO: Use a more reliable check to see if this product should 180 // support Bluetooth - see bug 988521 181 if (SystemProperties.get("ro.kernel.qemu").equals("1")) { 182 Slog.i(TAG, "Registering null Bluetooth Service (emulator)"); 183 ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null); 184 } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) { 185 Slog.i(TAG, "Registering null Bluetooth Service (factory test)"); 186 ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null); 187 } else { 188 Slog.i(TAG, "Bluetooth Service"); 189 bluetooth = new BluetoothService(context); 190 ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, bluetooth); 191 bluetooth.initAfterRegistration(); 192 bluetoothA2dp = new BluetoothA2dpService(context, bluetooth); 193 ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE, 194 bluetoothA2dp); 195 196 int bluetoothOn = Settings.Secure.getInt(mContentResolver, 197 Settings.Secure.BLUETOOTH_ON, 0); 198 if (bluetoothOn > 0) { 199 bluetooth.enable(); 200 } 201 } 202 203 } catch (RuntimeException e) { 204 Slog.e("System", "Failure starting core service", e); 205 } 206 207 DevicePolicyManagerService devicePolicy = null; 208 StatusBarManagerService statusBar = null; 209 InputMethodManagerService imm = null; 210 AppWidgetService appWidget = null; 211 NotificationManagerService notification = null; 212 WallpaperManagerService wallpaper = null; 213 LocationManagerService location = null; 214 215 if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { 216 try { 217 Slog.i(TAG, "Device Policy"); 218 devicePolicy = new DevicePolicyManagerService(context); 219 ServiceManager.addService(Context.DEVICE_POLICY_SERVICE, devicePolicy); 220 } catch (Throwable e) { 221 Slog.e(TAG, "Failure starting DevicePolicyService", e); 222 } 223 224 try { 225 Slog.i(TAG, "Status Bar"); 226 statusBar = new StatusBarManagerService(context); 227 ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar); 228 } catch (Throwable e) { 229 Slog.e(TAG, "Failure starting StatusBarManagerService", e); 230 } 231 232 try { 233 Slog.i(TAG, "Clipboard Service"); 234 ServiceManager.addService(Context.CLIPBOARD_SERVICE, 235 new ClipboardService(context)); 236 } catch (Throwable e) { 237 Slog.e(TAG, "Failure starting Clipboard Service", e); 238 } 239 240 try { 241 Slog.i(TAG, "Input Method Service"); 242 imm = new InputMethodManagerService(context, statusBar); 243 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm); 244 } catch (Throwable e) { 245 Slog.e(TAG, "Failure starting Input Manager Service", e); 246 } 247 248 try { 249 Slog.i(TAG, "NetStat Service"); 250 ServiceManager.addService("netstat", new NetStatService(context)); 251 } catch (Throwable e) { 252 Slog.e(TAG, "Failure starting NetStat Service", e); 253 } 254 255 try { 256 Slog.i(TAG, "NetworkManagement Service"); 257 ServiceManager.addService( 258 Context.NETWORKMANAGEMENT_SERVICE, 259 NetworkManagementService.create(context)); 260 } catch (Throwable e) { 261 Slog.e(TAG, "Failure starting NetworkManagement Service", e); 262 } 263 264 try { 265 Slog.i(TAG, "Connectivity Service"); 266 connectivity = ConnectivityService.getInstance(context); 267 ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity); 268 } catch (Throwable e) { 269 Slog.e(TAG, "Failure starting Connectivity Service", e); 270 } 271 272 try { 273 Slog.i(TAG, "Throttle Service"); 274 throttle = new ThrottleService(context); 275 ServiceManager.addService( 276 Context.THROTTLE_SERVICE, throttle); 277 } catch (Throwable e) { 278 Slog.e(TAG, "Failure starting ThrottleService", e); 279 } 280 281 try { 282 Slog.i(TAG, "Accessibility Manager"); 283 ServiceManager.addService(Context.ACCESSIBILITY_SERVICE, 284 new AccessibilityManagerService(context)); 285 } catch (Throwable e) { 286 Slog.e(TAG, "Failure starting Accessibility Manager", e); 287 } 288 289 try { 290 /* 291 * NotificationManagerService is dependant on MountService, 292 * (for media / usb notifications) so we must start MountService first. 293 */ 294 Slog.i(TAG, "Mount Service"); 295 ServiceManager.addService("mount", new MountService(context)); 296 } catch (Throwable e) { 297 Slog.e(TAG, "Failure starting Mount Service", e); 298 } 299 300 try { 301 Slog.i(TAG, "Notification Manager"); 302 notification = new NotificationManagerService(context, statusBar, lights); 303 ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification); 304 } catch (Throwable e) { 305 Slog.e(TAG, "Failure starting Notification Manager", e); 306 } 307 308 try { 309 Slog.i(TAG, "Device Storage Monitor"); 310 ServiceManager.addService(DeviceStorageMonitorService.SERVICE, 311 new DeviceStorageMonitorService(context)); 312 } catch (Throwable e) { 313 Slog.e(TAG, "Failure starting DeviceStorageMonitor service", e); 314 } 315 316 try { 317 Slog.i(TAG, "Location Manager"); 318 location = new LocationManagerService(context); 319 ServiceManager.addService(Context.LOCATION_SERVICE, location); 320 } catch (Throwable e) { 321 Slog.e(TAG, "Failure starting Location Manager", e); 322 } 323 324 try { 325 Slog.i(TAG, "Search Service"); 326 ServiceManager.addService(Context.SEARCH_SERVICE, 327 new SearchManagerService(context)); 328 } catch (Throwable e) { 329 Slog.e(TAG, "Failure starting Search Service", e); 330 } 331 332 if (INCLUDE_DEMO) { 333 Slog.i(TAG, "Installing demo data..."); 334 (new DemoThread(context)).start(); 335 } 336 337 try { 338 Slog.i(TAG, "DropBox Service"); 339 ServiceManager.addService(Context.DROPBOX_SERVICE, 340 new DropBoxManagerService(context, new File("/data/system/dropbox"))); 341 } catch (Throwable e) { 342 Slog.e(TAG, "Failure starting DropBoxManagerService", e); 343 } 344 345 try { 346 Slog.i(TAG, "Wallpaper Service"); 347 wallpaper = new WallpaperManagerService(context); 348 ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper); 349 } catch (Throwable e) { 350 Slog.e(TAG, "Failure starting Wallpaper Service", e); 351 } 352 353 try { 354 Slog.i(TAG, "Audio Service"); 355 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context)); 356 } catch (Throwable e) { 357 Slog.e(TAG, "Failure starting Audio Service", e); 358 } 359 360 try { 361 Slog.i(TAG, "Headset Observer"); 362 // Listen for wired headset changes 363 headset = new HeadsetObserver(context); 364 } catch (Throwable e) { 365 Slog.e(TAG, "Failure starting HeadsetObserver", e); 366 } 367 368 try { 369 Slog.i(TAG, "Dock Observer"); 370 // Listen for dock station changes 371 dock = new DockObserver(context, power); 372 } catch (Throwable e) { 373 Slog.e(TAG, "Failure starting DockObserver", e); 374 } 375 376 try { 377 Slog.i(TAG, "USB Observer"); 378 // Listen for USB changes 379 usb = new UsbObserver(context); 380 } catch (Throwable e) { 381 Slog.e(TAG, "Failure starting UsbObserver", e); 382 } 383 384 try { 385 Slog.i(TAG, "UI Mode Manager Service"); 386 // Listen for UI mode changes 387 uiMode = new UiModeManagerService(context); 388 } catch (Throwable e) { 389 Slog.e(TAG, "Failure starting UiModeManagerService", e); 390 } 391 392 try { 393 Slog.i(TAG, "Backup Service"); 394 ServiceManager.addService(Context.BACKUP_SERVICE, 395 new BackupManagerService(context)); 396 } catch (Throwable e) { 397 Slog.e(TAG, "Failure starting Backup Service", e); 398 } 399 400 try { 401 Slog.i(TAG, "AppWidget Service"); 402 appWidget = new AppWidgetService(context); 403 ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget); 404 } catch (Throwable e) { 405 Slog.e(TAG, "Failure starting AppWidget Service", e); 406 } 407 408 try { 409 Slog.i(TAG, "Recognition Service"); 410 recognition = new RecognitionManagerService(context); 411 } catch (Throwable e) { 412 Slog.e(TAG, "Failure starting Recognition Service", e); 413 } 414 415 try { 416 Slog.i(TAG, "Nfc Service"); 417 NfcService nfc; 418 try { 419 nfc = new NfcService(context); 420 } catch (UnsatisfiedLinkError e) { // gross hack to detect NFC 421 nfc = null; 422 Slog.w(TAG, "No NFC support"); 423 } 424 ServiceManager.addService(Context.NFC_SERVICE, nfc); 425 } catch (Throwable e) { 426 Slog.e(TAG, "Failure starting NFC Service", e); 427 } 428 429 try { 430 Slog.i(TAG, "DiskStats Service"); 431 ServiceManager.addService("diskstats", new DiskStatsService(context)); 432 } catch (Throwable e) { 433 Slog.e(TAG, "Failure starting DiskStats Service", e); 434 } 435 } 436 437 // make sure the ADB_ENABLED setting value matches the secure property value 438 Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED, 439 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0); 440 441 // register observer to listen for settings changes 442 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED), 443 false, new AdbSettingsObserver()); 444 445 // Before things start rolling, be sure we have decided whether 446 // we are in safe mode. 447 final boolean safeMode = wm.detectSafeMode(); 448 if (safeMode) { 449 try { 450 ActivityManagerNative.getDefault().enterSafeMode(); 451 // Post the safe mode state in the Zygote class 452 Zygote.systemInSafeMode = true; 453 // Disable the JIT for the system_server process 454 VMRuntime.getRuntime().disableJitCompilation(); 455 } catch (RemoteException e) { 456 } 457 } else { 458 // Enable the JIT for the system_server process 459 VMRuntime.getRuntime().startJitCompilation(); 460 } 461 462 // It is now time to start up the app processes... 463 464 if (devicePolicy != null) { 465 devicePolicy.systemReady(); 466 } 467 468 if (notification != null) { 469 notification.systemReady(); 470 } 471 472 if (statusBar != null) { 473 statusBar.systemReady(); 474 } 475 wm.systemReady(); 476 power.systemReady(); 477 try { 478 pm.systemReady(); 479 } catch (RemoteException e) { 480 } 481 482 // These are needed to propagate to the runnable below. 483 final StatusBarManagerService statusBarF = statusBar; 484 final BatteryService batteryF = battery; 485 final ConnectivityService connectivityF = connectivity; 486 final DockObserver dockF = dock; 487 final UsbObserver usbF = usb; 488 final ThrottleService throttleF = throttle; 489 final UiModeManagerService uiModeF = uiMode; 490 final AppWidgetService appWidgetF = appWidget; 491 final WallpaperManagerService wallpaperF = wallpaper; 492 final InputMethodManagerService immF = imm; 493 final RecognitionManagerService recognitionF = recognition; 494 final LocationManagerService locationF = location; 495 496 // We now tell the activity manager it is okay to run third party 497 // code. It will call back into us once it has gotten to the state 498 // where third party code can really run (but before it has actually 499 // started launching the initial applications), for us to complete our 500 // initialization. 501 ((ActivityManagerService)ActivityManagerNative.getDefault()) 502 .systemReady(new Runnable() { 503 public void run() { 504 Slog.i(TAG, "Making services ready"); 505 506 if (statusBarF != null) statusBarF.systemReady2(); 507 if (batteryF != null) batteryF.systemReady(); 508 if (connectivityF != null) connectivityF.systemReady(); 509 if (dockF != null) dockF.systemReady(); 510 if (usbF != null) usbF.systemReady(); 511 if (uiModeF != null) uiModeF.systemReady(); 512 if (recognitionF != null) recognitionF.systemReady(); 513 Watchdog.getInstance().start(); 514 515 // It is now okay to let the various system services start their 516 // third party code... 517 518 if (appWidgetF != null) appWidgetF.systemReady(safeMode); 519 if (wallpaperF != null) wallpaperF.systemReady(); 520 if (immF != null) immF.systemReady(); 521 if (locationF != null) locationF.systemReady(); 522 if (throttleF != null) throttleF.systemReady(); 523 } 524 }); 525 526 // For debug builds, log event loop stalls to dropbox for analysis. 527 if (StrictMode.conditionallyEnableDebugLogging()) { 528 Slog.i(TAG, "Enabled StrictMode for system server main thread."); 529 } 530 531 Looper.loop(); 532 Slog.d(TAG, "System ServerThread is exiting!"); 533 } 534} 535 536class DemoThread extends Thread 537{ 538 DemoThread(Context context) 539 { 540 mContext = context; 541 } 542 543 @Override 544 public void run() 545 { 546 try { 547 Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null); 548 boolean hasData = c != null && c.moveToFirst(); 549 if (c != null) { 550 c.deactivate(); 551 } 552 if (!hasData) { 553 DemoDataSet dataset = new DemoDataSet(); 554 dataset.add(mContext); 555 } 556 } catch (Throwable e) { 557 Slog.e("SystemServer", "Failure installing demo data", e); 558 } 559 560 } 561 562 Context mContext; 563} 564 565public class SystemServer 566{ 567 private static final String TAG = "SystemServer"; 568 569 public static final int FACTORY_TEST_OFF = 0; 570 public static final int FACTORY_TEST_LOW_LEVEL = 1; 571 public static final int FACTORY_TEST_HIGH_LEVEL = 2; 572 573 static Timer timer; 574 static final long SNAPSHOT_INTERVAL = 60 * 60 * 1000; // 1hr 575 576 /** 577 * This method is called from Zygote to initialize the system. This will cause the native 578 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back 579 * up into init2() to start the Android services. 580 */ 581 native public static void init1(String[] args); 582 583 public static void main(String[] args) { 584 if (SamplingProfilerIntegration.isEnabled()) { 585 SamplingProfilerIntegration.start(); 586 timer = new Timer(); 587 timer.schedule(new TimerTask() { 588 @Override 589 public void run() { 590 SamplingProfilerIntegration.writeSnapshot("system_server"); 591 } 592 }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL); 593 } 594 595 // The system server has to run all of the time, so it needs to be 596 // as efficient as possible with its memory usage. 597 VMRuntime.getRuntime().setTargetHeapUtilization(0.8f); 598 599 System.loadLibrary("android_servers"); 600 init1(args); 601 } 602 603 public static final void init2() { 604 Slog.i(TAG, "Entered the Android system server!"); 605 Thread thr = new ServerThread(); 606 thr.setName("android.server.ServerThread"); 607 thr.start(); 608 } 609} 610