1 /*
2  * Copyright (c) 2023-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 
16 
17 #include "listener_manager_adapter.h"
18 
19 #include "common_event_support.h"
20 
21 #include "standby_service_log.h"
22 #include "device_standby_switch.h"
23 #include "standby_service_impl.h"
24 #ifdef STANDBY_MULTIMODALINPUT_INPUT_ENABLE
25 #include "input_manager_listener.h"
26 #endif
27 #include "standby_service.h"
28 
29 #include "standby_config_manager.h"
30 #include "system_ability_definition.h"
31 #ifdef ENABLE_BACKGROUND_TASK_MGR
32 #include "background_task_listener.h"
33 #endif
34 
35 namespace OHOS {
36 namespace DevStandbyMgr {
Init()37 bool ListenerManagerAdapter::Init()
38 {
39     if (STANDBT_MODE != StandbyMode::PHONEMODE &&
40             STANDBT_MODE != StandbyMode::TABLETMODE &&
41             STANDBT_MODE != StandbyMode::UNKNOWN) {
42         STANDBYSERVICE_LOGD("listener manager plugin initialization failed");
43         return false;
44     }
45     #ifdef STANDBY_MULTIMODALINPUT_INPUT_ENABLE
46     messageListenerList_.emplace_back(std::make_shared<InputManagerListener>());
47     #endif
48     #ifdef ENABLE_BACKGROUND_TASK_MGR
49     // network and running lock strategy need background task listener
50     STANDBYSERVICE_LOGI("add background task listener");
51     std::shared_ptr<IMesssageListener> bgtaskListener_ = std::make_shared<BackgroundTaskListener>();
52     listenerPluginMap_.emplace(BACKGROUND_TASK_MANAGER_SERVICE_ID, bgtaskListener_);
53     #endif
54     STANDBYSERVICE_LOGI("listener manager plugin initialization succeed");
55     return true;
56 }
57 
UnInit()58 bool ListenerManagerAdapter::UnInit()
59 {
60     StopListener();
61     messageListenerList_.clear();
62     return true;
63 }
64 
HandleEvent(const StandbyMessage & message)65 void ListenerManagerAdapter::HandleEvent(const StandbyMessage& message)
66 {
67     switch (message.eventId_) {
68         case StandbyMessageType::SYS_ABILITY_STATUS_CHANGED:
69             UpdateListenerList(message);
70             break;
71         default:
72             break;
73     }
74 }
75 
UpdateListenerList(const StandbyMessage & message)76 void ListenerManagerAdapter::UpdateListenerList(const StandbyMessage& message)
77 {
78     bool isAdded = message.want_->GetBoolParam(SA_STATUS, false);
79     int32_t systemAbilityId = message.want_->GetIntParam(SA_ID, -1);
80     if (isAdded) {
81         // add listener if system ablity started
82         AddSystemServiceListener(systemAbilityId);
83         return;
84     }
85     RemoveSystemServiceListener(systemAbilityId);
86 }
87 
88 // when system ability is added, add relative listener
AddSystemServiceListener(int32_t systemAbilityId)89 void ListenerManagerAdapter::AddSystemServiceListener(int32_t systemAbilityId)
90 {
91     auto iter = listenerPluginMap_.find(systemAbilityId);
92     if (iter == listenerPluginMap_.end()) {
93         return;
94     }
95     STANDBYSERVICE_LOGI("%{public}d added, start listener", systemAbilityId);
96     std::shared_ptr<IMesssageListener> listener = iter->second;
97     if (listener->StartListener() == ERR_OK) {
98         messageListenerList_.emplace_back(listener);
99     }
100 }
101 
102 // when system ability is removed, remove relative listener
RemoveSystemServiceListener(int32_t systemAbilityId)103 void ListenerManagerAdapter::RemoveSystemServiceListener(int32_t systemAbilityId)
104 {
105     auto iter = listenerPluginMap_.find(systemAbilityId);
106     if (iter == listenerPluginMap_.end()) {
107         return;
108     }
109     auto listenerIter = std::remove(messageListenerList_.begin(), messageListenerList_.end(), iter->second);
110     if (listenerIter != messageListenerList_.end()) {
111         messageListenerList_.erase(listenerIter, messageListenerList_.end());
112     }
113 }
114 
StartListener()115 ErrCode ListenerManagerAdapter::StartListener()
116 {
117     for (auto& listener : messageListenerList_) {
118         listener->StartListener();
119     }
120     return ERR_OK;
121 }
122 
StopListener()123 ErrCode ListenerManagerAdapter::StopListener()
124 {
125     for (auto& listener : messageListenerList_) {
126         listener->StopListener();
127     }
128     return ERR_OK;
129 }
130 
ShellDump(const std::vector<std::string> & argsInStr,std::string & result)131 void ListenerManagerAdapter::ShellDump(const std::vector<std::string>& argsInStr, std::string& result)
132 {}
133 }  // namespace DevStandbyMgr
134 }  // namespace OHOS
135