• 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 #ifndef AMS_ABILITY_APPEND_TEST_A_SECOND_ABILITY_H
17 #define AMS_ABILITY_APPEND_TEST_A_SECOND_ABILITY_H
18 #include "ability.h"
19 #include "ability_loader.h"
20 #include "common_event.h"
21 #include "common_event_manager.h"
22 #include "ability_append_test_info.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 using namespace OHOS::EventFwk;
27 class SecondEventSubscriber;
28 class SecondAbility : public Ability {
29 public:
30     void SubscribeEvent();
31     void TestAbility(int apiIndex, int caseIndex, int code);
32     void TerminateAbilityResultCase1(int code);
33 
SecondAbility()34     SecondAbility()
35     {
36         mapCase_ = {
37             {(int)AppendApi::TerminateAbilityResult,
38                 {
39                     [this](int code) { TerminateAbilityResultCase1(code); },
40                 }},
41         };
42     }
43 
44     std::unordered_map<int, std::vector<std::function<void(int)>>> mapCase_;
45     ~SecondAbility();
46 
47 protected:
48     void Init(const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> &application,
49         std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override;
50     virtual void OnStart(const Want &want) override;
51     virtual void OnStop() override;
52     virtual void OnActive() override;
53     virtual void OnInactive() override;
54     virtual void OnBackground() override;
55     virtual void OnForeground(const Want &want) override;
56     virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId) override;
57     virtual sptr<IRemoteObject> OnConnect(const Want &want) override;
58     virtual void OnDisconnect(const Want &want) override;
59     std::shared_ptr<SecondEventSubscriber> subscriber_;
60 };
61 class SecondEventSubscriber : public EventFwk::CommonEventSubscriber {
62 public:
SecondEventSubscriber(const EventFwk::CommonEventSubscribeInfo & sp)63     explicit SecondEventSubscriber(const EventFwk::CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
64     {
65         mapTestFunc_ = {
66             {"Ability", [this](int apiIndex, int caseIndex, int code) { TestAbility(apiIndex, caseIndex, code); }},
67         };
68         mainAbility = nullptr;
69     }
70 
TestAbility(int apiIndex,int caseIndex,int code)71     void TestAbility(int apiIndex, int caseIndex, int code)
72     {
73         mainAbility->TestAbility(apiIndex, caseIndex, code);
74     }
75 
76     virtual void OnReceiveEvent(const EventFwk::CommonEventData &data);
77 
78     SecondAbility *mainAbility;
79     std::unordered_map<std::string, std::function<void(int, int, int)>> mapTestFunc_;
80     ~SecondEventSubscriber() = default;
81 };
82 }  // namespace AppExecFwk
83 }  // namespace OHOS
84 #endif  // AMS_ABILITY_APPEND_TEST_A_SECOND_ABILITY_H