SystemServer.java revision 6ccd2aff3eb4450c6f1021637d18e4e5d9346bf0
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.VMRuntime;
23
24import android.app.ActivityManagerNative;
25import android.content.ComponentName;
26import android.content.ContentResolver;
27import android.content.ContentService;
28import android.content.Context;
29import android.content.Intent;
30import android.content.pm.IPackageManager;
31import android.database.ContentObserver;
32import android.database.Cursor;
33import android.media.AudioService;
34import android.os.*;
35import android.provider.Contacts.People;
36import android.provider.Settings;
37import android.server.BluetoothA2dpService;
38import android.server.BluetoothService;
39import android.server.search.SearchManagerService;
40import android.util.EventLog;
41import android.util.Log;
42import android.accounts.AccountManagerService;
43
44class ServerThread extends Thread {
45    private static final String TAG = "SystemServer";
46    private final static boolean INCLUDE_DEMO = false;
47
48    private static final int LOG_BOOT_PROGRESS_SYSTEM_RUN = 3010;
49
50    private ContentResolver mContentResolver;
51
52    private class AdbSettingsObserver extends ContentObserver {
53        public AdbSettingsObserver() {
54            super(null);
55        }
56        @Override
57        public void onChange(boolean selfChange) {
58            boolean enableAdb = (Settings.Secure.getInt(mContentResolver,
59                Settings.Secure.ADB_ENABLED, 0) > 0);
60            // setting this secure property will start or stop adbd
61           SystemProperties.set("persist.service.adb.enable", enableAdb ? "1" : "0");
62        }
63    }
64
65    @Override
66    public void run() {
67        EventLog.writeEvent(LOG_BOOT_PROGRESS_SYSTEM_RUN,
68            SystemClock.uptimeMillis());
69
70        ActivityManagerService.prepareTraceFile(false);     // create dir
71
72        Looper.prepare();
73
74        android.os.Process.setThreadPriority(
75                android.os.Process.THREAD_PRIORITY_FOREGROUND);
76
77        String factoryTestStr = SystemProperties.get("ro.factorytest");
78        int factoryTest = "".equals(factoryTestStr) ? SystemServer.FACTORY_TEST_OFF
79                : Integer.parseInt(factoryTestStr);
80
81        HardwareService hardware = null;
82        PowerManagerService power = null;
83        BatteryService battery = null;
84        ConnectivityService connectivity = null;
85        IPackageManager pm = null;
86        Context context = null;
87        WindowManagerService wm = null;
88        BluetoothService bluetooth = null;
89        BluetoothA2dpService bluetoothA2dp = null;
90        HeadsetObserver headset = null;
91        DockObserver dock = null;
92
93        // Critical services...
94        try {
95            Log.i(TAG, "Starting Entropy Service.");
96            ServiceManager.addService("entropy", new EntropyService());
97
98            Log.i(TAG, "Starting Power Manager.");
99            power = new PowerManagerService();
100            ServiceManager.addService(Context.POWER_SERVICE, power);
101
102            Log.i(TAG, "Starting Activity Manager.");
103            context = ActivityManagerService.main(factoryTest);
104
105            Log.i(TAG, "Starting telephony registry");
106            ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));
107
108            AttributeCache.init(context);
109
110            Log.i(TAG, "Starting Package Manager.");
111            pm = PackageManagerService.main(context,
112                    factoryTest != SystemServer.FACTORY_TEST_OFF);
113
114            ActivityManagerService.setSystemProcess();
115
116            mContentResolver = context.getContentResolver();
117
118            try {
119                Log.i(TAG, "Starting Account Manager.");
120                ServiceManager.addService(Context.ACCOUNT_SERVICE,
121                        new AccountManagerService(context));
122            } catch (Throwable e) {
123                Log.e(TAG, "Failure starting Account Manager", e);
124            }
125
126            Log.i(TAG, "Starting Content Manager.");
127            ContentService.main(context,
128                    factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
129
130            Log.i(TAG, "Starting System Content Providers.");
131            ActivityManagerService.installSystemProviders();
132
133            Log.i(TAG, "Starting Battery Service.");
134            battery = new BatteryService(context);
135            ServiceManager.addService("battery", battery);
136
137            Log.i(TAG, "Starting Hardware Service.");
138            hardware = new HardwareService(context);
139            ServiceManager.addService("hardware", hardware);
140
141            // only initialize the power service after we have started the
142            // hardware service, content providers and the battery service.
143            power.init(context, hardware, ActivityManagerService.getDefault(), battery);
144
145            Log.i(TAG, "Starting Alarm Manager.");
146            AlarmManagerService alarm = new AlarmManagerService(context);
147            ServiceManager.addService(Context.ALARM_SERVICE, alarm);
148
149            Watchdog.getInstance().init(context, battery, power, alarm,
150                    ActivityManagerService.self());
151
152            // Sensor Service is needed by Window Manager, so this goes first
153            Log.i(TAG, "Starting Sensor Service.");
154            ServiceManager.addService(Context.SENSOR_SERVICE, new SensorService(context));
155
156            Log.i(TAG, "Starting Window Manager.");
157            wm = WindowManagerService.main(context, power,
158                    factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
159            ServiceManager.addService(Context.WINDOW_SERVICE, wm);
160
161            ((ActivityManagerService)ServiceManager.getService("activity"))
162                    .setWindowManager(wm);
163
164            // Skip Bluetooth if we have an emulator kernel
165            // TODO: Use a more reliable check to see if this product should
166            // support Bluetooth - see bug 988521
167            if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
168                Log.i(TAG, "Registering null Bluetooth Service (emulator)");
169                ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
170            } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
171                Log.i(TAG, "Registering null Bluetooth Service (factory test)");
172                ServiceManager.addService(Context.BLUETOOTH_SERVICE, null);
173            } else {
174                Log.i(TAG, "Starting Bluetooth Service.");
175                bluetooth = new BluetoothService(context);
176                ServiceManager.addService(Context.BLUETOOTH_SERVICE, bluetooth);
177                bluetooth.initAfterRegistration();
178                bluetoothA2dp = new BluetoothA2dpService(context, bluetooth);
179                ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
180                                          bluetoothA2dp);
181
182                int bluetoothOn = Settings.Secure.getInt(mContentResolver,
183                    Settings.Secure.BLUETOOTH_ON, 0);
184                if (bluetoothOn > 0) {
185                    bluetooth.enable();
186                }
187            }
188
189        } catch (RuntimeException e) {
190            Log.e("System", "Failure starting core service", e);
191        }
192
193        StatusBarService statusBar = null;
194        InputMethodManagerService imm = null;
195        AppWidgetService appWidget = null;
196        NotificationManagerService notification = null;
197        WallpaperManagerService wallpaper = 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                connectivity = ConnectivityService.getInstance(context);
233                ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
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                wallpaper = new WallpaperManagerService(context);
305                ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);
306            } catch (Throwable e) {
307                Log.e(TAG, "Failure starting Wallpaper Service", e);
308            }
309
310            try {
311                Log.i(TAG, "Starting Audio Service");
312                ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
313            } catch (Throwable e) {
314                Log.e(TAG, "Failure starting Audio Service", e);
315            }
316
317            try {
318                Log.i(TAG, "Starting HeadsetObserver");
319                // Listen for wired headset changes
320                headset = new HeadsetObserver(context);
321            } catch (Throwable e) {
322                Log.e(TAG, "Failure starting HeadsetObserver", e);
323            }
324
325            try {
326                Log.i(TAG, "Starting DockObserver");
327                // Listen for dock station changes
328                dock = new DockObserver(context);
329            } catch (Throwable e) {
330                Log.e(TAG, "Failure starting DockObserver", e);
331            }
332
333            try {
334                Log.i(TAG, "Starting Backup Service");
335                ServiceManager.addService(Context.BACKUP_SERVICE, new BackupManagerService(context));
336            } catch (Throwable e) {
337                Log.e(TAG, "Failure starting Backup Service", e);
338            }
339
340            try {
341                Log.i(TAG, "Starting AppWidget Service");
342                appWidget = new AppWidgetService(context);
343                ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
344            } catch (Throwable e) {
345                Log.e(TAG, "Failure starting AppWidget Service", e);
346            }
347
348            try {
349                com.android.server.status.StatusBarPolicy.installIcons(context, statusBar);
350            } catch (Throwable e) {
351                Log.e(TAG, "Failure installing status bar icons", e);
352            }
353        }
354
355        // make sure the ADB_ENABLED setting value matches the secure property value
356        Settings.Secure.putInt(mContentResolver, Settings.Secure.ADB_ENABLED,
357                "1".equals(SystemProperties.get("persist.service.adb.enable")) ? 1 : 0);
358
359        // register observer to listen for settings changes
360        mContentResolver.registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.ADB_ENABLED),
361                false, new AdbSettingsObserver());
362
363        // It is now time to start up the app processes...
364        boolean safeMode = wm.detectSafeMode();
365
366        if (notification != null) {
367            notification.systemReady();
368        }
369
370        if (statusBar != null) {
371            statusBar.systemReady();
372        }
373        if (imm != null) {
374            imm.systemReady();
375        }
376        wm.systemReady();
377        power.systemReady();
378        try {
379            pm.systemReady();
380        } catch (RemoteException e) {
381        }
382        if (appWidget != null) {
383            appWidget.systemReady(safeMode);
384        }
385
386        // After making the following code, third party code may be running...
387        try {
388            ActivityManagerNative.getDefault().systemReady();
389        } catch (RemoteException e) {
390        }
391
392        if (wallpaper != null) wallpaper.systemReady();
393        if (battery != null) battery.systemReady();
394        if (connectivity != null) connectivity.systemReady();
395        Watchdog.getInstance().start();
396
397        Looper.loop();
398        Log.d(TAG, "System ServerThread is exiting!");
399    }
400}
401
402class DemoThread extends Thread
403{
404    DemoThread(Context context)
405    {
406        mContext = context;
407    }
408
409    @Override
410    public void run()
411    {
412        try {
413            Cursor c = mContext.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
414            boolean hasData = c != null && c.moveToFirst();
415            if (c != null) {
416                c.deactivate();
417            }
418            if (!hasData) {
419                DemoDataSet dataset = new DemoDataSet();
420                dataset.add(mContext);
421            }
422        } catch (Throwable e) {
423            Log.e("SystemServer", "Failure installing demo data", e);
424        }
425
426    }
427
428    Context mContext;
429}
430
431public class SystemServer
432{
433    private static final String TAG = "SystemServer";
434
435    public static final int FACTORY_TEST_OFF = 0;
436    public static final int FACTORY_TEST_LOW_LEVEL = 1;
437    public static final int FACTORY_TEST_HIGH_LEVEL = 2;
438
439    /**
440     * This method is called from Zygote to initialize the system. This will cause the native
441     * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
442     * up into init2() to start the Android services.
443     */
444    native public static void init1(String[] args);
445
446    public static void main(String[] args) {
447        // The system server has to run all of the time, so it needs to be
448        // as efficient as possible with its memory usage.
449        VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
450
451        System.loadLibrary("android_servers");
452        init1(args);
453    }
454
455    public static final void init2() {
456        Log.i(TAG, "Entered the Android system server!");
457        Thread thr = new ServerThread();
458        thr.setName("android.server.ServerThread");
459        thr.start();
460    }
461}
462