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 #ifndef BATTERY_MGR_CLIENT_ADAPTER_IMPL_H
17 #define BATTERY_MGR_CLIENT_ADAPTER_IMPL_H
18 
19 #include "battery_mgr_client_adapter.h"
20 #include "battery_info.h"
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "matching_skills.h"
24 #include "want.h"
25 #include "common_event_subscriber.h"
26 
27 namespace OHOS::NWeb {
28 class WebBatteryInfoImpl final : public WebBatteryInfo {
29 public:
WebBatteryInfoImpl(double level,bool isCharging,int disChargingTime,int chargingTime)30     WebBatteryInfoImpl(double level, bool isCharging, int disChargingTime, int chargingTime)
31         : level_(level), isCharging_(isCharging), disChargingTime_(disChargingTime), chargingTime_(chargingTime) {}
32 
33     ~WebBatteryInfoImpl() override = default;
34 
35     double GetLevel() override;
36 
37     bool IsCharging() override;
38 
39     int DisChargingTime() override;
40 
41     int ChargingTime() override;
42 private:
43     double level_;
44     bool isCharging_;
45     int disChargingTime_;
46     int chargingTime_;
47 };
48 
49 class NWebBatteryEventSubscriber : public EventFwk::CommonEventSubscriber {
50 public:
51     NWebBatteryEventSubscriber(EventFwk::CommonEventSubscribeInfo& in,  std::shared_ptr<WebBatteryEventCallback> cb);
52     ~NWebBatteryEventSubscriber() override = default;
53     void OnReceiveEvent(const EventFwk::CommonEventData &data) override;
54 private:
55     std::shared_ptr<WebBatteryEventCallback> eventCallback_;
56 };
57 
58 class BatteryMgrClientAdapterImpl : public BatteryMgrClientAdapter {
59 public:
60     BatteryMgrClientAdapterImpl() = default;
61     ~BatteryMgrClientAdapterImpl() override = default;
62 
63     void RegBatteryEvent(std::shared_ptr<WebBatteryEventCallback> eventCallback) override;
64 
65     bool StartListen() override;
66 
67     void StopListen() override;
68 
69     std::shared_ptr<WebBatteryInfo> RequestBatteryInfo() override;
70 private:
71     std::shared_ptr<WebBatteryEventCallback> cb_ = nullptr;
72     std::shared_ptr<EventFwk::CommonEventSubscriber> commonEventSubscriber_ = nullptr;
73 };
74 }
75 
76 #endif
77