• 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 #include "power_status_manager.h"
16 
17 #include "common_event_support.h"
18 #include "common_event_manager.h"
19 #include "hiview_logger.h"
20 #include "power_mgr_client.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace UCollectUtil {
25 DEFINE_LOG_TAG("UCollectUtil-PowerState");
OnReceiveEvent(const CommonEventData & data)26 void PowerStateSubscriber::OnReceiveEvent(const CommonEventData &data)
27 {
28     std::string action = data.GetWant().GetAction();
29     HIVIEW_LOGI("OnReceiveEvent action%{public}s", action.c_str());
30     if (action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
31         PowerStatusManager::GetInstance().SetPowerState(SCREEN_ON);
32     } else if (action == CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
33         PowerStatusManager::GetInstance().SetPowerState(SCREEN_OFF);
34     }
35 }
36 
PowerStatusManager()37 PowerStatusManager::PowerStatusManager()
38 {
39     powerState_ = PowerMgr::PowerMgrClient::GetInstance().IsScreenOn() ? SCREEN_ON : SCREEN_OFF;
40     MatchingSkills matchingSkills;
41     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
42     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
43     CommonEventSubscribeInfo info(matchingSkills);
44     powerStateSubscriber_ = std::make_shared<PowerStateSubscriber>(info);
45     if (!CommonEventManager::SubscribeCommonEvent(powerStateSubscriber_)) {
46         HIVIEW_LOGE("register PowerStateSubscriber fail");
47     }
48 }
49 
~PowerStatusManager()50 PowerStatusManager::~PowerStatusManager()
51 {
52     if (powerStateSubscriber_ != nullptr) {
53         CommonEventManager::UnSubscribeCommonEvent(powerStateSubscriber_);
54     }
55 }
56 
SetPowerState(PowerState powerState)57 void PowerStatusManager::SetPowerState(PowerState powerState)
58 {
59     std::unique_lock<std::mutex> lock(mutex_);
60     powerState_ = powerState;
61     HIVIEW_LOGI("listeners size:%{public}zu", listeners_.size());
62     for (const auto& it : listeners_) {
63         if (powerState == SCREEN_ON) {
64             it.second->OnScreenOn();
65         } else {
66             it.second->OnScreenOff();
67         }
68     }
69 }
70 
GetPowerState()71 int32_t PowerStatusManager::GetPowerState()
72 {
73     std::unique_lock<std::mutex> lock(mutex_);
74     return powerState_;
75 }
76 
AddPowerListener(const std::string & name,std::shared_ptr<PowerListener> listener)77 void PowerStatusManager::AddPowerListener(const std::string &name, std::shared_ptr<PowerListener> listener)
78 {
79     std::unique_lock<std::mutex> lock(mutex_);
80     if (listeners_.find(name) == listeners_.end()) {
81         listeners_[name] = listener;
82     }
83 }
84 
RemovePowerListener(const std::string & name)85 void PowerStatusManager::RemovePowerListener(const std::string &name)
86 {
87     std::unique_lock<std::mutex> lock(mutex_);
88     auto it = listeners_.find(name);
89     if (it != listeners_.end()) {
90         listeners_.erase(it);
91     }
92 }
93 }
94 }
95 }