【Android】Android Framework系列---CarPower电源管理

Android Framework系列—CarPower电源管理

智能座舱通常包括中控系统、仪表系统、IVI系统 、后排娱乐、HUD、车联网等。这些系统需要由汽车电源进行供电。由于汽车自身的特殊供电环境(相比手机方便的充电环境,汽车的蓄电池如果没有电是需要专业人士操作的),其电源状态会比较复杂,既要满足车内的座舱系统启动足够快,又要保证汽车蓄电池的可考性,所以出了开机(on)、关机(off)外,会多出来一些电源状态(Suspend、STR、SLEEP等等)

所以,一般来说软件系统是需要一个专门的电源管理模块的。

CarPower电源管理

Android Automotive(基于Android平台的车载信息娱乐系统)提供了CarPower模块用于管理电源状态。CarPower Service属于CarService,在SystemSever的startOtherService阶段启动。
【Android】Android Framework系列---CarPower电源管理_第1张图片
CarPower Service根据来自于 Vehicle HAL(Hal层服务)的电源状态通知,进行相关的逻辑判断后,应用电源策略(简单理解成告知某些服务Enable 或者Disable)、发送状态通知给它自身的监听者、回复(Report)VehcileHAL处理结果。
Vehicle HAL的电源状态一般来讲是来自于MCU的(can通信 或者以太网通信,具体形式由Vehicle HAL和相关供应商决定)。电源状态变化时,MCU一般会拉低或者拉高某个引脚,然后通知事件给VehicleHAL,VehicleHAL再通过PropertyEvent通知给CarPower Service。Android系统处理完电源状态后(有超时要求),再由VehiclHAL告知MCU,MCU在对某个引脚进行相关操作。
【Android】Android Framework系列---CarPower电源管理_第2张图片

CarPower电源管理的开机流程

下面以Android车载系统开机为例。说明一下CarPower模块的开机时序。代码基于Android12.

  • CarPower服务启动:Android系统开机上电,在systemServer中启动CarService。
// frameworks/base/services/java/com/android/server/SystemServer.java

/**
 * Starts a miscellaneous grab bag of stuff that has yet to be refactored and organized.
 */
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
            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();
            }

}

// frameworks/opt/car/services/src/com/android/internal/car/CarServiceHelperService.java
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();
	// 启动了CarService------------------------------------------
	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启动后会创建CarPower实例。

<application android:label="@string/app_title"
			 android:directBootAware="true"
			 android:allowBackup="false"
			 android:persistent="true">

	<service android:name=".CarService"
			android:singleUser="true">
		<intent-filter>
			<action android:name="android.car.ICar" />
		intent-filter>
	service>

// packages/services/Car/service/src/com/android/car/ICarImpl.java
public class ICarImpl extends ICar.Stub {

