Debian Codegraph @

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResAttrDecoder.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.decoder;
18
19 import brut.androlib.AndrolibException;
20 import brut.androlib.err.UndefinedResObjectException;
21 import brut.androlib.res.data.ResID;
22 import brut.androlib.res.data.ResPackage;
23 import brut.androlib.res.data.ResResSpec;
24 import brut.androlib.res.data.value.ResAttr;
25 import brut.androlib.res.data.value.ResScalarValue;
26
27 public class ResAttrDecoder {
28 public String decode(int type, int value, String rawValue, int attrResId)
29 throws AndrolibException {
30 ResScalarValue resValue = mCurrentPackage.getValueFactory().factory(
31 type, value, rawValue);
32
33 String decoded = null;
34 if (attrResId > 0) {
35 try {
36 ResAttr attr = (ResAttr) getCurrentPackage().getResTable()
37 .getResSpec(attrResId).getDefaultResource().getValue();
38
39 decoded = attr.convertToResXmlFormat(resValue);
40 } catch (UndefinedResObjectException | ClassCastException ex) {
41 // ignored
42 }
43 }
44
45 return decoded != null ? decoded : resValue.encodeAsResXmlAttr();
46 }
47
48 public String decodeManifestAttr(int attrResId)
49 throws AndrolibException {
50
51 if (attrResId != 0) {
52 int attrId = attrResId;
53
54 // See also: brut.androlib.res.data.ResTable.getResSpec
55 if (attrId >> 24 == 0) {
56 ResPackage pkg = getCurrentPackage();
57 int packageId = pkg.getId();
58 int pkgId = (packageId == 0 ? 2 : packageId);
59 attrId = (0xFF000000 & (pkgId << 24)) | attrId;
60 }
61
62 // Retrieve the ResSpec in a package by id
63 ResID resId = new ResID(attrId);
64 ResPackage pkg = getCurrentPackage();
65 if (pkg.hasResSpec(resId)) {
66 ResResSpec resResSpec = pkg.getResSpec(resId);
67 if (resResSpec != null) {
68 return resResSpec.getName();
69 }
70 }
71 }
72
73 return null;
74 }
75
76 public ResPackage getCurrentPackage() throws AndrolibException {
77 if (mCurrentPackage == null) {
78 throw new AndrolibException("Current package not set");
79 }
80 return mCurrentPackage;
81 }
82
83 public void setCurrentPackage(ResPackage currentPackage) {
84 mCurrentPackage = currentPackage;
85 }
86
87 private ResPackage mCurrentPackage;
88 }