SystemServer.java revision 798ac4c7d79534a8230623b737b5b1b6a415d105
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.StatusBarManagerService;
21import com.android.internal.os.BinderInternal;
22import com.android.internal.os.SamplingProfilerIntegration;
23
24import dalvik.system.VMRuntime;
25import dalvik.system.Zygote;
26
27import android.app.ActivityManagerNative;
28import android.bluetooth.BluetoothAdapter;
29import android.content.ComponentName;
30import android.content.ContentResolver;
31import android.content.ContentService;
32import android.content.Context;
33import android.content.Intent;
34import android.content.pm.IPackageManager;
35import android.database.ContentObserver;
36import android.database.Cursor;
37import android.media.AudioService;
38import android.os.*;
39import android.provider.Contacts.People;
40import android.provider.Settings;
41import android.server.BluetoothA2dpService;
42import android.server.BluetoothService;
43import android.server.search.SearchManagerService;
44import android.util.EventLog;
45import android.util.Slog;
46import android.accounts.AccountManagerService;
47
48import java.io.File;
49import java.util.Timer;
50import java.util.TimerTask;
51
52class 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) {
66            boolean enableAdb = (Settings.Secure.getInt(mContentResolver,
67                Settings.Secure.ADB_ENABLED, 0) > 0);
68            // setting this secure property will start or stop adbd
69           SystemProperties.set("persist.service.adb.enable", enableAdb ? "1" : "0");
70        }
71    }
72
73    @Override
74    public void run() {
75        EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN,
76            SystemClock.uptimeMillis());
77
78        Looper.prepare();
79
80        android.os.Process.setThreadPriority(
81                android.os.Process.THREAD_PRIORITY_FOREGROUND);
82
83        BinderInternal.disableBackgroundScheduling(true);
84
85        String factoryTestStr = SystemProperties.get("ro.factorytest");
86        int factoryTest = "".equals(factoryTestStr) ? SystemServer.FACTORY_TEST_OFF
87                : Integer.parseInt(factoryTestStr);
88
89        LightsService lights = null;
90        PowerManagerService power = null;
91        BatteryService battery = null;
92        ConnectivityService connectivity = null;
93        IPackageManager pm = null;
94        Context context = null;
95        WindowManagerService wm = null;
96        BluetoothService bluetooth = null;
97        BluetoothA2dpService bluetoothA2dp = null;
98        HeadsetObserver headset = null;
99        DockObserver dock = null;
100        UiModeManagerService uiMode = null;
101        RecognitionManagerService recognition = null;
102        ThrottleService throttle = null;
103
104        // Critical services...
105        try {
106            Slog.i(TAG, "Entropy Service");
107            ServiceManager.addService("entropy", new EntropyService());
108
109            Slog.i(TAG, "Power Manager");
110            power = new PowerManagerService();
111            ServiceManager.addService(Context.POWER_SERVICE, power);
112
113            Slog.i(TAG, "Activity Manager");
114            context = ActivityManagerService.main(factoryTest);
115
116            Slog.i(TAG, "Telephony Registry");
117            ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));
118
119            AttributeCache.init(context);
120
121            Slog.i(TAG, "Package Manager");
122            pm = PackageManagerService.main(context,
123                    factoryTest != SystemServer.FACTORY_TEST_OFF);
124
125            ActivityManagerService.setSystemProcess();
126
127            mContentResolver = context.getContentResolver();
128
129            // The AccountManager must come before the ContentService
130            try {
131                Slog.i(TAG, "Account Manager");
132                ServiceManager.addService(Context.ACCOUNT_SERVICE,
133                        new AccountManagerService(context));
134            } catch (Throwable e) {
135                Slog.e(TAG, "Failure starting Account Manager", e);
136            }
137
138            Slog.i(TAG, "Content Manager");
139            ContentService.main(context,
140                    factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
141
142            Slog.i(TAG, "System Content Providers");
143            ActivityManagerService.installSystemProviders();
144
145            Slog.i(TAG, "Battery Service");
146            battery = new BatteryService(context);
147            ServiceManager.addService("battery", battery);
148
149            Slog.i(TAG, "Lights Service");
150            lights = new LightsService(context);
151
152            Slog.i(TAG, "Vibrator Service");
153            ServiceManager.addService("vibrator", new VibratorService(context));
154
155            // only initialize the power service after we have started the
156            // lights service, content providers and the battery service.
157            power.init(context, lights, ActivityManagerService.getDefault(), battery);
158
159            Slog.i(TAG, "Alarm Manager");
160            AlarmManagerService alarm = new AlarmManagerService(context);
161            ServiceManager.addService(Context.ALARM_SERVICE, alarm);
162
163            Slog.i(TAG, "Init Watchdog");
164            Watchdog.getInstance().init(context, battery, power, alarm,
165                    ActivityManagerService.self());
166
167            // Sensor Service is needed by Window Manager, so this goes first
168            Slog.i(TAG, "Sensor Service");
169            ServiceManager.addService(Context.SENSOR_SERVICE, new SensorService(context));
170
171            Slog.i(TAG, "Window Manager");
172            wm = WindowManagerService.main(context, power,
173                    factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
174            ServiceManager.addService(Context.WINDOW_SERVICE, wm);
175
176            ((ActivityManagerService)ServiceManager.getService("activity"))
177                    .setWindowManager(wm);
178
179            // Skip Bluetooth if we have an emulator kernel
180            // TODO: Use a more reliable check to see if this product should
181            // support Bluetooth - see bug 988521
182            if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
183                Slog.i(TAG, "Registering null Bluetooth Service (emulator)");
184                ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null);
185            } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
186                Slog.i(TAG, "Registering null Bluetooth Service (factory test)");
187                ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, null);
188            } else {
189                Slog.i(TAG, "Bluetooth Service");
190                bluetooth = new BluetoothService(context);
191                ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, bluetooth);
192                bluetooth.initAfterRegistration();
193                bluetoothA2dp = new BluetoothA2dpService(context, bluetooth);
194                ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
195                                          bluetoothA2dp);
196
197                int bluetoothOn = Settings.Secure.getInt(mContentResolver,
198                    Settings.Secure.BLUETOOTH_ON, 0);
199                if (bluetoothOn > 0) {
200                    bluetooth.enable();
201                }
202            }
203
204        } catch (RuntimeException e) {
205            Slog.e("System", "Failure starting core service", e);
206        }
207
208        DevicePolicyManagerService devicePolicy = null;
209        StatusBarManagerService statusBar = null;
210        InputMethodManagerService imm = null;
211        AppWidgetService appWidget = null;
212        NotificationManagerService notification = null;
213        WallpaperManagerService wallpaper = null;
214        LocationManagerService location = null;
215
216        if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
217            try {
218                Slog.i(TAG, "Device Policy");
219                devicePolicy = new DevicePolicyManagerService(context);
220                ServiceManager.addService(Context.DEVICE_POLICY_SERVICE, devicePolicy);
221            } catch (Throwable e) {
222                Slog.e(TAG, "Failure starting DevicePolicyService", e);
223            }
224
225            try {
226                Slog.i(TAG, "Status Bar");
227                statusBar = new StatusBarManagerService(context);
228                ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);
229            } catch (Throwable e) {
230                Slog.e(TAG, "Failure starting StatusBarManagerService", e);
231            }
232
233            try {
234                Slog.i(TAG, "Clipboard Service");
235                ServiceManager.addService(Context.CLIPBOARD_SERVICE,
236                        new ClipboardService(context));
237            } catch (Throwable e) {
238                Slog.e(TAG, "Failure starting Clipboard Service", e);
239            }
240
241            try {
242                Slog.i(TAG, "Input Method Service");
243                imm = new InputMethodManagerService(context, statusBar);
244                ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
245            } catch (Throwable e) {
246                Slog.e(TAG, "Failure starting Input Manager Service", e);
247            }
248
249            try {
250                Slog.i(TAG, "NetStat Service");
251                ServiceManager.addService("netstat", new NetStatService(context));
252            } catch (Throwable e) {
253                Slog.e(TAG, "Failure starting NetStat Service", e);
254            }
255
256            try {
257                Slog.i(TAG, "NetworkManagement Service");
258                ServiceManager.addService(
259                        Context.NETWORKMANAGEMENT_SERVICE, new NetworkManagementService(context));
260            } catch (Throwable e) {
261                Slog.e(TAG, "Failure starting NetworkManagement Service", e);
262            }
263
264            try {
265                Slog.i(TAG, "Connectivity Service");
266                connectivity = ConnectivityService.getInstance(context);
267                ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
268            } catch (Throwable e) {
269                Slog.e(TAG, "Failure starting Connectivity Service", e);
270            }
271
272            try {
273                Slog.i(TAG, "Throttle Service");
274                throttle = new ThrottleService(context);
275                ServiceManager.addService(
276                        Context.THROTTLE_SERVICE, throttle);
277            } catch (Throwable e) {
278                Slog.e(TAG, "Failure starting ThrottleService", e);
279            }
280
281            try {
282              Slog.i(TAG, "Accessibility Manager");
283              ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
284                      new AccessibilityManagerService(context));
285            } catch (Throwable e) {
286              Slog.e(TAG, "Failure starting Accessibility Manager", e);
287            }
288
289            try {
290                /*
291                 * NotificationManagerService is dependant on MountService,
292                 * (for media / usb notifications) so we must start MountService first.
293                 */
294                Slog.i(TAG, "Mount Service");
295                ServiceManager.addService("mount", new MountService(context));
296            } catch (Throwable e) {
297                Slog.e(TAG, "Failure starting Mount Service", e);
298            }
299
300            try {
301                Slog.i(TAG, "Notification Manager");
302                notification = new NotificationManagerService(context, statusBar, lights);
303                ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
304            } catch (Throwable e) {
305                Slog.e(TAG, "Failure starting Notification Manager", e);
306            }
307
308            try {
309                Slog.i(TAG, "Device Storage Monitor");
310                ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
311                        new DeviceStorageMonitorService(context));
312            } catch (Throwable e) {
313                Slog.e(TAG, "Failure starting DeviceStorageMonitor service", e);
314            }
315
316            try {
317                Slog.i(TAG, "Location Manager");
318                location = new LocationManagerService(context);
319                ServiceManager.addService(Context.LOCATION_SERVICE, location);
320            } catch (Throwable e) {
321                Slog.e(TAG, "Failure starting Location Manager", e);
322            }
323
324            try {
325                Slog.i(TAG, "Search Service");
326                ServiceManager.addService(Context.SEARCH_SERVICE,
327                        new SearchManagerService(context));
328            } catch (Throwable e) {
329                Slog.e(TAG, "Failure starting Search Service", e);
330            }
331
332            if (INCLUDE_DEMO) {
333                Slog.i(TAG, "Installing demo data...");
334                (new DemoThread(context)).start();
335            }
336
337            try {
338                Slog.i(TAG, "DropBox Service");
339                ServiceManager.addService(Context.DROPBOX_SERVICE,
340                        new DropBoxManagerService(context, new File("/data/system/dropbox")));
341            } catch (Throwable e) {
342                Slog.e(TAG, "Failure starting DropBoxManagerService", e);
343            }
344
345            try {
346                Slog.i(TAG, "Wallpaper Service");
347                wallpaper = new WallpaperManagerService(context);
348                ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);
349            } catch (Throwable e) {
350                Slog.e(TAG, "Failure starting Wallpaper Service", e);
351            }
352
353            try {
354                Slog.i(TAG, "Audio Service");
355                ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
356            } catch (Throwable e) {
357                Slog.e(TAG, "Failure starting Audio Service", e);
358            }
359
360            try {
361                Slog.i(TAG, "Headset Observer");
362                // Listen for wired headset changes
363                headset = new HeadsetObserver(context);
364            } catch (Throwable e) {
365                Slog.e(TAG, "Failure starting HeadsetObserver", e);
366            }
367
368            try {
369                Slog.i(TAG, "Dock Observer");
370                // Listen for dock station changes
371                dock = new DockObserver(context, power);
372            } catch (Throwable e) {
373                Slog.e(TAG, "Failure starting DockObserver", e);
374            }
375
376            try {
377                Slog.i(TAG, "UI Mode Manager Service");
378                // Listen for dock station changes
379                uiMode = new UiModeManagerService(context);
380            } catch (Throwable e) {
381                Slog.e(TAG, "Failure starting UiModeManagerService", e);
382            }
383
384            try {
385                Slog.i(TAG, "Backup Service");
386                ServiceManager.addService(Context.BACKUP_SERVICE,
387                        new BackupManagerService(context));
388            } catch (Throwable e) {
389                Slog.e(TAG, "Failure starting Backup Service", e);
390            }
391
392            try {
393                Slog.i(TAG, "AppWidget Service");
394                appWidget = new AppWidgetService(context);
395                ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
396            } catch (Throwable e) {
397                Slog.e(TAG, "Failure starting AppWidget Service", e);
398            }
399
400            try {
401                Slog.i(TAG, "Recognition Service");
402                recognition = new RecognitionManagerService(context);
403            } catch (Throwable e) {
404                Slog.e(TAG, "Failure starting Recognition Service", e);
405            }
406
407            try {
408                Slog.i(TAG, "DiskStats Service");
409                ServiceManager.addService("diskstats", new DiskStatsService(context));
410            } catch (Throwable e) {
411                Slog.e(TAG, "Failure starting DiskStats Service", e);
412            }
413        }
414
415        // make sure the ADB_ENABLED setting value matches the secure property value
416        Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
417                "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
418
419        // register observer to listen for settings changes
420        mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
421                false, new AdbSettingsObserver());
422
423        // Before things start rolling, be sure we have decided whether
424        // we are in safe mode.
425        final boolean safeMode = wm.detectSafeMode();
426        if (safeMode) {
427            try {
428                ActivityManagerNative.getDefault().enterSafeMode();
429                // Post the safe mode state in the Zygote class
430                Zygote.systemInSafeMode = true;
431                // Disable the JIT for the system_server process
432                VMRuntime.getRuntime().disableJitCompilation();
433            } catch (RemoteException e) {
434            }
435        } else {
436            // Enable the JIT for the system_server process
437            VMRuntime.getRuntime().startJitCompilation();
438        }
439
440        // It is now time to start up the app processes...
441
442        if (devicePolicy != null) {
443            devicePolicy.systemReady();
444        }
445
446        if (notification != null) {
447            notification.systemReady();
448        }
449
450        if (statusBar != null) {
451            statusBar.systemReady();
452        }
453        wm.systemReady();
454        power.systemReady();
455        try {
456            pm.systemReady();
457        } catch (RemoteException e) {
458        }
459
460        // These are needed to propagate to the runnable below.
461        final StatusBarManagerService statusBarF = statusBar;
462        final BatteryService batteryF = battery;
463        final ConnectivityService connectivityF = connectivity;
464        final DockObserver dockF = dock;
465        final ThrottleService throttleF = throttle;
466        final UiModeManagerService uiModeF = uiMode;
467        final AppWidgetService appWidgetF = appWidget;
468        final WallpaperManagerService wallpaperF = wallpaper;
469        final InputMethodManagerService immF = imm;
470        final RecognitionManagerService recognitionF = recognition;
471        final LocationManagerService locationF = location;
472
473        // We now tell the activity manager it is okay to run third party
474        // code.  It will call back into us once it has gotten to the state
475        // where third party code can really run (but before it has actually
476        // started launching the initial applications), for us to complete our
477        // initialization.
478        ((ActivityManagerService)ActivityManagerNative.getDefault())
479                .systemReady(new Runnable() {
480            public void run() {
481                Slog.i(TAG, "Making services ready");
482
483                if (statusBarF != null) statusBarF.systemReady2();
484                if (batteryF != null) batteryF.systemReady();
485                if (connectivityF != null) connectivityF.systemReady();
486                if (dockF != null) dockF.systemReady();
487                if (uiModeF != null) uiModeF.systemReady();
488                if (recognitionF != null) recognitionF.systemReady();
489                Watchdog.getInstance().start();
490
491                // It is now okay to let the various system services start their
492                // third party code...
493
494                if (appWidgetF != null) appWidgetF.systemReady(safeMode);
495                if (wallpaperF != null) wallpaperF.systemReady();
496                if (immF != null) immF.systemReady();
497                if (locationF != null) locationF.systemReady();
498                if (throttleF != null) throttleF.systemReady();
499            }
500        });
501
502        Looper.loop();
503        Slog.d(TAG, "System ServerThread is exiting!");
504    }
505}
506
507class DemoThread extends Thread
508{
509    DemoThread(Context context)
510    {
511        mContext = context;
512    }
513
514    @Override
515    public void run()
516    {
517        try {
518            Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
519            boolean hasData = c != null && c.moveToFirst();
520            if (c != null) {
521                c.deactivate();
522            }
523            if (!hasData) {
524                DemoDataSet dataset = new DemoDataSet();
525                dataset.add(mContext);
526            }
527        } catch (Throwable e) {
528            Slog.e("SystemServer", "Failure installing demo data", e);
529        }
530
531    }
532
533    Context mContext;
534}
535
536public class SystemServer
537{
538    private static final String TAG = "SystemServer";
539
540    public static final int FACTORY_TEST_OFF = 0;
541    public static final int FACTORY_TEST_LOW_LEVEL = 1;
542    public static final int FACTORY_TEST_HIGH_LEVEL = 2;
543
544    static Timer timer;
545    static final long SNAPSHOT_INTERVAL = 60 * 60 * 1000; // 1hr
546
547    /**
548     * This method is called from Zygote to initialize the system. This will cause the native
549     * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
550     * up into init2() to start the Android services.
551     */
552    native public static void init1(String[] args);
553
554    public static void main(String[] args) {
555        if (SamplingProfilerIntegration.isEnabled()) {
556            SamplingProfilerIntegration.start();
557            timer = new Timer();
558            timer.schedule(new TimerTask() {
559                @Override
560                public void run() {
561                    SamplingProfilerIntegration.writeSnapshot("system_server");
562                }
563            }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
564        }
565
566        // The system server has to run all of the time, so it needs to be
567        // as efficient as possible with its memory usage.
568        VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
569
570        System.loadLibrary("android_servers");
571        init1(args);
572    }
573
574    public static final void init2() {
575        Slog.i(TAG, "Entered the Android system server!");
576        Thread thr = new ServerThread();
577        thr.setName("android.server.ServerThread");
578        thr.start();
579    }
580}
581