Debian Codegraph @

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/data/value/ResAttr.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.value;
18
19 import brut.androlib.AndrolibException;
20 import brut.androlib.res.data.ResPackage;
21 import brut.androlib.res.data.ResResource;
22 import brut.androlib.res.xml.ResValuesXmlSerializable;
23 import brut.util.Duo;
24 import org.xmlpull.v1.XmlSerializer;
25
26 import java.io.IOException;
27
28 public class ResAttr extends ResBagValue implements ResValuesXmlSerializable {
29 ResAttr(ResReferenceValue parentVal, int type, Integer min, Integer max,
30 Boolean l10n) {
31 super(parentVal);
32 mType = type;
33 mMin = min;
34 mMax = max;
35 mL10n = l10n;
36 }
37
38 public String convertToResXmlFormat(ResScalarValue value) throws AndrolibException {
39 return null;
40 }
41
42 @Override
43 public void serializeToResValuesXml(XmlSerializer serializer, ResResource res)
44 throws IOException, AndrolibException {
45 String type = getTypeAsString();
46
47 serializer.startTag(null, "attr");
48 serializer.attribute(null, "name", res.getResSpec().getName());
49 if (type != null) {
50 serializer.attribute(null, "format", type);
51 }
52 if (mMin != null) {
53 serializer.attribute(null, "min", mMin.toString());
54 }
55 if (mMax != null) {
56 serializer.attribute(null, "max", mMax.toString());
57 }
58 if (mL10n != null && mL10n) {
59 serializer.attribute(null, "localization", "suggested");
60 }
61 serializeBody(serializer, res);
62 serializer.endTag(null, "attr");
63 }
64
65 public static ResAttr factory(ResReferenceValue parent,
66 Duo<Integer, ResScalarValue>[] items, ResValueFactory factory,
67 ResPackage pkg) throws AndrolibException {
68
69 int type = ((ResIntValue) items[0].m2).getValue();
70 int scalarType = type & 0xffff;
71 Integer min = null, max = null;
72 Boolean l10n = null;
73 int i;
74 for (i = 1; i < items.length; i++) {
75 switch (items[i].m1) {
76 case BAG_KEY_ATTR_MIN:
77 min = ((ResIntValue) items[i].m2).getValue();
78 continue;
79 case BAG_KEY_ATTR_MAX:
80 max = ((ResIntValue) items[i].m2).getValue();
81 continue;
82 case BAG_KEY_ATTR_L10N:
83 l10n = ((ResIntValue) items[i].m2).getValue() != 0;
84 continue;
85 }
86 break;
87 }
88
89 if (i == items.length) {
90 return new ResAttr(parent, scalarType, min, max, l10n);
91 }
92 Duo<ResReferenceValue, ResIntValue>[] attrItems = new Duo[items.length - i];
93 int j = 0;
94 for (; i < items.length; i++) {
95 int resId = items[i].m1;
96 pkg.addSynthesizedRes(resId);
97 attrItems[j++] = new Duo<>(factory.newReference(resId, null), (ResIntValue) items[i].m2);
98 }
99 switch (type & 0xff0000) {
100 case TYPE_ENUM:
101 return new ResEnumAttr(parent, scalarType, min, max, l10n, attrItems);
102 case TYPE_FLAGS:
103 return new ResFlagsAttr(parent, scalarType, min, max, l10n, attrItems);
104 }
105
106 throw new AndrolibException("Could not decode attr value");
107 }
108
109 protected void serializeBody(XmlSerializer serializer, ResResource res) throws AndrolibException, IOException {
110 }
111
112 protected String getTypeAsString() {
113 String s = "";
114 if ((mType & TYPE_REFERENCE) != 0) {
115 s += "|reference";
116 }
117 if ((mType & TYPE_STRING) != 0) {
118 s += "|string";
119 }
120 if ((mType & TYPE_INT) != 0) {
121 s += "|integer";
122 }
123 if ((mType & TYPE_BOOL) != 0) {
124 s += "|boolean";
125 }
126 if ((mType & TYPE_COLOR) != 0) {
127 s += "|color";
128 }
129 if ((mType & TYPE_FLOAT) != 0) {
130 s += "|float";
131 }
132 if ((mType & TYPE_DIMEN) != 0) {
133 s += "|dimension";
134 }
135 if ((mType & TYPE_FRACTION) != 0) {
136 s += "|fraction";
137 }
138 if (s.isEmpty()) {
139 return null;
140 }
141 return s.substring(1);
142 }
143
144 private final int mType;
145 private final Integer mMin;
146 private final Integer mMax;
147 private final Boolean mL10n;
148
149 public static final int BAG_KEY_ATTR_TYPE = 0x01000000;
150 private static final int BAG_KEY_ATTR_MIN = 0x01000001;
151 private static final int BAG_KEY_ATTR_MAX = 0x01000002;
152 private static final int BAG_KEY_ATTR_L10N = 0x01000003;
153
154 private final static int TYPE_REFERENCE = 0x01;
155 private final static int TYPE_STRING = 0x02;
156 private final static int TYPE_INT = 0x04;
157 private final static int TYPE_BOOL = 0x08;
158 private final static int TYPE_COLOR = 0x10;
159 private final static int TYPE_FLOAT = 0x20;
160 private final static int TYPE_DIMEN = 0x40;
161 private final static int TYPE_FRACTION = 0x80;
162 private final static int TYPE_ANY_STRING = 0xee;
163
164 private static final int TYPE_ENUM = 0x00010000;
165 private static final int TYPE_FLAGS = 0x00020000;
166 }