• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "slot_manager.h"
17 
18 #include <functional>
19 #include <iomanip>
20 #include <sstream>
21 
22 #include "access_token_helper.h"
23 #include "ans_inner_errors.h"
24 #include "ans_log_wrapper.h"
25 #include "ans_permission_def.h"
26 #include "errors.h"
27 #include "common_event_manager.h"
28 #include "common_event_support.h"
29 #include "hitrace_meter_adapter.h"
30 #include "os_account_manager_helper.h"
31 #include "ipc_skeleton.h"
32 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
33 #include "smart_reminder_center.h"
34 #endif
35 
36 #include "../advanced_notification_inline.cpp"
37 #include "notification_extension_wrapper.h"
38 #include "notification_analytics_util.h"
39 
40 namespace OHOS {
41 namespace Notification {
AddSlots(MessageParcel & data,MessageParcel & reply)42 ErrCode SlotManager::AddSlots(MessageParcel &data, MessageParcel &reply)
43 {
44     ANS_LOGD("%{public}s", __FUNCTION__);
45     std::vector<sptr<NotificationSlot>> slots;
46     if (!ReadParcelableVector(slots, data)) {
47         ANS_LOGE("[HandleAddSlots] fail: read slotsSize failed");
48         return ERR_ANS_PARCELABLE_FAILED;
49     }
50 
51     ErrCode result = AddSlotsSyncQue(slots);
52     if (!reply.WriteInt32(result)) {
53         ANS_LOGE("[HandleAddSlots] fail: write result failed, ErrCode=%{public}d", result);
54         return ERR_ANS_PARCELABLE_FAILED;
55     }
56     return ERR_OK;
57 }
58 
AddSlotsSyncQue(const std::vector<sptr<NotificationSlot>> & slots)59 ErrCode SlotManager::AddSlotsSyncQue(const std::vector<sptr<NotificationSlot>> &slots)
60 {
61     sptr<NotificationBundleOption> bundleOption = AdvancedNotificationService::GenerateBundleOption();
62     if (bundleOption == nullptr) {
63         return ERR_ANS_INVALID_BUNDLE;
64     }
65 
66     auto excuteQueue = AdvancedNotificationService::GetInstance()->GetNotificationSvrQueue();
67     if (excuteQueue == nullptr) {
68         ANS_LOGE("Serial queue is invalid.");
69         return ERR_ANS_INVALID_PARAM;
70     }
71 
72     ErrCode result;
73     ffrt::task_handle handler = excuteQueue->submit_h(std::bind([&]() {
74         result = AddSlotsInner(slots, bundleOption);
75     }));
76     excuteQueue->wait(handler);
77     return result;
78 }
79 
AddSlotsInner(const std::vector<sptr<NotificationSlot>> & slots,sptr<NotificationBundleOption> bundleOption)80 ErrCode SlotManager::AddSlotsInner(
81     const std::vector<sptr<NotificationSlot>> &slots, sptr<NotificationBundleOption> bundleOption)
82 {
83     if (slots.size() == 0) {
84         return ERR_ANS_INVALID_PARAM;
85     }
86 
87     ErrCode result = ERR_OK;
88     std::vector<sptr<NotificationSlot>> addSlots;
89     for (auto slot : slots) {
90         sptr<NotificationSlot> originalSlot;
91         result = NotificationPreferences::GetInstance()->GetNotificationSlot(bundleOption,
92             slot->GetType(), originalSlot);
93         if ((result == ERR_OK) && (originalSlot != nullptr)) {
94             continue;
95         }
96 
97         GenerateSlotReminderMode(slot, bundleOption, true);
98         addSlots.push_back(slot);
99     }
100 
101     if (addSlots.size() == 0) {
102         result = ERR_OK;
103     } else {
104         result = NotificationPreferences::GetInstance()->AddNotificationSlots(bundleOption, addSlots);
105     }
106     return result;
107 }
108 
109 }  // namespace Notification
110 }  // namespace OHOS
111