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_config_parse.h"
38 #include "notification_extension_wrapper.h"
39 #include "notification_analytics_util.h"
40
41 namespace OHOS {
42 namespace Notification {
43 SlotManager::SlotManager() = default;
44 SlotManager::~SlotManager() = default;
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)45 int32_t SlotManager::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
46 {
47 ErrCode result = CheckInterfacePermission(code);
48 if (result != ERR_OK) {
49 if (!reply.WriteInt32(result)) {
50 return ERR_ANS_PARCELABLE_FAILED;
51 }
52 return ERR_OK;
53 }
54
55 switch (code) {
56 case static_cast<uint32_t>(NotificationInterfaceCode::ADD_SLOTS): {
57 result = AddSlots(data, reply);
58 break;
59 }
60 case static_cast<uint32_t>(NotificationInterfaceCode::SET_ENABLED_FOR_BUNDLE_SLOT): {
61 result = SetEnabledForBundleSlot(data, reply);
62 break;
63 }
64 default: {
65 ANS_LOGE("[OnRemoteRequest] fail: unknown code!");
66 return ERR_ANS_INVALID_PARAM;
67 }
68 }
69 if (SUCCEEDED(result)) {
70 return NO_ERROR;
71 }
72
73 return result;
74 }
75
CheckInterfacePermission(uint32_t code)76 int32_t SlotManager::CheckInterfacePermission(uint32_t code)
77 {
78 switch (code) {
79 case static_cast<uint32_t>(NotificationInterfaceCode::ADD_SLOTS):
80 case static_cast<uint32_t>(NotificationInterfaceCode::SET_ENABLED_FOR_BUNDLE_SLOT): {
81 bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
82 if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
83 return ERR_ANS_NON_SYSTEM_APP;
84 }
85
86 if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
87 return ERR_ANS_PERMISSION_DENIED;
88 }
89
90 return ERR_OK;
91 }
92 default: {
93 ANS_LOGE("[OnRemoteRequest] fail: unknown code!");
94 return ERR_ANS_INVALID_PARAM;
95 }
96 }
97 }
98
GenerateSlotReminderMode(const sptr<NotificationSlot> & slot,const sptr<NotificationBundleOption> & bundle,bool isSpecifiedSlot,uint32_t defaultSlotFlags)99 void SlotManager::GenerateSlotReminderMode(const sptr<NotificationSlot> &slot,
100 const sptr<NotificationBundleOption> &bundle, bool isSpecifiedSlot, uint32_t defaultSlotFlags)
101 {
102 uint32_t slotFlags = defaultSlotFlags;
103 auto ret = NotificationPreferences::GetInstance()->GetNotificationSlotFlagsForBundle(bundle, slotFlags);
104 if (ret != ERR_OK) {
105 ANS_LOGI("Failed to get slotflags for bundle, use default slotflags.");
106 }
107
108 auto configSlotReminderMode =
109 DelayedSingleton<NotificationConfigParse>::GetInstance()->GetConfigSlotReminderModeByType(slot->GetType());
110 if (isSpecifiedSlot) {
111 slot->SetReminderMode(configSlotReminderMode & slotFlags & slot->GetReminderMode());
112 } else {
113 slot->SetReminderMode(configSlotReminderMode & slotFlags);
114 }
115
116 std::string bundleName = (bundle == nullptr) ? "" : bundle->GetBundleName();
117 ANS_LOGI("The reminder mode of %{public}d is %{public}d in %{public}s,specifiedSlot:%{public}d default:%{public}u",
118 slot->GetType(), slot->GetReminderMode(), bundleName.c_str(), isSpecifiedSlot, defaultSlotFlags);
119 }
120 } // namespace Notification
121 } // namespace OHOS
122