blob: c26ba5cca53ee426cf1f5ff7972db15d9fdf37ec [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;
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 {
Nick Kralevich4fb25612009-06-17 16:03:22 -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
Fred Quintana60307342009-03-24 22:48:12 -0700119 try {
120 Log.i(TAG, "Starting Account Manager.");
121 ServiceManager.addService(Context.ACCOUNT_SERVICE,
122 new AccountManagerService(context));
123 } catch (Throwable e) {
124 Log.e(TAG, "Failure starting Account Manager", e);
125 }
126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 Log.i(TAG, "Starting Content Manager.");
128 ContentService.main(context,
129 factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
130
131 Log.i(TAG, "Starting System Content Providers.");
132 ActivityManagerService.installSystemProviders();
133
134 Log.i(TAG, "Starting Battery Service.");
135 BatteryService battery = new BatteryService(context);
136 ServiceManager.addService("battery", battery);
137
The Android Open Source Project10592532009-03-18 17:39:46 -0700138 Log.i(TAG, "Starting Hardware Service.");
139 hardware = new HardwareService(context);
140 ServiceManager.addService("hardware", hardware);
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 // only initialize the power service after we have started the
The Android Open Source Project10592532009-03-18 17:39:46 -0700143 // hardware service, content providers and the battery service.
144 power.init(context, hardware, ActivityManagerService.getDefault(), battery);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
146 Log.i(TAG, "Starting Alarm Manager.");
147 AlarmManagerService alarm = new AlarmManagerService(context);
148 ServiceManager.addService(Context.ALARM_SERVICE, alarm);
149
150 Watchdog.getInstance().init(context, battery, power, alarm,
151 ActivityManagerService.self());
152
153 // Sensor Service is needed by Window Manager, so this goes first
154 Log.i(TAG, "Starting Sensor Service.");
155 ServiceManager.addService(Context.SENSOR_SERVICE, new SensorService(context));
156
157 Log.i(TAG, "Starting Window Manager.");
158 wm = WindowManagerService.main(context, power,
159 factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
160 ServiceManager.addService(Context.WINDOW_SERVICE, wm);
161
162 ((ActivityManagerService)ServiceManager.getService("activity"))
163 .setWindowManager(wm);
164
165 // Skip Bluetooth if we have an emulator kernel
166 // TODO: Use a more reliable check to see if this product should
167 // support Bluetooth - see bug 988521
168 if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
169 Log.i(TAG, "Registering null Bluetooth Service (emulator)");
170 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
171 } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
172 Log.i(TAG, "Registering null Bluetooth Service (factory test)");
173 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
174 } else {
175 Log.i(TAG, "Starting Bluetooth Service.");
176 bluetooth = new BluetoothDeviceService(context);
177 bluetooth.init();
178 ServiceManager.addService(Context.BLUETOOTH_SERVICE, bluetooth);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700179 bluetoothA2dp = new BluetoothA2dpService(context, bluetooth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
181 bluetoothA2dp);
182
183 int bluetoothOn = Settings.Secure.getInt(mContentResolver,
184 Settings.Secure.BLUETOOTH_ON, 0);
185 if (bluetoothOn > 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700186 bluetooth.enable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 }
188 }
189
190 } catch (RuntimeException e) {
191 Log.e("System", "Failure starting core service", e);
192 }
193
194 StatusBarService statusBar = null;
195 InputMethodManagerService imm = null;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700196 AppWidgetService appWidget = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
198 if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
199 try {
200 Log.i(TAG, "Starting Status Bar Service.");
201 statusBar = new StatusBarService(context);
202 ServiceManager.addService("statusbar", statusBar);
203 } catch (Throwable e) {
204 Log.e(TAG, "Failure starting StatusBarService", e);
205 }
206
207 try {
208 Log.i(TAG, "Starting Clipboard Service.");
209 ServiceManager.addService("clipboard", new ClipboardService(context));
210 } catch (Throwable e) {
211 Log.e(TAG, "Failure starting Clipboard Service", e);
212 }
213
214 try {
215 Log.i(TAG, "Starting Input Method Service.");
216 imm = new InputMethodManagerService(context, statusBar);
217 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
218 } catch (Throwable e) {
219 Log.e(TAG, "Failure starting Input Manager Service", e);
220 }
221
222 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 Log.i(TAG, "Starting NetStat Service.");
224 ServiceManager.addService("netstat", new NetStatService(context));
225 } catch (Throwable e) {
226 Log.e(TAG, "Failure starting NetStat Service", e);
227 }
228
229 try {
230 Log.i(TAG, "Starting Connectivity Service.");
231 ServiceManager.addService(Context.CONNECTIVITY_SERVICE,
232 ConnectivityService.getInstance(context));
233 } catch (Throwable e) {
234 Log.e(TAG, "Failure starting Connectivity Service", e);
235 }
236
237 try {
svetoslavganov75986cf2009-05-14 22:28:01 -0700238 Log.i(TAG, "Starting Accessibility Manager.");
239 ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
240 new AccessibilityManagerService(context));
241 } catch (Throwable e) {
242 Log.e(TAG, "Failure starting Accessibility Manager", e);
243 }
244
245 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 Log.i(TAG, "Starting Notification Manager.");
247 ServiceManager.addService(Context.NOTIFICATION_SERVICE,
The Android Open Source Project10592532009-03-18 17:39:46 -0700248 new NotificationManagerService(context, statusBar, hardware));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 } catch (Throwable e) {
250 Log.e(TAG, "Failure starting Notification Manager", e);
251 }
252
253 try {
254 // MountService must start after NotificationManagerService
255 Log.i(TAG, "Starting Mount Service.");
256 ServiceManager.addService("mount", new MountService(context));
257 } catch (Throwable e) {
258 Log.e(TAG, "Failure starting Mount Service", e);
259 }
260
261 try {
262 Log.i(TAG, "Starting DeviceStorageMonitor service");
263 ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
264 new DeviceStorageMonitorService(context));
265 } catch (Throwable e) {
266 Log.e(TAG, "Failure starting DeviceStorageMonitor service", e);
267 }
268
269 try {
270 Log.i(TAG, "Starting Location Manager.");
271 ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
272 } catch (Throwable e) {
273 Log.e(TAG, "Failure starting Location Manager", e);
274 }
275
276 try {
277 Log.i(TAG, "Starting Search Service.");
278 ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) );
279 } catch (Throwable e) {
280 Log.e(TAG, "Failure starting Search Service", e);
281 }
282
283 if (INCLUDE_DEMO) {
284 Log.i(TAG, "Installing demo data...");
285 (new DemoThread(context)).start();
286 }
287
288 try {
289 Log.i(TAG, "Starting Checkin Service.");
290 Intent intent = new Intent().setComponent(new ComponentName(
291 "com.google.android.server.checkin",
292 "com.google.android.server.checkin.CheckinService"));
293 if (context.startService(intent) == null) {
294 Log.w(TAG, "Using fallback Checkin Service.");
295 ServiceManager.addService("checkin", new FallbackCheckinService(context));
296 }
297 } catch (Throwable e) {
298 Log.e(TAG, "Failure starting Checkin Service", e);
299 }
300
301 try {
302 Log.i(TAG, "Starting Wallpaper Service");
303 ServiceManager.addService(Context.WALLPAPER_SERVICE, new WallpaperService(context));
304 } catch (Throwable e) {
305 Log.e(TAG, "Failure starting Wallpaper Service", e);
306 }
307
308 try {
309 Log.i(TAG, "Starting Audio Service");
310 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
311 } catch (Throwable e) {
312 Log.e(TAG, "Failure starting Audio Service", e);
313 }
314
315 try {
316 Log.i(TAG, "Starting HeadsetObserver");
317 // Listen for wired headset changes
318 headset = new HeadsetObserver(context);
319 } catch (Throwable e) {
320 Log.e(TAG, "Failure starting HeadsetObserver", e);
321 }
322
323 try {
Christopher Tate487529a2009-04-29 14:03:25 -0700324 Log.i(TAG, "Starting Backup Service");
325 ServiceManager.addService(Context.BACKUP_SERVICE, new BackupManagerService(context));
326 } 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();
355 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;
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700420
421 /**
422 * 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 -0800423 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
424 * up into init2() to start the Android services.
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700425 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 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);
Jaikumar Ganeshd5ac1ae2009-05-05 22:26:12 -0700432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 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}