From c9690491687879aee98803af16d9ead1ac7b78e3 Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Thu, 29 May 2025 15:10:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E5=90=88app=5Fmanager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangzezhong --- .../ani/app_manager/include/ets_app_manager.h | 26 +++ .../ani/app_manager/src/ets_app_manager.cpp | 163 ++++++++++++++++++ .../ets/ets/@ohos.app.ability.appManager.ets | 41 +++++ 3 files changed, 230 insertions(+) create mode 100644 frameworks/ets/ani/app_manager/include/ets_app_manager.h create mode 100644 frameworks/ets/ani/app_manager/src/ets_app_manager.cpp create mode 100644 frameworks/ets/ets/@ohos.app.ability.appManager.ets diff --git a/frameworks/ets/ani/app_manager/include/ets_app_manager.h b/frameworks/ets/ani/app_manager/include/ets_app_manager.h new file mode 100644 index 00000000000..a6cfa96e156 --- /dev/null +++ b/frameworks/ets/ani/app_manager/include/ets_app_manager.h @@ -0,0 +1,26 @@ +/* + * 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_APP_MANAGER_H +#define OHOS_ABILITY_RUNTIME_ETS_APP_MANAGER_H + +#include "ets_runtime.h" + +namespace OHOS { +namespace AppManagerEts { +void EtsAppManagerRegistryInit(ani_env *env); +} // namespace AppManagerEts +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_ETS_APP_MANAGER_H diff --git a/frameworks/ets/ani/app_manager/src/ets_app_manager.cpp b/frameworks/ets/ani/app_manager/src/ets_app_manager.cpp new file mode 100644 index 00000000000..b89266768c6 --- /dev/null +++ b/frameworks/ets/ani/app_manager/src/ets_app_manager.cpp @@ -0,0 +1,163 @@ +/* + * 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_app_manager.h" + +#include "hilog_tag_wrapper.h" +#include "ability_manager_client.h" +#include "ability_manager_interface.h" +#include "app_mgr_constants.h" +#include "app_mgr_interface.h" +#include "ani_common_util.h" +#include "ani_enum_convert.h" +#include "app_mgr_constants.h" +#include "app_mgr_interface.h" +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "ets_error_utils.h" + +namespace OHOS { +namespace AppManagerEts { + +const char* APP_MANAGER_CLASS_NAME = "L@ohos/app/ability/appManager/appManager;"; +class EtsAppManager final { +public: + static OHOS::sptr GetAppManagerInstance() + { + OHOS::sptr systemAbilityManager = + OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + OHOS::sptr appObject = systemAbilityManager->GetSystemAbility(OHOS::APP_MGR_SERVICE_ID); + return OHOS::iface_cast(appObject); + } + + static void PreloadApplication(ani_env *env, ani_object callback, ani_string etsBundleName, + ani_double etsUserId, ani_enum_item etsMode, ani_object etsAppIndex) + { + TAG_LOGD(AAFwkTag::APPMGR, "PreloadApplication"); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "env is null"); + return; + } + std::string bundleName; + if (!OHOS::AppExecFwk::GetStdString(env, etsBundleName, bundleName)) { + TAG_LOGE(AAFwkTag::APPMGR, "param bundlename err"); + AppExecFwk::AsyncCallback(env, callback, + OHOS::AbilityRuntime::CreateEtsErrorByNativeErr(env, + static_cast(AbilityRuntime::AbilityErrorCode::ERROR_CODE_INVALID_PARAM)), nullptr); + return; + } + TAG_LOGD(AAFwkTag::APPMGR, "PreloadApplication userId:%{public}f, bundleName %{public}s", + etsUserId, bundleName.c_str()); + int32_t userId = static_cast(etsUserId); + + ani_int mode; + if (!AAFwk::AniEnumConvertUtil::EnumConvertEtsToNative(env, etsMode, mode)) { + TAG_LOGE(AAFwkTag::APPMGR, "param mode err"); + AppExecFwk::AsyncCallback(env, callback, + OHOS::AbilityRuntime::CreateEtsErrorByNativeErr(env, + static_cast(AbilityRuntime::AbilityErrorCode::ERROR_CODE_INVALID_PARAM)), nullptr); + return; + } + ani_status status = ANI_OK; + int32_t appIndex = 0; + ani_boolean isUndefined = false; + if ((status = env->Reference_IsUndefined(etsAppIndex, &isUndefined)) != ANI_OK) { + TAG_LOGE(AAFwkTag::APPMGR, "Failed to check undefined status : %{public}d", status); + return; + } + ani_double dval = 0.0; + if (!isUndefined) { + if ((status = env->Object_CallMethodByName_Double(etsAppIndex, + "doubleValue", nullptr, &dval)) != ANI_OK) { + TAG_LOGE(AAFwkTag::APPMGR, "Object_CallMethodByName_Double status : %{public}d", status); + return; + } + TAG_LOGD(AAFwkTag::APPMGR, "etsAppIndex: %{public}f", dval); + appIndex = static_cast(dval); + } + TAG_LOGD(AAFwkTag::APPMGR, "PreloadApplication userId:%{public}d, mode:%{public}d, appIndex:%{public}d", + userId, mode, appIndex); + + sptr appMgr = GetAppManagerInstance(); + if (appMgr == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "appManager null ptr"); + AppExecFwk::AsyncCallback(env, callback, + OHOS::AbilityRuntime::CreateEtsErrorByNativeErr(env, + static_cast(AbilityRuntime::AbilityErrorCode::ERROR_CODE_INNER)), nullptr); + return; + } + auto ret = appMgr->PreloadApplication(bundleName, userId, static_cast(mode), + appIndex); + TAG_LOGD(AAFwkTag::APPMGR, "PreloadApplication ret %{public}d", ret); + AppExecFwk::AsyncCallback(env, callback, + OHOS::AbilityRuntime::CreateEtsErrorByNativeErr(env, static_cast(ret)), nullptr); + TAG_LOGD(AAFwkTag::APPMGR, "PreloadApplication END"); + } +}; + +void EtsAppManagerRegistryInit(ani_env *env) +{ + ani_status status = ANI_ERROR; + if (env->ResetError() != ANI_OK) { + TAG_LOGE(AAFwkTag::APPKIT, "ResetError failed"); + } + + ani_namespace ns; + status = env->FindNamespace(APP_MANAGER_CLASS_NAME, &ns); + if (status != ANI_OK) { + TAG_LOGE(AAFwkTag::APPKIT, "FindNamespace appManager failed status : %{public}d", status); + return; + } + + std::array kitFunctions = { + ani_native_function { + "nativePreloadApplication", nullptr, reinterpret_cast(EtsAppManager::PreloadApplication)}, + }; + + status = env->Namespace_BindNativeFunctions(ns, kitFunctions.data(), kitFunctions.size()); + if (status != ANI_OK) { + TAG_LOGE(AAFwkTag::APPKIT, "Namespace_BindNativeFunctions failed status : %{public}d", status); + } + + if (env->ResetError() != ANI_OK) { + TAG_LOGE(AAFwkTag::APPKIT, "ResetError failed"); + } +} + +extern "C" { +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + TAG_LOGD(AAFwkTag::APPMGR, "in AppManagerEts.ANI_Constructor"); + if (vm == nullptr || result == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "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) { + TAG_LOGE(AAFwkTag::APPMGR, "GetEnv failed, status=%{public}d", status); + return ANI_NOT_FOUND; + } + EtsAppManagerRegistryInit(env); + *result = ANI_VERSION_1; + TAG_LOGD(AAFwkTag::APPMGR, "AppManagerEts.ANI_Constructor finished"); + return ANI_OK; +} +} // extern "C" +} // namespace AppManagerEts +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ets/ets/@ohos.app.ability.appManager.ets b/frameworks/ets/ets/@ohos.app.ability.appManager.ets new file mode 100644 index 00000000000..f9c36797266 --- /dev/null +++ b/frameworks/ets/ets/@ohos.app.ability.appManager.ets @@ -0,0 +1,41 @@ +/* + * 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 default namespace appManager { +loadLibrary("ability_app_manager_ani_kit.z") + +export enum PreloadMode { + PRESS_DOWN +} + +export native function nativePreloadApplication(callback: AsyncCallbackWrapper, bundleName: string, userId: number, + mode: PreloadMode, appIndex?: number): void; +export function preloadApplication(bundleName: string, userId: number, mode: PreloadMode, appIndex?: number): Promise{ + let p:Promise = new Promise((resolve: (data:undefined)=>void, reject:(err: BusinessError)=>void):void => { + let myCall = new AsyncCallbackWrapper((err: BusinessError)=>{ + if (err.code == 0) { + resolve(undefined); + } else { + reject(err); + } + }); + taskpool.execute((): void => { + appManager.nativePreloadApplication(myCall, bundleName, userId, mode, appIndex); + }); + }); + return p; + } +} // namespace appManager + + -- Gitee