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,std::shared_ptr<WebBatteryEventCallback> cb)43 NWebBatteryEventSubscriber::NWebBatteryEventSubscriber(
44 EventFwk::CommonEventSubscribeInfo& in, std::shared_ptr<WebBatteryEventCallback> cb)
45 : EventFwk::CommonEventSubscriber(in), eventCallback_(cb)
46 {}
47
OnReceiveEvent(const EventFwk::CommonEventData & data)48 void NWebBatteryEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
49 {
50 const std::string action = data.GetWant().GetAction();
51 WVLOG_I("receive battery action: %{public}s", action.c_str());
52 if (action != EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED) {
53 return;
54 }
55 int32_t defaultInvalid = -1;
56 std::string KEY_CAPACITY = PowerMgr::BatteryInfo::COMMON_EVENT_KEY_CAPACITY;
57 std::string KEY_CHARGE_TYPE = PowerMgr::BatteryInfo::COMMON_EVENT_KEY_PLUGGED_TYPE;
58 auto capacity = data.GetWant().GetIntParam(KEY_CAPACITY, defaultInvalid);
59 auto isChangingType = data.GetWant().GetIntParam(KEY_CHARGE_TYPE, defaultInvalid);
60 bool ischanging = true;
61 if ((isChangingType == static_cast<int>(BatteryPluggedType::PLUGGED_TYPE_NONE)) ||
62 (isChangingType == static_cast<int>(BatteryPluggedType::PLUGGED_TYPE_BUTT))) {
63 ischanging = false;
64 }
65 const double changeFullLevel = 100.f;
66 double changeLevel = capacity / changeFullLevel;
67 WVLOG_I("receive capacity %{public}d isChangingType %{public}d", capacity, isChangingType);
68 std::shared_ptr<WebBatteryInfoImpl> batterinfo =
69 std::make_shared<WebBatteryInfoImpl>(changeLevel, ischanging, -1, -1);
70 eventCallback_->BatteryInfoChanged(batterinfo);
71 }
72
RegBatteryEvent(std::shared_ptr<WebBatteryEventCallback> eventCallback)73 void BatteryMgrClientAdapterImpl::RegBatteryEvent(std::shared_ptr<WebBatteryEventCallback> eventCallback)
74 {
75 if (!eventCallback) {
76 WVLOG_E("WebBatteryEventCallback is nullptr.");
77 return;
78 }
79
80 WVLOG_I("Reg Battery Event");
81 cb_ = std::move(eventCallback);
82 }
83
StartListen()84 bool BatteryMgrClientAdapterImpl::StartListen()
85 {
86 WVLOG_I("start battery listen");
87 EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
88 skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_CHANGED);
89 EventFwk::CommonEventSubscribeInfo info(skill);
90 this->commonEventSubscriber_ = std::make_shared<NWebBatteryEventSubscriber>(info, this->cb_);
91 bool ret = EventFwk::CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber_);
92 if (ret == false) {
93 WVLOG_E("start battery listen fail");
94 return false;
95 } else {
96 return true;
97 }
98 }
99
StopListen()100 void BatteryMgrClientAdapterImpl::StopListen()
101 {
102 WVLOG_I("stop battery listen");
103 if (this->commonEventSubscriber_ != nullptr) {
104 bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber_);
105 if (result) {
106 this->commonEventSubscriber_ = nullptr;
107 } else {
108 WVLOG_E("stop battery listen fail");
109 }
110 }
111 }
112
RequestBatteryInfo()113 std::shared_ptr<WebBatteryInfo> BatteryMgrClientAdapterImpl::RequestBatteryInfo()
114 {
115 WVLOG_I("request batteryInfo");
116 BatterySrvClient& battClient = BatterySrvClient::GetInstance();
117 auto capacity = battClient.GetCapacity();
118 auto isChangingType = battClient.GetPluggedType();
119 bool ischanging = true;
120 if ((isChangingType == BatteryPluggedType::PLUGGED_TYPE_NONE) ||
121 (isChangingType == BatteryPluggedType::PLUGGED_TYPE_BUTT)) {
122 ischanging = false;
123 }
124 const double changeFullLevel = 100.f;
125 double changeLevel = capacity / changeFullLevel;
126 return std::make_shared<WebBatteryInfoImpl>(changeLevel, ischanging, -1, -1);
127 }
128 }
129