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