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 #include "sts_bundle_option.h"
16
17 #include "ans_log_wrapper.h"
18 #include "sts_common.h"
19
20 namespace OHOS {
21 namespace NotificationSts {
UnwrapBundleOption(ani_env * env,ani_object obj,Notification::NotificationBundleOption & option)22 bool UnwrapBundleOption(ani_env *env, ani_object obj, Notification::NotificationBundleOption& option)
23 {
24 ANS_LOGD("UnwrapBundleOption call");
25 if (env == nullptr || obj == nullptr) {
26 ANS_LOGE("UnwrapBundleOption failed, has nullptr");
27 return false;
28 }
29 std::string tempStr;
30 ani_boolean isUndefined = ANI_TRUE;
31 if (GetPropertyString(env, obj, "bundle", isUndefined, tempStr) !=ANI_OK || isUndefined == ANI_TRUE) {
32 ANS_LOGE("UnwrapBundleOption Get bundle failed");
33 return false;
34 }
35 std::string bundleName = GetResizeStr(tempStr, STR_MAX_SIZE);
36 option.SetBundleName(bundleName);
37 ani_double result = 0.0;
38 if (GetPropertyDouble(env, obj, "uid", isUndefined, result) == ANI_OK && isUndefined == ANI_FALSE) {
39 int32_t uid = static_cast<int32_t>(result);
40 option.SetUid(uid);
41 } else {
42 ANS_LOGD("UnwrapBundleOption get uid failed");
43 }
44 ANS_LOGD("UnwrapBundleOption end");
45 return true;
46 }
47
GetAniArrayBundleOption(ani_env * env,const std::vector<BundleOption> & bundleOptions)48 ani_object GetAniArrayBundleOption(ani_env* env,
49 const std::vector<BundleOption> &bundleOptions)
50 {
51 ANS_LOGD("GetAniArrayActionButton call");
52 if (env == nullptr) {
53 ANS_LOGE("GetAniArrayActionButton failed, has nullptr");
54 return nullptr;
55 }
56 ani_object arrayObj = newArrayClass(env, bundleOptions.size());
57 if (arrayObj == nullptr) {
58 ANS_LOGE("GetAniArrayActionButton: arrayObj is nullptr");
59 return nullptr;
60 }
61 int32_t index = 0;
62 for (auto &option : bundleOptions) {
63 std::shared_ptr<BundleOption> optSp = std::make_shared<BundleOption>(option);
64 ani_object item;
65 if (!WrapBundleOption(env, optSp, item) || item == nullptr) {
66 ANS_LOGE("GetAniArrayActionButton: item is nullptr");
67 return nullptr;
68 }
69 if (ANI_OK != env->Object_CallMethodByName_Void(arrayObj, "$_set", "ILstd/core/Object;:V", index, item)) {
70 ANS_LOGE("GetAniArrayActionButton: Object_CallMethodByName_Void failed");
71 return nullptr;
72 }
73 index ++;
74 }
75 ANS_LOGD("GetAniArrayActionButton end");
76 return arrayObj;
77 }
78
UnwrapArrayBundleOption(ani_env * env,ani_ref arrayObj,std::vector<Notification::NotificationBundleOption> & options)79 bool UnwrapArrayBundleOption(ani_env *env,
80 ani_ref arrayObj, std::vector<Notification::NotificationBundleOption>& options)
81 {
82 ANS_LOGD("UnwrapArrayBundleOption call");
83 if (env == nullptr || arrayObj == nullptr) {
84 ANS_LOGE("UnwrapArrayBundleOption failed, has nullptr");
85 return false;
86 }
87 ani_status status;
88 ani_double length;
89 status = env->Object_GetPropertyByName_Double(static_cast<ani_object>(arrayObj), "length", &length);
90 if (status != ANI_OK) {
91 ANS_LOGE("UnwrapArrayBundleOption: get length failed, status = %{public}d", status);
92 return false;
93 }
94 Notification::NotificationBundleOption option;
95 for (int32_t i = 0; i < static_cast<int>(length); i++) {
96 ani_ref optionRef;
97 status = env->Object_CallMethodByName_Ref(static_cast<ani_object>(arrayObj),
98 "$_get", "I:Lstd/core/Object;", &optionRef, i);
99 if (status != ANI_OK) {
100 ANS_LOGE("UnwrapArrayBundleOption: get bundleOptionRef failed, status = %{public}d", status);
101 return false;
102 }
103 if (!UnwrapBundleOption(env, static_cast<ani_object>(optionRef), option)) {
104 ANS_LOGE("UnwrapArrayBundleOption: get option status = %{public}d, index = %{public}d", status, i);
105 return false;
106 }
107 options.push_back(option);
108 }
109 ANS_LOGD("UnwrapArrayBundleOption end");
110 return true;
111 }
112
WrapBundleOption(ani_env * env,const std::shared_ptr<BundleOption> & bundleOption,ani_object & bundleObject)113 bool WrapBundleOption(ani_env* env,
114 const std::shared_ptr<BundleOption> &bundleOption, ani_object &bundleObject)
115 {
116 ANS_LOGD("WrapBundleOption call");
117 if (env == nullptr || bundleOption == nullptr) {
118 ANS_LOGE("WrapBundleOption failed, has nullptr");
119 return false;
120 }
121 ani_class bundleCls = nullptr;
122 if (!CreateClassObjByClassName(env,
123 "Lnotification/NotificationCommonDef/BundleOptionInner;", bundleCls, bundleObject)
124 || bundleCls == nullptr || bundleObject == nullptr) {
125 ANS_LOGE("WrapBundleOption: create BundleOption failed");
126 return false;
127 }
128 // bundle: string;
129 ani_string stringValue = nullptr;
130 if (ANI_OK != GetAniStringByString(env, bundleOption->GetBundleName(), stringValue)
131 || !CallSetter(env, bundleCls, bundleObject, "bundle", stringValue)) {
132 ANS_LOGE("WrapBundleOption: set bundle failed");
133 return false;
134 }
135 // uid?: number;
136 uint32_t uid = bundleOption->GetUid();
137 SetPropertyOptionalByDouble(env, bundleObject, "uid", uid);
138 ANS_LOGD("WrapBundleOption end");
139 return true;
140 }
141 }
142 }