• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "second_ability.h"
17 #include "app_log_wrapper.h"
18 #include "test_utils.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
22 using namespace OHOS::EventFwk;
23 namespace
24 {
25 constexpr int numZero = 0;
26 constexpr int numOne = 1;
27 constexpr int numTwo = 2;
28 constexpr int numThree = 3;
29 } // namespace
30 
31 
Init(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)32 void SecondAbility::Init(const std::shared_ptr<AbilityInfo> &abilityInfo,
33     const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
34     const sptr<IRemoteObject> &token)
35 {
36     APP_LOGI("SecondAbility::Init");
37     Ability::Init(abilityInfo, application, handler, token);
38 }
39 
~SecondAbility()40 SecondAbility::~SecondAbility()
41 {
42     CommonEventManager::UnSubscribeCommonEvent(subscriber_);
43 }
44 
OnStart(const Want & want)45 void SecondAbility::OnStart(const Want &want)
46 {
47     APP_LOGI("SecondAbility::onStart");
48     SubscribeEvent();
49     Ability::OnStart(want);
50     TestUtils::PublishEvent(g_EVENT_RESP_SECOND, Ability::GetState(), "onStart");
51 }
52 
OnStop()53 void SecondAbility::OnStop()
54 {
55     APP_LOGI("SecondAbility::OnStop");
56     Ability::OnStop();
57     CommonEventManager::UnSubscribeCommonEvent(subscriber_);
58     TestUtils::PublishEvent(g_EVENT_RESP_SECOND, Ability::GetState(), "OnStop");
59 }
60 
OnActive()61 void SecondAbility::OnActive()
62 {
63     APP_LOGI("SecondAbility::OnActive");
64     Ability::OnActive();
65     TestUtils::PublishEvent(g_EVENT_RESP_SECOND, Ability::GetState(), "OnActive");
66 }
67 
OnInactive()68 void SecondAbility::OnInactive()
69 {
70     APP_LOGI("SecondAbility::OnInactive");
71     Ability::OnInactive();
72     TestUtils::PublishEvent(g_EVENT_RESP_SECOND, Ability::GetState(), "OnInactive");
73 }
74 
OnBackground()75 void SecondAbility::OnBackground()
76 {
77     APP_LOGI("SecondAbility::OnBackground");
78     Ability::OnBackground();
79     TestUtils::PublishEvent(g_EVENT_RESP_SECOND, Ability::GetState(), "OnBackground");
80 }
81 
OnForeground(const Want & want)82 void SecondAbility::OnForeground(const Want &want)
83 {
84     APP_LOGI("SecondAbility::OnForeground");
85     Ability::OnForeground(want);
86     TestUtils::PublishEvent(g_EVENT_RESP_SECOND, Ability::GetState(), "OnForeground");
87 }
OnCommand(const AAFwk::Want & want,bool restart,int startId)88 void SecondAbility::OnCommand(const AAFwk::Want &want, bool restart, int startId)
89 {
90     APP_LOGI("SecondAbility::OnCommand");
91 
92     Ability::OnCommand(want, restart, startId);
93     TestUtils::PublishEvent(g_EVENT_RESP_SECOND, AbilityLifecycleExecutor::LifecycleState::ACTIVE, "OnCommand");
94 }
SubscribeEvent()95 void SecondAbility::SubscribeEvent()
96 {
97     std::vector<std::string> eventList = {
98         g_EVENT_REQU_SECOND,
99     };
100     MatchingSkills matchingSkills;
101     for (const auto &e : eventList) {
102         matchingSkills.AddEvent(e);
103     }
104     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
105     subscribeInfo.SetPriority(1);
106     subscriber_ = std::make_shared<SecondEventSubscriber>(subscribeInfo);
107     subscriber_->mainAbility = this;
108     CommonEventManager::SubscribeCommonEvent(subscriber_);
109 }
110 
OnReceiveEvent(const CommonEventData & data)111 void SecondEventSubscriber::OnReceiveEvent(const CommonEventData &data)
112 {
113     APP_LOGI("SecondEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
114     APP_LOGI("SecondEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
115     APP_LOGI("SecondEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
116     auto eventName = data.GetWant().GetAction();
117     if (std::strcmp(eventName.c_str(), g_EVENT_REQU_SECOND.c_str()) == 0) {
118         auto target = data.GetData();
119         auto caseInfo = TestUtils::split(target, "_");
120         if (caseInfo.size() < numThree) {
121             return;
122         }
123         if (mapTestFunc_.find(caseInfo[numZero]) != mapTestFunc_.end()) {
124             mapTestFunc_[caseInfo[numZero]](std::stoi(caseInfo[numOne]), std::stoi(caseInfo[numTwo]), data.GetCode());
125         } else {
126             APP_LOGI("OnReceiveEvent: CommonEventData error(%{public}s)", target.c_str());
127         }
128     }
129 }
130 
TestDispatcher(int apiIndex,int caseIndex,int code)131 void SecondAbility::TestDispatcher(int apiIndex, int caseIndex, int code)
132 {
133     APP_LOGI("SecondAbility::TestAbility");
134     if (mapCase_.find(apiIndex) != mapCase_.end()) {
135         if (caseIndex < (int)mapCase_[apiIndex].size()) {
136             mapCase_[apiIndex][caseIndex](code);
137         }
138     }
139 }
140 
ExtraCase1(int code)141 void SecondAbility::ExtraCase1(int code)
142 {}
143 
144 REGISTER_AA(SecondAbility)
145 }  // namespace AppExecFwk
146 }  // namespace OHOS
147