fix: deduplicate low-battery notifications during upgrade retries#320
fix: deduplicate low-battery notifications during upgrade retries#320zhaohuiw42 merged 1 commit intolinuxdeepin:masterfrom
Conversation
Do not remove power properties handler at job end, otherwise the single watcher can be detached and later flows may miss notifications. Bug: https://pms.uniontech.com/bug-view-316659.html
|
CLA Assistant Lite bot: |
deepin pr auto review这段代码的改动主要是为了优化电源状态监听逻辑,防止重复注册监听器。以下是对这段代码的详细审查和改进建议: 1. 代码逻辑审查优点:
潜在问题:
2. 代码质量审查命名规范:
代码结构:
3. 代码性能审查
4. 代码安全审查
5. 改进建议5.1 错误处理改进onBatteryGlobal, err := m.sysPower.OnBattery().Get(0)
if err != nil {
logger.Warning("failed to get OnBattery status:", err)
onBatteryGlobal = false // 使用默认值
}
batteryPercentage, err := m.sysPower.BatteryPercentage().Get(0)
if err != nil {
logger.Warning("failed to get BatteryPercentage:", err)
batteryPercentage = 100.0 // 使用默认值
}5.2 函数拆分func (m *Manager) handleSysPowerBatteryEvent(lowPowerNotifyID *uint32, onBatteryGlobal *bool, batteryPercentage *float64, handleSysPowerBatteryEventMu *sync.Mutex) {
handleSysPowerBatteryEventMu.Lock()
defer handleSysPowerBatteryEventMu.Unlock()
if m.shouldShowLowPowerNotification(*onBatteryGlobal, *batteryPercentage) {
m.showLowPowerNotification(lowPowerNotifyID)
}
if !*onBatteryGlobal && *lowPowerNotifyID != 0 {
m.closeLowPowerNotification(lowPowerNotifyID)
}
}
func (m *Manager) shouldShowLowPowerNotification(onBatteryGlobal bool, batteryPercentage float64) bool {
return onBatteryGlobal && batteryPercentage < 60.0 && m.statusManager.isUpgrading()
}
func (m *Manager) showLowPowerNotification(lowPowerNotifyID *uint32) {
if *lowPowerNotifyID != 0 {
return
}
msg := gettext.Tr("The battery capacity is lower than 60%. To get successful updates, please plug in.")
*lowPowerNotifyID = m.sendNotify(updateNotifyShow, 0, "notification-battery_low", "", msg, nil, nil, system.NotifyExpireTimeoutNoHide)
}
func (m *Manager) closeLowPowerNotification(lowPowerNotifyID *uint32) {
closeErr := m.closeNotify(*lowPowerNotifyID)
if closeErr != nil {
logger.Warning(closeErr)
} else {
*lowPowerNotifyID = 0
}
}5.3 资源清理在 func (m *Manager) cleanup() {
if m.sysPower != nil {
// 移除所有监听器
// 注意:需要确保 sysPower 提供了相应的方法
}
// 其他清理逻辑...
}总结这段代码的改动总体上是积极的,主要改进了电源监听器的注册逻辑,避免了重复注册。建议进一步改进错误处理和函数结构,以提高代码的健壮性和可维护性。 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: qiuzhiqian, zhaohuiw42 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Do not remove power properties handler at job end, otherwise the single watcher can be detached and later flows may miss notifications.
Bug: https://pms.uniontech.com/bug-view-316659.html