欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android9:Car AutoMotive系统(Carservice 服务)开机流程

程序员文章站 2022-03-12 19:56:38
目的:学习并记录Android原生Car 系统。启动流程:(1)SystemServer.java 启动CarServiceHelperService服务frameworks/base/services/java/com/android/server/SystemServer.javaprivate static final String CAR_SERVICE_HELPER_SERVICE_CLASS = "com.android.internal.car.CarServi...

目的:学习并记录Android原生Car 系统。
启动流程:
(1)SystemServer.java 启动CarServiceHelperService服务
frameworks/base/services/java/com/android/server/SystemServer.java

private static final String CAR_SERVICE_HELPER_SERVICE_CLASS =
            "com.android.internal.car.CarServiceHelperService";
            
 public static void main(String[] args) {
        new SystemServer().run();
    }

private void run() {
 startOtherServices();
}

private void startOtherServices() {
 if(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
     traceBeginAndSlog("StartCarServiceHelperService");
     mSystemServiceManager.startService(CAR_SERVICE_HELPER_SERVICE_CLASS);
     traceEnd();
    }
}
    

(2)CarServiceHelperService.java ,绑定carservice服务 /frameworks/opt/car/services/src/com/android/internal/car/CarServiceHelperService.java

private final ServiceConnection mCarServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Slog.i(TAG, "**CarService connected**");
            mCarService = iBinder;
            // Cannot depend on ICar which is defined in CarService, so handle binder call directly
            // instead.
            // void setCarServiceHelper(in IBinder helper)
            Parcel data = Parcel.obtain();
            data.writeInterfaceToken(CAR_SERVICE_INTERFACE);
            data.writeStrongBinder(mHelper.asBinder());
            try {
                mCarService.transact(IBinder.FIRST_CALL_TRANSACTION, // setCarServiceHelper
                        data, null, Binder.FLAG_ONEWAY);
            } catch (RemoteException e) {
                Slog.w(TAG, "RemoteException from car service", e);
                handleCarServiceCrash();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            handleCarServiceCrash();
        }
    };
    
@Override
    public void onStart() {
        Intent intent = new Intent();
        //packages/services/Car/service/src/com/android/car/CarService.java
        //包名为com.android.car
        intent.setPackage("com.android.car");
        intent.setAction(CAR_SERVICE_INTERFACE);
        Slog.wtf(TAG, "liliadd:will start car service");
        if (!getContext().bindServiceAsUser(intent, mCarServiceConnection, Context.BIND_AUTO_CREATE,
                UserHandle.SYSTEM)) {
            Slog.wtf(TAG, "cannot start car service");
        }
        System.loadLibrary("car-framework-service-jni");
    }

(3)CarService.java ,创建ICarImpl实例,并调用init方法。
packages/services/Car/service/src/com/android/car/CarService.java

@Override
    public void onCreate() {
    mICarImpl = new ICarImpl(this,
                mVehicle,
                SystemInterface.Builder.defaultSystemInterface(this).build(),
                mCanBusErrorNotifier,
                mVehicleInterfaceName);
        mICarImpl.init();
    }

(4)ICarImpl.java 构造函数中启动服务。并对服务初始化
/packages/services/Car/service/src/com/android/car/ICarImpl.java

@MainThread
    void init() {
        traceBegin("VehicleHal.init");
        mHal.init();
        traceEnd();
        traceBegin("CarService.initAllServices");
        for (CarServiceBase service : mAllServices) {
            service.init();
        }
        traceEnd();
    }

调用对应服务得init方法。

注意:
1、CarService本质上是一个特殊的APP,它编译后生成CarService.apk;在真机上,它是在/system/priv-app/CarService/CarService.apk这里。
2、packages/services/Car/car_product/build/car.mk里面决定了是否编译相关apk。

参考文档:
https://blog.csdn.net/qilu0882/article/details/84990164
整理Android9 Car AutoMotive得启动流程。

本文地址:https://blog.csdn.net/zhuowalun8427/article/details/107190026