| 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 org.apache.commons.lang3.StringUtils; |
| 22 |
|
| 23 |
import java.util.LinkedHashMap; |
| 24 |
import java.util.LinkedHashSet; |
| 25 |
import java.util.Map; |
| 26 |
import java.util.Set; |
| 27 |
|
| 28 |
public class ResResSpec { |
| 29 |
private final ResID mId; |
| 30 |
private final String mName; |
| 31 |
private final ResPackage mPackage; |
| 32 |
private final ResTypeSpec mType; |
| 33 |
private final Map<ResConfigFlags, ResResource> mResources = new LinkedHashMap<>(); |
| 34 |
|
| 35 |
public ResResSpec(ResID id, String name, ResPackage pkg, ResTypeSpec type) { |
| 36 |
this.mId = id; |
| 37 |
String cleanName; |
| 38 |
|
| 39 |
name = (("(name removed)".equals(name)) ? null : name); |
| 40 |
|
| 41 |
ResResSpec resResSpec = type.getResSpecUnsafe(name); |
| 42 |
if (resResSpec != null) { |
| 43 |
cleanName = String.format("APKTOOL_DUPLICATE_%s_%s", type, id.toString()); |
| 44 |
} else { |
| 45 |
cleanName = ((name == null || name.isEmpty()) ? ("APKTOOL_DUMMYVAL_" + id.toString()) : name); |
| 46 |
} |
| 47 |
|
| 48 |
this.mName = cleanName; |
| 49 |
this.mPackage = pkg; |
| 50 |
this.mType = type; |
| 51 |
} |
| 52 |
|
| 53 |
public Set<ResResource> listResources() { |
| 54 |
return new LinkedHashSet<>(mResources.values()); |
| 55 |
} |
| 56 |
|
| 57 |
public ResResource getResource(ResType config) throws AndrolibException { |
| 58 |
return getResource(config.getFlags()); |
| 59 |
} |
| 60 |
|
| 61 |
public ResResource getResource(ResConfigFlags config) throws AndrolibException { |
| 62 |
ResResource res = mResources.get(config); |
| 63 |
if (res == null) { |
| 64 |
throw new UndefinedResObjectException(String.format("resource: spec=%s, config=%s", this, config)); |
| 65 |
} |
| 66 |
return res; |
| 67 |
} |
| 68 |
|
| 69 |
public ResResource getDefaultResource() throws AndrolibException { |
| 70 |
return getResource(new ResConfigFlags()); |
| 71 |
} |
| 72 |
|
| 73 |
public boolean hasDefaultResource() { |
| 74 |
return mResources.containsKey(new ResConfigFlags()); |
| 75 |
} |
| 76 |
|
| 77 |
public String getFullName(ResPackage relativeToPackage, boolean excludeType) { |
| 78 |
return getFullName(getPackage().equals(relativeToPackage), excludeType); |
| 79 |
} |
| 80 |
|
| 81 |
public String getFullName(boolean excludePackage, boolean excludeType) { |
| 82 |
return (excludePackage ? "" : getPackage().getName() + ":") |
| 83 |
+ (excludeType ? "" : getType().getName() + "/") + getName(); |
| 84 |
} |
| 85 |
|
| 86 |
public ResID getId() { |
| 87 |
return mId; |
| 88 |
} |
| 89 |
|
| 90 |
public String getName() { |
| 91 |
return StringUtils.replace(mName, "\"", "q"); |
| 92 |
} |
| 93 |
|
| 94 |
public ResPackage getPackage() { |
| 95 |
return mPackage; |
| 96 |
} |
| 97 |
|
| 98 |
public ResTypeSpec getType() { |
| 99 |
return mType; |
| 100 |
} |
| 101 |
|
| 102 |
public boolean isDummyResSpec() { |
| 103 |
return getName().startsWith("APKTOOL_DUMMY_"); |
| 104 |
} |
| 105 |
|
| 106 |
public void addResource(ResResource res) throws AndrolibException { |
| 107 |
addResource(res, false); |
| 108 |
} |
| 109 |
|
| 110 |
public void addResource(ResResource res, boolean overwrite) throws AndrolibException { |
| 111 |
ResConfigFlags flags = res.getConfig().getFlags(); |
| 112 |
if (mResources.put(flags, res) != null && !overwrite) { |
| 113 |
throw new AndrolibException(String.format("Multiple resources: spec=%s, config=%s", this, flags)); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public String toString() { |
| 119 |
return mId.toString() + " " + mType.toString() + "/" + mName; |
| 120 |
} |
| 121 |
} |