下面列出了android.content.res.Resources#getResourcePackageName() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public View inflate(int resourceId, ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
final String packageName = res.getResourcePackageName(resourceId);
final String resName = res.getResourceEntryName(resourceId);
final View view = original.inflate(resourceId, root, attachToRoot);
if (!appPackageName.equals(packageName)) {
return view;
}
View targetView = view;
if (root != null && attachToRoot) {
targetView = root.getChildAt(root.getChildCount() - 1);
}
targetView.setTag(TAG_RES_NAME, resName);
return view;
}
private static String getSpokenEmoticonDescription(final Context context,
final String outputText) {
final StringBuilder sb = new StringBuilder(SPOKEN_EMOTICON_RESOURCE_NAME_PREFIX);
final int textLength = outputText.length();
for (int index = 0; index < textLength; index = outputText.offsetByCodePoints(index, 1)) {
final int codePoint = outputText.codePointAt(index);
sb.append(String.format(Locale.ROOT, SPOKEN_EMOTICON_CODE_POINT_FORMAT, codePoint));
}
final String resourceName = sb.toString();
final Resources resources = context.getResources();
// Note that the resource package name may differ from the context package name.
final String resourcePackageName = resources.getResourcePackageName(
R.string.spoken_description_unknown);
final int resId = resources.getIdentifier(resourceName, "string", resourcePackageName);
return (resId == 0) ? null : resources.getString(resId);
}
private NetworkSecurityConfig.Builder parseDebugOverridesResource()
throws IOException, XmlPullParserException, ParserException {
Resources resources = mContext.getResources();
String packageName = resources.getResourcePackageName(mResourceId);
String entryName = resources.getResourceEntryName(mResourceId);
int resId = resources.getIdentifier(entryName + "_debug", "xml", packageName);
// No debug-overrides resource was found, nothing to parse.
if (resId == 0) {
return null;
}
NetworkSecurityConfig.Builder debugConfigBuilder = null;
// Parse debug-overrides out of the _debug resource.
try (XmlResourceParser parser = resources.getXml(resId)) {
XmlUtils.beginDocument(parser, "network-security-config");
int outerDepth = parser.getDepth();
boolean seenDebugOverrides = false;
while (XmlUtils.nextElementWithin(parser, outerDepth)) {
if ("debug-overrides".equals(parser.getName())) {
if (seenDebugOverrides) {
throw new ParserException(parser, "Only one debug-overrides allowed");
}
if (mDebugBuild) {
debugConfigBuilder =
parseConfigEntry(parser, null, null, CONFIG_DEBUG).get(0).first;
} else {
XmlUtils.skipCurrentTag(parser);
}
seenDebugOverrides = true;
} else {
XmlUtils.skipCurrentTag(parser);
}
}
}
return debugConfigBuilder;
}
public void setLocale(final Locale locale, final Context context) {
final Resources res = context.getResources();
// Null means the current system locale.
final String resourcePackageName = res.getResourcePackageName(
context.getApplicationInfo().labelRes);
setLocale(locale, res, resourcePackageName);
}
@Override
protected void setUp() throws Exception {
super.setUp();
final Context targetContext = getContext();
final Resources targetRes = targetContext.getResources();
final String targetPackageName = targetRes.getResourcePackageName(
R.string.english_ime_name);
mTextsSet.setLocale(TEST_LOCALE, targetRes, targetPackageName);
}
public void setLocale(final Locale locale, final Context context) {
final Resources res = context.getResources();
// Null means the current system locale.
final String resourcePackageName = res.getResourcePackageName(
context.getApplicationInfo().labelRes);
setLocale(locale, res, resourcePackageName);
}
public void setLocale(final Locale locale, final Context context) {
final Resources res = context.getResources();
// Null means the current system locale.
final String resourcePackageName = res.getResourcePackageName(
context.getApplicationInfo().labelRes);
setLocale(locale, res, resourcePackageName);
}
private int getSpokenDescriptionId(final Context context, final int code,
final String resourceNameFormat) {
final String resourceName = String.format(Locale.ROOT, resourceNameFormat, code);
final Resources resources = context.getResources();
// Note that the resource package name may differ from the context package name.
final String resourcePackageName = resources.getResourcePackageName(
R.string.spoken_description_unknown);
final int resId = resources.getIdentifier(resourceName, "string", resourcePackageName);
if (resId != 0) {
mKeyCodeMap.append(code, resId);
}
return resId;
}
public void setLocale(final Locale locale, final Context context) {
final Resources res = context.getResources();
// Null means the current system locale.
final String resourcePackageName = res.getResourcePackageName(
context.getApplicationInfo().labelRes);
setLocale(locale, res, resourcePackageName);
}
public static String getIdString(@Nullable Resources r, int resourceId)
throws Resources.NotFoundException {
if (r == null) {
return getFallbackIdString(resourceId);
}
String prefix;
String prefixSeparator;
switch (getResourcePackageId(resourceId)) {
case 0x7f:
prefix = "";
prefixSeparator = "";
break;
default:
prefix = r.getResourcePackageName(resourceId);
prefixSeparator = ":";
break;
}
String typeName = r.getResourceTypeName(resourceId);
String entryName = r.getResourceEntryName(resourceId);
StringBuilder sb = new StringBuilder(
1 + prefix.length() + prefixSeparator.length() +
typeName.length() + 1 + entryName.length());
sb.append("@");
sb.append(prefix);
sb.append(prefixSeparator);
sb.append(typeName);
sb.append("/");
sb.append(entryName);
return sb.toString();
}
public static String getIdString(@Nullable Resources r, int resourceId)
throws Resources.NotFoundException {
if (r == null) {
return getFallbackIdString(resourceId);
}
String prefix;
String prefixSeparator;
switch (getResourcePackageId(resourceId)) {
case 0x7f:
prefix = "";
prefixSeparator = "";
break;
default:
prefix = r.getResourcePackageName(resourceId);
prefixSeparator = ":";
break;
}
String typeName = r.getResourceTypeName(resourceId);
String entryName = r.getResourceEntryName(resourceId);
StringBuilder sb = new StringBuilder(
1 + prefix.length() + prefixSeparator.length() +
typeName.length() + 1 + entryName.length());
sb.append("@");
sb.append(prefix);
sb.append(prefixSeparator);
sb.append(typeName);
sb.append("/");
sb.append(entryName);
return sb.toString();
}
private int getSpokenDescriptionId(final Context context, final int code,
final String resourceNameFormat) {
final String resourceName = String.format(Locale.ROOT, resourceNameFormat, code);
final Resources resources = context.getResources();
// Note that the resource package name may differ from the context package name.
final String resourcePackageName = resources.getResourcePackageName(
R.string.spoken_description_unknown);
final int resId = resources.getIdentifier(resourceName, "string", resourcePackageName);
if (resId != 0) {
mKeyCodeMap.append(code, resId);
}
return resId;
}
private static int getXmlId(final Resources resources, final String keyboardLayoutSetName) {
final String packageName = resources.getResourcePackageName(
R.xml.keyboard_layout_set_qwerty);
return resources.getIdentifier(keyboardLayoutSetName, "xml", packageName);
}
private static int getXmlId(final Resources resources, final String keyboardLayoutSetName) {
final String packageName = resources.getResourcePackageName(
R.xml.keyboard_layout_set_qwerty);
return resources.getIdentifier(keyboardLayoutSetName, "xml", packageName);
}
private static String viewToString(View view) {
StringBuilder out = new StringBuilder(128);
out.append(view.getClass().getName());
out.append('{');
out.append(Integer.toHexString(System.identityHashCode(view)));
out.append(' ');
switch (view.getVisibility()) {
case View.VISIBLE: out.append('V'); break;
case View.INVISIBLE: out.append('I'); break;
case View.GONE: out.append('G'); break;
default: out.append('.'); break;
}
out.append(view.isFocusable() ? 'F' : '.');
out.append(view.isEnabled() ? 'E' : '.');
out.append(view.willNotDraw() ? '.' : 'D');
out.append(view.isHorizontalScrollBarEnabled()? 'H' : '.');
out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
out.append(view.isClickable() ? 'C' : '.');
out.append(view.isLongClickable() ? 'L' : '.');
out.append(' ');
out.append(view.isFocused() ? 'F' : '.');
out.append(view.isSelected() ? 'S' : '.');
out.append(view.isPressed() ? 'P' : '.');
out.append(' ');
out.append(view.getLeft());
out.append(',');
out.append(view.getTop());
out.append('-');
out.append(view.getRight());
out.append(',');
out.append(view.getBottom());
final int id = view.getId();
if (id != View.NO_ID) {
out.append(" #");
out.append(Integer.toHexString(id));
final Resources r = view.getResources();
if (id != 0 && r != null) {
try {
String pkgname;
switch (id&0xff000000) {
case 0x7f000000:
pkgname="app";
break;
case 0x01000000:
pkgname="android";
break;
default:
pkgname = r.getResourcePackageName(id);
break;
}
String typename = r.getResourceTypeName(id);
String entryname = r.getResourceEntryName(id);
out.append(" ");
out.append(pkgname);
out.append(":");
out.append(typename);
out.append("/");
out.append(entryname);
} catch (Resources.NotFoundException e) {
}
}
}
out.append("}");
return out.toString();
}
private static int getXmlId(final Resources resources, final String keyboardLayoutSetName) {
final String packageName = resources.getResourcePackageName(
R.xml.keyboard_layout_set_qwerty);
return resources.getIdentifier(keyboardLayoutSetName, "xml", packageName);
}
private NetworkSecurityConfig.Builder parseDebugOverridesResource()
throws IOException, XmlPullParserException, ParserException {
Resources resources = mContext.getResources();
String packageName = resources.getResourcePackageName(mResourceId);
String entryName = resources.getResourceEntryName(mResourceId);
int resId = resources.getIdentifier(entryName + "_debug", "xml", packageName);
// No debug-overrides resource was found, nothing to parse.
if (resId == 0) {
return null;
}
NetworkSecurityConfig.Builder debugConfigBuilder = null;
// Parse debug-overrides out of the _debug resource.
XmlResourceParser parser=null;
try {
parser = resources.getXml(resId);
XmlUtils.beginDocument(parser, "network-security-config");
int outerDepth = parser.getDepth();
boolean seenDebugOverrides = false;
while (XmlUtils.nextElementWithin(parser, outerDepth)) {
if ("debug-overrides".equals(parser.getName())) {
if (seenDebugOverrides) {
throw new ParserException(parser, "Only one debug-overrides allowed");
}
if (mDebugBuild) {
debugConfigBuilder =
parseConfigEntry(parser, null, null, CONFIG_DEBUG).get(0).first;
} else {
XmlUtils.skipCurrentTag(parser);
}
seenDebugOverrides = true;
} else {
XmlUtils.skipCurrentTag(parser);
}
}
}
finally {
if (parser!=null) parser.close();
}
return debugConfigBuilder;
}
private static String viewToString(View view) {
StringBuilder out = new StringBuilder(128);
out.append(view.getClass().getName());
out.append('{');
out.append(Integer.toHexString(System.identityHashCode(view)));
out.append(' ');
switch (view.getVisibility()) {
case View.VISIBLE: out.append('V'); break;
case View.INVISIBLE: out.append('I'); break;
case View.GONE: out.append('G'); break;
default: out.append('.'); break;
}
out.append(view.isFocusable() ? 'F' : '.');
out.append(view.isEnabled() ? 'E' : '.');
out.append(view.willNotDraw() ? '.' : 'D');
out.append(view.isHorizontalScrollBarEnabled()? 'H' : '.');
out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
out.append(view.isClickable() ? 'C' : '.');
out.append(view.isLongClickable() ? 'L' : '.');
out.append(' ');
out.append(view.isFocused() ? 'F' : '.');
out.append(view.isSelected() ? 'S' : '.');
out.append(view.isPressed() ? 'P' : '.');
out.append(' ');
out.append(view.getLeft());
out.append(',');
out.append(view.getTop());
out.append('-');
out.append(view.getRight());
out.append(',');
out.append(view.getBottom());
final int id = view.getId();
if (id != View.NO_ID) {
out.append(" #");
out.append(Integer.toHexString(id));
final Resources r = view.getResources();
if (id != 0 && r != null) {
try {
String pkgname;
switch (id&0xff000000) {
case 0x7f000000:
pkgname="app";
break;
case 0x01000000:
pkgname="android";
break;
default:
pkgname = r.getResourcePackageName(id);
break;
}
String typename = r.getResourceTypeName(id);
String entryname = r.getResourceEntryName(id);
out.append(" ");
out.append(pkgname);
out.append(":");
out.append(typename);
out.append("/");
out.append(entryname);
} catch (Resources.NotFoundException e) {
}
}
}
out.append("}");
return out.toString();
}
private static String viewToString(View view) {
StringBuilder out = new StringBuilder(128);
out.append(view.getClass().getName());
out.append('{');
out.append(Integer.toHexString(System.identityHashCode(view)));
out.append(' ');
switch (view.getVisibility()) {
case View.VISIBLE: out.append('V'); break;
case View.INVISIBLE: out.append('I'); break;
case View.GONE: out.append('G'); break;
default: out.append('.'); break;
}
out.append(view.isFocusable() ? 'F' : '.');
out.append(view.isEnabled() ? 'E' : '.');
out.append(view.willNotDraw() ? '.' : 'D');
out.append(view.isHorizontalScrollBarEnabled()? 'H' : '.');
out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
out.append(view.isClickable() ? 'C' : '.');
out.append(view.isLongClickable() ? 'L' : '.');
out.append(' ');
out.append(view.isFocused() ? 'F' : '.');
out.append(view.isSelected() ? 'S' : '.');
out.append(view.isPressed() ? 'P' : '.');
out.append(' ');
out.append(view.getLeft());
out.append(',');
out.append(view.getTop());
out.append('-');
out.append(view.getRight());
out.append(',');
out.append(view.getBottom());
final int id = view.getId();
if (id != View.NO_ID) {
out.append(" #");
out.append(Integer.toHexString(id));
final Resources r = view.getResources();
if (id != 0 && r != null) {
try {
String pkgname;
switch (id&0xff000000) {
case 0x7f000000:
pkgname="app";
break;
case 0x01000000:
pkgname="android";
break;
default:
pkgname = r.getResourcePackageName(id);
break;
}
String typename = r.getResourceTypeName(id);
String entryname = r.getResourceEntryName(id);
out.append(" ");
out.append(pkgname);
out.append(":");
out.append(typename);
out.append("/");
out.append(entryname);
} catch (Resources.NotFoundException e) {
}
}
}
out.append("}");
return out.toString();
}
/**
* Create a unique id for a given Resource and id.
*
* @param resources Resources instance
* @param id a resource id
* @param style style to be used for this resource, -1 if not available.
* @return Unique id for a given resource and id.
*/
private static String createResourceUid(final Resources resources, int id, boolean italic, int fontWeight) {
return resources.getResourcePackageName(id) + "-" + id + "-" + italic + "-" + fontWeight;
}