1 /* 2 * Copyright (c) 2024 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_REMINDER_INCLUDE_REMINDER_DATASHARE_HELPER_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_REMINDER_INCLUDE_REMINDER_DATASHARE_HELPER_H 18 19 #include "ffrt.h" 20 #include "reminder_request.h" 21 #include "datashare_helper.h" 22 #include "data_ability_observer_stub.h" 23 24 #include <vector> 25 #include <string> 26 27 namespace OHOS::Notification { 28 class ReminderDataShareHelper { 29 public: 30 static ReminderDataShareHelper& GetInstance(); 31 32 /** 33 * @brief Register datashare observer. 34 */ 35 bool RegisterObserver(); 36 37 /** 38 * @brief UnRegister datashare observer. 39 */ 40 bool UnRegisterObserver(); 41 42 public: 43 /** 44 * @brief Search for reminders from the current time to X minutes. 45 */ 46 bool Query(std::map<std::string, sptr<ReminderRequest>>& reminders); 47 48 /** 49 * @brief Update the reminder state. 50 * state is ReminderCalendarShareTable::STATE_* 51 */ 52 bool Update(const int32_t reminderId, const int32_t state); 53 54 /** 55 * @brief Start calendar data extension. 56 * reason is ReminderCalendarShareTable::START_* 57 */ 58 void StartDataExtension(const int32_t reason); 59 60 public: 61 /** 62 * @brief Set current user id. 63 */ SetUserId(const int32_t userId)64 void SetUserId(const int32_t userId) 65 { 66 curUserId_ = userId; 67 } 68 69 /** 70 * @brief Update calendar uid and calendar data uid. 71 */ 72 void UpdateCalendarUid(); 73 74 /** 75 * @brief Get cache update reminders. 76 */ 77 std::map<std::string, sptr<ReminderRequest>> GetCacheReminders(); 78 79 /** 80 * @brief Save update reminders to cache. 81 */ 82 void InsertCacheReminders(const std::map<std::string, sptr<ReminderRequest>>& reminders); 83 84 public: 85 /** 86 * @brief When datashare notify OnChange, the change type is insert or delete. 87 */ 88 void OnDataInsertOrDelete(); 89 90 /** 91 * @brief When datashare notify OnChange, the change type is update. 92 */ 93 void OnDataUpdate(const DataShare::DataShareObserver::ChangeInfo& info); 94 95 private: 96 /** 97 * @brief Build datasharehelper, need to release it after use, 98 * call ReleaseDataShareHelper. 99 */ 100 std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(); 101 bool ReleaseDataShareHelper(const std::shared_ptr<DataShare::DataShareHelper>& helper); 102 103 /** 104 * @brief Get share table columns. 105 */ 106 std::vector<std::string> GetColumns() const; 107 108 private: 109 /** 110 * @brief Build ReminderRequest from DataShareResultSet. 111 */ 112 sptr<ReminderRequest> CreateReminder(const std::shared_ptr<DataShare::DataShareResultSet>& result); 113 114 /** 115 * @brief Build ReminderRequest from ChangeInfo. 116 */ 117 std::map<std::string, sptr<ReminderRequest>> CreateReminder( 118 const DataShare::DataShareObserver::ChangeInfo& info); 119 120 /** 121 * @brief Init reminder base info. 122 */ 123 void InitNormalInfo(sptr<ReminderRequest>& reminder); 124 void InitBaseInfo(const DataShare::DataShareObserver::ChangeInfo::VBucket& info, 125 sptr<ReminderRequest>& reminder); 126 127 private: 128 // Singleton 129 ReminderDataShareHelper(); 130 ~ReminderDataShareHelper() = default; 131 132 ReminderDataShareHelper(const ReminderDataShareHelper&) = delete; 133 ReminderDataShareHelper& operator=(const ReminderDataShareHelper&) = delete; 134 ReminderDataShareHelper(ReminderDataShareHelper&&) = delete; 135 ReminderDataShareHelper& operator=(ReminderDataShareHelper&&) = delete; 136 137 private: 138 int32_t curUserId_ {0}; 139 int32_t uid_ {0}; // calendar 140 int32_t dataUid_ {0}; // calendardata 141 std::atomic<bool> insertTask_ {false}; 142 std::atomic<bool> updateTask_ {false}; 143 std::atomic<int64_t> insertTime_ {0}; 144 std::atomic<int64_t> updateTime_ {0}; 145 146 std::mutex mutex_; // for observer_ 147 std::shared_ptr<DataShare::DataShareObserver> observer_; 148 149 std::mutex cacheMutex_; // for cache 150 std::map<std::string, sptr<ReminderRequest>> cache_; // reminder cache 151 152 std::shared_ptr<ffrt::queue> queue_; // for OnChange 153 154 private: 155 class ReminderDataObserver : public DataShare::DataShareObserver { 156 public: 157 ReminderDataObserver() = default; 158 ~ReminderDataObserver() = default; 159 160 /** 161 * @brief Notification of data changes. 162 */ 163 void OnChange(const ChangeInfo& info) override; 164 }; 165 }; 166 } // namespace OHOS::Notification 167 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_REMINDER_INCLUDE_REMINDER_DATASHARE_HELPER_H