• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_state_data.h"
17 
18 #include "hilog_tag_wrapper.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
WrapAbilityStateDataInner(ani_env * env,ani_class cls,ani_object object,const AbilityStateData & data)22 ani_object WrapAbilityStateDataInner(ani_env *env, ani_class cls, ani_object object,
23     const AbilityStateData &data)
24 {
25     if (env == nullptr || cls == nullptr || object == nullptr) {
26         TAG_LOGE(AAFwkTag::ABILITYMGR, "invalid args");
27         return nullptr;
28     }
29 
30     if (!SetFieldStringByName(env, cls, object, "moduleName", data.moduleName)) {
31         TAG_LOGE(AAFwkTag::ABILITYMGR, "set moduleName failed");
32         return nullptr;
33     }
34 
35     if (!SetFieldStringByName(env, cls, object, "bundleName", data.bundleName)) {
36         TAG_LOGE(AAFwkTag::ABILITYMGR, "set bundleName failed");
37         return nullptr;
38     }
39 
40     if (!SetFieldStringByName(env, cls, object, "abilityName", data.abilityName)) {
41         TAG_LOGE(AAFwkTag::ABILITYMGR, "set abilityName failed");
42         return nullptr;
43     }
44 
45     if (!SetFieldIntByName(env, cls, object, "pid", data.pid)) {
46         TAG_LOGE(AAFwkTag::ABILITYMGR, "set pid failed");
47         return nullptr;
48     }
49 
50     if (!SetFieldIntByName(env, cls, object, "uid", data.uid)) {
51         TAG_LOGE(AAFwkTag::ABILITYMGR, "set uid failed");
52         return nullptr;
53     }
54 
55     if (!SetFieldIntByName(env, cls, object, "state", data.abilityState)) {
56         TAG_LOGE(AAFwkTag::ABILITYMGR, "set state failed");
57         return nullptr;
58     }
59 
60     if (!SetFieldIntByName(env, cls, object, "abilityType", data.abilityType)) {
61         TAG_LOGE(AAFwkTag::ABILITYMGR, "set type failed");
62         return nullptr;
63     }
64 
65     if (!SetFieldBoolByName(env, cls, object, "isAtomicService", data.isAtomicService)) {
66         TAG_LOGE(AAFwkTag::ABILITYMGR, "set isAtomicService failed");
67         return nullptr;
68     }
69 
70     if (data.appCloneIndex != -1 && !SetOptionalFieldInt(env, cls, object, "appCloneIndex", data.appCloneIndex)) {
71         TAG_LOGE(AAFwkTag::ABILITYMGR, "set appCloneIndex failed");
72         return nullptr;
73     }
74 
75     return object;
76 }
77 
WrapAbilityStateData(ani_env * env,const AbilityStateData & data)78 ani_object WrapAbilityStateData(ani_env *env, const AbilityStateData &data)
79 {
80     if (env == nullptr) {
81         TAG_LOGE(AAFwkTag::ABILITYMGR, "null env");
82         return nullptr;
83     }
84 
85     ani_class cls = nullptr;
86     ani_status status = ANI_ERROR;
87     ani_method ctor = nullptr;
88     ani_object object = nullptr;
89     static const char *className = "Lapplication/AbilityStateData/AbilityStateData;";
90 
91     if ((status = env->FindClass(className, &cls)) != ANI_OK || cls == nullptr) {
92         TAG_LOGE(AAFwkTag::ABILITYMGR, "FindClass status : %{public}d or null cls", status);
93         return nullptr;
94     }
95 
96     if ((status = env->Class_FindMethod(cls, "<ctor>", ":V", &ctor)) != ANI_OK || ctor == nullptr) {
97         TAG_LOGE(AAFwkTag::ABILITYMGR, "Class_FindMethod status : %{public}d or null ctor", status);
98         return nullptr;
99     }
100 
101     if ((status = env->Object_New(cls, ctor, &object)) != ANI_OK || object == nullptr) {
102         TAG_LOGE(AAFwkTag::ABILITYMGR, "Object_New status : %{public}d or null object", status);
103         return nullptr;
104     }
105 
106     return WrapAbilityStateDataInner(env, cls, object, data);
107 }
108 
CreateAniAbilityStateDataArray(ani_env * env,const std::vector<AbilityStateData> & list)109 ani_object CreateAniAbilityStateDataArray(ani_env *env, const std::vector<AbilityStateData> &list)
110 {
111     TAG_LOGD(AAFwkTag::ABILITYMGR, "call CreateAniAbilityStateDataArray, list.size=%{public}zu", list.size());
112 
113     if (env == nullptr) {
114         TAG_LOGE(AAFwkTag::ABILITYMGR, "null env");
115         return nullptr;
116     }
117 
118     ani_class cls = nullptr;
119     ani_status status = ANI_ERROR;
120     ani_method ctor = nullptr;
121     ani_object object = nullptr;
122     static const char *className = "Lescompat/Array;";
123 
124     if ((status = env->FindClass(className, &cls)) != ANI_OK || cls == nullptr) {
125         TAG_LOGE(AAFwkTag::ABILITYMGR, "FindClass status : %{public}d or null cls", status);
126         return nullptr;
127     }
128 
129     if ((status = env->Class_FindMethod(cls, "<ctor>", "I:V", &ctor)) != ANI_OK || ctor == nullptr) {
130         TAG_LOGE(AAFwkTag::ABILITYMGR, "Class_FindMethod status : %{public}d or null ctor", status);
131         return nullptr;
132     }
133 
134     if ((status = env->Object_New(cls, ctor, &object, list.size())) != ANI_OK || object == nullptr) {
135         TAG_LOGE(AAFwkTag::ABILITYMGR, "Object_New status : %{public}d or null object", status);
136         return nullptr;
137     }
138 
139     ani_size index = 0;
140     for (const auto &data : list) {
141         ani_object obj = WrapAbilityStateData(env, data);
142         if (obj == nullptr) {
143             TAG_LOGE(AAFwkTag::ABILITYMGR, "null obj");
144             return nullptr;
145         }
146         if (ANI_OK != env->Object_CallMethodByName_Void(object, "$_set", "ILstd/core/Object;:V", index, obj)) {
147             TAG_LOGE(AAFwkTag::ABILITYMGR, "Object_CallMethodByName_Void failed");
148             return nullptr;
149         }
150         index++;
151     }
152     return object;
153 }
154 } // namespace AppExecFwk
155 } // namespace OHOS