• 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 "third_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 using namespace OHOS::AAFwk;
24 
Init(const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<OHOSApplication> & application,std::shared_ptr<AbilityHandler> & handler,const sptr<IRemoteObject> & token)25 void ThirdAbility::Init(const std::shared_ptr<AbilityInfo> &abilityInfo,
26     const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
27     const sptr<IRemoteObject> &token)
28 {
29     APP_LOGI("ThirdAbility::Init");
30     Ability::Init(abilityInfo, application, handler, token);
31 }
32 
~ThirdAbility()33 ThirdAbility::~ThirdAbility()
34 {
35     CommonEventManager::UnSubscribeCommonEvent(subscriber_);
36 }
37 
OnStart(const Want & want)38 void ThirdAbility::OnStart(const Want &want)
39 {
40     APP_LOGI("ThirdAbility::OnStart");
41     SubscribeEvent();
42     Ability::OnStart(want);
43     callbackSeq += "OnStart";
44     TestUtils::PublishEvent(g_EVENT_RESP_THIRD_LIFECYCLE, THIRD_ABILITY_CODE, "OnStart");
45 }
46 
OnStop()47 void ThirdAbility::OnStop()
48 {
49     APP_LOGI("ThirdAbility::OnStop");
50     Ability::OnStop();
51     callbackSeq += "OnStop";  // OnInactiveOnBackgroundOnStop
52     CommonEventManager::UnSubscribeCommonEvent(subscriber_);
53     TestUtils::PublishEvent(g_EVENT_RESP_THIRD_LIFECYCLE, THIRD_ABILITY_CODE, callbackSeq);
54     callbackSeq = "";
55 }
56 
OnActive()57 void ThirdAbility::OnActive()
58 {
59     APP_LOGI("ThirdAbility::OnActive====<");
60     Ability::OnActive();
61     callbackSeq += "OnActive";  // OnStartOnActive
62     TestUtils::PublishEvent(g_EVENT_RESP_THIRD_LIFECYCLE, THIRD_ABILITY_CODE, callbackSeq);
63     callbackSeq = "";
64 }
65 
OnConfigurationUpdated(const Configuration & configuration)66 void ThirdAbility::OnConfigurationUpdated(const Configuration &configuration)
67 {
68     APP_LOGI("ThirdAbility::OnConfigurationUpdated====<");
69     Ability::OnConfigurationUpdated(configuration);
70     callbackUpdated += "Updated";  // UpdatedUpdated
71     TestUtils::PublishEvent(g_EVENT_RESP_THIRD_LIFECYCLE, THIRD_ABILITY_CODE, callbackUpdated);
72 }
73 
OnInactive()74 void ThirdAbility::OnInactive()
75 {
76     APP_LOGI("ThirdAbility::OnInactive");
77     Ability::OnInactive();
78     callbackSeq += "OnInactive";
79     TestUtils::PublishEvent(g_EVENT_RESP_THIRD_LIFECYCLE, THIRD_ABILITY_CODE, "OnInactive");
80 }
81 
OnBackground()82 void ThirdAbility::OnBackground()
83 {
84     APP_LOGI("ThirdAbility::OnBackground");
85     Ability::OnBackground();
86     callbackSeq += "OnBackground";
87     TestUtils::PublishEvent(g_EVENT_RESP_THIRD_LIFECYCLE, THIRD_ABILITY_CODE, "OnBackground");
88 }
89 
OnForeground(const Want & want)90 void ThirdAbility::OnForeground(const Want &want)
91 {
92     APP_LOGI("ThirdAbility::OnForeground");
93     Ability::OnForeground(want);
94     callbackSeq += "OnForeground";
95     TestUtils::PublishEvent(g_EVENT_RESP_THIRD_LIFECYCLE, THIRD_ABILITY_CODE, "OnForeground");
96 }
97 
SubscribeEvent()98 void ThirdAbility::SubscribeEvent()
99 {
100     std::vector<std::string> eventList = {
101         // g_EVENT_REQU_THIRD,
102     };
103     MatchingSkills matchingSkills;
104     for (const auto &e : eventList) {
105         matchingSkills.AddEvent(e);
106     }
107     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
108     subscribeInfo.SetPriority(1);
109     subscriber_ = std::make_shared<ThirdAbilityEventSubscriber>(subscribeInfo);
110     subscriber_->thirdAbility = this;
111     CommonEventManager::SubscribeCommonEvent(subscriber_);
112 }
113 
OnReceiveEvent(const CommonEventData & data)114 void ThirdAbilityEventSubscriber::OnReceiveEvent(const CommonEventData &data)
115 {
116     APP_LOGI("ThirdAbilityEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
117     APP_LOGI("ThirdAbilityEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
118     APP_LOGI("ThirdAbilityEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());
119     auto eventName = data.GetWant().GetAction();
120 }
121 
TestAbility(int apiIndex,int caseIndex,int code)122 void ThirdAbility::TestAbility(int apiIndex, int caseIndex, int code)
123 {
124     APP_LOGI("ThirdAbility::TestAbility");
125     if (mapCase_.find(apiIndex) != mapCase_.end()) {
126         if (caseIndex < (int)mapCase_[apiIndex].size()) {
127             mapCase_[apiIndex][caseIndex](code);
128         }
129     }
130 }
131 
132 REGISTER_AA(ThirdAbility)
133 }  // namespace AppExecFwk
134 }  // namespace OHOS
135