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