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