1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <ani.h>
17 #include <array>
18 #include <iostream>
19 #include "ani_accessibility_config.h"
20 #include "hilog_wrapper.h"
21 #include <ani_signature_builder.h>
22
23 using namespace arkts::ani_signature;
24
25 constexpr int32_t INVALID_ANI_VERSION = 1;
26 constexpr int32_t MODULE_NOT_FOUND = 2;
27 constexpr int32_t CLASS_NOT_FOUND = 3;
28 constexpr int32_t BIND_METHOD_FAILED = 4;
29
30 static bool BindMethod(ani_env *env, ani_class cls);
31
ANI_Constructor(ani_vm * vm,uint32_t * result)32 ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result)
33 {
34 ani_env *env;
35 if (vm->GetEnv(ANI_VERSION_1, &env) != ANI_OK) {
36 HILOG_ERROR("Unsupported ANI_VERSION_1");
37 return (ani_status)INVALID_ANI_VERSION;
38 }
39
40 Type highContrastTextClass = Builder::BuildClass("@ohos.accessibility.config.config.HighContrastTextConfig");
41 ani_class cls;
42 if (env->FindClass(highContrastTextClass.Descriptor().c_str(), &cls) != ANI_OK) {
43 HILOG_ERROR("HighContrastTextConfig class not found");
44 return (ani_status)CLASS_NOT_FOUND;
45 }
46
47 if (!BindMethod(env, cls)) {
48 HILOG_ERROR("bind method failed");
49 return (ani_status)BIND_METHOD_FAILED;
50 }
51
52 *result = ANI_VERSION_1;
53 return ANI_OK;
54 }
55
BindMethod(ani_env * env,ani_class cls)56 static bool BindMethod(ani_env *env, ani_class cls)
57 {
58 std::array methods = {
59 ani_native_function {"setSync", nullptr, reinterpret_cast<void *>(
60 ANIAccessibilityConfig::SetHighContrastText)},
61 };
62
63 if (env->Class_BindNativeMethods(cls, methods.data(), methods.size()) != ANI_OK) {
64 return false;
65 };
66
67 methods = {
68 ani_native_function {"getSync", nullptr, reinterpret_cast<void *>(
69 ANIAccessibilityConfig::GetHighContrastText)},
70 };
71
72 if (env->Class_BindNativeMethods(cls, methods.data(), methods.size()) != ANI_OK) {
73 return false;
74 };
75
76 return true;
77 }
78
ANI_Destructor(ani_vm * vm)79 ANI_EXPORT ani_status ANI_Destructor(ani_vm *vm)
80 {
81 return ANI_OK;
82 }