SystemServer.java revision afefa49abce3a1f8dd64644009f73aaeea86d7e0
1/*
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;
35import android.os.*;
36import 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;
43import android.accounts.AccountManagerService;
44
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
85        HardwareService hardware = null;
86        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 Entropy Service.");
97            ServiceManager.addService("entropy", new EntropyService());
98
99            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
119            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
127            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
138            Log.i(TAG, "Starting Hardware Service.");
139            hardware = new HardwareService(context);
140            ServiceManager.addService("hardware", hardware);
141
142            // only initialize the power service after we have started the
143            // hardware service, content providers and the battery service.
144            power.init(context, hardware, ActivityManagerService.getDefault(), battery);
145
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);
179                bluetoothA2dp = new BluetoothA2dpService(context, bluetooth);
180                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) {
186                    bluetooth.enable();
187                }
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;
196        AppWidgetService appWidget = null;
197        NotificationManagerService notification = null;
198
199        if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
200            try {
201                Log.i(TAG, "Starting Status Bar Service.");
202                statusBar = new StatusBarService(context);
203                ServiceManager.addService("statusbar", statusBar);
204            } catch (Throwable e) {
205                Log.e(TAG, "Failure starting StatusBarService", e);
206            }
207
208            try {
209                Log.i(TAG, "Starting Clipboard Service.");
210                ServiceManager.addService("clipboard", new ClipboardService(context));
211            } catch (Throwable e) {
212                Log.e(TAG, "Failure starting Clipboard Service", e);
213            }
214
215            try {
216                Log.i(TAG, "Starting Input Method Service.");
217                imm = new InputMethodManagerService(context, statusBar);
218                ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
219            } catch (Throwable e) {
220                Log.e(TAG, "Failure starting Input Manager Service", e);
221            }
222
223            try {
224                Log.i(TAG, "Starting NetStat Service.");
225                ServiceManager.addService("netstat", new NetStatService(context));
226            } catch (Throwable e) {
227                Log.e(TAG, "Failure starting NetStat Service", e);
228            }
229
230            try {
231                Log.i(TAG, "Starting Connectivity Service.");
232                ServiceManager.addService(Context.CONNECTIVITY_SERVICE,
233                        ConnectivityService.getInstance(context));
234            } catch (Throwable e) {
235                Log.e(TAG, "Failure starting Connectivity Service", e);
236            }
237
238            try {
239              Log.i(TAG, "Starting Accessibility Manager.");
240              ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
241                      new AccessibilityManagerService(context));
242            } catch (Throwable e) {
243              Log.e(TAG, "Failure starting Accessibility Manager", e);
244            }
245
246            try {
247                Log.i(TAG, "Starting Notification Manager.");
248                notification = new NotificationManagerService(context, statusBar, hardware);
249                ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
250            } catch (Throwable e) {
251                Log.e(TAG, "Failure starting Notification Manager", e);
252            }
253
254            try {
255                // MountService must start after NotificationManagerService
256                Log.i(TAG, "Starting Mount Service.");
257                ServiceManager.addService("mount", new MountService(context));
258            } catch (Throwable e) {
259                Log.e(TAG, "Failure starting Mount Service", e);
260            }
261
262            try {
263                Log.i(TAG, "Starting DeviceStorageMonitor service");
264                ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
265                        new DeviceStorageMonitorService(context));
266            } catch (Throwable e) {
267                Log.e(TAG, "Failure starting DeviceStorageMonitor service", e);
268            }
269
270            try {
271                Log.i(TAG, "Starting Location Manager.");
272                ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
273            } catch (Throwable e) {
274                Log.e(TAG, "Failure starting Location Manager", e);
275            }
276
277            try {
278                Log.i(TAG, "Starting Search Service.");
279                ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) );
280            } catch (Throwable e) {
281                Log.e(TAG, "Failure starting Search Service", e);
282            }
283
284            if (INCLUDE_DEMO) {
285                Log.i(TAG, "Installing demo data...");
286                (new DemoThread(context)).start();
287            }
288
289            try {
290                Log.i(TAG, "Starting Checkin Service.");
291                Intent intent = new Intent().setComponent(new ComponentName(
292                        "com.google.android.server.checkin",
293                        "com.google.android.server.checkin.CheckinService"));
294                if (context.startService(intent) == null) {
295                    Log.w(TAG, "Using fallback Checkin Service.");
296                    ServiceManager.addService("checkin", new FallbackCheckinService(context));
297                }
298            } catch (Throwable e) {
299                Log.e(TAG, "Failure starting Checkin Service", e);
300            }
301
302            try {
303                Log.i(TAG, "Starting Wallpaper Service");
304                ServiceManager.addService(Context.WALLPAPER_SERVICE, new WallpaperService(context));
305            } catch (Throwable e) {
306                Log.e(TAG, "Failure starting Wallpaper Service", e);
307            }
308
309            try {
310                Log.i(TAG, "Starting Audio Service");
311                ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
312            } catch (Throwable e) {
313                Log.e(TAG, "Failure starting Audio Service", e);
314            }
315
316            try {
317                Log.i(TAG, "Starting HeadsetObserver");
318                // Listen for wired headset changes
319                headset = new HeadsetObserver(context);
320            } catch (Throwable e) {
321                Log.e(TAG, "Failure starting HeadsetObserver", e);
322            }
323
324            try {
325                Log.i(TAG, "Starting Backup Service");
326                ServiceManager.addService(Context.BACKUP_SERVICE, new BackupManagerService(context));
327            } catch (Throwable e) {
328                Log.e(TAG, "Failure starting Backup Service", e);
329            }
330
331            try {
332                Log.i(TAG, "Starting AppWidget Service");
333                appWidget = new AppWidgetService(context);
334                ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
335            } catch (Throwable e) {
336                Log.e(TAG, "Failure starting AppWidget Service", e);
337            }
338
339            try {
340                com.android.server.status.StatusBarPolicy.installIcons(context, statusBar);
341            } catch (Throwable e) {
342                Log.e(TAG, "Failure installing status bar icons", e);
343            }
344        }
345
346        // make sure the ADB_ENABLED setting value matches the secure property value
347        Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
348                "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
349
350        // register observer to listen for settings changes
351        mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
352                false, new AdbSettingsObserver());
353
354        // It is now time to start up the app processes...
355        boolean safeMode = wm.detectSafeMode();
356
357        if (notification != null) {
358            notification.systemReady();
359        }
360
361        if (statusBar != null) {
362            statusBar.systemReady();
363        }
364        if (imm != null) {
365            imm.systemReady();
366        }
367        wm.systemReady();
368        power.systemReady();
369        try {
370            pm.systemReady();
371        } catch (RemoteException e) {
372        }
373        if (appWidget != null) {
374            appWidget.systemReady(safeMode);
375        }
376
377        // After making the following code, third party code may be running...
378        try {
379            ActivityManagerNative.getDefault().systemReady();
380        } catch (RemoteException e) {
381        }
382
383        Watchdog.getInstance().start();
384
385        Looper.loop();
386        Log.d(TAG, "System ServerThread is exiting!");
387    }
388}
389
390class DemoThread extends Thread
391{
392    DemoThread(Context context)
393    {
394        mContext = context;
395    }
396
397    @Override
398    public void run()
399    {
400        try {
401            Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
402            boolean hasData = c != null && c.moveToFirst();
403            if (c != null) {
404                c.deactivate();
405            }
406            if (!hasData) {
407                DemoDataSet dataset = new DemoDataSet();
408                dataset.add(mContext);
409            }
410        } catch (Throwable e) {
411            Log.e("SystemServer", "Failure installing demo data", e);
412        }
413
414    }
415
416    Context mContext;
417}
418
419public class SystemServer
420{
421    private static final String TAG = "SystemServer";
422
423    public static final int FACTORY_TEST_OFF = 0;
424    public static final int FACTORY_TEST_LOW_LEVEL = 1;
425    public static final int FACTORY_TEST_HIGH_LEVEL = 2;
426
427    /**
428     * This method is called from Zygote to initialize the system. This will cause the native
429     * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
430     * up into init2() to start the Android services.
431     */
432    native public static void init1(String[] args);
433
434    public static void main(String[] args) {
435        // The system server has to run all of the time, so it needs to be
436        // as efficient as possible with its memory usage.
437        VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
438
439        System.loadLibrary("android_servers");
440        init1(args);
441    }
442
443    public static final void init2() {
444        Log.i(TAG, "Entered the Android system server!");
445        Thread thr = new ServerThread();
446        thr.setName("android.server.ServerThread");
447        thr.start();
448    }
449}
450