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