1 /*
2  * Copyright (c) 2021 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 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SORTING_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SORTING_H
18 
19 #include "ans_log_wrapper.h"
20 #include "notification_slot.h"
21 #include "parcel.h"
22 #include "uri.h"
23 #include <cstddef>
24 
25 namespace OHOS {
26 namespace Notification {
27 class NotificationSorting final : public Parcelable {
28 public:
29     NotificationSorting();
30 
31     ~NotificationSorting();
32 
33     /**
34      * @brief A constructor used to create a NotificationSorting instance by copying parameters from an existing one.
35      *
36      * @param sorting Indicates the NotificationSorting object.
37      */
38     NotificationSorting(const NotificationSorting &sorting);
39 
40     /**
41      * @brief Obtains the sequence number of a notification among all the active notifications.
42      *
43      * @return Returns the sequence number of the notification.
44      */
GetRanking()45     inline uint64_t GetRanking() const
46     {
47         return ranking_;
48     };
49 
50     /**
51      * @brief Obtains the notification hash code, which is unique in the current application.
52      * Generally, the notification hash code is a string in the format
53      * Notification ID_Creator package name_Creator UID_Owner package name.
54      *
55      * @return Returns the notification hash code.
56      */
GetKey()57     inline std::string GetKey() const
58     {
59         return key_;
60     };
61 
62     /**
63      * @brief Obtains the importance level of the current notification set in the corresponding NotificationSlot.
64      *
65      * @return Returns the importance level of the notification.
66      */
GetImportance()67     inline int32_t GetImportance() const
68     {
69         return importance_;
70     };
71 
72     /**
73      * @brief Obtains the NotificationSlot the current notification belongs to.
74      * Each notification must be in a particular NotificationSlot.
75      *
76      * @return Returns the NotificationSlot of the notification.
77      */
GetSlot()78     inline NotificationSlot* GetSlot()
79     {
80         if (!slot_) {
81             slot_ = new (std::nothrow) NotificationSlot(NotificationConstant::SlotType::OTHER);
82             if (!slot_) {
83                 ANS_LOGE("Failed to create NotificationSlot");
84                 slot_ = nullptr;
85             }
86         }
87         return slot_;
88     };
89 
90     /**
91      * @brief Obtains the visibility of the current notification on the lock screen set.
92      *
93      * @return Returns the visibility of the notification on the lock screen.
94      */
GetVisiblenessOverride()95     inline int32_t GetVisiblenessOverride() const
96     {
97         return visiblenessOverride_;
98     };
99 
100     /**
101      * @brief Checks whether the badge is displayed for the current notification.
102      *
103      * @return Returns true if the badge is displayed; returns false otherwise.
104      */
IsDisplayBadge()105     inline bool IsDisplayBadge() const
106     {
107         return isDisplayBadge_;
108     };
109 
110     /**
111      * @brief Checks whether the current notification is hidden.
112      * A notification should be hidden if the application sending the notification is suspended.
113      *
114      * @return Returns true if the notification is hidden; returns false otherwise.
115      */
IsHiddenNotification()116     inline bool IsHiddenNotification() const
117     {
118         return isHiddenNotification_;
119     };
120 
121     /**
122      * @brief Obtains the overridden notification group key. If the system has overridden the group key,
123      * a non-null value will be returned.
124      *
125      * @return Returns the overridden notification group key used to bind notifications.
126      */
GetGroupKeyOverride()127     inline std::string GetGroupKeyOverride() const
128     {
129         return groupKeyOverride_;
130     };
131 
132     /**
133      * @brief Marshals a NotificationSorting object into a Parcel.
134      *
135      * @param parcel Indicates the Parcel object for marshalling.
136      * @return Returns true if the marshalling is successful; returns false otherwise.
137      */
138     bool Marshalling(Parcel &parcel) const override;
139 
140     /**
141      * @brief Unmarshals a NotificationSorting object from a Parcel.
142      *
143      * @param parcel Indicates the Parcel object for unmarshalling.
144      * @return Returns the NotificationSorting object.
145      */
146     static NotificationSorting *Unmarshalling(Parcel &parcel);
147 
148     /**
149      * @brief Dumps sorting info
150      *
151      * @return Returns sorting info.
152      */
153     std::string Dump() const;
154 
155 private:
156     void SetGroupKeyOverride(const std::string &str);
157     void SetKey(const std::string &key);
158     void SetImportance(const int32_t &importance);
159     void SetRanking(const uint64_t &ranking);
160     void SetSlot(const sptr<NotificationSlot> &slot);
161     void SetVisiblenessOverride(const int32_t &visibleness);
162     void SetDisplayBadge(const bool &isDisplayBadge);
163     void SetHiddenNotification(const bool &isHiddenNotfication);
164     bool ReadFromParcel(Parcel &parcel);
165 
166 private:
167     std::string key_ {};
168     uint64_t ranking_ {0};
169     int32_t importance_ {-1};
170     bool isDisplayBadge_ {true};
171     bool isHiddenNotification_ {};
172     std::string groupKeyOverride_ {};
173     int32_t visiblenessOverride_ {};
174     sptr<NotificationSlot> slot_ = new (std::nothrow) NotificationSlot(NotificationConstant::SlotType::OTHER);
175 
176     friend class AdvancedNotificationService;
177 };
178 }  // namespace Notification
179 }  // namespace OHOS
180 
181 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_NOTIFICATION_SORTING_H