1 /*
2  * Copyright (c) 2021-2022 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 "form_info.h"
17 #include "form_info_filter.h"
18 #include "form_info_storage.h"
19 
20 #include "fms_log_wrapper.h"
21 #include "json_serializer.h"
22 
23 namespace OHOS {
24 namespace AAFwk {
25 namespace {
26 const std::string JSON_KEY_USER_ID = "userId";
27 const std::string JSON_KEY_FORM_INFO = "formInfos";
28 const int32_t DEFAULT_RECT_SHAPE = 1;
29 } // namespace
30 
FormInfoStorage(int32_t userId,const std::vector<AppExecFwk::FormInfo> & formInfos)31 FormInfoStorage::FormInfoStorage(int32_t userId, const std::vector<AppExecFwk::FormInfo> &formInfos)
32 {
33     this->userId = userId;
34     for (const auto &item : formInfos) {
35         this->formInfos.push_back(item);
36     }
37 }
38 
GetAllFormsInfo(int32_t userId,std::vector<AppExecFwk::FormInfo> & formInfos) const39 void FormInfoStorage::GetAllFormsInfo(int32_t userId, std::vector<AppExecFwk::FormInfo> &formInfos) const
40 {
41     HILOG_DEBUG("Get all forms infos, current userId is%{public}d, this userId is %{public}d", userId, this->userId);
42     if (this->userId != userId && this->userId != AppExecFwk::Constants::DEFAULT_USERID) {
43         return;
44     }
45 
46     for (const auto &item : this->formInfos) {
47         formInfos.push_back(item);
48     }
49 }
50 
find_match_dimensions(const std::vector<int32_t> & targetDimensions,const std::vector<int32_t> & supportDimensions,std::vector<int32_t> & results)51 static bool find_match_dimensions(const std::vector<int32_t> &targetDimensions,
52     const std::vector<int32_t> &supportDimensions, std::vector<int32_t> &results)
53 {
54     for (const auto &val : supportDimensions) {
55         auto it = std::find(targetDimensions.begin(), targetDimensions.end(), val);
56         if (it != targetDimensions.end()) {
57             results.emplace_back(val);
58         }
59     }
60     return !results.empty();
61 }
62 
find_match_shapes(const std::vector<int32_t> & targetShapes,const std::vector<int32_t> & supportShapes)63 static bool find_match_shapes(const std::vector<int32_t> &targetShapes, const std::vector<int32_t> &supportShapes)
64 {
65     for (const auto &val : supportShapes) {
66         auto it = std::find(targetShapes.begin(), targetShapes.end(), val);
67         if (it != targetShapes.end()) {
68             return true;
69         }
70     }
71     return false;
72 }
73 
find_rect_shape(const std::vector<int32_t> & supportShapes)74 static bool find_rect_shape(const std::vector<int32_t> &supportShapes)
75 {
76     for (const auto &val : supportShapes) {
77         if (val == DEFAULT_RECT_SHAPE) {
78             return true;
79         }
80     }
81     return false;
82 }
83 
GetFormsInfoByFilter(int32_t userId,const AppExecFwk::FormInfoFilter & filter,std::vector<AppExecFwk::FormInfo> & formInfos) const84 void FormInfoStorage::GetFormsInfoByFilter(int32_t userId,
85     const AppExecFwk::FormInfoFilter &filter, std::vector<AppExecFwk::FormInfo> &formInfos) const
86 {
87     HILOG_DEBUG("current userId is:%{public}d, this userId is %{public}d", userId, this->userId);
88     if (this->userId != userId && this->userId != AppExecFwk::Constants::DEFAULT_USERID) {
89         HILOG_ERROR("Invalid userId");
90         return;
91     }
92     for (const auto &item : this->formInfos) {
93         if (!filter.moduleName.empty() && filter.moduleName != item.moduleName) {
94             continue;
95         }
96         if (filter.supportShapes.empty() && !find_rect_shape(item.supportShapes)) {
97             HILOG_WARN("Empty supportShape,without rectShape");
98             continue;
99         }
100         if (!filter.supportShapes.empty() && !find_match_shapes(filter.supportShapes, item.supportShapes)) {
101             continue;
102         }
103         if (filter.supportDimensions.empty()) {
104             formInfos.emplace_back(item);
105         } else {
106             std::vector<int32_t> results;
107             if (find_match_dimensions(filter.supportDimensions, item.supportDimensions, results)) {
108                 AppExecFwk::FormInfo formInfo = item;
109                 formInfo.supportDimensions = results;
110                 formInfos.emplace_back(formInfo);
111             }
112         }
113     }
114 }
115 
GetFormsInfoByModule(int32_t userId,const std::string & moduleName,std::vector<AppExecFwk::FormInfo> & formInfos) const116 void FormInfoStorage::GetFormsInfoByModule(int32_t userId, const std::string &moduleName,
117     std::vector<AppExecFwk::FormInfo> &formInfos) const
118 {
119     if (this->userId != userId && this->userId != AppExecFwk::Constants::DEFAULT_USERID) {
120         return;
121     }
122     for (const auto &item : this->formInfos) {
123         if (item.moduleName == moduleName) {
124             formInfos.push_back(item);
125         }
126     }
127 }
128 
to_json(nlohmann::json & jsonObject,const FormInfoStorage & formInfoStorage)129 void to_json(nlohmann::json &jsonObject, const FormInfoStorage &formInfoStorage)
130 {
131     jsonObject = nlohmann::json {
132         {JSON_KEY_USER_ID, formInfoStorage.userId},
133         {JSON_KEY_FORM_INFO, formInfoStorage.formInfos}
134     };
135 }
136 
from_json(const nlohmann::json & jsonObject,FormInfoStorage & formInfoStorage)137 void from_json(const nlohmann::json &jsonObject, FormInfoStorage &formInfoStorage)
138 {
139     if (jsonObject.contains(JSON_KEY_USER_ID) && !jsonObject.at(JSON_KEY_USER_ID).is_null() &&
140         jsonObject.at(JSON_KEY_USER_ID).is_number_integer()) {
141         formInfoStorage.userId = jsonObject.at(JSON_KEY_USER_ID).get<int32_t>();
142     }
143     if (jsonObject.contains(JSON_KEY_FORM_INFO) && !jsonObject.at(JSON_KEY_FORM_INFO).is_null() &&
144         jsonObject.at(JSON_KEY_FORM_INFO).is_array()) {
145         formInfoStorage.formInfos = jsonObject.at(JSON_KEY_FORM_INFO).get<std::vector<AppExecFwk::FormInfo>>();
146     }
147 }
148 } // namespace AppExecFwk
149 } // namespace OHOS
150