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 "ability_state_second.h"
17 #include <string>
18 #include "test_utils.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 using namespace OHOS::EventFwk;
23 std::string FwkAbilityState_Event_Requ_B = "requ_com_ohos_amsst_FwkAbilityStateB";
24 std::string FwkAbilityState_Event_Resp_B = "resp_com_ohos_amsst_FwkAbilityStateB";
25 int OnActiveCode = 1;
26
~FwkAbilityStateSecond()27 FwkAbilityStateSecond::~FwkAbilityStateSecond()
28 {
29 CommonEventManager::UnSubscribeCommonEvent(subscriber_);
30 }
31
OnStart(const Want & want)32 void FwkAbilityStateSecond::OnStart(const Want &want)
33 {
34 SubscribeEvent();
35 HILOG_INFO("FwkAbilityStateSecond::onStart");
36 Ability::OnStart(want);
37 callback_seq += "OnStart";
38 }
39
OnForeground(const Want & want)40 void FwkAbilityStateSecond::OnForeground(const Want &want)
41 {
42 HILOG_INFO("FwkAbilityStateSecond::OnForeground");
43 Ability::OnForeground(want);
44 }
45
OnNewWant(const Want & want)46 void FwkAbilityStateSecond::OnNewWant(const Want &want)
47 {
48 HILOG_INFO("FwkAbilityStateSecond::OnNewWant");
49 Ability::OnNewWant(want);
50 }
51
OnStop()52 void FwkAbilityStateSecond::OnStop()
53 {
54 HILOG_INFO("FwkAbilityStateSecond::onStop");
55 Ability::OnStop();
56 }
57
OnActive()58 void FwkAbilityStateSecond::OnActive()
59 {
60 HILOG_INFO("FwkAbilityStateSecond::OnActive");
61 Ability::OnActive();
62 callback_seq += "OnActive";
63 TestUtils::PublishEvent(FwkAbilityState_Event_Resp_B, OnActiveCode, callback_seq);
64 callback_seq = "";
65 }
66
OnInactive()67 void FwkAbilityStateSecond::OnInactive()
68 {
69 HILOG_INFO("FwkAbilityStateSecond::OnInactive");
70 Ability::OnInactive();
71 }
72
OnBackground()73 void FwkAbilityStateSecond::OnBackground()
74 {
75 HILOG_INFO("FwkAbilityStateSecond::OnBackground");
76 Ability::OnBackground();
77 }
78
OnSaveAbilityState(PacMap & outState)79 void FwkAbilityStateSecond::OnSaveAbilityState(PacMap &outState)
80 {
81 HILOG_INFO("FwkAbilityStateSecond::OnSaveAbilityState");
82 Ability::OnSaveAbilityState(outState);
83 }
84
OnRestoreAbilityState(const PacMap & inState)85 void FwkAbilityStateSecond::OnRestoreAbilityState(const PacMap &inState)
86 {
87 HILOG_INFO("FwkAbilityStateSecond::OnRestoreAbilityState");
88 Ability::OnRestoreAbilityState(inState);
89 }
90
91
SubscribeEvent()92 void FwkAbilityStateSecond::SubscribeEvent()
93 {
94 std::vector<std::string> eventList = {
95 FwkAbilityState_Event_Requ_B,
96 };
97 MatchingSkills matchingSkills;
98 for (const auto &e : eventList) {
99 matchingSkills.AddEvent(e);
100 }
101 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
102 subscribeInfo.SetPriority(1);
103 subscriber_ = std::make_shared<FwkAbilityStateSecondSubscriber>(subscribeInfo);
104 subscriber_->mainAbility = this;
105 CommonEventManager::SubscribeCommonEvent(subscriber_);
106 }
107
OnReceiveEvent(const CommonEventData & data)108 void FwkAbilityStateSecondSubscriber::OnReceiveEvent(const CommonEventData &data)
109 {
110 auto eventName = data.GetWant().GetAction();
111 if (strcmp(eventName.c_str(), FwkAbilityState_Event_Requ_B.c_str()) == 0) {
112 auto target = data.GetData();
113 if (mapAction_.find(target) != mapAction_.end()) {
114 mapAction_[target](target, data.GetCode());
115 } else {
116 HILOG_INFO("OnReceiveEvent: CommonEventData error(%{public}s)", target.c_str());
117 }
118 }
119 }
120
Action(std::string action,int code)121 void FwkAbilityStateSecond::Action(std::string action, int code)
122 {
123 if (mapAction_.find(action) != mapAction_.end()) {
124 mapAction_[action](code);
125 }
126 }
127
TerminateThis(int code)128 void FwkAbilityStateSecond::TerminateThis(int code)
129 {
130 std::string targetBundle = "com.ohos.amsst.fwkAbilityState";
131 std::string targetAbility = "FwkAbilityStateSecond";
132 Want want;
133 want.SetElementName(targetBundle, targetAbility);
134 StartAbility(want);
135 }
136
137 REGISTER_AA(FwkAbilityStateSecond);
138 } // namespace AppExecFwk
139 } // namespace OHOS