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 25 namespace OHOS { 26 namespace ReminderAgentNapi { 27 using namespace OHOS::Notification; 28 29 namespace { 30 const char* ACTION_BUTTON = "actionButton"; 31 const char* ACTION_BUTTON_TITLE = "title"; 32 const char* ACTION_BUTTON_TYPE = "type"; 33 const char* ALARM_HOUR = "hour"; 34 const char* ALARM_DAYS_OF_WEEK = "daysOfWeek"; 35 const char* ALARM_MINUTE = "minute"; 36 const char* CALENDAR_DATE_TIME = "dateTime"; 37 const char* CALENDAR_YEAR = "year"; 38 const char* CALENDAR_MONTH = "month"; 39 const char* CALENDAR_DAY = "day"; 40 const char* CALENDAR_HOUR = "hour"; 41 const char* CALENDAR_MINUTE = "minute"; 42 const char* CALENDAR_SECOND = "second"; 43 const char* CALENDAR_REPEAT_MONTHS = "repeatMonths"; 44 const char* CALENDAR_REPEAT_DAYS = "repeatDays"; 45 const char* CONTENT = "content"; 46 const char* EXPIRED_CONTENT = "expiredContent"; 47 const char* MAX_SCREEN_WANT_AGENT = "maxScreenWantAgent"; 48 const char* MAX_SCREEN_WANT_AGENT_PKG = "pkgName"; 49 const char* MAX_SCREEN_WANT_AGENT_ABILITY = "abilityName"; 50 const char* NOTIFICATION_ID = "notificationId"; 51 const char* REMINDER_TYPE = "reminderType"; 52 const char* RING_DURATION = "ringDuration"; 53 const char* SLOT_TYPE = "slotType"; 54 const char* SNOOZE_CONTENT = "snoozeContent"; 55 const char* SNOOZE_TIMES = "snoozeTimes"; 56 const char* TIME_INTERVAL = "timeInterval"; 57 const char* TITLE = "title"; 58 const char* TIMER_COUNT_DOWN_TIME = "triggerTimeInSeconds"; 59 const char* WANT_AGENT = "wantAgent"; 60 const char* WANT_AGENT_PKG = "pkgName"; 61 const char* WANT_AGENT_ABILITY = "abilityName"; 62 const char* BUTTON_WANT_AGENT = "wantAgent"; 63 const char* BUTTON_WANT_AGENT_PKG = "pkgName"; 64 const char* BUTTON_WANT_AGENT_ABILITY = "abilityName"; 65 const char* BUTTON_WANT_AGENT_URI = "uri"; 66 const char* TAPDISMISSED = "tapDismissed"; 67 const char* AUTODELETEDTIME = "autoDeletedTime"; 68 } 69 70 struct CallbackPromiseInfo { 71 napi_ref callback = nullptr; 72 napi_deferred deferred = nullptr; 73 bool isCallback = false; 74 int32_t errorCode = 0; 75 }; 76 77 class ReminderCommon { 78 ReminderCommon(); 79 ~ReminderCommon(); 80 ReminderCommon(ReminderCommon &other) = delete; 81 ReminderCommon& operator = (const ReminderCommon &other) = delete; 82 83 public: 84 static napi_value GetReminderRequest( 85 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 86 87 static bool GetStringUtf8(const napi_env &env, const napi_value &value, 88 const char* propertyName, char* propertyVal, const int32_t size); 89 90 static bool GetBool(const napi_env &env, const napi_value &value, 91 const char* propertyName, bool& propertyVal); 92 93 static bool GetInt32(const napi_env &env, const napi_value &value, 94 const char* propertyName, int32_t& propertyVal, bool isNecessary); 95 96 static bool GetInt64(const napi_env &env, const napi_value &value, 97 const char* propertyName, int64_t& propertyVal); 98 99 static bool GetObject(const napi_env &env, const napi_value &value, 100 const char* propertyName, napi_value& propertyVal); 101 102 static void HandleErrCode(const napi_env &env, int32_t errCode); 103 104 static void ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info, 105 const napi_value &result, bool isThrow = false); 106 107 static void SetCallback(const napi_env &env, const napi_ref &callbackIn, const int32_t &errorCode, 108 const napi_value &result); 109 110 static napi_value SetPromise(const napi_env &env, const CallbackPromiseInfo &info, 111 const napi_value &result); 112 113 static napi_value JSParaError(const napi_env &env, const napi_ref &callback); 114 115 static void PaddingCallbackPromiseInfo(const napi_env &env, const napi_ref &callback, 116 CallbackPromiseInfo &info, napi_value &promise); 117 118 private: 119 static bool CheckCalendarParams(const int32_t &year, const int32_t &month, const int32_t &day, 120 const int32_t &hour, const int32_t &min); 121 122 static napi_value CreateReminderTimer( 123 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 124 125 static napi_value CreateReminderAlarm( 126 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 127 128 static napi_value CreateReminderCalendar( 129 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 130 131 static bool CreateReminder( 132 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 133 134 static bool GetPropertyValIfExist(const napi_env &env, const napi_value &value, 135 const char* propertyName, napi_value& propertyVal); 136 137 static void GenWantAgent( 138 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 139 140 static void GenMaxScreenWantAgent( 141 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 142 143 static bool GenActionButtons( 144 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder, bool isSysApp); 145 146 static napi_value GenReminder( 147 const napi_env &env, const napi_value &value, std::shared_ptr<ReminderRequest>& reminder); 148 149 static napi_value ParseInt32Array(const napi_env &env, const napi_value &value, 150 const char* propertyName, std::vector<uint8_t> &propertyVal, uint8_t maxLen); 151 152 static std::string FindErrMsg(const napi_env &env, const int32_t errCode); 153 154 static napi_value GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg); 155 156 static void GetButtonWantAgent(const napi_env &env, const napi_value &value, 157 std::shared_ptr<ReminderRequest>& reminder, std::shared_ptr<ReminderRequest::ButtonWantAgent>& wantAgent); 158 static bool IsSelfSystemApp(std::shared_ptr<ReminderRequest>& reminder); 159 }; 160 } // namespace OHOS 161 } // namespace ReminderAgentNapi 162 163 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_REMINDER_COMMON_H 164