SystemServer.java revision e540833fdff4d58e37c9ba859388e24e2945ed45
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;
21import com.android.internal.os.SamplingProfilerIntegration;
22
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.BluetoothService;
40import android.server.search.SearchManagerService;
41import android.util.EventLog;
42import android.util.Log;
43import android.accounts.AccountManagerService;
44
45import java.util.Timer;
46import java.util.TimerTask;
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        BatteryService battery = null;
88        ConnectivityService connectivity = null;
89        IPackageManager pm = null;
90        Context context = null;
91        WindowManagerService wm = null;
92        BluetoothService bluetooth = null;
93        BluetoothA2dpService bluetoothA2dp = null;
94        HeadsetObserver headset = null;
95        DockObserver dock = null;
96
97        // Critical services...
98        try {
99            Log.i(TAG, "Entropy Service");
100            ServiceManager.addService("entropy", new EntropyService());
101
102            Log.i(TAG, "Power Manager");
103            power = new PowerManagerService();
104            ServiceManager.addService(Context.POWER_SERVICE, power);
105
106            Log.i(TAG, "Activity Manager");
107            context = ActivityManagerService.main(factoryTest);
108
109            Log.i(TAG, "Telephony Registry");
110            ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));
111
112            AttributeCache.init(context);
113
114            Log.i(TAG, "Package Manager");
115            pm = PackageManagerService.main(context,
116                    factoryTest != SystemServer.FACTORY_TEST_OFF);
117
118            ActivityManagerService.setSystemProcess();
119
120            mContentResolver = context.getContentResolver();
121
122            try {
123                Log.i(TAG, "Account Manager");
124                ServiceManager.addService(Context.ACCOUNT_SERVICE,
125                        new AccountManagerService(context));
126            } catch (Throwable e) {
127                Log.e(TAG, "Failure starting Account Manager", e);
128            }
129
130            Log.i(TAG, "Content Manager");
131            ContentService.main(context,
132                    factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
133
134            Log.i(TAG, "System Content Providers");
135            ActivityManagerService.installSystemProviders();
136
137            Log.i(TAG, "Battery Service");
138            battery = new BatteryService(context);
139            ServiceManager.addService("battery", battery);
140
141            Log.i(TAG, "Hardware Service");
142            hardware = new HardwareService(context);
143            ServiceManager.addService("hardware", hardware);
144
145            // only initialize the power service after we have started the
146            // hardware service, content providers and the battery service.
147            power.init(context, hardware, ActivityManagerService.getDefault(), battery);
148
149            Log.i(TAG, "Alarm Manager");
150            AlarmManagerService alarm = new AlarmManagerService(context);
151            ServiceManager.addService(Context.ALARM_SERVICE, alarm);
152
153            Log.i(TAG, "Init Watchdog");
154            Watchdog.getInstance().init(context, battery, power, alarm,
155                    ActivityManagerService.self());
156
157            // Sensor Service is needed by Window Manager, so this goes first
158            Log.i(TAG, "Sensor Service");
159            ServiceManager.addService(Context.SENSOR_SERVICE, new SensorService(context));
160
161            Log.i(TAG, "Window Manager");
162            wm = WindowManagerService.main(context, power,
163                    factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
164            ServiceManager.addService(Context.WINDOW_SERVICE, wm);
165
166            ((ActivityManagerService)ServiceManager.getService("activity"))
167                    .setWindowManager(wm);
168
169            // Skip Bluetooth if we have an emulator kernel
170            // TODO: Use a more reliable check to see if this product should
171            // support Bluetooth - see bug 988521
172            if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
173                Log.i(TAG, "Registering null Bluetooth Service (emulator)");
174                ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
175            } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
176                Log.i(TAG, "Registering null Bluetooth Service (factory test)");
177                ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
178            } else {
179                Log.i(TAG, "Bluetooth Service");
180                bluetooth = new BluetoothService(context);
181                ServiceManager.addService(Context.BLUETOOTH_SERVICE, bluetooth);
182                bluetooth.initAfterRegistration();
183                bluetoothA2dp = new BluetoothA2dpService(context, bluetooth);
184                ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
185                                          bluetoothA2dp);
186
187                int bluetoothOn = Settings.Secure.getInt(mContentResolver,
188                    Settings.Secure.BLUETOOTH_ON, 0);
189                if (bluetoothOn > 0) {
190                    bluetooth.enable();
191                }
192            }
193
194        } catch (RuntimeException e) {
195            Log.e("System", "Failure starting core service", e);
196        }
197
198        StatusBarService statusBar = null;
199        InputMethodManagerService imm = null;
200        AppWidgetService appWidget = null;
201        NotificationManagerService notification = null;
202        WallpaperManagerService wallpaper = null;
203
204        if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
205            try {
206                Log.i(TAG, "Status Bar");
207                statusBar = new StatusBarService(context);
208                ServiceManager.addService("statusbar", statusBar);
209            } catch (Throwable e) {
210                Log.e(TAG, "Failure starting StatusBarService", e);
211            }
212
213            try {
214                Log.i(TAG, "Clipboard Service");
215                ServiceManager.addService("clipboard", new ClipboardService(context));
216            } catch (Throwable e) {
217                Log.e(TAG, "Failure starting Clipboard Service", e);
218            }
219
220            try {
221                Log.i(TAG, "Input Method Service");
222                imm = new InputMethodManagerService(context, statusBar);
223                ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
224            } catch (Throwable e) {
225                Log.e(TAG, "Failure starting Input Manager Service", e);
226            }
227
228            try {
229                Log.i(TAG, "NetStat Service");
230                ServiceManager.addService("netstat", new NetStatService(context));
231            } catch (Throwable e) {
232                Log.e(TAG, "Failure starting NetStat Service", e);
233            }
234
235            try {
236                Log.i(TAG, "Connectivity Service");
237                connectivity = ConnectivityService.getInstance(context);
238                ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
239            } catch (Throwable e) {
240                Log.e(TAG, "Failure starting Connectivity Service", e);
241            }
242
243            try {
244              Log.i(TAG, "Accessibility Manager");
245              ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
246                      new AccessibilityManagerService(context));
247            } catch (Throwable e) {
248              Log.e(TAG, "Failure starting Accessibility Manager", e);
249            }
250
251            try {
252                Log.i(TAG, "Notification Manager");
253                notification = new NotificationManagerService(context, statusBar, hardware);
254                ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
255            } catch (Throwable e) {
256                Log.e(TAG, "Failure starting Notification Manager", e);
257            }
258
259            try {
260                // MountService must start after NotificationManagerService
261                Log.i(TAG, "Mount Service");
262                ServiceManager.addService("mount", new MountService(context));
263            } catch (Throwable e) {
264                Log.e(TAG, "Failure starting Mount Service", e);
265            }
266
267            try {
268                Log.i(TAG, "Device Storage Monitor");
269                ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
270                        new DeviceStorageMonitorService(context));
271            } catch (Throwable e) {
272                Log.e(TAG, "Failure starting DeviceStorageMonitor service", e);
273            }
274
275            try {
276                Log.i(TAG, "Location Manager");
277                ServiceManager.addService(Context.LOCATION_SERVICE, new LocationManagerService(context));
278            } catch (Throwable e) {
279                Log.e(TAG, "Failure starting Location Manager", e);
280            }
281
282            try {
283                Log.i(TAG, "Search Service");
284                ServiceManager.addService( Context.SEARCH_SERVICE, new SearchManagerService(context) );
285            } catch (Throwable e) {
286                Log.e(TAG, "Failure starting Search Service", e);
287            }
288
289            if (INCLUDE_DEMO) {
290                Log.i(TAG, "Installing demo data...");
291                (new DemoThread(context)).start();
292            }
293
294            try {
295                Log.i(TAG, "Checkin Service");
296                Intent intent = new Intent().setComponent(new ComponentName(
297                        "com.google.android.server.checkin",
298                        "com.google.android.server.checkin.CheckinService"));
299                if (context.startService(intent) == null) {
300                    Log.w(TAG, "Using fallback Checkin Service.");
301                    ServiceManager.addService("checkin", new FallbackCheckinService(context));
302                }
303            } catch (Throwable e) {
304                Log.e(TAG, "Failure starting Checkin Service", e);
305            }
306
307            try {
308                Log.i(TAG, "Wallpaper Service");
309                wallpaper = new WallpaperManagerService(context);
310                ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);
311            } catch (Throwable e) {
312                Log.e(TAG, "Failure starting Wallpaper Service", e);
313            }
314
315            try {
316                Log.i(TAG, "Audio Service");
317                ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
318            } catch (Throwable e) {
319                Log.e(TAG, "Failure starting Audio Service", e);
320            }
321
322            try {
323                Log.i(TAG, "Headset Observer");
324                // Listen for wired headset changes
325                headset = new HeadsetObserver(context);
326            } catch (Throwable e) {
327                Log.e(TAG, "Failure starting HeadsetObserver", e);
328            }
329
330            try {
331                Log.i(TAG, "Dock Observer");
332                // Listen for dock station changes
333                dock = new DockObserver(context);
334            } catch (Throwable e) {
335                Log.e(TAG, "Failure starting DockObserver", e);
336            }
337
338            try {
339                Log.i(TAG, "Backup Service");
340                ServiceManager.addService(Context.BACKUP_SERVICE, new BackupManagerService(context));
341            } catch (Throwable e) {
342                Log.e(TAG, "Failure starting Backup Service", e);
343            }
344
345            try {
346                Log.i(TAG, "AppWidget Service");
347                appWidget = new AppWidgetService(context);
348                ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
349            } catch (Throwable e) {
350                Log.e(TAG, "Failure starting AppWidget Service", e);
351            }
352
353            try {
354                com.android.server.status.StatusBarPolicy.installIcons(context, statusBar);
355            } catch (Throwable e) {
356                Log.e(TAG, "Failure installing status bar icons", e);
357            }
358        }
359
360        // make sure the ADB_ENABLED setting value matches the secure property value
361        Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
362                "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
363
364        // register observer to listen for settings changes
365        mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
366                false, new AdbSettingsObserver());
367
368        // It is now time to start up the app processes...
369        final boolean safeMode = wm.detectSafeMode();
370
371        if (notification != null) {
372            notification.systemReady();
373        }
374
375        if (statusBar != null) {
376            statusBar.systemReady();
377        }
378        wm.systemReady();
379        power.systemReady();
380        try {
381            pm.systemReady();
382        } catch (RemoteException e) {
383        }
384
385        // These are needed to propagate to the runnable below.
386        final BatteryService batteryF = battery;
387        final ConnectivityService connectivityF = connectivity;
388        final DockObserver dockF = dock;
389        final AppWidgetService appWidgetF = appWidget;
390        final WallpaperManagerService wallpaperF = wallpaper;
391        final InputMethodManagerService immF = imm;
392
393        // We now tell the activity manager it is okay to run third party
394        // code.  It will call back into us once it has gotten to the state
395        // where third party code can really run (but before it has actually
396        // started launching the initial applications), for us to complete our
397        // initialization.
398        ((ActivityManagerService)ActivityManagerNative.getDefault())
399                .systemReady(new Runnable() {
400            public void run() {
401                Log.i(TAG, "Making services ready");
402
403                if (batteryF != null) batteryF.systemReady();
404                if (connectivityF != null) connectivityF.systemReady();
405                if (dockF != null) dockF.systemReady();
406                Watchdog.getInstance().start();
407
408                // It is now okay to let the various system services start their
409                // third party code...
410
411                if (appWidgetF != null) appWidgetF.systemReady(safeMode);
412                if (wallpaperF != null) wallpaperF.systemReady();
413                if (immF != null) immF.systemReady();
414            }
415        });
416
417        Looper.loop();
418        Log.d(TAG, "System ServerThread is exiting!");
419    }
420}
421
422class DemoThread extends Thread
423{
424    DemoThread(Context context)
425    {
426        mContext = context;
427    }
428
429    @Override
430    public void run()
431    {
432        try {
433            Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
434            boolean hasData = c != null && c.moveToFirst();
435            if (c != null) {
436                c.deactivate();
437            }
438            if (!hasData) {
439                DemoDataSet dataset = new DemoDataSet();
440                dataset.add(mContext);
441            }
442        } catch (Throwable e) {
443            Log.e("SystemServer", "Failure installing demo data", e);
444        }
445
446    }
447
448    Context mContext;
449}
450
451public class SystemServer
452{
453    private static final String TAG = "SystemServer";
454
455    public static final int FACTORY_TEST_OFF = 0;
456    public static final int FACTORY_TEST_LOW_LEVEL = 1;
457    public static final int FACTORY_TEST_HIGH_LEVEL = 2;
458
459    static Timer timer;
460    static final long SNAPSHOT_INTERVAL = 60 * 60 * 1000; // 1hr
461
462    /**
463     * This method is called from Zygote to initialize the system. This will cause the native
464     * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
465     * up into init2() to start the Android services.
466     */
467    native public static void init1(String[] args);
468
469    public static void main(String[] args) {
470        if (SamplingProfilerIntegration.isEnabled()) {
471            SamplingProfilerIntegration.start();
472            timer = new Timer();
473            timer.schedule(new TimerTask() {
474                @Override
475                public void run() {
476                    SamplingProfilerIntegration.writeSnapshot("system_server");
477                }
478            }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
479        }
480
481        // The system server has to run all of the time, so it needs to be
482        // as efficient as possible with its memory usage.
483        VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
484
485        System.loadLibrary("android_servers");
486        init1(args);
487    }
488
489    public static final void init2() {
490        Log.i(TAG, "Entered the Android system server!");
491        Thread thr = new ServerThread();
492        thr.setName("android.server.ServerThread");
493        thr.start();
494    }
495}
496