• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 {
GetInstance()27 std::shared_ptr<NotificationCloneBundle> NotificationCloneBundle::GetInstance()
28 {
29     static std::shared_ptr<NotificationCloneBundle> instance =
30         std::make_shared<NotificationCloneBundle>();
31     return instance;
32 }
33 
NotificationCloneBundle()34 NotificationCloneBundle::NotificationCloneBundle()
35 {
36     cloneBundleQueue_ = std::make_shared<ffrt::queue>("NotificationCloneBundleQueue");
37     if (!cloneBundleQueue_) {
38         ANS_LOGE("ffrt create failed!");
39         return;
40     }
41 }
42 
~NotificationCloneBundle()43 NotificationCloneBundle::~NotificationCloneBundle()
44 {
45 }
46 
OnBackup(nlohmann::json & jsonObject)47 ErrCode NotificationCloneBundle::OnBackup(nlohmann::json &jsonObject)
48 {
49     ANS_LOGI("called");
50     int32_t userId = NotificationCloneUtil::GetActiveUserId();
51     std::vector<NotificationCloneBundleInfo> cloneBundles;
52     NotificationPreferences::GetInstance()->GetAllCLoneBundlesInfo(userId, cloneBundles);
53 
54     if (cloneBundles.empty()) {
55         ANS_LOGI("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 bundle backup %{public}s.", cloneBundles[index].Dump().c_str());
64     }
65     ANS_LOGD("Notification bundle list %{public}s", jsonObject.dump().c_str());
66     return ERR_OK;
67 }
68 
OnRestore(const nlohmann::json & jsonObject)69 void NotificationCloneBundle::OnRestore(const nlohmann::json &jsonObject)
70 {
71     ANS_LOGI("called");
72     if (jsonObject.is_null() || !jsonObject.is_array()) {
73         ANS_LOGI("Notification disturb profile list is null or not array.");
74         return;
75     }
76 
77     int32_t userId = NotificationCloneUtil::GetActiveUserId();
78     std::unique_lock lock(lock_);
79     if (!bundlesInfo_.empty()) {
80         NotificationPreferences::GetInstance()->DelBatchCloneBundleInfo(userId, bundlesInfo_);
81         bundlesInfo_.clear();
82     }
83     for (const auto &profile : jsonObject) {
84         NotificationCloneBundleInfo cloneBundleInfo;
85         cloneBundleInfo.FromJson(profile);
86         bundlesInfo_.emplace_back(cloneBundleInfo);
87     }
88     ANS_LOGI("Notification bundle list size %{public}zu.", bundlesInfo_.size());
89     if (cloneBundleQueue_ == nullptr || bundlesInfo_.empty()) {
90         ANS_LOGE("Clone bundle is invalidated or empty.");
91         return;
92     }
93 
94     for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end();) {
95         int32_t uid = NotificationCloneUtil::GetBundleUid(bundle->GetBundleName(),
96             userId, bundle->GetAppIndex());
97         if (uid == -1) {
98             bundle++;
99             continue;
100         }
101         bundle->SetUid(uid);
102         AdvancedNotificationService::GetInstance()->UpdateCloneBundleInfo(*bundle);
103         bundle = bundlesInfo_.erase(bundle);
104     }
105 
106     NotificationPreferences::GetInstance()->UpdateBatchCloneBundleInfo(userId, bundlesInfo_);
107     for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end(); bundle++) {
108         ANS_LOGI("Event bundle left %{public}s.", bundle->Dump().c_str());
109     }
110     ANS_LOGD("end");
111 }
112 
OnRestoreStart(const std::string bundleName,int32_t appIndex,int32_t userId,int32_t uid)113 void NotificationCloneBundle::OnRestoreStart(const std::string bundleName, int32_t appIndex,
114     int32_t userId, int32_t uid)
115 {
116     ANS_LOGI("Handle bundle event %{public}s %{public}d %{public}d %{public}d %{public}zu.",
117         bundleName.c_str(), appIndex, userId, uid, bundlesInfo_.size());
118     std::unique_lock lock(lock_);
119     if (bundlesInfo_.empty()) {
120         return;
121     }
122 
123     for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end();) {
124         if (bundle->GetBundleName() == bundleName && bundle->GetAppIndex() == appIndex) {
125             bundle->SetUid(uid);
126             AdvancedNotificationService::GetInstance()->UpdateCloneBundleInfo(*bundle);
127             NotificationPreferences::GetInstance()->DelCloneBundleInfo(userId, *bundle);
128             bundle = bundlesInfo_.erase(bundle);
129             break;
130         }
131         bundle++;
132     }
133     ANS_LOGI("Event bundle left %{public}zu.", bundlesInfo_.size());
134 }
135 
OnUserSwitch(int32_t userId)136 void NotificationCloneBundle::OnUserSwitch(int32_t userId)
137 {
138     ANS_LOGI("Handler user switch %{public}d", userId);
139     if (cloneBundleQueue_ == nullptr) {
140         ANS_LOGW("null cloneBundleQueue");
141         return;
142     }
143     cloneBundleQueue_->submit_h(std::bind([&, userId]() {
144         std::unique_lock lock(lock_);
145         bundlesInfo_.clear();
146         NotificationPreferences::GetInstance()->GetAllCloneBundleInfo(userId, bundlesInfo_);
147         for (auto bundle = bundlesInfo_.begin(); bundle != bundlesInfo_.end(); bundle++) {
148             ANS_LOGI("Event bundle OnUserSwitch %{public}s.", bundle->Dump().c_str());
149         }
150     }));
151 }
152 
153 }
154 }
155