• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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->componentType_ = 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,const std::shared_ptr<Ability> & ability)38 void STPageAbilityEvent::SubscribeEvent(std::vector<std::string> eventList, const std::shared_ptr<Ability> &ability)
39 {
40     MatchingSkills matchingSkills;
41     for (const auto &e : eventList) {
42         matchingSkills.AddEvent(e);
43     }
44     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
45     subscribeInfo.SetPriority(1);
46     subscriber_ = std::make_shared<STPageAbilityEventSubscriber>(subscribeInfo, ability);
47     CommonEventManager::SubscribeCommonEvent(subscriber_);
48 }
49 
UnsubscribeEvent()50 void STPageAbilityEvent::UnsubscribeEvent()
51 {
52     CommonEventManager::UnSubscribeCommonEvent(subscriber_);
53 }
54 
GetEventDate(const std::string & stateCallbackCount)55 std::string STPageAbilityEvent::GetEventDate(const std::string &stateCallbackCount)
56 {
57     return this->componentType_ + stateCallbackCount;
58 }
59 
GetCallBackPath(const std::string & callBackPath)60 std::string STPageAbilityEvent::GetCallBackPath(const std::string &callBackPath)
61 {
62     this->callBackPath_ += callBackPath;
63     return this->callBackPath_;
64 }
65 
GetAbilityStatus(const std::string & abilityStatus)66 std::string STPageAbilityEvent::GetAbilityStatus(const std::string &abilityStatus)
67 {
68     this->abilityStatus_ += abilityStatus;
69     return this->abilityStatus_;
70 }
71 
GetOnStartCount()72 int STPageAbilityEvent::GetOnStartCount()
73 {
74     onStartCount_++;
75     return onStartCount_;
76 }
77 
GetOnStopCount()78 int STPageAbilityEvent::GetOnStopCount()
79 {
80     onStopCount_++;
81     return onStopCount_;
82 }
83 
GetOnActiveCount()84 int STPageAbilityEvent::GetOnActiveCount()
85 {
86     onActiveCount_++;
87     return onActiveCount_;
88 }
89 
GetOnInactiveCount()90 int STPageAbilityEvent::GetOnInactiveCount()
91 {
92     onInactiveCount_++;
93     return onInactiveCount_;
94 }
95 
GetOnBackgroundCount()96 int STPageAbilityEvent::GetOnBackgroundCount()
97 {
98     onBackgroundCount_++;
99     return onBackgroundCount_;
100 }
101 
GetOnForegroundCount()102 int STPageAbilityEvent::GetOnForegroundCount()
103 {
104     onForegroundCount_++;
105     return onForegroundCount_;
106 }
107 
GetOnNewWantCount()108 int STPageAbilityEvent::GetOnNewWantCount()
109 {
110     onNewWantCount_++;
111     return onNewWantCount_;
112 }
113 
OnReceiveEvent(const CommonEventData & data)114 void STPageAbilityEventSubscriber::OnReceiveEvent(const CommonEventData &data)
115 {
116     APP_LOGI("DataTestPageAEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
117     APP_LOGI("DataTestPageAEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
118     APP_LOGI("DataTestPageAEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
119     auto eventName = data.GetWant().GetAction();
120     if (!eventName.compare("requ_page_ability_terminate")) {
121         std::string target = data.GetData();
122         if (!target.compare(this->ability_->GetAbilityName())) {
123             this->ability_->TerminateAbility();
124         }
125     }
126 }
127 } // namespace AppExecFwk
128 } // namespace OHOS