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