diff --git a/frameworks/ets/ets/@ohos.app.ability.AbilityStage.ets b/frameworks/ets/ets/@ohos.app.ability.AbilityStage.ets index 27df91c758a16f67c19e42313692874cd50a4e6d..ceed6d34cfc2c99a6e9fd65e52825cbc2efd6c33 100644 --- a/frameworks/ets/ets/@ohos.app.ability.AbilityStage.ets +++ b/frameworks/ets/ets/@ohos.app.ability.AbilityStage.ets @@ -15,6 +15,8 @@ import { Configuration } from '@ohos.app.ability.Configuration' import AbilityStageContext from 'application.AbilityStageContext' +import Want from '@ohos.app.ability.Want'; +import AbilityConstant from '@ohos.app.ability.AbilityConstant'; export default class AbilityStage { context: AbilityStageContext = new AbilityStageContext(); @@ -27,4 +29,15 @@ export default class AbilityStage { onDestroy(): void { } + + onAcceptWant(want: Want): string { + return ''; + } + + onMemoryLevel(level: AbilityConstant.MemoryLevel): void { + } + + onNewProcessRequest(want: Want): string { + return ''; + } } \ No newline at end of file diff --git a/frameworks/native/ability/native/ability_runtime/sts_ui_ability.cpp b/frameworks/native/ability/native/ability_runtime/sts_ui_ability.cpp index 353237d9fe327ac7cd1d6da7844881c03466ea63..7c028130760ee6613cb5b21ccf62193fede53b51 100644 --- a/frameworks/native/ability/native/ability_runtime/sts_ui_ability.cpp +++ b/frameworks/native/ability/native/ability_runtime/sts_ui_ability.cpp @@ -1347,7 +1347,47 @@ std::shared_ptr StsUIAbility::CreateADe void StsUIAbility::Dump(const std::vector ¶ms, std::vector &info) { UIAbility::Dump(params, info); - TAG_LOGD(AAFwkTag::UIABILITY, "called"); + auto env = stsRuntime_.GetAniEnv(); + if (env == nullptr || stsAbilityObj_ == nullptr) { + TAG_LOGE(AAFwkTag::SERVICE_EXT, "null env or stsAbilityObj"); + return; + } + ani_object arrayObj = nullptr; + if (!AppExecFwk::WrapArrayString(env, arrayObj, params)) { + TAG_LOGE(AAFwkTag::SERVICE_EXT, "WrapArrayString failed"); + return; + } + if (!stsAbilityObj_->aniObj || !stsAbilityObj_->aniCls) { + TAG_LOGE(AAFwkTag::SERVICE_EXT, "null aniObj or aniCls"); + return; + } + ani_status status = ANI_ERROR; + ani_method method = nullptr; + if ((status = env->Class_FindMethod(stsAbilityObj_->aniCls, "onDump", nullptr, &method)) != ANI_OK) { + TAG_LOGE(AAFwkTag::SERVICE_EXT, "Class_FindMethod FAILED: %{public}d", status); + return; + } + if (!method) { + TAG_LOGE(AAFwkTag::SERVICE_EXT, "find method onDump failed"); + return; + } + ani_ref strArrayRef; + if ((status = env->Object_CallMethod_Ref(stsAbilityObj_->aniObj, method, &strArrayRef, arrayObj)) != ANI_OK) { + TAG_LOGE(AAFwkTag::SERVICE_EXT, "Object_CallMethod_Ref FAILED: %{public}d", status); + return; + } + if (!strArrayRef) { + TAG_LOGE(AAFwkTag::SERVICE_EXT, "null strArrayRef"); + return; + } + std::vector dumpInfoStrArray; + if (!AppExecFwk::UnwrapArrayString(env, reinterpret_cast(strArrayRef), dumpInfoStrArray)) { + TAG_LOGE(AAFwkTag::SERVICE_EXT, "UnwrapArrayString failed"); + return; + } + for (auto dumpInfoStr:dumpInfoStrArray) { + info.push_back(dumpInfoStr); + } TAG_LOGD(AAFwkTag::UIABILITY, "dump info size: %{public}zu", info.size()); } diff --git a/frameworks/native/appkit/ability_runtime/app/sts_ability_stage.cpp b/frameworks/native/appkit/ability_runtime/app/sts_ability_stage.cpp index f7d1553fe21073f55c63fb29144ae67c6384dfa6..fa5f3097b5741dd3646f78597f27400aaed5165a 100644 --- a/frameworks/native/appkit/ability_runtime/app/sts_ability_stage.cpp +++ b/frameworks/native/appkit/ability_runtime/app/sts_ability_stage.cpp @@ -20,6 +20,8 @@ #include "configuration_convertor.h" #include "sts_ability_stage_context.h" #include "ani_common_configuration.h" +#include "ani_common_want.h" +#include "ani_enum_convert.h" #include "ohos_application.h" #include "startup_manager.h" #include "hitrace_meter.h" @@ -44,6 +46,8 @@ constexpr const char* WAIT_ON_MAIN_THREAD = "waitOnMainThread"; constexpr const char* CONFIG_ENTRY = "configEntry"; constexpr const char *TASKPOOL = "taskPool"; constexpr const char *TASKPOOL_LOWER = "taskpool"; +constexpr const char* MEMORY_LEVEL_ENUM_NAME = + "L@ohos/app/ability/AbilityConstant/AbilityConstant/MemoryLevel;"; namespace { void RegisterStopPreloadSoCallback(STSRuntime& stsRuntime) @@ -136,10 +140,10 @@ void STSAbilityStage::Init(const std::shared_ptr &context, void STSAbilityStage::OnCreate(const AAFwk::Want &want) const { + TAG_LOGD(AAFwkTag::APPKIT, "OnCreate called"); AbilityStage::OnCreate(want); - + FreezeUtil::GetInstance().AddAppLifecycleEvent(0, "ETSAbilityStage::OnCreate begin"); CallObjectMethod(false, "onCreate", ":V"); - FreezeUtil::GetInstance().AddAppLifecycleEvent(0, "STSAbilityStage::OnCreate end"); auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(AbilityRuntime::Runtime::Language::STS); if (delegator) { @@ -149,18 +153,53 @@ void STSAbilityStage::OnCreate(const AAFwk::Want &want) const void STSAbilityStage::OnDestroy() const { + TAG_LOGD(AAFwkTag::APPKIT, "OnDestroy called"); AbilityStage::OnDestroy(); CallObjectMethod(false, "onDestroy", ":V"); } std::string STSAbilityStage::OnAcceptWant(const AAFwk::Want &want) { - return std::string(); + TAG_LOGD(AAFwkTag::APPKIT, "OnAcceptWant called"); + AbilityStage::OnAcceptWant(want); + + auto env = stsRuntime_.GetAniEnv(); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "env nullptr"); + return ""; + } + ani_ref wantRef = OHOS::AppExecFwk::WrapWant(env, want); + std::string strResult; + ani_object resObj = CallObjectMethod("onAcceptWant", "L@ohos/app/ability/Want/Want;:Lstd/core/String;", wantRef); + ani_string ani_result = reinterpret_cast(resObj); + if (!OHOS::AppExecFwk::GetStdString(env, ani_result, strResult)) { + TAG_LOGW(AAFwkTag::APPKIT, "ani_result GetStdString failed"); + } + return strResult; } std::string STSAbilityStage::OnNewProcessRequest(const AAFwk::Want &want) { - return std::string(); + TAG_LOGD(AAFwkTag::APPKIT, "OnNewProcessRequest called"); + AbilityStage::OnNewProcessRequest(want); + + auto env = stsRuntime_.GetAniEnv(); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "env nullptr"); + return ""; + } + ani_ref wantRef = OHOS::AppExecFwk::WrapWant(env, want); + + std::string strResult; + ani_object resObj = CallObjectMethod("onNewProcessRequest", + "L@ohos/app/ability/Want/Want;:Lstd/core/String;", wantRef); + + ani_string ani_result = reinterpret_cast(resObj); + + if (!OHOS::AppExecFwk::GetStdString(env, ani_result, strResult)) { + TAG_LOGW(AAFwkTag::APPKIT, "ani_result GetStdString failed"); + } + return strResult; } void STSAbilityStage::OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) @@ -180,6 +219,21 @@ void STSAbilityStage::OnConfigurationUpdated(const AppExecFwk::Configuration& co void STSAbilityStage::OnMemoryLevel(int32_t level) { + TAG_LOGD(AAFwkTag::APPKIT, "OnMemoryLevel called"); + AbilityStage::OnMemoryLevel(level); + + auto env = stsRuntime_.GetAniEnv(); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ABILITY, "env nullptr"); + return; + } + ani_enum_item memoryLevelItem {}; + OHOS::AAFwk::AniEnumConvertUtil::EnumConvertNativeToSts(env, + MEMORY_LEVEL_ENUM_NAME, level, memoryLevelItem); + + CallObjectMethod(false, "onMemoryLevel", "L@ohos/app/ability/AbilityConstant/AbilityConstant/MemoryLevel;:V", + memoryLevelItem); + TAG_LOGD(AAFwkTag::APPKIT, "end"); } int32_t STSAbilityStage::RunAutoStartupTask(const std::function &callback, bool &isAsyncCallback, @@ -222,7 +276,6 @@ bool STSAbilityStage::CallObjectMethod(bool withResult, const char *name, const TAG_LOGE(AAFwkTag::ABILITY, "stsAbilityStageObj_ nullptr"); return false; } - auto env = stsRuntime_.GetAniEnv(); STSAbilityStageContext::ResetEnv(env); ani_status status = ANI_OK; @@ -254,6 +307,41 @@ bool STSAbilityStage::CallObjectMethod(bool withResult, const char *name, const return false; } +ani_object STSAbilityStage::CallObjectMethod(const char *name, const char *signature, ...) +{ + HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, std::string("CallObjectMethod:") + name); + TAG_LOGD(AAFwkTag::UIABILITY, "StsUIAbility call sts, name: %{public}s", name); + if (stsAbilityStageObj_ == nullptr) { + TAG_LOGE(AAFwkTag::UIABILITY, "null stsAbilityObj"); + return nullptr; + } + auto env = stsRuntime_.GetAniEnv(); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::UIABILITY, "null env"); + return nullptr; + } + auto obj = stsAbilityStageObj_->aniObj; + auto cls = stsAbilityStageObj_->aniCls; + ani_status status = ANI_ERROR; + + ani_method method {}; + if ((status = env->Class_FindMethod(cls, name, signature, &method)) != ANI_OK) { + TAG_LOGE(AAFwkTag::UIABILITY, "status : %{public}d", status); + env->ResetError(); + return nullptr; + } + ani_ref res {}; + va_list args; + va_start(args, signature); + if ((status = env->Object_CallMethod_Ref(obj, method, &res, args)) != ANI_OK) { + TAG_LOGE(AAFwkTag::UIABILITY, "status : %{public}d", status); + stsRuntime_.HandleUncaughtError(); + return nullptr; + } + va_end(args); + return reinterpret_cast(res); +} + std::shared_ptr STSAbilityStage::CreateStageProperty() const { auto property = std::make_shared(); diff --git a/interfaces/kits/native/appkit/ability_runtime/app/sts_ability_stage.h b/interfaces/kits/native/appkit/ability_runtime/app/sts_ability_stage.h index d1a7769ceaab30492b00092a232ccfca652146f9..bfccbb3967ce8032d641ea00e2203f725d8539a2 100644 --- a/interfaces/kits/native/appkit/ability_runtime/app/sts_ability_stage.h +++ b/interfaces/kits/native/appkit/ability_runtime/app/sts_ability_stage.h @@ -61,6 +61,8 @@ private: bool CallObjectMethod(bool withResult, const char* name, const char* signature, ...) const; + ani_object CallObjectMethod(const char *name, const char *signature, ...); + std::shared_ptr CreateStageProperty() const; std::string GetHapModuleProp(const std::string &propName) const;