| 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.ResResource; |
| 21 |
import brut.androlib.res.xml.ResValuesXmlSerializable; |
| 22 |
import brut.util.Duo; |
| 23 |
import org.xmlpull.v1.XmlSerializer; |
| 24 |
|
| 25 |
import java.io.IOException; |
| 26 |
import java.util.Arrays; |
| 27 |
|
| 28 |
public class ResArrayValue extends ResBagValue implements |
| 29 |
ResValuesXmlSerializable { |
| 30 |
|
| 31 |
ResArrayValue(ResReferenceValue parent, Duo<Integer, ResScalarValue>[] items) { |
| 32 |
super(parent); |
| 33 |
|
| 34 |
mItems = new ResScalarValue[items.length]; |
| 35 |
for (int i = 0; i < items.length; i++) { |
| 36 |
mItems[i] = items[i].m2; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
public ResArrayValue(ResReferenceValue parent, ResScalarValue[] items) { |
| 41 |
super(parent); |
| 42 |
mItems = items; |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public void serializeToResValuesXml(XmlSerializer serializer, |
| 47 |
ResResource res) throws IOException, AndrolibException { |
| 48 |
String type = getType(); |
| 49 |
type = (type == null ? "" : type + "-") + "array"; |
| 50 |
serializer.startTag(null, type); |
| 51 |
serializer.attribute(null, "name", res.getResSpec().getName()); |
| 52 |
|
| 53 |
// lets check if we need to add formatted="false" to this array |
| 54 |
for (ResScalarValue item : mItems) { |
| 55 |
if (item.hasMultipleNonPositionalSubstitutions()) { |
| 56 |
serializer.attribute(null, "formatted", "false"); |
| 57 |
break; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// add <item>'s |
| 62 |
for (ResScalarValue mItem : mItems) { |
| 63 |
serializer.startTag(null, "item"); |
| 64 |
serializer.text(mItem.encodeAsResXmlNonEscapedItemValue()); |
| 65 |
serializer.endTag(null, "item"); |
| 66 |
} |
| 67 |
serializer.endTag(null, type); |
| 68 |
} |
| 69 |
|
| 70 |
public String getType() throws AndrolibException { |
| 71 |
if (mItems.length == 0) { |
| 72 |
return null; |
| 73 |
} |
| 74 |
String type = mItems[0].getType(); |
| 75 |
for (ResScalarValue mItem : mItems) { |
| 76 |
if (mItem.encodeAsResXmlItemValue().startsWith("@string")) { |
| 77 |
return "string"; |
| 78 |
} else if (mItem.encodeAsResXmlItemValue().startsWith("@drawable")) { |
| 79 |
return null; |
| 80 |
} else if (mItem.encodeAsResXmlItemValue().startsWith("@integer")) { |
| 81 |
return "integer"; |
| 82 |
} else if (!"string".equals(type) && !"integer".equals(type)) { |
| 83 |
return null; |
| 84 |
} else if (!type.equals(mItem.getType())) { |
| 85 |
return null; |
| 86 |
} |
| 87 |
} |
| 88 |
if (!Arrays.asList(AllowedArrayTypes).contains(type)) { |
| 89 |
return "string"; |
| 90 |
} |
| 91 |
return type; |
| 92 |
} |
| 93 |
|
| 94 |
private final ResScalarValue[] mItems; |
| 95 |
private final String[] AllowedArrayTypes = {"string", "integer"}; |
| 96 |
|
| 97 |
public static final int BAG_KEY_ARRAY_START = 0x02000000; |
| 98 |
} |