Debian Codegraph @

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/data/ResTable.java

raw
1 /*
2 * Copyright (C) 2010 Ryszard Wiśniewski <[email protected]>
3 * Copyright (C) 2010 Connor Tumbleson <[email protected]>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package brut.androlib.res.data;
18
19 import brut.androlib.AndrolibException;
20 import brut.androlib.err.UndefinedResObjectException;
21 import brut.androlib.meta.VersionInfo;
22 import brut.androlib.res.AndrolibResources;
23 import brut.androlib.res.data.value.ResValue;
24 import java.util.*;
25
26 public class ResTable {
27 private final AndrolibResources mAndRes;
28
29 private final Map<Integer, ResPackage> mPackagesById = new HashMap<>();
30 private final Map<String, ResPackage> mPackagesByName = new HashMap<>();
31 private final Set<ResPackage> mMainPackages = new LinkedHashSet<>();
32 private final Set<ResPackage> mFramePackages = new LinkedHashSet<>();
33
34 private String mPackageRenamed;
35 private String mPackageOriginal;
36 private int mPackageId;
37 private boolean mAnalysisMode = false;
38 private boolean mSharedLibrary = false;
39 private boolean mSparseResources = false;
40
41 private final Map<String, String> mSdkInfo = new LinkedHashMap<>();
42 private final VersionInfo mVersionInfo = new VersionInfo();
43
44 public ResTable() {
45 mAndRes = null;
46 }
47
48 public ResTable(AndrolibResources andRes) {
49 mAndRes = andRes;
50 }
51
52 public ResResSpec getResSpec(int resID) throws AndrolibException {
53 // The pkgId is 0x00. That means a shared library is using its
54 // own resource, so lie to the caller replacing with its own
55 // packageId
56 if (resID >> 24 == 0) {
57 int pkgId = (mPackageId == 0 ? 2 : mPackageId);
58 resID = (0xFF000000 & (pkgId << 24)) | resID;
59 }
60 return getResSpec(new ResID(resID));
61 }
62
63 public ResResSpec getResSpec(ResID resID) throws AndrolibException {
64 return getPackage(resID.package_).getResSpec(resID);
65 }
66
67 public Set<ResPackage> listMainPackages() {
68 return mMainPackages;
69 }
70
71 public Set<ResPackage> listFramePackages() {
72 return mFramePackages;
73 }
74
75 public ResPackage getPackage(int id) throws AndrolibException {
76 ResPackage pkg = mPackagesById.get(id);
77 if (pkg != null) {
78 return pkg;
79 }
80 if (mAndRes != null) {
81 return mAndRes.loadFrameworkPkg(this, id, mAndRes.buildOptions.frameworkTag);
82 }
83 throw new UndefinedResObjectException(String.format("package: id=%d", id));
84 }
85
86 public ResPackage getHighestSpecPackage() throws AndrolibException {
87 int id = 0;
88 int value = 0;
89 for (ResPackage resPackage : mPackagesById.values()) {
90 if (resPackage.getResSpecCount() > value && !resPackage.getName().equalsIgnoreCase("android")) {
91 value = resPackage.getResSpecCount();
92 id = resPackage.getId();
93 }
94 }
95 // if id is still 0, we only have one pkgId which is "android" -> 1
96 return (id == 0) ? getPackage(1) : getPackage(id);
97 }
98
99 public ResPackage getCurrentResPackage() throws AndrolibException {
100 ResPackage pkg = mPackagesById.get(mPackageId);
101
102 if (pkg != null) {
103 return pkg;
104 } else {
105 if (mMainPackages.size() == 1) {
106 return mMainPackages.iterator().next();
107 }
108 return getHighestSpecPackage();
109 }
110 }
111
112 public ResPackage getPackage(String name) throws AndrolibException {
113 ResPackage pkg = mPackagesByName.get(name);
114 if (pkg == null) {
115 throw new UndefinedResObjectException("package: name=" + name);
116 }
117 return pkg;
118 }
119
120 public ResValue getValue(String package_, String type, String name) throws AndrolibException {
121 return getPackage(package_).getType(type).getResSpec(name).getDefaultResource().getValue();
122 }
123
124 public void addPackage(ResPackage pkg, boolean main) throws AndrolibException {
125 Integer id = pkg.getId();
126 if (mPackagesById.containsKey(id)) {
127 throw new AndrolibException("Multiple packages: id=" + id);
128 }
129 String name = pkg.getName();
130 if (mPackagesByName.containsKey(name)) {
131 throw new AndrolibException("Multiple packages: name=" + name);
132 }
133
134 mPackagesById.put(id, pkg);
135 mPackagesByName.put(name, pkg);
136 if (main) {
137 mMainPackages.add(pkg);
138 } else {
139 mFramePackages.add(pkg);
140 }
141 }
142
143 public void setAnalysisMode(boolean mode) {
144 mAnalysisMode = mode;
145 }
146
147 public void setPackageRenamed(String pkg) {
148 mPackageRenamed = pkg;
149 }
150
151 public void setPackageOriginal(String pkg) {
152 mPackageOriginal = pkg;
153 }
154
155 public void setPackageId(int id) {
156 mPackageId = id;
157 }
158
159 public void setSharedLibrary(boolean flag) {
160 mSharedLibrary = flag;
161 }
162
163 public void setSparseResources(boolean flag) {
164 mSparseResources = flag;
165 }
166
167 public void clearSdkInfo() {
168 mSdkInfo.clear();
169 }
170
171 public void addSdkInfo(String key, String value) {
172 mSdkInfo.put(key, value);
173 }
174
175 public void setVersionName(String versionName) {
176 mVersionInfo.versionName = versionName;
177 }
178
179 public void setVersionCode(String versionCode) {
180 mVersionInfo.versionCode = versionCode;
181 }
182
183 public VersionInfo getVersionInfo() {
184 return mVersionInfo;
185 }
186
187 public Map<String, String> getSdkInfo() {
188 return mSdkInfo;
189 }
190
191 public boolean getAnalysisMode() {
192 return mAnalysisMode;
193 }
194
195 public String getPackageRenamed() {
196 return mPackageRenamed;
197 }
198
199 public String getPackageOriginal() {
200 return mPackageOriginal;
201 }
202
203 public int getPackageId() {
204 return mPackageId;
205 }
206
207 public boolean getSharedLibrary() {
208 return mSharedLibrary;
209 }
210
211 public boolean getSparseResources() {
212 return mSparseResources;
213 }
214 }