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 #include "notification_sorting_map.h"
17
18 #include "parcel.h"
19
20 namespace OHOS {
21 namespace Notification {
NotificationSortingMap()22 NotificationSortingMap::NotificationSortingMap()
23 {}
24
~NotificationSortingMap()25 NotificationSortingMap::~NotificationSortingMap()
26 {}
27
NotificationSortingMap(const std::vector<NotificationSorting> & sortingList)28 NotificationSortingMap::NotificationSortingMap(const std::vector<NotificationSorting> &sortingList)
29 {
30 for (auto item : sortingList) {
31 sortedKey_.push_back(item.GetKey());
32 sortings_[item.GetKey()] = item;
33 }
34 }
35
SetKey(const std::string & key)36 void NotificationSortingMap::SetKey(const std::string &key)
37 {
38 auto it = std::find(sortedKey_.cbegin(), sortedKey_.cend(), key);
39 if (it == sortedKey_.cend()) {
40 sortedKey_.emplace_back(key);
41 }
42 }
43
GetNotificationSorting(const std::string & key,NotificationSorting & sorting) const44 bool NotificationSortingMap::GetNotificationSorting(const std::string &key, NotificationSorting &sorting) const
45 {
46 for (auto item : sortings_) {
47 if (item.first == key) {
48 sorting = item.second;
49 return true;
50 }
51 }
52
53 return false;
54 }
55
SetNotificationSorting(const std::vector<NotificationSorting> & sortingList)56 void NotificationSortingMap::SetNotificationSorting(const std::vector<NotificationSorting> &sortingList)
57 {
58 for (auto item : sortingList) {
59 sortedKey_.push_back(item.GetKey());
60 sortings_[item.GetKey()] = item;
61 }
62 }
63
Marshalling(Parcel & parcel) const64 bool NotificationSortingMap::Marshalling(Parcel &parcel) const
65 {
66 bool ret = true;
67 if (!parcel.WriteUint64(sortings_.size())) {
68 ANS_LOGE("Can't write sorting size");
69 return false;
70 }
71
72 size_t count = 0;
73 for (auto &sorting : sortings_) {
74 if (!parcel.WriteParcelable(&sorting.second)) {
75 ANS_LOGE("Can't write sorting");
76 ret = false;
77 }
78 count++;
79
80 if (count == MAX_ACTIVE_NUM) {
81 break;
82 }
83 }
84 return ret;
85 }
86
Unmarshalling(Parcel & parcel)87 NotificationSortingMap *NotificationSortingMap::Unmarshalling(Parcel &parcel)
88 {
89 std::vector<NotificationSorting> sortings;
90 // read sorting num
91 uint64_t size = 0;
92 parcel.ReadUint64(size);
93 size = (size <= MAX_ACTIVE_NUM) ? size : MAX_ACTIVE_NUM;
94
95 for (uint64_t i = 0; i < size; i++) {
96 // read sorting
97 NotificationSorting *sorting = parcel.ReadParcelable<NotificationSorting>();
98 if (sorting != nullptr) {
99 sortings.push_back(*sorting);
100 }
101 delete sorting;
102 sorting = nullptr;
103 }
104
105 NotificationSortingMap *sortingMap = new (std::nothrow) NotificationSortingMap(sortings);
106 return sortingMap;
107 }
108
Dump() const109 std::string NotificationSortingMap::Dump() const
110 {
111 std::string keys = "";
112 for (auto key : sortedKey_) {
113 keys += key;
114 keys += ", ";
115 }
116 return "NotificationSortingMap{ "
117 "sortedkey = [" + keys + "]"
118 " }";
119 }
120 } // namespace Notification
121 } // namespace OHOS
122