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)26void PowerStateSubscriber::OnReceiveEvent(const CommonEventData &data) 27 { 28 std::string action = data.GetWant().GetAction(); 29 HIVIEW_LOGD("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()37PowerStatusManager::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()50PowerStatusManager::~PowerStatusManager() 51 { 52 if (powerStateSubscriber_ != nullptr) { 53 CommonEventManager::UnSubscribeCommonEvent(powerStateSubscriber_); 54 } 55 } 56 SetPowerState(PowerState powerState)57void PowerStatusManager::SetPowerState(PowerState powerState) 58 { 59 std::unique_lock<std::mutex> lock(mutex_); 60 powerState_ = powerState; 61 } 62 GetPowerState()63int32_t PowerStatusManager::GetPowerState() 64 { 65 std::unique_lock<std::mutex> lock(mutex_); 66 return powerState_; 67 } 68 } 69 } 70 }