1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "battery_mgr_client_adapter_impl.h"
17 #include "nweb_log.h"
18 #include "battery_srv_client.h"
19
20 using namespace OHOS::NWeb;
21 using namespace OHOS::PowerMgr;
22 namespace OHOS::NWeb {
GetLevel()23 double WebBatteryInfoImpl::GetLevel()
24 {
25 return level_;
26 }
27
IsCharging()28 bool WebBatteryInfoImpl::IsCharging()
29 {
30 return isCharging_;
31 }
32
DisChargingTime()33 int WebBatteryInfoImpl::DisChargingTime()
34 {
35 return disChargingTime_;
36 }
37
ChargingTime()38 int WebBatteryInfoImpl::ChargingTime()
39 {
40 return chargingTime_;
41 }
42
NWebBatteryEventSubscriber(EventFwk::CommonEventSubscribeInfo & in,BatteryEventCallback & cb)43 NWebBatteryEventSubscriber::NWebBatteryEventSubscriber(EventFwk::CommonEventSubscribeInfo& in, BatteryEventCallback& cb)
44 : EventFwk::CommonEventSubscriber(in), eventCallback_(cb) {}
45
OnReceiveEvent(const EventFwk::CommonEventData & data)46 void NWebBatteryEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
47 {
48 const std::string action = data.GetWant().GetAction();
49 WVLOG_I("receive battery action: %{public}s", action.c_str());
50 if (action != EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED) {
51 return;
52 }
53 int32_t defaultInvalid = -1;
54 std::string KEY_CAPACITY = PowerMgr::BatteryInfo::COMMON_EVENT_KEY_CAPACITY;
55 std::string KEY_CHARGE_TYPE = PowerMgr::BatteryInfo::COMMON_EVENT_KEY_PLUGGED_TYPE;
56 auto capacity = data.GetWant().GetIntParam(KEY_CAPACITY, defaultInvalid);
57 auto isChangingType = data.GetWant().GetIntParam(KEY_CHARGE_TYPE, defaultInvalid);
58 bool ischanging = true;
59 if ((isChangingType == static_cast<int>(BatteryPluggedType::PLUGGED_TYPE_NONE)) ||
60 (isChangingType == static_cast<int>(BatteryPluggedType::PLUGGED_TYPE_BUTT))) {
61 ischanging = false;
62 }
63 const double changeFullLevel = 100.f;
64 double changeLevel = capacity / changeFullLevel;
65 WVLOG_I("receive capacity %{public}d isChangingType %{public}d", capacity, isChangingType);
66 WebBatteryInfoImpl batterinfo(changeLevel, ischanging, -1, -1);
67 eventCallback_(batterinfo);
68 }
69
RegBatteryEvent(const BatteryEventCallback && eventCallback)70 void BatteryMgrClientAdapterImpl::RegBatteryEvent(const BatteryEventCallback&& eventCallback)
71 {
72 WVLOG_I("Reg Battery Event");
73 cb = std::move(eventCallback);
74 }
75
StartListen()76 bool BatteryMgrClientAdapterImpl::StartListen()
77 {
78 WVLOG_I("start battery listen");
79 EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
80 skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
81 EventFwk::CommonEventSubscribeInfo info(skill);
82 this->commonEventSubscriber_ = std::make_shared<NWebBatteryEventSubscriber>(info, this->cb);
83 bool ret = EventFwk::CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber_);
84 if (ret == false) {
85 WVLOG_E("start battery listen fail");
86 return false;
87 } else {
88 return true;
89 }
90 }
91
StopListen()92 void BatteryMgrClientAdapterImpl::StopListen()
93 {
94 WVLOG_I("stop battery listen");
95 if (this->commonEventSubscriber_ != nullptr) {
96 bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber_);
97 if (result) {
98 this->commonEventSubscriber_ = nullptr;
99 } else {
100 WVLOG_E("stop battery listen fail");
101 }
102 }
103 }
104
RequestBatteryInfo()105 std::unique_ptr<WebBatteryInfo> BatteryMgrClientAdapterImpl::RequestBatteryInfo()
106 {
107 WVLOG_I("request batteryInfo");
108 BatterySrvClient& battClient = BatterySrvClient::GetInstance();
109 auto capacity = battClient.GetCapacity();
110 auto isChangingType = battClient.GetPluggedType();
111 bool ischanging = true;
112 if ((isChangingType == BatteryPluggedType::PLUGGED_TYPE_NONE) ||
113 (isChangingType == BatteryPluggedType::PLUGGED_TYPE_BUTT)) {
114 ischanging = false;
115 }
116 const double changeFullLevel = 100.f;
117 double changeLevel = capacity / changeFullLevel;
118 return std::make_unique<WebBatteryInfoImpl>(changeLevel, ischanging, -1, -1);
119 }
120 }