/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef BASE_NOTIFICATION_ANS_STANDARD_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H #define BASE_NOTIFICATION_ANS_STANDARD_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H #include #include #include "distributed_kv_data_manager.h" #include "notification_preferences_info.h" namespace OHOS { namespace Notification { class NotificationPreferencesDatabase final { public: NotificationPreferencesDatabase(); ~NotificationPreferencesDatabase(); bool PutSlotsToDisturbeDB( const std::string &bundleName, const int &bundleUid, const std::vector> &slots); bool PutGroupsToDisturbeDB( const std::string &bundleName, const int &bundleUid, const std::vector> &groups); bool PutBundlePropertyToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo); bool PutShowBadge(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enable); bool PutImportance(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int &importance); bool PutTotalBadgeNums(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int &totalBadgeNum); bool PutPrivateNotificationsAllowed(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &allow); bool PutNotificationsEnabledForBundle( const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled); bool PutNotificationsEnabled(const int32_t &userId, const bool &enabled); bool PutHasPoppedDialog(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &hasPopped); bool PutDoNotDisturbDate(const int32_t &userId, const sptr &date); bool ParseFromDisturbeDB(NotificationPreferencesInfo &info); bool RemoveAllDataFromDisturbeDB(); bool RemoveBundleFromDisturbeDB(const std::string &bundleKey); bool RemoveSlotFromDisturbeDB(const std::string &bundleKey, const NotificationConstant::SlotType &type); bool RemoveAllSlotsFromDisturbeDB(const std::string &bundleKey); bool RemoveGroupsFromDisturbeDB(const std::string &bundleKey, const std::vector &groupId); bool StoreDeathRecipient(); bool RemoveNotificationEnable(const int32_t userId); bool RemoveDoNotDisturbDate(const int32_t userId); private: void TryTwice(const std::function &func) const; DistributedKv::Status GetKvStore(); void CloseKvStore(); bool CheckKvStore(); bool CheckBundle(const std::string &bundleName, const int &bundleUid); bool PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo); template DistributedKv::Status PutBundlePropertyToDisturbeDB( const std::string &bundleKey, const BundleType &type, const T &t); bool PutBundleToDisturbeDB( const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo); void GetValueFromDisturbeDB(const std::string &key, std::function function); void GetValueFromDisturbeDB(const std::string &key, std::function function); bool GetRemoveGroupKeysFromDisturbeDB( const std::string &bundleKey, const std::string &groupId, std::vector &keys); bool SlotToEntry(const std::string &bundleName, const int &bundleUid, const sptr &slot, std::vector &entries); bool GroupToEntry(const std::string &bundleName, const int &bundleUid, const sptr &group, std::vector &entries); void GenerateGroupEntry(const std::string &bundleKey, const sptr &group, std::vector &entries) const; void GenerateSlotEntry(const std::string &bundleKey, const sptr &slot, std::vector &entries) const; void GenerateEntry( const std::string &key, const std::string &value, std::vector &entry) const; std::string FindLastString(const std::string &findString, const std::string &inputString) const; std::string SubUniqueIdentifyFromString(const std::string &findString, const std::string &keyStr) const; std::string VectorToString(const std::vector &data) const; void StringToVector(const std::string &str, std::vector &data) const; int StringToInt(const std::string &str) const; int64_t StringToInt64(const std::string &str) const; bool IsSlotKey(const std::string &bundleKey, const std::string &key) const; bool IsGroupKey(const std::string &bundleKey, const std::string &key) const; std::string GenerateSlotKey( const std::string &bundleKey, const std::string &type = "", const std::string &subType = "") const; std::string GenerateGroupKey(const std::string &bundleKey, const std::string &subType = "") const; std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const; void ParseBundleFromDistureDB( NotificationPreferencesInfo &info, const std::vector &entries); void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, const DistributedKv::Entry &entry); void ParseGroupFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, const DistributedKv::Entry &entry); void ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, const DistributedKv::Entry &entry); void ParseDoNotDisturbType(NotificationPreferencesInfo &info); void ParseDoNotDisturbBeginDate(NotificationPreferencesInfo &info); void ParseDoNotDisturbEndDate(NotificationPreferencesInfo &info); void ParseEnableAllNotification(NotificationPreferencesInfo &info); void ParseGroupDescription( const std::string &bundleKey, sptr &group, const DistributedKv::Entry &entry); void ParseBundleName(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleImportance(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleShowBadge(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleBadgeNum(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundlePrivateAllowed( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleEnableNotification( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundlePoppedDialog( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleUid(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseSlot( const std::string &findString, sptr &slot, const DistributedKv::Entry &entry); void ParseSlotGroupId(sptr &slot, const std::string &value) const; void ParseSlotDescription(sptr &slot, const std::string &value) const; void ParseSlotLevel(sptr &slot, const std::string &value) const; void ParseSlotShowBadge(sptr &slot, const std::string &value) const; void ParseSlotEnableLight(sptr &slot, const std::string &value) const; void ParseSlotEnableVrbration(sptr &slot, const std::string &value) const; void ParseSlotLedLightColor(sptr &slot, const std::string &value) const; void ParseSlotLockscreenVisibleness(sptr &slot, const std::string &value) const; void ParseSlotSound(sptr &slot, const std::string &value) const; void ParseSlotVibrationSytle(sptr &slot, const std::string &value) const; void ParseSlotEnableBypassDnd(sptr &slot, const std::string &value) const; std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo) const; void GetDoNotDisturbType(NotificationPreferencesInfo &info, int userId); void GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int userId); void GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int userId); void GetEnableAllNotification(NotificationPreferencesInfo &info, int userId); static const std::map &, std::string &)>> slotMap_; static const std::map> bundleMap_; const DistributedKv::AppId appId_ {APP_ID}; const DistributedKv::StoreId storeId_ {STORE_ID}; std::shared_ptr kvStorePtr_ = nullptr; DistributedKv::DistributedKvDataManager dataManager_; }; } // namespace Notification } // namespace OHOS #endif // BASE_NOTIFICATION_ANS_STANDARD_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H