1 /* 2 * Copyright (c) 2021-2022 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_ABILITY_RUNTIME_OHOS_APPLICATION_H 17 #define OHOS_ABILITY_RUNTIME_OHOS_APPLICATION_H 18 19 #include <string> 20 #include <list> 21 #include <memory> 22 23 #include "ability_lifecycle_callbacks.h" 24 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h" 25 #include "ability_stage.h" 26 #include "app_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 AppContext, 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::ApplicationContext> &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 * 229 * @brief update the application info after new module installed. 230 * 231 * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext. 232 * 233 */ 234 void UpdateApplicationInfoInstalled(const AppExecFwk::ApplicationInfo &appInfo); 235 236 /** 237 * @brief add the ability stage when a hap first load 238 * 239 * @param hapModuleInfo 240 * @return Returns true on success, false on failure 241 */ 242 bool AddAbilityStage(const AppExecFwk::HapModuleInfo &hapModuleInfo); 243 244 /** 245 * @brief remove the ability stage when all of the abilities in the hap have been removed 246 * 247 * @param abilityInfo 248 */ 249 void CleanAbilityStage(const sptr<IRemoteObject> &token, const std::shared_ptr<AbilityInfo> &abilityInfo); 250 251 /** 252 * @brief return the application context 253 * 254 * @param context 255 */ 256 std::shared_ptr<AbilityRuntime::Context> GetAppContext() const; 257 258 /** 259 * @brief return the application runtime 260 * 261 * @param runtime 262 */ 263 const std::unique_ptr<AbilityRuntime::Runtime>& GetRuntime(); 264 265 /* 266 * 267 * @brief Will be called the application ends 268 * 269 */ 270 virtual void SetConfiguration(const Configuration &config); 271 272 void ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName, std::string &flag); 273 274 virtual std::shared_ptr<Configuration> GetConfiguration(); 275 GetExtensionNameByType(int32_t type,std::string & name)276 void GetExtensionNameByType(int32_t type, std::string &name) 277 { 278 std::map<int32_t, std::string>::iterator it = extensionTypeMap_.find(type); 279 if (it == extensionTypeMap_.end()) { 280 return; 281 } 282 name = it->second; 283 } 284 285 /** 286 * @brief Set extension types. 287 * 288 * @param map The extension types. 289 */ 290 void SetExtensionTypeMap(std::map<int32_t, std::string> map); 291 292 bool NotifyLoadRepairPatch(const std::string &hqfFile, const std::string &hapPath); 293 294 bool NotifyHotReloadPage(); 295 296 bool NotifyUnLoadRepairPatch(const std::string &hqfFile); 297 298 private: 299 std::list<std::shared_ptr<AbilityLifecycleCallbacks>> abilityLifecycleCallbacks_; 300 std::list<std::shared_ptr<ElementsCallback>> elementsCallbacks_; 301 std::shared_ptr<AbilityRecordMgr> abilityRecordMgr_ = nullptr; 302 std::shared_ptr<AbilityRuntime::ApplicationContext> abilityRuntimeContext_ = nullptr; 303 std::unordered_map<std::string, std::shared_ptr<AbilityRuntime::AbilityStage>> abilityStages_; 304 std::unique_ptr<AbilityRuntime::Runtime> runtime_; 305 std::shared_ptr<Configuration> configuration_ = nullptr; 306 std::map<int32_t, std::string> extensionTypeMap_; 307 }; 308 } // namespace AppExecFwk 309 } // namespace OHOS 310 #endif // OHOS_ABILITY_RUNTIME_OHOS_APPLICATION_H 311