• 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* SNOOZE_SLOT_TYPE = "snoozeSlotType";
85 const char* REMINDER_INFO_REMINDER_REQ = "reminderReq";
86 const char* REMINDER_INFO_REMINDER_ID = "reminderId";
87 const int INDEX_KEY = 0;
88 const int INDEX_TYPE = 1;
89 const int INDEX_VALUE = 2;
90 }
91 
92 struct CallbackPromiseInfo {
93     napi_ref callback = nullptr;
94     napi_deferred deferred = nullptr;
95     bool isCallback = false;
96     int32_t errorCode = 0;
97 };
98 
99 class ReminderCommon {
100     ReminderCommon();
101     ~ReminderCommon();
102     ReminderCommon(ReminderCommon &other) = delete;
103     ReminderCommon& operator = (const ReminderCommon &other) = delete;
104 
105 public:
106     static napi_value GetReminderRequest(
107         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
108 
109     static bool GetStringUtf8(const napi_env &env, const napi_value &value,
110         const char* propertyName, char* propertyVal, const int32_t size);
111 
112     static bool GetBool(const napi_env &env, const napi_value &value,
113         const char* propertyName, bool& propertyVal);
114 
115     static bool GetInt32(const napi_env &env, const napi_value &value,
116         const char* propertyName, int32_t& propertyVal, bool isNecessary);
117 
118     static bool GetInt64(const napi_env &env, const napi_value &value,
119         const char* propertyName, int64_t& propertyVal);
120 
121     static bool GetObject(const napi_env &env, const napi_value &value,
122         const char* propertyName, napi_value& propertyVal);
123 
124     static bool GetDate(const napi_env& env, const napi_value& value,
125         const char* propertyName, double& date);
126 
127     static void HandleErrCode(const napi_env &env, int32_t errCode);
128 
129     static void ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info,
130         const napi_value &result, bool isThrow = false);
131 
132     static void SetCallback(const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode,
133         const napi_value &result);
134 
135     static napi_value  SetPromise(const napi_env &env, const CallbackPromiseInfo &info,
136         const napi_value &result);
137 
138     static napi_value JSParaError(const napi_env &env, const napi_ref &callback);
139 
140     static void PaddingCallbackPromiseInfo(const napi_env &env, const napi_ref &callback,
141         CallbackPromiseInfo &info, napi_value &promise);
142 
143 private:
144     static bool CheckCalendarParams(const int32_t &year, const int32_t &month, const int32_t &day,
145         const int32_t &hour, const int32_t &min);
146 
147     static bool ParseCalendarParams(const napi_env& env, const napi_value& value, std::vector<uint8_t>& repeatMonths,
148         std::vector<uint8_t>& repeatDays, std::vector<uint8_t> &daysOfWeek);
149 
150     static bool ParseLocalDateTime(const napi_env& env, const napi_value& dateTimeObj, struct tm& dateTime);
151 
152     static napi_value CreateReminderTimer(
153         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
154 
155     static napi_value CreateReminderAlarm(
156         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
157 
158     static napi_value CreateReminderCalendar(
159         const napi_env &env, const napi_value &value, const bool isSysApp, std::shared_ptr<ReminderRequest>& reminder);
160 
161     static bool CreateReminder(
162         const napi_env &env, const napi_value &value,  const bool isSysApp, std::shared_ptr<ReminderRequest>& reminder);
163 
164     static bool GetPropertyValIfExist(const napi_env &env, const napi_value &value,
165         const char* propertyName, napi_value& propertyVal);
166 
167     static bool GenWantAgent(const napi_env &env, const napi_value &value, const char* name,
168         std::shared_ptr<ReminderRequest::WantAgentInfo>& wantAgentInfo);
169 
170     static void GenMaxScreenWantAgent(
171         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
172 
173     static bool GenActionButtons(
174         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder, bool isSysApp);
175 
176     static napi_value GenReminder(
177         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
178 
179     static void HandleActionButtonTitle(const napi_env &env, const napi_value &actionButton,
180         std::shared_ptr<ReminderRequest>& reminder, const char* str, int32_t buttonType);
181 
182     static void GenReminderStringInner(
183         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
184 
185     static bool GenReminderIntInner(
186         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
187 
188     static bool GenReminderIntInnerOther(
189         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
190 
191     static void GenReminderBoolInner(
192         const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder);
193 
194     static napi_value ParseInt32Array(const napi_env &env, const napi_value &value,
195         const char* propertyName, std::vector<uint8_t> &propertyVal, uint8_t maxLen);
196 
197     static std::string FindErrMsg(const napi_env &env, const int32_t errCode);
198 
199     static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg);
200 
201     static void GetButtonWantAgent(const napi_env &env, const napi_value &value,
202         std::shared_ptr<ReminderRequest>& reminder, std::shared_ptr<ReminderRequest::ButtonWantAgent>& wantAgent);
203 
204     static void GetButtonDataShareUpdate(const napi_env &env, const napi_value &value,
205         std::shared_ptr<ReminderRequest::ButtonDataShareUpdate>& buttonDataShareUpdate);
206 
207     static bool GetValueBucketObject(std::string &ValueBucketString, const napi_env &env, const napi_value &arg);
208 
209     static std::string GetStringFromJS(const napi_env &env, const napi_value &param,
210         const std::string &defaultValue = "");
211 
212     static std::string Convert2Value(const napi_env &env, const napi_value &value, bool &status, std::string &type);
213 
214     static std::vector<uint8_t> Convert2U8Vector(const napi_env &env, const napi_value &input_array);
215 
216     static bool ValidateString(const std::string &str);
217 
218     static bool IsSelfSystemApp();
219 };
220 }  // namespace OHOS
221 }  // namespace ReminderAgentNapi
222 
223 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_REMINDER_COMMON_H
224