其他分享
首页 > 其他分享> > Android 9.0 CarService 分析

Android 9.0 CarService 分析

作者:互联网

一. CarService 启动流程:

1. 启动流程图

 2. SystemServer -> CarServiceHelperService -> CarService

 最终在 CarServiceHelperService 中通过 bindServiceAsUser 启动 CarService

frameworks/base/services/java/com/android/server/SystemServer.java
public final class SystemServer {
  private static final String CAR_SERVICE_HELPER_SERVICE_CLASS =
      "com.android.internal.car.CarServiceHelperService";
  private void run() {
    startBootstrapServices();
    startCoreServices();
    startOtherServices();
    SystemServerInitThreadPool.shutdown();
  }

  private void startOtherServices() {
    mActivityManagerService.systemReady(() -> {
      mSystemServiceManager.startBootPhase(
          SystemService.PHASE_ACTIVITY_MANAGER_READY);
      if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
        mSystemServiceManager.startService(CAR_SERVICE_HELPER_SERVICE_CLASS);
      }
    }
  }
}

frameworks/opt/car/services/src/com/android/internal/car/CarServiceHelperService.java
public class CarServiceHelperService extends SystemService {
  private static final String CAR_SERVICE_INTERFACE = "android.car.ICar";
  @Override
  public void onStart() {
    Intent intent = new Intent();
    intent.setPackage("com.android.car");
    intent.setAction(CAR_SERVICE_INTERFACE);
    getContext().bindServiceAsUser(intent, mCarServiceConnection, Context.BIND_AUTO_CREATE,
                UserHandle.SYSTEM)
    System.loadLibrary("car-framework-service-jni");
  }
}

 3. CarService -> ICarImpl -> Car 相关的 services

 通过 ICarImpl 实现

ackages/services/Car/service/src/com/android/car/CarService.java
package com.android.car;
public class CarService extends Service {
  private ICarImpl mICarImpl;
  private IVehicle mVehicle;
 
  public void onCreate() {
    mVehicle = getVehicle();
    mICarImpl = new ICarImpl(this,
                mVehicle,
                SystemInterface.Builder.defaultSystemInterface(this).build(),
                mCanBusErrorNotifier,
                mVehicleInterfaceName);
    mICarImpl.init();
    ServiceManager.addService("car_service", mICarImpl);
    super.onCreate();
  }
  
  @Override
  public IBinder onBind(Intent intent) {
    return mICarImpl;
  }
}

4. ICarImpl 启动 特定的 CarXXXServices,如 CarPowerManagementService/CarPropertyService 等

 

packages/services/Car/service/src/com/android/car/ICarImpl.java
public class ICarImpl extends ICar.Stub {
  private final CarPowerManagementService mCarPowerManagementService;
  private final CarPropertyService mCarPropertyService;
  private final CarServiceBase[] mAllServices;
 
  public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
            CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
    mHal = new VehicleHal(vehicle);
    mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),systemInterface);
    // mHal.getPropertyHal() -> PropertyHalService
    mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
    mCarAudioService = new CarAudioService(serviceContext, mCarPowerManagementService,
        mCarPropertyService);
    
    List<CarServiceBase> allServices = new ArrayList<>();
    allServices.add(mCarPowerManagementService);
    allServices.add(mCarPropertyService);
    allServices.add(mCarAudioService);
    mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
  }
  
  @Override
  public IBinder getCarService(String serviceName) {
    switch (serviceName) {
      case Car.CABIN_SERVICE:
      case Car.HVAC_SERVICE:
        return mCarPropertyService;
    }
  }
}

标签:SERVICE,ICarImpl,car,private,public,new,Android,9.0,CarService
来源: https://blog.csdn.net/SHK242673/article/details/122841640