blob: aa1a5cf9624956f7b1b00895d41eea311e1e0a59 [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;
35import android.os.IBinder;
36import android.os.Looper;
37import android.os.RemoteException;
38import android.os.ServiceManager;
39import android.os.SystemClock;
40import android.os.SystemProperties;
41import android.provider.Contacts.People;
42import android.provider.Settings;
43import android.server.BluetoothA2dpService;
44import android.server.BluetoothDeviceService;
45import android.server.search.SearchManagerService;
46import android.util.EventLog;
47import android.util.Log;
48
49import java.lang.reflect.Constructor;
50import java.lang.reflect.InvocationTargetException;
51
52class ServerThread extends Thread {
53 private static final String TAG = "SystemServer";
54 private final static boolean INCLUDE_DEMO = false;
Christopher Tate10c59a32009-07-16 15:15:25 -070055 private final static boolean INCLUDE_BACKUP = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
57 private static final int LOG_BOOT_PROGRESS_SYSTEM_RUN = 3010;
58
59 private ContentResolver mContentResolver;
60
61 private class AdbSettingsObserver extends ContentObserver {
62 public AdbSettingsObserver() {
63 super(null);
64 }
65 @Override
66 public void onChange(boolean selfChange) {
67 boolean enableAdb = (Settings.Secure.getInt(mContentResolver,
68 Settings.Secure.ADB_ENABLED, 0) > 0);
69 // setting this secure property will start or stop adbd
70 SystemProperties.set("persist.service.adb.enable", enableAdb ? "1" : "0");
71 }
72 }
73
74 @Override
75 public void run() {
76 EventLog.writeEvent(LOG_BOOT_PROGRESS_SYSTEM_RUN,
77 SystemClock.uptimeMillis());
78
79 ActivityManagerService.prepareTraceFile(false); // create dir
80
81 Looper.prepare();
82
83 android.os.Process.setThreadPriority(
84 android.os.Process.THREAD_PRIORITY_FOREGROUND);
85
86 String factoryTestStr = SystemProperties.get("ro.factorytest");
87 int factoryTest = "".equals(factoryTestStr) ? SystemServer.FACTORY_TEST_OFF
88 : Integer.parseInt(factoryTestStr);
89
The Android Open Source Project10592532009-03-18 17:39:46 -070090 HardwareService hardware = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 PowerManagerService power = null;
92 IPackageManager pm = null;
93 Context context = null;
94 WindowManagerService wm = null;
95 BluetoothDeviceService bluetooth = null;
96 BluetoothA2dpService bluetoothA2dp = null;
97 HeadsetObserver headset = null;
98
99 // Critical services...
100 try {
Nick Kralevich4fb25612009-06-17 16:03:22 -0700101 Log.i(TAG, "Starting Entropy Service.");
102 ServiceManager.addService("entropy", new EntropyService());
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 Log.i(TAG, "Starting Power Manager.");
105 power = new PowerManagerService();
106 ServiceManager.addService(Context.POWER_SERVICE, power);
107
108 Log.i(TAG, "Starting Activity Manager.");
109 context = ActivityManagerService.main(factoryTest);
110
111 Log.i(TAG, "Starting telephony registry");
112 ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));
113
114 AttributeCache.init(context);
115
116 Log.i(TAG, "Starting Package Manager.");
117 pm = PackageManagerService.main(context,
118 factoryTest != SystemServer.FACTORY_TEST_OFF);
119
120 ActivityManagerService.setSystemProcess();
121
122 mContentResolver = context.getContentResolver();
123
124 Log.i(TAG, "Starting Content Manager.");
125 ContentService.main(context,
126 factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
127
128 Log.i(TAG, "Starting System Content Providers.");
129 ActivityManagerService.installSystemProviders();
130
131 Log.i(TAG, "Starting Battery Service.");
132 BatteryService battery = new BatteryService(context);
133 ServiceManager.addService("battery", battery);
134
The Android Open Source Project10592532009-03-18 17:39:46 -0700135 Log.i(TAG, "Starting Hardware Service.");
136 hardware = new HardwareService(context);
137 ServiceManager.addService("hardware", hardware);
138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 // only initialize the power service after we have started the
The Android Open Source Project10592532009-03-18 17:39:46 -0700140 // hardware service, content providers and the battery service.
141 power.init(context, hardware, ActivityManagerService.getDefault(), battery);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
143 Log.i(TAG, "Starting Alarm Manager.");
144 AlarmManagerService alarm = new AlarmManagerService(context);
145 ServiceManager.addService(Context.ALARM_SERVICE, alarm);
146
147 Watchdog.getInstance().init(context, battery, power, alarm,
148 ActivityManagerService.self());
149
150 // Sensor Service is needed by Window Manager, so this goes first
151 Log.i(TAG, "Starting Sensor Service.");
152 ServiceManager.addService(Context.SENSOR_SERVICE, new SensorService(context));
153
154 Log.i(TAG, "Starting Window Manager.");
155 wm = WindowManagerService.main(context, power,
156 factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
157 ServiceManager.addService(Context.WINDOW_SERVICE, wm);
158
159 ((ActivityManagerService)ServiceManager.getService("activity"))
160 .setWindowManager(wm);
161
162 // Skip Bluetooth if we have an emulator kernel
163 // TODO: Use a more reliable check to see if this product should
164 // support Bluetooth - see bug 988521
165 if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
166 Log.i(TAG, "Registering null Bluetooth Service (emulator)");
167 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
168 } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
169 Log.i(TAG, "Registering null Bluetooth Service (factory test)");
170 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
171 } else {
172 Log.i(TAG, "Starting Bluetooth Service.");
173 bluetooth = new BluetoothDeviceService(context);
174 bluetooth.init();
175 ServiceManager.addService(Context.BLUETOOTH_SERVICE, bluetooth);
176 bluetoothA2dp = new BluetoothA2dpService(context);
177 ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
178 bluetoothA2dp);
179
180 int bluetoothOn = Settings.Secure.getInt(mContentResolver,
181 Settings.Secure.BLUETOOTH_ON, 0);
182 if (bluetoothOn > 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700183 bluetooth.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 }
185 }
186
187 } catch (RuntimeException e) {
188 Log.e("System", "Failure starting core service", e);
189 }
190
191 StatusBarService statusBar = null;
192 InputMethodManagerService imm = null;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700193 AppWidgetService appWidget = null;
Joe Onorato30275482009-07-08 17:09:14 -0700194 NotificationManagerService notification = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195
196 if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
197 try {
198 Log.i(TAG, "Starting Status Bar Service.");
199 statusBar = new StatusBarService(context);
200 ServiceManager.addService("statusbar", statusBar);
201 } catch (Throwable e) {
202 Log.e(TAG, "Failure starting StatusBarService", e);
203 }
204
205 try {
206 Log.i(TAG, "Starting Clipboard Service.");
207 ServiceManager.addService("clipboard", new ClipboardService(context));
208 } catch (Throwable e) {
209 Log.e(TAG, "Failure starting Clipboard Service", e);
210 }
211
212 try {
213 Log.i(TAG, "Starting Input Method Service.");
214 imm = new InputMethodManagerService(context, statusBar);
215 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
216 } catch (Throwable e) {
217 Log.e(TAG, "Failure starting Input Manager Service", e);
218 }
219
220 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 Log.i(TAG, "Starting NetStat Service.");
222 ServiceManager.addService("netstat", new NetStatService(context));
223 } catch (Throwable e) {
224 Log.e(TAG, "Failure starting NetStat Service", e);
225 }
226
227 try {
228 Log.i(TAG, "Starting Connectivity Service.");
229 ServiceManager.addService(Context.CONNECTIVITY_SERVICE,
230 ConnectivityService.getInstance(context));
231 } catch (Throwable e) {
232 Log.e(TAG, "Failure starting Connectivity Service", e);
233 }
234
235 try {
svetoslavganov75986cf2009-05-14 22:28:01 -0700236 Log.i(TAG, "Starting Accessibility Manager.");
237 ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
238 new AccessibilityManagerService(context));
239 } catch (Throwable e) {
240 Log.e(TAG, "Failure starting Accessibility Manager", e);
241 }
242
243 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 Log.i(TAG, "Starting Notification Manager.");
Joe Onorato30275482009-07-08 17:09:14 -0700245 notification = new NotificationManagerService(context, statusBar, hardware);
246 ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 } catch (Throwable e) {
248 Log.e(TAG, "Failure starting Notification Manager", e);
249 }
250
251 try {
252 // MountService must start after NotificationManagerService
253 Log.i(TAG, "Starting Mount Service.");
254 ServiceManager.addService("mount", new MountService(context));
255 } catch (Throwable e) {
256 Log.e(TAG, "Failure starting Mount Service", e);
257 }
258
259 try {
260 Log.i(TAG, "Starting DeviceStorageMonitor service");
261 ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
262 new DeviceStorageMonitorService(context));
263 } catch (Throwable e) {
264 Log.e(TAG, "Failure starting DeviceStorageMonitor service", e);
265 }
266
267 try {
268 Log.i(TAG, "Starting Location Manager.");
269 ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
270 } catch (Throwable e) {
271 Log.e(TAG, "Failure starting Location Manager", e);
272 }
273
274 try {
275 Log.i(TAG, "Starting Search Service.");
276 ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) );
277 } catch (Throwable e) {
278 Log.e(TAG, "Failure starting Search Service", e);
279 }
280
281 if (INCLUDE_DEMO) {
282 Log.i(TAG, "Installing demo data...");
283 (new DemoThread(context)).start();
284 }
285
286 try {
287 Log.i(TAG, "Starting Checkin Service.");
288 Intent intent = new Intent().setComponent(new ComponentName(
289 "com.google.android.server.checkin",
290 "com.google.android.server.checkin.CheckinService"));
291 if (context.startService(intent) == null) {
292 Log.w(TAG, "Using fallback Checkin Service.");
293 ServiceManager.addService("checkin", new FallbackCheckinService(context));
294 }
295 } catch (Throwable e) {
296 Log.e(TAG, "Failure starting Checkin Service", e);
297 }
298
299 try {
300 Log.i(TAG, "Starting Wallpaper Service");
301 ServiceManager.addService(Context.WALLPAPER_SERVICE, new WallpaperService(context));
302 } catch (Throwable e) {
303 Log.e(TAG, "Failure starting Wallpaper Service", e);
304 }
305
306 try {
307 Log.i(TAG, "Starting Audio Service");
308 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
309 } catch (Throwable e) {
310 Log.e(TAG, "Failure starting Audio Service", e);
311 }
312
313 try {
314 Log.i(TAG, "Starting HeadsetObserver");
315 // Listen for wired headset changes
316 headset = new HeadsetObserver(context);
317 } catch (Throwable e) {
318 Log.e(TAG, "Failure starting HeadsetObserver", e);
319 }
320
321 try {
Christopher Tate10c59a32009-07-16 15:15:25 -0700322 if (INCLUDE_BACKUP) {
323 Log.i(TAG, "Starting Backup Service");
324 ServiceManager.addService(Context.BACKUP_SERVICE, new BackupManagerService(context));
325 }
Christopher Tate487529a2009-04-29 14:03:25 -0700326 } catch (Throwable e) {
327 Log.e(TAG, "Failure starting Backup Service", e);
328 }
329
330 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700331 Log.i(TAG, "Starting AppWidget Service");
332 appWidget = new AppWidgetService(context);
333 ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 } catch (Throwable e) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700335 Log.e(TAG, "Failure starting AppWidget Service", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 }
337
338 try {
339 com.android.server.status.StatusBarPolicy.installIcons(context, statusBar);
340 } catch (Throwable e) {
341 Log.e(TAG, "Failure installing status bar icons", e);
342 }
343 }
344
345 // make sure the ADB_ENABLED setting value matches the secure property value
346 Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
347 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
348
349 // register observer to listen for settings changes
350 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
351 false, new AdbSettingsObserver());
352
353 // It is now time to start up the app processes...
354 boolean safeMode = wm.detectSafeMode();
Joe Onorato30275482009-07-08 17:09:14 -0700355
356 if (notification != null) {
357 notification.systemReady();
358 }
359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 if (statusBar != null) {
361 statusBar.systemReady();
362 }
363 if (imm != null) {
364 imm.systemReady();
365 }
366 wm.systemReady();
367 power.systemReady();
368 try {
369 pm.systemReady();
370 } catch (RemoteException e) {
371 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700372 if (appWidget != null) {
373 appWidget.systemReady(safeMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
375
376 // After making the following code, third party code may be running...
377 try {
378 ActivityManagerNative.getDefault().systemReady();
379 } catch (RemoteException e) {
380 }
381
382 Watchdog.getInstance().start();
383
384 Looper.loop();
385 Log.d(TAG, "System ServerThread is exiting!");
386 }
387}
388
389class DemoThread extends Thread
390{
391 DemoThread(Context context)
392 {
393 mContext = context;
394 }
395
396 @Override
397 public void run()
398 {
399 try {
400 Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
401 boolean hasData = c != null && c.moveToFirst();
402 if (c != null) {
403 c.deactivate();
404 }
405 if (!hasData) {
406 DemoDataSet dataset = new DemoDataSet();
407 dataset.add(mContext);
408 }
409 } catch (Throwable e) {
410 Log.e("SystemServer", "Failure installing demo data", e);
411 }
412
413 }
414
415 Context mContext;
416}
417
418public class SystemServer
419{
420 private static final String TAG = "SystemServer";
421
422 public static final int FACTORY_TEST_OFF = 0;
423 public static final int FACTORY_TEST_LOW_LEVEL = 1;
424 public static final int FACTORY_TEST_HIGH_LEVEL = 2;
425
426 /**
427 * This method is called from Zygote to initialize the system. This will cause the native
428 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
429 * up into init2() to start the Android services.
430 */
431 native public static void init1(String[] args);
432
433 public static void main(String[] args) {
434 // The system server has to run all of the time, so it needs to be
435 // as efficient as possible with its memory usage.
436 VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
437
438 System.loadLibrary("android_servers");
439 init1(args);
440 }
441
442 public static final void init2() {
443 Log.i(TAG, "Entered the Android system server!");
444 Thread thr = new ServerThread();
445 thr.setName("android.server.ServerThread");
446 thr.start();
447 }
448}