IVICarServiceHelperService启动

Posted xhBruce

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IVICarServiceHelperService启动相关的知识,希望对你有一定的参考价值。

【IVI】CarServiceHelperService启动

android12-release
Android Automotive OS知识体系
Android 知识体系

1、SystemServer中在startOtherServices启动

  • mActivityManagerService.systemReady()里启动
  • 判断feature属性值PackageManager.FEATURE_AUTOMOTIVE是否支持(FEATURE_AUTOMOTIVE = “android.hardware.type.automotive”)
  • SystemServiceManager启动CAR_SERVICE_HELPER_SERVICE_CLASS = "com.android.internal.car.CarServiceHelperService",调用CarServiceHelperService构造函数、onStart()

frameworks/base/services/java/com/android/server/SystemServer.java

if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) 
    t.traceBegin("StartCarServiceHelperService");
    final SystemService cshs = mSystemServiceManager
            .startService(CAR_SERVICE_HELPER_SERVICE_CLASS);
    if (cshs instanceof Dumpable) 
        mDumper.addDumpable((Dumpable) cshs);
    
    if (cshs instanceof DevicePolicySafetyChecker) 
        dpms.setDevicePolicySafetyChecker((DevicePolicySafetyChecker) cshs);
    
    t.traceEnd();

2、CarServiceHelperService初始化

2.1 CarServiceHelperService构造函数

  • mHandler在HandlerThread处理事件
  • CarLaunchParamsModifier当Activity启动时控制管理Car显示。该政策只能影响乘客可以使用的显示器。当前用户可以始终启动任何显示。
  • CarWatchdogDaemonHelper car看门狗守护程序Watchdog的帮助程序类。
  • mCarServiceProxy管理CarServiceHelperService请求的CarService操作。它用于在连接、终止和重新连接时向CarService发送和重新发送活页夹调用。(packages/services/Car/service/src/com/android/car/CarService.java)
public CarServiceHelperService(Context context) 
    this(context,
            new CarLaunchParamsModifier(context),
            new CarWatchdogDaemonHelper(TAG),
            null
    );


@VisibleForTesting
CarServiceHelperService(
        Context context,
        CarLaunchParamsModifier carLaunchParamsModifier,
        CarWatchdogDaemonHelper carWatchdogDaemonHelper,
        CarServiceProxy carServiceOperationManager) 
    super(context);

    mContext = context;
    mHandlerThread.start();
    mHandler = new Handler(mHandlerThread.getLooper());
    mCarLaunchParamsModifier = carLaunchParamsModifier;
    mCarWatchdogDaemonHelper = carWatchdogDaemonHelper;
    mCarServiceProxy =
            carServiceOperationManager == null ? new CarServiceProxy(this)
                    : carServiceOperationManager;
    UserManagerInternal umi = LocalServices.getService(UserManagerInternal.class);
    if (umi != null) 
        umi.addUserLifecycleListener(new UserLifecycleListener() 
            @Override
            public void onUserCreated(UserInfo user, Object token) 
                if (DBG) Slogf.d(TAG, "onUserCreated(): %s", user.toFullString());
            
            @Override
            public void onUserRemoved(UserInfo user) 
                if (DBG) Slogf.d(TAG, "onUserRemoved(): $s", user.toFullString());
                mCarServiceProxy.onUserRemoved(user);
            
        );
     else 
        Slogf.e(TAG, "UserManagerInternal not available - should only happen on unit tests");
    
    mCarDevicePolicySafetyChecker = new CarDevicePolicySafetyChecker(this);

2.2 onStart()启动绑定CarService

  • mShutdownEventReceiver注册监听广播,通知car watchdog daemon改变StateType
  • mCarWatchdogDaemonHelper.connect()连接car watchdog daemon:ServiceManager.getService(CAR_WATCHDOG_DAEMON_INTERFACE),其中CAR_WATCHDOG_DAEMON_INTERFACE = "carwatchdogd_system"
  • mContext.bindServiceAsUser(intent, mCarServiceConnection, ...)绑定CarService
  • loadNativeLibrary() 导入jni:System.loadLibrary("car-framework-service-jni")
public void onStart() 
    EventLog.writeEvent(EventLogTags.CAR_HELPER_START);

    IntentFilter filter = new IntentFilter(Intent.ACTION_REBOOT);
    filter.addAction(Intent.ACTION_SHUTDOWN);
    mContext.registerReceiverForAllUsers(mShutdownEventReceiver, filter, null, null);
    mCarWatchdogDaemonHelper.addOnConnectionChangeListener(mConnectionListener);
    mCarWatchdogDaemonHelper.connect();
    Intent intent = new Intent();
    intent.setPackage("com.android.car");
    intent.setAction(CAR_SERVICE_INTERFACE);
    if (!mContext.bindServiceAsUser(intent, mCarServiceConnection, Context.BIND_AUTO_CREATE,
            mHandler, UserHandle.SYSTEM)) 
        Slogf.wtf(TAG, "cannot start car service");
    
    loadNativeLibrary();

3、绑定CarService回调mCarServiceConnection

  • 查看packages/services/Car/service/AndroidManifest.xml,绑定服务CarService服务
  • handleCarServiceConnection(iBinder)连接CarService,通过Parcel数据传递,ICarServiceHelperImpl、CarServiceConnectedCallback响应处理

public static final String CAR_SERVICE_INTERFACE = “android.car.ICar”

private final ServiceConnection mCarServiceConnection = new ServiceConnection() 
   @Override
   public void onServiceConnected(ComponentName componentName, IBinder iBinder) 
       if (DBG) 
           Slogf.d(TAG, "onServiceConnected: %s", iBinder);
       
       handleCarServiceConnection(iBinder);
   

   @Override
   public void onServiceDisconnected(ComponentName componentName) 
       handleCarServiceCrash();
   
;

4、简要时序图

以上是关于IVICarServiceHelperService启动的主要内容,如果未能解决你的问题,请参考以下文章