1 /*
2 * Copyright (c) 2025 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 #include "sts_sorting_map.h"
16
17 #include "sts_common.h"
18 #include "sts_sorting.h"
19
20 namespace OHOS {
21 namespace NotificationSts {
GetKeySToRecode(ani_env * env,const std::shared_ptr<NotificationSortingMap> & sortingMap,ani_object & recordObj)22 bool GetKeySToRecode(ani_env *env, const std::shared_ptr<NotificationSortingMap> &sortingMap, ani_object &recordObj)
23 {
24 ani_status status = ANI_ERROR;
25 std::vector<std::string> keys = sortingMap->GetKey();
26 for (auto &it : keys) {
27 Notification::NotificationSorting sorting;
28 if (!sortingMap->GetNotificationSorting(it, sorting)) {
29 ANS_LOGE("GetNotificationSorting faild.");
30 return false;
31 }
32 ani_string keyString;
33 if (ANI_OK != GetAniStringByString(env, it, keyString)) {
34 ANS_LOGE("GetAniStringByString faild. key: %{public}s", it.c_str());
35 return false;
36 }
37 ani_object sortingObj;
38 if (!WarpNotificationSorting(env, sorting, sortingObj)) {
39 ANS_LOGE("WarpNotificationSorting faild. key: %{public}s", it.c_str());
40 return false;
41 }
42 if (keyString == nullptr) {
43 ANS_LOGE("GetAniString faild. key: %{public}s", it.c_str());
44 return false;
45 }
46 if (ANI_OK != (status = env->Object_CallMethodByName_Void(
47 recordObj, "$_set", "Lstd/core/Object;Lstd/core/Object;:V", keyString, sortingObj))) {
48 ANS_LOGE("set key value faild. key: %{public}s status %{public}d", it.c_str(), status);
49 return false;
50 }
51 }
52 return true;
53 }
54
WarpNotificationSortingMap(ani_env * env,const std::shared_ptr<NotificationSortingMap> & sortingMap,ani_object & outObj)55 bool WarpNotificationSortingMap(ani_env *env,
56 const std::shared_ptr<NotificationSortingMap> &sortingMap, ani_object &outObj)
57 {
58 ani_class cls;
59 ani_object recordObj;
60 ani_class recordCls;
61 ani_status status;
62 if (sortingMap == nullptr || env == nullptr) {
63 ANS_LOGE("invalid parameter value");
64 return false;
65 }
66
67 if (!CreateClassObjByClassName(env,
68 "Lnotification/notificationSortingMap/NotificationSortingMapInner;", cls, outObj)) {
69 ANS_LOGE("CreateClassObjByClassName faild.");
70 return false;
71 }
72
73 if (!CreateClassObjByClassName(env, "Lescompat/Record;", recordCls, recordObj) || recordObj == nullptr) {
74 ANS_LOGE("Create recordObj faild.");
75 return false;
76 }
77 if (!GetKeySToRecode(env, sortingMap, recordObj)) {
78 ANS_LOGE("GetKeySToRecode failed.");
79 return false;
80 }
81 if (ANI_OK != (status = env->Object_SetPropertyByName_Ref(outObj, "sortings", recordObj))) {
82 ANS_LOGE("Object_SetPropertyByName_Ref sortings faild. status %{public}d", status);
83 return false;
84 }
85 std::vector<std::string> keys = sortingMap->GetKey();
86 if (!keys.empty()) {
87 ani_object arrayObj = GetAniStringArrayByVectorString(env, keys);
88 if (arrayObj == nullptr) {
89 ANS_LOGE("WarpVectorStringToSts sortedHashCode faild");
90 return false;
91 }
92 if (ANI_OK != (status = env->Object_SetPropertyByName_Ref(outObj, "sortedHashCode", arrayObj))) {
93 ANS_LOGE("Object_SetPropertyByName_Ref sortedHashCode faild. status %{public}d", status);
94 return false;
95 }
96 }
97 return true;
98 }
99 } // namespace NotificationSts
100 } // OHOS