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