blob: df01c616541541b7ccf5f9b1ae81030b6e038492 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import dalvik.system.VMRuntime;
23
24import android.app.ActivityManagerNative;
25import android.content.ComponentName;
26import android.content.ContentResolver;
27import android.content.ContentService;
28import android.content.Context;
29import android.content.Intent;
30import android.content.pm.IPackageManager;
31import android.database.ContentObserver;
32import android.database.Cursor;
33import android.media.AudioService;
Fred Quintana60307342009-03-24 22:48:12 -070034import android.os.*;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.provider.Contacts.People;
36import android.provider.Settings;
37import android.server.BluetoothA2dpService;
Nick Pellybd022f42009-08-14 18:33:38 -070038import android.server.BluetoothService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.server.search.SearchManagerService;
40import android.util.EventLog;
41import android.util.Log;
Fred Quintana60307342009-03-24 22:48:12 -070042import android.accounts.AccountManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044class ServerThread extends Thread {
45 private static final String TAG = "SystemServer";
46 private final static boolean INCLUDE_DEMO = false;
47
48 private static final int LOG_BOOT_PROGRESS_SYSTEM_RUN = 3010;
49
50 private ContentResolver mContentResolver;
51
52 private class AdbSettingsObserver extends ContentObserver {
53 public AdbSettingsObserver() {
54 super(null);
55 }
56 @Override
57 public void onChange(boolean selfChange) {
58 boolean enableAdb = (Settings.Secure.getInt(mContentResolver,
59 Settings.Secure.ADB_ENABLED, 0) > 0);
60 // setting this secure property will start or stop adbd
61 SystemProperties.set("persist.service.adb.enable", enableAdb ? "1" : "0");
62 }
63 }
64
65 @Override
66 public void run() {
67 EventLog.writeEvent(LOG_BOOT_PROGRESS_SYSTEM_RUN,
68 SystemClock.uptimeMillis());
69
70 ActivityManagerService.prepareTraceFile(false); // create dir
71
72 Looper.prepare();
73
74 android.os.Process.setThreadPriority(
75 android.os.Process.THREAD_PRIORITY_FOREGROUND);
76
77 String factoryTestStr = SystemProperties.get("ro.factorytest");
78 int factoryTest = "".equals(factoryTestStr) ? SystemServer.FACTORY_TEST_OFF
79 : Integer.parseInt(factoryTestStr);
80
The Android Open Source Project10592532009-03-18 17:39:46 -070081 HardwareService hardware = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 PowerManagerService power = null;
Mike Lockwood07a500f2009-08-12 09:56:44 -040083 BatteryService battery = null;
Mike Lockwood0f79b542009-08-14 14:18:49 -040084 ConnectivityService connectivity = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 IPackageManager pm = null;
86 Context context = null;
87 WindowManagerService wm = null;
Nick Pellybd022f42009-08-14 18:33:38 -070088 BluetoothService bluetooth = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 BluetoothA2dpService bluetoothA2dp = null;
90 HeadsetObserver headset = null;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050091 DockObserver dock = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93 // Critical services...
94 try {
Nick Kralevich4fb25612009-06-17 16:03:22 -070095 Log.i(TAG, "Starting Entropy Service.");
96 ServiceManager.addService("entropy", new EntropyService());
97
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 Log.i(TAG, "Starting Power Manager.");
99 power = new PowerManagerService();
100 ServiceManager.addService(Context.POWER_SERVICE, power);
101
102 Log.i(TAG, "Starting Activity Manager.");
103 context = ActivityManagerService.main(factoryTest);
104
105 Log.i(TAG, "Starting telephony registry");
106 ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));
107
108 AttributeCache.init(context);
109
110 Log.i(TAG, "Starting Package Manager.");
111 pm = PackageManagerService.main(context,
112 factoryTest != SystemServer.FACTORY_TEST_OFF);
113
114 ActivityManagerService.setSystemProcess();
115
116 mContentResolver = context.getContentResolver();
117
Fred Quintana60307342009-03-24 22:48:12 -0700118 try {
119 Log.i(TAG, "Starting Account Manager.");
120 ServiceManager.addService(Context.ACCOUNT_SERVICE,
121 new AccountManagerService(context));
122 } catch (Throwable e) {
123 Log.e(TAG, "Failure starting Account Manager", e);
124 }
125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 Log.i(TAG, "Starting Content Manager.");
127 ContentService.main(context,
128 factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
129
130 Log.i(TAG, "Starting System Content Providers.");
131 ActivityManagerService.installSystemProviders();
132
133 Log.i(TAG, "Starting Battery Service.");
Mike Lockwood07a500f2009-08-12 09:56:44 -0400134 battery = new BatteryService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 ServiceManager.addService("battery", battery);
136
The Android Open Source Project10592532009-03-18 17:39:46 -0700137 Log.i(TAG, "Starting Hardware Service.");
138 hardware = new HardwareService(context);
139 ServiceManager.addService("hardware", hardware);
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 // only initialize the power service after we have started the
The Android Open Source Project10592532009-03-18 17:39:46 -0700142 // hardware service, content providers and the battery service.
143 power.init(context, hardware, ActivityManagerService.getDefault(), battery);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
145 Log.i(TAG, "Starting Alarm Manager.");
146 AlarmManagerService alarm = new AlarmManagerService(context);
147 ServiceManager.addService(Context.ALARM_SERVICE, alarm);
148
149 Watchdog.getInstance().init(context, battery, power, alarm,
150 ActivityManagerService.self());
151
152 // Sensor Service is needed by Window Manager, so this goes first
153 Log.i(TAG, "Starting Sensor Service.");
154 ServiceManager.addService(Context.SENSOR_SERVICE, new SensorService(context));
155
156 Log.i(TAG, "Starting Window Manager.");
157 wm = WindowManagerService.main(context, power,
158 factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
159 ServiceManager.addService(Context.WINDOW_SERVICE, wm);
160
161 ((ActivityManagerService)ServiceManager.getService("activity"))
162 .setWindowManager(wm);
163
164 // Skip Bluetooth if we have an emulator kernel
165 // TODO: Use a more reliable check to see if this product should
166 // support Bluetooth - see bug 988521
167 if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
168 Log.i(TAG, "Registering null Bluetooth Service (emulator)");
169 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
170 } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
171 Log.i(TAG, "Registering null Bluetooth Service (factory test)");
172 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
173 } else {
174 Log.i(TAG, "Starting Bluetooth Service.");
Nick Pellybd022f42009-08-14 18:33:38 -0700175 bluetooth = new BluetoothService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 ServiceManager.addService(Context.BLUETOOTH_SERVICE, bluetooth);
Nick Pellybd022f42009-08-14 18:33:38 -0700177 bluetooth.initAfterRegistration();
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700178 bluetoothA2dp = new BluetoothA2dpService(context, bluetooth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
180 bluetoothA2dp);
181
182 int bluetoothOn = Settings.Secure.getInt(mContentResolver,
183 Settings.Secure.BLUETOOTH_ON, 0);
184 if (bluetoothOn > 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700185 bluetooth.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 }
187 }
188
189 } catch (RuntimeException e) {
190 Log.e("System", "Failure starting core service", e);
191 }
192
193 StatusBarService statusBar = null;
194 InputMethodManagerService imm = null;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700195 AppWidgetService appWidget = null;
Joe Onorato30275482009-07-08 17:09:14 -0700196 NotificationManagerService notification = null;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700197 WallpaperManagerService wallpaper = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
199 if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
200 try {
201 Log.i(TAG, "Starting Status Bar Service.");
202 statusBar = new StatusBarService(context);
203 ServiceManager.addService("statusbar", statusBar);
204 } catch (Throwable e) {
205 Log.e(TAG, "Failure starting StatusBarService", e);
206 }
207
208 try {
209 Log.i(TAG, "Starting Clipboard Service.");
210 ServiceManager.addService("clipboard", new ClipboardService(context));
211 } catch (Throwable e) {
212 Log.e(TAG, "Failure starting Clipboard Service", e);
213 }
214
215 try {
216 Log.i(TAG, "Starting Input Method Service.");
217 imm = new InputMethodManagerService(context, statusBar);
218 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
219 } catch (Throwable e) {
220 Log.e(TAG, "Failure starting Input Manager Service", e);
221 }
222
223 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 Log.i(TAG, "Starting NetStat Service.");
225 ServiceManager.addService("netstat", new NetStatService(context));
226 } catch (Throwable e) {
227 Log.e(TAG, "Failure starting NetStat Service", e);
228 }
229
230 try {
231 Log.i(TAG, "Starting Connectivity Service.");
Mike Lockwood0f79b542009-08-14 14:18:49 -0400232 connectivity = ConnectivityService.getInstance(context);
233 ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 } catch (Throwable e) {
235 Log.e(TAG, "Failure starting Connectivity Service", e);
236 }
237
238 try {
svetoslavganov75986cf2009-05-14 22:28:01 -0700239 Log.i(TAG, "Starting Accessibility Manager.");
240 ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
241 new AccessibilityManagerService(context));
242 } catch (Throwable e) {
243 Log.e(TAG, "Failure starting Accessibility Manager", e);
244 }
245
246 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 Log.i(TAG, "Starting Notification Manager.");
Joe Onorato30275482009-07-08 17:09:14 -0700248 notification = new NotificationManagerService(context, statusBar, hardware);
249 ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 } catch (Throwable e) {
251 Log.e(TAG, "Failure starting Notification Manager", e);
252 }
253
254 try {
255 // MountService must start after NotificationManagerService
256 Log.i(TAG, "Starting Mount Service.");
257 ServiceManager.addService("mount", new MountService(context));
258 } catch (Throwable e) {
259 Log.e(TAG, "Failure starting Mount Service", e);
260 }
261
262 try {
263 Log.i(TAG, "Starting DeviceStorageMonitor service");
264 ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
265 new DeviceStorageMonitorService(context));
266 } catch (Throwable e) {
267 Log.e(TAG, "Failure starting DeviceStorageMonitor service", e);
268 }
269
270 try {
271 Log.i(TAG, "Starting Location Manager.");
272 ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
273 } catch (Throwable e) {
274 Log.e(TAG, "Failure starting Location Manager", e);
275 }
276
277 try {
278 Log.i(TAG, "Starting Search Service.");
279 ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) );
280 } catch (Throwable e) {
281 Log.e(TAG, "Failure starting Search Service", e);
282 }
283
284 if (INCLUDE_DEMO) {
285 Log.i(TAG, "Installing demo data...");
286 (new DemoThread(context)).start();
287 }
288
289 try {
290 Log.i(TAG, "Starting Checkin Service.");
291 Intent intent = new Intent().setComponent(new ComponentName(
292 "com.google.android.server.checkin",
293 "com.google.android.server.checkin.CheckinService"));
294 if (context.startService(intent) == null) {
295 Log.w(TAG, "Using fallback Checkin Service.");
296 ServiceManager.addService("checkin", new FallbackCheckinService(context));
297 }
298 } catch (Throwable e) {
299 Log.e(TAG, "Failure starting Checkin Service", e);
300 }
301
302 try {
303 Log.i(TAG, "Starting Wallpaper Service");
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700304 wallpaper = new WallpaperManagerService(context);
305 ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 } catch (Throwable e) {
307 Log.e(TAG, "Failure starting Wallpaper Service", e);
308 }
309
310 try {
311 Log.i(TAG, "Starting Audio Service");
312 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
313 } catch (Throwable e) {
314 Log.e(TAG, "Failure starting Audio Service", e);
315 }
316
317 try {
318 Log.i(TAG, "Starting HeadsetObserver");
319 // Listen for wired headset changes
320 headset = new HeadsetObserver(context);
321 } catch (Throwable e) {
322 Log.e(TAG, "Failure starting HeadsetObserver", e);
323 }
324
325 try {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500326 Log.i(TAG, "Starting DockObserver");
327 // Listen for dock station changes
328 dock = new DockObserver(context);
329 } catch (Throwable e) {
330 Log.e(TAG, "Failure starting DockObserver", e);
331 }
332
333 try {
Christopher Tate487529a2009-04-29 14:03:25 -0700334 Log.i(TAG, "Starting Backup Service");
335 ServiceManager.addService(Context.BACKUP_SERVICE, new BackupManagerService(context));
336 } catch (Throwable e) {
337 Log.e(TAG, "Failure starting Backup Service", e);
338 }
339
340 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700341 Log.i(TAG, "Starting AppWidget Service");
342 appWidget = new AppWidgetService(context);
343 ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 } catch (Throwable e) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700345 Log.e(TAG, "Failure starting AppWidget Service", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
347
348 try {
349 com.android.server.status.StatusBarPolicy.installIcons(context, statusBar);
350 } catch (Throwable e) {
351 Log.e(TAG, "Failure installing status bar icons", e);
352 }
353 }
354
355 // make sure the ADB_ENABLED setting value matches the secure property value
356 Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
357 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
358
359 // register observer to listen for settings changes
360 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
361 false, new AdbSettingsObserver());
362
363 // It is now time to start up the app processes...
364 boolean safeMode = wm.detectSafeMode();
Joe Onorato30275482009-07-08 17:09:14 -0700365
366 if (notification != null) {
367 notification.systemReady();
368 }
369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 if (statusBar != null) {
371 statusBar.systemReady();
372 }
373 if (imm != null) {
374 imm.systemReady();
375 }
376 wm.systemReady();
377 power.systemReady();
378 try {
379 pm.systemReady();
380 } catch (RemoteException e) {
381 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700382 if (appWidget != null) {
383 appWidget.systemReady(safeMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 }
385
386 // After making the following code, third party code may be running...
387 try {
388 ActivityManagerNative.getDefault().systemReady();
389 } catch (RemoteException e) {
390 }
391
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700392 if (wallpaper != null) wallpaper.systemReady();
Mike Lockwood0f79b542009-08-14 14:18:49 -0400393 if (battery != null) battery.systemReady();
394 if (connectivity != null) connectivity.systemReady();
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700395 if (dock != null) dock.systemReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 Watchdog.getInstance().start();
397
398 Looper.loop();
399 Log.d(TAG, "System ServerThread is exiting!");
400 }
401}
402
403class DemoThread extends Thread
404{
405 DemoThread(Context context)
406 {
407 mContext = context;
408 }
409
410 @Override
411 public void run()
412 {
413 try {
414 Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
415 boolean hasData = c != null && c.moveToFirst();
416 if (c != null) {
417 c.deactivate();
418 }
419 if (!hasData) {
420 DemoDataSet dataset = new DemoDataSet();
421 dataset.add(mContext);
422 }
423 } catch (Throwable e) {
424 Log.e("SystemServer", "Failure installing demo data", e);
425 }
426
427 }
428
429 Context mContext;
430}
431
432public class SystemServer
433{
434 private static final String TAG = "SystemServer";
435
436 public static final int FACTORY_TEST_OFF = 0;
437 public static final int FACTORY_TEST_LOW_LEVEL = 1;
438 public static final int FACTORY_TEST_HIGH_LEVEL = 2;
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700439
440 /**
441 * 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 -0800442 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
443 * up into init2() to start the Android services.
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700444 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 native public static void init1(String[] args);
446
447 public static void main(String[] args) {
448 // The system server has to run all of the time, so it needs to be
449 // as efficient as possible with its memory usage.
450 VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 System.loadLibrary("android_servers");
453 init1(args);
454 }
455
456 public static final void init2() {
457 Log.i(TAG, "Entered the Android system server!");
458 Thread thr = new ServerThread();
459 thr.setName("android.server.ServerThread");
460 thr.start();
461 }
462}