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 Search value from uri. 50 */ 51 bool Query(Uri& uri, const std::string& key, std::string& value); 52 53 /** 54 * @brief Update the reminder state. 55 * state is ReminderCalendarShareTable::STATE_* 56 */ 57 bool Update(const int32_t reminderId, const int32_t state); 58 59 /** 60 * @brief Start calendar data extension. 61 * reason is ReminderCalendarShareTable::START_* 62 */ 63 void StartDataExtension(const int32_t reason); 64 65 public: 66 /** 67 * @brief Set current user id. 68 */ SetUserId(const int32_t userId)69 void SetUserId(const int32_t userId) 70 { 71 curUserId_ = userId; 72 } 73 74 /** 75 * @brief Update calendar uid and calendar data uid. 76 */ 77 void UpdateCalendarUid(); 78 79 /** 80 * @brief Get cache update reminders. 81 */ 82 std::map<std::string, sptr<ReminderRequest>> GetCacheReminders(); 83 84 /** 85 * @brief Save update reminders to cache. 86 */ 87 void InsertCacheReminders(const std::map<std::string, sptr<ReminderRequest>>& reminders); 88 89 public: 90 /** 91 * @brief When datashare notify OnChange, the change type is insert or delete. 92 */ 93 void OnDataInsertOrDelete(); 94 95 /** 96 * @brief When datashare notify OnChange, the change type is update. 97 */ 98 void OnDataUpdate(const DataShare::DataShareObserver::ChangeInfo& info); 99 100 private: 101 /** 102 * @brief Build datasharehelper, need to release it after use, 103 * call ReleaseDataShareHelper. 104 */ 105 std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(const std::string& uriStr); 106 bool ReleaseDataShareHelper(const std::shared_ptr<DataShare::DataShareHelper>& helper); 107 108 /** 109 * @brief Get share table columns. 110 */ 111 std::vector<std::string> GetColumns() const; 112 113 private: 114 /** 115 * @brief Build ReminderRequest from DataShareResultSet. 116 */ 117 sptr<ReminderRequest> CreateReminder(const std::shared_ptr<DataShare::DataShareResultSet>& result); 118 119 /** 120 * @brief Build ReminderRequest from ChangeInfo. 121 */ 122 std::map<std::string, sptr<ReminderRequest>> CreateReminder( 123 const DataShare::DataShareObserver::ChangeInfo& info); 124 125 /** 126 * @brief Init reminder base info. 127 */ 128 void InitNormalInfo(sptr<ReminderRequest>& reminder); 129 void InitBaseInfo(const DataShare::DataShareObserver::ChangeInfo::VBucket& info, 130 sptr<ReminderRequest>& reminder); 131 132 /** 133 * @brief Calendar database version1 134 */ 135 void BuildReminderV1(const std::shared_ptr<DataShare::DataShareResultSet>& result, 136 sptr<ReminderRequest>& reminder); 137 void BuildReminderV1(const DataShare::DataShareObserver::ChangeInfo::VBucket& info, 138 sptr<ReminderRequest>& reminder); 139 private: 140 // Singleton 141 ReminderDataShareHelper(); 142 ~ReminderDataShareHelper() = default; 143 144 ReminderDataShareHelper(const ReminderDataShareHelper&) = delete; 145 ReminderDataShareHelper& operator=(const ReminderDataShareHelper&) = delete; 146 ReminderDataShareHelper(ReminderDataShareHelper&&) = delete; 147 ReminderDataShareHelper& operator=(ReminderDataShareHelper&&) = delete; 148 149 private: 150 int32_t curUserId_ {0}; 151 int32_t uid_ {0}; // calendar 152 int32_t dataUid_ {0}; // calendardata 153 bool isNewRdbVer_ = false; // is new calendar rdb version 154 std::atomic<bool> insertTask_ {false}; 155 std::atomic<bool> updateTask_ {false}; 156 std::atomic<int64_t> insertTime_ {0}; 157 std::atomic<int64_t> updateTime_ {0}; 158 159 std::mutex mutex_; // for observer_ 160 std::shared_ptr<DataShare::DataShareObserver> observer_; 161 162 std::mutex cacheMutex_; // for cache 163 std::map<std::string, sptr<ReminderRequest>> cache_; // reminder cache 164 165 std::shared_ptr<ffrt::queue> queue_; // for OnChange 166 167 private: 168 class ReminderDataObserver : public DataShare::DataShareObserver { 169 public: 170 ReminderDataObserver() = default; 171 ~ReminderDataObserver() = default; 172 173 /** 174 * @brief Notification of data changes. 175 */ 176 void OnChange(const ChangeInfo& info) override; 177 }; 178 }; 179 } // namespace OHOS::Notification 180 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_REMINDER_INCLUDE_REMINDER_DATASHARE_HELPER_H