• 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 BASE_ABILITY_H_
17 #define BASE_ABILITY_H_
18 #include "ability.h"
19 #include "common_event.h"
20 #include "common_event_manager.h"
21 #include "event_handler.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 constexpr int MAX_LOOP = 10;
26 
27 template<class T, class G>
SubscribeEvent(const Want & want,const std::vector<std::string> & eventList,const G & ability)28 void SubscribeEvent(const Want &want, const std::vector<std::string> &eventList, const G &ability)
29 {
30     EventFwk::MatchingSkills matchingSkills;
31     for (const auto &e : eventList) {
32         matchingSkills.AddEvent(e);
33     }
34     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
35     subscribeInfo.SetPriority(1);
36     auto subscriber = std::make_shared<T>(subscribeInfo);
37     subscriber->want = want;
38     subscriber->mainAbility = ability;
39     EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
40 }
41 
42 template<class T>
DoWhile()43 void DoWhile()
44 {
45     long cnt = 0;
46     while (cnt <= MAX_LOOP) {
47         cnt++;
48     }
49 }
50 
51 template<class T>
PostTask()52 void PostTask()
53 {
54     auto eventHandler = EventHandler::Current();
55     if (!eventHandler) {
56         return;
57     }
58     eventHandler->PostTask([=]() { DoWhile<T>(); });
59 }
60 
61 class BaseAbility : public Ability {
62 public:
63     using EventHandlerPtr = std::shared_ptr<EventHandler>;
64 
65 protected:
66     virtual void Init(const std::shared_ptr<AbilityInfo> &abilityInfo,
67         const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
68         const sptr<IRemoteObject> &token) override;
69     virtual void OnStart(const Want &want) override;
70     virtual void OnStop() override;
71     virtual void OnActive() override;
72     virtual void OnInactive() override;
73     virtual void OnBackground() override;
74     virtual void OnForeground(const Want &want) override;
75     virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId) override;
76     virtual sptr<IRemoteObject> OnConnect(const Want &want) override;
77     virtual void OnDisconnect(const Want &want) override;
78     virtual void OnNewWant(const Want &want) override;
79     virtual void OnAbilityResult(int requestCode, int resultCode, const Want &resultData) override;
80     virtual void OnBackPressed() override;
81 
82     std::string GetNoFromWantInfo(const Want &want);
83 
84 private:
85     EventHandlerPtr envenHandler_ = {};
86 };
87 }  // namespace AppExecFwk
88 }  // namespace OHOS
89 #endif  // BASE_ABILITY_H_
90