blob: 77383bdaa0cd2a5eaad0112eb53036f5356e0775 [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
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
The Android Open Source Projectd24b8182009-02-10 15:44:00 -080019import com.android.server.am.ActivityManagerService;
20import com.android.server.status.StatusBarService;
21
22import dalvik.system.PathClassLoader;
23import dalvik.system.VMRuntime;
24
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070025import android.app.ActivityManagerNative;
The Android Open Source Projectd24b8182009-02-10 15:44:00 -080026import android.content.ComponentName;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070027import android.content.ContentResolver;
28import android.content.ContentService;
29import android.content.Context;
The Android Open Source Projectd24b8182009-02-10 15:44:00 -080030import android.content.Intent;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070031import android.content.pm.IPackageManager;
32import android.database.ContentObserver;
33import android.database.Cursor;
34import android.media.AudioService;
The Android Open Source Projectd24b8182009-02-10 15:44:00 -080035import android.os.IBinder;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070036import android.os.Looper;
37import android.os.RemoteException;
38import android.os.ServiceManager;
39import android.os.SystemClock;
40import android.os.SystemProperties;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070041import android.provider.Contacts.People;
The Android Open Source Projectd24b8182009-02-10 15:44:00 -080042import android.provider.Settings;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080043import android.server.BluetoothA2dpService;
The Android Open Source Projectd24b8182009-02-10 15:44:00 -080044import android.server.BluetoothDeviceService;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070045import android.server.search.SearchManagerService;
46import android.util.EventLog;
47import android.util.Log;
48
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070049import java.lang.reflect.Constructor;
50import java.lang.reflect.InvocationTargetException;
51
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070052class ServerThread extends Thread {
53 private static final String TAG = "SystemServer";
54 private final static boolean INCLUDE_DEMO = false;
55
56 private static final int LOG_BOOT_PROGRESS_SYSTEM_RUN = 3010;
57
58 private ContentResolver mContentResolver;
59
60 private class AdbSettingsObserver extends ContentObserver {
61 public AdbSettingsObserver() {
62 super(null);
63 }
64 @Override
65 public void onChange(boolean selfChange) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080066 boolean enableAdb = (Settings.Secure.getInt(mContentResolver,
67 Settings.Secure.ADB_ENABLED, 0) > 0);
68 // setting this secure property will start or stop adbd
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070069 SystemProperties.set("persist.service.adb.enable", enableAdb ? "1" : "0");
70 }
71 }
72
73 @Override
74 public void run() {
75 EventLog.writeEvent(LOG_BOOT_PROGRESS_SYSTEM_RUN,
76 SystemClock.uptimeMillis());
77
78 ActivityManagerService.prepareTraceFile(false); // create dir
79
80 Looper.prepare();
81
82 android.os.Process.setThreadPriority(
83 android.os.Process.THREAD_PRIORITY_FOREGROUND);
84
85 String factoryTestStr = SystemProperties.get("ro.factorytest");
86 int factoryTest = "".equals(factoryTestStr) ? SystemServer.FACTORY_TEST_OFF
87 : Integer.parseInt(factoryTestStr);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080088
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070089 PowerManagerService power = null;
90 IPackageManager pm = null;
91 Context context = null;
92 WindowManagerService wm = null;
93 BluetoothDeviceService bluetooth = null;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080094 BluetoothA2dpService bluetoothA2dp = null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070095 HeadsetObserver headset = null;
96
97 // Critical services...
98 try {
99 Log.i(TAG, "Starting Power Manager.");
100 power = new PowerManagerService();
101 ServiceManager.addService(Context.POWER_SERVICE, power);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800102
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700103 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);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800110
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700111 Log.i(TAG, "Starting Package Manager.");
112 pm = PackageManagerService.main(context,
113 factoryTest != SystemServer.FACTORY_TEST_OFF);
114
115 ActivityManagerService.setSystemProcess();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800116
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700117 mContentResolver = context.getContentResolver();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800118
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700119 Log.i(TAG, "Starting Content Manager.");
120 ContentService.main(context,
121 factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800122
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700123 Log.i(TAG, "Starting System Content Providers.");
124 ActivityManagerService.installSystemProviders();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800125
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700126 Log.i(TAG, "Starting Battery Service.");
127 BatteryService battery = new BatteryService(context);
128 ServiceManager.addService("battery", battery);
129
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800130 // only initialize the power service after we have started the
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700131 // content providers and the batter service.
132 power.init(context, ActivityManagerService.getDefault(), battery);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800133
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700134 Log.i(TAG, "Starting Alarm Manager.");
135 AlarmManagerService alarm = new AlarmManagerService(context);
136 ServiceManager.addService(Context.ALARM_SERVICE, alarm);
137
138 Watchdog.getInstance().init(context, battery, power, alarm,
139 ActivityManagerService.self());
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800140
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700141 // Sensor Service is needed by Window Manager, so this goes first
142 Log.i(TAG, "Starting Sensor Service.");
143 ServiceManager.addService(Context.SENSOR_SERVICE, new SensorService(context));
144
145 Log.i(TAG, "Starting Window Manager.");
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800146 wm = WindowManagerService.main(context, power,
147 factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700148 ServiceManager.addService(Context.WINDOW_SERVICE, wm);
149
150 ((ActivityManagerService)ServiceManager.getService("activity"))
151 .setWindowManager(wm);
152
153 // Skip Bluetooth if we have an emulator kernel
154 // TODO: Use a more reliable check to see if this product should
155 // support Bluetooth - see bug 988521
156 if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
157 Log.i(TAG, "Registering null Bluetooth Service (emulator)");
158 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
159 } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
160 Log.i(TAG, "Registering null Bluetooth Service (factory test)");
161 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
162 } else {
163 Log.i(TAG, "Starting Bluetooth Service.");
164 bluetooth = new BluetoothDeviceService(context);
165 bluetooth.init();
166 ServiceManager.addService(Context.BLUETOOTH_SERVICE, bluetooth);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800167 bluetoothA2dp = new BluetoothA2dpService(context);
168 ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
169 bluetoothA2dp);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700170
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800171 int bluetoothOn = Settings.Secure.getInt(mContentResolver,
172 Settings.Secure.BLUETOOTH_ON, 0);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700173 if (bluetoothOn > 0) {
174 bluetooth.enable(null);
175 }
176 }
177
178 } catch (RuntimeException e) {
179 Log.e("System", "Failure starting core service", e);
180 }
181
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800182 StatusBarService statusBar = null;
183 InputMethodManagerService imm = null;
The Android Open Source Project3001a032009-02-19 10:57:31 -0800184 GadgetService gadget = null;
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800185
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700186 if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700187 try {
188 Log.i(TAG, "Starting Status Bar Service.");
189 statusBar = new StatusBarService(context);
190 ServiceManager.addService("statusbar", statusBar);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700191 } catch (Throwable e) {
192 Log.e(TAG, "Failure starting StatusBarService", e);
193 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800194
195 try {
196 Log.i(TAG, "Starting Clipboard Service.");
197 ServiceManager.addService("clipboard", new ClipboardService(context));
198 } catch (Throwable e) {
199 Log.e(TAG, "Failure starting Clipboard Service", e);
200 }
201
202 try {
203 Log.i(TAG, "Starting Input Method Service.");
204 imm = new InputMethodManagerService(context, statusBar);
205 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
206 } catch (Throwable e) {
207 Log.e(TAG, "Failure starting Input Manager Service", e);
208 }
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800209
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700210 try {
211 Log.i(TAG, "Starting Hardware Service.");
212 ServiceManager.addService("hardware", new HardwareService(context));
213 } catch (Throwable e) {
214 Log.e(TAG, "Failure starting Hardware Service", e);
215 }
216
217 try {
218 Log.i(TAG, "Starting NetStat Service.");
219 ServiceManager.addService("netstat", new NetStatService(context));
220 } catch (Throwable e) {
221 Log.e(TAG, "Failure starting NetStat Service", e);
222 }
223
224 try {
225 Log.i(TAG, "Starting Connectivity Service.");
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800226 ServiceManager.addService(Context.CONNECTIVITY_SERVICE,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700227 ConnectivityService.getInstance(context));
228 } catch (Throwable e) {
229 Log.e(TAG, "Failure starting Connectivity Service", e);
230 }
231
232 try {
233 Log.i(TAG, "Starting Notification Manager.");
234 ServiceManager.addService(Context.NOTIFICATION_SERVICE,
235 new NotificationManagerService(context, statusBar));
236 } catch (Throwable e) {
237 Log.e(TAG, "Failure starting Notification Manager", e);
238 }
239
240 try {
241 // MountService must start after NotificationManagerService
242 Log.i(TAG, "Starting Mount Service.");
243 ServiceManager.addService("mount", new MountService(context));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700244 } catch (Throwable e) {
245 Log.e(TAG, "Failure starting Mount Service", e);
246 }
247
248 try {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800249 Log.i(TAG, "Starting DeviceStorageMonitor service");
250 ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
251 new DeviceStorageMonitorService(context));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700252 } catch (Throwable e) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800253 Log.e(TAG, "Failure starting DeviceStorageMonitor service", e);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700254 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800255
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700256 try {
257 Log.i(TAG, "Starting Location Manager.");
258 ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
259 } catch (Throwable e) {
260 Log.e(TAG, "Failure starting Location Manager", e);
261 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800262
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700263 try {
264 Log.i(TAG, "Starting Search Service.");
265 ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) );
266 } catch (Throwable e) {
267 Log.e(TAG, "Failure starting Search Service", e);
268 }
269
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700270 if (INCLUDE_DEMO) {
271 Log.i(TAG, "Installing demo data...");
272 (new DemoThread(context)).start();
273 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800274
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700275 try {
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800276 Log.i(TAG, "Starting Checkin Service.");
277 Intent intent = new Intent().setComponent(new ComponentName(
278 "com.google.android.server.checkin",
279 "com.google.android.server.checkin.CheckinService"));
280 if (context.startService(intent) == null) {
281 Log.w(TAG, "Using fallback Checkin Service.");
282 ServiceManager.addService("checkin", new FallbackCheckinService(context));
283 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700284 } catch (Throwable e) {
285 Log.e(TAG, "Failure starting Checkin Service", e);
286 }
287
288 try {
289 Log.i(TAG, "Starting Wallpaper Service");
290 ServiceManager.addService(Context.WALLPAPER_SERVICE, new WallpaperService(context));
291 } catch (Throwable e) {
292 Log.e(TAG, "Failure starting Wallpaper Service", e);
293 }
294
295 try {
296 Log.i(TAG, "Starting Audio Service");
297 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
298 } catch (Throwable e) {
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -0800299 Log.e(TAG, "Failure starting Audio Service", e);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700300 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800301
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700302 try {
303 Log.i(TAG, "Starting HeadsetObserver");
304 // Listen for wired headset changes
305 headset = new HeadsetObserver(context);
306 } catch (Throwable e) {
307 Log.e(TAG, "Failure starting HeadsetObserver", e);
308 }
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -0800309
310 try {
311 Log.i(TAG, "Starting Gadget Service");
The Android Open Source Project3001a032009-02-19 10:57:31 -0800312 gadget = new GadgetService(context);
313 ServiceManager.addService(Context.GADGET_SERVICE, gadget);
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -0800314 } catch (Throwable e) {
315 Log.e(TAG, "Failure starting Gadget Service", e);
316 }
The Android Open Source Project3dec7d52009-03-02 22:54:33 -0800317
318 try {
319 com.android.server.status.StatusBarPolicy.installIcons(context, statusBar);
320 } catch (Throwable e) {
321 Log.e(TAG, "Failure installing status bar icons", e);
322 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700323 }
324
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800325 // make sure the ADB_ENABLED setting value matches the secure property value
326 Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700327 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
328
329 // register observer to listen for settings changes
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800330 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700331 false, new AdbSettingsObserver());
332
333 // It is now time to start up the app processes...
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800334 boolean safeMode = wm.detectSafeMode();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800335 if (statusBar != null) {
336 statusBar.systemReady();
337 }
338 if (imm != null) {
339 imm.systemReady();
340 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700341 wm.systemReady();
342 power.systemReady();
343 try {
344 pm.systemReady();
345 } catch (RemoteException e) {
346 }
The Android Open Source Project3001a032009-02-19 10:57:31 -0800347 if (gadget != null) {
348 gadget.systemReady(safeMode);
349 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800350
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700351 // After making the following code, third party code may be running...
352 try {
353 ActivityManagerNative.getDefault().systemReady();
354 } catch (RemoteException e) {
355 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800356
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700357 Watchdog.getInstance().start();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800358
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700359 Looper.loop();
360 Log.d(TAG, "System ServerThread is exiting!");
361 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700362}
363
364class DemoThread extends Thread
365{
366 DemoThread(Context context)
367 {
368 mContext = context;
369 }
370
371 @Override
372 public void run()
373 {
374 try {
375 Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
376 boolean hasData = c != null && c.moveToFirst();
377 if (c != null) {
378 c.deactivate();
379 }
380 if (!hasData) {
381 DemoDataSet dataset = new DemoDataSet();
382 dataset.add(mContext);
383 }
384 } catch (Throwable e) {
385 Log.e("SystemServer", "Failure installing demo data", e);
386 }
387
388 }
389
390 Context mContext;
391}
392
393public class SystemServer
394{
395 private static final String TAG = "SystemServer";
396
397 public static final int FACTORY_TEST_OFF = 0;
398 public static final int FACTORY_TEST_LOW_LEVEL = 1;
399 public static final int FACTORY_TEST_HIGH_LEVEL = 2;
400
401 /**
402 * This method is called from Zygote to initialize the system. This will cause the native
403 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
404 * up into init2() to start the Android services.
405 */
406 native public static void init1(String[] args);
407
408 public static void main(String[] args) {
409 // The system server has to run all of the time, so it needs to be
410 // as efficient as possible with its memory usage.
411 VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
412
413 System.loadLibrary("android_servers");
414 init1(args);
415 }
416
417 public static final void init2() {
418 Log.i(TAG, "Entered the Android system server!");
419 Thread thr = new ServerThread();
420 thr.setName("android.server.ServerThread");
421 thr.start();
422 }
423}