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 OHOS_SLITE_ABILITY_H
17 #define OHOS_SLITE_ABILITY_H
18 
19 #include "ability_inner_message.h"
20 #include "ability_saved_data.h"
21 #include "lite_context.h"
22 #include "want.h"
23 
24 namespace OHOS {
25 namespace AbilitySlite {
26 /**
27  * @brief Declares ability-related functions, including ability lifecycle callbacks and functions for connecting to or
28  *        disconnecting from Particle Abilities.
29  *
30  * As the fundamental unit of OpenHarmony applications, abilities are classified into Feature Abilities and Particle
31  * Abilities. Feature Abilities support the Page template, and Particle Abilities support the Service template.
32  * An ability using the Page template is called Page ability for short and that using the Service template
33  * is called Service ability.
34  *
35  * @since 1.0
36  * @version 1.0
37  */
38 class SliteAbility : public LiteContext {
39 public:
40     SliteAbility(const char *bundleName = nullptr);
41 
42     virtual ~SliteAbility();
43 
44     /**
45      * @brief Called when this ability is created. You must override this function if you want to perform some
46      *        initialization operations during ability startup.
47      *
48      * This function can be called only once in the entire lifecycle of an ability.
49      * @param want Indicates the {@link Want} structure containing startup information about the ability.
50      */
51     virtual void OnCreate(const Want &want);
52 
53     /**
54      * @brief Called when the user data need to be restored.
55      *
56      * You can override this function to implement your own processing logic.
57      */
58     virtual void OnRestoreData(AbilitySavedData *data);
59 
60     /**
61      * @brief Called when this ability enters the <b>SLITE_STATE_FOREGROUND</b> state.
62      *
63      * The ability in the <b>SLITE_STATE_FOREGROUND</b> state is visible and has focus.
64      * You can override this function to implement your own processing logic.
65      *
66      * @param want Indicates the {@link Want} structure containing activation information about the ability.
67      */
68     virtual void OnForeground(const Want &want);
69 
70     /**
71      * @brief Called when this ability enters the <b>SLITE_STATE_BACKGROUND</b> state.
72      *
73      *
74      * The ability in the <b>SLITE_STATE_BACKGROUND</b> state is invisible.
75      * You can override this function to implement your own processing logic.
76      */
77     virtual void OnBackground();
78 
79     /**
80      * @brief Called when the user data need to be saved.
81      *
82      * You can override this function to implement your own processing logic.
83      */
84     virtual void OnSaveData(AbilitySavedData *data);
85 
86     /**
87      * @brief Called when this ability enters the <b>SLITE_STATE_UNINITIALIZED</b> state.
88      *
89      * The ability in the <b>SLITE_STATE_UNINITIALIZED</b> is being destroyed.
90      * You can override this function to implement your own processing logic.
91      */
92     virtual void OnDestroy();
93 
94     virtual void HandleExtraMessage(const SliteAbilityInnerMsg &innerMsg);
95 
96     int32_t GetState() const;
97 
98     char *bundleName_ = nullptr;
99 
100 private:
101     int32_t abilityState_ = 0;
102 };
103 } // namespace AbilitySlite
104 using AbilitySlite::SliteAbility;
105 } // namespace OHOS
106 #endif // OHOS_SLITE_ABILITY_H
107