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_common_ability_result.h"
17
18 #include "ani_common_want.h"
19 #include "hilog_tag_wrapper.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 constexpr const char *ABILITY_RESULT_CLASS_NAME = "Lability/abilityResult/AbilityResultInner;";
25 } // namespace
26
WrapAbilityResult(ani_env * env,int32_t resultCode,const AAFwk::Want & want)27 ani_object WrapAbilityResult(ani_env *env, int32_t resultCode, const AAFwk::Want &want)
28 {
29 if (env == nullptr) {
30 TAG_LOGE(AAFwkTag::ANI, "null env");
31 return nullptr;
32 }
33 ani_class cls = nullptr;
34 ani_status status = env->FindClass(ABILITY_RESULT_CLASS_NAME, &cls);
35 if (status != ANI_OK) {
36 TAG_LOGE(AAFwkTag::ANI, "FindClass status: %{public}d", status);
37 return nullptr;
38 }
39
40 ani_method ctor = nullptr;
41 if ((status = env->Class_FindMethod(cls, "<ctor>", nullptr, &ctor)) != ANI_OK) {
42 TAG_LOGE(AAFwkTag::ANI, "Class_FindMethod status: %{public}d", status);
43 return nullptr;
44 }
45
46 ani_object resultObj = nullptr;
47 if ((status = env->Object_New(cls, ctor, &resultObj)) != ANI_OK) {
48 TAG_LOGE(AAFwkTag::ANI, "Object_New status: %{public}d", status);
49 return nullptr;
50 }
51
52 ani_method resultCodeSetter = nullptr;
53 if ((status = env->Class_FindMethod(cls, "<set>resultCode", nullptr, &resultCodeSetter)) != ANI_OK) {
54 TAG_LOGE(AAFwkTag::ANI, "Class_FindMethod status: %{public}d", status);
55 return nullptr;
56 }
57
58 ani_double dResultCode {resultCode};
59 if ((status = env->Object_CallMethod_Void(resultObj, resultCodeSetter, dResultCode)) != ANI_OK) {
60 TAG_LOGE(AAFwkTag::ANI, "Object_CallMethod_Void status: %{public}d", status);
61 return nullptr;
62 }
63
64 ani_method wantSetter = nullptr;
65 if ((status = env->Class_FindMethod(cls, "<set>want", nullptr, &wantSetter)) != ANI_OK) {
66 TAG_LOGE(AAFwkTag::ANI, "Class_FindMethod status: %{public}d", status);
67 return nullptr;
68 }
69
70 ani_object wantObj = AppExecFwk::WrapWant(env, want);
71 if ((status = env->Object_CallMethod_Void(resultObj, wantSetter, wantObj)) != ANI_OK) {
72 TAG_LOGE(AAFwkTag::ANI, "Object_CallMethod_Void status: %{public}d", status);
73 return nullptr;
74 }
75
76 return resultObj;
77 }
78 } // namespace AppExecFwk
79 } // namespace OHOS
80