Home | History | Annotate | Download | only in server
      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 
     17 package com.android.server;
     18 
     19 import android.accounts.AccountManagerService;
     20 import android.app.ActivityManagerNative;
     21 import android.bluetooth.BluetoothAdapter;
     22 import android.content.ComponentName;
     23 import android.content.ContentResolver;
     24 import android.content.ContentService;
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.content.pm.IPackageManager;
     28 import android.content.res.Configuration;
     29 import android.media.AudioService;
     30 import android.net.wifi.p2p.WifiP2pService;
     31 import android.os.Looper;
     32 import android.os.RemoteException;
     33 import android.os.ServiceManager;
     34 import android.os.StrictMode;
     35 import android.os.SystemClock;
     36 import android.os.SystemProperties;
     37 import android.provider.Settings;
     38 import android.server.BluetoothA2dpService;
     39 import android.server.BluetoothService;
     40 import android.server.search.SearchManagerService;
     41 import android.util.DisplayMetrics;
     42 import android.util.EventLog;
     43 import android.util.Log;
     44 import android.util.Slog;
     45 import android.view.WindowManager;
     46 
     47 import com.android.internal.app.ShutdownThread;
     48 import com.android.internal.os.BinderInternal;
     49 import com.android.internal.os.SamplingProfilerIntegration;
     50 import com.android.server.accessibility.AccessibilityManagerService;
     51 import com.android.server.am.ActivityManagerService;
     52 import com.android.server.net.NetworkPolicyManagerService;
     53 import com.android.server.net.NetworkStatsService;
     54 import com.android.server.pm.PackageManagerService;
     55 import com.android.server.usb.UsbService;
     56 import com.android.server.wm.WindowManagerService;
     57 
     58 import dalvik.system.VMRuntime;
     59 import dalvik.system.Zygote;
     60 
     61 import java.io.File;
     62 import java.util.Timer;
     63 import java.util.TimerTask;
     64 
     65 class ServerThread extends Thread {
     66     private static final String TAG = "SystemServer";
     67     private static final String ENCRYPTING_STATE = "trigger_restart_min_framework";
     68     private static final String ENCRYPTED_STATE = "1";
     69 
     70     ContentResolver mContentResolver;
     71 
     72     void reportWtf(String msg, Throwable e) {
     73         Slog.w(TAG, "***********************************************");
     74         Log.wtf(TAG, "BOOT FAILURE " + msg, e);
     75     }
     76 
     77     @Override
     78     public void run() {
     79         EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN,
     80             SystemClock.uptimeMillis());
     81 
     82         Looper.prepare();
     83 
     84         android.os.Process.setThreadPriority(
     85                 android.os.Process.THREAD_PRIORITY_FOREGROUND);
     86 
     87         BinderInternal.disableBackgroundScheduling(true);
     88         android.os.Process.setCanSelfBackground(false);
     89 
     90         // Check whether we failed to shut down last time we tried.
     91         {
     92             final String shutdownAction = SystemProperties.get(
     93                     ShutdownThread.SHUTDOWN_ACTION_PROPERTY, "");
     94             if (shutdownAction != null && shutdownAction.length() > 0) {
     95                 boolean reboot = (shutdownAction.charAt(0) == '1');
     96 
     97                 final String reason;
     98                 if (shutdownAction.length() > 1) {
     99                     reason = shutdownAction.substring(1, shutdownAction.length());
    100                 } else {
    101                     reason = null;
    102                 }
    103 
    104                 ShutdownThread.rebootOrShutdown(reboot, reason);
    105             }
    106         }
    107 
    108         String factoryTestStr = SystemProperties.get("ro.factorytest");
    109         int factoryTest = "".equals(factoryTestStr) ? SystemServer.FACTORY_TEST_OFF
    110                 : Integer.parseInt(factoryTestStr);
    111 
    112         LightsService lights = null;
    113         PowerManagerService power = null;
    114         BatteryService battery = null;
    115         AlarmManagerService alarm = null;
    116         NetworkManagementService networkManagement = null;
    117         NetworkStatsService networkStats = null;
    118         NetworkPolicyManagerService networkPolicy = null;
    119         ConnectivityService connectivity = null;
    120         WifiP2pService wifiP2p = null;
    121         WifiService wifi = null;
    122         IPackageManager pm = null;
    123         Context context = null;
    124         WindowManagerService wm = null;
    125         BluetoothService bluetooth = null;
    126         BluetoothA2dpService bluetoothA2dp = null;
    127         DockObserver dock = null;
    128         UsbService usb = null;
    129         UiModeManagerService uiMode = null;
    130         RecognitionManagerService recognition = null;
    131         ThrottleService throttle = null;
    132         NetworkTimeUpdateService networkTimeUpdater = null;
    133 
    134         // Critical services...
    135         try {
    136             Slog.i(TAG, "Entropy Service");
    137             ServiceManager.addService("entropy", new EntropyService());
    138 
    139             Slog.i(TAG, "Power Manager");
    140             power = new PowerManagerService();
    141             ServiceManager.addService(Context.POWER_SERVICE, power);
    142 
    143             Slog.i(TAG, "Activity Manager");
    144             context = ActivityManagerService.main(factoryTest);
    145 
    146             Slog.i(TAG, "Telephony Registry");
    147             ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));
    148 
    149             AttributeCache.init(context);
    150 
    151             Slog.i(TAG, "Package Manager");
    152             // Only run "core" apps if we're encrypting the device.
    153             String cryptState = SystemProperties.get("vold.decrypt");
    154             boolean onlyCore = false;
    155             if (ENCRYPTING_STATE.equals(cryptState)) {
    156                 Slog.w(TAG, "Detected encryption in progress - only parsing core apps");
    157                 onlyCore = true;
    158             } else if (ENCRYPTED_STATE.equals(cryptState)) {
    159                 Slog.w(TAG, "Device encrypted - only parsing core apps");
    160                 onlyCore = true;
    161             }
    162 
    163             pm = PackageManagerService.main(context,
    164                     factoryTest != SystemServer.FACTORY_TEST_OFF,
    165                     onlyCore);
    166             boolean firstBoot = false;
    167             try {
    168                 firstBoot = pm.isFirstBoot();
    169             } catch (RemoteException e) {
    170             }
    171 
    172             ActivityManagerService.setSystemProcess();
    173 
    174             mContentResolver = context.getContentResolver();
    175 
    176             // The AccountManager must come before the ContentService
    177             try {
    178                 Slog.i(TAG, "Account Manager");
    179                 ServiceManager.addService(Context.ACCOUNT_SERVICE,
    180                         new AccountManagerService(context));
    181             } catch (Throwable e) {
    182                 Slog.e(TAG, "Failure starting Account Manager", e);
    183             }
    184 
    185             Slog.i(TAG, "Content Manager");
    186             ContentService.main(context,
    187                     factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
    188 
    189             Slog.i(TAG, "System Content Providers");
    190             ActivityManagerService.installSystemProviders();
    191 
    192             Slog.i(TAG, "Lights Service");
    193             lights = new LightsService(context);
    194 
    195             Slog.i(TAG, "Battery Service");
    196             battery = new BatteryService(context, lights);
    197             ServiceManager.addService("battery", battery);
    198 
    199             Slog.i(TAG, "Vibrator Service");
    200             ServiceManager.addService("vibrator", new VibratorService(context));
    201 
    202             // only initialize the power service after we have started the
    203             // lights service, content providers and the battery service.
    204             power.init(context, lights, ActivityManagerService.self(), battery);
    205 
    206             Slog.i(TAG, "Alarm Manager");
    207             alarm = new AlarmManagerService(context);
    208             ServiceManager.addService(Context.ALARM_SERVICE, alarm);
    209 
    210             Slog.i(TAG, "Init Watchdog");
    211             Watchdog.getInstance().init(context, battery, power, alarm,
    212                     ActivityManagerService.self());
    213 
    214             Slog.i(TAG, "Window Manager");
    215             wm = WindowManagerService.main(context, power,
    216                     factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL,
    217                     !firstBoot);
    218             ServiceManager.addService(Context.WINDOW_SERVICE, wm);
    219 
    220             ActivityManagerService.self().setWindowManager(wm);
    221 
    222             // Skip Bluetooth if we have an emulator kernel
    223             // TODO: Use a more reliable check to see if this product should
    224             // support Bluetooth - see bug 988521
    225             if (SystemProperties.get("ro.kernel.qemu").equals("1")) {
    226                 Slog.i(TAG, "No Bluetooh Service (emulator)");
    227             } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
    228                 Slog.i(TAG, "No Bluetooth Service (factory test)");
    229             } else {
    230                 Slog.i(TAG, "Bluetooth Service");
    231                 bluetooth = new BluetoothService(context);
    232                 ServiceManager.addService(BluetoothAdapter.BLUETOOTH_SERVICE, bluetooth);
    233                 bluetooth.initAfterRegistration();
    234                 bluetoothA2dp = new BluetoothA2dpService(context, bluetooth);
    235                 ServiceManager.addService(BluetoothA2dpService.BLUETOOTH_A2DP_SERVICE,
    236                                           bluetoothA2dp);
    237                 bluetooth.initAfterA2dpRegistration();
    238 
    239                 int airplaneModeOn = Settings.System.getInt(mContentResolver,
    240                         Settings.System.AIRPLANE_MODE_ON, 0);
    241                 int bluetoothOn = Settings.Secure.getInt(mContentResolver,
    242                     Settings.Secure.BLUETOOTH_ON, 0);
    243                 if (airplaneModeOn == 0 && bluetoothOn != 0) {
    244                     bluetooth.enable();
    245                 }
    246             }
    247 
    248         } catch (RuntimeException e) {
    249             Slog.e("System", "******************************************");
    250             Slog.e("System", "************ Failure starting core service", e);
    251         }
    252 
    253         DevicePolicyManagerService devicePolicy = null;
    254         StatusBarManagerService statusBar = null;
    255         InputMethodManagerService imm = null;
    256         AppWidgetService appWidget = null;
    257         NotificationManagerService notification = null;
    258         WallpaperManagerService wallpaper = null;
    259         LocationManagerService location = null;
    260         CountryDetectorService countryDetector = null;
    261         TextServicesManagerService tsms = null;
    262 
    263         // Bring up services needed for UI.
    264         if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
    265             try {
    266                 Slog.i(TAG, "Input Method Service");
    267                 imm = new InputMethodManagerService(context);
    268                 ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
    269             } catch (Throwable e) {
    270                 reportWtf("starting Input Manager Service", e);
    271             }
    272 
    273             try {
    274                 Slog.i(TAG, "Accessibility Manager");
    275                 ServiceManager.addService(Context.ACCESSIBILITY_SERVICE,
    276                         new AccessibilityManagerService(context));
    277             } catch (Throwable e) {
    278                 reportWtf("starting Accessibility Manager", e);
    279             }
    280         }
    281 
    282         try {
    283             wm.displayReady();
    284         } catch (Throwable e) {
    285             reportWtf("making display ready", e);
    286         }
    287 
    288         try {
    289             pm.performBootDexOpt();
    290         } catch (Throwable e) {
    291             reportWtf("performing boot dexopt", e);
    292         }
    293 
    294         try {
    295             ActivityManagerNative.getDefault().showBootMessage(
    296                     context.getResources().getText(
    297                             com.android.internal.R.string.android_upgrading_starting_apps),
    298                             false);
    299         } catch (RemoteException e) {
    300         }
    301 
    302         if (factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
    303             try {
    304                 Slog.i(TAG, "Device Policy");
    305                 devicePolicy = new DevicePolicyManagerService(context);
    306                 ServiceManager.addService(Context.DEVICE_POLICY_SERVICE, devicePolicy);
    307             } catch (Throwable e) {
    308                 reportWtf("starting DevicePolicyService", e);
    309             }
    310 
    311             try {
    312                 Slog.i(TAG, "Status Bar");
    313                 statusBar = new StatusBarManagerService(context, wm);
    314                 ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);
    315             } catch (Throwable e) {
    316                 reportWtf("starting StatusBarManagerService", e);
    317             }
    318 
    319             try {
    320                 Slog.i(TAG, "Clipboard Service");
    321                 ServiceManager.addService(Context.CLIPBOARD_SERVICE,
    322                         new ClipboardService(context));
    323             } catch (Throwable e) {
    324                 reportWtf("starting Clipboard Service", e);
    325             }
    326 
    327             try {
    328                 Slog.i(TAG, "NetworkManagement Service");
    329                 networkManagement = NetworkManagementService.create(context);
    330                 ServiceManager.addService(Context.NETWORKMANAGEMENT_SERVICE, networkManagement);
    331             } catch (Throwable e) {
    332                 reportWtf("starting NetworkManagement Service", e);
    333             }
    334 
    335             try {
    336                 Slog.i(TAG, "Text Service Manager Service");
    337                 tsms = new TextServicesManagerService(context);
    338                 ServiceManager.addService(Context.TEXT_SERVICES_MANAGER_SERVICE, tsms);
    339             } catch (Throwable e) {
    340                 reportWtf("starting Text Service Manager Service", e);
    341             }
    342 
    343             try {
    344                 Slog.i(TAG, "NetworkStats Service");
    345                 networkStats = new NetworkStatsService(context, networkManagement, alarm);
    346                 ServiceManager.addService(Context.NETWORK_STATS_SERVICE, networkStats);
    347             } catch (Throwable e) {
    348                 reportWtf("starting NetworkStats Service", e);
    349             }
    350 
    351             try {
    352                 Slog.i(TAG, "NetworkPolicy Service");
    353                 networkPolicy = new NetworkPolicyManagerService(
    354                         context, ActivityManagerService.self(), power,
    355                         networkStats, networkManagement);
    356                 ServiceManager.addService(Context.NETWORK_POLICY_SERVICE, networkPolicy);
    357             } catch (Throwable e) {
    358                 reportWtf("starting NetworkPolicy Service", e);
    359             }
    360 
    361            try {
    362                 Slog.i(TAG, "Wi-Fi P2pService");
    363                 wifiP2p = new WifiP2pService(context);
    364                 ServiceManager.addService(Context.WIFI_P2P_SERVICE, wifiP2p);
    365             } catch (Throwable e) {
    366                 reportWtf("starting Wi-Fi P2pService", e);
    367             }
    368 
    369            try {
    370                 Slog.i(TAG, "Wi-Fi Service");
    371                 wifi = new WifiService(context);
    372                 ServiceManager.addService(Context.WIFI_SERVICE, wifi);
    373             } catch (Throwable e) {
    374                 reportWtf("starting Wi-Fi Service", e);
    375             }
    376 
    377             try {
    378                 Slog.i(TAG, "Connectivity Service");
    379                 connectivity = new ConnectivityService(
    380                         context, networkManagement, networkStats, networkPolicy);
    381                 ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
    382                 networkStats.bindConnectivityManager(connectivity);
    383                 networkPolicy.bindConnectivityManager(connectivity);
    384                 wifi.checkAndStartWifi();
    385                 wifiP2p.connectivityServiceReady();
    386             } catch (Throwable e) {
    387                 reportWtf("starting Connectivity Service", e);
    388             }
    389 
    390             try {
    391                 Slog.i(TAG, "Throttle Service");
    392                 throttle = new ThrottleService(context);
    393                 ServiceManager.addService(
    394                         Context.THROTTLE_SERVICE, throttle);
    395             } catch (Throwable e) {
    396                 reportWtf("starting ThrottleService", e);
    397             }
    398 
    399             try {
    400                 /*
    401                  * NotificationManagerService is dependant on MountService,
    402                  * (for media / usb notifications) so we must start MountService first.
    403                  */
    404                 Slog.i(TAG, "Mount Service");
    405                 ServiceManager.addService("mount", new MountService(context));
    406             } catch (Throwable e) {
    407                 reportWtf("starting Mount Service", e);
    408             }
    409 
    410             try {
    411                 Slog.i(TAG, "Notification Manager");
    412                 notification = new NotificationManagerService(context, statusBar, lights);
    413                 ServiceManager.addService(Context.NOTIFICATION_SERVICE, notification);
    414                 networkPolicy.bindNotificationManager(notification);
    415             } catch (Throwable e) {
    416                 reportWtf("starting Notification Manager", e);
    417             }
    418 
    419             try {
    420                 Slog.i(TAG, "Device Storage Monitor");
    421                 ServiceManager.addService(DeviceStorageMonitorService.SERVICE,
    422                         new DeviceStorageMonitorService(context));
    423             } catch (Throwable e) {
    424                 reportWtf("starting DeviceStorageMonitor service", e);
    425             }
    426 
    427             try {
    428                 Slog.i(TAG, "Location Manager");
    429                 location = new LocationManagerService(context);
    430                 ServiceManager.addService(Context.LOCATION_SERVICE, location);
    431             } catch (Throwable e) {
    432                 reportWtf("starting Location Manager", e);
    433             }
    434 
    435             try {
    436                 Slog.i(TAG, "Country Detector");
    437                 countryDetector = new CountryDetectorService(context);
    438                 ServiceManager.addService(Context.COUNTRY_DETECTOR, countryDetector);
    439             } catch (Throwable e) {
    440                 reportWtf("starting Country Detector", e);
    441             }
    442 
    443             try {
    444                 Slog.i(TAG, "Search Service");
    445                 ServiceManager.addService(Context.SEARCH_SERVICE,
    446                         new SearchManagerService(context));
    447             } catch (Throwable e) {
    448                 reportWtf("starting Search Service", e);
    449             }
    450 
    451             try {
    452                 Slog.i(TAG, "DropBox Service");
    453                 ServiceManager.addService(Context.DROPBOX_SERVICE,
    454                         new DropBoxManagerService(context, new File("/data/system/dropbox")));
    455             } catch (Throwable e) {
    456                 reportWtf("starting DropBoxManagerService", e);
    457             }
    458 
    459             try {
    460                 Slog.i(TAG, "Wallpaper Service");
    461                 wallpaper = new WallpaperManagerService(context);
    462                 ServiceManager.addService(Context.WALLPAPER_SERVICE, wallpaper);
    463             } catch (Throwable e) {
    464                 reportWtf("starting Wallpaper Service", e);
    465             }
    466 
    467             try {
    468                 Slog.i(TAG, "Audio Service");
    469                 ServiceManager.addService(Context.AUDIO_SERVICE, new AudioService(context));
    470             } catch (Throwable e) {
    471                 reportWtf("starting Audio Service", e);
    472             }
    473 
    474             try {
    475                 Slog.i(TAG, "Dock Observer");
    476                 // Listen for dock station changes
    477                 dock = new DockObserver(context, power);
    478             } catch (Throwable e) {
    479                 reportWtf("starting DockObserver", e);
    480             }
    481 
    482             try {
    483                 Slog.i(TAG, "Wired Accessory Observer");
    484                 // Listen for wired headset changes
    485                 new WiredAccessoryObserver(context);
    486             } catch (Throwable e) {
    487                 reportWtf("starting WiredAccessoryObserver", e);
    488             }
    489 
    490             try {
    491                 Slog.i(TAG, "USB Service");
    492                 // Manage USB host and device support
    493                 usb = new UsbService(context);
    494                 ServiceManager.addService(Context.USB_SERVICE, usb);
    495             } catch (Throwable e) {
    496                 reportWtf("starting UsbService", e);
    497             }
    498 
    499             try {
    500                 Slog.i(TAG, "UI Mode Manager Service");
    501                 // Listen for UI mode changes
    502                 uiMode = new UiModeManagerService(context);
    503             } catch (Throwable e) {
    504                 reportWtf("starting UiModeManagerService", e);
    505             }
    506 
    507             try {
    508                 Slog.i(TAG, "Backup Service");
    509                 ServiceManager.addService(Context.BACKUP_SERVICE,
    510                         new BackupManagerService(context));
    511             } catch (Throwable e) {
    512                 Slog.e(TAG, "Failure starting Backup Service", e);
    513             }
    514 
    515             try {
    516                 Slog.i(TAG, "AppWidget Service");
    517                 appWidget = new AppWidgetService(context);
    518                 ServiceManager.addService(Context.APPWIDGET_SERVICE, appWidget);
    519             } catch (Throwable e) {
    520                 reportWtf("starting AppWidget Service", e);
    521             }
    522 
    523             try {
    524                 Slog.i(TAG, "Recognition Service");
    525                 recognition = new RecognitionManagerService(context);
    526             } catch (Throwable e) {
    527                 reportWtf("starting Recognition Service", e);
    528             }
    529 
    530             try {
    531                 Slog.i(TAG, "DiskStats Service");
    532                 ServiceManager.addService("diskstats", new DiskStatsService(context));
    533             } catch (Throwable e) {
    534                 reportWtf("starting DiskStats Service", e);
    535             }
    536 
    537             try {
    538                 // need to add this service even if SamplingProfilerIntegration.isEnabled()
    539                 // is false, because it is this service that detects system property change and
    540                 // turns on SamplingProfilerIntegration. Plus, when sampling profiler doesn't work,
    541                 // there is little overhead for running this service.
    542                 Slog.i(TAG, "SamplingProfiler Service");
    543                 ServiceManager.addService("samplingprofiler",
    544                             new SamplingProfilerService(context));
    545             } catch (Throwable e) {
    546                 reportWtf("starting SamplingProfiler Service", e);
    547             }
    548 
    549             try {
    550                 Slog.i(TAG, "NetworkTimeUpdateService");
    551                 networkTimeUpdater = new NetworkTimeUpdateService(context);
    552             } catch (Throwable e) {
    553                 reportWtf("starting NetworkTimeUpdate service", e);
    554             }
    555         }
    556 
    557         // Before things start rolling, be sure we have decided whether
    558         // we are in safe mode.
    559         final boolean safeMode = wm.detectSafeMode();
    560         if (safeMode) {
    561             ActivityManagerService.self().enterSafeMode();
    562             // Post the safe mode state in the Zygote class
    563             Zygote.systemInSafeMode = true;
    564             // Disable the JIT for the system_server process
    565             VMRuntime.getRuntime().disableJitCompilation();
    566         } else {
    567             // Enable the JIT for the system_server process
    568             VMRuntime.getRuntime().startJitCompilation();
    569         }
    570 
    571         // It is now time to start up the app processes...
    572 
    573         if (devicePolicy != null) {
    574             try {
    575                 devicePolicy.systemReady();
    576             } catch (Throwable e) {
    577                 reportWtf("making Device Policy Service ready", e);
    578             }
    579         }
    580 
    581         if (notification != null) {
    582             try {
    583                 notification.systemReady();
    584             } catch (Throwable e) {
    585                 reportWtf("making Notification Service ready", e);
    586             }
    587         }
    588 
    589         try {
    590             wm.systemReady();
    591         } catch (Throwable e) {
    592             reportWtf("making Window Manager Service ready", e);
    593         }
    594 
    595         if (safeMode) {
    596             ActivityManagerService.self().showSafeModeOverlay();
    597         }
    598 
    599         // Update the configuration for this context by hand, because we're going
    600         // to start using it before the config change done in wm.systemReady() will
    601         // propagate to it.
    602         Configuration config = wm.computeNewConfiguration();
    603         DisplayMetrics metrics = new DisplayMetrics();
    604         WindowManager w = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
    605         w.getDefaultDisplay().getMetrics(metrics);
    606         context.getResources().updateConfiguration(config, metrics);
    607 
    608         power.systemReady();
    609         try {
    610             pm.systemReady();
    611         } catch (Throwable e) {
    612             reportWtf("making Package Manager Service ready", e);
    613         }
    614 
    615         // These are needed to propagate to the runnable below.
    616         final Context contextF = context;
    617         final BatteryService batteryF = battery;
    618         final NetworkManagementService networkManagementF = networkManagement;
    619         final NetworkStatsService networkStatsF = networkStats;
    620         final NetworkPolicyManagerService networkPolicyF = networkPolicy;
    621         final ConnectivityService connectivityF = connectivity;
    622         final DockObserver dockF = dock;
    623         final UsbService usbF = usb;
    624         final ThrottleService throttleF = throttle;
    625         final UiModeManagerService uiModeF = uiMode;
    626         final AppWidgetService appWidgetF = appWidget;
    627         final WallpaperManagerService wallpaperF = wallpaper;
    628         final InputMethodManagerService immF = imm;
    629         final RecognitionManagerService recognitionF = recognition;
    630         final LocationManagerService locationF = location;
    631         final CountryDetectorService countryDetectorF = countryDetector;
    632         final NetworkTimeUpdateService networkTimeUpdaterF = networkTimeUpdater;
    633         final TextServicesManagerService textServiceManagerServiceF = tsms;
    634         final StatusBarManagerService statusBarF = statusBar;
    635 
    636         // We now tell the activity manager it is okay to run third party
    637         // code.  It will call back into us once it has gotten to the state
    638         // where third party code can really run (but before it has actually
    639         // started launching the initial applications), for us to complete our
    640         // initialization.
    641         ActivityManagerService.self().systemReady(new Runnable() {
    642             public void run() {
    643                 Slog.i(TAG, "Making services ready");
    644 
    645                 startSystemUi(contextF);
    646                 try {
    647                     if (batteryF != null) batteryF.systemReady();
    648                 } catch (Throwable e) {
    649                     reportWtf("making Battery Service ready", e);
    650                 }
    651                 try {
    652                     if (networkManagementF != null) networkManagementF.systemReady();
    653                 } catch (Throwable e) {
    654                     reportWtf("making Network Managment Service ready", e);
    655                 }
    656                 try {
    657                     if (networkStatsF != null) networkStatsF.systemReady();
    658                 } catch (Throwable e) {
    659                     reportWtf("making Network Stats Service ready", e);
    660                 }
    661                 try {
    662                     if (networkPolicyF != null) networkPolicyF.systemReady();
    663                 } catch (Throwable e) {
    664                     reportWtf("making Network Policy Service ready", e);
    665                 }
    666                 try {
    667                     if (connectivityF != null) connectivityF.systemReady();
    668                 } catch (Throwable e) {
    669                     reportWtf("making Connectivity Service ready", e);
    670                 }
    671                 try {
    672                     if (dockF != null) dockF.systemReady();
    673                 } catch (Throwable e) {
    674                     reportWtf("making Dock Service ready", e);
    675                 }
    676                 try {
    677                     if (usbF != null) usbF.systemReady();
    678                 } catch (Throwable e) {
    679                     reportWtf("making USB Service ready", e);
    680                 }
    681                 try {
    682                     if (uiModeF != null) uiModeF.systemReady();
    683                 } catch (Throwable e) {
    684                     reportWtf("making UI Mode Service ready", e);
    685                 }
    686                 try {
    687                     if (recognitionF != null) recognitionF.systemReady();
    688                 } catch (Throwable e) {
    689                     reportWtf("making Recognition Service ready", e);
    690                 }
    691                 Watchdog.getInstance().start();
    692 
    693                 // It is now okay to let the various system services start their
    694                 // third party code...
    695 
    696                 try {
    697                     if (appWidgetF != null) appWidgetF.systemReady(safeMode);
    698                 } catch (Throwable e) {
    699                     reportWtf("making App Widget Service ready", e);
    700                 }
    701                 try {
    702                     if (wallpaperF != null) wallpaperF.systemReady();
    703                 } catch (Throwable e) {
    704                     reportWtf("making Wallpaper Service ready", e);
    705                 }
    706                 try {
    707                     if (immF != null) immF.systemReady(statusBarF);
    708                 } catch (Throwable e) {
    709                     reportWtf("making Input Method Service ready", e);
    710                 }
    711                 try {
    712                     if (locationF != null) locationF.systemReady();
    713                 } catch (Throwable e) {
    714                     reportWtf("making Location Service ready", e);
    715                 }
    716                 try {
    717                     if (countryDetectorF != null) countryDetectorF.systemReady();
    718                 } catch (Throwable e) {
    719                     reportWtf("making Country Detector Service ready", e);
    720                 }
    721                 try {
    722                     if (throttleF != null) throttleF.systemReady();
    723                 } catch (Throwable e) {
    724                     reportWtf("making Throttle Service ready", e);
    725                 }
    726                 try {
    727                     if (networkTimeUpdaterF != null) networkTimeUpdaterF.systemReady();
    728                 } catch (Throwable e) {
    729                     reportWtf("making Network Time Service ready", e);
    730                 }
    731                 try {
    732                     if (textServiceManagerServiceF != null) textServiceManagerServiceF.systemReady();
    733                 } catch (Throwable e) {
    734                     reportWtf("making Text Services Manager Service ready", e);
    735                 }
    736             }
    737         });
    738 
    739         // For debug builds, log event loop stalls to dropbox for analysis.
    740         if (StrictMode.conditionallyEnableDebugLogging()) {
    741             Slog.i(TAG, "Enabled StrictMode for system server main thread.");
    742         }
    743 
    744         Looper.loop();
    745         Slog.d(TAG, "System ServerThread is exiting!");
    746     }
    747 
    748     static final void startSystemUi(Context context) {
    749         Intent intent = new Intent();
    750         intent.setComponent(new ComponentName("com.android.systemui",
    751                     "com.android.systemui.SystemUIService"));
    752         Slog.d(TAG, "Starting service: " + intent);
    753         context.startService(intent);
    754     }
    755 }
    756 
    757 public class SystemServer {
    758     private static final String TAG = "SystemServer";
    759 
    760     public static final int FACTORY_TEST_OFF = 0;
    761     public static final int FACTORY_TEST_LOW_LEVEL = 1;
    762     public static final int FACTORY_TEST_HIGH_LEVEL = 2;
    763 
    764     static Timer timer;
    765     static final long SNAPSHOT_INTERVAL = 60 * 60 * 1000; // 1hr
    766 
    767     // The earliest supported time.  We pick one day into 1970, to
    768     // give any timezone code room without going into negative time.
    769     private static final long EARLIEST_SUPPORTED_TIME = 86400 * 1000;
    770 
    771     /**
    772      * This method is called from Zygote to initialize the system. This will cause the native
    773      * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back
    774      * up into init2() to start the Android services.
    775      */
    776     native public static void init1(String[] args);
    777 
    778     public static void main(String[] args) {
    779         if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
    780             // If a device's clock is before 1970 (before 0), a lot of
    781             // APIs crash dealing with negative numbers, notably
    782             // java.io.File#setLastModified, so instead we fake it and
    783             // hope that time from cell towers or NTP fixes it
    784             // shortly.
    785             Slog.w(TAG, "System clock is before 1970; setting to 1970.");
    786             SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
    787         }
    788 
    789         if (SamplingProfilerIntegration.isEnabled()) {
    790             SamplingProfilerIntegration.start();
    791             timer = new Timer();
    792             timer.schedule(new TimerTask() {
    793                 @Override
    794                 public void run() {
    795                     SamplingProfilerIntegration.writeSnapshot("system_server", null);
    796                 }
    797             }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
    798         }
    799 
    800         // Mmmmmm... more memory!
    801         dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
    802 
    803         // The system server has to run all of the time, so it needs to be
    804         // as efficient as possible with its memory usage.
    805         VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
    806 
    807         System.loadLibrary("android_servers");
    808         init1(args);
    809     }
    810 
    811     public static final void init2() {
    812         Slog.i(TAG, "Entered the Android system server!");
    813         Thread thr = new ServerThread();
    814         thr.setName("android.server.ServerThread");
    815         thr.start();
    816     }
    817 }
    818