blob: ce476ebc4e33bf141aa1e57365099ac29e516c8f [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;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050095 DockObserver dock = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
97 // Critical services...
98 try {
Nick Kralevich4fb25612009-06-17 16:03:22 -070099 Log.i(TAG, "Starting Entropy Service.");
100 ServiceManager.addService("entropy", new EntropyService());
101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 Log.i(TAG, "Starting Power Manager.");
103 power = new PowerManagerService();
104 ServiceManager.addService(Context.POWER_SERVICE, power);
105
106 Log.i(TAG, "Starting Activity Manager.");
107 context = ActivityManagerService.main(factoryTest);
108
109 Log.i(TAG, "Starting telephony registry");
110 ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));
111
112 AttributeCache.init(context);
113
114 Log.i(TAG, "Starting Package Manager.");
115 pm = PackageManagerService.main(context,
116 factoryTest != SystemServer.FACTORY_TEST_OFF);
117
118 ActivityManagerService.setSystemProcess();
119
120 mContentResolver = context.getContentResolver();
121
Fred Quintana60307342009-03-24 22:48:12 -0700122 try {
123 Log.i(TAG, "Starting Account Manager.");
124 ServiceManager.addService(Context.ACCOUNT_SERVICE,
125 new AccountManagerService(context));
126 } catch (Throwable e) {
127 Log.e(TAG, "Failure starting Account Manager", e);
128 }
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 Log.i(TAG, "Starting Content Manager.");
131 ContentService.main(context,
132 factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
133
134 Log.i(TAG, "Starting System Content Providers.");
135 ActivityManagerService.installSystemProviders();
136
137 Log.i(TAG, "Starting Battery Service.");
Mike Lockwood07a500f2009-08-12 09:56:44 -0400138 battery = new BatteryService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 ServiceManager.addService("battery", battery);
140
The Android Open Source Project10592532009-03-18 17:39:46 -0700141 Log.i(TAG, "Starting Hardware Service.");
142 hardware = new HardwareService(context);
143 ServiceManager.addService("hardware", hardware);
144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 // only initialize the power service after we have started the
The Android Open Source Project10592532009-03-18 17:39:46 -0700146 // hardware service, content providers and the battery service.
147 power.init(context, hardware, ActivityManagerService.getDefault(), battery);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
149 Log.i(TAG, "Starting Alarm Manager.");
150 AlarmManagerService alarm = new AlarmManagerService(context);
151 ServiceManager.addService(Context.ALARM_SERVICE, alarm);
152
153 Watchdog.getInstance().init(context, battery, power, alarm,
154 ActivityManagerService.self());
155
156 // Sensor Service is needed by Window Manager, so this goes first
157 Log.i(TAG, "Starting Sensor Service.");
158 ServiceManager.addService(Context.SENSOR_SERVICE, new SensorService(context));
159
160 Log.i(TAG, "Starting Window Manager.");
161 wm = WindowManagerService.main(context, power,
162 factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
163 ServiceManager.addService(Context.WINDOW_SERVICE, wm);
164
165 ((ActivityManagerService)ServiceManager.getService("activity"))
166 .setWindowManager(wm);
167
168 // Skip Bluetooth if we have an emulator kernel
169 // TODO: Use a more reliable check to see if this product should
170 // support Bluetooth - see bug 988521
171 if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
172 Log.i(TAG, "Registering null Bluetooth Service (emulator)");
173 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
174 } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
175 Log.i(TAG, "Registering null Bluetooth Service (factory test)");
176 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
177 } else {
178 Log.i(TAG, "Starting Bluetooth Service.");
179 bluetooth = new BluetoothDeviceService(context);
180 bluetooth.init();
181 ServiceManager.addService(Context.BLUETOOTH_SERVICE, bluetooth);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700182 bluetoothA2dp = new BluetoothA2dpService(context, bluetooth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
184 bluetoothA2dp);
185
186 int bluetoothOn = Settings.Secure.getInt(mContentResolver,
187 Settings.Secure.BLUETOOTH_ON, 0);
188 if (bluetoothOn > 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700189 bluetooth.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 }
191 }
192
193 } catch (RuntimeException e) {
194 Log.e("System", "Failure starting core service", e);
195 }
196
197 StatusBarService statusBar = null;
198 InputMethodManagerService imm = null;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700199 AppWidgetService appWidget = null;
Joe Onorato30275482009-07-08 17:09:14 -0700200 NotificationManagerService notification = null;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700201 WallpaperManagerService wallpaper = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202
203 if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
204 try {
205 Log.i(TAG, "Starting Status Bar Service.");
206 statusBar = new StatusBarService(context);
207 ServiceManager.addService("statusbar", statusBar);
208 } catch (Throwable e) {
209 Log.e(TAG, "Failure starting StatusBarService", e);
210 }
211
212 try {
213 Log.i(TAG, "Starting Clipboard Service.");
214 ServiceManager.addService("clipboard", new ClipboardService(context));
215 } catch (Throwable e) {
216 Log.e(TAG, "Failure starting Clipboard Service", e);
217 }
218
219 try {
220 Log.i(TAG, "Starting Input Method Service.");
221 imm = new InputMethodManagerService(context, statusBar);
222 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
223 } catch (Throwable e) {
224 Log.e(TAG, "Failure starting Input Manager Service", e);
225 }
226
227 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 Log.i(TAG, "Starting NetStat Service.");
229 ServiceManager.addService("netstat", new NetStatService(context));
230 } catch (Throwable e) {
231 Log.e(TAG, "Failure starting NetStat Service", e);
232 }
233
234 try {
235 Log.i(TAG, "Starting Connectivity Service.");
Mike Lockwood0f79b542009-08-14 14:18:49 -0400236 connectivity = ConnectivityService.getInstance(context);
237 ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 } catch (Throwable e) {
239 Log.e(TAG, "Failure starting Connectivity Service", e);
240 }
241
242 try {
svetoslavganov75986cf2009-05-14 22:28:01 -0700243 Log.i(TAG, "Starting Accessibility Manager.");
244 ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
245 new AccessibilityManagerService(context));
246 } catch (Throwable e) {
247 Log.e(TAG, "Failure starting Accessibility Manager", e);
248 }
249
250 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 Log.i(TAG, "Starting Notification Manager.");
Joe Onorato30275482009-07-08 17:09:14 -0700252 notification = new NotificationManagerService(context, statusBar, hardware);
253 ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 } catch (Throwable e) {
255 Log.e(TAG, "Failure starting Notification Manager", e);
256 }
257
258 try {
259 // MountService must start after NotificationManagerService
260 Log.i(TAG, "Starting Mount Service.");
261 ServiceManager.addService("mount", new MountService(context));
262 } catch (Throwable e) {
263 Log.e(TAG, "Failure starting Mount Service", e);
264 }
265
266 try {
267 Log.i(TAG, "Starting DeviceStorageMonitor service");
268 ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
269 new DeviceStorageMonitorService(context));
270 } catch (Throwable e) {
271 Log.e(TAG, "Failure starting DeviceStorageMonitor service", e);
272 }
273
274 try {
275 Log.i(TAG, "Starting Location Manager.");
276 ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
277 } catch (Throwable e) {
278 Log.e(TAG, "Failure starting Location Manager", e);
279 }
280
281 try {
282 Log.i(TAG, "Starting Search Service.");
283 ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) );
284 } catch (Throwable e) {
285 Log.e(TAG, "Failure starting Search Service", e);
286 }
287
288 if (INCLUDE_DEMO) {
289 Log.i(TAG, "Installing demo data...");
290 (new DemoThread(context)).start();
291 }
292
293 try {
294 Log.i(TAG, "Starting Checkin Service.");
295 Intent intent = new Intent().setComponent(new ComponentName(
296 "com.google.android.server.checkin",
297 "com.google.android.server.checkin.CheckinService"));
298 if (context.startService(intent) == null) {
299 Log.w(TAG, "Using fallback Checkin Service.");
300 ServiceManager.addService("checkin", new FallbackCheckinService(context));
301 }
302 } catch (Throwable e) {
303 Log.e(TAG, "Failure starting Checkin Service", e);
304 }
305
306 try {
307 Log.i(TAG, "Starting Wallpaper Service");
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700308 wallpaper = new WallpaperManagerService(context);
309 ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 } catch (Throwable e) {
311 Log.e(TAG, "Failure starting Wallpaper Service", e);
312 }
313
314 try {
315 Log.i(TAG, "Starting Audio Service");
316 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
317 } catch (Throwable e) {
318 Log.e(TAG, "Failure starting Audio Service", e);
319 }
320
321 try {
322 Log.i(TAG, "Starting HeadsetObserver");
323 // Listen for wired headset changes
324 headset = new HeadsetObserver(context);
325 } catch (Throwable e) {
326 Log.e(TAG, "Failure starting HeadsetObserver", e);
327 }
328
329 try {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500330 Log.i(TAG, "Starting DockObserver");
331 // Listen for dock station changes
332 dock = new DockObserver(context);
333 } catch (Throwable e) {
334 Log.e(TAG, "Failure starting DockObserver", e);
335 }
336
337 try {
Christopher Tate487529a2009-04-29 14:03:25 -0700338 Log.i(TAG, "Starting Backup Service");
339 ServiceManager.addService(Context.BACKUP_SERVICE, new BackupManagerService(context));
340 } catch (Throwable e) {
341 Log.e(TAG, "Failure starting Backup Service", e);
342 }
343
344 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700345 Log.i(TAG, "Starting AppWidget Service");
346 appWidget = new AppWidgetService(context);
347 ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 } catch (Throwable e) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700349 Log.e(TAG, "Failure starting AppWidget Service", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 }
351
352 try {
353 com.android.server.status.StatusBarPolicy.installIcons(context, statusBar);
354 } catch (Throwable e) {
355 Log.e(TAG, "Failure installing status bar icons", e);
356 }
357 }
358
359 // make sure the ADB_ENABLED setting value matches the secure property value
360 Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
361 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
362
363 // register observer to listen for settings changes
364 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
365 false, new AdbSettingsObserver());
366
367 // It is now time to start up the app processes...
368 boolean safeMode = wm.detectSafeMode();
Joe Onorato30275482009-07-08 17:09:14 -0700369
370 if (notification != null) {
371 notification.systemReady();
372 }
373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 if (statusBar != null) {
375 statusBar.systemReady();
376 }
377 if (imm != null) {
378 imm.systemReady();
379 }
380 wm.systemReady();
381 power.systemReady();
382 try {
383 pm.systemReady();
384 } catch (RemoteException e) {
385 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700386 if (appWidget != null) {
387 appWidget.systemReady(safeMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 }
389
390 // After making the following code, third party code may be running...
391 try {
392 ActivityManagerNative.getDefault().systemReady();
393 } catch (RemoteException e) {
394 }
395
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700396 if (wallpaper != null) wallpaper.systemReady();
Mike Lockwood0f79b542009-08-14 14:18:49 -0400397 if (battery != null) battery.systemReady();
398 if (connectivity != null) connectivity.systemReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 Watchdog.getInstance().start();
400
401 Looper.loop();
402 Log.d(TAG, "System ServerThread is exiting!");
403 }
404}
405
406class DemoThread extends Thread
407{
408 DemoThread(Context context)
409 {
410 mContext = context;
411 }
412
413 @Override
414 public void run()
415 {
416 try {
417 Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
418 boolean hasData = c != null && c.moveToFirst();
419 if (c != null) {
420 c.deactivate();
421 }
422 if (!hasData) {
423 DemoDataSet dataset = new DemoDataSet();
424 dataset.add(mContext);
425 }
426 } catch (Throwable e) {
427 Log.e("SystemServer", "Failure installing demo data", e);
428 }
429
430 }
431
432 Context mContext;
433}
434
435public class SystemServer
436{
437 private static final String TAG = "SystemServer";
438
439 public static final int FACTORY_TEST_OFF = 0;
440 public static final int FACTORY_TEST_LOW_LEVEL = 1;
441 public static final int FACTORY_TEST_HIGH_LEVEL = 2;
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700442
443 /**
444 * 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 -0800445 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
446 * up into init2() to start the Android services.
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700447 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 native public static void init1(String[] args);
449
450 public static void main(String[] args) {
451 // The system server has to run all of the time, so it needs to be
452 // as efficient as possible with its memory usage.
453 VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 System.loadLibrary("android_servers");
456 init1(args);
457 }
458
459 public static final void init2() {
460 Log.i(TAG, "Entered the Android system server!");
461 Thread thr = new ServerThread();
462 thr.setName("android.server.ServerThread");
463 thr.start();
464 }
465}