From 83c715d617af82422d3153f4dd45074b543ad72a Mon Sep 17 00:00:00 2001 From: lanhaoyu Date: Wed, 2 Apr 2025 15:35:35 +0800 Subject: [PATCH] add bms arkts1.2 function Signed-off-by: lanhaoyu --- interfaces/kits/ani/bundle_manager/BUILD.gn | 2 + .../ani/bundle_manager/ani_bundle_manager.cpp | 293 +++++++++++++++++- .../ets/@ohos.bundle.bundleManager.ets | 235 +++++++++++++- interfaces/kits/ani/common/common_fun_ani.cpp | 22 ++ interfaces/kits/ani/common/common_fun_ani.h | 2 + 5 files changed, 531 insertions(+), 23 deletions(-) diff --git a/interfaces/kits/ani/bundle_manager/BUILD.gn b/interfaces/kits/ani/bundle_manager/BUILD.gn index 508474dd28..af66f62582 100755 --- a/interfaces/kits/ani/bundle_manager/BUILD.gn +++ b/interfaces/kits/ani/bundle_manager/BUILD.gn @@ -44,6 +44,8 @@ ohos_shared_library("anibundlemanager") { external_deps = [ "ability_base:want", + "ability_runtime:ani_common", + "ability_runtime:sts_environment", "c_utils:utils", "common_event_service:cesfwk_core", "common_event_service:cesfwk_innerkits", diff --git a/interfaces/kits/ani/bundle_manager/ani_bundle_manager.cpp b/interfaces/kits/ani/bundle_manager/ani_bundle_manager.cpp index 529d4977b8..07673e3777 100644 --- a/interfaces/kits/ani/bundle_manager/ani_bundle_manager.cpp +++ b/interfaces/kits/ani/bundle_manager/ani_bundle_manager.cpp @@ -25,6 +25,7 @@ #include #include "ani_bundle_manager.h" +#include "ani_common_want.h" #include "app_log_wrapper.h" #include "bundle_errors.h" #include "bundle_mgr_client.h" @@ -39,6 +40,7 @@ namespace OHOS { namespace AppExecFwk { namespace { constexpr const char* BUNDLE_FLAGS = "bundleFlags"; +constexpr const char* ABILITY_FLAGS = "abilityFlags"; constexpr const char* APPLICATION_FLAGS = "applicationFlags"; constexpr const char* ERR_MSG_BUNDLE_SERVICE_EXCEPTION = "Bundle manager service is excepted."; const std::string IS_APPLICATION_ENABLED_SYNC = "IsApplicationEnabledSync"; @@ -47,7 +49,15 @@ const std::string GET_BUNDLE_INFO_SYNC = "GetBundleInfoSync"; const std::string GET_APPLICATION_INFO_SYNC = "GetApplicationInfoSync"; const std::string BUNDLE_PERMISSIONS = "ohos.permission.GET_BUNDLE_INFO or ohos.permission.GET_BUNDLE_INFO_PRIVILEGED"; const std::string GET_BUNDLE_INFO = "GetBundleInfo"; +const std::string GET_BUNDLE_INFOS = "GetBundleInfos"; +const std::string GET_APPLICATION_INFOS = "GetApplicationInfos"; +const std::string IS_APPLICATION_ENABLED = "IsApplicationEnabled"; +const std::string QUERY_ABILITY_INFOS_SYNC = "QueryAbilityInfosSync"; +const std::string INVALID_WANT_ERROR = + "implicit query condition, at least one query param(action, entities, uri, type, or linkFeature) non-empty."; +const std::string PARAM_TYPE_CHECK_ERROR = "param type check error"; constexpr const char* GET_APPLICATION_INFO = "GetApplicationInfo"; +const char* LINK_FEATURE = "linkFeature"; static ani_vm* g_vm; static std::mutex g_aniClearCacheListenerMutex; static std::shared_ptr g_aniClearCacheListener; @@ -71,7 +81,92 @@ static void CheckToCache( } } -static ani_boolean isApplicationEnabledSync([[maybe_unused]] ani_env* env, ani_string aniBundleName) +static void CheckAbilityInfoCache(ani_env* env, const ANIQuery& query, + const OHOS::AAFwk::Want &want, std::vector abilityInfos, ani_object aniObject) +{ + ElementName element = want.GetElement(); + if (element.GetBundleName().empty() || element.GetAbilityName().empty()) { + return; + } + + uint32_t explicitQueryResultLen = 1; + if (abilityInfos.size() != explicitQueryResultLen || + abilityInfos[0].uid != IPCSkeleton::GetCallingUid()) { + return; + } + + ani_ref cacheAbilityInfo = nullptr; + ani_status status = env->GlobalReference_Create(aniObject, &cacheAbilityInfo); + if (status == ANI_OK) { + std::unique_lock lock(g_aniCacheMutex); + g_aniCache[query] = cacheAbilityInfo; + } +} + +static bool ParseAniWant(ani_env* env, ani_object aniWant, OHOS::AAFwk::Want& want) +{ + std::string bundleName = CommonFunAni::GetStringOrUndefined(env, aniWant, "bundleName"); + std::string abilityName = CommonFunAni::GetStringOrUndefined(env, aniWant, "abilityName"); + std::string moduleName = CommonFunAni::GetStringOrUndefined(env, aniWant, "moduleName"); + if (!bundleName.empty() && !abilityName.empty()) { + ElementName elementName("", bundleName, abilityName, moduleName); + want.SetElement(elementName); + return true; + } + if (!UnwrapWant(env, aniWant, want)) { + APP_LOGW("parse want failed"); + return false; + } + bool isExplicit = !want.GetBundle().empty() && !want.GetElement().GetAbilityName().empty(); + if (!isExplicit && want.GetAction().empty() && want.GetEntities().empty() && + want.GetUriString().empty() && want.GetType().empty() && want.GetStringParam(LINK_FEATURE).empty()) { + APP_LOGW("implicit params all empty"); + return false; + } + return true; +} + +static ErrCode ParamsProcessQueryAbilityInfosSync(ani_env* env, ani_object aniWant, ani_double abilityFlags, + ani_object userIdObject, OHOS::AAFwk::Want& want, int32_t& abilityFlagsInt, int32_t& userIdInt) +{ + if (!ParseAniWant(env, aniWant, want)) { + APP_LOGE("ParseAniWant failed"); + BusinessErrorAni::ThrowError(env, ERROR_PARAM_CHECK_ERROR, INVALID_WANT_ERROR); + return ERROR_PARAM_CHECK_ERROR; + } + + if (!CommonFunAni::TryCastDoubleTo(abilityFlags, &abilityFlagsInt)) { + APP_LOGE("Cast abilityFlags failed"); + BusinessErrorAni::ThrowParameterTypeError( + env, ERROR_PARAM_CHECK_ERROR, ABILITY_FLAGS, CommonFunAniNS::TYPE_NUMBER); + return ERROR_PARAM_CHECK_ERROR; + } + ani_boolean isUndefined; + env->Reference_IsUndefined(userIdObject, &isUndefined); + if (!isUndefined) { + ani_double userId; + ani_status status = env->Object_CallMethodByName_Double(userIdObject, "doubleValue", nullptr, &userId); + if (status != ANI_OK) { + APP_LOGE("Object_CallMethodByName_Double : %{public}d", status); + BusinessErrorAni::ThrowParameterTypeError( + env, ERROR_PARAM_CHECK_ERROR, Constants::USER_ID, CommonFunAniNS::TYPE_NUMBER); + return ERROR_PARAM_CHECK_ERROR; + } + if (!CommonFunAni::TryCastDoubleTo(userId, &userIdInt)) { + APP_LOGE("Cast userId failed"); + BusinessErrorAni::ThrowParameterTypeError( + env, ERROR_PARAM_CHECK_ERROR, Constants::USER_ID, CommonFunAniNS::TYPE_NUMBER); + return ERROR_PARAM_CHECK_ERROR; + } + } + int32_t callingUid = IPCSkeleton::GetCallingUid(); + if (userIdInt == Constants::UNSPECIFIED_USERID) { + userIdInt = callingUid / Constants::BASE_USER_RANGE; + } + return ERR_OK; +} + +static ani_boolean isApplicationEnabledSync(ani_env* env, ani_string aniBundleName) { bool isEnable = false; std::string bundleName = CommonFunAni::AniStrToString(env, aniBundleName); @@ -87,7 +182,7 @@ static ani_boolean isApplicationEnabledSync([[maybe_unused]] ani_env* env, ani_s BusinessErrorAni::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION); return isEnable; } - int32_t ret = iBundleMgr->IsApplicationEnabled(bundleName, isEnable); + ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->IsApplicationEnabled(bundleName, isEnable)); if (ret != ERR_OK) { APP_LOGE("IsApplicationEnabled failed ret: %{public}d", ret); BusinessErrorAni::ThrowParameterTypeError(env, ret, IS_APPLICATION_ENABLED_SYNC, ""); @@ -95,13 +190,13 @@ static ani_boolean isApplicationEnabledSync([[maybe_unused]] ani_env* env, ani_s return isEnable; } -static ani_object getBundleInfoForSelfSync([[maybe_unused]] ani_env* env, ani_double bundleFlags) +static ani_object getBundleInfoForSelfSync(ani_env* env, ani_double bundleFlags) { int32_t bundleFlagsInt = 0; if (!CommonFunAni::TryCastDoubleTo(bundleFlags, &bundleFlagsInt)) { APP_LOGE("Cast bundleFlags failed"); BusinessErrorAni::ThrowParameterTypeError( - env, ERROR_PARAM_CHECK_ERROR, BUNDLE_FLAGS, CommonFunAniNS::TYPE_INT); + env, ERROR_PARAM_CHECK_ERROR, BUNDLE_FLAGS, CommonFunAniNS::TYPE_NUMBER); return nullptr; } auto iBundleMgr = CommonFunc::GetBundleMgr(); @@ -123,7 +218,7 @@ static ani_object getBundleInfoForSelfSync([[maybe_unused]] ani_env* env, ani_do } BundleInfo bundleInfo; - int32_t ret = iBundleMgr->GetBundleInfoForSelf(bundleFlagsInt, bundleInfo); + ErrCode ret = CommonFunc::ConvertErrCode(iBundleMgr->GetBundleInfoForSelf(bundleFlagsInt, bundleInfo)); if (ret != ERR_OK) { APP_LOGE("GetBundleInfoForSelf failed ret: %{public}d", ret); BusinessErrorAni::ThrowParameterTypeError(env, ret, GET_BUNDLE_INFO_FOR_SELF_SYNC, BUNDLE_PERMISSIONS); @@ -138,21 +233,21 @@ static ani_object getBundleInfoForSelfSync([[maybe_unused]] ani_env* env, ani_do return objectBundleInfo; } -static ani_object getBundleInfoSync([[maybe_unused]] ani_env* env, +static ani_object getBundleInfoSync(ani_env* env, ani_string aniBundleName, ani_double bundleFlags, ani_double userId) { int32_t bundleFlagsInt = 0; if (!CommonFunAni::TryCastDoubleTo(bundleFlags, &bundleFlagsInt)) { APP_LOGE("Cast bundleFlags failed"); BusinessErrorAni::ThrowParameterTypeError( - env, ERROR_PARAM_CHECK_ERROR, BUNDLE_FLAGS, CommonFunAniNS::TYPE_INT); + env, ERROR_PARAM_CHECK_ERROR, BUNDLE_FLAGS, CommonFunAniNS::TYPE_NUMBER); return nullptr; } int32_t userIdInt = 0; if (!CommonFunAni::TryCastDoubleTo(userId, &userIdInt)) { APP_LOGE("Cast userId failed"); BusinessErrorAni::ThrowParameterTypeError( - env, ERROR_PARAM_CHECK_ERROR, Constants::USER_ID, CommonFunAniNS::TYPE_INT); + env, ERROR_PARAM_CHECK_ERROR, Constants::USER_ID, CommonFunAniNS::TYPE_NUMBER); return nullptr; } int32_t callingUid = IPCSkeleton::GetCallingUid(); @@ -184,7 +279,8 @@ static ani_object getBundleInfoSync([[maybe_unused]] ani_env* env, return nullptr; } BundleInfo bundleInfo; - ErrCode ret = iBundleMgr->GetBundleInfoV9(bundleName, bundleFlagsInt, bundleInfo, userIdInt); + ErrCode ret = CommonFunc::ConvertErrCode( + iBundleMgr->GetBundleInfoV9(bundleName, bundleFlagsInt, bundleInfo, userIdInt)); if (ret != ERR_OK) { APP_LOGE("GetBundleInfoV9 failed ret: %{public}d", ret); BusinessErrorAni::ThrowParameterTypeError(env, ret, GET_BUNDLE_INFO_SYNC, BUNDLE_PERMISSIONS); @@ -199,21 +295,21 @@ static ani_object getBundleInfoSync([[maybe_unused]] ani_env* env, return objectBundleInfo; } -static ani_object getApplicationInfoSync([[maybe_unused]] ani_env* env, +static ani_object getApplicationInfoSync(ani_env* env, ani_string aniBundleName, ani_double applicationFlags, ani_double userId) { int32_t applicationFlagsInt = 0; if (!CommonFunAni::TryCastDoubleTo(applicationFlags, &applicationFlagsInt)) { APP_LOGE("Cast applicationFlags failed"); BusinessErrorAni::ThrowParameterTypeError( - env, ERROR_PARAM_CHECK_ERROR, APPLICATION_FLAGS, CommonFunAniNS::TYPE_INT); + env, ERROR_PARAM_CHECK_ERROR, APPLICATION_FLAGS, CommonFunAniNS::TYPE_NUMBER); return nullptr; } int32_t userIdInt = 0; if (!CommonFunAni::TryCastDoubleTo(userId, &userIdInt)) { APP_LOGE("Cast userId failed"); BusinessErrorAni::ThrowParameterTypeError( - env, ERROR_PARAM_CHECK_ERROR, Constants::USER_ID, CommonFunAniNS::TYPE_INT); + env, ERROR_PARAM_CHECK_ERROR, Constants::USER_ID, CommonFunAniNS::TYPE_NUMBER); return nullptr; } std::string bundleName = CommonFunAni::AniStrToString(env, aniBundleName); @@ -244,7 +340,8 @@ static ani_object getApplicationInfoSync([[maybe_unused]] ani_env* env, return nullptr; } ApplicationInfo appInfo; - ErrCode ret = iBundleMgr->GetApplicationInfoV9(bundleName, applicationFlagsInt, userIdInt, appInfo); + ErrCode ret = CommonFunc::ConvertErrCode( + iBundleMgr->GetApplicationInfoV9(bundleName, applicationFlagsInt, userIdInt, appInfo)); if (ret != ERR_OK) { APP_LOGE("GetApplicationInfoV9 failed ret: %{public}d,userIdInt: %{public}d", ret, userIdInt); BusinessErrorAni::ThrowParameterTypeError(env, ret, GET_APPLICATION_INFO_SYNC, BUNDLE_PERMISSIONS); @@ -257,6 +354,167 @@ static ani_object getApplicationInfoSync([[maybe_unused]] ani_env* env, return objectApplicationInfo; } +static ani_object getAllBundleInfoSync(ani_env* env, ani_double bundleFlags, ani_double userId) +{ + int32_t bundleFlagsInt = 0; + if (!CommonFunAni::TryCastDoubleTo(bundleFlags, &bundleFlagsInt)) { + APP_LOGE("Cast bundleFlags failed"); + BusinessErrorAni::ThrowParameterTypeError( + env, ERROR_PARAM_CHECK_ERROR, BUNDLE_FLAGS, CommonFunAniNS::TYPE_NUMBER); + return nullptr; + } + int32_t userIdInt = 0; + if (!CommonFunAni::TryCastDoubleTo(userId, &userIdInt)) { + APP_LOGE("Cast userId failed"); + BusinessErrorAni::ThrowParameterTypeError( + env, ERROR_PARAM_CHECK_ERROR, Constants::USER_ID, CommonFunAniNS::TYPE_NUMBER); + return nullptr; + } + int32_t callingUid = IPCSkeleton::GetCallingUid(); + if (userIdInt == EMPTY_USER_ID) { + userIdInt = callingUid / Constants::BASE_USER_RANGE; + } + + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("Get bundle mgr failed"); + BusinessErrorAni::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION); + return nullptr; + } + std::vector bundleInfos; + ErrCode ret = CommonFunc::ConvertErrCode( + iBundleMgr->GetBundleInfosV9(bundleFlagsInt, bundleInfos, userIdInt)); + if (ret != ERR_OK) { + APP_LOGE("GetBundleInfosV9 failed ret: %{public}d", ret); + BusinessErrorAni::ThrowParameterTypeError(env, ret, GET_BUNDLE_INFOS, + Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST); + return nullptr; + } + APP_LOGE("GetBundleInfosV9 ret: %{public}d, bundleInfo size: %{public}d", ret, bundleInfos.size()); + + return CommonFunAni::ConvertAniArray(env, bundleInfos, CommonFunAni::ConvertBundleInfo); +} + +static ani_object getAllApplicationInfoSync(ani_env* env, ani_double applicationFlags, ani_double userId) +{ + int32_t applicationFlagsInt = 0; + if (!CommonFunAni::TryCastDoubleTo(applicationFlags, &applicationFlagsInt)) { + APP_LOGE("Cast applicationFlags failed"); + BusinessErrorAni::ThrowParameterTypeError( + env, ERROR_PARAM_CHECK_ERROR, APPLICATION_FLAGS, CommonFunAniNS::TYPE_NUMBER); + return nullptr; + } + int32_t userIdInt = 0; + if (!CommonFunAni::TryCastDoubleTo(userId, &userIdInt)) { + APP_LOGE("Cast userId failed"); + BusinessErrorAni::ThrowParameterTypeError( + env, ERROR_PARAM_CHECK_ERROR, Constants::USER_ID, CommonFunAniNS::TYPE_NUMBER); + return nullptr; + } + + int32_t callingUid = IPCSkeleton::GetCallingUid(); + if (userIdInt == EMPTY_USER_ID) { + userIdInt = callingUid / Constants::BASE_USER_RANGE; + } + + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("nullptr iBundleMgr"); + BusinessErrorAni::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION); + return nullptr; + } + std::vector appInfos; + ErrCode ret = CommonFunc::ConvertErrCode( + iBundleMgr->GetApplicationInfosV9(applicationFlagsInt, userIdInt, appInfos)); + if (ret != ERR_OK) { + APP_LOGE("GetApplicationInfosV9 failed ret: %{public}d", ret); + BusinessErrorAni::ThrowParameterTypeError(env, ret, GET_APPLICATION_INFOS, + Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST); + return nullptr; + } + + return CommonFunAni::ConvertAniArray(env, appInfos, CommonFunAni::ConvertApplicationInfo); +} + +static ani_boolean isApplicationEnabledWithAppIndexSync(ani_env* env, + ani_string aniBundleName, ani_double appIndex) +{ + bool isEnable = false; + std::string bundleName = CommonFunAni::AniStrToString(env, aniBundleName); + if (bundleName.empty()) { + APP_LOGE("BundleName is empty"); + BusinessErrorAni::ThrowParameterTypeError( + env, ERROR_PARAM_CHECK_ERROR, Constants::BUNDLE_NAME, CommonFunAniNS::TYPE_STRING); + return isEnable; + } + int32_t appIndexInt = 0; + if (!CommonFunAni::TryCastDoubleTo(appIndex, &appIndexInt)) { + APP_LOGE("Cast appIndex failed"); + BusinessErrorAni::ThrowParameterTypeError( + env, ERROR_PARAM_CHECK_ERROR, Constants::APP_INDEX, CommonFunAniNS::TYPE_NUMBER); + return false; + } + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("GetBundleMgr failed"); + BusinessErrorAni::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION); + return isEnable; + } + ErrCode ret = ERR_OK; + if (appIndexInt != 0) { + ret = CommonFunc::ConvertErrCode( + iBundleMgr->IsCloneApplicationEnabled(bundleName, appIndexInt, isEnable)); + } else { + ret = CommonFunc::ConvertErrCode(iBundleMgr->IsApplicationEnabled(bundleName, isEnable)); + } + if (ret != ERR_OK) { + APP_LOGE("IsCloneApplicationEnabled failed ret: %{public}d", ret); + BusinessErrorAni::ThrowParameterTypeError(env, ret, IS_APPLICATION_ENABLED, ""); + } + return isEnable; +} + +static ani_object queryAbilityInfoSync(ani_env* env, + ani_object aniWant, ani_double abilityFlags, ani_object userIdObject) +{ + OHOS::AAFwk::Want want; + int32_t abilityFlagsInt = 0; + int32_t userIdInt = Constants::UNSPECIFIED_USERID; + if (ParamsProcessQueryAbilityInfosSync( + env, aniWant, abilityFlags, userIdObject, want, abilityFlagsInt, userIdInt) != ERR_OK) { + APP_LOGE("paramsProcess is invalid"); + BusinessErrorAni::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_TYPE_CHECK_ERROR); + return nullptr; + } + + const ANIQuery query(want.ToString(), QUERY_ABILITY_INFOS_SYNC, abilityFlagsInt, userIdInt); + { + std::shared_lock lock(g_aniCacheMutex); + auto item = g_aniCache.find(query); + if (item != g_aniCache.end()) { + return reinterpret_cast(item->second); + } + } + + auto iBundleMgr = CommonFunc::GetBundleMgr(); + if (iBundleMgr == nullptr) { + APP_LOGE("GetBundleMgr failed"); + BusinessErrorAni::ThrowError(env, ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION); + return nullptr; + } + std::vector abilityInfos; + ErrCode ret = CommonFunc::ConvertErrCode( + iBundleMgr->QueryAbilityInfosV9(want, abilityFlagsInt, userIdInt, abilityInfos)); + if (ret != ERR_OK) { + APP_LOGE("QueryAbilityInfosV9 failed ret: %{public}d", ret); + BusinessErrorAni::ThrowParameterTypeError(env, ret, QUERY_ABILITY_INFOS_SYNC, BUNDLE_PERMISSIONS); + } + ani_object aniAbilityInfos = + CommonFunAni::ConvertAniArray(env, abilityInfos, CommonFunAni::ConvertAbilityInfo); + CheckAbilityInfoCache(env, query, want, abilityInfos, aniAbilityInfos); + return aniAbilityInfos; +} + extern "C" { ANI_EXPORT ani_status ANI_Constructor(ani_vm* vm, uint32_t* result) { @@ -284,6 +542,15 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm* vm, uint32_t* result) ani_native_function { "getApplicationInfoSync", "Lstd/core/String;DD:LbundleManager/ApplicationInfo/ApplicationInfo;", reinterpret_cast(getApplicationInfoSync) }, + ani_native_function { "getAllBundleInfoSync", "DD:Lescompat/Array;", + reinterpret_cast(getAllBundleInfoSync) }, + ani_native_function { "getAllApplicationInfoSync", "DD:Lescompat/Array;", + reinterpret_cast(getAllApplicationInfoSync) }, + ani_native_function { "isApplicationEnabledSync", "Lstd/core/String;D:Z", + reinterpret_cast(isApplicationEnabledWithAppIndexSync) }, + ani_native_function { "queryAbilityInfoSync", + "L@ohos/app/ability/Want/Want;DLstd/core/Double;:Lescompat/Array;", + reinterpret_cast(queryAbilityInfoSync) }, }; if (env->Namespace_BindNativeFunctions(kitNs, methods.data(), methods.size()) != ANI_OK) { diff --git a/interfaces/kits/ani/bundle_manager/ets/@ohos.bundle.bundleManager.ets b/interfaces/kits/ani/bundle_manager/ets/@ohos.bundle.bundleManager.ets index f3b1db1445..737fbfae1b 100644 --- a/interfaces/kits/ani/bundle_manager/ets/@ohos.bundle.bundleManager.ets +++ b/interfaces/kits/ani/bundle_manager/ets/@ohos.bundle.bundleManager.ets @@ -16,6 +16,9 @@ import { BundleInfo } from 'bundleManager.BundleInfo'; import { AsyncCallback, BusinessError } from '@ohos.base'; import { ApplicationInfo } from 'bundleManager.ApplicationInfo'; +import { AbilityInfo } from 'bundleManager.AbilityInfo'; +import Want from '@ohos.app.ability.Want'; +import { BundleInfoInner } from './bundleManager/BundleInfoInner'; namespace bundleManager { @@ -179,6 +182,14 @@ namespace bundleManager { export native function getApplicationInfoSync(bundleName: string, applicationFlags: number, userId: number): ApplicationInfo; + export native function getAllBundleInfoSync(bundleFlags: number, userId: number): Array; + + export native function getAllApplicationInfoSync(appFlags: number, userId: number): Array; + + export native function isApplicationEnabledSync(bundleName: string, appIndex: number): boolean; + + export native function queryAbilityInfoSync(want: Want, abilityFlags: number, userId?: number): Array; + function getBundleInfoForSelf(bundleFlags: number, callback: AsyncCallback):void { let execFun = (): BundleInfo => { return bundleManager.getBundleInfoForSelfSync(bundleFlags); @@ -186,14 +197,16 @@ namespace bundleManager { let p1 = taskpool.execute(execFun); p1.then((e: NullishType) => { let resultBundleInfo: BundleInfo = e as BundleInfo; - let r: BusinessError = { code: 0, name: "error", message: "error", data: undefined }; + let r = new BusinessError(); callback(r, resultBundleInfo); - }, (err: Object): void => { + }, (err: BusinessError): void => { + let resultBundleInfo = new BundleInfoInner(); + callback(err, resultBundleInfo); }); } function getBundleInfoForSelf(bundleFlags: number):Promise { - let p = new Promise((resolve: (bundleInfo: BundleInfo) => void, reject: (error: Object) => void) => { + let p = new Promise((resolve: (bundleInfo: BundleInfo) => void, reject: (error: BusinessError) => void) => { let execFun = (): BundleInfo => { return bundleManager.getBundleInfoForSelfSync(bundleFlags); }; @@ -201,7 +214,8 @@ namespace bundleManager { p1.then((e: NullishType) => { let resultBundleInfo: BundleInfo = e as BundleInfo; resolve(resultBundleInfo); - }, (err: Object): void => { + }, (err: BusinessError): void => { + reject(err); }); } ); @@ -213,7 +227,7 @@ namespace bundleManager { } function getBundleInfo(bundleName: string, bundleFlags: number, userId?: number): Promise { - let p = new Promise((resolve: (bundleInfo: BundleInfo) => void, reject: (error: Object) => void) => { + let p = new Promise((resolve: (bundleInfo: BundleInfo) => void, reject: (error: BusinessError) => void) => { let userIdInfo: number = userId ?? -500; let execFun = (): BundleInfo => { return bundleManager.getBundleInfoSync(bundleName, bundleFlags, userIdInfo); @@ -222,7 +236,8 @@ namespace bundleManager { p1.then((e: NullishType) => { let resultBundleInfo: BundleInfo = e as BundleInfo; resolve(resultBundleInfo); - }, (err: Object): void => { + }, (err: BusinessError): void => { + reject(err); }); } ); @@ -236,9 +251,11 @@ namespace bundleManager { let p1 = taskpool.execute(execFun); p1.then((e: NullishType) => { let resultBundleInfo: BundleInfo = e as BundleInfo; - let r: BusinessError = { code: 0, name: "error", message: "error", data: undefined }; + let r = new BusinessError(); callback(r, resultBundleInfo); - }, (err: Object): void => { + },(err: BusinessError): void => { + let resultBundleInfo = new BundleInfoInner(); + callback(err, resultBundleInfo); }); } @@ -249,15 +266,213 @@ namespace bundleManager { let p1 = taskpool.execute(execFun); p1.then((e: NullishType) => { let resultBundleInfo: BundleInfo = e as BundleInfo; - let r: BusinessError = { code: 0, name: "error", message: "error", data: undefined }; + let r = new BusinessError(); callback(r, resultBundleInfo); - }, (err: Object): void => { + },(err: BusinessError): void => { + let resultBundleInfo = new BundleInfoInner(); + callback(err, resultBundleInfo); }); } function getApplicationInfoSync(bundleName: string, applicationFlags: number): ApplicationInfo { return bundleManager.getApplicationInfoSync(bundleName, applicationFlags, -500); } + + function getAllBundleInfo(bundleFlags: number, userId?: number): Promise> { + let p = new Promise>((resolve: (bundleInfos: Array) + => void, reject: (error: BusinessError) => void) => { + let userIdInfo: number = userId ?? -500; + let execFun = (): Array => { + return bundleManager.getAllBundleInfoSync(bundleFlags, userIdInfo); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let resultBundleInfos: Array = e as Array; + resolve(resultBundleInfos); + }, (err: BusinessError): void => { + reject(err); + }); + } + ); + return p; + } + + function getAllBundleInfo(bundleFlags: number, callback: AsyncCallback>): void { + let execFun = (): Array => { + return bundleManager.getAllBundleInfoSync(bundleFlags, -500); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let resultBundleInfos: Array = e as Array; + let r = new BusinessError(); + callback(r, resultBundleInfos); + }, (err: BusinessError): void => { + let resultBundleInfos = new Array; + callback(err, resultBundleInfos); + }); + } + + function getAllBundleInfo(bundleFlags: number, userId: number, callback: AsyncCallback>): void { + let execFun = (): Array => { + return bundleManager.getAllBundleInfoSync(bundleFlags, userId); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let resultBundleInfos: Array = e as Array; + let r = new BusinessError(); + callback(r, resultBundleInfos); + }, (err: BusinessError): void => { + let resultBundleInfos = new Array; + callback(err, resultBundleInfos); + }); + } + + function getAllApplicationInfo(appFlags: number, userId?: number): Promise> { + let p = new Promise>((resolve: (applicationInfos: Array) + => void, reject: (error: BusinessError) => void) => { + let userIdInfo: number = userId ?? -500; + let execFun = (): Array => { + return bundleManager.getAllApplicationInfoSync(appFlags, userIdInfo); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let resultApplicationInfos: Array = e as Array; + resolve(resultApplicationInfos); + }, (err: BusinessError): void => { + reject(err); + }); + } + ); + return p; + } + + function getAllApplicationInfo(appFlags: number, callback: AsyncCallback>): void { + let execFun = (): Array => { + return bundleManager.getAllApplicationInfoSync(appFlags, -500); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let resultApplicationInfos: Array = e as Array; + let r = new BusinessError(); + callback(r, resultApplicationInfos); + }, (err: BusinessError): void => { + let resultApplicationInfos = new Array; + callback(err, resultApplicationInfos); + }); + } + + function getAllApplicationInfo(appFlags: number, userId: number, + callback: AsyncCallback>): void { + let execFun = (): Array => { + return bundleManager.getAllApplicationInfoSync(appFlags, userId); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let resultApplicationInfos: Array = e as Array; + let r = new BusinessError(); + callback(r, resultApplicationInfos); + }, (err: BusinessError): void => { + let resultApplicationInfos = new Array; + callback(err, resultApplicationInfos); + }); + } + + function isApplicationEnabled(bundleName: string, appIndex: number): Promise { + let p = new Promise((resolve: (isEnabled: boolean) => void, reject: (error: BusinessError) => void) => { + let execFun = (): boolean => { + return bundleManager.isApplicationEnabledSync(bundleName, appIndex); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let isEnabled: boolean = e as boolean; + resolve(isEnabled); + }, (err: BusinessError): void => { + reject(err); + }); + } + ); + return p; + } + + function isApplicationEnabled(bundleName: string): Promise { + let p = new Promise((resolve: (isEnabled: boolean) => void, reject: (error: BusinessError) => void) => { + let execFun = (): boolean => { + return bundleManager.isApplicationEnabledSync(bundleName); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let isEnabled: boolean = e as boolean; + resolve(isEnabled); + }, (err: BusinessError): void => { + reject(err); + }); + } + ); + return p; + } + + function isApplicationEnabled(bundleName: string, callback: AsyncCallback): void { + let execFun = (): boolean => { + return bundleManager.isApplicationEnabledSync(bundleName); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let isEnabled: boolean = e as boolean; + let r = new BusinessError(); + callback(r, isEnabled); + }, (err: BusinessError): void => { + callback(err, false); + }); + } + + function queryAbilityInfo(want: Want, abilityFlags: number, userId?: number): Promise> { + let p = new Promise>((resolve: (abilityInfos: Array) + => void, reject: (error: BusinessError) => void) => { + let execFun = (): Array => { + return bundleManager.queryAbilityInfoSync(want, abilityFlags, userId); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let resultAbilityInfos: Array = e as Array; + resolve(resultAbilityInfos); + }, (err: BusinessError): void => { + reject(err); + }); + } + ); + return p; + } + + function queryAbilityInfo(want: Want, abilityFlags: number, callback: AsyncCallback>): void { + let execFun = (): Array => { + return bundleManager.queryAbilityInfoSync(want, abilityFlags); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let resultAbilityInfos: Array = e as Array; + let r = new BusinessError(); + callback(r, resultAbilityInfos); + }, (err: BusinessError): void => { + let resultAbilityInfos = new Array; + callback(err, resultAbilityInfos); + }); + } + + function queryAbilityInfo(want: Want, + abilityFlags: number, userId: number, callback: AsyncCallback>): void { + let execFun = (): Array => { + return bundleManager.queryAbilityInfoSync(want, abilityFlags, userId); + }; + let p1 = taskpool.execute(execFun); + p1.then((e: NullishType) => { + let resultAbilityInfos: Array = e as Array; + let r = new BusinessError(); + callback(r, resultAbilityInfos); + }, (err: BusinessError): void => { + let resultAbilityInfos = new Array; + callback(err, resultAbilityInfos); + }); + } } export default bundleManager; diff --git a/interfaces/kits/ani/common/common_fun_ani.cpp b/interfaces/kits/ani/common/common_fun_ani.cpp index 473f0bf110..2a45f17cfb 100644 --- a/interfaces/kits/ani/common/common_fun_ani.cpp +++ b/interfaces/kits/ani/common/common_fun_ani.cpp @@ -234,6 +234,28 @@ ani_object CommonFunAni::CreateNewObjectByClass(ani_env* env, ani_class cls) return object; } +std::string CommonFunAni::GetStringOrUndefined(ani_env *env, ani_object param, const char *name) +{ + ani_ref obj = nullptr; + ani_boolean isUndefined = true; + + ani_status status = env->Object_GetFieldByName_Ref(param, name, &obj); + if (status != ANI_OK) { + APP_LOGE("Object_GetFieldByName_Ref : %{public}d", status); + return ""; + } + status = env->Reference_IsUndefined(obj, &isUndefined); + if (status != ANI_OK) { + APP_LOGE("Reference_IsUndefined : %{public}d", status); + return ""; + } + if (isUndefined) { + APP_LOGE("%{public}s : undefined", name); + return ""; + } + return AniStrToString(env, reinterpret_cast(obj)); +} + ani_ref CommonFunAni::ConvertAniArrayString(ani_env* env, const std::vector& cArray) { RETURN_NULL_IF_NULL(env); diff --git a/interfaces/kits/ani/common/common_fun_ani.h b/interfaces/kits/ani/common/common_fun_ani.h index 27356fa129..aa34d60881 100644 --- a/interfaces/kits/ani/common/common_fun_ani.h +++ b/interfaces/kits/ani/common/common_fun_ani.h @@ -31,6 +31,7 @@ namespace OHOS { namespace AppExecFwk { namespace CommonFunAniNS { constexpr const char* TYPE_INT = "int"; +constexpr const char* TYPE_NUMBER = "number"; constexpr const char* TYPE_STRING = "string"; } // namespace CommonFunAniNS @@ -83,6 +84,7 @@ public: } return true; } + static std::string GetStringOrUndefined(ani_env *env, ani_object param, const char *name); // Convert from native to ets static ani_object ConvertMultiAppMode(ani_env* env, const MultiAppModeData& multiAppMode); -- Gitee