| 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.util.Duo; |
| 22 |
import org.xmlpull.v1.XmlSerializer; |
| 23 |
|
| 24 |
import java.io.IOException; |
| 25 |
import java.util.Arrays; |
| 26 |
|
| 27 |
public class ResFlagsAttr extends ResAttr { |
| 28 |
ResFlagsAttr(ResReferenceValue parent, int type, Integer min, Integer max, |
| 29 |
Boolean l10n, Duo<ResReferenceValue, ResIntValue>[] items) { |
| 30 |
super(parent, type, min, max, l10n); |
| 31 |
|
| 32 |
mItems = new FlagItem[items.length]; |
| 33 |
for (int i = 0; i < items.length; i++) { |
| 34 |
mItems[i] = new FlagItem(items[i].m1, items[i].m2.getValue()); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public String convertToResXmlFormat(ResScalarValue value) |
| 40 |
throws AndrolibException { |
| 41 |
if(value instanceof ResReferenceValue) { |
| 42 |
return value.encodeAsResXml(); |
| 43 |
} |
| 44 |
if (!(value instanceof ResIntValue)) { |
| 45 |
return super.convertToResXmlFormat(value); |
| 46 |
} |
| 47 |
loadFlags(); |
| 48 |
int intVal = ((ResIntValue) value).getValue(); |
| 49 |
|
| 50 |
if (intVal == 0) { |
| 51 |
return renderFlags(mZeroFlags); |
| 52 |
} |
| 53 |
|
| 54 |
FlagItem[] flagItems = new FlagItem[mFlags.length]; |
| 55 |
int[] flags = new int[mFlags.length]; |
| 56 |
int flagsCount = 0; |
| 57 |
for (FlagItem flagItem : mFlags) { |
| 58 |
int flag = flagItem.flag; |
| 59 |
|
| 60 |
if ((intVal & flag) != flag) { |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
if (!isSubpartOf(flag, flags)) { |
| 65 |
flags[flagsCount] = flag; |
| 66 |
flagItems[flagsCount++] = flagItem; |
| 67 |
} |
| 68 |
} |
| 69 |
return renderFlags(Arrays.copyOf(flagItems, flagsCount)); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
protected void serializeBody(XmlSerializer serializer, ResResource res) |
| 74 |
throws AndrolibException, IOException { |
| 75 |
for (FlagItem item : mItems) { |
| 76 |
serializer.startTag(null, "flag"); |
| 77 |
serializer.attribute(null, "name", item.getValue()); |
| 78 |
serializer.attribute(null, "value", |
| 79 |
String.format("0x%08x", item.flag)); |
| 80 |
serializer.endTag(null, "flag"); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
private boolean isSubpartOf(int flag, int[] flags) { |
| 85 |
for (int j : flags) { |
| 86 |
if ((j & flag) == flag) { |
| 87 |
return true; |
| 88 |
} |
| 89 |
} |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
private String renderFlags(FlagItem[] flags) throws AndrolibException { |
| 94 |
StringBuilder ret = new StringBuilder(); |
| 95 |
for (FlagItem flag : flags) { |
| 96 |
ret.append("|").append(flag.getValue()); |
| 97 |
} |
| 98 |
if (ret.length() == 0) { |
| 99 |
return ret.toString(); |
| 100 |
} |
| 101 |
return ret.substring(1); |
| 102 |
} |
| 103 |
|
| 104 |
private void loadFlags() { |
| 105 |
if (mFlags != null) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
FlagItem[] zeroFlags = new FlagItem[mItems.length]; |
| 110 |
int zeroFlagsCount = 0; |
| 111 |
FlagItem[] flags = new FlagItem[mItems.length]; |
| 112 |
int flagsCount = 0; |
| 113 |
|
| 114 |
for (FlagItem item : mItems) { |
| 115 |
if (item.flag == 0) { |
| 116 |
zeroFlags[zeroFlagsCount++] = item; |
| 117 |
} else { |
| 118 |
flags[flagsCount++] = item; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
mZeroFlags = Arrays.copyOf(zeroFlags, zeroFlagsCount); |
| 123 |
mFlags = Arrays.copyOf(flags, flagsCount); |
| 124 |
|
| 125 |
Arrays.sort(mFlags, (o1, o2) -> Integer.compare(Integer.bitCount(o2.flag), Integer.bitCount(o1.flag))); |
| 126 |
} |
| 127 |
|
| 128 |
private final FlagItem[] mItems; |
| 129 |
|
| 130 |
private FlagItem[] mZeroFlags; |
| 131 |
private FlagItem[] mFlags; |
| 132 |
|
| 133 |
private static class FlagItem { |
| 134 |
public final ResReferenceValue ref; |
| 135 |
public final int flag; |
| 136 |
public String value; |
| 137 |
|
| 138 |
public FlagItem(ResReferenceValue ref, int flag) { |
| 139 |
this.ref = ref; |
| 140 |
this.flag = flag; |
| 141 |
} |
| 142 |
|
| 143 |
public String getValue() throws AndrolibException { |
| 144 |
if (value == null) { |
| 145 |
if (ref.referentIsNull()) { |
| 146 |
return "@null"; |
| 147 |
} |
| 148 |
value = ref.getReferent().getName(); |
| 149 |
} |
| 150 |
return value; |
| 151 |
} |
| 152 |
} |
| 153 |
} |