blob: ad8e8921751dcbcd5e31e44f98be6aad4181073d [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 IPackageManager pm = null;
89 Context context = null;
90 WindowManagerService wm = null;
91 BluetoothDeviceService bluetooth = null;
92 BluetoothA2dpService bluetoothA2dp = null;
93 HeadsetObserver headset = null;
94
95 // Critical services...
96 try {
Nick Kralevich4fb25612009-06-17 16:03:22 -070097 Log.i(TAG, "Starting Entropy Service.");
98 ServiceManager.addService("entropy", new EntropyService());
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 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
Fred Quintana60307342009-03-24 22:48:12 -0700120 try {
121 Log.i(TAG, "Starting Account Manager.");
122 ServiceManager.addService(Context.ACCOUNT_SERVICE,
123 new AccountManagerService(context));
124 } catch (Throwable e) {
125 Log.e(TAG, "Failure starting Account Manager", e);
126 }
127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 Log.i(TAG, "Starting Content Manager.");
129 ContentService.main(context,
130 factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
131
132 Log.i(TAG, "Starting System Content Providers.");
133 ActivityManagerService.installSystemProviders();
134
135 Log.i(TAG, "Starting Battery Service.");
Mike Lockwood07a500f2009-08-12 09:56:44 -0400136 battery = new BatteryService(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 ServiceManager.addService("battery", battery);
138
The Android Open Source Project10592532009-03-18 17:39:46 -0700139 Log.i(TAG, "Starting Hardware Service.");
140 hardware = new HardwareService(context);
141 ServiceManager.addService("hardware", hardware);
142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 // only initialize the power service after we have started the
The Android Open Source Project10592532009-03-18 17:39:46 -0700144 // hardware service, content providers and the battery service.
145 power.init(context, hardware, ActivityManagerService.getDefault(), battery);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146
147 Log.i(TAG, "Starting Alarm Manager.");
148 AlarmManagerService alarm = new AlarmManagerService(context);
149 ServiceManager.addService(Context.ALARM_SERVICE, alarm);
150
151 Watchdog.getInstance().init(context, battery, power, alarm,
152 ActivityManagerService.self());
153
154 // Sensor Service is needed by Window Manager, so this goes first
155 Log.i(TAG, "Starting Sensor Service.");
156 ServiceManager.addService(Context.SENSOR_SERVICE, new SensorService(context));
157
158 Log.i(TAG, "Starting Window Manager.");
159 wm = WindowManagerService.main(context, power,
160 factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
161 ServiceManager.addService(Context.WINDOW_SERVICE, wm);
162
163 ((ActivityManagerService)ServiceManager.getService("activity"))
164 .setWindowManager(wm);
165
166 // Skip Bluetooth if we have an emulator kernel
167 // TODO: Use a more reliable check to see if this product should
168 // support Bluetooth - see bug 988521
169 if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
170 Log.i(TAG, "Registering null Bluetooth Service (emulator)");
171 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
172 } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
173 Log.i(TAG, "Registering null Bluetooth Service (factory test)");
174 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
175 } else {
176 Log.i(TAG, "Starting Bluetooth Service.");
177 bluetooth = new BluetoothDeviceService(context);
178 bluetooth.init();
179 ServiceManager.addService(Context.BLUETOOTH_SERVICE, bluetooth);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700180 bluetoothA2dp = new BluetoothA2dpService(context, bluetooth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
182 bluetoothA2dp);
183
184 int bluetoothOn = Settings.Secure.getInt(mContentResolver,
185 Settings.Secure.BLUETOOTH_ON, 0);
186 if (bluetoothOn > 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700187 bluetooth.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 }
189 }
190
191 } catch (RuntimeException e) {
192 Log.e("System", "Failure starting core service", e);
193 }
194
195 StatusBarService statusBar = null;
196 InputMethodManagerService imm = null;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700197 AppWidgetService appWidget = null;
Joe Onorato30275482009-07-08 17:09:14 -0700198 NotificationManagerService notification = null;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700199 WallpaperManagerService wallpaper = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200
201 if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
202 try {
203 Log.i(TAG, "Starting Status Bar Service.");
204 statusBar = new StatusBarService(context);
205 ServiceManager.addService("statusbar", statusBar);
206 } catch (Throwable e) {
207 Log.e(TAG, "Failure starting StatusBarService", e);
208 }
209
210 try {
211 Log.i(TAG, "Starting Clipboard Service.");
212 ServiceManager.addService("clipboard", new ClipboardService(context));
213 } catch (Throwable e) {
214 Log.e(TAG, "Failure starting Clipboard Service", e);
215 }
216
217 try {
218 Log.i(TAG, "Starting Input Method Service.");
219 imm = new InputMethodManagerService(context, statusBar);
220 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
221 } catch (Throwable e) {
222 Log.e(TAG, "Failure starting Input Manager Service", e);
223 }
224
225 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 Log.i(TAG, "Starting NetStat Service.");
227 ServiceManager.addService("netstat", new NetStatService(context));
228 } catch (Throwable e) {
229 Log.e(TAG, "Failure starting NetStat Service", e);
230 }
231
232 try {
233 Log.i(TAG, "Starting Connectivity Service.");
234 ServiceManager.addService(Context.CONNECTIVITY_SERVICE,
235 ConnectivityService.getInstance(context));
236 } catch (Throwable e) {
237 Log.e(TAG, "Failure starting Connectivity Service", e);
238 }
239
240 try {
svetoslavganov75986cf2009-05-14 22:28:01 -0700241 Log.i(TAG, "Starting Accessibility Manager.");
242 ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
243 new AccessibilityManagerService(context));
244 } catch (Throwable e) {
245 Log.e(TAG, "Failure starting Accessibility Manager", e);
246 }
247
248 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 Log.i(TAG, "Starting Notification Manager.");
Joe Onorato30275482009-07-08 17:09:14 -0700250 notification = new NotificationManagerService(context, statusBar, hardware);
251 ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 } catch (Throwable e) {
253 Log.e(TAG, "Failure starting Notification Manager", e);
254 }
255
256 try {
257 // MountService must start after NotificationManagerService
258 Log.i(TAG, "Starting Mount Service.");
259 ServiceManager.addService("mount", new MountService(context));
260 } catch (Throwable e) {
261 Log.e(TAG, "Failure starting Mount Service", e);
262 }
263
264 try {
265 Log.i(TAG, "Starting DeviceStorageMonitor service");
266 ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
267 new DeviceStorageMonitorService(context));
268 } catch (Throwable e) {
269 Log.e(TAG, "Failure starting DeviceStorageMonitor service", e);
270 }
271
272 try {
273 Log.i(TAG, "Starting Location Manager.");
274 ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
275 } catch (Throwable e) {
276 Log.e(TAG, "Failure starting Location Manager", e);
277 }
278
279 try {
280 Log.i(TAG, "Starting Search Service.");
281 ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) );
282 } catch (Throwable e) {
283 Log.e(TAG, "Failure starting Search Service", e);
284 }
285
286 if (INCLUDE_DEMO) {
287 Log.i(TAG, "Installing demo data...");
288 (new DemoThread(context)).start();
289 }
290
291 try {
292 Log.i(TAG, "Starting Checkin Service.");
293 Intent intent = new Intent().setComponent(new ComponentName(
294 "com.google.android.server.checkin",
295 "com.google.android.server.checkin.CheckinService"));
296 if (context.startService(intent) == null) {
297 Log.w(TAG, "Using fallback Checkin Service.");
298 ServiceManager.addService("checkin", new FallbackCheckinService(context));
299 }
300 } catch (Throwable e) {
301 Log.e(TAG, "Failure starting Checkin Service", e);
302 }
303
304 try {
305 Log.i(TAG, "Starting Wallpaper Service");
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700306 wallpaper = new WallpaperManagerService(context);
307 ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 } catch (Throwable e) {
309 Log.e(TAG, "Failure starting Wallpaper Service", e);
310 }
311
312 try {
313 Log.i(TAG, "Starting Audio Service");
314 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
315 } catch (Throwable e) {
316 Log.e(TAG, "Failure starting Audio Service", e);
317 }
318
319 try {
320 Log.i(TAG, "Starting HeadsetObserver");
321 // Listen for wired headset changes
322 headset = new HeadsetObserver(context);
323 } catch (Throwable e) {
324 Log.e(TAG, "Failure starting HeadsetObserver", e);
325 }
326
327 try {
Christopher Tate487529a2009-04-29 14:03:25 -0700328 Log.i(TAG, "Starting Backup Service");
329 ServiceManager.addService(Context.BACKUP_SERVICE, new BackupManagerService(context));
330 } catch (Throwable e) {
331 Log.e(TAG, "Failure starting Backup Service", e);
332 }
333
334 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700335 Log.i(TAG, "Starting AppWidget Service");
336 appWidget = new AppWidgetService(context);
337 ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 } catch (Throwable e) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700339 Log.e(TAG, "Failure starting AppWidget Service", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 }
341
342 try {
343 com.android.server.status.StatusBarPolicy.installIcons(context, statusBar);
344 } catch (Throwable e) {
345 Log.e(TAG, "Failure installing status bar icons", e);
346 }
347 }
348
349 // make sure the ADB_ENABLED setting value matches the secure property value
350 Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
351 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
352
353 // register observer to listen for settings changes
354 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
355 false, new AdbSettingsObserver());
356
357 // It is now time to start up the app processes...
358 boolean safeMode = wm.detectSafeMode();
Joe Onorato30275482009-07-08 17:09:14 -0700359
360 if (notification != null) {
361 notification.systemReady();
362 }
363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 if (statusBar != null) {
365 statusBar.systemReady();
366 }
367 if (imm != null) {
368 imm.systemReady();
369 }
370 wm.systemReady();
371 power.systemReady();
372 try {
373 pm.systemReady();
374 } catch (RemoteException e) {
375 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700376 if (appWidget != null) {
377 appWidget.systemReady(safeMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
379
380 // After making the following code, third party code may be running...
381 try {
382 ActivityManagerNative.getDefault().systemReady();
383 } catch (RemoteException e) {
384 }
385
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700386 if (wallpaper != null) wallpaper.systemReady();
Mike Lockwood07a500f2009-08-12 09:56:44 -0400387 battery.systemReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 Watchdog.getInstance().start();
389
390 Looper.loop();
391 Log.d(TAG, "System ServerThread is exiting!");
392 }
393}
394
395class DemoThread extends Thread
396{
397 DemoThread(Context context)
398 {
399 mContext = context;
400 }
401
402 @Override
403 public void run()
404 {
405 try {
406 Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
407 boolean hasData = c != null && c.moveToFirst();
408 if (c != null) {
409 c.deactivate();
410 }
411 if (!hasData) {
412 DemoDataSet dataset = new DemoDataSet();
413 dataset.add(mContext);
414 }
415 } catch (Throwable e) {
416 Log.e("SystemServer", "Failure installing demo data", e);
417 }
418
419 }
420
421 Context mContext;
422}
423
424public class SystemServer
425{
426 private static final String TAG = "SystemServer";
427
428 public static final int FACTORY_TEST_OFF = 0;
429 public static final int FACTORY_TEST_LOW_LEVEL = 1;
430 public static final int FACTORY_TEST_HIGH_LEVEL = 2;
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700431
432 /**
433 * 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 -0800434 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
435 * up into init2() to start the Android services.
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700436 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 native public static void init1(String[] args);
438
439 public static void main(String[] args) {
440 // The system server has to run all of the time, so it needs to be
441 // as efficient as possible with its memory usage.
442 VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 System.loadLibrary("android_servers");
445 init1(args);
446 }
447
448 public static final void init2() {
449 Log.i(TAG, "Entered the Android system server!");
450 Thread thr = new ServerThread();
451 thr.setName("android.server.ServerThread");
452 thr.start();
453 }
454}