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 16 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H 18 19 #include <functional> 20 #include <memory> 21 #include <sstream> 22 23 #include "notification_rdb_data_mgr.h" 24 #include "notification_preferences_info.h" 25 26 namespace OHOS { 27 namespace Notification { 28 class NotificationPreferencesDatabase final { 29 public: 30 NotificationPreferencesDatabase(); 31 ~NotificationPreferencesDatabase(); 32 33 /** 34 * @brief Put notification slots into disturbe DB. 35 * 36 * @param bundleName Indicates bundle name. 37 * @param bundleUid Indicates bundle uid. 38 * @param slots Indicates notification slots. 39 * @return Return true on success, false on failure. 40 */ 41 bool PutSlotsToDisturbeDB( 42 const std::string &bundleName, const int32_t &bundleUid, const std::vector<sptr<NotificationSlot>> &slots); 43 44 /** 45 * @brief Put notification bundle into disturbe DB. 46 * 47 * @param bundleInfo Indicates bundle info. 48 * @return Return true on success, false on failure. 49 */ 50 bool PutBundlePropertyToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo); 51 52 /** 53 * @brief Put show badge in the of bundle into disturbe DB. 54 * 55 * @param bundleInfo Indicates bundle info. 56 * @param enable Indicates to whether show badge. 57 * @return Return true on success, false on failure. 58 */ 59 bool PutShowBadge(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enable); 60 61 /** 62 * @brief Put importance in the of bundle into disturbe DB. 63 * 64 * @param bundleInfo Indicates bundle info. 65 * @param importance Indicates to importance level which can be LEVEL_NONE, 66 LEVEL_MIN, LEVEL_LOW, LEVEL_DEFAULT, LEVEL_HIGH, or LEVEL_UNDEFINED. 67 * @return Return true on success, false on failure. 68 */ 69 bool PutImportance(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &importance); 70 71 /** 72 * @brief Put badge total nums in the of bundle into disturbe DB. 73 * 74 * @param bundleInfo Indicates bundle info. 75 * @param totalBadgeNum Indicates to total badge num. 76 * @return Return true on success, false on failure. 77 */ 78 bool PutTotalBadgeNums(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int32_t &totalBadgeNum); 79 80 /** 81 * @brief Put private notification allowed in the of bundle into disturbe DB. 82 * 83 * @param bundleInfo Indicates bundle info. 84 * @param allow Indicates to whether to allow 85 * @return Return true on success, false on failure. 86 */ 87 bool PutPrivateNotificationsAllowed(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &allow); 88 89 /** 90 * @brief Put enable notification in the of bundle into disturbe DB. 91 * 92 * @param bundleInfo Indicates bundle info. 93 * @param enabled Indicates to whether to enabled 94 * @return Return true on success, false on failure. 95 */ 96 bool PutNotificationsEnabledForBundle( 97 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled); 98 99 /** 100 * @brief Put enable notification into disturbe DB. 101 * 102 * @param userId Indicates user. 103 * @param enabled Indicates to whether to enabled 104 * @return Return true on success, false on failure. 105 */ 106 bool PutNotificationsEnabled(const int32_t &userId, const bool &enabled); 107 bool PutHasPoppedDialog(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped); 108 109 /** 110 * @brief Put do not disturbe date into disturbe DB. 111 * 112 * @param userId Indicates user. 113 * @param date Indicates to do not disturbe date. 114 * @return Return true on success, false on failure. 115 */ 116 bool PutDoNotDisturbDate(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date); 117 118 /** 119 * @brief Parse notification info from disturbe DB. 120 * 121 * @param info Indicates notification info. 122 * @return Return true on success, false on failure. 123 */ 124 bool ParseFromDisturbeDB(NotificationPreferencesInfo &info); 125 126 /** 127 * @brief Delete all data from disturbe DB. 128 * 129 * @return Return true on success, false on failure. 130 */ 131 bool RemoveAllDataFromDisturbeDB(); 132 133 /** 134 * @brief Delete bundle data from disturbe DB. 135 * 136 * @param bundleKey Indicates the bundle key. 137 * @return Return true on success, false on failure. 138 */ 139 bool RemoveBundleFromDisturbeDB(const std::string &bundleKey); 140 141 /** 142 * @brief Delete slot from disturbe DB. 143 * 144 * @param bundleKey Indicates to which a bundle. 145 * @param type Indicates to slot type. 146 * @return Return true on success, false on failure. 147 */ 148 bool RemoveSlotFromDisturbeDB(const std::string &bundleKey, const NotificationConstant::SlotType &type); 149 150 /** 151 * @brief Delete all slots in the of bundle from disturbe DB. 152 * 153 * @param bundleKey Indicates to which a bundle. 154 * @return Return true on success, false on failure. 155 */ 156 bool RemoveAllSlotsFromDisturbeDB(const std::string &bundleKey); 157 158 /** 159 * @brief Deal death recipient. 160 * 161 * @return Return true on success, false on failure. 162 */ 163 bool StoreDeathRecipient(); 164 165 bool RemoveNotificationEnable(const int32_t userId); 166 bool RemoveDoNotDisturbDate(const int32_t userId); 167 bool RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid); 168 169 private: 170 bool CheckRdbStore(); 171 172 bool CheckBundle(const std::string &bundleName, const int32_t &bundleUid); 173 bool PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo); 174 template <typename T> 175 int32_t PutBundlePropertyToDisturbeDB( 176 const std::string &bundleKey, const BundleType &type, const T &t); 177 bool PutBundleToDisturbeDB( 178 const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo); 179 180 void GetValueFromDisturbeDB(const std::string &key, std::function<void(std::string &)> function); 181 void GetValueFromDisturbeDB(const std::string &key, 182 std::function<void(int32_t &, std::string &)> function); 183 184 bool SlotToEntry(const std::string &bundleName, const int32_t &bundleUid, const sptr<NotificationSlot> &slot, 185 std::unordered_map<std::string, std::string> &values); 186 void GenerateSlotEntry(const std::string &bundleKey, const sptr<NotificationSlot> &slot, 187 std::unordered_map<std::string, std::string> &values) const; 188 void GenerateEntry( 189 const std::string &key, const std::string &value, std::unordered_map<std::string, std::string> &values) const; 190 191 std::string FindLastString(const std::string &findString, const std::string &inputString) const; 192 std::string SubUniqueIdentifyFromString(const std::string &findString, const std::string &keyStr) const; 193 std::string VectorToString(const std::vector<int64_t> &data) const; 194 void StringToVector(const std::string &str, std::vector<int64_t> &data) const; 195 int32_t StringToInt(const std::string &str) const; 196 int64_t StringToInt64(const std::string &str) const; 197 bool IsSlotKey(const std::string &bundleKey, const std::string &key) const; 198 std::string GenerateSlotKey( 199 const std::string &bundleKey, const std::string &type = "", const std::string &subType = "") const; 200 std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const; 201 202 void ParseBundleFromDistureDB( 203 NotificationPreferencesInfo &info, const std::unordered_map<std::string, std::string> &entries); 204 void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, 205 const std::pair<std::string, std::string> &entry); 206 void ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, 207 const std::string &bundleKey, const std::pair<std::string, std::string> &entry); 208 void ParseDoNotDisturbType(NotificationPreferencesInfo &info); 209 void ParseDoNotDisturbBeginDate(NotificationPreferencesInfo &info); 210 void ParseDoNotDisturbEndDate(NotificationPreferencesInfo &info); 211 void ParseEnableAllNotification(NotificationPreferencesInfo &info); 212 void ParseBundleName(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 213 void ParseBundleImportance(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 214 void ParseBundleShowBadge(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 215 void ParseBundleBadgeNum(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 216 void ParseBundlePrivateAllowed( 217 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 218 void ParseBundleEnableNotification( 219 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 220 void ParseBundlePoppedDialog( 221 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 222 void ParseBundleUid(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 223 void ParseSlot( 224 const std::string &findString, sptr<NotificationSlot> &slot, const std::pair<std::string, std::string> &entry); 225 void ParseSlotDescription(sptr<NotificationSlot> &slot, const std::string &value) const; 226 void ParseSlotLevel(sptr<NotificationSlot> &slot, const std::string &value) const; 227 void ParseSlotShowBadge(sptr<NotificationSlot> &slot, const std::string &value) const; 228 void ParseSlotEnableLight(sptr<NotificationSlot> &slot, const std::string &value) const; 229 void ParseSlotEnableVrbration(sptr<NotificationSlot> &slot, const std::string &value) const; 230 void ParseSlotLedLightColor(sptr<NotificationSlot> &slot, const std::string &value) const; 231 void ParseSlotLockscreenVisibleness(sptr<NotificationSlot> &slot, const std::string &value) const; 232 void ParseSlotSound(sptr<NotificationSlot> &slot, const std::string &value) const; 233 void ParseSlotVibrationSytle(sptr<NotificationSlot> &slot, const std::string &value) const; 234 void ParseSlotEnableBypassDnd(sptr<NotificationSlot> &slot, const std::string &value) const; 235 void ParseSlotEnabled(sptr<NotificationSlot> &slot, const std::string &value) const; 236 237 std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo) const; 238 void GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId); 239 void GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId); 240 void GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId); 241 void GetEnableAllNotification(NotificationPreferencesInfo &info, int32_t userId); 242 243 static const std::map<std::string, 244 std::function<void(NotificationPreferencesDatabase *, sptr<NotificationSlot> &, std::string &)>> 245 slotMap_; 246 static const std::map<std::string, std::function<void(NotificationPreferencesDatabase *, 247 NotificationPreferencesInfo::BundleInfo &, std::string &)>> 248 bundleMap_; 249 250 std::shared_ptr<NotificationDataMgr> rdbDataManager_; 251 }; 252 } // namespace Notification 253 } // namespace OHOS 254 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H 255