blob: ff74472ebec4462b924af8b327489eebd5637af8 [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
19import android.app.ActivityManagerNative;
20import android.content.ContentResolver;
21import android.content.ContentService;
22import android.content.Context;
23import android.content.pm.IPackageManager;
24import android.database.ContentObserver;
25import android.database.Cursor;
26import android.media.AudioService;
27import android.os.Looper;
28import android.os.RemoteException;
29import android.os.ServiceManager;
30import android.os.SystemClock;
31import android.os.SystemProperties;
32import android.os.IBinder;
33import android.provider.Settings;
34import android.provider.Contacts.People;
35import android.server.BluetoothDeviceService;
36import android.server.checkin.FallbackCheckinService;
37import android.server.search.SearchManagerService;
38import android.util.EventLog;
39import android.util.Log;
40
41import dalvik.system.VMRuntime;
42
43import com.android.server.am.ActivityManagerService;
44import com.android.server.status.StatusBarService;
45
46import java.lang.reflect.Constructor;
47import java.lang.reflect.InvocationTargetException;
48
49
50class ServerThread extends Thread {
51 private static final String TAG = "SystemServer";
52 private final static boolean INCLUDE_DEMO = false;
53
54 private static final int LOG_BOOT_PROGRESS_SYSTEM_RUN = 3010;
55
56 private ContentResolver mContentResolver;
57
58 private class AdbSettingsObserver extends ContentObserver {
59 public AdbSettingsObserver() {
60 super(null);
61 }
62 @Override
63 public void onChange(boolean selfChange) {
64 boolean enableAdb = (Settings.System.getInt(mContentResolver,
65 Settings.System.ADB_ENABLED, 0) > 0);
66 // setting this system property will start or stop adbd
67 SystemProperties.set("persist.service.adb.enable", enableAdb ? "1" : "0");
68 }
69 }
70
71 @Override
72 public void run() {
73 EventLog.writeEvent(LOG_BOOT_PROGRESS_SYSTEM_RUN,
74 SystemClock.uptimeMillis());
75
76 ActivityManagerService.prepareTraceFile(false); // create dir
77
78 Looper.prepare();
79
80 android.os.Process.setThreadPriority(
81 android.os.Process.THREAD_PRIORITY_FOREGROUND);
82
83 String factoryTestStr = SystemProperties.get("ro.factorytest");
84 int factoryTest = "".equals(factoryTestStr) ? SystemServer.FACTORY_TEST_OFF
85 : Integer.parseInt(factoryTestStr);
86
87 PowerManagerService power = null;
88 IPackageManager pm = null;
89 Context context = null;
90 WindowManagerService wm = null;
91 BluetoothDeviceService bluetooth = 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
116 Log.i(TAG, "Starting Content Manager.");
117 ContentService.main(context,
118 factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
119
120 Log.i(TAG, "Starting System Content Providers.");
121 ActivityManagerService.installSystemProviders();
122
123 Log.i(TAG, "Starting Battery Service.");
124 BatteryService battery = new BatteryService(context);
125 ServiceManager.addService("battery", battery);
126
127 // only initialize the power service after we have started the
128 // content providers and the batter service.
129 power.init(context, ActivityManagerService.getDefault(), battery);
130
131 Log.i(TAG, "Starting Alarm Manager.");
132 AlarmManagerService alarm = new AlarmManagerService(context);
133 ServiceManager.addService(Context.ALARM_SERVICE, alarm);
134
135 Watchdog.getInstance().init(context, battery, power, alarm,
136 ActivityManagerService.self());
137
138 // Sensor Service is needed by Window Manager, so this goes first
139 Log.i(TAG, "Starting Sensor Service.");
140 ServiceManager.addService(Context.SENSOR_SERVICE, new SensorService(context));
141
142 Log.i(TAG, "Starting Window Manager.");
143 wm = WindowManagerService.main(context, power);
144 ServiceManager.addService(Context.WINDOW_SERVICE, wm);
145
146 ((ActivityManagerService)ServiceManager.getService("activity"))
147 .setWindowManager(wm);
148
149 // Skip Bluetooth if we have an emulator kernel
150 // TODO: Use a more reliable check to see if this product should
151 // support Bluetooth - see bug 988521
152 if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
153 Log.i(TAG, "Registering null Bluetooth Service (emulator)");
154 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
155 } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
156 Log.i(TAG, "Registering null Bluetooth Service (factory test)");
157 ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
158 } else {
159 Log.i(TAG, "Starting Bluetooth Service.");
160 bluetooth = new BluetoothDeviceService(context);
161 bluetooth.init();
162 ServiceManager.addService(Context.BLUETOOTH_SERVICE, bluetooth);
163
164 int bluetoothOn = Settings.System.getInt(mContentResolver,
165 Settings.System.BLUETOOTH_ON, 0);
166 if (bluetoothOn > 0) {
167 bluetooth.enable(null);
168 }
169 }
170
171 } catch (RuntimeException e) {
172 Log.e("System", "Failure starting core service", e);
173 }
174
175 if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
176 StatusBarService statusBar = null;
177 try {
178 Log.i(TAG, "Starting Status Bar Service.");
179 statusBar = new StatusBarService(context);
180 ServiceManager.addService("statusbar", statusBar);
181 com.android.server.status.StatusBarPolicy.installIcons(context, statusBar);
182 } catch (Throwable e) {
183 Log.e(TAG, "Failure starting StatusBarService", e);
184 }
185
186 try {
187 Log.i(TAG, "Starting Hardware Service.");
188 ServiceManager.addService("hardware", new HardwareService(context));
189 } catch (Throwable e) {
190 Log.e(TAG, "Failure starting Hardware Service", e);
191 }
192
193 try {
194 Log.i(TAG, "Starting NetStat Service.");
195 ServiceManager.addService("netstat", new NetStatService(context));
196 } catch (Throwable e) {
197 Log.e(TAG, "Failure starting NetStat Service", e);
198 }
199
200 try {
201 Log.i(TAG, "Starting Connectivity Service.");
202 ServiceManager.addService(Context.CONNECTIVITY_SERVICE,
203 ConnectivityService.getInstance(context));
204 } catch (Throwable e) {
205 Log.e(TAG, "Failure starting Connectivity Service", e);
206 }
207
208 try {
209 Log.i(TAG, "Starting Notification Manager.");
210 ServiceManager.addService(Context.NOTIFICATION_SERVICE,
211 new NotificationManagerService(context, statusBar));
212 } catch (Throwable e) {
213 Log.e(TAG, "Failure starting Notification Manager", e);
214 }
215
216 try {
217 // MountService must start after NotificationManagerService
218 Log.i(TAG, "Starting Mount Service.");
219 ServiceManager.addService("mount", new MountService(context));
220
221 } catch (Throwable e) {
222 Log.e(TAG, "Failure starting Mount Service", e);
223 }
224
225 try {
226 Log.i(TAG, "Starting DeviceMemoryMonitor service");
227 ServiceManager.addService("devicememorymonitor", new DeviceMemoryMonitorService(context));
228 } catch (Throwable e) {
229 Log.e(TAG, "Failure starting DeviceMemoryMonitor service", e);
230 }
231
232 try {
233 Log.i(TAG, "Starting Location Manager.");
234 ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
235 } catch (Throwable e) {
236 Log.e(TAG, "Failure starting Location Manager", e);
237 }
238
239 try {
240 Log.i(TAG, "Starting Search Service.");
241 ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) );
242 } catch (Throwable e) {
243 Log.e(TAG, "Failure starting Search Service", e);
244 }
245
246 try {
247 Log.i(TAG, "Starting Clipboard Service.");
248 ServiceManager.addService("clipboard", new ClipboardService(context));
249 } catch (Throwable e) {
250 Log.e(TAG, "Failure starting Clipboard Service", e);
251 }
252
253 if (INCLUDE_DEMO) {
254 Log.i(TAG, "Installing demo data...");
255 (new DemoThread(context)).start();
256 }
257
258 try {
259 Log.i(TAG, "Starting Checkin Service");
260 addService(context, "checkin", "com.google.android.server.checkin.CheckinService",
261 FallbackCheckinService.class);
262 } catch (Throwable e) {
263 Log.e(TAG, "Failure starting Checkin Service", e);
264 }
265
266 try {
267 Log.i(TAG, "Starting Wallpaper Service");
268 ServiceManager.addService(Context.WALLPAPER_SERVICE, new WallpaperService(context));
269 } catch (Throwable e) {
270 Log.e(TAG, "Failure starting Wallpaper Service", e);
271 }
272
273 try {
274 Log.i(TAG, "Starting Audio Service");
275 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
276 } catch (Throwable e) {
277 Log.e(TAG, "Failure starting Volume Service", e);
278 }
279
280 try {
281 Log.i(TAG, "Starting HeadsetObserver");
282 // Listen for wired headset changes
283 headset = new HeadsetObserver(context);
284 } catch (Throwable e) {
285 Log.e(TAG, "Failure starting HeadsetObserver", e);
286 }
287 }
288
289 // make sure the ADB_ENABLED setting value matches the system property value
290 Settings.System.putInt(mContentResolver, Settings.System.ADB_ENABLED,
291 "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
292
293 // register observer to listen for settings changes
294 mContentResolver.registerContentObserver(Settings.System.getUriFor(Settings.System.ADB_ENABLED),
295 false, new AdbSettingsObserver());
296
297 // It is now time to start up the app processes...
298 wm.systemReady();
299 power.systemReady();
300 try {
301 pm.systemReady();
302 } catch (RemoteException e) {
303 }
304
305 // After making the following code, third party code may be running...
306 try {
307 ActivityManagerNative.getDefault().systemReady();
308 } catch (RemoteException e) {
309 }
310
311 Watchdog.getInstance().start();
312
313 Looper.loop();
314 Log.d(TAG, "System ServerThread is exiting!");
315 }
316
317 private void addService(Context context, String name, String serviceClass,
318 Class<? extends IBinder> fallback) {
319
320 final IBinder service = findService(context, serviceClass, fallback);
321 if (service != null) {
322 ServiceManager.addService(name, service);
323 } else {
324 Log.e(TAG, "Failure starting service '" + name + "' with class " + serviceClass);
325 }
326 }
327
328 private IBinder findService(Context context, String serviceClass,
329 Class<? extends IBinder> fallback) {
330
331 IBinder service = null;
332 try {
333 Class<?> klass = Class.forName(serviceClass);
334 Constructor<?> c = klass.getConstructor(Context.class);
335 service = (IBinder) c.newInstance(context);
336 } catch (ClassNotFoundException e) {
337 // Ignore
338 } catch (IllegalAccessException e) {
339 // Ignore
340 } catch (NoSuchMethodException e) {
341 // Ignore
342 } catch (InvocationTargetException e) {
343 // Ignore
344 } catch (InstantiationException e) {
345 // Ignore
346 }
347
348 if (service == null && fallback != null) {
349 Log.w(TAG, "Could not find " + serviceClass + ", trying fallback");
350 try {
351 service = fallback.newInstance();
352 } catch (IllegalAccessException e) {
353 // Ignore
354 } catch (InstantiationException e) {
355 // Ignore
356 }
357 }
358
359 return service;
360 }
361}
362
363class DemoThread extends Thread
364{
365 DemoThread(Context context)
366 {
367 mContext = context;
368 }
369
370 @Override
371 public void run()
372 {
373 try {
374 Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
375 boolean hasData = c != null && c.moveToFirst();
376 if (c != null) {
377 c.deactivate();
378 }
379 if (!hasData) {
380 DemoDataSet dataset = new DemoDataSet();
381 dataset.add(mContext);
382 }
383 } catch (Throwable e) {
384 Log.e("SystemServer", "Failure installing demo data", e);
385 }
386
387 }
388
389 Context mContext;
390}
391
392public class SystemServer
393{
394 private static final String TAG = "SystemServer";
395
396 public static final int FACTORY_TEST_OFF = 0;
397 public static final int FACTORY_TEST_LOW_LEVEL = 1;
398 public static final int FACTORY_TEST_HIGH_LEVEL = 2;
399
400 /**
401 * This method is called from Zygote to initialize the system. This will cause the native
402 * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
403 * up into init2() to start the Android services.
404 */
405 native public static void init1(String[] args);
406
407 public static void main(String[] args) {
408 // The system server has to run all of the time, so it needs to be
409 // as efficient as possible with its memory usage.
410 VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
411
412 System.loadLibrary("android_servers");
413 init1(args);
414 }
415
416 public static final void init2() {
417 Log.i(TAG, "Entered the Android system server!");
418 Thread thr = new ServerThread();
419 thr.setName("android.server.ServerThread");
420 thr.start();
421 }
422}