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 "resource_info.h"
17
18 #include "json_util.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 constexpr const char* SEPARATOR = "/";
24 constexpr const char* UNDER_LINE = "_";
25 constexpr const char* EXTENSION_ABILITY_SEPARATOR = "+";
26 }
27
ResourceInfo()28 ResourceInfo::ResourceInfo()
29 {}
30
~ResourceInfo()31 ResourceInfo::~ResourceInfo()
32 {}
33
GetKey() const34 std::string ResourceInfo::GetKey() const
35 {
36 std::string key = bundleName_;
37 /**
38 * if moduleName and abilityName both empty, it represents bundle resource,
39 * otherwise it represents launcher ability resource.
40 */
41 if (!abilityName_.empty()) {
42 key = moduleName_.empty() ? key : (key + SEPARATOR + moduleName_);
43 key = abilityName_.empty() ? key : (key + SEPARATOR + abilityName_);
44 }
45 if (appIndex_ > 0) {
46 key = std::to_string(appIndex_) + UNDER_LINE + key;
47 }
48 if (extensionAbilityType_ >= 0) {
49 key = key + EXTENSION_ABILITY_SEPARATOR + std::to_string(extensionAbilityType_);
50 }
51 return key;
52 }
53
ParseKey(const std::string & key)54 void ResourceInfo::ParseKey(const std::string &key)
55 {
56 std::string baseKey = key;
57 auto plusPos = key.find_last_of(EXTENSION_ABILITY_SEPARATOR);
58 if (plusPos != std::string::npos) {
59 std::string extensionTypeStr = key.substr(plusPos + 1);
60 if (!OHOS::StrToInt(extensionTypeStr, extensionAbilityType_)) {
61 extensionAbilityType_ = -1;
62 }
63 baseKey = key.substr(0, plusPos);
64 }
65 auto firstPos = baseKey.find_first_of(SEPARATOR);
66 if (firstPos == std::string::npos) {
67 InnerParseAppIndex(baseKey);
68 moduleName_ = std::string();
69 abilityName_ = std::string();
70 return;
71 }
72 InnerParseAppIndex(baseKey.substr(0, firstPos));
73 auto lastPos = baseKey.find_last_of(SEPARATOR);
74 abilityName_ = baseKey.substr(lastPos + 1);
75 if (firstPos != lastPos) {
76 moduleName_ = baseKey.substr(firstPos + 1, lastPos - firstPos - 1);
77 return;
78 }
79 moduleName_ = std::string();
80 }
81
ConvertFromBundleResourceInfo(const BundleResourceInfo & bundleResourceInfo)82 void ResourceInfo::ConvertFromBundleResourceInfo(const BundleResourceInfo &bundleResourceInfo)
83 {
84 bundleName_ = bundleResourceInfo.bundleName;
85 moduleName_ = std::string();
86 abilityName_ = std::string();
87 icon_ = bundleResourceInfo.icon;
88 foreground_ = bundleResourceInfo.foreground;
89 background_ = bundleResourceInfo.background;
90 appIndex_ = bundleResourceInfo.appIndex;
91 if (appIndex_ > 0) {
92 label_ = bundleResourceInfo.label + std::to_string(appIndex_);
93 } else {
94 label_ = bundleResourceInfo.label;
95 }
96 }
97
ConvertFromLauncherAbilityResourceInfo(const LauncherAbilityResourceInfo & launcherAbilityResourceInfo)98 void ResourceInfo::ConvertFromLauncherAbilityResourceInfo(
99 const LauncherAbilityResourceInfo &launcherAbilityResourceInfo)
100 {
101 bundleName_ = launcherAbilityResourceInfo.bundleName;
102 moduleName_ = launcherAbilityResourceInfo.moduleName;
103 abilityName_ = launcherAbilityResourceInfo.abilityName;
104 icon_ = launcherAbilityResourceInfo.icon;
105 foreground_ = launcherAbilityResourceInfo.foreground;
106 background_ = launcherAbilityResourceInfo.background;
107 appIndex_ = launcherAbilityResourceInfo.appIndex;
108 if (appIndex_ > 0) {
109 label_ = launcherAbilityResourceInfo.label + std::to_string(appIndex_);
110 } else {
111 label_ = launcherAbilityResourceInfo.label;
112 }
113 extensionAbilityType_ = launcherAbilityResourceInfo.extensionAbilityType;
114 }
115
InnerParseAppIndex(const std::string & key)116 void ResourceInfo::InnerParseAppIndex(const std::string &key)
117 {
118 bundleName_ = key;
119 appIndex_ = 0;
120 auto pos = key.find(UNDER_LINE);
121 if ((pos == std::string::npos) || (pos == 0)) {
122 return;
123 }
124 std::string index = key.substr(0, pos);
125 if (!OHOS::StrToInt(index, appIndex_)) {
126 appIndex_ = 0;
127 return;
128 }
129 bundleName_ = key.substr(pos + 1);
130 }
131 } // AppExecFwk
132 } // OHOS
133