| 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; |
| 18 |
|
| 19 |
import brut.androlib.AndrolibException; |
| 20 |
import brut.androlib.err.UndefinedResObjectException; |
| 21 |
import brut.androlib.res.data.value.ResFileValue; |
| 22 |
import brut.androlib.res.data.value.ResValueFactory; |
| 23 |
import brut.androlib.res.xml.ResValuesXmlSerializable; |
| 24 |
import brut.util.Duo; |
| 25 |
import java.util.*; |
| 26 |
import java.util.logging.Logger; |
| 27 |
|
| 28 |
public class ResPackage { |
| 29 |
private final ResTable mResTable; |
| 30 |
private final int mId; |
| 31 |
private final String mName; |
| 32 |
private final Map<ResID, ResResSpec> mResSpecs = new LinkedHashMap<>(); |
| 33 |
private final Map<ResConfigFlags, ResType> mConfigs = new LinkedHashMap<>(); |
| 34 |
private final Map<String, ResTypeSpec> mTypes = new LinkedHashMap<>(); |
| 35 |
private final Set<ResID> mSynthesizedRes = new HashSet<>(); |
| 36 |
|
| 37 |
private ResValueFactory mValueFactory; |
| 38 |
|
| 39 |
public ResPackage(ResTable resTable, int id, String name) { |
| 40 |
this.mResTable = resTable; |
| 41 |
this.mId = id; |
| 42 |
this.mName = name; |
| 43 |
} |
| 44 |
|
| 45 |
public List<ResResSpec> listResSpecs() { |
| 46 |
return new ArrayList<>(mResSpecs.values()); |
| 47 |
} |
| 48 |
|
| 49 |
public boolean hasResSpec(ResID resID) { |
| 50 |
return mResSpecs.containsKey(resID); |
| 51 |
} |
| 52 |
|
| 53 |
public ResResSpec getResSpec(ResID resID) throws UndefinedResObjectException { |
| 54 |
ResResSpec spec = mResSpecs.get(resID); |
| 55 |
if (spec == null) { |
| 56 |
throw new UndefinedResObjectException("resource spec: " + resID.toString()); |
| 57 |
} |
| 58 |
return spec; |
| 59 |
} |
| 60 |
|
| 61 |
public int getResSpecCount() { |
| 62 |
return mResSpecs.size(); |
| 63 |
} |
| 64 |
|
| 65 |
public ResType getOrCreateConfig(ResConfigFlags flags) { |
| 66 |
ResType config = mConfigs.get(flags); |
| 67 |
if (config == null) { |
| 68 |
config = new ResType(flags); |
| 69 |
mConfigs.put(flags, config); |
| 70 |
} |
| 71 |
return config; |
| 72 |
} |
| 73 |
|
| 74 |
public ResTypeSpec getType(String typeName) throws AndrolibException { |
| 75 |
ResTypeSpec type = mTypes.get(typeName); |
| 76 |
if (type == null) { |
| 77 |
throw new UndefinedResObjectException("type: " + typeName); |
| 78 |
} |
| 79 |
return type; |
| 80 |
} |
| 81 |
|
| 82 |
public Set<ResResource> listFiles() { |
| 83 |
Set<ResResource> ret = new HashSet<>(); |
| 84 |
for (ResResSpec spec : mResSpecs.values()) { |
| 85 |
for (ResResource res : spec.listResources()) { |
| 86 |
if (res.getValue() instanceof ResFileValue) { |
| 87 |
ret.add(res); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
return ret; |
| 92 |
} |
| 93 |
|
| 94 |
public Collection<ResValuesFile> listValuesFiles() { |
| 95 |
Map<Duo<ResTypeSpec, ResType>, ResValuesFile> ret = new HashMap<>(); |
| 96 |
for (ResResSpec spec : mResSpecs.values()) { |
| 97 |
for (ResResource res : spec.listResources()) { |
| 98 |
if (res.getValue() instanceof ResValuesXmlSerializable) { |
| 99 |
ResTypeSpec type = res.getResSpec().getType(); |
| 100 |
ResType config = res.getConfig(); |
| 101 |
Duo<ResTypeSpec, ResType> key = new Duo<>(type, config); |
| 102 |
ResValuesFile values = ret.get(key); |
| 103 |
if (values == null) { |
| 104 |
values = new ResValuesFile(this, type, config); |
| 105 |
ret.put(key, values); |
| 106 |
} |
| 107 |
values.addResource(res); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
return ret.values(); |
| 112 |
} |
| 113 |
|
| 114 |
public ResTable getResTable() { |
| 115 |
return mResTable; |
| 116 |
} |
| 117 |
|
| 118 |
public int getId() { |
| 119 |
return mId; |
| 120 |
} |
| 121 |
|
| 122 |
public String getName() { |
| 123 |
return mName; |
| 124 |
} |
| 125 |
|
| 126 |
boolean isSynthesized(ResID resId) { |
| 127 |
return mSynthesizedRes.contains(resId); |
| 128 |
} |
| 129 |
|
| 130 |
public void removeResSpec(ResResSpec spec) { |
| 131 |
mResSpecs.remove(spec.getId()); |
| 132 |
} |
| 133 |
|
| 134 |
public void addResSpec(ResResSpec spec) throws AndrolibException { |
| 135 |
if (mResSpecs.put(spec.getId(), spec) != null) { |
| 136 |
throw new AndrolibException("Multiple resource specs: " + spec); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
public void addType(ResTypeSpec type) { |
| 141 |
if (mTypes.containsKey(type.getName())) { |
| 142 |
LOGGER.warning("Multiple types detected! " + type + " ignored!"); |
| 143 |
} else { |
| 144 |
mTypes.put(type.getName(), type); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public void addSynthesizedRes(int resId) { |
| 149 |
mSynthesizedRes.add(new ResID(resId)); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public String toString() { |
| 154 |
return mName; |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public boolean equals(Object obj) { |
| 159 |
if (obj == null) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
if (getClass() != obj.getClass()) { |
| 163 |
return false; |
| 164 |
} |
| 165 |
final ResPackage other = (ResPackage) obj; |
| 166 |
if (!Objects.equals(this.mResTable, other.mResTable)) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
return this.mId == other.mId; |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public int hashCode() { |
| 174 |
int hash = 17; |
| 175 |
hash = 31 * hash + (this.mResTable != null ? this.mResTable.hashCode() : 0); |
| 176 |
hash = 31 * hash + this.mId; |
| 177 |
return hash; |
| 178 |
} |
| 179 |
|
| 180 |
public ResValueFactory getValueFactory() { |
| 181 |
if (mValueFactory == null) { |
| 182 |
mValueFactory = new ResValueFactory(this); |
| 183 |
} |
| 184 |
return mValueFactory; |
| 185 |
} |
| 186 |
|
| 187 |
private final static Logger LOGGER = Logger.getLogger(ResPackage.class.getName()); |
| 188 |
} |