/* * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_ABILITY_RUNTIME_OHOS_APPLICATION_H #define OHOS_ABILITY_RUNTIME_OHOS_APPLICATION_H #include #include #include #include #include "ability_lifecycle_callbacks.h" #include "ability_stage.h" #include "app_context.h" #include "context.h" #include "element_callback.h" #include "ability_stage_context.h" #include "application_configuration_manager.h" namespace OHOS { namespace AbilityRuntime { class Runtime; } // namespace AbilityRuntime namespace AppExecFwk { class ElementsCallback; class ApplicationImpl; class Configuration; class AbilityRecordMgr; class OHOSApplication : public AppContext, public AbilityLifecycleCallbacks { public: OHOSApplication(); virtual ~OHOSApplication(); /** * @brief dump OHOSApplication info * * @param extra dump OHOSApplication info */ void DumpApplication(); /** * @brief Set Runtime * * @param runtime Runtime instance. */ void SetRuntime(std::unique_ptr&& runtime); /** * @brief Set ApplicationContext * * @param context ApplicationContext instance. */ void SetApplicationContext(const std::shared_ptr &abilityRuntimeContext); /** * * @brief Set the abilityRecordMgr to the OHOSApplication. * * @param abilityRecordMgr */ void SetAbilityRecordMgr(const std::shared_ptr &abilityRecordMgr); /** * * @brief Register AbilityLifecycleCallbacks with OHOSApplication * * @param callBack callBack When the life cycle of the ability in the application changes, */ void RegisterAbilityLifecycleCallbacks(const std::shared_ptr &callBack); /** * * @brief Unregister AbilityLifecycleCallbacks with OHOSApplication * * @param callBack RegisterAbilityLifecycleCallbacks`s callBack */ void UnregisterAbilityLifecycleCallbacks(const std::shared_ptr &callBack); /** * * @brief Will be called when the given ability calls Ability->onStart * * @param Ability Indicates the ability object that calls the onStart() method. */ void OnAbilityStart(const std::shared_ptr &ability); /** * * @brief Will be called when the given ability calls Ability->onInactive * * @param Ability Indicates the Ability object that calls the onInactive() method. */ void OnAbilityInactive(const std::shared_ptr &ability); /** * * @brief Will be called when the given ability calls Ability->onBackground * * @param Ability Indicates the Ability object that calls the onBackground() method. */ void OnAbilityBackground(const std::shared_ptr &ability); /** * * @brief Will be called when the given ability calls Ability->onForeground * * @param Ability Indicates the Ability object that calls the onForeground() method. */ void OnAbilityForeground(const std::shared_ptr &ability); /** * * @brief Will be called when the given ability calls Ability->onActive * * @param Ability Indicates the Ability object that calls the onActive() method. */ void OnAbilityActive(const std::shared_ptr &ability); /** * * @brief Will be called when the given ability calls Ability->onStop * * @param Ability Indicates the Ability object that calls the onStop() method. */ void OnAbilityStop(const std::shared_ptr &ability); /** * * Called when Ability#onSaveAbilityState(PacMap) was called on an ability. * * @param outState Indicates the PacMap object passed to Ability#onSaveAbilityState(PacMap) * for storing user data and states. This parameter cannot be null. */ void DispatchAbilitySavedState(const PacMap &outState); /** * * @brief Called when an ability calls Ability#onSaveAbilityState(PacMap). * You can implement your own logic in this method. * @param outState IIndicates the {@link PacMap} object passed to the onSaveAbilityState() callback. * */ void OnAbilitySaveState(const PacMap &outState); /** * * @brief Register ElementsCallback with OHOSApplication * * @param callBack callBack when the system configuration of the device changes. */ void RegisterElementsCallbacks(const std::shared_ptr &callback); /** * * @brief Unregister ElementsCallback with OHOSApplication * * @param callback RegisterElementsCallbacks`s callback */ void UnregisterElementsCallbacks(const std::shared_ptr &callback); /** * * @brief Will be Called when the system configuration of the device changes. * * @param config Indicates the new Configuration object. */ virtual void OnConfigurationUpdated(Configuration config, AbilityRuntime::SetLevel level = AbilityRuntime::SetLevel::System); /** * * @brief Will be Called when the application font of the device changes. * * @param config Indicates the new Configuration object. */ virtual void OnFontUpdated(Configuration config); /** * * @brief Called when the system has determined to trim the memory, for example, * when the ability is running in the background and there is no enough memory for * running as many background processes as possible. * * @param level Indicates the memory trim level, which shows the current memory usage status. */ virtual void OnMemoryLevel(int level); /** * * @brief Will be called the application foregrounds * */ virtual void OnForeground(); /** * * @brief Will be called the application backgrounds * */ virtual void OnBackground(); /** * * @brief Will be called the application starts * */ virtual void OnStart(); /** * * @brief Will be called the application ends */ virtual void OnTerminate(); /** * @brief add the ability stage when a hap first load * * @param abilityRecord * @return abilityStage context */ std::shared_ptr AddAbilityStage( const std::shared_ptr &abilityRecord, const std::function &)> &callback, bool &isAsyncCallback); /** * * @brief update the application info after new module installed. * * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext. * */ void UpdateApplicationInfoInstalled(const AppExecFwk::ApplicationInfo &appInfo); /** * @brief add the ability stage when a hap first load * * @param hapModuleInfo * @return Returns true on success, false on failure */ bool AddAbilityStage( const AppExecFwk::HapModuleInfo &hapModuleInfo, const std::function &callback, bool &isAsyncCallback); /** * @brief remove the ability stage when all of the abilities in the hap have been removed * * @param abilityInfo */ void CleanAbilityStage(const sptr &token, const std::shared_ptr &abilityInfo, bool isCacheProcess); /** * @brief return the application context * * @param context */ std::shared_ptr GetAppContext() const; /** * @brief return the application runtime * * @param runtime */ const std::unique_ptr& GetRuntime(); /* * * @brief Will be called the application ends * */ virtual void SetConfiguration(const Configuration &config); void ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName, std::string &flag); void ScheduleNewProcessRequest(const AAFwk::Want &want, const std::string &moduleName, std::string &flag); virtual std::shared_ptr GetConfiguration(); void GetExtensionNameByType(int32_t type, std::string &name) { std::map::iterator it = extensionTypeMap_.find(type); if (it == extensionTypeMap_.end()) { return; } name = it->second; } /** * @brief Set extension types. * * @param map The extension types. */ void SetExtensionTypeMap(std::map map); bool NotifyLoadRepairPatch(const std::string &hqfFile, const std::string &hapPath); bool NotifyHotReloadPage(); bool NotifyUnLoadRepairPatch(const std::string &hqfFile); void CleanAppTempData(bool isLastProcess = false); void CleanUselessTempData(); void SetAppEnv(const std::vector& appEnvironments); void AutoStartupDone(const std::shared_ptr &abilityRecord, const std::shared_ptr &abilityStage, const std::string &moduleName); void AutoStartupDone(const std::shared_ptr &abilityStage, const AppExecFwk::HapModuleInfo &hapModuleInfo); void CleanEmptyAbilityStage(); private: void UpdateAppContextResMgr(const Configuration &config); bool IsUpdateColorNeeded(Configuration &config, AbilityRuntime::SetLevel level); bool isUpdateFontSize(Configuration &config, AbilityRuntime::SetLevel level); bool IsUpdateLanguageNeeded(Configuration &config, AbilityRuntime::SetLevel level); const std::function CreateAutoStartupCallback( const std::shared_ptr abilityStage, const std::shared_ptr abilityRecord, const std::function&)>& callback); const std::function CreateAutoStartupCallback( const std::shared_ptr &abilityStage, const AppExecFwk::HapModuleInfo &hapModuleInfo, const std::function& callback); bool IsMainProcess(const std::string &bundleName, const std::string &process); private: std::list> abilityLifecycleCallbacks_; std::list> elementsCallbacks_; std::shared_ptr abilityRecordMgr_ = nullptr; std::shared_ptr abilityRuntimeContext_ = nullptr; std::unordered_map> abilityStages_; std::unique_ptr runtime_; std::shared_ptr configuration_ = nullptr; std::map extensionTypeMap_; }; } // namespace AppExecFwk } // namespace OHOS #endif // OHOS_ABILITY_RUNTIME_OHOS_APPLICATION_H