1 /*
2  * Copyright (c) 2023-2023 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 "common.h"
17 #include "ans_inner_errors.h"
18 #include "ans_log_wrapper.h"
19 #include "js_native_api.h"
20 #include "js_native_api_types.h"
21 #include "napi_common.h"
22 #include "napi_common_util.h"
23 #include "notification_action_button.h"
24 #include "notification_capsule.h"
25 #include "notification_constant.h"
26 #include "notification_local_live_view_content.h"
27 #include "notification_progress.h"
28 #include "notification_time.h"
29 #include "pixel_map_napi.h"
30 
31 namespace OHOS {
32 namespace NotificationNapi {
SetNotificationByDistributedOptions(const napi_env & env,const OHOS::Notification::Notification * notification,napi_value & result)33 napi_value Common::SetNotificationByDistributedOptions(
34     const napi_env &env, const OHOS::Notification::Notification *notification, napi_value &result)
35 {
36     ANS_LOGD("enter");
37     if (notification == nullptr) {
38         ANS_LOGE("notification is nullptr");
39         return NapiGetBoolean(env, false);
40     }
41 
42     NotificationDistributedOptions options = notification->GetNotificationRequest().GetNotificationDistributedOptions();
43     napi_value value = nullptr;
44     // isDistributed?: boolean
45     if (notification->GetDeviceId().empty()) {
46         napi_get_boolean(env, false, &value);
47     } else {
48         napi_get_boolean(env, options.IsDistributed(), &value);
49     }
50     napi_set_named_property(env, result, "isDistributed", value);
51 
52     // supportDisplayDevices?: Array<string>
53     uint32_t count = 0;
54     napi_value arrSupportDisplayDevices = nullptr;
55     napi_create_array(env, &arrSupportDisplayDevices);
56     std::vector<std::string> displayDevices = options.GetDevicesSupportDisplay();
57     for (auto vec : displayDevices) {
58         napi_value vecValue = nullptr;
59         ANS_LOGI("supportDisplayDevices = %{public}s", vec.c_str());
60         napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &vecValue);
61         napi_set_element(env, arrSupportDisplayDevices, count, vecValue);
62         count++;
63     }
64     napi_set_named_property(env, result, "supportDisplayDevices", arrSupportDisplayDevices);
65 
66     // supportOperateDevices?: Array<string>
67     count = 0;
68     napi_value arrSupportOperateDevices = nullptr;
69     napi_create_array(env, &arrSupportOperateDevices);
70     std::vector<std::string> operateDevices = options.GetDevicesSupportOperate();
71     for (auto vec : operateDevices) {
72         napi_value vecValue = nullptr;
73         ANS_LOGI("supportOperateDevices  = %{public}s", vec.c_str());
74         napi_create_string_utf8(env, vec.c_str(), NAPI_AUTO_LENGTH, &vecValue);
75         napi_set_element(env, arrSupportOperateDevices, count, vecValue);
76         count++;
77     }
78     napi_set_named_property(env, result, "supportOperateDevices", arrSupportOperateDevices);
79 
80     // readonly remindType?: number
81     enum DeviceRemindType outType = DeviceRemindType::IDLE_DONOT_REMIND;
82     if (!AnsEnumUtil::DeviceRemindTypeCToJS(notification->GetRemindType(), outType)) {
83         return NapiGetBoolean(env, false);
84     }
85     napi_create_int32(env, static_cast<int32_t>(outType), &value);
86     napi_set_named_property(env, result, "remindType", value);
87 
88     return NapiGetBoolean(env, true);
89 }
90 
SetNotification(const napi_env & env,const OHOS::Notification::Notification * notification,napi_value & result)91 napi_value Common::SetNotification(
92     const napi_env &env, const OHOS::Notification::Notification *notification, napi_value &result)
93 {
94     ANS_LOGD("enter");
95 
96     if (notification == nullptr) {
97         ANS_LOGE("notification is nullptr");
98         return NapiGetBoolean(env, false);
99     }
100     napi_value value = nullptr;
101     NotificationRequest request = notification->GetNotificationRequest();
102     if (!SetNotificationRequest(env, &request, result)) {
103         return NapiGetBoolean(env, false);
104     }
105 
106     // hashCode?: string
107     napi_create_string_utf8(env, notification->GetKey().c_str(), NAPI_AUTO_LENGTH, &value);
108     napi_set_named_property(env, result, "hashCode", value);
109 
110     // isFloatingIcon ?: boolean
111     napi_get_boolean(env, notification->IsFloatingIcon(), &value);
112     napi_set_named_property(env, result, "isFloatingIcon", value);
113 
114     // readonly creatorBundleName?: string
115     napi_create_string_utf8(
116         env, notification->GetBundleName().c_str(), NAPI_AUTO_LENGTH, &value);
117     napi_set_named_property(env, result, "creatorBundleName", value);
118 
119     // readonly creatorUid?: number
120     napi_create_int32(env, notification->GetNotificationRequest().GetOwnerUid(), &value);
121     napi_set_named_property(env, result, "creatorUid", value);
122 
123     // readonly creatorUserId?: number
124     napi_create_int32(env, notification->GetRecvUserId(), &value);
125     napi_set_named_property(env, result, "creatorUserId", value);
126 
127     // readonly creatorInstanceKey?: number
128     napi_create_int32(env, notification->GetInstanceKey(), &value);
129     napi_set_named_property(env, result, "creatorInstanceKey", value);
130 
131     // readonly creatorPid?: number
132     napi_create_int32(env, notification->GetPid(), &value);
133     napi_set_named_property(env, result, "creatorPid", value);
134 
135     // distributedOption?:DistributedOptions
136     napi_value distributedResult = nullptr;
137     napi_create_object(env, &distributedResult);
138     if (!SetNotificationByDistributedOptions(env, notification, distributedResult)) {
139         return NapiGetBoolean(env, false);
140     }
141     napi_set_named_property(env, result, "distributedOption", distributedResult);
142 
143     // readonly isRemoveAllowed?: boolean
144     napi_get_boolean(env, notification->IsRemoveAllowed(), &value);
145     napi_set_named_property(env, result, "isRemoveAllowed", value);
146 
147     // readonly source?: number
148     SourceType sourceType = SourceType::TYPE_NORMAL;
149     if (!AnsEnumUtil::SourceTypeCToJS(notification->GetSourceType(), sourceType)) {
150         return NapiGetBoolean(env, false);
151     }
152     napi_create_int32(env, static_cast<int32_t>(sourceType), &value);
153     napi_set_named_property(env, result, "source", value);
154 
155     // readonly deviceId?: string
156     napi_create_string_utf8(env, notification->GetDeviceId().c_str(), NAPI_AUTO_LENGTH, &value);
157     napi_set_named_property(env, result, "deviceId", value);
158 
159     // notificationControlFlags?: number
160     napi_create_int32(env, notification->GetNotificationRequest().GetNotificationControlFlags(), &value);
161     napi_set_named_property(env, result, "notificationControlFlags", value);
162 
163     return NapiGetBoolean(env, true);
164 }
165 
166 
GetNotificationRequestDistributedOptions(const napi_env & env,const napi_value & value,NotificationRequest & request)167 napi_value Common::GetNotificationRequestDistributedOptions(const napi_env &env,
168     const napi_value &value, NotificationRequest &request)
169 {
170     ANS_LOGD("enter");
171     napi_valuetype valuetype = napi_undefined;
172     napi_value result = nullptr;
173     bool hasProperty = false;
174 
175     // distributedOption?: DistributedOptions
176     NAPI_CALL(env, napi_has_named_property(env, value, "distributedOption", &hasProperty));
177     if (hasProperty) {
178         napi_get_named_property(env, value, "distributedOption", &result);
179         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
180         if (valuetype != napi_object) {
181             ANS_LOGE("Wrong argument type. Object expected.");
182             return nullptr;
183         }
184 
185         // isDistributed?: boolean
186         if (GetNotificationIsDistributed(env, result, request) == nullptr) {
187             return nullptr;
188         }
189 
190         // supportDisplayDevices?: Array<string>
191         if (GetNotificationSupportDisplayDevices(env, result, request) == nullptr) {
192             return nullptr;
193         }
194 
195         // supportOperateDevices?: Array<string>
196         if (GetNotificationSupportOperateDevices(env, result, request) == nullptr) {
197             return nullptr;
198         }
199     }
200 
201     return NapiGetNull(env);
202 }
203 
GetNotificationIsDistributed(const napi_env & env,const napi_value & value,NotificationRequest & request)204 napi_value Common::GetNotificationIsDistributed(
205     const napi_env &env, const napi_value &value, NotificationRequest &request)
206 {
207     ANS_LOGD("enter");
208 
209     napi_valuetype valuetype = napi_undefined;
210     napi_value result = nullptr;
211     bool hasProperty = false;
212     bool isDistributed = false;
213 
214     NAPI_CALL(env, napi_has_named_property(env, value, "isDistributed", &hasProperty));
215     if (hasProperty) {
216         napi_get_named_property(env, value, "isDistributed", &result);
217         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
218         if (valuetype != napi_boolean) {
219             ANS_LOGE("Wrong argument type. Bool expected.");
220             return nullptr;
221         }
222         napi_get_value_bool(env, result, &isDistributed);
223         request.SetDistributed(isDistributed);
224     }
225 
226     return NapiGetNull(env);
227 }
228 }
229 }
230