1 /*
2 * Copyright (c) 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 "system_defined_appitem.h"
17
18 namespace OHOS {
19 namespace UDMF {
SystemDefinedAppItem()20 SystemDefinedAppItem::SystemDefinedAppItem()
21 {
22 SetType(SYSTEM_DEFINED_APP_ITEM);
23 }
24
SystemDefinedAppItem(UDType type,ValueType value)25 SystemDefinedAppItem::SystemDefinedAppItem(UDType type, ValueType value) : SystemDefinedRecord(type, value)
26 {
27 SetType(SYSTEM_DEFINED_APP_ITEM);
28 if (std::holds_alternative<std::shared_ptr<Object>>(value)) {
29 auto object = std::get<std::shared_ptr<Object>>(value);
30 object->GetValue(APPID, appId_);
31 object->GetValue(APPNAME, appName_);
32 object->GetValue(APPICONID, appIconId_);
33 object->GetValue(APPLABELID, appLabelId_);
34 object->GetValue(BUNDLENAME, bundleName_);
35 object->GetValue(ABILITYNAME, abilityName_);
36 std::shared_ptr<Object> detailObj = nullptr;
37 if (object->GetValue(DETAILS, detailObj)) {
38 details_ = ObjectUtils::ConvertToUDDetails(detailObj);
39 }
40 hasObject_ = true;
41 }
42 }
43
GetSize()44 int64_t SystemDefinedAppItem::GetSize()
45 {
46 return static_cast<int64_t>(UnifiedDataUtils::GetDetailsSize(this->details_) + this->appId_.size() +
47 this->appName_.size() + this->appIconId_.size() + this->appLabelId_.size() + this->bundleName_.size() +
48 this->abilityName_.size()) + GetInnerEntriesSize();
49 }
50
GetAppId() const51 std::string SystemDefinedAppItem::GetAppId() const
52 {
53 return this->appId_;
54 }
55
SetAppId(const std::string & appId)56 void SystemDefinedAppItem::SetAppId(const std::string &appId)
57 {
58 this->appId_ = appId;
59 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
60 std::get<std::shared_ptr<Object>>(value_)->value_[APPID] = appId_;
61 }
62 }
63
GetAppName() const64 std::string SystemDefinedAppItem::GetAppName() const
65 {
66 return this->appName_;
67 }
68
SetAppName(const std::string & appName)69 void SystemDefinedAppItem::SetAppName(const std::string &appName)
70 {
71 this->appName_ = appName;
72 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
73 std::get<std::shared_ptr<Object>>(value_)->value_[APPNAME] = appName_;
74 }
75 }
76
GetAppIconId() const77 std::string SystemDefinedAppItem::GetAppIconId() const
78 {
79 return this->appIconId_;
80 }
81
SetAppIconId(const std::string & appIconId)82 void SystemDefinedAppItem::SetAppIconId(const std::string &appIconId)
83 {
84 this->appIconId_ = appIconId;
85 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
86 std::get<std::shared_ptr<Object>>(value_)->value_[APPICONID] = appIconId_;
87 }
88 }
89
GetAppLabelId() const90 std::string SystemDefinedAppItem::GetAppLabelId() const
91 {
92 return this->appLabelId_;
93 }
94
SetAppLabelId(const std::string & appLabelId)95 void SystemDefinedAppItem::SetAppLabelId(const std::string &appLabelId)
96 {
97 this->appLabelId_ = appLabelId;
98 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
99 std::get<std::shared_ptr<Object>>(value_)->value_[APPLABELID] = appLabelId_;
100 }
101 }
102
GetBundleName() const103 std::string SystemDefinedAppItem::GetBundleName() const
104 {
105 return this->bundleName_;
106 }
107
SetBundleName(const std::string & bundleName)108 void SystemDefinedAppItem::SetBundleName(const std::string &bundleName)
109 {
110 this->bundleName_ = bundleName;
111 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
112 std::get<std::shared_ptr<Object>>(value_)->value_[BUNDLENAME] = bundleName_;
113 }
114 }
115
GetAbilityName() const116 std::string SystemDefinedAppItem::GetAbilityName() const
117 {
118 return this->abilityName_;
119 }
120
SetAbilityName(const std::string & abilityName)121 void SystemDefinedAppItem::SetAbilityName(const std::string &abilityName)
122 {
123 this->abilityName_ = abilityName;
124 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
125 std::get<std::shared_ptr<Object>>(value_)->value_[ABILITYNAME] = abilityName_;
126 }
127 }
128
SetItems(UDDetails & details)129 void SystemDefinedAppItem::SetItems(UDDetails &details)
130 {
131 for (auto &item : details) {
132 auto* value = std::get_if<std::string>(&item.second);
133 if (value == nullptr) {
134 continue;
135 }
136 if (item.first == APPID) {
137 SetAppId(*value);
138 }
139 if (item.first == APPNAME) {
140 SetAppName(*value);
141 }
142 if (item.first == APPICONID) {
143 SetAppIconId(*value);
144 }
145 if (item.first == APPLABELID) {
146 SetAppLabelId(*value);
147 }
148 if (item.first == BUNDLENAME) {
149 SetBundleName(*value);
150 }
151 if (item.first == ABILITYNAME) {
152 SetAbilityName(*value);
153 }
154 }
155 }
156
GetItems()157 UDDetails SystemDefinedAppItem::GetItems()
158 {
159 UDDetails items;
160 items[APPID] = GetAppId();
161 items[APPNAME] = GetAppName();
162 items[APPICONID] = GetAppIconId();
163 items[APPLABELID] = GetAppLabelId();
164 items[BUNDLENAME] = GetBundleName();
165 items[ABILITYNAME] = GetAbilityName();
166 return items;
167 }
168
InitObject()169 void SystemDefinedAppItem::InitObject()
170 {
171 if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
172 auto value = value_;
173 value_ = std::make_shared<Object>();
174 auto object = std::get<std::shared_ptr<Object>>(value_);
175 object->value_[UNIFORM_DATA_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(dataType_);
176 object->value_[APPID] = appId_;
177 object->value_[APPNAME] = appName_;
178 object->value_[APPICONID] = appIconId_;
179 object->value_[APPLABELID] = appLabelId_;
180 object->value_[BUNDLENAME] = bundleName_;
181 object->value_[ABILITYNAME] = abilityName_;
182 object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
183 object->value_.insert_or_assign(VALUE_TYPE, std::move(value));
184 }
185 }
186 } // namespace UDMF
187 } // namespace OHOS