1 /* 2 * Copyright (c) 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 #include "system_event_observer.h" 16 #include "event_handler.h" 17 #include "common_event_manager.h" 18 #include "common_event_support.h" 19 #include "intell_voice_log.h" 20 21 #define LOG_TAG "SystemEventObserver" 22 23 using namespace OHOS::AppExecFwk; 24 using namespace OHOS::EventFwk; 25 26 namespace OHOS { 27 namespace IntellVoiceEngine { SystemEventObserver(const OHOS::EventFwk::CommonEventSubscribeInfo & subscribeInfo)28SystemEventObserver::SystemEventObserver(const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo) 29 : EventFwk::CommonEventSubscriber(subscribeInfo), handler_(nullptr) 30 { 31 INTELL_VOICE_LOG_INFO("SystemEventObserver create"); 32 } 33 ~SystemEventObserver()34SystemEventObserver::~SystemEventObserver() 35 { 36 handler_ = nullptr; 37 } 38 Create(const OHOS::EventFwk::CommonEventSubscribeInfo & subscribeInfo)39std::shared_ptr<SystemEventObserver> SystemEventObserver::Create( 40 const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo) 41 { 42 return std::shared_ptr<SystemEventObserver>(new (std::nothrow) SystemEventObserver(subscribeInfo)); 43 } 44 Subscribe()45bool SystemEventObserver::Subscribe() 46 { 47 if (!OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(GetPtr())) { 48 INTELL_VOICE_LOG_ERROR("SubscribeCommonEvent occur exception."); 49 return false; 50 } 51 return true; 52 } 53 Unsubscribe()54bool SystemEventObserver::Unsubscribe() 55 { 56 if (!OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(GetPtr())) { 57 INTELL_VOICE_LOG_ERROR("UnsubscribeCommonEvent occur exception."); 58 return false; 59 } 60 return true; 61 } 62 SetEventHandler(const std::shared_ptr<OHOS::AppExecFwk::EventHandler> & handler)63void SystemEventObserver::SetEventHandler(const std::shared_ptr<OHOS::AppExecFwk::EventHandler> &handler) 64 { 65 handler_ = handler; 66 } 67 OnReceiveEvent(const OHOS::EventFwk::CommonEventData & eventData)68void SystemEventObserver::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) 69 { 70 INTELL_VOICE_LOG_INFO("enter"); 71 72 const OHOS::AAFwk::Want& want = eventData.GetWant(); 73 std::string action = want.GetAction(); 74 if (action.empty()) { 75 INTELL_VOICE_LOG_ERROR("action is empty"); 76 return; 77 } 78 79 if (handler_ == nullptr) { 80 INTELL_VOICE_LOG_ERROR("handler_ is nullptr"); 81 return; 82 } 83 84 INTELL_VOICE_LOG_INFO("action:%{public}s.", action.c_str()); 85 if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) { 86 INTELL_VOICE_LOG_INFO("COMMON_EVENT_SCREEN_ON"); 87 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) { 88 INTELL_VOICE_LOG_INFO("COMMON_EVENT_SCREEN_OFF"); 89 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { 90 INTELL_VOICE_LOG_INFO("COMMON_EVENT_PACKAGE_REMOVED"); 91 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED) { 92 INTELL_VOICE_LOG_INFO("COMMON_EVENT_PACKAGE_REPLACED"); 93 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) { 94 INTELL_VOICE_LOG_INFO("COMMON_EVENT_PACKAGE_DATA_CLEARED"); 95 } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) { 96 INTELL_VOICE_LOG_INFO("COMMON_EVENT_USER_UNLOCKED"); 97 } else { 98 INTELL_VOICE_LOG_INFO("unkonw event"); 99 } 100 } 101 } 102 }