   public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
            CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
        this(serviceContext, vehicle, systemInterface, errorNotifier, vehicleInterfaceName,
                /* carUserService= */ null, /* carWatchdogService= */ null);
    }

    @VisibleForTesting
    ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
            CanBusErrorNotifier errorNotifier, String vehicleInterfaceName,
            @Nullable CarUserService carUserService,
            @Nullable CarWatchdogService carWatchdogService) {
        mContext = serviceContext;
        mSystemInterface = systemInterface;
        mHal = new VehicleHal(serviceContext, vehicle);
        // Do this before any other service components to allow feature check. It should work
        // even without init. For that, vhal get is retried as it can be too early.
        VehiclePropValue disabledOptionalFeatureValue = mHal.getIfAvailableOrFailForEarlyStage(
                    VehicleProperty.DISABLED_OPTIONAL_FEATURES, INITIAL_VHAL_GET_RETRY);
        String[] disabledFeaturesFromVhal = null;
        if (disabledOptionalFeatureValue != null) {
            String disabledFeatures = disabledOptionalFeatureValue.value.stringValue;
            if (disabledFeatures != null && !disabledFeatures.isEmpty()) {
                disabledFeaturesFromVhal = disabledFeatures.split(",");
            }
        }
        if (disabledFeaturesFromVhal == null) {
            disabledFeaturesFromVhal = new String[0];
        }
        Resources res = mContext.getResources();
        String[] defaultEnabledFeatures = res.getStringArray(
                R.array.config_allowed_optional_car_features);
        mFeatureController = new CarFeatureController(serviceContext, defaultEnabledFeatures,
                disabledFeaturesFromVhal , mSystemInterface.getSystemCarDir());
        CarLocalServices.addService(CarFeatureController.class, mFeatureController);
        mVehicleInterfaceName = vehicleInterfaceName;
        mUserManagerHelper = new CarUserManagerHelper(serviceContext);
        if (carUserService != null) {
            mCarUserService = carUserService;
        } else {
            UserManager userManager =
                    (UserManager) serviceContext.getSystemService(Context.USER_SERVICE);
            int maxRunningUsers = res.getInteger(
                    com.android.internal.R.integer.config_multiuserMaxRunningUsers);
            mCarUserService = new CarUserService(serviceContext, mHal.getUserHal(),
                    mUserManagerHelper, userManager, ActivityManager.getService(), maxRunningUsers);
        }
        mCarOccupantZoneService = new CarOccupantZoneService(serviceContext);
        mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
		// 这里创建了CarPower服务实例-----------------------------------------------------------
        mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
                systemInterface, mCarUserService);
        if (mFeatureController.isFeatureEnabled(CarFeatures.FEATURE_CAR_USER_NOTICE_SERVICE)) {
            mCarUserNoticeService = new CarUserNoticeService(serviceContext);
        } else {
            mCarUserNoticeService = null;
        }
        mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
        mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
        mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
                mCarDrivingStateService, mCarPropertyService);
        if (mFeatureController.isFeatureEnabled(Car.OCCUPANT_AWARENESS_SERVICE)) {
            mOccupantAwarenessService = new OccupantAwarenessService(serviceContext);
        } else {
            mOccupantAwarenessService = null;
        }
        mCarPackageManagerService = new CarPackageManagerService(serviceContext,
                mCarUXRestrictionsService,
                mSystemActivityMonitoringService,
                mCarUserService);
        mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext, mCarUserService);
        mCarBluetoothService = new CarBluetoothService(serviceContext, mPerUserCarServiceHelper);
        mCarInputService = new CarInputService(serviceContext, mHal.getInputHal(), mCarUserService);
        mCarProjectionService = new CarProjectionService(
                serviceContext, null /* handler */, mCarInputService, mCarBluetoothService);
        mGarageModeService = new GarageModeService(mContext);
        mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
        mCarAudioService = new CarAudioService(serviceContext);
        mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
        mFixedActivityService = new FixedActivityService(serviceContext);
        mInstrumentClusterService = new InstrumentClusterService(serviceContext,
                mAppFocusService, mCarInputService);
        mSystemStateControllerService = new SystemStateControllerService(
                serviceContext, mCarAudioService, this);
        mCarStatsService = new CarStatsService(serviceContext);
        mCarStatsService.init();
        if (mFeatureController.isFeatureEnabled(Car.VEHICLE_MAP_SERVICE)) {
            mVmsBrokerService = new VmsBrokerService(mContext, mCarStatsService);
        } else {
            mVmsBrokerService = null;
        }
        if (mFeatureController.isFeatureEnabled(Car.DIAGNOSTIC_SERVICE)) {
            mCarDiagnosticService = new CarDiagnosticService(serviceContext,
                    mHal.getDiagnosticHal());
        } else {
            mCarDiagnosticService = null;
        }
        if (mFeatureController.isFeatureEnabled(Car.STORAGE_MONITORING_SERVICE)) {
            mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
                    systemInterface);
        } else {
            mCarStorageMonitoringService = null;
        }
        mCarConfigurationService =
                new CarConfigurationService(serviceContext, new JsonReaderImpl());
        mCarLocationService = new CarLocationService(serviceContext);
        mCarTrustedDeviceService = new CarTrustedDeviceService(serviceContext);
        mCarMediaService = new CarMediaService(serviceContext, mCarUserService);
        mCarBugreportManagerService = new CarBugreportManagerService(serviceContext);
        if (!Build.IS_USER) {
            mCarExperimentalFeatureServiceController = new CarExperimentalFeatureServiceController(
                    serviceContext);
        } else {
            mCarExperimentalFeatureServiceController = null;
        }
        if (carWatchdogService == null) {
            mCarWatchdogService = new CarWatchdogService(serviceContext);
        } else {
            mCarWatchdogService = carWatchdogService;
        }
		//  将电源服务,添加到Car本地服务List中-----------------------------
        CarLocalServices.addService(CarPowerManagementService.class, mCarPowerManagementService);
        CarLocalServices.addService(CarPropertyService.class, mCarPropertyService);
        CarLocalServices.addService(CarUserService.class, mCarUserService);
        CarLocalServices.addService(CarTrustedDeviceService.class, mCarTrustedDeviceService);
        CarLocalServices.addService(CarUserNoticeService.class, mCarUserNoticeService);
        CarLocalServices.addService(SystemInterface.class, mSystemInterface);
        CarLocalServices.addService(CarDrivingStateService.class, mCarDrivingStateService);
        CarLocalServices.addService(PerUserCarServiceHelper.class, mPerUserCarServiceHelper);
        CarLocalServices.addService(FixedActivityService.class, mFixedActivityService);
        CarLocalServices.addService(VmsBrokerService.class, mVmsBrokerService);
        CarLocalServices.addService(CarOccupantZoneService.class, mCarOccupantZoneService);
        CarLocalServices.addService(AppFocusService.class, mAppFocusService);

        // Be careful with order. Service depending on other service should be inited later.
        List<CarServiceBase> allServices = new ArrayList<>();
        allServices.add(mFeatureController);
        allServices.add(mCarUserService);
        allServices.add(mSystemActivityMonitoringService);
		//  将电源服务,添加到Car服务List中-----------------------------
        allServices.add(mCarPowerManagementService);
        allServices.add(mCarPropertyService);
        allServices.add(mCarDrivingStateService);
        allServices.add(mCarOccupantZoneService);
        allServices.add(mCarUXRestrictionsService);
        addServiceIfNonNull(allServices, mOccupantAwarenessService);
        allServices.add(mCarPackageManagerService);
        allServices.add(mCarInputService);
        allServices.add(mGarageModeService);
        addServiceIfNonNull(allServices, mCarUserNoticeService);
        allServices.add(mAppFocusService);
        allServices.add(mCarAudioService);
        allServices.add(mCarNightService);
        allServices.add(mFixedActivityService);
        allServices.add(mInstrumentClusterService);
        allServices.add(mSystemStateControllerService);
        allServices.add(mPerUserCarServiceHelper);
        allServices.add(mCarBluetoothService);
        allServices.add(mCarProjectionService);
        addServiceIfNonNull(allServices, mCarDiagnosticService);
        addServiceIfNonNull(allServices, mCarStorageMonitoringService);
        allServices.add(mCarConfigurationService);
        addServiceIfNonNull(allServices, mVmsBrokerService);
        allServices.add(mCarTrustedDeviceService);
        allServices.add(mCarMediaService);
        allServices.add(mCarLocationService);
        allServices.add(mCarBugreportManagerService);
        allServices.add(mCarWatchdogService);
        // Always put mCarExperimentalFeatureServiceController in last.
        addServiceIfNonNull(allServices, mCarExperimentalFeatureServiceController);
        mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
    }
}
  • CarPower服务启动后,会注册成为PowerHalService的监听者。并主动发出WAIT_FOR_VHALL(初始状态)告知CarPower的监听者,并取一下当前的屏幕亮度发送给VHAL。并连接Native层的CarPowerPolicyService来初始化电源管理政策。
