1/*
2 * Copyright (c) 2021-2023 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 CHARGER_THREAD_H
17#define CHARGER_THREAD_H
18
19#include <thread>
20
21#include "battery_backlight.h"
22#include "battery_led.h"
23#include "battery_thread.h"
24#include "battery_vibrate.h"
25#include "charger_animation.h"
26#include <linux/input.h>
27#include <input_manager.h>
28
29namespace OHOS {
30namespace PowerMgr {
31class ChargerThread : public BatteryThread {
32private:
33    void Init();
34    static void UpdateAnimation(const int32_t& chargeState, const int32_t& capacity);
35    void Run(void* service) override;
36    void UpdateBatteryInfo(void* arg) override;
37    void HandleChargingState();
38    void HandleScreenState();
39    void HandleTemperature(const int32_t& temperature);
40    void HandleCapacity(const int32_t& capacity);
41    void HandleStates() override;
42    int32_t UpdateWaitInterval() override;
43    void CycleMatters() override;
44    static void InitAnimation();
45    void SetKeyWait(struct KeyState& key, int64_t timeout);
46    void InputMonitorInit();
47    void InputMonitorCancel();
48    void HandlePowerKeyState();
49    void HandlePowerKey(int32_t keycode, int64_t now);
50    void InitLackPowerCapacity();
51    void InitBatteryFileSystem();
52    void InitVibration();
53    void InitBacklight();
54    void InitLed();
55    std::unique_ptr<PowerSupplyProvider> provider_ = nullptr;
56    std::unique_ptr<BatteryVibrate> vibrate_ = nullptr;
57    std::unique_ptr<BatteryBacklight> backlight_ = nullptr;
58    std::unique_ptr<BatteryLed> led_ = nullptr;
59
60    static const int32_t INVALID = -1;
61    int64_t keyWait_ = INVALID;
62    int64_t backlightWait_ = INVALID;
63    int32_t capacity_ = INVALID;
64    int32_t chargeState_ = PowerSupplyProvider::BatteryChargeState::CHARGE_STATE_RESERVED;
65    bool started_ = false;
66
67    static std::unique_ptr<ChargerAnimation> animation_;
68    static bool isChargeStateChanged_;
69    static bool isConfigParse_;
70    static int32_t lackPowerCapacity_;
71    int32_t inputMonitorId_ {-1};
72};
73
74class ChargerThreadInputMonitor : public MMI::IInputEventConsumer {
75public:
76    virtual void OnInputEvent(std::shared_ptr<OHOS::MMI::KeyEvent> keyEvent) const;
77    virtual void OnInputEvent(std::shared_ptr<OHOS::MMI::PointerEvent> pointerEvent) const;
78    virtual void OnInputEvent(std::shared_ptr<OHOS::MMI::AxisEvent> axisEvent) const;
79
80private:
81    void SetKeyState(int32_t code, int32_t value, int64_t now) const;
82};
83} // namespace PowerMgr
84} // namespace OHOS
85#endif
86