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