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