// packages/services/Car/service/src/com/android/car/CarPowerManagementService.java

/**
 * Power Management service class for cars. Controls the power states and interacts with other
 * parts of the system to ensure its own state.
 */
public class CarPowerManagementService extends ICarPower.Stub implements
        CarServiceBase, PowerHalService.PowerEventListener {
    public CarPowerManagementService(Context context, PowerHalService powerHal,
            SystemInterface systemInterface, CarUserService carUserService) {
        this(context, context.getResources(), powerHal, systemInterface, UserManager.get(context),
                carUserService, new InitialUserSetter(context,
                        (u) -> carUserService.setInitialUser(u),
                        context.getString(R.string.default_guest_name)),
                IVoiceInteractionManagerService.Stub.asInterface(
                        ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE)));
    }

    @VisibleForTesting
    public CarPowerManagementService(Context context, Resources resources, PowerHalService powerHal,
            SystemInterface systemInterface, UserManager userManager, CarUserService carUserService,
            InitialUserSetter initialUserSetter,
            IVoiceInteractionManagerService voiceInteractionService) {
        mContext = context;
        mHal = powerHal;
        mSystemInterface = systemInterface;
        mUserManager = userManager;
        mDisableUserSwitchDuringResume = resources
                .getBoolean(R.bool.config_disableUserSwitchDuringResume);
        mShutdownPrepareTimeMs = resources.getInteger(
                R.integer.maxGarageModeRunningDurationInSecs) * 1000;
        mSwitchGuestUserBeforeSleep = resources.getBoolean(
                R.bool.config_switchGuestUserBeforeGoingSleep);
        if (mShutdownPrepareTimeMs < MIN_MAX_GARAGE_MODE_DURATION_MS) {
            Slog.w(TAG,
                    "maxGarageModeRunningDurationInSecs smaller than minimum required, resource:"
                    + mShutdownPrepareTimeMs + "(ms) while should exceed:"
                    +  MIN_MAX_GARAGE_MODE_DURATION_MS + "(ms), Ignore resource.");
            mShutdownPrepareTimeMs = MIN_MAX_GARAGE_MODE_DURATION_MS;
        }
        mUserService = carUserService;
        mInitialUserSetter = initialUserSetter;
        mVoiceInteractionManagerService = voiceInteractionService;
        mWifiManager = context.getSystemService(WifiManager.class);
        mWifiStateFile = new AtomicFile(
                new File(mSystemInterface.getSystemCarDir(), WIFI_STATE_FILENAME));
    }
}

