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