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