@Override
public void init() {
	mPolicyReader.init();
	mPowerComponentHandler.init();
	mHal.setListener(this);
	if (mHal.isPowerStateSupported()) {
		// 发出初始化状态-------------------------------------------
		// Initialize CPMS in WAIT_FOR_VHAL state
		onApPowerStateChange(CpmsState.WAIT_FOR_VHAL, CarPowerStateListener.WAIT_FOR_VHAL);
	} else {
		Slogf.w(TAG, "Vehicle hal does not support power state yet.");
		onApPowerStateChange(CpmsState.ON, CarPowerStateListener.ON);
	}
	//  监控屏幕状态,这个函数中会将屏幕亮度发给Vehicle HAL
	mSystemInterface.startDisplayStateMonitoring(this);
	//  连接Car PowerPolicy服务,初始化电源政策
	connectToPowerPolicyDaemon();
}
  • CarPower启动会立刻向监听者和VehicleHAL发出WAIT_FOR_VHAL这个状态
// packages/services/Car/service/src/com/android/car/power/CarPowerManagementService.java
private void doHandlePowerStateChange() {
	CpmsState state;
	synchronized (mLock) {
		state = mPendingPowerStates.peekFirst();
		mPendingPowerStates.clear();
		if (state == null) {
			Slogf.e(TAG, "Null power state was requested");
			return;
		}
		Slogf.i(TAG, "doHandlePowerStateChange: newState=%s", state.name());
		if (!needPowerStateChangeLocked(state)) {
			return;
		}
		// now real power change happens. Whatever was queued before should be all cancelled.
		releaseTimerLocked();
		mCurrentState = state;
	}
	mHandler.cancelProcessingComplete();
	Slogf.i(TAG, "setCurrentState %s", state);
	CarStatsLogHelper.logPowerState(state.mState);
	switch (state.mState) {
		case CpmsState.WAIT_FOR_VHAL:
			//  处理WAIT_FOR_VHAL状态----------------------------
			handleWaitForVhal(state);
			break;
		case CpmsState.ON:
			handleOn();
			break;
		case CpmsState.SHUTDOWN_PREPARE:
			handleShutdownPrepare(state);
			break;
		case CpmsState.SIMULATE_SLEEP:
			simulateShutdownPrepare();
			break;
		case CpmsState.WAIT_FOR_FINISH:
			handleWaitForFinish(state);
			break;
		case CpmsState.SUSPEND:
			// Received FINISH from VHAL
			handleFinish();
			break;
		default:
			// Illegal state
			// TODO:  Throw exception?
			break;
	}
}


