blob: 8919ccc24e08f54577e85bd075902519a4c7002a [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
200 if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
201 try {
202 Log.i(TAG, "Starting Status Bar Service.");
203 statusBar = new StatusBarService(context);
204 ServiceManager.addService("statusbar", statusBar);
205 } catch (Throwable e) {
206 Log.e(TAG, "Failure starting StatusBarService", e);
207 }
208
209 try {
210 Log.i(TAG, "Starting Clipboard Service.");
211 ServiceManager.addService("clipboard", new ClipboardService(context));
212 } catch (Throwable e) {
213 Log.e(TAG, "Failure starting Clipboard Service", e);
214 }
215
216 try {
217 Log.i(TAG, "Starting Input Method Service.");
218 imm = new InputMethodManagerService(context, statusBar);
219 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
220 } catch (Throwable e) {
221 Log.e(TAG, "Failure starting Input Manager Service", e);
222 }
223
224 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 Log.i(TAG, "Starting NetStat Service.");
226 ServiceManager.addService("netstat", new NetStatService(context));
227 } catch (Throwable e) {
228 Log.e(TAG, "Failure starting NetStat Service", e);
229 }
230
231 try {
232 Log.i(TAG, "Starting Connectivity Service.");
233 ServiceManager.addService(Context.CONNECTIVITY_SERVICE,
234 ConnectivityService.getInstance(context));
235 } catch (Throwable e) {
236 Log.e(TAG, "Failure starting Connectivity Service", e);
237 }
238
239 try {
svetoslavganov75986cf2009-05-14 22:28:01 -0700240 Log.i(TAG, "Starting Accessibility Manager.");
241 ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
242 new AccessibilityManagerService(context));
243 } catch (Throwable e) {
244 Log.e(TAG, "Failure starting Accessibility Manager", e);
245 }
246
247 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 Log.i(TAG, "Starting Notification Manager.");
Joe Onorato30275482009-07-08 17:09:14 -0700249 notification = new NotificationManagerService(context, statusBar, hardware);
250 ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 } catch (Throwable e) {
252 Log.e(TAG, "Failure starting Notification Manager", e);
253 }
254
255 try {
256 // MountService must start after NotificationManagerService
257 Log.i(TAG, "Starting Mount Service.");
258 ServiceManager.addService("mount", new MountService(context));
259 } catch (Throwable e) {
260 Log.e(TAG, "Failure starting Mount Service", e);
261 }
262
263 try {
264 Log.i(TAG, "Starting DeviceStorageMonitor service");
265 ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
266 new DeviceStorageMonitorService(context));
267 } catch (Throwable e) {
268 Log.e(TAG, "Failure starting DeviceStorageMonitor service", e);
269 }
270
271 try {
272 Log.i(TAG, "Starting Location Manager.");
273 ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
274 } catch (Throwable e) {
275 Log.e(TAG, "Failure starting Location Manager", e);
276 }
277
278 try {
279 Log.i(TAG, "Starting Search Service.");
280 ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) );
281 } catch (Throwable e) {
282 Log.e(TAG, "Failure starting Search Service", e);
283 }
284
285 if (INCLUDE_DEMO) {
286 Log.i(TAG, "Installing demo data...");
287 (new DemoThread(context)).start();
288 }
289
290 try {
291 Log.i(TAG, "Starting Checkin Service.");
292 Intent intent = new Intent().setComponent(new ComponentName(
293 "com.google.android.server.checkin",
294 "com.google.android.server.checkin.CheckinService"));
295 if (context.startService(intent) == null) {
296 Log.w(TAG, "Using fallback Checkin Service.");
297 ServiceManager.addService("checkin", new FallbackCheckinService(context));
298 }
299 } catch (Throwable e) {
300 Log.e(TAG, "Failure starting Checkin Service", e);
301 }
302
303 try {
304 Log.i(TAG, "Starting Wallpaper Service");
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700305 ServiceManager.addService(Context.WALLPAPER_SERVICE, new WallpaperManagerService(context));
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 {
Christopher Tate487529a2009-04-29 14:03:25 -0700326 Log.i(TAG, "Starting Backup Service");
327 ServiceManager.addService(Context.BACKUP_SERVICE, new BackupManagerService(context));
328 } catch (Throwable e) {
329 Log.e(TAG, "Failure starting Backup Service", e);
330 }
331
332 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700333 Log.i(TAG, "Starting AppWidget Service");
334 appWidget = new AppWidgetService(context);
335 ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 } catch (Throwable e) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700337 Log.e(TAG, "Failure starting AppWidget Service", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 }
339
340 try {
341 com.android.server.status.StatusBarPolicy.installIcons(context, statusBar);
342 } catch (Throwable e) {
343 Log.e(TAG, "Failure installing status bar icons", e);
344 }
345 }
346
347 // make sure the ADB_ENABLED setting value matches the secure property value
348 Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
349 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
350
351 // register observer to listen for settings changes
352 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
353 false, new AdbSettingsObserver());
354
355 // It is now time to start up the app processes...
356 boolean safeMode = wm.detectSafeMode();
Joe Onorato30275482009-07-08 17:09:14 -0700357
358 if (notification != null) {
359 notification.systemReady();
360 }
361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 if (statusBar != null) {
363 statusBar.systemReady();
364 }
365 if (imm != null) {
366 imm.systemReady();
367 }
368 wm.systemReady();
369 power.systemReady();
370 try {
371 pm.systemReady();
372 } catch (RemoteException e) {
373 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700374 if (appWidget != null) {
375 appWidget.systemReady(safeMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
377
378 // After making the following code, third party code may be running...
379 try {
380 ActivityManagerNative.getDefault().systemReady();
381 } catch (RemoteException e) {
382 }
383
Mike Lockwood07a500f2009-08-12 09:56:44 -0400384 battery.systemReady();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 Watchdog.getInstance().start();
386
387 Looper.loop();
388 Log.d(TAG, "System ServerThread is exiting!");
389 }
390}
391
392class DemoThread extends Thread
393{
394 DemoThread(Context context)
395 {
396 mContext = context;
397 }
398
399 @Override
400 public void run()
401 {
402 try {
403 Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
404 boolean hasData = c != null && c.moveToFirst();
405 if (c != null) {
406 c.deactivate();
407 }
408 if (!hasData) {
409 DemoDataSet dataset = new DemoDataSet();
410 dataset.add(mContext);
411 }
412 } catch (Throwable e) {
413 Log.e("SystemServer", "Failure installing demo data", e);
414 }
415
416 }
417
418 Context mContext;
419}
420
421public class SystemServer
422{
423 private static final String TAG = "SystemServer";
424
425 public static final int FACTORY_TEST_OFF = 0;
426 public static final int FACTORY_TEST_LOW_LEVEL = 1;
427 public static final int FACTORY_TEST_HIGH_LEVEL = 2;
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700428
429 /**
430 * 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 -0800431 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
432 * up into init2() to start the Android services.
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700433 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 native public static void init1(String[] args);
435
436 public static void main(String[] args) {
437 // The system server has to run all of the time, so it needs to be
438 // as efficient as possible with its memory usage.
439 VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 System.loadLibrary("android_servers");
442 init1(args);
443 }
444
445 public static final void init2() {
446 Log.i(TAG, "Entered the Android system server!");
447 Thread thr = new ServerThread();
448 thr.setName("android.server.ServerThread");
449 thr.start();
450 }
451}