1 /*
2 * Copyright (c) 2021-2023 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 "advanced_notification_service.h"
17
18 #include <functional>
19 #include <iomanip>
20 #include <sstream>
21 #include <filesystem>
22 #include <file_ex.h>
23
24 #include "accesstoken_kit.h"
25 #include "ans_inner_errors.h"
26 #include "ans_log_wrapper.h"
27 #include "errors.h"
28
29 #include "ipc_skeleton.h"
30 #include "access_token_helper.h"
31 #include "notification_constant.h"
32 #include "notification_request.h"
33 #include "reminder_helper.h"
34 #include "os_account_manager.h"
35 #include "hitrace_meter_adapter.h"
36 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
37 #include "distributed_notification_manager.h"
38 #include "distributed_preferences.h"
39 #include "distributed_screen_status_manager.h"
40 #endif
41
42 #include "advanced_notification_inline.h"
43
44 namespace OHOS {
45 namespace Notification {
46 constexpr const char* REMINDER_DB_PATH = "/data/service/el1/public/notification/notification.db";
47 constexpr const char* REMINDER_AGENT_SERVICE_CONFIG_PATH =
48 "/data/service/el1/public/notification/reminder_agent_service_config";
49 constexpr const char* CALENDAR_DATA_NAME = "com.ohos.calendardata";
50
51 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
GetRemindType()52 NotificationConstant::RemindType AdvancedNotificationService::GetRemindType()
53 {
54 bool remind = localScreenOn_;
55 if (distributedReminderPolicy_ == NotificationConstant::DistributedReminderPolicy::DEFAULT) {
56 bool remoteUsing = false;
57 ErrCode result = DistributedScreenStatusManager::GetInstance()->CheckRemoteDevicesIsUsing(remoteUsing);
58 if (result != ERR_OK) {
59 remind = true;
60 }
61 if (!localScreenOn_ && !remoteUsing) {
62 remind = true;
63 }
64 } else if (distributedReminderPolicy_ == NotificationConstant::DistributedReminderPolicy::ALWAYS_REMIND) {
65 remind = true;
66 } else if (distributedReminderPolicy_ == NotificationConstant::DistributedReminderPolicy::DO_NOT_REMIND) {
67 remind = false;
68 }
69
70 if (localScreenOn_) {
71 if (remind) {
72 return NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND;
73 } else {
74 return NotificationConstant::RemindType::DEVICE_ACTIVE_DONOT_REMIND;
75 }
76 } else {
77 if (remind) {
78 return NotificationConstant::RemindType::DEVICE_IDLE_REMIND;
79 } else {
80 return NotificationConstant::RemindType::DEVICE_IDLE_DONOT_REMIND;
81 }
82 }
83 }
84 #endif
85
SetNotificationRemindType(sptr<Notification> notification,bool isLocal)86 ErrCode AdvancedNotificationService::SetNotificationRemindType(sptr<Notification> notification, bool isLocal)
87 {
88 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
89 notification->SetRemindType(GetRemindType());
90 #else
91 notification->SetRemindType(NotificationConstant::RemindType::NONE);
92 #endif
93 return ERR_OK;
94 }
95
TryStartReminderAgentService()96 void AdvancedNotificationService::TryStartReminderAgentService()
97 {
98 auto checkCalendarFunc = []() {
99 int32_t activeUserId = 0;
100 if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(activeUserId) != ERR_OK) {
101 ANSR_LOGE("Failed to get active user id");
102 return false;
103 }
104 std::shared_ptr<BundleManagerHelper> bundleMgr = BundleManagerHelper::GetInstance();
105 if (bundleMgr == nullptr) {
106 ANSR_LOGE("Failed to get bundle manager");
107 return false;
108 }
109 int32_t uid = bundleMgr->GetDefaultUidByBundleName(CALENDAR_DATA_NAME, activeUserId);
110 return uid != -1;
111 };
112 if (!checkCalendarFunc()) {
113 if (access(REMINDER_DB_PATH, F_OK) != 0) {
114 ANS_LOGW("Reminder db no exist");
115 return;
116 }
117 std::string reminderAgentServiceConfig;
118 OHOS::LoadStringFromFile(REMINDER_AGENT_SERVICE_CONFIG_PATH, reminderAgentServiceConfig);
119 if (reminderAgentServiceConfig != "1") {
120 return;
121 }
122 }
123 ANS_LOGI("Reminder db exist, start reminder service");
124 ReminderHelper::StartReminderAgentService();
125 }
126 } // namespace Notification
127 } // namespace OHOS
128