private void handleWaitForVhal(CpmsState state) {
	int carPowerStateListenerState = state.mCarPowerStateListenerState;
	// TODO(b/177478420): Restore Wifi, Audio, Location, and Bluetooth, if they are artificially
	// modified for S2R.
	mSilentModeHandler.querySilentModeHwState();
	// 给CarPower服务的监听者发送状态
	sendPowerManagerEvent(carPowerStateListenerState);
	// Inspect CarPowerStateListenerState to decide which message to send via VHAL
	switch (carPowerStateListenerState) {
		case CarPowerStateListener.WAIT_FOR_VHAL:
			// 通过PowerHAlService向VehicleHAL发送状态
			mHal.sendWaitForVhal();
			break;
		case CarPowerStateListener.SHUTDOWN_CANCELLED:
			mShutdownOnNextSuspend = false; // This cancels the "NextSuspend"
			mHal.sendShutdownCancel();
			break;
		case CarPowerStateListener.SUSPEND_EXIT:
			mHal.sendSleepExit();
			break;
	}
	if (mWifiAdjustmentForSuspend) restoreWifi();
}

  • 接下来,MCU会给VehicleHAL发送电源通知,VehicleHAL通知(AP_POWER_STATE_REQ= ON) 给CarPower服务。
@Override
public void onHalEvents(List<VehiclePropValue> values) {
	PowerEventListener listener;
	synchronized (mLock) {
		if (mListener == null) {
			if (mQueuedEvents == null) {
				mQueuedEvents = new LinkedList<>();
			}
			mQueuedEvents.addAll(values);
			return;
		}
		listener = mListener;
	}
	dispatchEvents(values, listener);
}

private void dispatchEvents(List<VehiclePropValue> values, PowerEventListener listener) {
	for (VehiclePropValue v : values) {
		switch (v.prop) {
			case AP_POWER_STATE_REPORT:
				// Ignore this property event. It was generated inside of CarService.
				break;
			case AP_POWER_STATE_REQ:
				// 发送的是这个状态------ AP_POWER_STATE_REQ=ON
				int state = v.value.int32Values.get(VehicleApPowerStateReqIndex.STATE);
				int param = v.value.int32Values.get(VehicleApPowerStateReqIndex.ADDITIONAL);
				Slog.i(CarLog.TAG_POWER, "Received AP_POWER_STATE_REQ="
						+ powerStateReqName(state) + " param=" + param);
				listener.onApPowerStateChange(new PowerState(state, param));
				break;
			case DISPLAY_BRIGHTNESS:
			{
				int maxBrightness;
				synchronized (mLock) {
					maxBrightness = mMaxDisplayBrightness;
				}
				int brightness = v.value.int32Values.get(0) * MAX_BRIGHTNESS / maxBrightness;
				if (brightness < 0) {
					Slog.e(CarLog.TAG_POWER, "invalid brightness: " + brightness
							+ ", set to 0");
					brightness = 0;
				} else if (brightness > MAX_BRIGHTNESS) {
					Slog.e(CarLog.TAG_POWER, "invalid brightness: " + brightness + ", set to "
							+ MAX_BRIGHTNESS);
					brightness = MAX_BRIGHTNESS;
				}
				Slog.i(CarLog.TAG_POWER, "Received DISPLAY_BRIGHTNESS=" + brightness);
				listener.onDisplayBrightnessChange(brightness);
			}
				break;
		}
	}
}
  • CarPowerService接收到 AP_POWER_STATE_REQ= ON,认为当前是电源ON的状态。应用电源策略(告知机能模块可以使能)、通知观察者电源状态为ON,然后向VehicleHAL进行回复(也叫report)
