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* EXPIRED_CONTENT = "expiredContent"; 50 const char* MAX_SCREEN_WANT_AGENT = "maxScreenWantAgent"; 51 const char* MAX_SCREEN_WANT_AGENT_PKG = "pkgName"; 52 const char* MAX_SCREEN_WANT_AGENT_ABILITY = "abilityName"; 53 const char* NOTIFICATION_ID = "notificationId"; 54 const char* REMINDER_TYPE = "reminderType"; 55 const char* RING_DURATION = "ringDuration"; 56 const char* SLOT_TYPE = "slotType"; 57 const char* SNOOZE_CONTENT = "snoozeContent"; 58 const char* SNOOZE_TIMES = "snoozeTimes"; 59 const char* TIME_INTERVAL = "timeInterval"; 60 const char* TITLE = "title"; 61 const char* TIMER_COUNT_DOWN_TIME = "triggerTimeInSeconds"; 62 const char* WANT_AGENT = "wantAgent"; 63 const char* RRULL_WANT_AGENT = "rruleWantAgent"; 64 const char* WANT_AGENT_PKG = "pkgName"; 65 const char* WANT_AGENT_ABILITY = "abilityName"; 66 const char* WANT_AGENT_URI = "uri"; 67 const char* WANT_AGENT_PARAMETERS = "parameters"; 68 const char* BUTTON_WANT_AGENT = "wantAgent"; 69 const char* BUTTON_WANT_AGENT_PKG = "pkgName"; 70 const char* BUTTON_WANT_AGENT_ABILITY = "abilityName"; 71 const char* BUTTON_WANT_AGENT_URI = "uri"; 72 const char* BUTTON_DATA_SHARE_UPDATE = "dataShareUpdate"; 73 const char* BUTTON_DATA_SHARE_UPDATE_URI = "uri"; 74 const char* BUTTON_DATA_SHARE_UPDATE_EQUALTO = "equalTo"; 75 const char* BUTTON_DATA_SHARE_UPDATE_VALUE = "value"; 76 const char* TAPDISMISSED = "tapDismissed"; 77 const char* AUTODELETEDTIME = "autoDeletedTime"; 78 const char* GROUP_ID = "groupId"; 79 const char* CUSTOM_RING_URI = "customRingUri"; 80 const char* SNOOZE_SLOT_TYPE = "snoozeSlotType"; 81 const char* REMINDER_INFO_REMINDER_REQ = "reminderReq"; 82 const char* REMINDER_INFO_REMINDER_ID = "reminderId"; 83 const int INDEX_KEY = 0; 84 const int INDEX_TYPE = 1; 85 const int INDEX_VALUE = 2; 86 } 87 88 struct CallbackPromiseInfo { 89 napi_ref callback = nullptr; 90 napi_deferred deferred = nullptr; 91 bool isCallback = false; 92 int32_t errorCode = 0; 93 }; 94 95 class ReminderCommon { 96 ReminderCommon(); 97 ~ReminderCommon(); 98 ReminderCommon(ReminderCommon &other) = delete; 99 ReminderCommon& operator = (const ReminderCommon &other) = delete; 100 101 public: 102 static napi_value GetReminderRequest( 103 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 104 105 static bool GetStringUtf8(const napi_env &env, const napi_value &value, 106 const char* propertyName, char* propertyVal, const int32_t size); 107 108 static bool GetBool(const napi_env &env, const napi_value &value, 109 const char* propertyName, bool& propertyVal); 110 111 static bool GetInt32(const napi_env &env, const napi_value &value, 112 const char* propertyName, int32_t& propertyVal, bool isNecessary); 113 114 static bool GetInt64(const napi_env &env, const napi_value &value, 115 const char* propertyName, int64_t& propertyVal); 116 117 static bool GetObject(const napi_env &env, const napi_value &value, 118 const char* propertyName, napi_value& propertyVal); 119 120 static bool GetDate(const napi_env& env, const napi_value& value, 121 const char* propertyName, double& date); 122 123 static void HandleErrCode(const napi_env &env, int32_t errCode); 124 125 static void ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info, 126 const napi_value &result, bool isThrow = false); 127 128 static void SetCallback(const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode, 129 const napi_value &result); 130 131 static napi_value SetPromise(const napi_env &env, const CallbackPromiseInfo &info, 132 const napi_value &result); 133 134 static napi_value JSParaError(const napi_env &env, const napi_ref &callback); 135 136 static void PaddingCallbackPromiseInfo(const napi_env &env, const napi_ref &callback, 137 CallbackPromiseInfo &info, napi_value &promise); 138 139 private: 140 static bool CheckCalendarParams(const int32_t &year, const int32_t &month, const int32_t &day, 141 const int32_t &hour, const int32_t &min); 142 143 static bool ParseCalendarParams(const napi_env& env, const napi_value& value, std::vector<uint8_t>& repeatMonths, 144 std::vector<uint8_t>& repeatDays, std::vector<uint8_t> &daysOfWeek); 145 146 static bool ParseLocalDateTime(const napi_env& env, const napi_value& dateTimeObj, struct tm& dateTime); 147 148 static napi_value CreateReminderTimer( 149 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 150 151 static napi_value CreateReminderAlarm( 152 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 153 154 static napi_value CreateReminderCalendar( 155 const napi_env &env, const napi_value &value, const bool isSysApp, std::shared_ptr<ReminderRequest>& reminder); 156 157 static bool CreateReminder( 158 const napi_env &env, const napi_value &value, const bool isSysApp, std::shared_ptr<ReminderRequest>& reminder); 159 160 static bool GetPropertyValIfExist(const napi_env &env, const napi_value &value, 161 const char* propertyName, napi_value& propertyVal); 162 163 static bool GenWantAgent(const napi_env &env, const napi_value &value, const char* name, 164 std::shared_ptr<ReminderRequest::WantAgentInfo>& wantAgentInfo); 165 166 static void GenMaxScreenWantAgent( 167 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 168 169 static bool GenActionButtons( 170 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder, bool isSysApp); 171 172 static napi_value GenReminder( 173 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 174 175 static void HandleActionButtonTitle(const napi_env &env, const napi_value &actionButton, 176 std::shared_ptr<ReminderRequest>& reminder, const char* str, int32_t buttonType); 177 178 static void GenReminderStringInner( 179 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 180 181 static bool GenReminderIntInner( 182 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 183 184 static void GenReminderBoolInner( 185 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 186 187 static napi_value ParseInt32Array(const napi_env &env, const napi_value &value, 188 const char* propertyName, std::vector<uint8_t> &propertyVal, uint8_t maxLen); 189 190 static std::string FindErrMsg(const napi_env &env, const int32_t errCode); 191 192 static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg); 193 194 static void GetButtonWantAgent(const napi_env &env, const napi_value &value, 195 std::shared_ptr<ReminderRequest>& reminder, std::shared_ptr<ReminderRequest::ButtonWantAgent>& wantAgent); 196 197 static void GetButtonDataShareUpdate(const napi_env &env, const napi_value &value, 198 std::shared_ptr<ReminderRequest::ButtonDataShareUpdate>& buttonDataShareUpdate); 199 200 static bool GetValueBucketObject(std::string &ValueBucketString, const napi_env &env, const napi_value &arg); 201 202 static std::string GetStringFromJS(const napi_env &env, const napi_value ¶m, 203 const std::string &defaultValue = ""); 204 205 static std::string Convert2Value(const napi_env &env, const napi_value &value, bool &status, std::string &type); 206 207 static std::vector<uint8_t> Convert2U8Vector(const napi_env &env, const napi_value &input_array); 208 209 static bool ValidateString(const std::string &str); 210 211 static bool IsSelfSystemApp(); 212 }; 213 } // namespace OHOS 214 } // namespace ReminderAgentNapi 215 216 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_REMINDER_COMMON_H 217