diff --git a/frameworks/ets/ani/ani_common/BUILD.gn b/frameworks/ets/ani/ani_common/BUILD.gn index 2d67c749d72b6ace36f668e26e8434a9337a652b..eeb2538c60c4903e600cf43d5c7267decae8d9fb 100644 --- a/frameworks/ets/ani/ani_common/BUILD.gn +++ b/frameworks/ets/ani/ani_common/BUILD.gn @@ -40,6 +40,8 @@ ohos_shared_library("ani_common") { public_configs = [ ":ani_common_public_config" ] sources = [ + "src/ani_common_ability_result.cpp", + "src/ani_common_configuration.cpp", "src/ani_common_start_options.cpp", "src/ani_common_util.cpp", "src/ani_common_want.cpp", diff --git a/frameworks/ets/ani/ani_common/include/ani_common_ability_result.h b/frameworks/ets/ani/ani_common/include/ani_common_ability_result.h new file mode 100644 index 0000000000000000000000000000000000000000..0d5618a91372b43fc761c61a2256aef7a5d9cf80 --- /dev/null +++ b/frameworks/ets/ani/ani_common/include/ani_common_ability_result.h @@ -0,0 +1,28 @@ +/* + * 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_COMMON_ABILITY_RESULT_H +#define OHOS_ABILITY_RUNTIME_ANI_COMMON_ABILITY_RESULT_H + +#include "ani.h" +#include "want.h" + +namespace OHOS { +namespace AppExecFwk { +ani_object WrapAbilityResult(ani_env *env, int32_t resultCode, const AAFwk::Want &want); +} // namespace AppExecFwk +} // namespace OHOS + +#endif // OHOS_ABILITY_RUNTIME_ANI_COMMON_ABILITY_RESULT_H \ No newline at end of file diff --git a/frameworks/ets/ani/ani_common/include/ani_common_configuration.h b/frameworks/ets/ani/ani_common/include/ani_common_configuration.h new file mode 100644 index 0000000000000000000000000000000000000000..039b86e511988f279096dd718cfeb44723cabb78 --- /dev/null +++ b/frameworks/ets/ani/ani_common/include/ani_common_configuration.h @@ -0,0 +1,28 @@ +/* + * 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_COMMON_CONFIGURATION_H +#define OHOS_ABILITY_RUNTIME_ANI_COMMON_CONFIGURATION_H + +#include "ani_common_util.h" +#include "configuration.h" + +namespace OHOS { +namespace AppExecFwk { +ani_object WrapConfiguration(ani_env *env, const AppExecFwk::Configuration &configuration); +bool UnwrapConfiguration(ani_env *env, ani_object param, Configuration &config); +} // namespace AppExecFwk +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_ANI_COMMON_CONFIGURATION_H diff --git a/frameworks/ets/ani/ani_common/src/ani_common_ability_result.cpp b/frameworks/ets/ani/ani_common/src/ani_common_ability_result.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73e45b2e9922cf27fb7d1f6354e6bb94a4d39339 --- /dev/null +++ b/frameworks/ets/ani/ani_common/src/ani_common_ability_result.cpp @@ -0,0 +1,72 @@ +/* + * 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 "ani_common_ability_result.h" + +#include "ani_common_want.h" +#include "hilog_tag_wrapper.h" + +namespace OHOS { +namespace AppExecFwk { + +ani_object WrapAbilityResult(ani_env *env, int32_t resultCode, const AAFwk::Want &want) +{ + ani_class cls = nullptr; + ani_status status = ANI_ERROR; + ani_method ctor = nullptr; + ani_object result_obj = {}; + static const char *className = "Lability/abilityResult/AbilityResultInner;"; + + if ((status = env->FindClass(className, &cls)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "status : %{public}d", status); + return nullptr; + } + + if ((status = env->Class_FindMethod(cls, "", nullptr, &ctor)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "status : %{public}d", status); + return nullptr; + } + + if ((status = env->Object_New(cls, ctor, &result_obj)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "status : %{public}d", status); + return nullptr; + } + + ani_method resultCodeSetter = nullptr; + if ((status = env->Class_FindMethod(cls, "resultCode", nullptr, &resultCodeSetter)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "status : %{public}d", status); + } + + ani_double dResultCode {resultCode}; + if ((status = env->Object_CallMethod_Void(result_obj, resultCodeSetter, dResultCode)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "status : %{public}d", status); + return nullptr; + } + + ani_method wantSetter = nullptr; + if ((status = env->Class_FindMethod(cls, "want", nullptr, &wantSetter)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "status : %{public}d", status); + } + + ani_object wantObj = AppExecFwk::WrapWant(env, want); + if ((status = env->Object_CallMethod_Void(result_obj, wantSetter, wantObj)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "status : %{public}d", status); + return nullptr; + } + + return result_obj; +} +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ets/ani/ani_common/src/ani_common_configuration.cpp b/frameworks/ets/ani/ani_common/src/ani_common_configuration.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4848f232845a85ce2c6a9df15f58e6a167412bef --- /dev/null +++ b/frameworks/ets/ani/ani_common/src/ani_common_configuration.cpp @@ -0,0 +1,162 @@ +/* + * 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 "ani_common_configuration.h" + +#include "ani_enum_convert.h" +#include "configuration_convertor.h" +#include "hilog_tag_wrapper.h" + +namespace OHOS { +namespace AppExecFwk { +namespace { +constexpr double FONT_SIZE_MIN_SCALE = 0.0; +constexpr double FONT_SIZE_MAX_SCALE = 3.2; +constexpr double FONT_WEIGHT_MIN_SCALE = 0.0; +constexpr double FONT_WEIGHT_MAX_SCALE = 1.25; +constexpr const char* COLOR_MODE_ENUM_NAME = + "L@ohos/app/ability/ConfigurationConstant/ConfigurationConstant/ColorMode;"; +constexpr const char* DIRECTION_ENUM_NAME = + "L@ohos/app/ability/ConfigurationConstant/ConfigurationConstant/Direction;"; +constexpr const char* DENSITY_ENUM_NAME = + "L@ohos/app/ability/ConfigurationConstant/ConfigurationConstant/ScreenDensity;"; +constexpr const char* CONFIGURATION_IMPL_CLASS_NAME = "L@ohos/app/ability/Configuration/ConfigurationImpl;"; + +void SetBasicConfiguration( + ani_env *env, ani_class cls, ani_object object, const AppExecFwk::Configuration &configuration) +{ + std::string str = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); + env->Object_SetPropertyByName_Ref(object, "language", GetAniString(env, str)); + + ani_enum_item colorModeItem {}; + OHOS::AAFwk::AniEnumConvertUtil::EnumConvert_NativeToEts(env, + COLOR_MODE_ENUM_NAME, + ConvertColorMode(configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE)), colorModeItem); + env->Object_SetPropertyByName_Ref(object, "colorMode", colorModeItem); + + int32_t displayId = ConvertDisplayId(configuration.GetItem(ConfigurationInner::APPLICATION_DISPLAYID)); + env->Object_SetPropertyByName_Ref(object, "displayId", CreateDouble(env, static_cast(displayId))); + + std::string direction = configuration.GetItem(displayId, ConfigurationInner::APPLICATION_DIRECTION); + ani_enum_item directionItem {}; + OHOS::AAFwk::AniEnumConvertUtil::EnumConvert_NativeToEts(env, + DIRECTION_ENUM_NAME, ConvertDirection(direction), + directionItem); + env->Object_SetPropertyByName_Ref(object, "direction", directionItem); + + std::string density = configuration.GetItem(displayId, ConfigurationInner::APPLICATION_DENSITYDPI); + ani_enum_item densityItem {}; + OHOS::AAFwk::AniEnumConvertUtil::EnumConvert_NativeToEts(env, + DENSITY_ENUM_NAME, ConvertDensity(density), densityItem); + env->Object_SetPropertyByName_Ref(object, "screenDensity", densityItem); +} + +void SetAdditionalConfiguration( + ani_env *env, ani_class cls, ani_object object, const AppExecFwk::Configuration &configuration) +{ + std::string hasPointerDevice = configuration.GetItem(AAFwk::GlobalConfigurationKey::INPUT_POINTER_DEVICE); + env->Object_SetPropertyByName_Ref( + object, "hasPointerDevice", CreateBoolean(env, hasPointerDevice == "true" ? true : false)); + + std::string str = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_ID); + env->Object_SetPropertyByName_Ref(object, "fontId", GetAniString(env, str)); + + std::string fontSizeScale = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE); + env->Object_SetPropertyByName_Ref( + object, "fontSizeScale", CreateDouble(env, fontSizeScale != "" ? std::stod(fontSizeScale) : 1.0)); + + std::string fontWeightScale = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_WEIGHT_SCALE); + env->Object_SetPropertyByName_Ref( + object, "fontWeightScale", CreateDouble(env, fontWeightScale != "" ? std::stod(fontWeightScale) : 1.0)); + + str = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_MCC); + env->Object_SetPropertyByName_Ref(object, "mcc", GetAniString(env, str)); + + str = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_MNC); + env->Object_SetPropertyByName_Ref(object, "mnc", GetAniString(env, str)); +} +} // namespace + +ani_object WrapConfiguration(ani_env *env, const AppExecFwk::Configuration &configuration) +{ + ani_class cls {}; + ani_status status = ANI_ERROR; + ani_method method {}; + ani_object object = nullptr; + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return nullptr; + } + if ((status = env->FindClass(CONFIGURATION_IMPL_CLASS_NAME, &cls)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "status : %{public}d", status); + return nullptr; + } + if (cls == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null Configuration"); + return nullptr; + } + if ((status = env->Class_FindMethod(cls, "", ":V", &method)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "status : %{public}d", status); + return nullptr; + } + if ((status = env->Object_New(cls, method, &object)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "status : %{public}d", status); + return nullptr; + } + if (object == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null object"); + return nullptr; + } + SetBasicConfiguration(env, cls, object, configuration); + SetAdditionalConfiguration(env, cls, object, configuration); + return object; +} + +bool UnwrapConfiguration(ani_env *env, ani_object param, Configuration &config) +{ + std::string language { "" }; + if (GetFieldStringByName(env, param, "language", language)) { + TAG_LOGD(AAFwkTag::ANI, "The parsed language part %{public}s", language.c_str()); + if (!config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, language)) { + TAG_LOGE(AAFwkTag::ANI, "language Parsing failed"); + return false; + } + } + + ani_double fontSizeScale = 0.0; + if (GetFieldDoubleByName(env, param, "fontSizeScale", fontSizeScale)) { + if (fontSizeScale < FONT_SIZE_MIN_SCALE || fontSizeScale > FONT_SIZE_MAX_SCALE) { + TAG_LOGE(AAFwkTag::ANI, "invalid fontSizeScale"); + return false; + } + if (!config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE, std::to_string(fontSizeScale))) { + return false; + } + } + + ani_double fontWeightScale = 0.0; + if (GetFieldDoubleByName(env, param, "fontWeightScale", fontWeightScale)) { + if (fontWeightScale < FONT_WEIGHT_MIN_SCALE || fontWeightScale > FONT_WEIGHT_MAX_SCALE) { + TAG_LOGE(AAFwkTag::ANI, "invalid fontWeightScale"); + return false; + } + if (!config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_WEIGHT_SCALE, std::to_string(fontWeightScale))) { + return false; + } + } + return true; +} +} // namespace AppExecFwk +} // namespace OHOS diff --git a/frameworks/ets/ets/@ohos.app.ability.Configuration.ets b/frameworks/ets/ets/@ohos.app.ability.Configuration.ets new file mode 100644 index 0000000000000000000000000000000000000000..e9db058b47feefc55707f57db1c93690d94facb3 --- /dev/null +++ b/frameworks/ets/ets/@ohos.app.ability.Configuration.ets @@ -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. + */ + +import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant'; + +export interface Configuration { + language?: string; + colorMode?: ConfigurationConstant.ColorMode; + direction?: ConfigurationConstant.Direction; + screenDensity?: ConfigurationConstant.ScreenDensity; + displayId?: number; + hasPointerDevice?: boolean; + fontId?: string; + fontSizeScale?: number; + fontWeightScale?: number; + mcc?: string; + mnc?: string; +} + +class ConfigurationImpl implements Configuration { + language?: string; + colorMode?: ConfigurationConstant.ColorMode; + direction?: ConfigurationConstant.Direction; + screenDensity?: ConfigurationConstant.ScreenDensity; + displayId?: number; + hasPointerDevice?: boolean; + fontId?: string; + fontSizeScale?: number; + fontWeightScale?: number; + mcc?: string; + mnc?: string; +} diff --git a/frameworks/ets/ets/@ohos.app.ability.ConfigurationConstant.ets b/frameworks/ets/ets/@ohos.app.ability.ConfigurationConstant.ets new file mode 100644 index 0000000000000000000000000000000000000000..06b6f4d4745a0250f12fee783548c2b7db74a15e --- /dev/null +++ b/frameworks/ets/ets/@ohos.app.ability.ConfigurationConstant.ets @@ -0,0 +1,40 @@ +/* + * 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. + */ + +namespace ConfigurationConstant { + export enum ColorMode { + COLOR_MODE_NOT_SET = -1, + COLOR_MODE_DARK = 0, + COLOR_MODE_LIGHT = 1 + } + + export enum Direction { + DIRECTION_NOT_SET = -1, + DIRECTION_VERTICAL = 0, + DIRECTION_HORIZONTAL = 1 + } + + export enum ScreenDensity { + SCREEN_DENSITY_NOT_SET = 0, + SCREEN_DENSITY_SDPI = 120, + SCREEN_DENSITY_MDPI = 160, + SCREEN_DENSITY_LDPI = 240, + SCREEN_DENSITY_XLDPI = 320, + SCREEN_DENSITY_XXLDPI = 480, + SCREEN_DENSITY_XXXLDPI = 640 + } +} + +export default ConfigurationConstant; diff --git a/frameworks/ets/ets/BUILD.gn b/frameworks/ets/ets/BUILD.gn index 9f07fdb1852acb6b8a7f2fdd8836c92349d787e4..6cfb8a86a46f41692c36d20351cc11806722472b 100644 --- a/frameworks/ets/ets/BUILD.gn +++ b/frameworks/ets/ets/BUILD.gn @@ -111,11 +111,63 @@ ohos_prebuilt_etc("ability_runtime_want_constant_abc_etc") { deps = [ ":ability_runtime_want_constant_abc" ] } +generate_static_abc("ability_runtime_configuration_abc") { + base_url = "./" + files = [ "./@ohos.app.ability.Configuration.ets" ] + + is_boot_abc = "True" + device_dst_file = "/system/framework/ability_runtime_configuration_abc.abc" +} + +ohos_prebuilt_etc("ability_runtime_configuration_abc_etc") { + source = "$target_out_dir/ability_runtime_configuration_abc.abc" + module_install_dir = "framework" + subsystem_name = "ability" + part_name = "ability_runtime" + deps = [ ":ability_runtime_configuration_abc" ] +} + +generate_static_abc("ability_runtime_configuration_constant_abc") { + base_url = "./" + files = [ "./@ohos.app.ability.ConfigurationConstant.ets" ] + + is_boot_abc = "True" + device_dst_file = + "/system/framework/ability_runtime_configuration_constant_abc.abc" +} + +ohos_prebuilt_etc("ability_runtime_configuration_constant_abc_etc") { + source = "$target_out_dir/ability_runtime_configuration_constant_abc.abc" + module_install_dir = "framework" + subsystem_name = "ability" + part_name = "ability_runtime" + deps = [ ":ability_runtime_configuration_constant_abc" ] +} + +generate_static_abc("ability_runtime_abilityResult_abc") { + base_url = "./" + files = [ "./ability/abilityResult.ets" ] + + is_boot_abc = "True" + device_dst_file = "/system/framework/ability_runtime_abilityResult_abc.abc" +} + +ohos_prebuilt_etc("ability_runtime_abilityResult_abc_etc") { + source = "$target_out_dir/ability_runtime_abilityResult_abc.abc" + module_install_dir = "framework" + subsystem_name = "ability" + part_name = "ability_runtime" + deps = [ ":ability_runtime_abilityResult_abc" ] +} + group("ets_packages") { deps = [ + ":ability_runtime_abilityResult_abc_etc", ":ability_runtime_ability_constant_abc_etc", ":ability_runtime_ability_utils_abc_etc", ":ability_runtime_base_context_abc_etc", + ":ability_runtime_configuration_abc_etc", + ":ability_runtime_configuration_constant_abc_etc", ":ability_runtime_start_options_abc_etc", ":ability_runtime_want_abc_etc", ":ability_runtime_want_constant_abc_etc", diff --git a/frameworks/ets/ets/ability/abilityResult.ets b/frameworks/ets/ets/ability/abilityResult.ets new file mode 100644 index 0000000000000000000000000000000000000000..012a9d9cc6bf2babf5dae3c6c5de8caec397862d --- /dev/null +++ b/frameworks/ets/ets/ability/abilityResult.ets @@ -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. + */ + +import Want from '@ohos.app.ability.Want'; + +export interface AbilityResult { + resultCode: number; + want?: Want; +} + +class AbilityResultInner implements AbilityResult { + resultCode: number; + want?: Want | undefined = {}; +}