前言:
在上一篇文章中,介绍了术语和STR的意义,以及谷歌资料的位置。现在开始正式讲解。
名词不了解可以看上一篇 Android STR研究之一-CSDN博客
开机:
启动CarService
开机会启动SystemServer,SystemServer启动CarServiceHelperService,然后启动CarService
private static final String CAR_SERVICE_HELPER_SERVICE_CLASS =
"com.android.internal.car.CarServiceHelperService";
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
t.traceBegin("StartCarServiceHelperService");
mSystemServiceManager.startService(CAR_SERVICE_HELPER_SERVICE_CLASS);
t.traceEnd();
}
CarServiceHelperService启动CarService
public void onStart() {
EventLog.writeEvent(EventLogTags.CAR_HELPER_START, mHalEnabled ? 1 : 0);
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(ICarConstants.CAR_SERVICE_INTERFACE);
if (!mContext.bindServiceAsUser(intent, mCarServiceConnection, Context.BIND_AUTO_CREATE,
UserHandle.SYSTEM)) {
Slog.wtf(TAG, "cannot start car service");
}
loadNativeLibrary();
}
CarService 启动的时候会启动多个service,其中就包括CarPowerManagementService,还有CarPowerManagementService的"小伙伴"---->PowerHalService,电源状态核心的两个service
可以详见CarService的代码
mICarImpl = new ICarImpl(this,
mVehicle,
SystemInterface.Builder.defaultSystemInterface(this).build(),
mCanBusErrorNotifier,
mVehicleInterfaceName);
mICarImpl.init();
CarPowerManagementService启动
流程图:
1.初始化,会发送WAIT_FOR_VHAL
public void init() {
mHal.setListener(this);
if (mHal.isPowerStateSupported()) {
// Initialize CPMS in WAIT_FOR_VHAL state
onApPowerStateChange(CpmsState.WAIT_FOR_VHAL, CarPowerStateListener.WAIT_FOR_VHAL);
} else {
Slog.w(TAG, "Vehicle hal does not support power state yet.");
onApPowerStateChange(CpmsState.ON, CarPowerStateListener.ON);
}
mSystemInterface.startDisplayStateMonitoring(this);
}
发送WAIT_FOR_VHAL最终会执行handleWaitForVhal方法,给vhal发送这个状态信号(信号:VehicleProperty.AP_POWER_STATE_REPORT,信号值WAIT_FOR_VHAL)没有执行其他操作
2.接受到vhal返回的信号(信号:VehicleProperty.AP_POWER_STATE_REQ,值:VehicleApPowerStateReq.ON)
执行handleOn方法
private void handleOn() {
// If current user is a Guest User, we want to inform CarUserNoticeService not to show
// notice for current user, and show user notice only for the target user.
if (!mSwitchGuestUserBeforeSleep) {
updateCarUserNoticeServiceIfNecessary();
}
// Some OEMs have their own user-switching logic, which may not be coordinated with this
// code. To avoid contention, we don't switch users when we coming alive. The OEM's code
// should do the switch.
boolean allowUserSwitch = true;
synchronized (mLock) {
if (mIsBooting) {
// The system is booting, so don't switch users
allowUserSwitch = false;
mIsBooting = false;
mIsResuming = false;
Slog.i(TAG, "User switch disallowed while booting");
} else {
// The system is resuming after a suspension. Optionally disable user switching.
allowUserSwitch = !mDisableUserSwitchDuringResume;
mIsBooting = false;
mIsResuming = false;
if (!allowUserSwitch) {
Slog.i(TAG, "User switch disallowed while resuming");
}
}
}
//申请了wakelock,屏幕不会超时灭屏
mSystemInterface.setDisplayState(true);
//发送电源状态:On
sendPowerManagerEvent(CarPowerStateListener.ON);
//给vhal发送信号,状态On
mHal.sendOn();
try {
switchUserOnResumeIfNecessary(allowUserSwitch);
} catch (Exception e) {
Slog.e(TAG, "Could not switch user on resume", e);
}
setVoiceInteractionDisabled(false);
}
handlerOn的核心目的就是申请wakelock,让屏幕不会超时灭屏,并且发送电源状态通知。如果这里不申请wakelock的话,屏幕会超时灭屏,60秒(可以改settings里的屏幕亮持续时间修改)
到这里车机就屏幕长亮并且通知监听电源状态的app,当前电源状态是On。