• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "intell_voice_service_manager.h"
21 
22 #define LOG_TAG "SystemEventObserver"
23 
24 using namespace OHOS::AppExecFwk;
25 using namespace OHOS::EventFwk;
26 
27 namespace OHOS {
28 namespace IntellVoiceEngine {
SystemEventObserver(const OHOS::EventFwk::CommonEventSubscribeInfo & subscribeInfo,SystemEventReceiver receiver)29 SystemEventObserver::SystemEventObserver(const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo,
30     SystemEventReceiver receiver) : EventFwk::CommonEventSubscriber(subscribeInfo), receiver_(receiver)
31 {
32     INTELL_VOICE_LOG_INFO("SystemEventObserver create");
33 }
34 
~SystemEventObserver()35 SystemEventObserver::~SystemEventObserver()
36 {
37 }
38 
Create(const OHOS::EventFwk::CommonEventSubscribeInfo & subscribeInfo,SystemEventReceiver receiver)39 std::shared_ptr<SystemEventObserver> SystemEventObserver::Create(
40     const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo, SystemEventReceiver receiver)
41 {
42     return std::shared_ptr<SystemEventObserver>(new (std::nothrow) SystemEventObserver(subscribeInfo, receiver));
43 }
44 
Subscribe()45 bool SystemEventObserver::Subscribe()
46 {
47     std::lock_guard<std::mutex> lock(mutex_);
48     if (state_ != IDLE) {
49         INTELL_VOICE_LOG_INFO("state(%{public}d) is not idle", state_);
50         return true;
51     }
52     if (!OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(GetPtr())) {
53         INTELL_VOICE_LOG_ERROR("SubscribeCommonEvent occur exception.");
54         return false;
55     }
56     state_ = SUBSCRIBED;
57     return true;
58 }
59 
Unsubscribe()60 bool SystemEventObserver::Unsubscribe()
61 {
62     std::lock_guard<std::mutex> lock(mutex_);
63     if (state_ != SUBSCRIBED) {
64         INTELL_VOICE_LOG_INFO("state(%{public}d) is not subscribe", state_);
65         return true;
66     }
67     if (!OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(GetPtr())) {
68         INTELL_VOICE_LOG_ERROR("UnsubscribeCommonEvent occur exception.");
69     }
70     state_ = UNSUBSCRIBED;
71     return true;
72 }
73 
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & eventData)74 void SystemEventObserver::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData)
75 {
76     INTELL_VOICE_LOG_INFO("enter");
77 
78     const OHOS::AAFwk::Want& want = eventData.GetWant();
79     std::string action = want.GetAction();
80     if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
81         INTELL_VOICE_LOG_INFO("COMMON_EVENT_SCREEN_ON");
82         ServiceManagerType::GetInstance().SetScreenOff(false);
83     } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
84         INTELL_VOICE_LOG_INFO("COMMON_EVENT_SCREEN_OFF");
85         ServiceManagerType::GetInstance().SetScreenOff(true);
86     } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
87         INTELL_VOICE_LOG_INFO("COMMON_EVENT_PACKAGE_REMOVED");
88     } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED) {
89         INTELL_VOICE_LOG_INFO("COMMON_EVENT_PACKAGE_REPLACED");
90     } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
91         INTELL_VOICE_LOG_INFO("COMMON_EVENT_PACKAGE_DATA_CLEARED");
92     } else if (action == OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY) {
93         INTELL_VOICE_LOG_INFO("COMMON_EVENT_DATA_SHARE_READY");
94         if (receiver_ != nullptr) {
95             receiver_(eventData);
96         }
97     } else {
98         INTELL_VOICE_LOG_INFO("unkonw event, action:%{public}s", action.c_str());
99     }
100 }
101 }
102 }