blob: b2848ac5c3da0ad474c4964b15b86d17407e2152 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.Looper;
35import android.os.RemoteException;
36import android.os.ServiceManager;
37import android.os.SystemClock;
38import android.os.SystemProperties;
39import android.provider.Contacts.People;
40import android.provider.Settings;
41import android.server.BluetoothA2dpService;
42import android.server.BluetoothDeviceService;
43import android.server.search.SearchManagerService;
44import android.util.EventLog;
45import android.util.Log;
46
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047class ServerThread extends Thread {
48 private static final String TAG = "SystemServer";
49 private final static boolean INCLUDE_DEMO = false;
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -070050 private final static boolean INCLUDE_BACKUP = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
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;
87 IPackageManager pm = null;
88 Context context = null;
89 WindowManagerService wm = null;
90 BluetoothDeviceService bluetooth = null;
91 BluetoothA2dpService bluetoothA2dp = null;
92 HeadsetObserver headset = null;
93
94 // Critical services...
95 try {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -070096 Log.i(TAG, "Starting Entropy Service.");
97 ServiceManager.addService("entropy", new EntropyService());
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 Log.i(TAG, "Starting Power Manager.");
100 power = new PowerManagerService();
101 ServiceManager.addService(Context.POWER_SERVICE, power);
102
103 Log.i(TAG, "Starting Activity Manager.");
104 context = ActivityManagerService.main(factoryTest);
105
106 Log.i(TAG, "Starting telephony registry");
107 ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));
108
109 AttributeCache.init(context);
110
111 Log.i(TAG, "Starting Package Manager.");
112 pm = PackageManagerService.main(context,
113 factoryTest != SystemServer.FACTORY_TEST_OFF);
114
115 ActivityManagerService.setSystemProcess();
116
117 mContentResolver = context.getContentResolver();
118
119 Log.i(TAG, "Starting Content Manager.");
120 ContentService.main(context,
121 factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
122
123 Log.i(TAG, "Starting System Content Providers.");
124 ActivityManagerService.installSystemProviders();
125
126 Log.i(TAG, "Starting Battery Service.");
127 BatteryService battery = new BatteryService(context);
128 ServiceManager.addService("battery", battery);
129
The Android Open Source Project10592532009-03-18 17:39:46 -0700130 Log.i(TAG, "Starting Hardware Service.");
131 hardware = new HardwareService(context);
132 ServiceManager.addService("hardware", hardware);
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 // only initialize the power service after we have started the
The Android Open Source Project10592532009-03-18 17:39:46 -0700135 // hardware service, content providers and the battery service.
136 power.init(context, hardware, ActivityManagerService.getDefault(), battery);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
138 Log.i(TAG, "Starting Alarm Manager.");
139 AlarmManagerService alarm = new AlarmManagerService(context);
140 ServiceManager.addService(Context.ALARM_SERVICE, alarm);
141
142 Watchdog.getInstance().init(context, battery, power, alarm,
143 ActivityManagerService.self());
144
145 // Sensor Service is needed by Window Manager, so this goes first
146 Log.i(TAG, "Starting Sensor Service.");
147 ServiceManager.addService(Context.SENSOR_SERVICE, new SensorService(context));
148
149 Log.i(TAG, "Starting Window Manager.");
150 wm = WindowManagerService.main(context, power,
151 factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
152 ServiceManager.addService(Context.WINDOW_SERVICE, wm);
153
154 ((ActivityManagerService)ServiceManager.getService("activity"))
155 .setWindowManager(wm);
156
157 // Skip Bluetooth if we have an emulator kernel
158 // TODO: Use a more reliable check to see if this product should
159 // support Bluetooth - see bug 988521
160 if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
161 Log.i(TAG, "Registering null Bluetooth Service (emulator)");
162 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
163 } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
164 Log.i(TAG, "Registering null Bluetooth Service (factory test)");
165 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
166 } else {
167 Log.i(TAG, "Starting Bluetooth Service.");
168 bluetooth = new BluetoothDeviceService(context);
169 bluetooth.init();
170 ServiceManager.addService(Context.BLUETOOTH_SERVICE, bluetooth);
171 bluetoothA2dp = new BluetoothA2dpService(context);
172 ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
173 bluetoothA2dp);
174
175 int bluetoothOn = Settings.Secure.getInt(mContentResolver,
176 Settings.Secure.BLUETOOTH_ON, 0);
177 if (bluetoothOn > 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700178 bluetooth.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 }
180 }
181
182 } catch (RuntimeException e) {
183 Log.e("System", "Failure starting core service", e);
184 }
185
186 StatusBarService statusBar = null;
187 InputMethodManagerService imm = null;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700188 AppWidgetService appWidget = null;
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -0700189 NotificationManagerService notification = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
191 if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
192 try {
193 Log.i(TAG, "Starting Status Bar Service.");
194 statusBar = new StatusBarService(context);
195 ServiceManager.addService("statusbar", statusBar);
196 } catch (Throwable e) {
197 Log.e(TAG, "Failure starting StatusBarService", e);
198 }
199
200 try {
201 Log.i(TAG, "Starting Clipboard Service.");
202 ServiceManager.addService("clipboard", new ClipboardService(context));
203 } catch (Throwable e) {
204 Log.e(TAG, "Failure starting Clipboard Service", e);
205 }
206
207 try {
208 Log.i(TAG, "Starting Input Method Service.");
209 imm = new InputMethodManagerService(context, statusBar);
210 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
211 } catch (Throwable e) {
212 Log.e(TAG, "Failure starting Input Manager Service", e);
213 }
214
215 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 Log.i(TAG, "Starting NetStat Service.");
217 ServiceManager.addService("netstat", new NetStatService(context));
218 } catch (Throwable e) {
219 Log.e(TAG, "Failure starting NetStat Service", e);
220 }
221
222 try {
223 Log.i(TAG, "Starting Connectivity Service.");
224 ServiceManager.addService(Context.CONNECTIVITY_SERVICE,
225 ConnectivityService.getInstance(context));
226 } catch (Throwable e) {
227 Log.e(TAG, "Failure starting Connectivity Service", e);
228 }
229
230 try {
Jean-Baptiste Querucf4550c2009-07-21 11:16:54 -0700231 Log.i(TAG, "Starting Accessibility Manager.");
232 ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
233 new AccessibilityManagerService(context));
234 } catch (Throwable e) {
235 Log.e(TAG, "Failure starting Accessibility Manager", e);
236 }
237
238 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 Log.i(TAG, "Starting Notification Manager.");
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -0700240 notification = new NotificationManagerService(context, statusBar, hardware);
241 ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 } catch (Throwable e) {
243 Log.e(TAG, "Failure starting Notification Manager", e);
244 }
245
246 try {
247 // MountService must start after NotificationManagerService
248 Log.i(TAG, "Starting Mount Service.");
249 ServiceManager.addService("mount", new MountService(context));
250 } catch (Throwable e) {
251 Log.e(TAG, "Failure starting Mount Service", e);
252 }
253
254 try {
255 Log.i(TAG, "Starting DeviceStorageMonitor service");
256 ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
257 new DeviceStorageMonitorService(context));
258 } catch (Throwable e) {
259 Log.e(TAG, "Failure starting DeviceStorageMonitor service", e);
260 }
261
262 try {
263 Log.i(TAG, "Starting Location Manager.");
264 ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
265 } catch (Throwable e) {
266 Log.e(TAG, "Failure starting Location Manager", e);
267 }
268
269 try {
270 Log.i(TAG, "Starting Search Service.");
271 ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) );
272 } catch (Throwable e) {
273 Log.e(TAG, "Failure starting Search Service", e);
274 }
275
276 if (INCLUDE_DEMO) {
277 Log.i(TAG, "Installing demo data...");
278 (new DemoThread(context)).start();
279 }
280
281 try {
282 Log.i(TAG, "Starting Checkin Service.");
283 Intent intent = new Intent().setComponent(new ComponentName(
284 "com.google.android.server.checkin",
285 "com.google.android.server.checkin.CheckinService"));
286 if (context.startService(intent) == null) {
287 Log.w(TAG, "Using fallback Checkin Service.");
288 ServiceManager.addService("checkin", new FallbackCheckinService(context));
289 }
290 } catch (Throwable e) {
291 Log.e(TAG, "Failure starting Checkin Service", e);
292 }
293
294 try {
295 Log.i(TAG, "Starting Wallpaper Service");
296 ServiceManager.addService(Context.WALLPAPER_SERVICE, new WallpaperService(context));
297 } catch (Throwable e) {
298 Log.e(TAG, "Failure starting Wallpaper Service", e);
299 }
300
301 try {
302 Log.i(TAG, "Starting Audio Service");
303 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
304 } catch (Throwable e) {
305 Log.e(TAG, "Failure starting Audio Service", e);
306 }
307
308 try {
309 Log.i(TAG, "Starting HeadsetObserver");
310 // Listen for wired headset changes
311 headset = new HeadsetObserver(context);
312 } catch (Throwable e) {
313 Log.e(TAG, "Failure starting HeadsetObserver", e);
314 }
315
316 try {
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -0700317 if (INCLUDE_BACKUP) {
318 Log.i(TAG, "Starting Backup Service");
319 ServiceManager.addService(Context.BACKUP_SERVICE, new BackupManagerService(context));
320 }
Jean-Baptiste Queru843ef362009-05-20 11:28:04 -0700321 } catch (Throwable e) {
322 Log.e(TAG, "Failure starting Backup Service", e);
323 }
324
325 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700326 Log.i(TAG, "Starting AppWidget Service");
327 appWidget = new AppWidgetService(context);
328 ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 } catch (Throwable e) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700330 Log.e(TAG, "Failure starting AppWidget Service", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 }
332
333 try {
334 com.android.server.status.StatusBarPolicy.installIcons(context, statusBar);
335 } catch (Throwable e) {
336 Log.e(TAG, "Failure installing status bar icons", e);
337 }
338 }
339
340 // make sure the ADB_ENABLED setting value matches the secure property value
341 Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
342 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
343
344 // register observer to listen for settings changes
345 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
346 false, new AdbSettingsObserver());
347
348 // It is now time to start up the app processes...
349 boolean safeMode = wm.detectSafeMode();
Jean-Baptiste Querua8675f62009-07-29 14:25:07 -0700350
351 if (notification != null) {
352 notification.systemReady();
353 }
354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 if (statusBar != null) {
356 statusBar.systemReady();
357 }
358 if (imm != null) {
359 imm.systemReady();
360 }
361 wm.systemReady();
362 power.systemReady();
363 try {
364 pm.systemReady();
365 } catch (RemoteException e) {
366 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700367 if (appWidget != null) {
368 appWidget.systemReady(safeMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 }
370
371 // After making the following code, third party code may be running...
372 try {
373 ActivityManagerNative.getDefault().systemReady();
374 } catch (RemoteException e) {
375 }
376
377 Watchdog.getInstance().start();
378
379 Looper.loop();
380 Log.d(TAG, "System ServerThread is exiting!");
381 }
382}
383
384class DemoThread extends Thread
385{
386 DemoThread(Context context)
387 {
388 mContext = context;
389 }
390
391 @Override
392 public void run()
393 {
394 try {
395 Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
396 boolean hasData = c != null && c.moveToFirst();
397 if (c != null) {
398 c.deactivate();
399 }
400 if (!hasData) {
401 DemoDataSet dataset = new DemoDataSet();
402 dataset.add(mContext);
403 }
404 } catch (Throwable e) {
405 Log.e("SystemServer", "Failure installing demo data", e);
406 }
407
408 }
409
410 Context mContext;
411}
412
413public class SystemServer
414{
415 private static final String TAG = "SystemServer";
416
417 public static final int FACTORY_TEST_OFF = 0;
418 public static final int FACTORY_TEST_LOW_LEVEL = 1;
419 public static final int FACTORY_TEST_HIGH_LEVEL = 2;
420
421 /**
422 * This method is called from Zygote to initialize the system. This will cause the native
423 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
424 * up into init2() to start the Android services.
425 */
426 native public static void init1(String[] args);
427
428 public static void main(String[] args) {
429 // The system server has to run all of the time, so it needs to be
430 // as efficient as possible with its memory usage.
431 VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
432
433 System.loadLibrary("android_servers");
434 init1(args);
435 }
436
437 public static final void init2() {
438 Log.i(TAG, "Entered the Android system server!");
439 Thread thr = new ServerThread();
440 thr.setName("android.server.ServerThread");
441 thr.start();
442 }
443}