• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <functional>
17 #include <iomanip>
18 #include <sstream>
19 
20 #include "ans_inner_errors.h"
21 #include "ans_log_wrapper.h"
22 #include "access_token_helper.h"
23 #include "ans_permission_def.h"
24 #include "bundle_manager_helper.h"
25 #include "errors.h"
26 #include "ipc_skeleton.h"
27 #include "notification_constant.h"
28 #include "os_account_manager.h"
29 #include "notification_preferences.h"
30 
31 
32 namespace OHOS {
33 namespace Notification {
GetClientBundleName()34 inline std::string GetClientBundleName()
35 {
36     std::string bundle;
37 
38     int32_t callingUid = IPCSkeleton::GetCallingUid();
39 
40     std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
41     if (bundleManager != nullptr) {
42         bundle = bundleManager->GetBundleNameByUid(callingUid);
43     }
44 
45     return bundle;
46 }
47 
ResetSeconds(int64_t date)48 inline int64_t ResetSeconds(int64_t date)
49 {
50     auto milliseconds = std::chrono::milliseconds(date);
51     auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(milliseconds);
52     auto tp_minutes = std::chrono::time_point_cast<std::chrono::minutes>(tp);
53     auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(tp_minutes.time_since_epoch());
54     return duration.count();
55 }
56 
GetCurrentTime()57 inline int64_t GetCurrentTime()
58 {
59     auto now = std::chrono::system_clock::now();
60     auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
61     return duration.count();
62 }
63 
GetLocalTime(time_t time)64 inline tm GetLocalTime(time_t time)
65 {
66     struct tm ret = {0};
67     localtime_r(&time, &ret);
68     return ret;
69 }
70 
AssignValidNotificationSlot(const std::shared_ptr<NotificationRecord> & record)71 inline ErrCode AssignValidNotificationSlot(const std::shared_ptr<NotificationRecord> &record)
72 {
73     sptr<NotificationSlot> slot;
74     NotificationConstant::SlotType slotType = record->request->GetSlotType();
75     ErrCode result = NotificationPreferences::GetInstance().GetNotificationSlot(record->bundleOption, slotType, slot);
76     if ((result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) ||
77         (result == ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST)) {
78         slot = new (std::nothrow) NotificationSlot(slotType);
79         if (slot == nullptr) {
80             ANS_LOGE("Failed to create NotificationSlot instance");
81             return ERR_NO_MEMORY;
82         }
83         std::vector<sptr<NotificationSlot>> slots;
84         slots.push_back(slot);
85         result = NotificationPreferences::GetInstance().AddNotificationSlots(record->bundleOption, slots);
86     }
87     if (result == ERR_OK) {
88         if (slot != nullptr && slot->GetEnable()) {
89             record->slot = slot;
90         } else {
91             result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED;
92             ANS_LOGE("Type[%{public}d] slot enable closed", slotType);
93         }
94     }
95     return result;
96 }
97 
CheckPictureSize(const sptr<NotificationRequest> & request)98 inline ErrCode CheckPictureSize(const sptr<NotificationRequest> &request)
99 {
100     auto result = request->CheckImageSizeForContent();
101     if (result != ERR_OK) {
102         ANS_LOGE("Check image size failed.");
103         return result;
104     }
105 
106     if (request->CheckImageOverSizeForPixelMap(request->GetLittleIcon(), MAX_ICON_SIZE)) {
107         return ERR_ANS_ICON_OVER_SIZE;
108     }
109 
110     if (request->CheckImageOverSizeForPixelMap(request->GetBigIcon(), MAX_ICON_SIZE)) {
111         return ERR_ANS_ICON_OVER_SIZE;
112     }
113 
114     if (request->CheckImageOverSizeForPixelMap(request->GetOverlayIcon(), MAX_ICON_SIZE)) {
115         return ERR_ANS_ICON_OVER_SIZE;
116     }
117 
118     return ERR_OK;
119 }
120 
RemoveExpired(std::list<std::chrono::system_clock::time_point> & list,const std::chrono::system_clock::time_point & now)121 inline void RemoveExpired(
122     std::list<std::chrono::system_clock::time_point> &list, const std::chrono::system_clock::time_point &now)
123 {
124     auto iter = list.begin();
125     while (iter != list.end()) {
126         if (abs(now - *iter) > std::chrono::seconds(1)) {
127             iter = list.erase(iter);
128         } else {
129             break;
130         }
131     }
132 }
133 }  // namespace Notification
134 }  // namespace OHOS
135