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(APP_ID, appId_);
31         object->GetValue(APP_NAME, appName_);
32         object->GetValue(APP_ICON_ID, appIconId_);
33         object->GetValue(APP_LABEL_ID, appLabelId_);
34         object->GetValue(BUNDLE_NAME, bundleName_);
35         object->GetValue(ABILITY_NAME, 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 UnifiedDataUtils::GetDetailsSize(this->details_) + this->appId_.size() + this->appName_.size()
47            + this->appIconId_.size() + this->appLabelId_.size() + this->bundleName_.size() + this->abilityName_.size();
48 }
49 
GetAppId() const50 std::string SystemDefinedAppItem::GetAppId() const
51 {
52     return this->appId_;
53 }
54 
SetAppId(const std::string & appId)55 void SystemDefinedAppItem::SetAppId(const std::string &appId)
56 {
57     this->appId_ = appId;
58     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
59         std::get<std::shared_ptr<Object>>(value_)->value_[APP_ID] = appId_;
60     }
61 }
62 
GetAppName() const63 std::string SystemDefinedAppItem::GetAppName() const
64 {
65     return this->appName_;
66 }
67 
SetAppName(const std::string & appName)68 void SystemDefinedAppItem::SetAppName(const std::string &appName)
69 {
70     this->appName_ = appName;
71     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
72         std::get<std::shared_ptr<Object>>(value_)->value_[APP_NAME] = appName_;
73     }
74 }
75 
GetAppIconId() const76 std::string SystemDefinedAppItem::GetAppIconId() const
77 {
78     return this->appIconId_;
79 }
80 
SetAppIconId(const std::string & appIconId)81 void SystemDefinedAppItem::SetAppIconId(const std::string &appIconId)
82 {
83     this->appIconId_ = appIconId;
84     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
85         std::get<std::shared_ptr<Object>>(value_)->value_[APP_ICON_ID] = appIconId_;
86     }
87 }
88 
GetAppLabelId() const89 std::string SystemDefinedAppItem::GetAppLabelId() const
90 {
91     return this->appLabelId_;
92 }
93 
SetAppLabelId(const std::string & appLabelId)94 void SystemDefinedAppItem::SetAppLabelId(const std::string &appLabelId)
95 {
96     this->appLabelId_ = appLabelId;
97     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
98         std::get<std::shared_ptr<Object>>(value_)->value_[APP_LABEL_ID] = appLabelId_;
99     }
100 }
101 
GetBundleName() const102 std::string SystemDefinedAppItem::GetBundleName() const
103 {
104     return this->bundleName_;
105 }
106 
SetBundleName(const std::string & bundleName)107 void SystemDefinedAppItem::SetBundleName(const std::string &bundleName)
108 {
109     this->bundleName_ = bundleName;
110     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
111         std::get<std::shared_ptr<Object>>(value_)->value_[BUNDLE_NAME] = bundleName_;
112     }
113 }
114 
GetAbilityName() const115 std::string SystemDefinedAppItem::GetAbilityName() const
116 {
117     return this->abilityName_;
118 }
119 
SetAbilityName(const std::string & abilityName)120 void SystemDefinedAppItem::SetAbilityName(const std::string &abilityName)
121 {
122     this->abilityName_ = abilityName;
123     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
124         std::get<std::shared_ptr<Object>>(value_)->value_[ABILITY_NAME] = abilityName_;
125     }
126 }
127 
SetItems(UDDetails & details)128 void SystemDefinedAppItem::SetItems(UDDetails &details)
129 {
130     for (auto &item : details) {
131         auto* value = std::get_if<std::string>(&item.second);
132         if (value == nullptr) {
133             continue;
134         }
135         if (item.first == APP_ID) {
136             SetAppId(*value);
137         }
138         if (item.first == APP_NAME) {
139             SetAppName(*value);
140         }
141         if (item.first == APP_ICON_ID) {
142             SetAppIconId(*value);
143         }
144         if (item.first == APP_LABEL_ID) {
145             SetAppLabelId(*value);
146         }
147         if (item.first == BUNDLE_NAME) {
148             SetBundleName(*value);
149         }
150         if (item.first == ABILITY_NAME) {
151             SetAbilityName(*value);
152         }
153     }
154 }
155 
GetItems()156 UDDetails SystemDefinedAppItem::GetItems()
157 {
158     UDDetails items;
159     items[APP_ID] = GetAppId();
160     items[APP_NAME] = GetAppName();
161     items[APP_ICON_ID] = GetAppIconId();
162     items[APP_LABEL_ID] = GetAppLabelId();
163     items[BUNDLE_NAME] = GetBundleName();
164     items[ABILITY_NAME] = GetAbilityName();
165     return items;
166 }
167 
InitObject()168 void SystemDefinedAppItem::InitObject()
169 {
170     if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
171         auto value = value_;
172         value_ = std::make_shared<Object>();
173         auto object = std::get<std::shared_ptr<Object>>(value_);
174         object->value_[UNIFORM_DATA_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(dataType_);
175         object->value_[APP_ID] = appId_;
176         object->value_[APP_NAME] = appName_;
177         object->value_[APP_ICON_ID] = appIconId_;
178         object->value_[APP_LABEL_ID] = appLabelId_;
179         object->value_[BUNDLE_NAME] = bundleName_;
180         object->value_[ABILITY_NAME] = abilityName_;
181         object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
182         object->value_[VALUE_TYPE] = value;
183     }
184 }
185 } // namespace UDMF
186 } // namespace OHOS