packages/services/Car/service/src/com/android/car/power/CarPowerManagementService.java


@Override
public void onApPowerStateChange(PowerState state) {
	synchronized (mLock) {
		mPendingPowerStates.addFirst(new CpmsState(state));
		mLock.notify();
	}
	// 发给handler,在单独的线程中处理。最终调用的是自身的doHandlePowerStateChange函数
	mHandler.handlePowerStateChange();
}


private void doHandlePowerStateChange() {
	CpmsState state;
	synchronized (mLock) {
		state = mPendingPowerStates.peekFirst();
		mPendingPowerStates.clear();
		if (state == null) {
			Slogf.e(TAG, "Null power state was requested");
			return;
		}
		Slogf.i(TAG, "doHandlePowerStateChange: newState=%s", state.name());
		if (!needPowerStateChangeLocked(state)) {
			return;
		}
		// now real power change happens. Whatever was queued before should be all cancelled.
		releaseTimerLocked();
		mCurrentState = state;
	}
	mHandler.cancelProcessingComplete();
	Slogf.i(TAG, "setCurrentState %s", state);
	CarStatsLogHelper.logPowerState(state.mState);
	switch (state.mState) {
		case CpmsState.WAIT_FOR_VHAL:
			handleWaitForVhal(state);
			break;
		case CpmsState.ON:
			// 处理电源ON----------------------------------------------------------------
			handleOn();
			break;
		case CpmsState.SHUTDOWN_PREPARE:
			handleShutdownPrepare(state);
			break;
		case CpmsState.SIMULATE_SLEEP:
			simulateShutdownPrepare();
			break;
		case CpmsState.WAIT_FOR_FINISH:
			handleWaitForFinish(state);
			break;
		case CpmsState.SUSPEND:
			// Received FINISH from VHAL
			handleFinish();
			break;
		default:
			// Illegal state
			// TODO:  Throw exception?
			break;
	}
}


@VisibleForTesting
void handleOn() {
	if (factoryResetIfNeeded()) return;

	// 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();
	}

	boolean isPreemptive;
	synchronized (mLock) {
		isPreemptive = mPolicyReader.isPreemptivePowerPolicy(mCurrentPowerPolicyId);
	}
	if (!mSilentModeHandler.isSilentMode() && isPreemptive) {
		cancelPreemptivePowerPolicy();
	} else {
		// 应用电源策略---------------------------------
		applyDefaultPowerPolicyForState(VehicleApPowerStateReport.ON,
				PolicyReader.POWER_POLICY_ID_ALL_ON);
	}
	// 通知观察者电源状态为ON------------------------
	sendPowerManagerEvent(CarPowerStateListener.ON);
	// 通过PowerHAlServicee,回复Vehicle HAL(VehicleApPowerStateReport.ON)
	mHal.sendOn();

	synchronized (mLock) {
		if (mIsBooting) {
			Slogf.d(TAG, "handleOn(): called on boot");
			mIsBooting = false;
			return;
		}
	}

	try {
		mUserService.onResume();
	} catch (Exception e) {
		Slogf.e(TAG, e, "Could not switch user on resume");
	}
}
  • 到这里,CarPower主要的开机处理流程完毕。实际上可以看出来,主要是接收Vehicle HAl Event(包括回复VehicleHAL状态),告知监听者电源状态、应用电源政策使能相关机能。并且在启动阶段,取了一下当前的屏幕亮度,告知VehicleHAL(最终是告知MCU)。当然还有一些其他的处理,比如用户服务的恢复,有兴趣的可以读一下这部分代码。
  • CarPowerService上电时序图。
    【Android】Android Framework系列---CarPower电源管理_第3张图片

你可能感兴趣的:(Android,android,电源管理,CarPower,开机,Car服务)