• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (!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     if (notification->GetNotificationRequest().IsAgentNotification()) {
115         // Agent notification, replace creator with owner
116         // readonly creatorBundleName?: string
117         napi_create_string_utf8(
118             env, notification->GetNotificationRequest().GetOwnerBundleName().c_str(), NAPI_AUTO_LENGTH, &value);
119         napi_set_named_property(env, result, "creatorBundleName", value);
120 
121         // readonly creatorUid?: number
122         napi_create_int32(env, notification->GetNotificationRequest().GetOwnerUid(), &value);
123         napi_set_named_property(env, result, "creatorUid", value);
124 
125         // readonly creatorUserId?: number
126         napi_create_int32(env, notification->GetNotificationRequest().GetOwnerUserId(), &value);
127         napi_set_named_property(env, result, "creatorUserId", value);
128     } else {
129         // readonly creatorBundleName?: string
130         napi_create_string_utf8(env, notification->GetCreateBundle().c_str(), NAPI_AUTO_LENGTH, &value);
131         napi_set_named_property(env, result, "creatorBundleName", value);
132 
133         // readonly creatorUid?: number
134         napi_create_int32(env, notification->GetUid(), &value);
135         napi_set_named_property(env, result, "creatorUid", value);
136 
137         // readonly creatorUserId?: number
138         napi_create_int32(env, notification->GetUserId(), &value);
139         napi_set_named_property(env, result, "creatorUserId", value);
140     }
141 
142     // readonly creatorPid?: number
143     napi_create_int32(env, notification->GetPid(), &value);
144     napi_set_named_property(env, result, "creatorPid", value);
145 
146     // distributedOption?:DistributedOptions
147     napi_value distributedResult = nullptr;
148     napi_create_object(env, &distributedResult);
149     if (!SetNotificationByDistributedOptions(env, notification, distributedResult)) {
150         return NapiGetBoolean(env, false);
151     }
152     napi_set_named_property(env, result, "distributedOption", distributedResult);
153 
154     // readonly isRemoveAllowed?: boolean
155     napi_get_boolean(env, notification->IsRemoveAllowed(), &value);
156     napi_set_named_property(env, result, "isRemoveAllowed", value);
157 
158     // readonly source?: number
159     SourceType sourceType = SourceType::TYPE_NORMAL;
160     if (!SourceTypeCToJS(notification->GetSourceType(), sourceType)) {
161         return NapiGetBoolean(env, false);
162     }
163     napi_create_int32(env, static_cast<int32_t>(sourceType), &value);
164     napi_set_named_property(env, result, "source", value);
165 
166     // readonly deviceId?: string
167     napi_create_string_utf8(env, notification->GetDeviceId().c_str(), NAPI_AUTO_LENGTH, &value);
168     napi_set_named_property(env, result, "deviceId", value);
169 
170     return NapiGetBoolean(env, true);
171 }
172 
173 
GetNotificationRequestDistributedOptions(const napi_env & env,const napi_value & value,NotificationRequest & request)174 napi_value Common::GetNotificationRequestDistributedOptions(const napi_env &env,
175     const napi_value &value, NotificationRequest &request)
176 {
177     ANS_LOGD("enter");
178     napi_valuetype valuetype = napi_undefined;
179     napi_value result = nullptr;
180     bool hasProperty = false;
181 
182     // distributedOption?: DistributedOptions
183     NAPI_CALL(env, napi_has_named_property(env, value, "distributedOption", &hasProperty));
184     if (hasProperty) {
185         napi_get_named_property(env, value, "distributedOption", &result);
186         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
187         if (valuetype != napi_object) {
188             ANS_LOGE("Wrong argument type. Object expected.");
189             return nullptr;
190         }
191 
192         // isDistributed?: boolean
193         if (GetNotificationIsDistributed(env, result, request) == nullptr) {
194             return nullptr;
195         }
196 
197         // supportDisplayDevices?: Array<string>
198         if (GetNotificationSupportDisplayDevices(env, result, request) == nullptr) {
199             return nullptr;
200         }
201 
202         // supportOperateDevices?: Array<string>
203         if (GetNotificationSupportOperateDevices(env, result, request) == nullptr) {
204             return nullptr;
205         }
206     }
207 
208     return NapiGetNull(env);
209 }
210 
GetNotificationIsDistributed(const napi_env & env,const napi_value & value,NotificationRequest & request)211 napi_value Common::GetNotificationIsDistributed(
212     const napi_env &env, const napi_value &value, NotificationRequest &request)
213 {
214     ANS_LOGD("enter");
215 
216     napi_valuetype valuetype = napi_undefined;
217     napi_value result = nullptr;
218     bool hasProperty = false;
219     bool isDistributed = false;
220 
221     NAPI_CALL(env, napi_has_named_property(env, value, "isDistributed", &hasProperty));
222     if (hasProperty) {
223         napi_get_named_property(env, value, "isDistributed", &result);
224         NAPI_CALL(env, napi_typeof(env, result, &valuetype));
225         if (valuetype != napi_boolean) {
226             ANS_LOGE("Wrong argument type. Bool expected.");
227             return nullptr;
228         }
229         napi_get_value_bool(env, result, &isDistributed);
230         request.SetDistributed(isDistributed);
231     }
232 
233     return NapiGetNull(env);
234 }
235 }
236 }
237