1 /*
2 * Copyright (c) 2024-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 #include "dh_notification_clone_bundle_service.h"
17
18 #include "ans_log_wrapper.h"
19 #include "notification_preferences.h"
20 #include "notification_clone_util.h"
21 #include "notification_clone_bundle_info.h"
22 #include "os_account_manager_helper.h"
23 #include "advanced_notification_service.h"
24
25 namespace OHOS {
26 namespace Notification {
27
GetInstance()28 std::shared_ptr<DhNotificationCloneBundle> DhNotificationCloneBundle::GetInstance()
29 {
30 static std::shared_ptr<DhNotificationCloneBundle> instance =
31 std::make_shared<DhNotificationCloneBundle>();
32 return instance;
33 }
34
DhNotificationCloneBundle()35 DhNotificationCloneBundle::DhNotificationCloneBundle()
36 {
37 dhCloneBundleQueue_ = std::make_shared<ffrt::queue>("DhNotificationCloneBundleQueue");
38 if (!dhCloneBundleQueue_) {
39 ANS_LOGE("ffrt create failed!");
40 return;
41 }
42 }
43
~DhNotificationCloneBundle()44 DhNotificationCloneBundle::~DhNotificationCloneBundle()
45 {
46 }
47
OnBackup(nlohmann::json & jsonObject)48 ErrCode DhNotificationCloneBundle::OnBackup(nlohmann::json &jsonObject)
49 {
50 ANS_LOGI("called");
51 std::vector<NotificationCloneBundleInfo> cloneBundles;
52 NotificationPreferences::GetInstance()->GetAllCLoneBundlesInfo(ZERO_USERID, cloneBundles);
53
54 if (cloneBundles.empty()) {
55 ANS_LOGI("dh Notification bundle list is empty.");
56 return ERR_OK;
57 }
58 jsonObject = nlohmann::json::array();
59 for (size_t index = 0; index < cloneBundles.size(); index++) {
60 nlohmann::json jsonNode;
61 cloneBundles[index].ToJson(jsonNode);
62 jsonObject.emplace_back(jsonNode);
63 ANS_LOGD("Event dh bundle backup %{public}s.", cloneBundles[index].Dump().c_str());
64 }
65 ANS_LOGD("dh Notification bundle list %{public}s", jsonObject.dump().c_str());
66 return ERR_OK;
67 }
68
OnRestore(const nlohmann::json & jsonObject)69 void DhNotificationCloneBundle::OnRestore(const nlohmann::json &jsonObject)
70 {
71 ANS_LOGI("called");
72 if (jsonObject.is_null() || !jsonObject.is_array()) {
73 ANS_LOGI("dh Notification bundle list is null or not array.");
74 return;
75 }
76
77 std::unique_lock lock(lock_);
78 if (!bundlesInfo_.empty()) {
79 NotificationPreferences::GetInstance()->DelBatchCloneBundleInfo(ZERO_USERID, bundlesInfo_);
80 bundlesInfo_.clear();
81 }
82 for (const auto &profile : jsonObject) {
83 NotificationCloneBundleInfo cloneBundleInfo;
84 cloneBundleInfo.FromJson(profile);
85 bundlesInfo_.emplace_back(cloneBundleInfo);
86 }
87 ANS_LOGI("dh Notification bundle list size %{public}zu.", bundlesInfo_.size());
88 if (dhCloneBundleQueue_ == nullptr || bundlesInfo_.empty()) {
89 ANS_LOGE("Clone dh bundle is invalidated or empty.");
90 return;
91 }
92
93 NotificationPreferences::GetInstance()->UpdateBatchCloneBundleInfo(ZERO_USERID, bundlesInfo_);
94 for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end(); bundle++) {
95 ANS_LOGI("Event dh bundle left %{public}s.", bundle->Dump().c_str());
96 }
97 ANS_LOGI("dh Notification bundle list on restore end.");
98 }
99
OnRestoreStart(const std::string bundleName,int32_t appIndex,int32_t userId,int32_t uid)100 void DhNotificationCloneBundle::OnRestoreStart(const std::string bundleName, int32_t appIndex,
101 int32_t userId, int32_t uid)
102 {
103 ANS_LOGI("Handle dh bundle event %{public}s %{public}d %{public}zu.",
104 bundleName.c_str(), uid, bundlesInfo_.size());
105 std::unique_lock lock(lock_);
106 if (bundlesInfo_.empty()) {
107 return;
108 }
109
110 for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end();) {
111 if (bundle->GetBundleName() == bundleName) {
112 bundle->SetUid(uid);
113 AdvancedNotificationService::GetInstance()->UpdateCloneBundleInfo(*bundle);
114 NotificationPreferences::GetInstance()->DelCloneBundleInfo(ZERO_USERID, *bundle);
115 bundle = bundlesInfo_.erase(bundle);
116 break;
117 }
118 bundle++;
119 }
120 ANS_LOGI("Event dh bundle left %{public}zu.", bundlesInfo_.size());
121 }
122
OnUserSwitch(int32_t userId)123 void DhNotificationCloneBundle::OnUserSwitch(int32_t userId)
124 {
125 ANS_LOGI("Handler user switch %{public}d", userId);
126 if (dhCloneBundleQueue_ == nullptr) {
127 ANS_LOGW("null dhCloneBundleQueue");
128 return;
129 }
130 dhCloneBundleQueue_->submit_h(std::bind([&, userId]() {
131 std::unique_lock lock(lock_);
132 bundlesInfo_.clear();
133 NotificationPreferences::GetInstance()->GetAllCloneBundleInfo(ZERO_USERID, bundlesInfo_);
134 for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end(); bundle++) {
135 ANS_LOGI("Event dh bundle OnUserSwitch %{public}s.", bundle->Dump().c_str());
136 }
137 }));
138 }
139
isDhSource()140 bool DhNotificationCloneBundle::isDhSource()
141 {
142 return true;
143 }
144
145 }
146 }