• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
HandleAddDoNotDisturbProfiles(MessageParcel & data,MessageParcel & reply)29 ErrCode DisturbManager::HandleAddDoNotDisturbProfiles(MessageParcel &data, MessageParcel &reply)
30 {
31     std::vector<sptr<NotificationDoNotDisturbProfile>> profiles;
32     if (!ReadParcelableVector(profiles, data)) {
33         ANS_LOGE("Read profiles failed.");
34         return ERR_ANS_PARCELABLE_FAILED;
35     }
36 
37     if (profiles.size() > MAX_STATUS_VECTOR_NUM) {
38         ANS_LOGE("The profiles is exceeds limit.");
39         return ERR_ANS_INVALID_PARAM;
40     }
41 
42     ErrCode result = AddDoNotDisturbProfilesSyncQueue(profiles);
43     if (!reply.WriteInt32(result)) {
44         ANS_LOGE("Write result failed, ErrCode is %{public}d", result);
45         return ERR_ANS_PARCELABLE_FAILED;
46     }
47     return ERR_OK;
48 }
49 
AddDoNotDisturbProfilesSyncQueue(const std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)50 ErrCode DisturbManager::AddDoNotDisturbProfilesSyncQueue(
51     const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
52 {
53     int32_t userId = SUBSCRIBE_USER_INIT;
54     if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) {
55         ANS_LOGW("No active user found.");
56         return ERR_ANS_GET_ACTIVE_USER_FAILED;
57     }
58     AdvancedNotificationService::GetInstance()->SubmitSyncTask(
59         std::bind([copyUserId = userId, copyProfiles = profiles]() {
60             NotificationPreferences::GetInstance()->AddDoNotDisturbProfiles(copyUserId, copyProfiles);
61         }));
62     return ERR_OK;
63 }
64 }  // namespace Notification
65 }  // namespace OHOS
66