下面列出了android.util.TypedValue#coerceToString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public String getAttributeValue(int index) {
int offset = getAttributeOffset(index);
int valueType = m_attributes[offset + ATTRIBUTE_IX_VALUE_TYPE];
int valueData = m_attributes[offset + ATTRIBUTE_IX_VALUE_DATA];
int valueRaw = m_attributes[offset + ATTRIBUTE_IX_VALUE_STRING];
if (mAttrDecoder != null) {
try {
return mAttrDecoder.decode(
valueType,
valueData,
valueRaw == -1 ? null : ResXmlEncoders.escapeXmlChars(m_strings.getString(valueRaw)),
getAttributeNameResource(index));
} catch (AndrolibException ex) {
setFirstError(ex);
LOGGER.log(Level.WARNING, String.format("Could not decode attr value, using undecoded value "
+ "instead: ns=%s, name=%s, value=0x%08x",
getAttributePrefix(index),
getAttributeName(index),
valueData), ex);
}
}
return TypedValue.coerceToString(valueType, valueData);
}
/**
* Retrieves the string value associated with a particular resource
* identifier for the current configuration.
*
* @param resId the resource identifier to load
* @param bagEntryId the index into the bag to load
* @return the string value, or {@code null}
*/
@Nullable CharSequence getResourceBagText(@StringRes int resId, int bagEntryId) {
synchronized (this) {
ensureValidLocked();
final TypedValue outValue = mValue;
final int cookie = nativeGetResourceBagValue(mObject, resId, bagEntryId, outValue);
if (cookie <= 0) {
return null;
}
// Convert the changing configurations flags populated by native code.
outValue.changingConfigurations = ActivityInfo.activityInfoConfigNativeToJava(
outValue.changingConfigurations);
if (outValue.type == TypedValue.TYPE_STRING) {
return mApkAssets[cookie - 1].getStringFromPool(outValue.data);
}
return outValue.coerceToString();
}
}
private static boolean checkFullScreenByTheme(Context context) {
Resources.Theme theme = context.getTheme();
if (theme != null) {
TypedValue typedValue = new TypedValue();
boolean result = theme.resolveAttribute(android.R.attr.windowFullscreen, typedValue, false);
if (result) {
typedValue.coerceToString();
if (typedValue.type == TypedValue.TYPE_INT_BOOLEAN) {
return typedValue.data != 0;
}
}
}
return false;
}
private boolean parsePackageItemInfo(DynamicApkInfo owner, PackageItemInfo outInfo,
String[] outError, String tag, TypedArray sa,
int nameRes, int labelRes, int iconRes, int logoRes, int bannerRes) {
String name = sa.getString(nameRes);
if (name == null) {
outError[0] = tag + " does not specify android:name";
return false;
}
outInfo.name
= buildClassName(owner.applicationInfo.packageName, name, outError);
if (outInfo.name == null) {
return false;
}
int iconVal = sa.getResourceId(iconRes, 0);
if (iconVal != 0) {
outInfo.icon = iconVal;
outInfo.nonLocalizedLabel = null;
}
int logoVal = sa.getResourceId(logoRes, 0);
if (logoVal != 0) {
outInfo.logo = logoVal;
}
int bannerVal = sa.getResourceId(bannerRes, 0);
if (bannerVal != 0) {
outInfo.banner = bannerVal;
}
TypedValue v = sa.peekValue(labelRes);
if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
outInfo.nonLocalizedLabel = v.coerceToString();
}
outInfo.packageName = owner.packageName;
return true;
}
public Component(final ParsePackageItemArgs args, final PackageItemInfo outInfo) {
owner = args.owner;
intents = new ArrayList<II>(0);
String name = args.sa.getNonConfigurationString(args.nameRes, 0);
if (name == null) {
className = null;
args.outError[0] = args.tag + " does not specify android:name";
return;
}
outInfo.name
= buildClassName(owner.applicationInfo.packageName, name, args.outError);
if (outInfo.name == null) {
className = null;
args.outError[0] = args.tag + " does not have valid android:name";
return;
}
className = outInfo.name;
int iconVal = args.sa.getResourceId(args.iconRes, 0);
if (iconVal != 0) {
outInfo.icon = iconVal;
outInfo.nonLocalizedLabel = null;
}
int logoVal = args.sa.getResourceId(args.logoRes, 0);
if (logoVal != 0) {
outInfo.logo = logoVal;
}
TypedValue v = args.sa.peekValue(args.labelRes);
if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
outInfo.nonLocalizedLabel = v.coerceToString();
}
outInfo.packageName = owner.packageName;
}
public static boolean checkFullScreenByTheme(Context context) {
Resources.Theme theme = context.getTheme();
if (theme != null) {
TypedValue typedValue = new TypedValue();
boolean result = theme.resolveAttribute(android.R.attr.windowFullscreen, typedValue, false);
if (result) {
typedValue.coerceToString();
if (typedValue.type == TypedValue.TYPE_INT_BOOLEAN) {
return typedValue.data != 0;
}
}
}
return false;
}
private boolean parsePackageItemInfo(Package owner, PackageItemInfo outInfo,
String[] outError, String tag, TypedArray sa,
int nameRes, int labelRes, int iconRes, int logoRes) {
String name = sa.getNonConfigurationString(nameRes, 0);
if (name == null) {
outError[0] = tag + " does not specify android:name";
return false;
}
outInfo.name
= buildClassName(owner.applicationInfo.packageName, name, outError);
if (outInfo.name == null) {
return false;
}
int iconVal = sa.getResourceId(iconRes, 0);
if (iconVal != 0) {
outInfo.icon = iconVal;
outInfo.nonLocalizedLabel = null;
}
int logoVal = sa.getResourceId(logoRes, 0);
if (logoVal != 0) {
outInfo.logo = logoVal;
}
TypedValue v = sa.peekValue(labelRes);
if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
outInfo.nonLocalizedLabel = v.coerceToString();
}
outInfo.packageName = owner.packageName;
return true;
}
public Component(final ParsePackageItemArgs args, final PackageItemInfo outInfo) {
owner = args.owner;
intents = new ArrayList<II>(0);
String name = args.sa.getNonConfigurationString(args.nameRes, 0);
if (name == null) {
className = null;
args.outError[0] = args.tag + " does not specify android:name";
return;
}
outInfo.name
= buildClassName(owner.applicationInfo.packageName, name, args.outError);
if (outInfo.name == null) {
className = null;
args.outError[0] = args.tag + " does not have valid android:name";
return;
}
className = outInfo.name;
int iconVal = args.sa.getResourceId(args.iconRes, 0);
if (iconVal != 0) {
outInfo.icon = iconVal;
outInfo.nonLocalizedLabel = null;
}
int logoVal = args.sa.getResourceId(args.logoRes, 0);
if (logoVal != 0) {
outInfo.logo = logoVal;
}
TypedValue v = args.sa.peekValue(args.labelRes);
if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
outInfo.nonLocalizedLabel = v.coerceToString();
}
outInfo.packageName = owner.packageName;
}
public String getAttributeValue(int index) {
int id = nativeGetAttributeStringValue(mParseState, index);
if (DEBUG) System.out.println("getAttributeValue of " + index + " = " + id);
if (id >= 0) return mStrings.get(id).toString();
// May be some other type... check and try to convert if so.
int t = nativeGetAttributeDataType(mParseState, index);
if (t == TypedValue.TYPE_NULL) {
throw new IndexOutOfBoundsException(String.valueOf(index));
}
int v = nativeGetAttributeData(mParseState, index);
return TypedValue.coerceToString(t, v);
}
/**
* Retrieves the string value for the attribute at <var>index</var> that is
* not allowed to change with the given configurations.
*
* @param index Index of attribute to retrieve.
* @param allowedChangingConfigs Bit mask of configurations from
* {@link Configuration}.NATIVE_CONFIG_* that are allowed to change.
*
* @return String holding string data. Any styling information is removed.
* Returns {@code null} if the attribute is not defined.
* @throws RuntimeException if the TypedArray has already been recycled.
* @hide
*/
public String getNonConfigurationString(@StyleableRes int index,
@Config int allowedChangingConfigs) {
if (mRecycled) {
throw new RuntimeException("Cannot make calls to a recycled instance!");
}
index *= STYLE_NUM_ENTRIES;
final int[] data = mData;
final int type = data[index + STYLE_TYPE];
final @Config int changingConfigs = ActivityInfo.activityInfoConfigNativeToJava(
data[index + STYLE_CHANGING_CONFIGURATIONS]);
if ((changingConfigs & ~allowedChangingConfigs) != 0) {
return null;
}
if (type == TypedValue.TYPE_NULL) {
return null;
} else if (type == TypedValue.TYPE_STRING) {
return loadStringValueAt(index).toString();
}
final TypedValue v = mValue;
if (getValueAt(index, v)) {
final CharSequence cs = v.coerceToString();
return cs != null ? cs.toString() : null;
}
// We already checked for TYPE_NULL. This should never happen.
throw new RuntimeException("getNonConfigurationString of bad type: 0x"
+ Integer.toHexString(type));
}
/**
* Retrieve the float value for the attribute at <var>index</var>.
* <p>
* If the attribute is not a float or an integer, this method will attempt
* to coerce it to a float using {@link Float#parseFloat(String)}.
*
* @param index Index of attribute to retrieve.
*
* @return Attribute float value, or defValue if the attribute was
* not defined or could not be coerced to a float.
* @throws RuntimeException if the TypedArray has already been recycled.
*/
public float getFloat(@StyleableRes int index, float defValue) {
if (mRecycled) {
throw new RuntimeException("Cannot make calls to a recycled instance!");
}
index *= STYLE_NUM_ENTRIES;
final int[] data = mData;
final int type = data[index + STYLE_TYPE];
if (type == TypedValue.TYPE_NULL) {
return defValue;
} else if (type == TypedValue.TYPE_FLOAT) {
return Float.intBitsToFloat(data[index + STYLE_DATA]);
} else if (type >= TypedValue.TYPE_FIRST_INT
&& type <= TypedValue.TYPE_LAST_INT) {
return data[index + STYLE_DATA];
}
final TypedValue v = mValue;
if (getValueAt(index, v)) {
final CharSequence str = v.coerceToString();
if (str != null) {
StrictMode.noteResourceMismatch(v);
return Float.parseFloat(str.toString());
}
}
// We already checked for TYPE_NULL. This should never happen.
throw new RuntimeException("getFloat of bad type: 0x" + Integer.toHexString(type));
}
/**
* Retrieves the string value associated with a particular resource
* identifier for the current configuration.
*
* @param resId the resource identifier to load
* @return the string value, or {@code null}
*/
@Nullable CharSequence getResourceText(@StringRes int resId) {
synchronized (this) {
final TypedValue outValue = mValue;
if (getResourceValue(resId, 0, outValue, true)) {
return outValue.coerceToString();
}
return null;
}
}
private void parseStartTagChunk() {
log("\nparse Start Tag Chunk");
log("chunk type: 0x%x", Xml.START_TAG_CHUNK_TYPE);
try {
int chunkSize = reader.readInt();
log("chunk size: %d", chunkSize);
int lineNumber = reader.readInt();
log("line number: %d", lineNumber);
reader.skip(4); // 0xffffffff
int namespaceUri = reader.readInt();
if (namespaceUri == -1)
log("namespace uri: null");
else
log("namespace uri: %s", stringChunkList.get(namespaceUri));
int name = reader.readInt();
log("name: %s", stringChunkList.get(name));
reader.skip(4); // flag 0x00140014
int attributeCount = reader.readInt();
log("attributeCount: %d", attributeCount);
int classAttribute = reader.readInt();
log("class attribute: %s", classAttribute);
List<Attribute> attributes = new ArrayList<>();
for (int i = 0; i < attributeCount; i++) {
log("Attribute[%d]", i);
int namespaceUriAttr = reader.readInt();
if (namespaceUriAttr == -1)
log(" namespace uri: null");
else
log(" namespace uri: %s", stringChunkList.get(namespaceUriAttr));
int nameAttr = reader.readInt();
if (nameAttr == -1)
log(" name: null");
else
log(" name: %s", stringChunkList.get(nameAttr));
int valueStr = reader.readInt();
if (valueStr == -1)
log(" valueStr: null");
else
log(" valueStr: %s", stringChunkList.get(valueStr));
int type = reader.readInt() >> 24;
log(" type: %d", type);
int data = reader.readInt();
String dataString = type == TypedValue.TYPE_STRING ? stringChunkList.get(data) : TypedValue.coerceToString(type, data);
log(" data: %s", dataString);
Attribute attribute = new Attribute(namespaceUriAttr == -1 ? null : stringChunkList.get(namespaceUriAttr),
stringChunkList.get(nameAttr), valueStr, type, dataString);
attributes.add(attribute);
}
StartTagChunk startTagChunk = new StartTagChunk(namespaceUri, stringChunkList.get(name), attributes);
chunkList.add(startTagChunk);
} catch (IOException e) {
e.printStackTrace();
log("parse Start NameSpace Chunk error!");
}
}
public Component(final ParsePackageItemArgs args, final PackageItemInfo outInfo) {
owner = args.owner;
intents = new ArrayList<II>(0);
String name = args.sa.getString(args.nameRes);
if (name == null) {
className = null;
args.outError[0] = args.tag + " does not specify android:name";
return;
}
outInfo.name
= buildClassName(owner.applicationInfo.packageName, name, args.outError);
if (outInfo.name == null) {
className = null;
args.outError[0] = args.tag + " does not have valid android:name";
return;
}
className = outInfo.name;
int iconVal = args.sa.getResourceId(args.iconRes, 0);
if (iconVal != 0) {
outInfo.icon = iconVal;
outInfo.nonLocalizedLabel = null;
}
int logoVal = args.sa.getResourceId(args.logoRes, 0);
if (logoVal != 0) {
outInfo.logo = logoVal;
}
// int bannerVal = args.sa.getResourceId(args.bannerRes, 0);
// if (bannerVal != 0) {
// outInfo.banner = bannerVal;
// }
TypedValue v = args.sa.peekValue(args.labelRes);
if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
outInfo.nonLocalizedLabel = v.coerceToString();
}
outInfo.packageName = owner.packageName;
}
@Override
protected String encodeAsResXml() throws AndrolibException {
return TypedValue.coerceToString(TypedValue.TYPE_FRACTION, mValue);
}
@Override
protected String encodeAsResValue() throws IOException {
return TypedValue.coerceToString(type, mValue);
}
@Override
protected String encodeAsResValue() throws IOException {
return TypedValue.coerceToString(type, mValue);
}
@Override
protected String encodeAsResValue() {
return TypedValue.coerceToString(TypedValue.TYPE_DIMENSION, mValue);
}
@Override
protected String encodeAsResValue() {
return TypedValue.coerceToString(TypedValue.TYPE_FRACTION, mValue);
}
/**
* Parse a name/value pair out of an XML tag holding that data. The
* AttributeSet must be holding the data defined by
* {@link android.R.styleable#Extra}. The following value types are supported:
* <ul>
* <li> {@link TypedValue#TYPE_STRING}:
* {@link Bundle#putCharSequence Bundle.putCharSequence()}
* <li> {@link TypedValue#TYPE_INT_BOOLEAN}:
* {@link Bundle#putCharSequence Bundle.putBoolean()}
* <li> {@link TypedValue#TYPE_FIRST_INT}-{@link TypedValue#TYPE_LAST_INT}:
* {@link Bundle#putCharSequence Bundle.putBoolean()}
* <li> {@link TypedValue#TYPE_FLOAT}:
* {@link Bundle#putCharSequence Bundle.putFloat()}
* </ul>
*
* @param tagName The name of the tag these attributes come from; this is
* only used for reporting error messages.
* @param attrs The attributes from which to retrieve the name/value pair.
* @param outBundle The Bundle in which to place the parsed value.
* @throws XmlPullParserException If the attributes are not valid.
*/
public void parseBundleExtra(String tagName, AttributeSet attrs,
Bundle outBundle) throws XmlPullParserException {
TypedArray sa = obtainAttributes(attrs,
com.android.internal.R.styleable.Extra);
String name = sa.getString(
com.android.internal.R.styleable.Extra_name);
if (name == null) {
sa.recycle();
throw new XmlPullParserException("<" + tagName
+ "> requires an android:name attribute at "
+ attrs.getPositionDescription());
}
TypedValue v = sa.peekValue(
com.android.internal.R.styleable.Extra_value);
if (v != null) {
if (v.type == TypedValue.TYPE_STRING) {
CharSequence cs = v.coerceToString();
outBundle.putCharSequence(name, cs);
} else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
outBundle.putBoolean(name, v.data != 0);
} else if (v.type >= TypedValue.TYPE_FIRST_INT
&& v.type <= TypedValue.TYPE_LAST_INT) {
outBundle.putInt(name, v.data);
} else if (v.type == TypedValue.TYPE_FLOAT) {
outBundle.putFloat(name, v.getFloat());
} else {
sa.recycle();
throw new XmlPullParserException("<" + tagName
+ "> only supports string, integer, float, color, and boolean at "
+ attrs.getPositionDescription());
}
} else {
sa.recycle();
throw new XmlPullParserException("<" + tagName
+ "> requires an android:value or android:resource attribute at "
+ attrs.getPositionDescription());
}
sa.recycle();
}