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_ANS_STANDARD_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H 17 #define BASE_NOTIFICATION_ANS_STANDARD_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H 18 19 #include <functional> 20 #include <memory> 21 22 #include "distributed_kv_data_manager.h" 23 #include "notification_preferences_info.h" 24 25 namespace OHOS { 26 namespace Notification { 27 class NotificationPreferencesDatabase final { 28 public: 29 NotificationPreferencesDatabase(); 30 ~NotificationPreferencesDatabase(); 31 bool PutSlotsToDisturbeDB( 32 const std::string &bundleName, const int &bundleUid, const std::vector<sptr<NotificationSlot>> &slots); 33 bool PutGroupsToDisturbeDB( 34 const std::string &bundleName, const int &bundleUid, const std::vector<sptr<NotificationSlotGroup>> &groups); 35 bool PutBundlePropertyToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo); 36 37 bool PutShowBadge(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enable); 38 bool PutImportance(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int &importance); 39 bool PutTotalBadgeNums(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int &totalBadgeNum); 40 bool PutPrivateNotificationsAllowed(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &allow); 41 bool PutNotificationsEnabledForBundle( 42 const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled); 43 bool PutNotificationsEnabled(const int32_t &userId, const bool &enabled); 44 bool PutHasPoppedDialog(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped); 45 bool PutDoNotDisturbDate(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> &date); 46 47 bool ParseFromDisturbeDB(NotificationPreferencesInfo &info); 48 49 bool RemoveAllDataFromDisturbeDB(); 50 bool RemoveBundleFromDisturbeDB(const std::string &bundleKey); 51 bool RemoveSlotFromDisturbeDB(const std::string &bundleKey, const NotificationConstant::SlotType &type); 52 bool RemoveAllSlotsFromDisturbeDB(const std::string &bundleKey); 53 bool RemoveGroupsFromDisturbeDB(const std::string &bundleKey, const std::vector<std::string> &groupId); 54 55 bool StoreDeathRecipient(); 56 57 bool RemoveNotificationEnable(const int32_t userId); 58 bool RemoveDoNotDisturbDate(const int32_t userId); 59 60 private: 61 void TryTwice(const std::function<DistributedKv::Status()> &func) const; 62 DistributedKv::Status GetKvStore(); 63 void CloseKvStore(); 64 bool CheckKvStore(); 65 66 bool CheckBundle(const std::string &bundleName, const int &bundleUid); 67 bool PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo); 68 template <typename T> 69 DistributedKv::Status PutBundlePropertyToDisturbeDB( 70 const std::string &bundleKey, const BundleType &type, const T &t); 71 bool PutBundleToDisturbeDB( 72 const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo); 73 74 void GetValueFromDisturbeDB(const std::string &key, std::function<void(DistributedKv::Value &)> function); 75 void GetValueFromDisturbeDB(const std::string &key, 76 std::function<void(DistributedKv::Status &, DistributedKv::Value &)> function); 77 bool GetRemoveGroupKeysFromDisturbeDB( 78 const std::string &bundleKey, const std::string &groupId, std::vector<DistributedKv::Key> &keys); 79 80 bool SlotToEntry(const std::string &bundleName, const int &bundleUid, const sptr<NotificationSlot> &slot, 81 std::vector<DistributedKv::Entry> &entries); 82 bool GroupToEntry(const std::string &bundleName, const int &bundleUid, const sptr<NotificationSlotGroup> &group, 83 std::vector<DistributedKv::Entry> &entries); 84 void GenerateGroupEntry(const std::string &bundleKey, const sptr<NotificationSlotGroup> &group, 85 std::vector<DistributedKv::Entry> &entries) const; 86 void GenerateSlotEntry(const std::string &bundleKey, const sptr<NotificationSlot> &slot, 87 std::vector<DistributedKv::Entry> &entries) const; 88 void GenerateEntry( 89 const std::string &key, const std::string &value, std::vector<DistributedKv::Entry> &entry) const; 90 91 std::string FindLastString(const std::string &findString, const std::string &inputString) const; 92 std::string SubUniqueIdentifyFromString(const std::string &findString, const std::string &keyStr) const; 93 std::string VectorToString(const std::vector<int64_t> &data) const; 94 void StringToVector(const std::string &str, std::vector<int64_t> &data) const; 95 int StringToInt(const std::string &str) const; 96 int64_t StringToInt64(const std::string &str) const; 97 bool IsSlotKey(const std::string &bundleKey, const std::string &key) const; 98 bool IsGroupKey(const std::string &bundleKey, const std::string &key) const; 99 std::string GenerateSlotKey( 100 const std::string &bundleKey, const std::string &type = "", const std::string &subType = "") const; 101 std::string GenerateGroupKey(const std::string &bundleKey, const std::string &subType = "") const; 102 std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const; 103 104 void ParseBundleFromDistureDB( 105 NotificationPreferencesInfo &info, const std::vector<DistributedKv::Entry> &entries); 106 void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, 107 const DistributedKv::Entry &entry); 108 void ParseGroupFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, 109 const DistributedKv::Entry &entry); 110 void ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, 111 const std::string &bundleKey, const DistributedKv::Entry &entry); 112 void ParseDoNotDisturbType(NotificationPreferencesInfo &info); 113 void ParseDoNotDisturbBeginDate(NotificationPreferencesInfo &info); 114 void ParseDoNotDisturbEndDate(NotificationPreferencesInfo &info); 115 void ParseEnableAllNotification(NotificationPreferencesInfo &info); 116 void ParseGroupDescription( 117 const std::string &bundleKey, sptr<NotificationSlotGroup> &group, const DistributedKv::Entry &entry); 118 void ParseBundleName(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 119 void ParseBundleImportance(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 120 void ParseBundleShowBadge(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 121 void ParseBundleBadgeNum(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 122 void ParseBundlePrivateAllowed( 123 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 124 void ParseBundleEnableNotification( 125 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 126 void ParseBundlePoppedDialog( 127 NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 128 void ParseBundleUid(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; 129 void ParseSlot( 130 const std::string &findString, sptr<NotificationSlot> &slot, const DistributedKv::Entry &entry); 131 void ParseSlotGroupId(sptr<NotificationSlot> &slot, const std::string &value) const; 132 void ParseSlotDescription(sptr<NotificationSlot> &slot, const std::string &value) const; 133 void ParseSlotLevel(sptr<NotificationSlot> &slot, const std::string &value) const; 134 void ParseSlotShowBadge(sptr<NotificationSlot> &slot, const std::string &value) const; 135 void ParseSlotEnableLight(sptr<NotificationSlot> &slot, const std::string &value) const; 136 void ParseSlotEnableVrbration(sptr<NotificationSlot> &slot, const std::string &value) const; 137 void ParseSlotLedLightColor(sptr<NotificationSlot> &slot, const std::string &value) const; 138 void ParseSlotLockscreenVisibleness(sptr<NotificationSlot> &slot, const std::string &value) const; 139 void ParseSlotSound(sptr<NotificationSlot> &slot, const std::string &value) const; 140 void ParseSlotVibrationSytle(sptr<NotificationSlot> &slot, const std::string &value) const; 141 void ParseSlotEnableBypassDnd(sptr<NotificationSlot> &slot, const std::string &value) const; 142 143 std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo) const; 144 void GetDoNotDisturbType(NotificationPreferencesInfo &info, int userId); 145 void GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int userId); 146 void GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int userId); 147 void GetEnableAllNotification(NotificationPreferencesInfo &info, int userId); 148 149 static const std::map<std::string, 150 std::function<void(NotificationPreferencesDatabase *, sptr<NotificationSlot> &, std::string &)>> 151 slotMap_; 152 static const std::map<std::string, std::function<void(NotificationPreferencesDatabase *, 153 NotificationPreferencesInfo::BundleInfo &, std::string &)>> 154 bundleMap_; 155 156 const DistributedKv::AppId appId_ {APP_ID}; 157 const DistributedKv::StoreId storeId_ {STORE_ID}; 158 std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_ = nullptr; 159 DistributedKv::DistributedKvDataManager dataManager_; 160 }; 161 } // namespace Notification 162 } // namespace OHOS 163 #endif // BASE_NOTIFICATION_ANS_STANDARD_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H 164