SystemServer.java revision 105925376f8d0f6b318c9938c7b83ef7fef094da
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 Notification Manager."); 232 ServiceManager.addService(Context.NOTIFICATION_SERVICE, 233 new NotificationManagerService(context, statusBar, hardware)); 234 } catch (Throwable e) { 235 Log.e(TAG, "Failure starting Notification Manager", e); 236 } 237 238 try { 239 // MountService must start after NotificationManagerService 240 Log.i(TAG, "Starting Mount Service."); 241 ServiceManager.addService("mount", new MountService(context)); 242 } catch (Throwable e) { 243 Log.e(TAG, "Failure starting Mount Service", e); 244 } 245 246 try { 247 Log.i(TAG, "Starting DeviceStorageMonitor service"); 248 ServiceManager.addService(DeviceStorageMonitorService.SERVICE, 249 new DeviceStorageMonitorService(context)); 250 } catch (Throwable e) { 251 Log.e(TAG, "Failure starting DeviceStorageMonitor service", e); 252 } 253 254 try { 255 Log.i(TAG, "Starting Location Manager."); 256 ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context)); 257 } catch (Throwable e) { 258 Log.e(TAG, "Failure starting Location Manager", e); 259 } 260 261 try { 262 Log.i(TAG, "Starting Search Service."); 263 ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) ); 264 } catch (Throwable e) { 265 Log.e(TAG, "Failure starting Search Service", e); 266 } 267 268 if (INCLUDE_DEMO) { 269 Log.i(TAG, "Installing demo data..."); 270 (new DemoThread(context)).start(); 271 } 272 273 try { 274 Log.i(TAG, "Starting Checkin Service."); 275 Intent intent = new Intent().setComponent(new ComponentName( 276 "com.google.android.server.checkin", 277 "com.google.android.server.checkin.CheckinService")); 278 if (context.startService(intent) == null) { 279 Log.w(TAG, "Using fallback Checkin Service."); 280 ServiceManager.addService("checkin", new FallbackCheckinService(context)); 281 } 282 } catch (Throwable e) { 283 Log.e(TAG, "Failure starting Checkin Service", e); 284 } 285 286 try { 287 Log.i(TAG, "Starting Wallpaper Service"); 288 ServiceManager.addService(Context.WALLPAPER_SERVICE, new WallpaperService(context)); 289 } catch (Throwable e) { 290 Log.e(TAG, "Failure starting Wallpaper Service", e); 291 } 292 293 try { 294 Log.i(TAG, "Starting Audio Service"); 295 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context)); 296 } catch (Throwable e) { 297 Log.e(TAG, "Failure starting Audio Service", e); 298 } 299 300 try { 301 Log.i(TAG, "Starting HeadsetObserver"); 302 // Listen for wired headset changes 303 headset = new HeadsetObserver(context); 304 } catch (Throwable e) { 305 Log.e(TAG, "Failure starting HeadsetObserver", e); 306 } 307 308 try { 309 Log.i(TAG, "Starting AppWidget Service"); 310 appWidget = new AppWidgetService(context); 311 ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget); 312 } catch (Throwable e) { 313 Log.e(TAG, "Failure starting AppWidget Service", e); 314 } 315 316 try { 317 com.android.server.status.StatusBarPolicy.installIcons(context, statusBar); 318 } catch (Throwable e) { 319 Log.e(TAG, "Failure installing status bar icons", e); 320 } 321 } 322 323 // make sure the ADB_ENABLED setting value matches the secure property value 324 Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED, 325 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0); 326 327 // register observer to listen for settings changes 328 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED), 329 false, new AdbSettingsObserver()); 330 331 // It is now time to start up the app processes... 332 boolean safeMode = wm.detectSafeMode(); 333 if (statusBar != null) { 334 statusBar.systemReady(); 335 } 336 if (imm != null) { 337 imm.systemReady(); 338 } 339 wm.systemReady(); 340 power.systemReady(); 341 try { 342 pm.systemReady(); 343 } catch (RemoteException e) { 344 } 345 if (appWidget != null) { 346 appWidget.systemReady(safeMode); 347 } 348 349 // After making the following code, third party code may be running... 350 try { 351 ActivityManagerNative.getDefault().systemReady(); 352 } catch (RemoteException e) { 353 } 354 355 Watchdog.getInstance().start(); 356 357 Looper.loop(); 358 Log.d(TAG, "System ServerThread is exiting!"); 359 } 360} 361 362class DemoThread extends Thread 363{ 364 DemoThread(Context context) 365 { 366 mContext = context; 367 } 368 369 @Override 370 public void run() 371 { 372 try { 373 Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null); 374 boolean hasData = c != null && c.moveToFirst(); 375 if (c != null) { 376 c.deactivate(); 377 } 378 if (!hasData) { 379 DemoDataSet dataset = new DemoDataSet(); 380 dataset.add(mContext); 381 } 382 } catch (Throwable e) { 383 Log.e("SystemServer", "Failure installing demo data", e); 384 } 385 386 } 387 388 Context mContext; 389} 390 391public class SystemServer 392{ 393 private static final String TAG = "SystemServer"; 394 395 public static final int FACTORY_TEST_OFF = 0; 396 public static final int FACTORY_TEST_LOW_LEVEL = 1; 397 public static final int FACTORY_TEST_HIGH_LEVEL = 2; 398 399 /** 400 * This method is called from Zygote to initialize the system. This will cause the native 401 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back 402 * up into init2() to start the Android services. 403 */ 404 native public static void init1(String[] args); 405 406 public static void main(String[] args) { 407 // The system server has to run all of the time, so it needs to be 408 // as efficient as possible with its memory usage. 409 VMRuntime.getRuntime().setTargetHeapUtilization(0.8f); 410 411 System.loadLibrary("android_servers"); 412 init1(args); 413 } 414 415 public static final void init2() { 416 Log.i(TAG, "Entered the Android system server!"); 417 Thread thr = new ServerThread(); 418 thr.setName("android.server.ServerThread"); 419 thr.start(); 420 } 421} 422