blob: fc25e388b0a5f2529ef35af8c396082a2d3e826e [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);
191 com.android.server.status.StatusBarPolicy.installIcons(context, statusBar);
192 } catch (Throwable e) {
193 Log.e(TAG, "Failure starting StatusBarService", e);
194 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800195
196 try {
197 Log.i(TAG, "Starting Clipboard Service.");
198 ServiceManager.addService("clipboard", new ClipboardService(context));
199 } catch (Throwable e) {
200 Log.e(TAG, "Failure starting Clipboard Service", e);
201 }
202
203 try {
204 Log.i(TAG, "Starting Input Method Service.");
205 imm = new InputMethodManagerService(context, statusBar);
206 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
207 } catch (Throwable e) {
208 Log.e(TAG, "Failure starting Input Manager Service", e);
209 }
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800210
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700211 try {
212 Log.i(TAG, "Starting Hardware Service.");
213 ServiceManager.addService("hardware", new HardwareService(context));
214 } catch (Throwable e) {
215 Log.e(TAG, "Failure starting Hardware Service", e);
216 }
217
218 try {
219 Log.i(TAG, "Starting NetStat Service.");
220 ServiceManager.addService("netstat", new NetStatService(context));
221 } catch (Throwable e) {
222 Log.e(TAG, "Failure starting NetStat Service", e);
223 }
224
225 try {
226 Log.i(TAG, "Starting Connectivity Service.");
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800227 ServiceManager.addService(Context.CONNECTIVITY_SERVICE,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700228 ConnectivityService.getInstance(context));
229 } catch (Throwable e) {
230 Log.e(TAG, "Failure starting Connectivity Service", e);
231 }
232
233 try {
234 Log.i(TAG, "Starting Notification Manager.");
235 ServiceManager.addService(Context.NOTIFICATION_SERVICE,
236 new NotificationManagerService(context, statusBar));
237 } catch (Throwable e) {
238 Log.e(TAG, "Failure starting Notification Manager", e);
239 }
240
241 try {
242 // MountService must start after NotificationManagerService
243 Log.i(TAG, "Starting Mount Service.");
244 ServiceManager.addService("mount", new MountService(context));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700245 } catch (Throwable e) {
246 Log.e(TAG, "Failure starting Mount Service", e);
247 }
248
249 try {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800250 Log.i(TAG, "Starting DeviceStorageMonitor service");
251 ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
252 new DeviceStorageMonitorService(context));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700253 } catch (Throwable e) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800254 Log.e(TAG, "Failure starting DeviceStorageMonitor service", e);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700255 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800256
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700257 try {
258 Log.i(TAG, "Starting Location Manager.");
259 ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
260 } catch (Throwable e) {
261 Log.e(TAG, "Failure starting Location Manager", e);
262 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800263
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700264 try {
265 Log.i(TAG, "Starting Search Service.");
266 ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) );
267 } catch (Throwable e) {
268 Log.e(TAG, "Failure starting Search Service", e);
269 }
270
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700271 if (INCLUDE_DEMO) {
272 Log.i(TAG, "Installing demo data...");
273 (new DemoThread(context)).start();
274 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800275
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700276 try {
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800277 Log.i(TAG, "Starting Checkin Service.");
278 Intent intent = new Intent().setComponent(new ComponentName(
279 "com.google.android.server.checkin",
280 "com.google.android.server.checkin.CheckinService"));
281 if (context.startService(intent) == null) {
282 Log.w(TAG, "Using fallback Checkin Service.");
283 ServiceManager.addService("checkin", new FallbackCheckinService(context));
284 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700285 } catch (Throwable e) {
286 Log.e(TAG, "Failure starting Checkin Service", e);
287 }
288
289 try {
290 Log.i(TAG, "Starting Wallpaper Service");
291 ServiceManager.addService(Context.WALLPAPER_SERVICE, new WallpaperService(context));
292 } catch (Throwable e) {
293 Log.e(TAG, "Failure starting Wallpaper Service", e);
294 }
295
296 try {
297 Log.i(TAG, "Starting Audio Service");
298 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
299 } catch (Throwable e) {
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -0800300 Log.e(TAG, "Failure starting Audio Service", e);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700301 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800302
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700303 try {
304 Log.i(TAG, "Starting HeadsetObserver");
305 // Listen for wired headset changes
306 headset = new HeadsetObserver(context);
307 } catch (Throwable e) {
308 Log.e(TAG, "Failure starting HeadsetObserver", e);
309 }
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -0800310
311 try {
312 Log.i(TAG, "Starting Gadget Service");
The Android Open Source Project3001a032009-02-19 10:57:31 -0800313 gadget = new GadgetService(context);
314 ServiceManager.addService(Context.GADGET_SERVICE, gadget);
The Android Open Source Project22f7dfd2009-01-20 14:03:58 -0800315 } catch (Throwable e) {
316 Log.e(TAG, "Failure starting Gadget Service", e);
317 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700318 }
319
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800320 // make sure the ADB_ENABLED setting value matches the secure property value
321 Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700322 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
323
324 // register observer to listen for settings changes
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800325 mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700326 false, new AdbSettingsObserver());
327
328 // It is now time to start up the app processes...
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800329 boolean safeMode = wm.detectSafeMode();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800330 if (statusBar != null) {
331 statusBar.systemReady();
332 }
333 if (imm != null) {
334 imm.systemReady();
335 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700336 wm.systemReady();
337 power.systemReady();
338 try {
339 pm.systemReady();
340 } catch (RemoteException e) {
341 }
The Android Open Source Project3001a032009-02-19 10:57:31 -0800342 if (gadget != null) {
343 gadget.systemReady(safeMode);
344 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800345
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700346 // After making the following code, third party code may be running...
347 try {
348 ActivityManagerNative.getDefault().systemReady();
349 } catch (RemoteException e) {
350 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800351
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700352 Watchdog.getInstance().start();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800353
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700354 Looper.loop();
355 Log.d(TAG, "System ServerThread is exiting!");
356 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700357}
358
359class DemoThread extends Thread
360{
361 DemoThread(Context context)
362 {
363 mContext = context;
364 }
365
366 @Override
367 public void run()
368 {
369 try {
370 Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
371 boolean hasData = c != null && c.moveToFirst();
372 if (c != null) {
373 c.deactivate();
374 }
375 if (!hasData) {
376 DemoDataSet dataset = new DemoDataSet();
377 dataset.add(mContext);
378 }
379 } catch (Throwable e) {
380 Log.e("SystemServer", "Failure installing demo data", e);
381 }
382
383 }
384
385 Context mContext;
386}
387
388public class SystemServer
389{
390 private static final String TAG = "SystemServer";
391
392 public static final int FACTORY_TEST_OFF = 0;
393 public static final int FACTORY_TEST_LOW_LEVEL = 1;
394 public static final int FACTORY_TEST_HIGH_LEVEL = 2;
395
396 /**
397 * This method is called from Zygote to initialize the system. This will cause the native
398 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
399 * up into init2() to start the Android services.
400 */
401 native public static void init1(String[] args);
402
403 public static void main(String[] args) {
404 // The system server has to run all of the time, so it needs to be
405 // as efficient as possible with its memory usage.
406 VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
407
408 System.loadLibrary("android_servers");
409 init1(args);
410 }
411
412 public static final void init2() {
413 Log.i(TAG, "Entered the Android system server!");
414 Thread thr = new ServerThread();
415 thr.setName("android.server.ServerThread");
416 thr.start();
417 }
418}