| 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.ResResSpec; |
| 21 |
import brut.androlib.res.data.ResResource; |
| 22 |
import brut.util.Duo; |
| 23 |
import org.xmlpull.v1.XmlSerializer; |
| 24 |
|
| 25 |
import java.io.IOException; |
| 26 |
import java.util.HashMap; |
| 27 |
import java.util.Map; |
| 28 |
|
| 29 |
public class ResEnumAttr extends ResAttr { |
| 30 |
ResEnumAttr(ResReferenceValue parent, int type, Integer min, Integer max, |
| 31 |
Boolean l10n, Duo<ResReferenceValue, ResIntValue>[] items) { |
| 32 |
super(parent, type, min, max, l10n); |
| 33 |
mItems = items; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public String convertToResXmlFormat(ResScalarValue value) |
| 38 |
throws AndrolibException { |
| 39 |
if (value instanceof ResIntValue) { |
| 40 |
String ret = decodeValue(((ResIntValue) value).getValue()); |
| 41 |
if (ret != null) { |
| 42 |
return ret; |
| 43 |
} |
| 44 |
} |
| 45 |
return super.convertToResXmlFormat(value); |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
protected void serializeBody(XmlSerializer serializer, ResResource res) |
| 50 |
throws AndrolibException, IOException { |
| 51 |
for (Duo<ResReferenceValue, ResIntValue> duo : mItems) { |
| 52 |
int intVal = duo.m2.getValue(); |
| 53 |
ResResSpec m1Referent = duo.m1.getReferent(); |
| 54 |
|
| 55 |
serializer.startTag(null, "enum"); |
| 56 |
serializer.attribute(null, "name", |
| 57 |
m1Referent != null ? m1Referent.getName() : "@null" |
| 58 |
); |
| 59 |
serializer.attribute(null, "value", String.valueOf(intVal)); |
| 60 |
serializer.endTag(null, "enum"); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
private String decodeValue(int value) throws AndrolibException { |
| 65 |
String value2 = mItemsCache.get(value); |
| 66 |
if (value2 == null) { |
| 67 |
ResReferenceValue ref = null; |
| 68 |
for (Duo<ResReferenceValue, ResIntValue> duo : mItems) { |
| 69 |
if (duo.m2.getValue() == value) { |
| 70 |
ref = duo.m1; |
| 71 |
break; |
| 72 |
} |
| 73 |
} |
| 74 |
if (ref != null && !ref.referentIsNull()) { |
| 75 |
value2 = ref.getReferent().getName(); |
| 76 |
mItemsCache.put(value, value2); |
| 77 |
} |
| 78 |
} |
| 79 |
return value2; |
| 80 |
} |
| 81 |
|
| 82 |
private final Duo<ResReferenceValue, ResIntValue>[] mItems; |
| 83 |
private final Map<Integer, String> mItemsCache = new HashMap<>(); |
| 84 |
} |