• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef HIVIEWDFX_HIVIEW_POWER_STATUS_MANAGER_H
16 #define HIVIEWDFX_HIVIEW_POWER_STATUS_MANAGER_H
17 
18 #include <map>
19 
20 #include "common_event_data.h"
21 #include "common_event_subscriber.h"
22 #include "singleton.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace UCollectUtil {
27 using namespace EventFwk;
28 
29 enum PowerState {
30     SCREEN_ON = 0,
31     SCREEN_OFF = 10,
32 };
33 
34 class PowerListener {
35 public:
36     PowerListener() = default;
37     virtual ~PowerListener() = default;
38     virtual void OnScreenOn() = 0;
39     virtual void OnScreenOff() = 0;
40 
41     PowerListener(const PowerListener &) = delete;
42     PowerListener& operator=(const PowerListener&) = delete;
43 };
44 
45 class PowerStateSubscriber : public CommonEventSubscriber {
46 public:
PowerStateSubscriber(const CommonEventSubscribeInfo & subscribeInfo)47     PowerStateSubscriber(const CommonEventSubscribeInfo &subscribeInfo) : CommonEventSubscriber(subscribeInfo) {}
48     void OnReceiveEvent(const CommonEventData &data) override;
49 };
50 
51 class PowerStatusManager : public OHOS::DelayedRefSingleton<PowerStatusManager> {
52 public:
53     PowerStatusManager();
54     ~PowerStatusManager();
55     void SetPowerState(PowerState powerState);
56     int32_t GetPowerState();
57     void AddPowerListener(const std::string &name, std::shared_ptr<PowerListener> listener);
58     void RemovePowerListener(const std::string &name);
59 
60 private:
61     std::mutex mutex_;
62     int32_t powerState_;
63     std::map<std::string, std::shared_ptr<PowerListener>> listeners_;
64     std::shared_ptr<EventFwk::CommonEventSubscriber> powerStateSubscriber_ = nullptr;
65 };
66 }
67 }
68 }
69 #endif //HIVIEWDFX_HIVIEW_POWER_STATUS_MANAGER_H
70