• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_REMINDER_COMMON_H
16 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_REMINDER_COMMON_H
17 
18 #include "ans_inner_errors.h"
19 #include "ans_log_wrapper.h"
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22 #include "reminder_helper.h"
23 #include "reminder_request.h"
24 #include "reminder_request_calendar.h"
25 
26 namespace OHOS {
27 namespace ReminderAgentNapi {
28 using namespace OHOS::Notification;
29 
30 namespace {
31 const char* ACTION_BUTTON = "actionButton";
32 const char* ACTION_BUTTON_TITLE = "title";
33 const char* ACTION_BUTTON_TYPE = "type";
34 const char* ACTION_BUTTON_RESOURCE = "titleResource";
35 const char* ALARM_HOUR = "hour";
36 const char* REPEAT_DAYS_OF_WEEK = "daysOfWeek";
37 const char* ALARM_MINUTE = "minute";
38 const char* CALENDAR_END_DATE_TIME = "endDateTime";
39 const char* CALENDAR_DATE_TIME = "dateTime";
40 const char* CALENDAR_YEAR = "year";
41 const char* CALENDAR_MONTH = "month";
42 const char* CALENDAR_DAY = "day";
43 const char* CALENDAR_HOUR = "hour";
44 const char* CALENDAR_MINUTE = "minute";
45 const char* CALENDAR_SECOND = "second";
46 const char* CALENDAR_REPEAT_MONTHS = "repeatMonths";
47 const char* CALENDAR_REPEAT_DAYS = "repeatDays";
48 const char* CONTENT = "content";
49 const char* CONTENT_RESOURCE_ID = "contentResourceId";
50 const char* EXPIRED_CONTENT = "expiredContent";
51 const char* EXPIRED_CONTENT_RESOURCE_ID = "expiredContentResourceId";
52 const char* MAX_SCREEN_WANT_AGENT = "maxScreenWantAgent";
53 const char* MAX_SCREEN_WANT_AGENT_PKG = "pkgName";
54 const char* MAX_SCREEN_WANT_AGENT_ABILITY = "abilityName";
55 const char* NOTIFICATION_ID = "notificationId";
56 const char* REMINDER_TYPE = "reminderType";
57 const char* RING_DURATION = "ringDuration";
58 const char* SLOT_TYPE = "slotType";
59 const char* SNOOZE_CONTENT = "snoozeContent";
60 const char* SNOOZE_CONTENT_RESOURCE_ID = "snoozeContentResourceId";
61 const char* SNOOZE_TIMES = "snoozeTimes";
62 const char* TIME_INTERVAL = "timeInterval";
63 const char* TITLE = "title";
64 const char* TITLE_RESOURCE_ID = "titleResourceId";
65 const char* TIMER_COUNT_DOWN_TIME = "triggerTimeInSeconds";
66 const char* WANT_AGENT = "wantAgent";
67 const char* RRULL_WANT_AGENT = "rruleWantAgent";
68 const char* WANT_AGENT_PKG = "pkgName";
69 const char* WANT_AGENT_ABILITY = "abilityName";
70 const char* WANT_AGENT_URI = "uri";
71 const char* WANT_AGENT_PARAMETERS = "parameters";
72 const char* BUTTON_WANT_AGENT = "wantAgent";
73 const char* BUTTON_WANT_AGENT_PKG = "pkgName";
74 const char* BUTTON_WANT_AGENT_ABILITY = "abilityName";
75 const char* BUTTON_WANT_AGENT_URI = "uri";
76 const char* BUTTON_DATA_SHARE_UPDATE = "dataShareUpdate";
77 const char* BUTTON_DATA_SHARE_UPDATE_URI = "uri";
78 const char* BUTTON_DATA_SHARE_UPDATE_EQUALTO = "equalTo";
79 const char* BUTTON_DATA_SHARE_UPDATE_VALUE = "value";
80 const char* TAPDISMISSED = "tapDismissed";
81 const char* AUTODELETEDTIME = "autoDeletedTime";
82 const char* GROUP_ID = "groupId";
83 const char* CUSTOM_RING_URI = "customRingUri";
84 const char* RING_CHANNEL = "ringChannel";
85 const char* SNOOZE_SLOT_TYPE = "snoozeSlotType";
86 const char* REMINDER_INFO_REMINDER_REQ = "reminderReq";
87 const char* REMINDER_INFO_REMINDER_ID = "reminderId";
88 const int INDEX_KEY = 0;
89 const int INDEX_TYPE = 1;
90 const int INDEX_VALUE = 2;
91 }
92 
93 struct CallbackPromiseInfo {
94     napi_ref callback = nullptr;
95     napi_deferred deferred = nullptr;
96     bool isCallback = false;
97     int32_t errorCode = 0;
98 };
99 
100 class ReminderCommon {
101     ReminderCommon();
102     ~ReminderCommon();
103     ReminderCommon(ReminderCommon &other) = delete;
104     ReminderCommon& operator = (const ReminderCommon &other) = delete;
105 
106 public:
107     static napi_value GetReminderRequest(
108         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
109 
110     static bool GetStringUtf8(const napi_env &env, const napi_value &value,
111         const char* propertyName, char* propertyVal, const int32_t size);
112 
113     static bool GetBool(const napi_env &env, const napi_value &value,
114         const char* propertyName, bool& propertyVal);
115 
116     static bool GetInt32(const napi_env &env, const napi_value &value,
117         const char* propertyName, int32_t& propertyVal, bool isNecessary);
118 
119     static bool GetInt64(const napi_env &env, const napi_value &value,
120         const char* propertyName, int64_t& propertyVal);
121 
122     static bool GetObject(const napi_env &env, const napi_value &value,
123         const char* propertyName, napi_value& propertyVal);
124 
125     static bool GetDate(const napi_env& env, const napi_value& value,
126         const char* propertyName, double& date);
127 
128     static void HandleErrCode(const napi_env &env, int32_t errCode);
129 
130     static void ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info,
131         const napi_value &result, bool isThrow = false);
132 
133     static void SetCallback(const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode,
134         const napi_value &result);
135 
136     static napi_value  SetPromise(const napi_env &env, const CallbackPromiseInfo &info,
137         const napi_value &result);
138 
139     static napi_value JSParaError(const napi_env &env, const napi_ref &callback);
140 
141     static void PaddingCallbackPromiseInfo(const napi_env &env, const napi_ref &callback,
142         CallbackPromiseInfo &info, napi_value &promise);
143 
144 private:
145     static bool CheckCalendarParams(const int32_t &year, const int32_t &month, const int32_t &day,
146         const int32_t &hour, const int32_t &min);
147 
148     static bool ParseCalendarParams(const napi_env& env, const napi_value& value, std::vector<uint8_t>& repeatMonths,
149         std::vector<uint8_t>& repeatDays, std::vector<uint8_t> &daysOfWeek);
150 
151     static bool ParseLocalDateTime(const napi_env& env, const napi_value& dateTimeObj, struct tm& dateTime);
152 
153     static napi_value CreateReminderTimer(
154         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
155 
156     static napi_value CreateReminderAlarm(
157         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
158 
159     static napi_value CreateReminderCalendar(
160         const napi_env &env, const napi_value &value, const bool isSysApp, std::shared_ptr<ReminderRequest>& reminder);
161 
162     static bool CreateReminder(
163         const napi_env &env, const napi_value &value,  const bool isSysApp, std::shared_ptr<ReminderRequest>& reminder);
164 
165     static bool GetPropertyValIfExist(const napi_env &env, const napi_value &value,
166         const char* propertyName, napi_value& propertyVal);
167 
168     static bool GenWantAgent(const napi_env &env, const napi_value &value, const char* name,
169         std::shared_ptr<ReminderRequest::WantAgentInfo>& wantAgentInfo);
170 
171     static void GenMaxScreenWantAgent(
172         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
173 
174     static bool GenActionButtons(
175         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder, bool isSysApp);
176 
177     static bool GenRingChannel(const napi_env& env, const napi_value& value,
178         std::shared_ptr<ReminderRequest>& reminder);
179 
180     static napi_value GenReminder(
181         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
182 
183     static void HandleActionButtonTitle(const napi_env &env, const napi_value &actionButton,
184         std::shared_ptr<ReminderRequest>& reminder, const char* str, int32_t buttonType);
185 
186     static void GenReminderStringInner(
187         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
188 
189     static bool GenReminderIntInner(
190         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
191 
192     static bool GenReminderIntInnerOther(
193         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
194 
195     static void GenReminderBoolInner(
196         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
197 
198     static napi_value ParseInt32Array(const napi_env &env, const napi_value &value,
199         const char* propertyName, std::vector<uint8_t> &propertyVal, uint8_t maxLen);
200 
201     static std::string FindErrMsg(const napi_env &env, const int32_t errCode);
202 
203     static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg);
204 
205     static void GetButtonWantAgent(const napi_env &env, const napi_value &value,
206         std::shared_ptr<ReminderRequest>& reminder, std::shared_ptr<ReminderRequest::ButtonWantAgent>& wantAgent);
207 
208     static void GetButtonDataShareUpdate(const napi_env &env, const napi_value &value,
209         std::shared_ptr<ReminderRequest::ButtonDataShareUpdate>& buttonDataShareUpdate);
210 
211     static bool GetValueBucketObject(std::string &ValueBucketString, const napi_env &env, const napi_value &arg);
212 
213     static std::string GetStringFromJS(const napi_env &env, const napi_value &param,
214         const std::string &defaultValue = "");
215 
216     static std::string Convert2Value(const napi_env &env, const napi_value &value, bool &status, std::string &type);
217 
218     static std::vector<uint8_t> Convert2U8Vector(const napi_env &env, const napi_value &input_array);
219 
220     static bool ValidateString(const std::string &str);
221 
222     static bool IsSelfSystemApp();
223 };
224 }  // namespace OHOS
225 }  // namespace ReminderAgentNapi
226 
227 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_REMINDER_COMMON_H
228