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_H 17 #define BASE_NOTIFICATION_ANS_STANDARD_SERVICES_NOTIFICATION_PREFERENCES_H 18 19 #include "refbase.h" 20 #include "singleton.h" 21 22 #include "notification_do_not_disturb_date.h" 23 #include "notification_preferences_database.h" 24 25 namespace OHOS { 26 namespace Notification { 27 class NotificationPreferences final { 28 public: 29 DISALLOW_COPY_AND_MOVE(NotificationPreferences); 30 static NotificationPreferences &GetInstance(); 31 ErrCode AddNotificationSlots( 32 const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots); 33 ErrCode AddNotificationSlotGroups( 34 const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlotGroup>> &groups); 35 ErrCode AddNotificationBundleProperty(const sptr<NotificationBundleOption> &bundleOption); 36 ErrCode RemoveNotificationSlot( 37 const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType); 38 ErrCode RemoveNotificationAllSlots(const sptr<NotificationBundleOption> &bundleOption); 39 ErrCode RemoveNotificationSlotGroups( 40 const sptr<NotificationBundleOption> &bundleOption, const std::vector<std::string> &groupIds); 41 ErrCode RemoveNotificationForBundle(const sptr<NotificationBundleOption> &bundleOption); 42 ErrCode UpdateNotificationSlots( 43 const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slot); 44 ErrCode UpdateNotificationSlotGroups( 45 const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlotGroup>> &groups); 46 ErrCode GetNotificationSlot(const sptr<NotificationBundleOption> &bundleOption, 47 const NotificationConstant::SlotType &type, sptr<NotificationSlot> &slot); 48 ErrCode GetNotificationAllSlots( 49 const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<NotificationSlot>> &slots); 50 ErrCode GetNotificationSlotsNumForBundle(const sptr<NotificationBundleOption> &bundleOption, int &num); 51 ErrCode GetNotificationSlotGroup(const sptr<NotificationBundleOption> &bundleOption, const std::string &groupId, 52 sptr<NotificationSlotGroup> &group); 53 ErrCode GetNotificationAllSlotGroups( 54 const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<NotificationSlotGroup>> &groups); 55 ErrCode GetNotificationAllSlotInSlotGroup(const sptr<NotificationBundleOption> &bundleOption, 56 const std::string &groupId, std::vector<sptr<NotificationSlot>> &slots); 57 ErrCode IsShowBadge(const sptr<NotificationBundleOption> &bundleOption, bool &enable); 58 ErrCode SetShowBadge(const sptr<NotificationBundleOption> &bundleOption, const bool enable); 59 ErrCode GetImportance(const sptr<NotificationBundleOption> &bundleOption, int &importance); 60 ErrCode SetImportance(const sptr<NotificationBundleOption> &bundleOption, const int &importance); 61 ErrCode GetTotalBadgeNums(const sptr<NotificationBundleOption> &bundleOption, int &totalBadgeNum); 62 ErrCode SetTotalBadgeNums(const sptr<NotificationBundleOption> &bundleOption, const int num); 63 ErrCode GetPrivateNotificationsAllowed(const sptr<NotificationBundleOption> &bundleOption, bool &allow); 64 ErrCode SetPrivateNotificationsAllowed(const sptr<NotificationBundleOption> &bundleOption, const bool allow); 65 ErrCode GetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, bool &enabled); 66 ErrCode SetNotificationsEnabledForBundle(const sptr<NotificationBundleOption> &bundleOption, const bool enabled); 67 ErrCode GetNotificationsEnabled(const int32_t &userId, bool &enabled); 68 ErrCode SetNotificationsEnabled(const int32_t &userId, const bool &enabled); 69 ErrCode GetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool &hasPopped); 70 ErrCode SetHasPoppedDialog(const sptr<NotificationBundleOption> &bundleOption, bool hasPopped); 71 ErrCode GetDoNotDisturbDate(const int32_t &userId, sptr<NotificationDoNotDisturbDate> &date); 72 ErrCode SetDoNotDisturbDate(const int32_t &userId, const sptr<NotificationDoNotDisturbDate> date); 73 ErrCode GetTemplateSupported(const std::string& templateName, bool &support); 74 75 ErrCode ClearNotificationInRestoreFactorySettings(); 76 77 void OnDistributedKvStoreDeathRecipient(); 78 void InitSettingFromDisturbDB(); 79 void RemoveSettings(int32_t userId); 80 81 private: 82 ErrCode CheckSlotForCreateSlot(const sptr<NotificationBundleOption> &bundleOption, 83 const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const; 84 ErrCode CheckGroupForCreateSlotGroup(const sptr<NotificationBundleOption> &bundleOption, 85 const sptr<NotificationSlotGroup> &group, NotificationPreferencesInfo &preferencesInfo) const; 86 ErrCode CheckSlotForRemoveSlot(const sptr<NotificationBundleOption> &bundleOption, 87 const NotificationConstant::SlotType &slotType, NotificationPreferencesInfo &preferencesInfo) const; 88 ErrCode CheckGroupForRemoveSlotGroup(const sptr<NotificationBundleOption> &bundleOption, const std::string &groupId, 89 NotificationPreferencesInfo &preferencesInfo) const; 90 ErrCode CheckSlotForUpdateSlot(const sptr<NotificationBundleOption> &bundleOption, 91 const sptr<NotificationSlot> &slot, NotificationPreferencesInfo &preferencesInfo) const; 92 ErrCode CheckGroupForUpdateSlotGroup(const sptr<NotificationBundleOption> &bundleOption, 93 const sptr<NotificationSlotGroup> &group, NotificationPreferencesInfo &preferencesInfo) const; 94 template <typename T> 95 ErrCode SetBundleProperty(NotificationPreferencesInfo &preferencesInfo, 96 const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value); 97 template <typename T> 98 ErrCode SaveBundleProperty(NotificationPreferencesInfo::BundleInfo &bundleInfo, 99 const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, const T &value); 100 template <typename T> 101 ErrCode GetBundleProperty( 102 const sptr<NotificationBundleOption> &bundleOption, const BundleType &type, T &value) const; 103 std::string GenerateBundleKey(const sptr<NotificationBundleOption> &bundleOption) const; 104 bool CheckApiCompatibility(const sptr<NotificationBundleOption> &bundleOption) const; 105 106 private: 107 NotificationPreferencesInfo preferencesInfo_ {}; 108 std::unique_ptr<NotificationPreferencesDatabase> preferncesDB_ = nullptr; 109 DECLARE_DELAYED_REF_SINGLETON(NotificationPreferences); 110 }; 111 } // namespace Notification 112 } // namespace OHOS 113 114 #endif // NOTIFICATION_PREFERENCES_H 115