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 "application_context.h" 22 #include "ability_lifecycle_callbacks.h" 23 #include "element_callback.h" 24 25 namespace OHOS { 26 namespace AppExecFwk { 27 class ElementsCallback; 28 class ApplicationImpl; 29 class Configuration; 30 class AbilityRecordMgr; 31 class OHOSApplication : public ApplicationContext, 32 public AbilityLifecycleCallbacks, 33 public std::enable_shared_from_this<OHOSApplication> { 34 public: 35 OHOSApplication(); 36 virtual ~OHOSApplication() = default; 37 38 /** 39 * @brief dump OHOSApplication info 40 * 41 * @param extra dump OHOSApplication info 42 */ 43 void DumpApplication(); 44 45 /** 46 * 47 * @brief Set the abilityRecordMgr to the OHOSApplication. 48 * 49 * @param abilityRecordMgr 50 */ 51 void SetAbilityRecordMgr(const std::shared_ptr<AbilityRecordMgr> &abilityRecordMgr); 52 53 /** 54 * 55 * @brief Register AbilityLifecycleCallbacks with OHOSApplication 56 * 57 * @param callBack callBack When the life cycle of the ability in the application changes, 58 */ 59 void RegisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> &callBack); 60 61 /** 62 * 63 * @brief Unregister AbilityLifecycleCallbacks with OHOSApplication 64 * 65 * @param callBack RegisterAbilityLifecycleCallbacks`s callBack 66 */ 67 void UnregisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> &callBack); 68 69 /** 70 * 71 * @brief Will be called when the given ability calls Ability->onStart 72 * 73 * @param Ability Indicates the ability object that calls the onStart() method. 74 */ 75 void OnAbilityStart(const std::shared_ptr<Ability> &ability); 76 77 /** 78 * 79 * @brief Will be called when the given ability calls Ability->onInactive 80 * 81 * @param Ability Indicates the Ability object that calls the onInactive() method. 82 */ 83 void OnAbilityInactive(const std::shared_ptr<Ability> &ability); 84 85 /** 86 * 87 * @brief Will be called when the given ability calls Ability->onBackground 88 * 89 * @param Ability Indicates the Ability object that calls the onBackground() method. 90 */ 91 void OnAbilityBackground(const std::shared_ptr<Ability> &ability); 92 93 /** 94 * 95 * @brief Will be called when the given ability calls Ability->onForeground 96 * 97 * @param Ability Indicates the Ability object that calls the onForeground() method. 98 */ 99 void OnAbilityForeground(const std::shared_ptr<Ability> &ability); 100 101 /** 102 * 103 * @brief Will be called when the given ability calls Ability->onActive 104 * 105 * @param Ability Indicates the Ability object that calls the onActive() method. 106 */ 107 void OnAbilityActive(const std::shared_ptr<Ability> &ability); 108 109 /** 110 * 111 * @brief Will be called when the given ability calls Ability->onStop 112 * 113 * @param Ability Indicates the Ability object that calls the onStop() method. 114 */ 115 void OnAbilityStop(const std::shared_ptr<Ability> &ability); 116 117 /** 118 * 119 * Called when Ability#onSaveAbilityState(PacMap) was called on an ability. 120 * 121 * @param outState Indicates the PacMap object passed to Ability#onSaveAbilityState(PacMap) 122 * for storing user data and states. This parameter cannot be null. 123 */ 124 void DispatchAbilitySavedState(const PacMap &outState); 125 126 /** 127 * 128 * @brief Called when an ability calls Ability#onSaveAbilityState(PacMap). 129 * You can implement your own logic in this method. 130 * @param outState IIndicates the {@link PacMap} object passed to the onSaveAbilityState() callback. 131 * 132 */ 133 void OnAbilitySaveState(const PacMap &outState); 134 135 /** 136 * 137 * @brief Register ElementsCallback with OHOSApplication 138 * 139 * @param callBack callBack when the system configuration of the device changes. 140 */ 141 void RegisterElementsCallbacks(const std::shared_ptr<ElementsCallback> &callback); 142 143 /** 144 * 145 * @brief Unregister ElementsCallback with OHOSApplication 146 * 147 * @param callback RegisterElementsCallbacks`s callback 148 */ 149 void UnregisterElementsCallbacks(const std::shared_ptr<ElementsCallback> &callback); 150 151 /** 152 * 153 * @brief Will be Called when the system configuration of the device changes. 154 * 155 * @param config Indicates the new Configuration object. 156 */ 157 virtual void OnConfigurationUpdated(const Configuration &config); 158 159 /** 160 * 161 * @brief Called when the system has determined to trim the memory, for example, 162 * when the ability is running in the background and there is no enough memory for 163 * running as many background processes as possible. 164 * 165 * @param level Indicates the memory trim level, which shows the current memory usage status. 166 */ 167 virtual void OnMemoryLevel(int level); 168 169 /** 170 * 171 * @brief Will be called the application foregrounds 172 * 173 */ 174 virtual void OnForeground(); 175 176 /** 177 * 178 * @brief Will be called the application backgrounds 179 * 180 */ 181 virtual void OnBackground(); 182 183 /** 184 * 185 * @brief Will be called the application starts 186 * 187 */ 188 virtual void OnStart(); 189 190 /** 191 * 192 * @brief Will be called the application ends 193 * 194 */ 195 virtual void OnTerminate(); 196 197 private: 198 std::list<std::shared_ptr<AbilityLifecycleCallbacks>> abilityLifecycleCallbacks_; 199 std::list<std::shared_ptr<ElementsCallback>> elementsCallbacks_; 200 std::shared_ptr<AbilityRecordMgr> abilityRecordMgr_ = nullptr; 201 }; 202 } // namespace AppExecFwk 203 } // namespace OHOS 204 #endif // FOUNDATION_APPEXECFWK_OHOS_APPLICATION_H 205