blob: b19e2eeb71ea66ef4dd19a23e5c273c719836b49 [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 {
96 Log.i(TAG, "Starting Power Manager.");
97 power = new PowerManagerService();
98 ServiceManager.addService(Context.POWER_SERVICE, power);
99
100 Log.i(TAG, "Starting Activity Manager.");
101 context = ActivityManagerService.main(factoryTest);
102
103 Log.i(TAG, "Starting telephony registry");
104 ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));
105
106 AttributeCache.init(context);
107
108 Log.i(TAG, "Starting Package Manager.");
109 pm = PackageManagerService.main(context,
110 factoryTest != SystemServer.FACTORY_TEST_OFF);
111
112 ActivityManagerService.setSystemProcess();
113
114 mContentResolver = context.getContentResolver();
115
Fred Quintana60307342009-03-24 22:48:12 -0700116 try {
117 Log.i(TAG, "Starting Account Manager.");
118 ServiceManager.addService(Context.ACCOUNT_SERVICE,
119 new AccountManagerService(context));
120 } catch (Throwable e) {
121 Log.e(TAG, "Failure starting Account Manager", e);
122 }
123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194
195 if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
196 try {
197 Log.i(TAG, "Starting Status Bar Service.");
198 statusBar = new StatusBarService(context);
199 ServiceManager.addService("statusbar", statusBar);
200 } catch (Throwable e) {
201 Log.e(TAG, "Failure starting StatusBarService", e);
202 }
203
204 try {
205 Log.i(TAG, "Starting Clipboard Service.");
206 ServiceManager.addService("clipboard", new ClipboardService(context));
207 } catch (Throwable e) {
208 Log.e(TAG, "Failure starting Clipboard Service", e);
209 }
210
211 try {
212 Log.i(TAG, "Starting Input Method Service.");
213 imm = new InputMethodManagerService(context, statusBar);
214 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
215 } catch (Throwable e) {
216 Log.e(TAG, "Failure starting Input Manager Service", e);
217 }
218
219 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 Log.i(TAG, "Starting NetStat Service.");
221 ServiceManager.addService("netstat", new NetStatService(context));
222 } catch (Throwable e) {
223 Log.e(TAG, "Failure starting NetStat Service", e);
224 }
225
226 try {
227 Log.i(TAG, "Starting Connectivity Service.");
228 ServiceManager.addService(Context.CONNECTIVITY_SERVICE,
229 ConnectivityService.getInstance(context));
230 } catch (Throwable e) {
231 Log.e(TAG, "Failure starting Connectivity Service", e);
232 }
233
234 try {
235 Log.i(TAG, "Starting Notification Manager.");
236 ServiceManager.addService(Context.NOTIFICATION_SERVICE,
The Android Open Source Project10592532009-03-18 17:39:46 -0700237 new NotificationManagerService(context, statusBar, hardware));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 } catch (Throwable e) {
239 Log.e(TAG, "Failure starting Notification Manager", e);
240 }
241
242 try {
243 // MountService must start after NotificationManagerService
244 Log.i(TAG, "Starting Mount Service.");
245 ServiceManager.addService("mount", new MountService(context));
246 } catch (Throwable e) {
247 Log.e(TAG, "Failure starting Mount Service", e);
248 }
249
250 try {
251 Log.i(TAG, "Starting DeviceStorageMonitor service");
252 ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
253 new DeviceStorageMonitorService(context));
254 } catch (Throwable e) {
255 Log.e(TAG, "Failure starting DeviceStorageMonitor service", e);
256 }
257
258 try {
259 Log.i(TAG, "Starting Location Manager.");
260 ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
261 } catch (Throwable e) {
262 Log.e(TAG, "Failure starting Location Manager", e);
263 }
264
265 try {
266 Log.i(TAG, "Starting Search Service.");
267 ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) );
268 } catch (Throwable e) {
269 Log.e(TAG, "Failure starting Search Service", e);
270 }
271
272 if (INCLUDE_DEMO) {
273 Log.i(TAG, "Installing demo data...");
274 (new DemoThread(context)).start();
275 }
276
277 try {
278 Log.i(TAG, "Starting Checkin Service.");
279 Intent intent = new Intent().setComponent(new ComponentName(
280 "com.google.android.server.checkin",
281 "com.google.android.server.checkin.CheckinService"));
282 if (context.startService(intent) == null) {
283 Log.w(TAG, "Using fallback Checkin Service.");
284 ServiceManager.addService("checkin", new FallbackCheckinService(context));
285 }
286 } catch (Throwable e) {
287 Log.e(TAG, "Failure starting Checkin Service", e);
288 }
289
290 try {
291 Log.i(TAG, "Starting Wallpaper Service");
292 ServiceManager.addService(Context.WALLPAPER_SERVICE, new WallpaperService(context));
293 } catch (Throwable e) {
294 Log.e(TAG, "Failure starting Wallpaper Service", e);
295 }
296
297 try {
298 Log.i(TAG, "Starting Audio Service");
299 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
300 } catch (Throwable e) {
301 Log.e(TAG, "Failure starting Audio Service", e);
302 }
303
304 try {
305 Log.i(TAG, "Starting HeadsetObserver");
306 // Listen for wired headset changes
307 headset = new HeadsetObserver(context);
308 } catch (Throwable e) {
309 Log.e(TAG, "Failure starting HeadsetObserver", e);
310 }
311
312 try {
Christopher Tate487529a2009-04-29 14:03:25 -0700313 Log.i(TAG, "Starting Backup Service");
314 ServiceManager.addService(Context.BACKUP_SERVICE, new BackupManagerService(context));
315 } catch (Throwable e) {
316 Log.e(TAG, "Failure starting Backup Service", e);
317 }
318
319 try {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700320 Log.i(TAG, "Starting AppWidget Service");
321 appWidget = new AppWidgetService(context);
322 ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 } catch (Throwable e) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700324 Log.e(TAG, "Failure starting AppWidget Service", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 }
326
327 try {
328 com.android.server.status.StatusBarPolicy.installIcons(context, statusBar);
329 } catch (Throwable e) {
330 Log.e(TAG, "Failure installing status bar icons", e);
331 }
332 }
333
334 // make sure the ADB_ENABLED setting value matches the secure property value
335 Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
336 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
337
338 // register observer to listen for settings changes
339 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
340 false, new AdbSettingsObserver());
341
342 // It is now time to start up the app processes...
343 boolean safeMode = wm.detectSafeMode();
344 if (statusBar != null) {
345 statusBar.systemReady();
346 }
347 if (imm != null) {
348 imm.systemReady();
349 }
350 wm.systemReady();
351 power.systemReady();
352 try {
353 pm.systemReady();
354 } catch (RemoteException e) {
355 }
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700356 if (appWidget != null) {
357 appWidget.systemReady(safeMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
359
360 // After making the following code, third party code may be running...
361 try {
362 ActivityManagerNative.getDefault().systemReady();
363 } catch (RemoteException e) {
364 }
365
366 Watchdog.getInstance().start();
367
368 Looper.loop();
369 Log.d(TAG, "System ServerThread is exiting!");
370 }
371}
372
373class DemoThread extends Thread
374{
375 DemoThread(Context context)
376 {
377 mContext = context;
378 }
379
380 @Override
381 public void run()
382 {
383 try {
384 Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
385 boolean hasData = c != null && c.moveToFirst();
386 if (c != null) {
387 c.deactivate();
388 }
389 if (!hasData) {
390 DemoDataSet dataset = new DemoDataSet();
391 dataset.add(mContext);
392 }
393 } catch (Throwable e) {
394 Log.e("SystemServer", "Failure installing demo data", e);
395 }
396
397 }
398
399 Context mContext;
400}
401
402public class SystemServer
403{
404 private static final String TAG = "SystemServer";
405
406 public static final int FACTORY_TEST_OFF = 0;
407 public static final int FACTORY_TEST_LOW_LEVEL = 1;
408 public static final int FACTORY_TEST_HIGH_LEVEL = 2;
409
410 /**
411 * This method is called from Zygote to initialize the system. This will cause the native
412 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
413 * up into init2() to start the Android services.
414 */
415 native public static void init1(String[] args);
416
417 public static void main(String[] args) {
418 // The system server has to run all of the time, so it needs to be
419 // as efficient as possible with its memory usage.
420 VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
421
422 System.loadLibrary("android_servers");
423 init1(args);
424 }
425
426 public static final void init2() {
427 Log.i(TAG, "Entered the Android system server!");
428 Thread thr = new ServerThread();
429 thr.setName("android.server.ServerThread");
430 thr.start();
431 }
432}