• 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 FOUNDATION_APPEXECFWK_OHOS_APPLICATION_H
17 #define FOUNDATION_APPEXECFWK_OHOS_APPLICATION_H
18 
19 #include <string>
20 #include <list>
21 #include <memory>
22 
23 #include "ability_lifecycle_callbacks.h"
24 #include "ability_runtime/context/context.h"
25 #include "ability_stage.h"
26 #include "application_context.h"
27 #include "element_callback.h"
28 
29 namespace OHOS {
30 namespace AbilityRuntime {
31 class Runtime;
32 } // namespace AbilityRuntime
33 namespace AppExecFwk {
34 class ElementsCallback;
35 class ApplicationImpl;
36 class Configuration;
37 class AbilityRecordMgr;
38 class OHOSApplication : public ApplicationContext,
39                         public AbilityLifecycleCallbacks,
40                         public std::enable_shared_from_this<OHOSApplication> {
41 public:
42     OHOSApplication();
43     virtual ~OHOSApplication();
44 
45     /**
46      * @brief dump OHOSApplication info
47      *
48      * @param extra dump OHOSApplication info
49      */
50     void DumpApplication();
51 
52     /**
53      * @brief Set Runtime
54      *
55      * @param runtime Runtime instance.
56      */
57     void SetRuntime(std::unique_ptr<AbilityRuntime::Runtime>&& runtime);
58 
59     /**
60      * @brief Set ApplicationContext
61      *
62      * @param context ApplicationContext instance.
63      */
64     void SetApplicationContext(const std::shared_ptr<AbilityRuntime::Context> &abilityRuntimeContext);
65 
66     /**
67      *
68      * @brief Set the abilityRecordMgr to the OHOSApplication.
69      *
70      * @param abilityRecordMgr
71      */
72     void SetAbilityRecordMgr(const std::shared_ptr<AbilityRecordMgr> &abilityRecordMgr);
73 
74     /**
75      *
76      * @brief Register AbilityLifecycleCallbacks with OHOSApplication
77      *
78      * @param callBack callBack When the life cycle of the ability in the application changes,
79      */
80     void RegisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> &callBack);
81 
82     /**
83      *
84      * @brief Unregister AbilityLifecycleCallbacks with OHOSApplication
85      *
86      * @param callBack RegisterAbilityLifecycleCallbacks`s callBack
87      */
88     void UnregisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> &callBack);
89 
90     /**
91      *
92      * @brief Will be called when the given ability calls Ability->onStart
93      *
94      * @param Ability Indicates the ability object that calls the onStart() method.
95      */
96     void OnAbilityStart(const std::shared_ptr<Ability> &ability);
97 
98     /**
99      *
100      * @brief Will be called when the given ability calls Ability->onInactive
101      *
102      * @param Ability Indicates the Ability object that calls the onInactive() method.
103      */
104     void OnAbilityInactive(const std::shared_ptr<Ability> &ability);
105 
106     /**
107      *
108      * @brief Will be called when the given ability calls Ability->onBackground
109      *
110      * @param Ability Indicates the Ability object that calls the onBackground() method.
111      */
112     void OnAbilityBackground(const std::shared_ptr<Ability> &ability);
113 
114     /**
115      *
116      * @brief Will be called when the given ability calls Ability->onForeground
117      *
118      * @param Ability Indicates the Ability object that calls the onForeground() method.
119      */
120     void OnAbilityForeground(const std::shared_ptr<Ability> &ability);
121 
122     /**
123      *
124      * @brief Will be called when the given ability calls Ability->onActive
125      *
126      * @param Ability Indicates the Ability object that calls the onActive() method.
127      */
128     void OnAbilityActive(const std::shared_ptr<Ability> &ability);
129 
130     /**
131      *
132      * @brief Will be called when the given ability calls Ability->onStop
133      *
134      * @param Ability Indicates the Ability object that calls the onStop() method.
135      */
136     void OnAbilityStop(const std::shared_ptr<Ability> &ability);
137 
138     /**
139      *
140      * Called when Ability#onSaveAbilityState(PacMap) was called on an ability.
141      *
142      * @param outState Indicates the PacMap object passed to Ability#onSaveAbilityState(PacMap)
143      * for storing user data and states. This parameter cannot be null.
144      */
145     void DispatchAbilitySavedState(const PacMap &outState);
146 
147     /**
148      *
149      * @brief Called when an ability calls Ability#onSaveAbilityState(PacMap).
150      * You can implement your own logic in this method.
151      * @param outState IIndicates the {@link PacMap} object passed to the onSaveAbilityState() callback.
152      *
153      */
154     void OnAbilitySaveState(const PacMap &outState);
155 
156     /**
157      *
158      * @brief Register ElementsCallback with OHOSApplication
159      *
160      * @param callBack callBack when the system configuration of the device changes.
161      */
162     void RegisterElementsCallbacks(const std::shared_ptr<ElementsCallback> &callback);
163 
164     /**
165      *
166      * @brief Unregister ElementsCallback with OHOSApplication
167      *
168      * @param callback RegisterElementsCallbacks`s callback
169      */
170     void UnregisterElementsCallbacks(const std::shared_ptr<ElementsCallback> &callback);
171 
172     /**
173      *
174      * @brief Will be Called when the system configuration of the device changes.
175      *
176      * @param config Indicates the new Configuration object.
177      */
178     virtual void OnConfigurationUpdated(const Configuration &config);
179 
180     /**
181      *
182      * @brief Called when the system has determined to trim the memory, for example,
183      * when the ability is running in the background and there is no enough memory for
184      * running as many background processes as possible.
185      *
186      * @param level Indicates the memory trim level, which shows the current memory usage status.
187      */
188     virtual void OnMemoryLevel(int level);
189 
190     /**
191      *
192      * @brief Will be called the application foregrounds
193      *
194      */
195     virtual void OnForeground();
196 
197     /**
198      *
199      * @brief Will be called the application backgrounds
200      *
201      */
202     virtual void OnBackground();
203 
204     /**
205      *
206      * @brief Will be called the application starts
207      *
208      */
209     virtual void OnStart();
210 
211     /**
212      *
213      * @brief Will be called the application ends
214      *
215      */
216     virtual void OnTerminate();
217 
218     /**
219      * @brief add the ability stage when a hap first load
220      *
221      * @param abilityRecord
222      * @return abilityStage context
223      */
224     std::shared_ptr<AbilityRuntime::Context> AddAbilityStage(
225         const std::shared_ptr<AbilityLocalRecord> &abilityRecord);
226 
227     /**
228      * @brief add the ability stage when a hap first load
229      *
230      * @param hapModuleInfo
231      * @return Returns true on success, false on failure
232      */
233     bool AddAbilityStage(const AppExecFwk::HapModuleInfo &hapModuleInfo);
234 
235     /**
236      * @brief remove the ability stage when all of the abilities in the hap have been removed
237      *
238      * @param abilityInfo
239      */
240     void CleanAbilityStage(const sptr<IRemoteObject> &token, const std::shared_ptr<AbilityInfo> &abilityInfo);
241 
242     /**
243      * @brief return the application context
244      *
245      * @param context
246      */
247     std::shared_ptr<AbilityRuntime::Context> GetAppContext() const;
248 
249     /**
250      * @brief return the application runtime
251      *
252      * @param runtime
253      */
254     const std::unique_ptr<AbilityRuntime::Runtime>& GetRuntime();
255 
256 	 /*
257      *
258      * @brief Will be called the application ends
259      *
260      */
261     virtual void SetConfiguration(const Configuration &config);
262 
263     void ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName, std::string &flag);
264 
265     virtual std::shared_ptr<Configuration> GetConfiguration();
266 
267 private:
268     std::list<std::shared_ptr<AbilityLifecycleCallbacks>> abilityLifecycleCallbacks_;
269     std::list<std::shared_ptr<ElementsCallback>> elementsCallbacks_;
270     std::shared_ptr<AbilityRecordMgr> abilityRecordMgr_ = nullptr;
271     std::shared_ptr<AbilityRuntime::Context> abilityRuntimeContext_ = nullptr;
272     std::unordered_map<std::string, std::shared_ptr<AbilityRuntime::AbilityStage>> abilityStages_;
273     std::unique_ptr<AbilityRuntime::Runtime> runtime_;
274     std::shared_ptr<Configuration> configuration_ = nullptr;
275 };
276 }  // namespace AppExecFwk
277 }  // namespace OHOS
278 #endif  // FOUNDATION_APPEXECFWK_OHOS_APPLICATION_H
279