1 /*
2 * Copyright (c) 2024-2025 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 "disturb_manager.h"
17
18 #include <functional>
19 #include <iomanip>
20 #include <sstream>
21
22 #include "advanced_notification_service.h"
23 #include "notification_preferences.h"
24 #include "os_account_manager_helper.h"
25
26 #include "../advanced_notification_inline.cpp"
27 namespace OHOS {
28 namespace Notification {
HandleGetDoNotDisturbDate(MessageParcel & data,MessageParcel & reply)29 ErrCode DisturbManager::HandleGetDoNotDisturbDate(MessageParcel &data, MessageParcel &reply)
30 {
31 sptr<NotificationDoNotDisturbDate> date = nullptr;
32
33 ErrCode result = GetDoNotDisturbDateSyncQueue(date);
34 if (!reply.WriteInt32(result)) {
35 ANS_LOGE("[HandleSetDoNotDisturbDate] fail: write result failed, ErrCode=%{public}d", result);
36 return ERR_ANS_PARCELABLE_FAILED;
37 }
38
39 if (result == ERR_OK) {
40 if (!reply.WriteParcelable(date)) {
41 ANS_LOGE("[HandleSetDoNotDisturbDate] fail: write date failed.");
42 return ERR_ANS_PARCELABLE_FAILED;
43 }
44 }
45
46 return ERR_OK;
47 }
48
HandleGetDoNotDisturbDateByUser(MessageParcel & data,MessageParcel & reply)49 ErrCode DisturbManager::HandleGetDoNotDisturbDateByUser(MessageParcel &data, MessageParcel &reply)
50 {
51 int32_t userId = SUBSCRIBE_USER_INIT;
52 if (!data.ReadInt32(userId)) {
53 ANS_LOGE("[HandleGetDoNotDisturbDateByUser] fail: read userId failed.");
54 return ERR_ANS_PARCELABLE_FAILED;
55 }
56
57 sptr<NotificationDoNotDisturbDate> date = nullptr;
58 ErrCode result = GetDoNotDisturbDateByUserSyncQueue(userId, date);
59 if (!reply.WriteInt32(result)) {
60 ANS_LOGE("[HandleGetDoNotDisturbDateByUser] fail: write result failed, ErrCode=%{public}d", result);
61 return ERR_ANS_PARCELABLE_FAILED;
62 }
63
64 if (result == ERR_OK) {
65 if (!reply.WriteParcelable(date)) {
66 ANS_LOGE("[HandleGetDoNotDisturbDateByUser] fail: write date failed.");
67 return ERR_ANS_PARCELABLE_FAILED;
68 }
69 }
70
71 return ERR_OK;
72 }
73
GetDoNotDisturbDateSyncQueue(sptr<NotificationDoNotDisturbDate> & date)74 ErrCode DisturbManager::GetDoNotDisturbDateSyncQueue(sptr<NotificationDoNotDisturbDate> &date)
75 {
76 ANS_LOGD("%{public}s", __FUNCTION__);
77
78 int32_t userId = SUBSCRIBE_USER_INIT;
79 if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) {
80 return ERR_ANS_GET_ACTIVE_USER_FAILED;
81 }
82 ErrCode result = ERR_OK;
83 AdvancedNotificationService::GetInstance()->SubmitSyncTask(std::bind([&]() {
84 result = GetDoNotDisturbDateByUserInner(userId, date);
85 }));
86 return result;
87 }
88
GetDoNotDisturbDateByUserSyncQueue(const int32_t & userId,sptr<NotificationDoNotDisturbDate> & date)89 ErrCode DisturbManager::GetDoNotDisturbDateByUserSyncQueue(const int32_t &userId,
90 sptr<NotificationDoNotDisturbDate> &date)
91 {
92 ANS_LOGD("%{public}s", __FUNCTION__);
93
94 if (userId <= SUBSCRIBE_USER_INIT) {
95 ANS_LOGE("Input userId is invalid.");
96 return ERR_ANS_INVALID_PARAM;
97 }
98 ErrCode result = ERR_OK;
99 AdvancedNotificationService::GetInstance()->SubmitSyncTask(std::bind([&]() {
100 ANS_LOGD("ffrt enter!");
101 result = GetDoNotDisturbDateByUserInner(userId, date);
102 }));
103 return result;
104 }
105
GetDoNotDisturbDateByUserInner(const int32_t & userId,sptr<NotificationDoNotDisturbDate> & date)106 ErrCode DisturbManager::GetDoNotDisturbDateByUserInner(const int32_t &userId, sptr<NotificationDoNotDisturbDate> &date)
107 {
108 sptr<NotificationDoNotDisturbDate> currentConfig = nullptr;
109 ErrCode result = NotificationPreferences::GetInstance()->GetDoNotDisturbDate(userId, currentConfig);
110 if (result != ERR_OK) {
111 return result;
112 }
113 int64_t now = GetCurrentTime();
114 switch (currentConfig->GetDoNotDisturbType()) {
115 case NotificationConstant::DoNotDisturbType::CLEARLY:
116 case NotificationConstant::DoNotDisturbType::ONCE:
117 if (now >= currentConfig->GetEndDate()) {
118 date = new (std::nothrow) NotificationDoNotDisturbDate(
119 NotificationConstant::DoNotDisturbType::NONE, 0, 0);
120 if (date == nullptr) {
121 ANS_LOGE("Failed to create NotificationDoNotDisturbDate instance");
122 return result;
123 }
124 NotificationPreferences::GetInstance()->SetDoNotDisturbDate(userId, date);
125 } else {
126 date = currentConfig;
127 }
128 break;
129 default:
130 date = currentConfig;
131 break;
132 }
133 return result;
134 }
135 } // namespace Notification
136 } // namespace OHOS
137