1 /*
2  * Copyright (c) 2024-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 "reminder_data_manager.h"
17 
18 #include "ability_manager_client.h"
19 #include "access_token_helper.h"
20 #include "ans_log_wrapper.h"
21 #include "ans_const_define.h"
22 #include "common_event_support.h"
23 #include "common_event_manager.h"
24 #include "reminder_request_calendar.h"
25 #include "in_process_call_wrapper.h"
26 #ifdef DEVICE_STANDBY_ENABLE
27 #include "standby_service_client.h"
28 #include "allow_type.h"
29 #endif
30 #include "ipc_skeleton.h"
31 #include "notification_slot.h"
32 #include "os_account_manager.h"
33 #include "reminder_event_manager.h"
34 #include "time_service_client.h"
35 #include "singleton.h"
36 #include "locale_config.h"
37 #include "bundle_manager_helper.h"
38 #include "datashare_predicates_object.h"
39 #include "datashare_value_object.h"
40 #include "datashare_helper.h"
41 #include "data_share_permission.h"
42 #include "datashare_errno.h"
43 #include "datashare_template.h"
44 #include "system_ability_definition.h"
45 #include "app_mgr_constants.h"
46 #include "iservice_registry.h"
47 #include "config_policy_utils.h"
48 #include "hitrace_meter_adapter.h"
49 #ifdef HAS_HISYSEVENT_PART
50 #include "hisysevent.h"
51 #endif
52 
53 namespace OHOS {
54 namespace Notification {
55 namespace {
56 constexpr int32_t ALL_SA_READY_FLAG = 2;  // bundle service and ability service ready.
57 }
58 
IsSystemReady()59 bool ReminderDataManager::IsSystemReady()
60 {
61     return saReadyFlag_ >= ALL_SA_READY_FLAG;
62 }
63 
IsActionButtonDataShareValid(const sptr<ReminderRequest>& reminder, const uint32_t callerTokenId)64 bool ReminderDataManager::IsActionButtonDataShareValid(const sptr<ReminderRequest>& reminder,
65     const uint32_t callerTokenId)
66 {
67     auto actionButtonMap = reminder->GetActionButtons();
68     for (auto it = actionButtonMap.begin(); it != actionButtonMap.end(); ++it) {
69         ReminderRequest::ActionButtonInfo& buttonInfo = it->second;
70         if (buttonInfo.dataShareUpdate->uri.empty()) {
71             continue;
72         }
73         Uri uri(buttonInfo.dataShareUpdate->uri);
74         auto ret = DataShare::DataSharePermission::VerifyPermission(callerTokenId, uri, false);
75         if (ret != DataShare::E_OK) {
76             ANSR_LOGE("publish failed, DataSharePermission::VerifyPermission return error[%{public}d],",
77                 static_cast<int32_t>(ret));
78             return false;
79         }
80     }
81     return true;
82 }
83 
HandleAutoDeleteReminder(const int32_t notificationId, const int32_t uid, const int64_t autoDeletedTime)84 void ReminderDataManager::HandleAutoDeleteReminder(const int32_t notificationId, const int32_t uid,
85     const int64_t autoDeletedTime)
86 {
87     ANSR_LOGI("auto delete reminder start");
88     std::vector<sptr<ReminderRequest>> showedReminder;
89     {
90         std::lock_guard<std::mutex> lock(ReminderDataManager::SHOW_MUTEX);
91         showedReminder = showedReminderVector_;
92     }
93     for (auto reminder : showedReminder) {
94         if (reminder == nullptr) {
95             continue;
96         }
97 
98         if (reminder->GetUid() != uid || notificationId != reminder->GetNotificationId() ||
99             reminder->GetAutoDeletedTime() != autoDeletedTime) {
100             continue;
101         }
102         CloseReminder(reminder, true);
103         UpdateAppDatabase(reminder, ReminderRequest::ActionButtonType::CLOSE);
104         CheckNeedNotifyStatus(reminder, ReminderRequest::ActionButtonType::CLOSE);
105     }
106     StartRecentReminder();
107 }
108 
OnBundleMgrServiceStart()109 void ReminderDataManager::OnBundleMgrServiceStart()
110 {
111     saReadyFlag_.fetch_add(1);
112 }
113 
OnAbilityMgrServiceStart()114 void ReminderDataManager::OnAbilityMgrServiceStart()
115 {
116     saReadyFlag_.fetch_add(1);
117 }
118 
GetCustomRingFileDesc(const sptr<ReminderRequest>& reminder, Global::Resource::ResourceManager::RawFileDescriptor& desc)119 bool ReminderDataManager::GetCustomRingFileDesc(const sptr<ReminderRequest>& reminder,
120     Global::Resource::ResourceManager::RawFileDescriptor& desc)
121 {
122     // obtains the resource manager
123     std::lock_guard<std::mutex> locker(resourceMutex_);
124     soundResource_ = GetResourceMgr(reminder->GetBundleName(), reminder->GetUid());
125     if (soundResource_ == nullptr) {
126         ANSR_LOGE("GetResourceMgr fail.");
127         return false;
128     }
129     auto result = soundResource_->GetRawFileDescriptor(reminder->GetCustomRingUri(), desc);
130     if (result != Global::Resource::SUCCESS) {
131         ANSR_LOGE("GetRawFileDescriptor fail[%{public}d].", static_cast<int32_t>(result));
132         return false;
133     }
134     return true;
135 }
136 
CloseCustomRingFileDesc(const int32_t reminderId, const std::string& customRingUri)137 void ReminderDataManager::CloseCustomRingFileDesc(const int32_t reminderId, const std::string& customRingUri)
138 {
139     std::lock_guard<std::mutex> locker(resourceMutex_);
140     if (soundResource_ == nullptr) {
141         ANSR_LOGW("ResourceManager is nullptr.");
142         return;
143     }
144     auto result = soundResource_->CloseRawFileDescriptor(customRingUri);
145     if (result != Global::Resource::SUCCESS) {
146         ANSR_LOGW("CloseRawFileDescriptor fail[%{public}d]", static_cast<int32_t>(result));
147     }
148     ANSR_LOGI("Stop custom sound, reminderId:[%{public}d].", reminderId);
149     soundResource_ = nullptr;
150 }
151 
ReportSysEvent(const sptr<ReminderRequest>& reminder)152 void ReminderDataManager::ReportSysEvent(const sptr<ReminderRequest>& reminder)
153 {
154 #ifdef HAS_HISYSEVENT_PART
155     std::string event = "ALARM_TRIGGER";
156     std::string bundleName = reminder->GetBundleName();
157     int32_t uid = reminder->GetUid();
158     int32_t type = static_cast<int32_t>(reminder->GetReminderType());
159     int32_t repeat = static_cast<int32_t>(reminder->IsRepeat());
160     uint64_t triggerTime = reminder->GetTriggerTimeInMilli();
161     int32_t ringTime = static_cast<int32_t>(reminder->GetRingDuration());
162     HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::NOTIFICATION, event, HiviewDFX::HiSysEvent::EventType::STATISTIC,
163         "UID", uid, "NAME", bundleName, "TYPE", type, "REPEAT", repeat, "TRIGGER_TIME", triggerTime,
164         "RING_TIME", ringTime);
165 #endif
166 }
167 }
168 }
169