• 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 FWK_ABILITY_STATE_ABILITY_A_H
17 #define FWK_ABILITY_STATE_ABILITY_A_H
18 #include <string>
19 #include "ability.h"
20 #include "ability_loader.h"
21 #include "hilog_wrapper.h"
22 #include "common_event.h"
23 #include "common_event_manager.h"
24 #include "pac_map.h"
25 #include "stpageabilityevent.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 class FwkAbilityStateMainSubscriber;
30 class FwkAbilityStateMain : public Ability {
31 public:
FwkAbilityStateMain()32     FwkAbilityStateMain()
33     {
34         mapAction_["StartNextAbility"] = [this](int code) { StartNextAbility(code); };
35     }
36     ~FwkAbilityStateMain();
37     virtual void OnStart(const Want &want) override;
38     virtual void OnStop() override;
39     virtual void OnActive() override;
40     virtual void OnInactive() override;
41     virtual void OnBackground() override;
42     virtual void OnForeground(const Want &want) override;
43     virtual void OnNewWant(const Want &want) override;
44     virtual void OnSaveAbilityState(PacMap &outState) override;
45     virtual void OnRestoreAbilityState(const PacMap &inState) override;
46 
47     void Action(std::string action, int code);
48     void StartNextAbility(int code);
49     void DoCrash(std::string action, int code);
50     void BlockAndStart(std::string action, int code);
51     void OnBlockProcess(bool &bIsBlockFlag);
52 
53 private:
54     std::shared_ptr<FwkAbilityStateMainSubscriber> subscriber_;
55     std::string callback_seq;
56     std::string action;
57     std::unordered_map<std::string, std::function<void(int)>> mapAction_;
58     bool bIsBlockSave;
59     bool bIsBlockRestore;
60     void SubscribeEvent();
61 };
62 
63 class FwkAbilityStateMainSubscriber : public EventFwk::CommonEventSubscriber {
64 public:
FwkAbilityStateMainSubscriber(const EventFwk::CommonEventSubscribeInfo & sp)65     explicit FwkAbilityStateMainSubscriber(const EventFwk::CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp)
66     {
67         mapAction_ = {
68             {"StartNextAbility", [this](std::string action, int code) { Action(action, code); }},
69             {"DoCrash", [this](std::string action, int code) { DoCrash(action, code); }},
70             {"BlockAndStart", [this](std::string action, int code) { BlockAndStart(action, code); }},
71         };
72         mainAbility = nullptr;
73     }
74 
Action(std::string action,int code)75     void Action(std::string action, int code)
76     {
77         mainAbility->Action(action, code);
78     }
79 
DoCrash(std::string action,int code)80     void DoCrash(std::string action, int code)
81     {
82         mainAbility->DoCrash(action, code);
83     }
84 
BlockAndStart(std::string action,int code)85     void BlockAndStart(std::string action, int code)
86     {
87         mainAbility->BlockAndStart(action, code);
88     }
89 
90     virtual void OnReceiveEvent(const EventFwk::CommonEventData &data) override;
91 
92     FwkAbilityStateMain *mainAbility;
93     std::unordered_map<std::string, std::function<void(std::string, int)>> mapAction_;
94     ~FwkAbilityStateMainSubscriber() = default;
95 };
96 class CrashMaker {
97 public:
CrashTry()98     int CrashTry()
99     {
100         return this->crashData;
101     };
102 
103 private:
104     int crashData = 0;
105 };
106 }  // namespace AppExecFwk
107 }  // namespace OHOS
108 #endif  // _FWK_ABILITY_STATE_ABILITY_A_H