1f16e0440Sopenharmony_ci/*
2f16e0440Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3f16e0440Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f16e0440Sopenharmony_ci * you may not use this file except in compliance with the License.
5f16e0440Sopenharmony_ci * You may obtain a copy of the License at
6f16e0440Sopenharmony_ci *
7f16e0440Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f16e0440Sopenharmony_ci *
9f16e0440Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f16e0440Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f16e0440Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f16e0440Sopenharmony_ci * See the License for the specific language governing permissions and
13f16e0440Sopenharmony_ci * limitations under the License.
14f16e0440Sopenharmony_ci */
15f16e0440Sopenharmony_ci
16f16e0440Sopenharmony_ci#include "battery_notify.h"
17f16e0440Sopenharmony_ci#include <regex>
18f16e0440Sopenharmony_ci
19f16e0440Sopenharmony_ci#include "common_event_data.h"
20f16e0440Sopenharmony_ci#include "common_event_manager.h"
21f16e0440Sopenharmony_ci#include "common_event_publish_info.h"
22f16e0440Sopenharmony_ci#include "common_event_support.h"
23f16e0440Sopenharmony_ci#include "errors.h"
24f16e0440Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
25f16e0440Sopenharmony_ci#include "hisysevent.h"
26f16e0440Sopenharmony_ci#endif
27f16e0440Sopenharmony_ci#include "if_system_ability_manager.h"
28f16e0440Sopenharmony_ci#include "iservice_registry.h"
29f16e0440Sopenharmony_ci#include "string_ex.h"
30f16e0440Sopenharmony_ci#include "system_ability_definition.h"
31f16e0440Sopenharmony_ci
32f16e0440Sopenharmony_ci#include "battery_config.h"
33f16e0440Sopenharmony_ci#include "battery_log.h"
34f16e0440Sopenharmony_ci#include "battery_service.h"
35f16e0440Sopenharmony_ci#include "power_vibrator.h"
36f16e0440Sopenharmony_ci#include "power_mgr_client.h"
37f16e0440Sopenharmony_ci
38f16e0440Sopenharmony_ciusing namespace OHOS::AAFwk;
39f16e0440Sopenharmony_ciusing namespace OHOS::EventFwk;
40f16e0440Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
41f16e0440Sopenharmony_ciusing namespace OHOS::HiviewDFX;
42f16e0440Sopenharmony_ci#endif
43f16e0440Sopenharmony_ci
44f16e0440Sopenharmony_cinamespace OHOS {
45f16e0440Sopenharmony_cinamespace PowerMgr {
46f16e0440Sopenharmony_cibool g_batteryLowOnce = false;
47f16e0440Sopenharmony_cibool g_batteryOkOnce = false;
48f16e0440Sopenharmony_cibool g_batteryConnectOnce = false;
49f16e0440Sopenharmony_cibool g_batteryDisconnectOnce = false;
50f16e0440Sopenharmony_cibool g_batteryChargingOnce = false;
51f16e0440Sopenharmony_cibool g_batteryDischargingOnce = false;
52f16e0440Sopenharmony_cibool g_commonEventInitSuccess = false;
53f16e0440Sopenharmony_ciOHOS::PowerMgr::BatteryCapacityLevel g_lastCapacityLevel = OHOS::PowerMgr::BatteryCapacityLevel::LEVEL_NONE;
54f16e0440Sopenharmony_ciconst std::string POWER_SUPPLY = "SUBSYSTEM=power_supply";
55f16e0440Sopenharmony_ciconst std::string SHUTDOWN = "shutdown";
56f16e0440Sopenharmony_ciconst std::string REBOOT = "reboot";
57f16e0440Sopenharmony_ciconst std::string SEND_COMMONEVENT = "sendcommonevent";
58f16e0440Sopenharmony_ciconst std::string SEND_CUSTOMEVENT = "sendcustomevent";
59f16e0440Sopenharmony_ciconst std::string BATTERY_CUSTOM_EVENT_PREFIX = "usual.event.";
60f16e0440Sopenharmony_cisptr<BatteryService> g_service = DelayedSpSingleton<BatteryService>::GetInstance();
61f16e0440Sopenharmony_ci
62f16e0440Sopenharmony_ciBatteryNotify::BatteryNotify()
63f16e0440Sopenharmony_ci{
64f16e0440Sopenharmony_ci    const int32_t DEFAULT_LOW_CAPACITY = 20;
65f16e0440Sopenharmony_ci    lowCapacity_ = BatteryConfig::GetInstance().GetInt("soc.low", DEFAULT_LOW_CAPACITY);
66f16e0440Sopenharmony_ci    BATTERY_HILOGI(COMP_SVC, "Low broadcast power=%{public}d", lowCapacity_);
67f16e0440Sopenharmony_ci}
68f16e0440Sopenharmony_ci
69f16e0440Sopenharmony_ciint32_t BatteryNotify::PublishEvents(BatteryInfo& info)
70f16e0440Sopenharmony_ci{
71f16e0440Sopenharmony_ci    if (!g_commonEventInitSuccess) {
72f16e0440Sopenharmony_ci        if (!IsCommonEventServiceAbilityExist()) {
73f16e0440Sopenharmony_ci            return ERR_NO_INIT;
74f16e0440Sopenharmony_ci        }
75f16e0440Sopenharmony_ci    }
76f16e0440Sopenharmony_ci    if (info.GetUevent() != POWER_SUPPLY && info.GetUevent() != "") {
77f16e0440Sopenharmony_ci        HandleUevent(info);
78f16e0440Sopenharmony_ci        return ERR_OK;
79f16e0440Sopenharmony_ci    }
80f16e0440Sopenharmony_ci
81f16e0440Sopenharmony_ci    bool isAllSuccess = true;
82f16e0440Sopenharmony_ci    bool ret = PublishChangedEvent(info);
83f16e0440Sopenharmony_ci    isAllSuccess &= ret;
84f16e0440Sopenharmony_ci    ret = PublishChangedEventInner(info);
85f16e0440Sopenharmony_ci    isAllSuccess &= ret;
86f16e0440Sopenharmony_ci    ret = PublishLowEvent(info);
87f16e0440Sopenharmony_ci    isAllSuccess &= ret;
88f16e0440Sopenharmony_ci    ret = PublishOkayEvent(info);
89f16e0440Sopenharmony_ci    isAllSuccess &= ret;
90f16e0440Sopenharmony_ci    ret = PublishPowerConnectedEvent(info);
91f16e0440Sopenharmony_ci    isAllSuccess &= ret;
92f16e0440Sopenharmony_ci    ret = PublishPowerDisconnectedEvent(info);
93f16e0440Sopenharmony_ci    isAllSuccess &= ret;
94f16e0440Sopenharmony_ci    ret = PublishChargingEvent(info);
95f16e0440Sopenharmony_ci    isAllSuccess &= ret;
96f16e0440Sopenharmony_ci    ret = PublishDischargingEvent(info);
97f16e0440Sopenharmony_ci    isAllSuccess &= ret;
98f16e0440Sopenharmony_ci    ret = PublishChargeTypeChangedEvent(info);
99f16e0440Sopenharmony_ci    isAllSuccess &= ret;
100f16e0440Sopenharmony_ci
101f16e0440Sopenharmony_ci    return isAllSuccess ? ERR_OK : ERR_NO_INIT;
102f16e0440Sopenharmony_ci}
103f16e0440Sopenharmony_ci
104f16e0440Sopenharmony_civoid BatteryNotify::HandleUevent(BatteryInfo& info)
105f16e0440Sopenharmony_ci{
106f16e0440Sopenharmony_ci    std::string uevent = info.GetUevent();
107f16e0440Sopenharmony_ci    auto pos = uevent.rfind('$');
108f16e0440Sopenharmony_ci    if (pos != std::string::npos) {
109f16e0440Sopenharmony_ci        std::string ueventName = uevent.substr(0, pos);
110f16e0440Sopenharmony_ci        std::string ueventAct = uevent.substr(++pos);
111f16e0440Sopenharmony_ci        BATTERY_HILOGI(COMP_SVC, "%{public}s decision %{public}s",
112f16e0440Sopenharmony_ci            ueventName.c_str(), ueventAct.c_str());
113f16e0440Sopenharmony_ci        if (ueventAct == SHUTDOWN) {
114f16e0440Sopenharmony_ci            const std::string reason = "POWEROFF_CHARGE_DISABLE";
115f16e0440Sopenharmony_ci            PowerMgrClient::GetInstance().ShutDownDevice(reason);
116f16e0440Sopenharmony_ci        } else if (ueventAct == REBOOT) {
117f16e0440Sopenharmony_ci            PowerMgrClient::GetInstance().RebootDevice(ueventName);
118f16e0440Sopenharmony_ci        } else if (ueventAct == SEND_COMMONEVENT) {
119f16e0440Sopenharmony_ci            info.SetUevent(ueventName);
120f16e0440Sopenharmony_ci            PublishChangedEvent(info);
121f16e0440Sopenharmony_ci        } else if (ueventAct.compare(0, BATTERY_CUSTOM_EVENT_PREFIX.size(), BATTERY_CUSTOM_EVENT_PREFIX) == 0) {
122f16e0440Sopenharmony_ci            info.SetUevent(ueventName);
123f16e0440Sopenharmony_ci            PublishCustomEvent(info, ueventAct);
124f16e0440Sopenharmony_ci        } else {
125f16e0440Sopenharmony_ci            BATTERY_HILOGE(COMP_SVC, "undefine uevent act %{public}s", ueventAct.c_str());
126f16e0440Sopenharmony_ci        }
127f16e0440Sopenharmony_ci    }
128f16e0440Sopenharmony_ci    BATTERY_HILOGI(COMP_SVC, "handle uevent info %{public}s", uevent.c_str());
129f16e0440Sopenharmony_ci}
130f16e0440Sopenharmony_ci
131f16e0440Sopenharmony_cibool BatteryNotify::PublishChargeTypeChangedEvent(const BatteryInfo& info)
132f16e0440Sopenharmony_ci{
133f16e0440Sopenharmony_ci    ChargeType chargeType = info.GetChargeType();
134f16e0440Sopenharmony_ci    bool isSuccess = true;
135f16e0440Sopenharmony_ci    if (batteryInfoChargeType_ == chargeType) {
136f16e0440Sopenharmony_ci        BATTERY_HILOGD(COMP_SVC, "No need to send chargetype event");
137f16e0440Sopenharmony_ci        return isSuccess;
138f16e0440Sopenharmony_ci    }
139f16e0440Sopenharmony_ci    batteryInfoChargeType_ = chargeType;
140f16e0440Sopenharmony_ci    Want want;
141f16e0440Sopenharmony_ci    want.SetAction(CommonEventSupport::COMMON_EVENT_CHARGE_TYPE_CHANGED);
142f16e0440Sopenharmony_ci    CommonEventData data;
143f16e0440Sopenharmony_ci    data.SetWant(want);
144f16e0440Sopenharmony_ci    CommonEventPublishInfo publishInfo;
145f16e0440Sopenharmony_ci    publishInfo.SetOrdered(false);
146f16e0440Sopenharmony_ci
147f16e0440Sopenharmony_ci    data.SetCode(static_cast<int32_t>(chargeType));
148f16e0440Sopenharmony_ci    BATTERY_HILOGD(COMP_SVC, "publisher chargeType=%{public}d", chargeType);
149f16e0440Sopenharmony_ci    isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
150f16e0440Sopenharmony_ci    if (!isSuccess) {
151f16e0440Sopenharmony_ci        BATTERY_HILOGD(COMP_SVC, "failed to publish battery charge type event");
152f16e0440Sopenharmony_ci    }
153f16e0440Sopenharmony_ci
154f16e0440Sopenharmony_ci    return isSuccess;
155f16e0440Sopenharmony_ci}
156f16e0440Sopenharmony_ci
157f16e0440Sopenharmony_cibool BatteryNotify::IsCommonEventServiceAbilityExist() const
158f16e0440Sopenharmony_ci{
159f16e0440Sopenharmony_ci    sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
160f16e0440Sopenharmony_ci    if (!sysMgr) {
161f16e0440Sopenharmony_ci        BATTERY_HILOGE(COMP_SVC,
162f16e0440Sopenharmony_ci            "IsCommonEventServiceAbilityExist Get ISystemAbilityManager failed, no SystemAbilityManager");
163f16e0440Sopenharmony_ci        return false;
164f16e0440Sopenharmony_ci    }
165f16e0440Sopenharmony_ci    sptr<IRemoteObject> remote = sysMgr->CheckSystemAbility(COMMON_EVENT_SERVICE_ID);
166f16e0440Sopenharmony_ci    if (!remote) {
167f16e0440Sopenharmony_ci        BATTERY_HILOGE(COMP_SVC, "No CesServiceAbility");
168f16e0440Sopenharmony_ci        return false;
169f16e0440Sopenharmony_ci    }
170f16e0440Sopenharmony_ci
171f16e0440Sopenharmony_ci    if (!g_commonEventInitSuccess) {
172f16e0440Sopenharmony_ci        BATTERY_HILOGI(COMP_SVC, "common event service ability init success");
173f16e0440Sopenharmony_ci        g_commonEventInitSuccess = true;
174f16e0440Sopenharmony_ci    }
175f16e0440Sopenharmony_ci
176f16e0440Sopenharmony_ci    return true;
177f16e0440Sopenharmony_ci}
178f16e0440Sopenharmony_ci
179f16e0440Sopenharmony_cibool BatteryNotify::PublishChangedEvent(const BatteryInfo& info)
180f16e0440Sopenharmony_ci{
181f16e0440Sopenharmony_ci    Want want;
182f16e0440Sopenharmony_ci    int32_t capacity = info.GetCapacity();
183f16e0440Sopenharmony_ci    int32_t pluggedType = static_cast<int32_t>(info.GetPluggedType());
184f16e0440Sopenharmony_ci    int32_t temperature = info.GetTemperature();
185f16e0440Sopenharmony_ci    int32_t healthState = static_cast<int32_t>(info.GetHealthState());
186f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CAPACITY, capacity);
187f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_VOLTAGE, info.GetVoltage());
188f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_TEMPERATURE, temperature);
189f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_HEALTH_STATE, healthState);
190f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_TYPE, pluggedType);
191f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CHARGE_STATE, static_cast<int32_t>(info.GetChargeState()));
192f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PRESENT, info.IsPresent());
193f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_TECHNOLOGY, info.GetTechnology());
194f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_UEVENT, info.GetUevent());
195f16e0440Sopenharmony_ci
196f16e0440Sopenharmony_ci    auto capacityLevel = g_service->GetCapacityLevel();
197f16e0440Sopenharmony_ci    if (capacityLevel != g_lastCapacityLevel) {
198f16e0440Sopenharmony_ci        want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CAPACITY_LEVEL, static_cast<int32_t>(capacityLevel));
199f16e0440Sopenharmony_ci        g_lastCapacityLevel = capacityLevel;
200f16e0440Sopenharmony_ci    }
201f16e0440Sopenharmony_ci
202f16e0440Sopenharmony_ci    want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
203f16e0440Sopenharmony_ci    CommonEventData data;
204f16e0440Sopenharmony_ci    data.SetWant(want);
205f16e0440Sopenharmony_ci    CommonEventPublishInfo publishInfo;
206f16e0440Sopenharmony_ci    publishInfo.SetOrdered(false);
207f16e0440Sopenharmony_ci    if (capacity != lastCapacity_ || pluggedType != lastPluggedType_ ||
208f16e0440Sopenharmony_ci        temperature != lastTemperature_ || healthState != lastHealthState_) {
209f16e0440Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
210f16e0440Sopenharmony_ci        HiSysEventWrite(HiSysEvent::Domain::BATTERY, "CHANGED", HiSysEvent::EventType::STATISTIC,
211f16e0440Sopenharmony_ci            "LEVEL", capacity, "CHARGER", pluggedType, "VOLTAGE", info.GetVoltage(),
212f16e0440Sopenharmony_ci            "TEMPERATURE", temperature, "HEALTH", healthState, "CURRENT", info.GetNowCurrent());
213f16e0440Sopenharmony_ci#endif
214f16e0440Sopenharmony_ci        lastCapacity_ = capacity;
215f16e0440Sopenharmony_ci        lastPluggedType_ = pluggedType;
216f16e0440Sopenharmony_ci        lastTemperature_ = temperature;
217f16e0440Sopenharmony_ci        lastHealthState_ = healthState;
218f16e0440Sopenharmony_ci    }
219f16e0440Sopenharmony_ci    bool isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
220f16e0440Sopenharmony_ci    if (!isSuccess) {
221f16e0440Sopenharmony_ci        BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish BATTERY_CHANGED event");
222f16e0440Sopenharmony_ci    }
223f16e0440Sopenharmony_ci    return isSuccess;
224f16e0440Sopenharmony_ci}
225f16e0440Sopenharmony_ci
226f16e0440Sopenharmony_cibool BatteryNotify::PublishChangedEventInner(const BatteryInfo& info) const
227f16e0440Sopenharmony_ci{
228f16e0440Sopenharmony_ci    Want want;
229f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_MAX_CURRENT, info.GetPluggedMaxCurrent());
230f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_MAX_VOLTAGE, info.GetPluggedMaxVoltage());
231f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_PLUGGED_NOW_CURRENT, info.GetNowCurrent());
232f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_CHARGE_COUNTER, info.GetChargeCounter());
233f16e0440Sopenharmony_ci
234f16e0440Sopenharmony_ci    want.SetAction(BatteryInfo::COMMON_EVENT_BATTERY_CHANGED_INNER);
235f16e0440Sopenharmony_ci    CommonEventData data;
236f16e0440Sopenharmony_ci    data.SetWant(want);
237f16e0440Sopenharmony_ci    CommonEventPublishInfo publishInfo;
238f16e0440Sopenharmony_ci    publishInfo.SetOrdered(false);
239f16e0440Sopenharmony_ci    const std::vector<std::string> permissionVec { "ohos.permission.POWER_OPTIMIZATION" };
240f16e0440Sopenharmony_ci    publishInfo.SetSubscriberPermissions(permissionVec);
241f16e0440Sopenharmony_ci
242f16e0440Sopenharmony_ci    bool isSuccess = true;
243f16e0440Sopenharmony_ci    isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
244f16e0440Sopenharmony_ci    if (!isSuccess) {
245f16e0440Sopenharmony_ci        BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish BATTERY_CHANGED_INNER event");
246f16e0440Sopenharmony_ci    }
247f16e0440Sopenharmony_ci    return isSuccess;
248f16e0440Sopenharmony_ci}
249f16e0440Sopenharmony_ci
250f16e0440Sopenharmony_cibool BatteryNotify::PublishLowEvent(const BatteryInfo& info) const
251f16e0440Sopenharmony_ci{
252f16e0440Sopenharmony_ci    bool isSuccess = true;
253f16e0440Sopenharmony_ci
254f16e0440Sopenharmony_ci    if (info.GetCapacity() > lowCapacity_) {
255f16e0440Sopenharmony_ci        g_batteryLowOnce = false;
256f16e0440Sopenharmony_ci        return isSuccess;
257f16e0440Sopenharmony_ci    }
258f16e0440Sopenharmony_ci
259f16e0440Sopenharmony_ci    if (g_batteryLowOnce) {
260f16e0440Sopenharmony_ci        return isSuccess;
261f16e0440Sopenharmony_ci    }
262f16e0440Sopenharmony_ci
263f16e0440Sopenharmony_ci    Want want;
264f16e0440Sopenharmony_ci    want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_LOW);
265f16e0440Sopenharmony_ci    CommonEventData data;
266f16e0440Sopenharmony_ci    data.SetWant(want);
267f16e0440Sopenharmony_ci    CommonEventPublishInfo publishInfo;
268f16e0440Sopenharmony_ci    publishInfo.SetOrdered(false);
269f16e0440Sopenharmony_ci    data.SetCode(info.GetCapacity());
270f16e0440Sopenharmony_ci    BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher capacity=%{public}d", info.GetCapacity());
271f16e0440Sopenharmony_ci    isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
272f16e0440Sopenharmony_ci    if (!isSuccess) {
273f16e0440Sopenharmony_ci        BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish battery_low event");
274f16e0440Sopenharmony_ci    }
275f16e0440Sopenharmony_ci    g_batteryLowOnce = true;
276f16e0440Sopenharmony_ci    return isSuccess;
277f16e0440Sopenharmony_ci}
278f16e0440Sopenharmony_ci
279f16e0440Sopenharmony_cibool BatteryNotify::PublishOkayEvent(const BatteryInfo& info) const
280f16e0440Sopenharmony_ci{
281f16e0440Sopenharmony_ci    bool isSuccess = true;
282f16e0440Sopenharmony_ci
283f16e0440Sopenharmony_ci    if (info.GetCapacity() <= lowCapacity_) {
284f16e0440Sopenharmony_ci        g_batteryOkOnce = false;
285f16e0440Sopenharmony_ci        return isSuccess;
286f16e0440Sopenharmony_ci    }
287f16e0440Sopenharmony_ci
288f16e0440Sopenharmony_ci    if (g_batteryOkOnce) {
289f16e0440Sopenharmony_ci        return isSuccess;
290f16e0440Sopenharmony_ci    }
291f16e0440Sopenharmony_ci
292f16e0440Sopenharmony_ci    Want want;
293f16e0440Sopenharmony_ci    want.SetAction(CommonEventSupport::COMMON_EVENT_BATTERY_OKAY);
294f16e0440Sopenharmony_ci    CommonEventData data;
295f16e0440Sopenharmony_ci    data.SetWant(want);
296f16e0440Sopenharmony_ci    CommonEventPublishInfo publishInfo;
297f16e0440Sopenharmony_ci    publishInfo.SetOrdered(false);
298f16e0440Sopenharmony_ci    data.SetCode(info.GetCapacity());
299f16e0440Sopenharmony_ci    BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher capacity=%{public}d", info.GetCapacity());
300f16e0440Sopenharmony_ci    isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
301f16e0440Sopenharmony_ci    if (!isSuccess) {
302f16e0440Sopenharmony_ci        BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery_okay event");
303f16e0440Sopenharmony_ci    }
304f16e0440Sopenharmony_ci    g_batteryOkOnce = true;
305f16e0440Sopenharmony_ci    return isSuccess;
306f16e0440Sopenharmony_ci}
307f16e0440Sopenharmony_ci
308f16e0440Sopenharmony_cibool BatteryNotify::PublishPowerConnectedEvent(const BatteryInfo& info) const
309f16e0440Sopenharmony_ci{
310f16e0440Sopenharmony_ci    bool isSuccess = true;
311f16e0440Sopenharmony_ci
312f16e0440Sopenharmony_ci    if ((info.GetPluggedType() == BatteryPluggedType::PLUGGED_TYPE_NONE) ||
313f16e0440Sopenharmony_ci        (info.GetPluggedType() == BatteryPluggedType::PLUGGED_TYPE_BUTT)) {
314f16e0440Sopenharmony_ci        g_batteryConnectOnce = false;
315f16e0440Sopenharmony_ci        return isSuccess;
316f16e0440Sopenharmony_ci    }
317f16e0440Sopenharmony_ci
318f16e0440Sopenharmony_ci    if (g_batteryConnectOnce) {
319f16e0440Sopenharmony_ci        return isSuccess;
320f16e0440Sopenharmony_ci    }
321f16e0440Sopenharmony_ci    StartVibrator();
322f16e0440Sopenharmony_ci    Want want;
323f16e0440Sopenharmony_ci    want.SetAction(CommonEventSupport::COMMON_EVENT_POWER_CONNECTED);
324f16e0440Sopenharmony_ci    CommonEventData data;
325f16e0440Sopenharmony_ci    data.SetWant(want);
326f16e0440Sopenharmony_ci    CommonEventPublishInfo publishInfo;
327f16e0440Sopenharmony_ci    publishInfo.SetOrdered(false);
328f16e0440Sopenharmony_ci    data.SetCode(static_cast<int32_t>(info.GetPluggedType()));
329f16e0440Sopenharmony_ci    BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher pluggedtype=%{public}u",
330f16e0440Sopenharmony_ci        static_cast<uint32_t>(info.GetPluggedType()));
331f16e0440Sopenharmony_ci    isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
332f16e0440Sopenharmony_ci    if (!isSuccess) {
333f16e0440Sopenharmony_ci        BATTERY_HILOGE(FEATURE_BATT_INFO, "failed to publish power_connected event");
334f16e0440Sopenharmony_ci    }
335f16e0440Sopenharmony_ci
336f16e0440Sopenharmony_ci    g_batteryConnectOnce = true;
337f16e0440Sopenharmony_ci    return isSuccess;
338f16e0440Sopenharmony_ci}
339f16e0440Sopenharmony_ci
340f16e0440Sopenharmony_civoid BatteryNotify::StartVibrator() const
341f16e0440Sopenharmony_ci{
342f16e0440Sopenharmony_ci    std::shared_ptr<PowerVibrator> vibrator = PowerVibrator::GetInstance();
343f16e0440Sopenharmony_ci    std::string scene = "start_charge";
344f16e0440Sopenharmony_ci    vibrator->StartVibrator(scene);
345f16e0440Sopenharmony_ci}
346f16e0440Sopenharmony_ci
347f16e0440Sopenharmony_cibool BatteryNotify::PublishPowerDisconnectedEvent(const BatteryInfo& info) const
348f16e0440Sopenharmony_ci{
349f16e0440Sopenharmony_ci    bool isSuccess = true;
350f16e0440Sopenharmony_ci
351f16e0440Sopenharmony_ci    if ((info.GetPluggedType() != BatteryPluggedType::PLUGGED_TYPE_NONE) &&
352f16e0440Sopenharmony_ci        (info.GetPluggedType() != BatteryPluggedType::PLUGGED_TYPE_BUTT)) {
353f16e0440Sopenharmony_ci        g_batteryDisconnectOnce = false;
354f16e0440Sopenharmony_ci        return isSuccess;
355f16e0440Sopenharmony_ci    }
356f16e0440Sopenharmony_ci
357f16e0440Sopenharmony_ci    if (g_batteryDisconnectOnce) {
358f16e0440Sopenharmony_ci        return isSuccess;
359f16e0440Sopenharmony_ci    }
360f16e0440Sopenharmony_ci
361f16e0440Sopenharmony_ci    Want want;
362f16e0440Sopenharmony_ci    want.SetAction(CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
363f16e0440Sopenharmony_ci    CommonEventData data;
364f16e0440Sopenharmony_ci    data.SetWant(want);
365f16e0440Sopenharmony_ci    CommonEventPublishInfo publishInfo;
366f16e0440Sopenharmony_ci    publishInfo.SetOrdered(false);
367f16e0440Sopenharmony_ci    data.SetCode(static_cast<int32_t>(info.GetPluggedType()));
368f16e0440Sopenharmony_ci    BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher pluggedtype=%{public}u",
369f16e0440Sopenharmony_ci        static_cast<uint32_t>(info.GetPluggedType()));
370f16e0440Sopenharmony_ci    isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
371f16e0440Sopenharmony_ci    if (!isSuccess) {
372f16e0440Sopenharmony_ci        BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish power_disconnected event");
373f16e0440Sopenharmony_ci    }
374f16e0440Sopenharmony_ci
375f16e0440Sopenharmony_ci    g_batteryDisconnectOnce = true;
376f16e0440Sopenharmony_ci    return isSuccess;
377f16e0440Sopenharmony_ci}
378f16e0440Sopenharmony_ci
379f16e0440Sopenharmony_cibool BatteryNotify::PublishChargingEvent(const BatteryInfo& info) const
380f16e0440Sopenharmony_ci{
381f16e0440Sopenharmony_ci    bool isSuccess = true;
382f16e0440Sopenharmony_ci
383f16e0440Sopenharmony_ci    if ((info.GetChargeState() != BatteryChargeState::CHARGE_STATE_ENABLE) &&
384f16e0440Sopenharmony_ci        (info.GetChargeState() != BatteryChargeState::CHARGE_STATE_FULL)) {
385f16e0440Sopenharmony_ci        g_batteryChargingOnce = false;
386f16e0440Sopenharmony_ci        return isSuccess;
387f16e0440Sopenharmony_ci    }
388f16e0440Sopenharmony_ci
389f16e0440Sopenharmony_ci    if (g_batteryChargingOnce) {
390f16e0440Sopenharmony_ci        return isSuccess;
391f16e0440Sopenharmony_ci    }
392f16e0440Sopenharmony_ci
393f16e0440Sopenharmony_ci    Want want;
394f16e0440Sopenharmony_ci    want.SetAction(CommonEventSupport::COMMON_EVENT_CHARGING);
395f16e0440Sopenharmony_ci    CommonEventData data;
396f16e0440Sopenharmony_ci    data.SetWant(want);
397f16e0440Sopenharmony_ci    CommonEventPublishInfo publishInfo;
398f16e0440Sopenharmony_ci    publishInfo.SetOrdered(false);
399f16e0440Sopenharmony_ci    data.SetCode(static_cast<int32_t>(info.GetChargeState()));
400f16e0440Sopenharmony_ci    BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher chargeState=%{public}u",
401f16e0440Sopenharmony_ci        static_cast<uint32_t>(info.GetChargeState()));
402f16e0440Sopenharmony_ci    isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
403f16e0440Sopenharmony_ci    if (!isSuccess) {
404f16e0440Sopenharmony_ci        BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery charing event");
405f16e0440Sopenharmony_ci    }
406f16e0440Sopenharmony_ci
407f16e0440Sopenharmony_ci    g_batteryChargingOnce = true;
408f16e0440Sopenharmony_ci    return isSuccess;
409f16e0440Sopenharmony_ci}
410f16e0440Sopenharmony_ci
411f16e0440Sopenharmony_cibool BatteryNotify::PublishDischargingEvent(const BatteryInfo& info) const
412f16e0440Sopenharmony_ci{
413f16e0440Sopenharmony_ci    bool isSuccess = true;
414f16e0440Sopenharmony_ci
415f16e0440Sopenharmony_ci    if ((info.GetChargeState() == BatteryChargeState::CHARGE_STATE_ENABLE) ||
416f16e0440Sopenharmony_ci        (info.GetChargeState() == BatteryChargeState::CHARGE_STATE_FULL)) {
417f16e0440Sopenharmony_ci        g_batteryDischargingOnce = false;
418f16e0440Sopenharmony_ci        return isSuccess;
419f16e0440Sopenharmony_ci    }
420f16e0440Sopenharmony_ci
421f16e0440Sopenharmony_ci    if (g_batteryDischargingOnce) {
422f16e0440Sopenharmony_ci        return isSuccess;
423f16e0440Sopenharmony_ci    }
424f16e0440Sopenharmony_ci
425f16e0440Sopenharmony_ci    Want want;
426f16e0440Sopenharmony_ci    want.SetAction(CommonEventSupport::COMMON_EVENT_DISCHARGING);
427f16e0440Sopenharmony_ci    CommonEventData data;
428f16e0440Sopenharmony_ci    data.SetWant(want);
429f16e0440Sopenharmony_ci    CommonEventPublishInfo publishInfo;
430f16e0440Sopenharmony_ci    publishInfo.SetOrdered(false);
431f16e0440Sopenharmony_ci    data.SetCode(static_cast<int32_t>(info.GetChargeState()));
432f16e0440Sopenharmony_ci    BATTERY_HILOGD(FEATURE_BATT_INFO, "publisher chargeState=%{public}u",
433f16e0440Sopenharmony_ci        static_cast<uint32_t>(info.GetChargeState()));
434f16e0440Sopenharmony_ci    isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
435f16e0440Sopenharmony_ci    if (!isSuccess) {
436f16e0440Sopenharmony_ci        BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery charing event");
437f16e0440Sopenharmony_ci    }
438f16e0440Sopenharmony_ci
439f16e0440Sopenharmony_ci    g_batteryDischargingOnce = true;
440f16e0440Sopenharmony_ci    return isSuccess;
441f16e0440Sopenharmony_ci}
442f16e0440Sopenharmony_ci
443f16e0440Sopenharmony_cibool BatteryNotify::PublishCustomEvent(const BatteryInfo& info, const std::string& commonEventName) const
444f16e0440Sopenharmony_ci{
445f16e0440Sopenharmony_ci    Want want;
446f16e0440Sopenharmony_ci    want.SetParam(BatteryInfo::COMMON_EVENT_KEY_UEVENT, info.GetUevent());
447f16e0440Sopenharmony_ci    want.SetAction(commonEventName);
448f16e0440Sopenharmony_ci    CommonEventData data;
449f16e0440Sopenharmony_ci    data.SetWant(want);
450f16e0440Sopenharmony_ci    CommonEventPublishInfo publishInfo;
451f16e0440Sopenharmony_ci    publishInfo.SetOrdered(false);
452f16e0440Sopenharmony_ci    const std::vector<std::string> permissionVec { "ohos.permission.POWER_OPTIMIZATION" };
453f16e0440Sopenharmony_ci    publishInfo.SetSubscriberPermissions(permissionVec);
454f16e0440Sopenharmony_ci
455f16e0440Sopenharmony_ci    bool isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo);
456f16e0440Sopenharmony_ci    if (!isSuccess) {
457f16e0440Sopenharmony_ci        BATTERY_HILOGD(FEATURE_BATT_INFO, "failed to publish battery custom event");
458f16e0440Sopenharmony_ci    }
459f16e0440Sopenharmony_ci    return isSuccess;
460f16e0440Sopenharmony_ci}
461f16e0440Sopenharmony_ci} // namespace PowerMgr
462f16e0440Sopenharmony_ci} // namespace OHOS
463