From 442d67bbae2c00e02471bd5332a0603673078f5b Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Fri, 8 Aug 2025 18:08:53 +0800 Subject: [PATCH] add strongbase interface Signed-off-by: zhangzezhong --- frameworks/ets/ani/BUILD.gn | 2 + .../ability_auto_startup_callback/BUILD.gn | 71 +++++ .../ets_ability_auto_startup_callback.h | 57 ++++ .../ets_ability_auto_startup_manager_utils.h | 30 ++ .../src/ets_ability_auto_startup_callback.cpp | 185 ++++++++++++ ...ets_ability_auto_startup_manager_utils.cpp | 174 +++++++++++ .../ani/ability_auto_startup_manager/BUILD.gn | 61 ++++ .../ets_ability_auto_startup_manager.h | 44 +++ .../src/ets_ability_auto_startup_manager.cpp | 271 ++++++++++++++++++ .../@ohos.app.ability.autoStartupManager.ets | 110 +++++++ frameworks/ets/ets/BUILD.gn | 50 ++++ .../ets/application/AutoStartupCallback.ets | 21 ++ .../ets/ets/application/AutoStartupInfo.ets | 36 +++ 13 files changed, 1112 insertions(+) create mode 100644 frameworks/ets/ani/ability_auto_startup_callback/BUILD.gn create mode 100644 frameworks/ets/ani/ability_auto_startup_callback/include/ets_ability_auto_startup_callback.h create mode 100644 frameworks/ets/ani/ability_auto_startup_callback/include/ets_ability_auto_startup_manager_utils.h create mode 100644 frameworks/ets/ani/ability_auto_startup_callback/src/ets_ability_auto_startup_callback.cpp create mode 100644 frameworks/ets/ani/ability_auto_startup_callback/src/ets_ability_auto_startup_manager_utils.cpp create mode 100644 frameworks/ets/ani/ability_auto_startup_manager/BUILD.gn create mode 100644 frameworks/ets/ani/ability_auto_startup_manager/include/ets_ability_auto_startup_manager.h create mode 100644 frameworks/ets/ani/ability_auto_startup_manager/src/ets_ability_auto_startup_manager.cpp create mode 100644 frameworks/ets/ets/@ohos.app.ability.autoStartupManager.ets create mode 100644 frameworks/ets/ets/application/AutoStartupCallback.ets create mode 100644 frameworks/ets/ets/application/AutoStartupInfo.ets diff --git a/frameworks/ets/ani/BUILD.gn b/frameworks/ets/ani/BUILD.gn index 16e11653544..e1570016c3a 100644 --- a/frameworks/ets/ani/BUILD.gn +++ b/frameworks/ets/ani/BUILD.gn @@ -16,6 +16,8 @@ import("//foundation/ability/ability_runtime/ability_runtime.gni") group("ani_packages") { deps = [ + "${ability_runtime_path}/frameworks/ets/ani/ability_auto_startup_callback:autostartupcallback_ani", + "${ability_runtime_path}/frameworks/ets/ani/ability_auto_startup_manager:auto_startup_manager", "${ability_runtime_path}/frameworks/ets/ani/ability_delegator:ability_delegator_registry_ani_kit", "${ability_runtime_path}/frameworks/ets/ani/ability_manager:ability_manager_ani_kit", "${ability_runtime_path}/frameworks/ets/ani/ani_common:ani_common", diff --git a/frameworks/ets/ani/ability_auto_startup_callback/BUILD.gn b/frameworks/ets/ani/ability_auto_startup_callback/BUILD.gn new file mode 100644 index 00000000000..8ba626090de --- /dev/null +++ b/frameworks/ets/ani/ability_auto_startup_callback/BUILD.gn @@ -0,0 +1,71 @@ +# Copyright (c) 2025 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. + +import("//build/ohos.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +ohos_shared_library("autostartupcallback_ani") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + cfi_vcall_icall_only = true + debug = false + } + + include_dirs = [ + "./include", + "${ability_runtime_path}/frameworks/ets/ani/ani_common/include", + "${ability_runtime_path}/frameworks/ets/ani/enum_convert", + "${ability_runtime_path}/interfaces/kits/native/ability/native", + "${ability_runtime_path}/interfaces/inner_api/runtime/include", + "${ability_runtime_utils_path}/global/constant", + ] + + sources = [ + "./src/ets_ability_auto_startup_callback.cpp", + "./src/ets_ability_auto_startup_manager_utils.cpp", + ] + + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_innerkits_path}/ability_manager:ability_start_options", + "${ability_runtime_innerkits_path}/app_manager:app_manager", + "${ability_runtime_innerkits_path}/error_utils:ability_runtime_error_util", + "${ability_runtime_innerkits_path}/runtime:runtime", + "${ability_runtime_native_path}/ability/native:ability_business_error", + "${ability_runtime_native_path}/ability/native:auto_startup_callback", + "${ability_runtime_path}/frameworks/ets/ani/ani_common:ani_common", + ] + + external_deps = [ + "ability_base:session_info", + "c_utils:utils", + "eventhandler:libeventhandler", + "hilog:libhilog", + "ipc:ipc_core", + "napi:ace_napi", + "runtime_core:ani", + ] + + if (ability_runtime_graphics) { + defines = [ + "SUPPORT_GRAPHICS", + "SUPPORT_SCREEN", + ] + } + + innerapi_tags = [ "platformsdk" ] + subsystem_name = "ability" + part_name = "ability_runtime" +} diff --git a/frameworks/ets/ani/ability_auto_startup_callback/include/ets_ability_auto_startup_callback.h b/frameworks/ets/ani/ability_auto_startup_callback/include/ets_ability_auto_startup_callback.h new file mode 100644 index 00000000000..689da273eb5 --- /dev/null +++ b/frameworks/ets/ani/ability_auto_startup_callback/include/ets_ability_auto_startup_callback.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 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_ETS_ABILITY_AUTO_STARTUP_CALLBACK_H +#define OHOS_ABILITY_RUNTIME_ETS_ABILITY_AUTO_STARTUP_CALLBACK_H + +#include +#include +#include + +#include "ani.h" +#include "ani_common_util.h" +#include "auto_startup_callback_stub.h" +#include "auto_startup_info.h" +#include "ets_native_reference.h" +#include "parcel.h" + +namespace OHOS { +namespace AbilityRuntime { +/** + * + * @class EtsAbilityAutoStartupCallBack + */ +class EtsAbilityAutoStartupCallBack : public AutoStartupCallBackStub { +public: + explicit EtsAbilityAutoStartupCallBack(ani_vm *etsVm); + virtual ~EtsAbilityAutoStartupCallBack(); + void Register(ani_object value); + void UnRegister(ani_object value); + void OnAutoStartupOn(const AutoStartupInfo &info) override; + void OnAutoStartupOff(const AutoStartupInfo &info) override; + bool IsCallbacksEmpty(); + +private: + void EtsCallFunction(const AutoStartupInfo &info, const std::string &methodName); + void EtsCallFunctionWorker(const AutoStartupInfo &info, const std::string &methodName); + bool IsEtsCallbackEquals(const AppExecFwk::ETSNativeReference &callback, ani_object value); + + ani_vm *etsVm_; + std::vector callbacks_; + std::mutex mutexlock_; +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_ETS_ABILITY_AUTO_STARTUP_CALLBACK_H diff --git a/frameworks/ets/ani/ability_auto_startup_callback/include/ets_ability_auto_startup_manager_utils.h b/frameworks/ets/ani/ability_auto_startup_callback/include/ets_ability_auto_startup_manager_utils.h new file mode 100644 index 00000000000..e254a124732 --- /dev/null +++ b/frameworks/ets/ani/ability_auto_startup_callback/include/ets_ability_auto_startup_manager_utils.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2025 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_ANI_ABILITY_AUTO_STARTUP_MANAGER_UTILS_H +#define OHOS_ABILITY_RUNTIME_ANI_ABILITY_AUTO_STARTUP_MANAGER_UTILS_H + +#include "ani.h" +#include "auto_startup_info.h" + +namespace OHOS { +namespace AbilityRuntime { +bool UnwrapAutoStartupInfo(ani_env *env, ani_object param, AutoStartupInfo &info); +ani_object ConvertAutoStartupInfos(ani_env *env, const std::vector &infos); +ani_object CreateAniAutoStartupInfo(ani_env *env, const AutoStartupInfo &info); +} // namespace AbilityRuntime +} // namespace OHOS + +#endif // OHOS_ABILITY_RUNTIME_JS_ABILITY_AUTO_STARTUP_MANAGER_UTILS_H \ No newline at end of file diff --git a/frameworks/ets/ani/ability_auto_startup_callback/src/ets_ability_auto_startup_callback.cpp b/frameworks/ets/ani/ability_auto_startup_callback/src/ets_ability_auto_startup_callback.cpp new file mode 100644 index 00000000000..097de723cdf --- /dev/null +++ b/frameworks/ets/ani/ability_auto_startup_callback/src/ets_ability_auto_startup_callback.cpp @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2025 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. + */ + +#include "ets_ability_auto_startup_callback.h" + +#include "ets_ability_auto_startup_manager_utils.h" +#include "ets_error_utils.h" +#include "ets_runtime.h" +#include "hilog_tag_wrapper.h" + +namespace OHOS { +namespace AbilityRuntime { +namespace { +constexpr const char *METHOD_ON = "onAutoStartupOn"; +constexpr const char *METHOD_OFF = "onAutoStartupOff"; +constexpr const char *SIGNATURE_AUTO_STARTUP_INFO = "Lapplication/AutoStartupInfo/AutoStartupInfo;:V"; +} // namespace +EtsAbilityAutoStartupCallBack::EtsAbilityAutoStartupCallBack(ani_vm *etsVm) : etsVm_(etsVm) {} + +EtsAbilityAutoStartupCallBack::~EtsAbilityAutoStartupCallBack() {} + +void EtsAbilityAutoStartupCallBack::OnAutoStartupOn(const AutoStartupInfo &info) +{ + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called OnAutoStartupOn"); + EtsCallFunction(info, METHOD_ON); +} + +void EtsAbilityAutoStartupCallBack::OnAutoStartupOff(const AutoStartupInfo &info) +{ + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called OnAutoStartupOff"); + EtsCallFunction(info, METHOD_OFF); +} + +void EtsAbilityAutoStartupCallBack::Register(ani_object value) +{ + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called Register"); + std::lock_guard lock(mutexlock_); + for (const auto &callback : callbacks_) { + if (IsEtsCallbackEquals(callback, value)) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "callback exist"); + return; + } + } + + ani_ref ref = nullptr; + ani_status status = ANI_ERROR; + ani_env *env = nullptr; + status = etsVm_->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK || env == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "GetEnv failed"); + return; + } + if ((status = env->GlobalReference_Create(value, &ref)) != ANI_OK || ref == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "status : %{public}d or null ref", status); + return; + } + + AppExecFwk::ETSNativeReference stRef; + stRef.aniObj = value; + stRef.aniRef = ref; + callbacks_.emplace_back(stRef); + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "Register end"); +} + +void EtsAbilityAutoStartupCallBack::UnRegister(ani_object value) +{ + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called UnRegister"); + std::lock_guard lock(mutexlock_); + ani_boolean isUndefined = false; + ani_status status = ANI_ERROR; + ani_env *env = nullptr; + status = etsVm_->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK || env == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "GetEnv failed"); + return; + } + if (env->Reference_IsUndefined(value, &isUndefined) != ANI_OK || isUndefined == true) { + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "invalid callback, clear all callback"); + for (auto &callback : callbacks_) { + env->GlobalReference_Delete(callback.aniRef); + } + callbacks_.clear(); + return; + } + + auto it = std::find_if(callbacks_.begin(), callbacks_.end(), + [&value](const AppExecFwk::ETSNativeReference &item) { + return item.aniObj == value; + }); + if (it == callbacks_.end()) { + env->GlobalReference_Delete(it->aniRef); + callbacks_.erase(it); + } + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "UnRegister end"); +} + +bool EtsAbilityAutoStartupCallBack::IsCallbacksEmpty() +{ + return callbacks_.empty(); +} + +void EtsAbilityAutoStartupCallBack::EtsCallFunction(const AutoStartupInfo &info, const std::string &methodName) +{ + wptr stub = iface_cast(AsObject()); + sptr obj = stub.promote(); + if (obj == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null obj"); + return; + } + + obj->EtsCallFunctionWorker(info, methodName); + + ani_status status = ANI_ERROR; + ani_env *env = nullptr; + status = etsVm_->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK || env == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "GetEnv failed"); + return; + } + AppExecFwk::AsyncCallback(env, nullptr, EtsErrorUtil::CreateErrorByNativeErr( + env, static_cast(AbilityErrorCode::ERROR_CODE_INNER)), nullptr); +} + +void EtsAbilityAutoStartupCallBack::EtsCallFunctionWorker(const AutoStartupInfo &info, const std::string &methodName) +{ + std::lock_guard lock(mutexlock_); + for (auto callback : callbacks_) { + if (callback.aniRef == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null obj"); + continue; + } + + ani_status status = ANI_ERROR; + ani_env *env = nullptr; + status = etsVm_->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK || env == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "GetEnv failed"); + return; + } + auto autoStartupInfoObj = CreateAniAutoStartupInfo(env, info); + if ((status = env->Object_CallMethodByName_Void(reinterpret_cast(callback.aniRef), + methodName.c_str(), SIGNATURE_AUTO_STARTUP_INFO, autoStartupInfoObj)) != ANI_OK) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "status: %{public}d", status); + return; + } + } +} + +bool EtsAbilityAutoStartupCallBack::IsEtsCallbackEquals( + const AppExecFwk::ETSNativeReference &callback, ani_object value) +{ + if (callback.aniObj == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null object"); + return false; + } + + ani_boolean result = false; + ani_status status = ANI_ERROR; + ani_env *env = nullptr; + status = etsVm_->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK || env == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "GetEnv failed"); + return false; + } + if (env->Reference_StrictEquals(callback.aniObj, value, &result) != ANI_OK) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "objects not match"); + return false; + } + + return static_cast(result); +} +} // namespace AbilityRuntime +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ets/ani/ability_auto_startup_callback/src/ets_ability_auto_startup_manager_utils.cpp b/frameworks/ets/ani/ability_auto_startup_callback/src/ets_ability_auto_startup_manager_utils.cpp new file mode 100644 index 00000000000..e96c0728987 --- /dev/null +++ b/frameworks/ets/ani/ability_auto_startup_callback/src/ets_ability_auto_startup_manager_utils.cpp @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2025 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. + */ + +#include "ets_ability_auto_startup_manager_utils.h" + +#include "ani_common_util.h" +#include "global_constant.h" +#include "hilog_tag_wrapper.h" + +namespace OHOS { +namespace AbilityRuntime { +namespace { +constexpr const char *CLASSNAME_ARRAY = "Lescompat/Array;"; +constexpr const char *SIGNATURE_AUTO_STARTUP_INFO = "Lapplication/AutoStartupInfo/AutoStartupInfoInner;"; +} +bool UnwrapAutoStartupInfo(ani_env *env, ani_object param, AutoStartupInfo &info) +{ + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called UnwrapAutoStartupInfo"); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null env"); + return false; + } + if (!AppExecFwk::GetStringProperty(env, param, "bundleName", info.bundleName)) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Failed to get bundleName"); + return false; + } + if (!AppExecFwk::GetStringProperty(env, param, "abilityName", info.abilityName)) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Failed to get abilityName"); + return false; + } + if (AppExecFwk::IsExistsProperty(env, param, "appCloneIndex")) { + ani_int appCloneIndex = 0; + if (!AppExecFwk::GetIntPropertyObject(env, param, "appCloneIndex", appCloneIndex)) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Failed to get appCloneIndex"); + return false; + } + info.appCloneIndex = appCloneIndex; + } + if (!AppExecFwk::GetStringProperty(env, param, "moduleName", info.moduleName)) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Failed to get moduleName"); + return false; + } + return true; +} + +static bool SetAutoStartupInfo(ani_env *env, ani_object object, const AutoStartupInfo &info) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null env"); + return false; + } + if (!AppExecFwk::SetRefProperty(env, object, "bundleName", AppExecFwk::GetAniString(env, info.bundleName))) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Failed to set bundleName"); + return false; + } + if (!AppExecFwk::SetRefProperty(env, object, "moduleName", AppExecFwk::GetAniString(env, info.moduleName))) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Failed to set moduleName"); + return false; + } + if (!AppExecFwk::SetRefProperty(env, object, "abilityName", AppExecFwk::GetAniString(env, info.abilityName))) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Failed to set abilityName"); + return false; + } + if (!AppExecFwk::SetRefProperty( + env, object, "abilityTypeName", AppExecFwk::GetAniString(env, info.abilityTypeName))) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Failed to set abilityTypeName"); + return false; + } + if (info.appCloneIndex >= 0 && info.appCloneIndex < GlobalConstant::MAX_APP_CLONE_INDEX) { + if (!AppExecFwk::SetIntPropertyObject(env, object, "appCloneIndex", info.appCloneIndex)) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Failed to set appCloneIndex"); + return false; + } + } + if (!AppExecFwk::SetIntPropertyObject(env, object, "userId", info.userId)) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Failed to set userId"); + return false; + } + if (!AppExecFwk::SetIntPropertyObject(env, object, "setterUserId", info.setterUserId)) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Failed to set setterUserId"); + return false; + } + if (!AppExecFwk::SetRefProperty( + env, object, "canUserModify", AppExecFwk::CreateBoolean(env, info.canUserModify))) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Failed to set canUserModify"); + return false; + } + return true; +} + +ani_object ConvertAutoStartupInfos(ani_env *env, const std::vector &infos) +{ + ani_class arrayCls = nullptr; + ani_status status = ANI_OK; + if (env == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null env"); + return nullptr; + } + status = env->FindClass(CLASSNAME_ARRAY, &arrayCls); + if (status != ANI_OK || arrayCls == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "FindClass failed status : %{public}d or null arrayCls", status); + return nullptr; + } + ani_method arrayCtor = nullptr; + status = env->Class_FindMethod(arrayCls, "", "I:V", &arrayCtor); + if (status != ANI_OK || arrayCtor == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "find ctor failed status : %{public}d or null arrayCtor", status); + return nullptr; + } + ani_object arrayObj = nullptr; + status = env->Object_New(arrayCls, arrayCtor, &arrayObj, infos.size()); + if (status != ANI_OK || arrayObj == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Object_New array status : %{public}d or null arrayObj", status); + return arrayObj; + } + ani_size index = 0; + for (auto &info : infos) { + ani_object ani_info = CreateAniAutoStartupInfo(env, info); + if (ani_info == nullptr) { + TAG_LOGW(AAFwkTag::AUTO_STARTUP, "null ani_info"); + break; + } + status = env->Object_CallMethodByName_Void(arrayObj, "$_set", "ILstd/core/Object;:V", index, ani_info); + if (status != ANI_OK) { + TAG_LOGW(AAFwkTag::AUTO_STARTUP, "Object_CallMethodByName_Void failed status : %{public}d", status); + break; + } + index++; + } + return arrayObj; +} + +ani_object CreateAniAutoStartupInfo(ani_env *env, const AutoStartupInfo &info) +{ + ani_class cls = nullptr; + ani_status status = ANI_ERROR; + ani_method method = nullptr; + ani_object object = nullptr; + if (env == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null env"); + return nullptr; + } + if ((status = env->FindClass(SIGNATURE_AUTO_STARTUP_INFO, &cls)) != ANI_OK || cls == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "find class failed status : %{public}d or null cls", status); + return nullptr; + } + if ((status = env->Class_FindMethod(cls, "", ":V", &method)) != ANI_OK || method == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "find ctor failed status : %{public}d or null method", status); + return nullptr; + } + if ((status = env->Object_New(cls, method, &object)) != ANI_OK || object == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Object_New failed status : %{public}d or null object", status); + return nullptr; + } + if (!SetAutoStartupInfo(env, object, info)) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "SetBusinessAbilityInfo failed"); + return nullptr; + } + return object; +} +} // namespace AbilityRuntime +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ets/ani/ability_auto_startup_manager/BUILD.gn b/frameworks/ets/ani/ability_auto_startup_manager/BUILD.gn new file mode 100644 index 00000000000..9083a0109a8 --- /dev/null +++ b/frameworks/ets/ani/ability_auto_startup_manager/BUILD.gn @@ -0,0 +1,61 @@ +# Copyright (c) 2025 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. + +import("//build/ohos.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +ohos_shared_library("auto_startup_manager") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + cfi_vcall_icall_only = true + debug = false + } + + include_dirs = [ + "./include", + "${ability_runtime_path}/frameworks/ets/ani/ability_auto_startup_callback/include", + ] + + sources = [ "./src/ets_ability_auto_startup_manager.cpp" ] + + configs = [] + + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_innerkits_path}/ability_manager:ability_start_options", + "${ability_runtime_innerkits_path}/app_manager:app_manager", + "${ability_runtime_innerkits_path}/error_utils:ability_runtime_error_util", + "${ability_runtime_innerkits_path}/runtime:runtime", + "${ability_runtime_native_path}/ability/native:ability_business_error", + "${ability_runtime_path}/frameworks/ets/ani/ability_auto_startup_callback:autostartupcallback_ani", + "${ability_runtime_path}/frameworks/ets/ani/ani_common:ani_common", + ] + + external_deps = [ + "access_token:libtokenid_sdk", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "eventhandler:libeventhandler", + "hilog:libhilog", + "ipc:ipc_core", + "napi:ace_napi", + "runtime_core:ani", + "samgr:samgr_proxy", + ] + + innerapi_tags = [ "platformsdk" ] + subsystem_name = "ability" + part_name = "ability_runtime" +} diff --git a/frameworks/ets/ani/ability_auto_startup_manager/include/ets_ability_auto_startup_manager.h b/frameworks/ets/ani/ability_auto_startup_manager/include/ets_ability_auto_startup_manager.h new file mode 100644 index 00000000000..0ff2777298d --- /dev/null +++ b/frameworks/ets/ani/ability_auto_startup_manager/include/ets_ability_auto_startup_manager.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025 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_ETS_ABILITY_AUTO_STARTUP_MANAGER_H +#define OHOS_ABILITY_RUNTIME_ETS_ABILITY_AUTO_STARTUP_MANAGER_H + +#include "ani.h" +#include "ets_ability_auto_startup_callback.h" +#include "ets_runtime.h" +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" + +namespace OHOS { +namespace AbilityRuntime { +class EtsAbilityAutoStartupManager { +public: + EtsAbilityAutoStartupManager() = default; + ~EtsAbilityAutoStartupManager() = default; + static void RegisterAutoStartupCallback(ani_env *env, ani_string aniType, ani_object callback); + static void UnregisterAutoStartupCallback(ani_env *env, ani_string aniType, ani_object callback); + static void SetApplicationAutoStartup(ani_env *env, ani_object info, ani_object callback); + static void CancelApplicationAutoStartup(ani_env *env, ani_object info, ani_object callback); + static void QueryAllAutoStartupApplications(ani_env *env, ani_object callback); + static bool CheckCallerIsSystemApp(); + + static sptr etsAutoStartupCallback_; +}; +void EtsAbilityAutoStartupManagerInit(ani_env *env); +} // namespace AbilityRuntime +} // namespace OHOS + +#endif // OHOS_ABILITY_RUNTIME_ETS_ABILITY_AUTO_STARTUP_MANAGER_H \ No newline at end of file diff --git a/frameworks/ets/ani/ability_auto_startup_manager/src/ets_ability_auto_startup_manager.cpp b/frameworks/ets/ani/ability_auto_startup_manager/src/ets_ability_auto_startup_manager.cpp new file mode 100644 index 00000000000..843d9862ad5 --- /dev/null +++ b/frameworks/ets/ani/ability_auto_startup_manager/src/ets_ability_auto_startup_manager.cpp @@ -0,0 +1,271 @@ +/* + * Copyright (c) 2025 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. + */ +#include "ets_ability_auto_startup_manager.h" + +#include "ability_business_error.h" +#include "ability_manager_client.h" +#include "ability_manager_interface.h" +#include "auto_startup_info.h" +#include "hilog_tag_wrapper.h" +#include "ipc_skeleton.h" +#include "ets_ability_auto_startup_manager_utils.h" +#include "ets_error_utils.h" +#include "permission_constants.h" +#include "tokenid_kit.h" + +namespace OHOS { +namespace AbilityRuntime { +using namespace OHOS::AAFwk; +namespace { +constexpr int32_t INVALID_PARAM = static_cast(AbilityErrorCode::ERROR_CODE_INVALID_PARAM); +constexpr const char *ETS_AUTO_STARTUP_MANAGER_NAMESPACE = + "L@ohos/app/ability/autoStartupManager/autoStartupManager;"; +constexpr const char *ON_OFF_TYPE_SYSTEM = "systemAutoStartup"; +} // namespace + +sptr EtsAbilityAutoStartupManager::etsAutoStartupCallback_ = nullptr; + +bool EtsAbilityAutoStartupManager::CheckCallerIsSystemApp() +{ + auto selfToken = IPCSkeleton::GetSelfTokenID(); + if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken)) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "not system app"); + return false; + } + return true; +} + +void EtsAbilityAutoStartupManager::RegisterAutoStartupCallback( + ani_env *env, ani_string aniType, ani_object callback) +{ + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called RegisterAutoStartupCallback"); + + std::string type; + if (!AppExecFwk::GetStdString(env, aniType, type) || type != ON_OFF_TYPE_SYSTEM) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "invalid param"); + EtsErrorUtil::ThrowError(env, INVALID_PARAM, "Parameter error. Convert type fail."); + return; + } + if (!CheckCallerIsSystemApp()) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "not system app"); + EtsErrorUtil::ThrowError(env, AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP); + return; + } + ani_vm *aniVM = nullptr; + if (env->GetVM(&aniVM) != ANI_OK) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "get aniVM failed"); + EtsErrorUtil::ThrowInvalidParamError(env, "Get aniVm failed."); + return; + } + + if (etsAutoStartupCallback_ == nullptr) { + etsAutoStartupCallback_ = new (std::nothrow) EtsAbilityAutoStartupCallBack(aniVM); + if (etsAutoStartupCallback_ == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null etsAutoStartupCallback_"); + EtsErrorUtil::ThrowError(env, AbilityErrorCode::ERROR_CODE_INNER); + return; + } + + auto ret = AbilityManagerClient::GetInstance()->RegisterAutoStartupSystemCallback( + etsAutoStartupCallback_->AsObject()); + if (ret != ERR_OK) { + etsAutoStartupCallback_ = nullptr; + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "reg callback failed[%{public}d]", ret); + if (ret == CHECK_PERMISSION_FAILED) { + EtsErrorUtil::ThrowNoPermissionError(env, PermissionConstants::PERMISSION_MANAGE_APP_BOOT); + } else { + EtsErrorUtil::ThrowError(env, EtsErrorUtil::CreateErrorByNativeErr(env, ret)); + } + return; + } + } + + etsAutoStartupCallback_->Register(callback); +} + +void EtsAbilityAutoStartupManager::UnregisterAutoStartupCallback( + ani_env *env, ani_string aniType, ani_object callback) +{ + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called UnregisterAutoStartupCallback"); + + std::string type; + if (!AppExecFwk::GetStdString(env, aniType, type) || type != ON_OFF_TYPE_SYSTEM) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "parse type failed"); + EtsErrorUtil::ThrowError(env, INVALID_PARAM, "Parameter error. Convert type fail."); + return; + } + + if (!CheckCallerIsSystemApp()) { + EtsErrorUtil::ThrowError(env, AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP); + return; + } + + if (etsAutoStartupCallback_ == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null etsAutoStartupCallback_"); + EtsErrorUtil::ThrowError(env, AbilityErrorCode::ERROR_CODE_INNER); + return; + } + + etsAutoStartupCallback_->UnRegister(callback); + if (etsAutoStartupCallback_->IsCallbacksEmpty()) { + auto ret = AbilityManagerClient::GetInstance()->UnregisterAutoStartupSystemCallback( + etsAutoStartupCallback_->AsObject()); + if (ret != ERR_OK) { + if (ret == CHECK_PERMISSION_FAILED) { + EtsErrorUtil::ThrowNoPermissionError(env, PermissionConstants::PERMISSION_MANAGE_APP_BOOT); + } else { + EtsErrorUtil::ThrowError(env, EtsErrorUtil::CreateErrorByNativeErr(env, ret)); + } + } + etsAutoStartupCallback_ = nullptr; + } +} + +void EtsAbilityAutoStartupManager::SetApplicationAutoStartup(ani_env *env, ani_object info, ani_object callback) +{ + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called SetApplicationAutoStartup"); + + if (!CheckCallerIsSystemApp()) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "not system app"); + AppExecFwk::AsyncCallback(env, callback, + EtsErrorUtil::CreateError(env, AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP), nullptr); + return; + } + + AutoStartupInfo autoStartupInfo; + if (!UnwrapAutoStartupInfo(env, info, autoStartupInfo)) { + AppExecFwk::AsyncCallback(env, callback, + EtsErrorUtil::CreateError(env, INVALID_PARAM, "unwrap AutoStartupInfo failed"), nullptr); + return; + } + + auto ret = AbilityManagerClient::GetInstance()->SetApplicationAutoStartup(autoStartupInfo); + + AppExecFwk::AsyncCallback(env, callback, + AbilityRuntime::EtsErrorUtil::CreateErrorByNativeErr(env, static_cast(ret)), nullptr); +} + +void EtsAbilityAutoStartupManager::CancelApplicationAutoStartup(ani_env *env, ani_object info, ani_object callback) +{ + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called CancelApplicationAutoStartup"); + + if (!CheckCallerIsSystemApp()) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "not system app"); + AppExecFwk::AsyncCallback(env, callback, + EtsErrorUtil::CreateError(env, AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP), nullptr); + return; + } + + AutoStartupInfo autoStartupInfo; + if (!UnwrapAutoStartupInfo(env, info, autoStartupInfo)) { + AppExecFwk::AsyncCallback(env, callback, + EtsErrorUtil::CreateError(env, INVALID_PARAM, "Parameter error. Convert autoStartupInfo fail."), nullptr); + return; + } + + auto ret = AbilityManagerClient::GetInstance()->CancelApplicationAutoStartup(autoStartupInfo); + + AppExecFwk::AsyncCallback(env, callback, + AbilityRuntime::EtsErrorUtil::CreateErrorByNativeErr(env, static_cast(ret)), nullptr); +} + +void EtsAbilityAutoStartupManager::QueryAllAutoStartupApplications(ani_env *env, ani_object callback) +{ + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called QueryAllAutoStartupApplications"); + if (!CheckCallerIsSystemApp()) { + AppExecFwk::AsyncCallback(env, callback, + EtsErrorUtil::CreateError(env, AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP), nullptr); + return; + } + std::vector infos; + auto ret = AbilityManagerClient::GetInstance()->QueryAllAutoStartupApplications(infos); + if (ret != ERR_OK) { + AppExecFwk::AsyncCallback(env, callback, + AbilityRuntime::EtsErrorUtil::CreateErrorByNativeErr(env, static_cast(ret)), nullptr); + return; + } + ani_object result = ConvertAutoStartupInfos(env, infos); + AppExecFwk::AsyncCallback(env, callback, + AbilityRuntime::EtsErrorUtil::CreateErrorByNativeErr(env, static_cast(ERR_OK)), result); +} + +void EtsAbilityAutoStartupManagerInit(ani_env *env) +{ + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "call EtsAbilityAutoStartupManagerInit"); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null env"); + return; + } + ani_status status = ANI_ERROR; + if (env->ResetError() != ANI_OK) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "ResetError failed"); + } + ani_namespace ns = nullptr; + status = env->FindNamespace(ETS_AUTO_STARTUP_MANAGER_NAMESPACE, &ns); + if (status != ANI_OK || ns == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, + "FindNamespace abilityManager failed status : %{public}d or ns null", status); + return; + } + std::array methods = { + ani_native_function {"nativeOnApplicationAutoStartupStateChangeSync", + "Lstd/core/String;Lapplication/AutoStartupCallback/AutoStartupCallback;:V", + reinterpret_cast(EtsAbilityAutoStartupManager::RegisterAutoStartupCallback)}, + ani_native_function {"nativeOffApplicationAutoStartupStateChangeSync", + "Lstd/core/String;Lapplication/AutoStartupCallback/AutoStartupCallback;:V", + reinterpret_cast(EtsAbilityAutoStartupManager::UnregisterAutoStartupCallback)}, + ani_native_function {"nativeSetApplicationAutoStartup", + "Lapplication/AutoStartupInfo/AutoStartupInfo;Lutils/AbilityUtils/AsyncCallbackWrapper;:V", + reinterpret_cast(EtsAbilityAutoStartupManager::SetApplicationAutoStartup)}, + ani_native_function {"nativeCancelApplicationAutoStartup", + "Lapplication/AutoStartupInfo/AutoStartupInfo;Lutils/AbilityUtils/AsyncCallbackWrapper;:V", + reinterpret_cast(EtsAbilityAutoStartupManager::CancelApplicationAutoStartup)}, + ani_native_function {"nativeQueryAllAutoStartupApplications", + "Lutils/AbilityUtils/AsyncCallbackWrapper;:V", + reinterpret_cast(EtsAbilityAutoStartupManager::QueryAllAutoStartupApplications)}, + }; + status = env->Namespace_BindNativeFunctions(ns, methods.data(), methods.size()); + if (status != ANI_OK) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Namespace_BindNativeFunctions failed status : %{public}d", status); + } + if (env->ResetError() != ANI_OK) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "ResetError failed"); + } +} + +extern "C" { +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "in AbilityAutoStartupManagerEts.ANI_Constructor"); + if (vm == nullptr || result == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null vm or result"); + return ANI_INVALID_ARGS; + } + + ani_env *env = nullptr; + ani_status status = ANI_ERROR; + status = vm->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK || env == nullptr) { + TAG_LOGE(AAFwkTag::AUTO_STARTUP, "GetEnv failed, status=%{public}d or null env", status); + return ANI_NOT_FOUND; + } + EtsAbilityAutoStartupManagerInit(env); + *result = ANI_VERSION_1; + TAG_LOGD(AAFwkTag::AUTO_STARTUP, "AbilityAutoStartupManagerEts.ANI_Constructor finished"); + return ANI_OK; +} +} +} // namespace AbilityRuntime +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ets/ets/@ohos.app.ability.autoStartupManager.ets b/frameworks/ets/ets/@ohos.app.ability.autoStartupManager.ets new file mode 100644 index 00000000000..40e7ec55260 --- /dev/null +++ b/frameworks/ets/ets/@ohos.app.ability.autoStartupManager.ets @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2025 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. + */ + +import AutoStartupCallback from 'application.AutoStartupCallback'; +import AutoStartupInfo from 'application.AutoStartupInfo'; +import { AsyncCallback, BusinessError } from '@ohos.base'; +import AsyncCallbackWrapper from './utils/AbilityUtils'; + +export default namespace autoStartupManager { + loadLibrary("auto_startup_manager.z") + + export native function nativeOnApplicationAutoStartupStateChangeSync(type: string, callback: AutoStartupCallback): void; + export native function nativeOffApplicationAutoStartupStateChangeSync(type: string, callback?: AutoStartupCallback): void; + export native function nativeSetApplicationAutoStartup(info: AutoStartupInfo, callback: AsyncCallbackWrapper): void; + export native function nativeCancelApplicationAutoStartup(info: AutoStartupInfo, callback: AsyncCallbackWrapper): void; + export native function nativeQueryAllAutoStartupApplications(callback: AsyncCallbackWrapper>): void; + + export function on(type: 'systemAutoStartup', callback: AutoStartupCallback): void { + autoStartupManager.nativeOnApplicationAutoStartupStateChangeSync('systemAutoStartup', callback); + } + + export function off(type: 'systemAutoStartup', callback?: AutoStartupCallback): void { + autoStartupManager.nativeOffApplicationAutoStartupStateChangeSync('systemAutoStartup', callback); + } + + export function setApplicationAutoStartup(info: AutoStartupInfo, callback: AsyncCallback): void { + let myCall = new AsyncCallbackWrapper(callback); + taskpool.execute((): void => { + autoStartupManager.nativeSetApplicationAutoStartup(info, myCall); + }); + } + + export function setApplicationAutoStartup(info: AutoStartupInfo): Promise { + let p = new Promise((resolve: (data: undefined)=>void, reject:(err: BusinessError) => void): void => { + let myCall = new AsyncCallbackWrapper((err: BusinessError | null) => { + if (err == null || err.code == 0) { + resolve(undefined); + } else { + reject(err); + } + }); + taskpool.execute((): void => { + autoStartupManager.nativeSetApplicationAutoStartup(info, myCall); + }); + }); + return p; + } + + export function cancelApplicationAutoStartup(info: AutoStartupInfo, callback: AsyncCallback): void { + let myCall = new AsyncCallbackWrapper(callback); + taskpool.execute((): void => { + autoStartupManager.nativeCancelApplicationAutoStartup(info, myCall); + }); + } + + export function cancelApplicationAutoStartup(info: AutoStartupInfo): Promise { + let p = new Promise((resolve: (data: undefined)=>void, reject:(err: BusinessError) => void): void => { + let myCall = new AsyncCallbackWrapper((err: BusinessError | null) => { + if (err == null || err.code == 0) { + resolve(undefined); + } else { + reject(err); + } + }); + taskpool.execute((): void => { + autoStartupManager.nativeCancelApplicationAutoStartup(info, myCall); + }); + }); + return p; + } + + export function queryAllAutoStartupApplications(callback: AsyncCallback>): void { + let myCall = new AsyncCallbackWrapper>(callback); + taskpool.execute((): void => { + autoStartupManager.nativeQueryAllAutoStartupApplications(myCall); + }); + } + + export function queryAllAutoStartupApplications(): Promise> { + let p = new Promise>((resolve: (data: Array) => void, + reject: (err: BusinessError) => void): void => { + let myCall = new AsyncCallbackWrapper>((err: BusinessError | null, + data: Array | undefined) => { + if (err == null || err.code == 0) { + resolve(data as Array); + } else { + reject(err); + } + }); + taskpool.execute((): void => { + autoStartupManager.nativeQueryAllAutoStartupApplications(myCall); + }).catch((err: Error): void => { + reject(err as BusinessError); + }); + }); + return p; + } +} \ No newline at end of file diff --git a/frameworks/ets/ets/BUILD.gn b/frameworks/ets/ets/BUILD.gn index 07d5b3ee261..a7e7203e088 100644 --- a/frameworks/ets/ets/BUILD.gn +++ b/frameworks/ets/ets/BUILD.gn @@ -937,6 +937,53 @@ ohos_prebuilt_etc("ability_runtime_auto_fill_type_abc_etc") { deps = [ ":ability_runtime_auto_fill_type_abc" ] } +generate_static_abc("ability_runtime_auto_startup_callback_abc") { + base_url = "./" + files = [ "./application/AutoStartupCallback.ets" ] + + is_boot_abc = "True" + device_dst_file = "/system/framework/ability_runtime_auto_startup_callback_abc.abc" +} + +ohos_prebuilt_etc("ability_runtime_auto_startup_callback_abc_etc") { + source = "$target_out_dir/ability_runtime_auto_startup_callback_abc.abc" + module_install_dir = "framework" + subsystem_name = "ability" + part_name = "ability_runtime" + deps = [ ":ability_runtime_auto_startup_callback_abc" ] +} + +generate_static_abc("ability_runtime_auto_startup_info_abc") { + base_url = "./" + files = [ "./application/AutoStartupInfo.ets" ] + + is_boot_abc = "True" + device_dst_file = "/system/framework/ability_runtime_auto_startup_info_abc.abc" +} + +ohos_prebuilt_etc("ability_runtime_auto_startup_info_abc_etc") { + source = "$target_out_dir/ability_runtime_auto_startup_info_abc.abc" + module_install_dir = "framework" + subsystem_name = "ability" + part_name = "ability_runtime" + deps = [ ":ability_runtime_auto_startup_info_abc" ] +} + +generate_static_abc("ability_runtime_auto_startup_manager_abc") { + base_url = "./" + files = [ "./@ohos.app.ability.autoStartupManager.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/ability_runtime_auto_startup_manager_abc.abc" +} + +ohos_prebuilt_etc("ability_runtime_auto_startup_manager_abc_etc") { + source = "$target_out_dir/ability_runtime_auto_startup_manager_abc.abc" + module_install_dir = "framework" + subsystem_name = "ability" + part_name = "ability_runtime" + deps = [ ":ability_runtime_auto_startup_manager_abc" ] +} + generate_static_abc("ability_runtime_error_code_abc") { base_url = "./" files = [ "./@ohos.ability.errorCode.ets" ] @@ -1255,6 +1302,9 @@ group("ets_packages") { ":ability_runtime_application_context_abc_etc", ":ability_runtime_auto_fill_manager_abc_etc", ":ability_runtime_auto_fill_type_abc_etc", + ":ability_runtime_auto_startup_callback_abc_etc", + ":ability_runtime_auto_startup_info_abc_etc", + ":ability_runtime_auto_startup_manager_abc_etc", ":ability_runtime_application_state_change_callback_abc_etc", ":ability_runtime_base_context_abc_etc", ":ability_runtime_caller_callee_abc_etc", diff --git a/frameworks/ets/ets/application/AutoStartupCallback.ets b/frameworks/ets/ets/application/AutoStartupCallback.ets new file mode 100644 index 00000000000..2290cfd6499 --- /dev/null +++ b/frameworks/ets/ets/application/AutoStartupCallback.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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. + */ + +import AutoStartupInfo from 'application.AutoStartupInfo'; + +export interface AutoStartupCallback { + onAutoStartupOn(info: AutoStartupInfo): void; + onAutoStartupOff(info: AutoStartupInfo): void; +} \ No newline at end of file diff --git a/frameworks/ets/ets/application/AutoStartupInfo.ets b/frameworks/ets/ets/application/AutoStartupInfo.ets new file mode 100644 index 00000000000..7ebb371c7bb --- /dev/null +++ b/frameworks/ets/ets/application/AutoStartupInfo.ets @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 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. + */ + +export interface AutoStartupInfo { + bundleName: string; + moduleName?: string; + abilityName: string; + abilityTypeName?: string; + appCloneIndex?: int; + readonly userId?: int; + readonly setterUserId?: int; + readonly canUserModify?: boolean; +} + +class AutoStartupInfoInner implements AutoStartupInfo { + bundleName: string = ''; + moduleName?: string = ''; + abilityName: string = ''; + abilityTypeName?: string = ''; + appCloneIndex?: int = 0; + readonly userId?: int = 0; + readonly setterUserId?: int = 0; + readonly canUserModify?: boolean = false; +} -- Gitee