1 /*
2 * Copyright (c) 2021 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 #include "stpageabilityevent.h"
17
18 namespace OHOS {
19 namespace AppExecFwk {
20 using namespace OHOS::EventFwk;
21
STPageAbilityEvent(const std::string & className)22 STPageAbilityEvent::STPageAbilityEvent(const std::string &className)
23 {
24 this->className_ = className;
25 }
26
PublishEvent(const std::string & eventName,const int & code,const std::string & data)27 bool STPageAbilityEvent::PublishEvent(const std::string &eventName, const int &code, const std::string &data)
28 {
29 Want want;
30 want.SetAction(eventName);
31 CommonEventData commonData;
32 commonData.SetWant(want);
33 commonData.SetCode(code);
34 commonData.SetData(data);
35 return CommonEventManager::PublishCommonEvent(commonData);
36 }
37
SubscribeEvent(std::vector<std::string> eventList,std::shared_ptr<Ability> ability,sptr<AAFwk::IAbilityConnection> stub)38 void STPageAbilityEvent::SubscribeEvent(
39 std::vector<std::string> eventList, std::shared_ptr<Ability> ability, sptr<AAFwk::IAbilityConnection> stub)
40 {
41 MatchingSkills matchingSkills;
42 for (const auto &e : eventList) {
43 matchingSkills.AddEvent(e);
44 }
45 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
46 subscribeInfo.SetPriority(1);
47 subscriber_ = std::make_shared<STPageAbilityEventSubscriber>(subscribeInfo, ability, stub);
48 CommonEventManager::SubscribeCommonEvent(subscriber_);
49 }
50
UnsubscribeEvent()51 void STPageAbilityEvent::UnsubscribeEvent()
52 {
53 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
54 }
55
GetEventDate(const std::string & stateCallbackCount)56 std::string STPageAbilityEvent::GetEventDate(const std::string &stateCallbackCount)
57 {
58 return this->className_ + stateCallbackCount;
59 }
60
GetCallBackPath(const std::string & callBackPath)61 std::string STPageAbilityEvent::GetCallBackPath(const std::string &callBackPath)
62 {
63 this->callBackPath_ += callBackPath;
64 return this->callBackPath_;
65 }
66
GetAbilityStatus(const std::string & abilityStatus)67 std::string STPageAbilityEvent::GetAbilityStatus(const std::string &abilityStatus)
68 {
69 this->abilityStatus_ += abilityStatus;
70 return this->abilityStatus_;
71 }
72
GetOnStartCount()73 int STPageAbilityEvent::GetOnStartCount()
74 {
75 onStartCount_++;
76 return onStartCount_;
77 }
78
GetOnStopCount()79 int STPageAbilityEvent::GetOnStopCount()
80 {
81 onStopCount_++;
82 return onStopCount_;
83 }
84
GetOnActiveCount()85 int STPageAbilityEvent::GetOnActiveCount()
86 {
87 onActiveCount_++;
88 return onActiveCount_;
89 }
90
GetOnInactiveCount()91 int STPageAbilityEvent::GetOnInactiveCount()
92 {
93 onInactiveCount_++;
94 return onInactiveCount_;
95 }
96
GetOnBackgroundCount()97 int STPageAbilityEvent::GetOnBackgroundCount()
98 {
99 onBackgroundCount_++;
100 return onBackgroundCount_;
101 }
102
GetOnForegroundCount()103 int STPageAbilityEvent::GetOnForegroundCount()
104 {
105 onForegroundCount_++;
106 return onForegroundCount_;
107 }
108
GetOnNewWantCount()109 int STPageAbilityEvent::GetOnNewWantCount()
110 {
111 onNewWantCount_++;
112 return onNewWantCount_;
113 }
114
GetOnCommandCount()115 int STPageAbilityEvent::GetOnCommandCount()
116 {
117 onCommandCount_++;
118 return onCommandCount_;
119 }
120
GetOnConnectCount()121 int STPageAbilityEvent::GetOnConnectCount()
122 {
123 onConnectCount_++;
124 return onConnectCount_;
125 }
126
GetOnDisconnectCount()127 int STPageAbilityEvent::GetOnDisconnectCount()
128 {
129 onDisconnectCount_++;
130 return onDisconnectCount_;
131 }
132
OnReceiveEvent(const CommonEventData & data)133 void STPageAbilityEventSubscriber::OnReceiveEvent(const CommonEventData &data)
134 {
135 APP_LOGI("DataTestPageAEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
136 APP_LOGI("DataTestPageAEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
137 APP_LOGI("DataTestPageAEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
138 auto eventName = data.GetWant().GetAction();
139 if (!this->ability_.lock()) {
140 APP_LOGI("STPageAbilityEventSubscriber:ability_ is nullptr");
141 }
142 if (eventName.compare("requ_page_ability_terminate") == 0) {
143 std::string target = data.GetData();
144 if (target.compare((this->ability_.lock())->GetAbilityName()) == 0) {
145 (this->ability_.lock())->TerminateAbility();
146 }
147 }
148 if (eventName.compare("requ_get_process_memory_info") == 0) {
149 }
150 if (eventName.compare("requ_disconnect_service") == 0) {
151 std::string target = data.GetData();
152 if (target.compare((this->ability_.lock())->GetAbilityName()) == 0) {
153 if (stub_.promote()) {
154 (this->ability_.lock())->DisconnectAbility(stub_.promote());
155 }
156 APP_LOGI("GetMyProcessMemoryInfo:stub_ is nullptr");
157 }
158 }
159 if (eventName.compare("requ_page_ability_terminate_caller") == 0) {
160 std::string target = data.GetData();
161 if (target.compare((this->ability_.lock())->GetAbilityName()) == 0) {
162 int requestCode = data.GetCode();
163 (this->ability_.lock())->TerminateAbility(requestCode);
164 }
165 }
166 }
167
RunningProcessInfoToString(std::vector<RunningProcessInfo> & infos)168 std::string STPageAbilityEventSubscriber::RunningProcessInfoToString(std::vector<RunningProcessInfo> &infos)
169 {
170 std::string data = "";
171 return data;
172 }
173
PublishEvent(const std::string & eventName,const int & code,const std::string & data)174 bool STPageAbilityEventSubscriber::PublishEvent(const std::string &eventName, const int &code, const std::string &data)
175 {
176 Want want;
177 want.SetAction(eventName);
178 CommonEventData commonData;
179 commonData.SetWant(want);
180 commonData.SetCode(code);
181 commonData.SetData(data);
182 return CommonEventManager::PublishCommonEvent(commonData);
183 }
184 } // namespace AppExecFwk
185 } // namespace OHOS