use of android.content.res.XmlResourceParser in project MagicaSakura by Bilibili.
the class DrawableUtils method createDrawable.
static Drawable createDrawable(Context context, int resId) {
if (resId <= 0)
return null;
final TypedValue typedValue = new TypedValue();
final Resources res = context.getResources();
res.getValue(resId, typedValue, true);
Drawable dr = null;
if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) {
dr = new ColorDrawable(ThemeUtils.replaceColorById(context, resId));
} else {
try {
if (typedValue.string != null && typedValue.string.toString().endsWith("xml")) {
final XmlResourceParser rp = res.getXml(resId);
final AttributeSet attrs = Xml.asAttributeSet(rp);
int type;
while ((type = rp.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
// Empty loop
}
if (type != XmlPullParser.START_TAG) {
throw new XmlPullParserException("No start tag found");
}
dr = createFromXmlInner(context, rp, attrs);
rp.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
return dr;
}
use of android.content.res.XmlResourceParser in project ActionBarSherlock by JakeWharton.
the class ResourcesCompat method loadLogoFromManifest.
/**
* Attempt to programmatically load the logo from the manifest file of an
* activity by using an XML pull parser. This should allow us to read the
* logo attribute regardless of the platform it is being run on.
*
* @param activity Activity instance.
* @return Logo resource ID.
*/
public static int loadLogoFromManifest(Activity activity) {
int logo = 0;
try {
final String thisPackage = activity.getClass().getName();
if (ActionBarSherlock.DEBUG)
Log.i(TAG, "Parsing AndroidManifest.xml for " + thisPackage);
final String packageName = activity.getApplicationInfo().packageName;
final AssetManager am = activity.createPackageContext(packageName, 0).getAssets();
final XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml");
int eventType = xml.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
String name = xml.getName();
if ("application".equals(name)) {
//Check if the <application> has the attribute
if (ActionBarSherlock.DEBUG)
Log.d(TAG, "Got <application>");
for (int i = xml.getAttributeCount() - 1; i >= 0; i--) {
if (ActionBarSherlock.DEBUG)
Log.d(TAG, xml.getAttributeName(i) + ": " + xml.getAttributeValue(i));
if ("logo".equals(xml.getAttributeName(i))) {
logo = xml.getAttributeResourceValue(i, 0);
//out of for loop
break;
}
}
} else if ("activity".equals(name)) {
//Check if the <activity> is us and has the attribute
if (ActionBarSherlock.DEBUG)
Log.d(TAG, "Got <activity>");
Integer activityLogo = null;
String activityPackage = null;
boolean isOurActivity = false;
for (int i = xml.getAttributeCount() - 1; i >= 0; i--) {
if (ActionBarSherlock.DEBUG)
Log.d(TAG, xml.getAttributeName(i) + ": " + xml.getAttributeValue(i));
//We need both uiOptions and name attributes
String attrName = xml.getAttributeName(i);
if ("logo".equals(attrName)) {
activityLogo = xml.getAttributeResourceValue(i, 0);
} else if ("name".equals(attrName)) {
activityPackage = ActionBarSherlockCompat.cleanActivityName(packageName, xml.getAttributeValue(i));
if (!thisPackage.equals(activityPackage)) {
//on to the next
break;
}
isOurActivity = true;
}
//Make sure we have both attributes before processing
if ((activityLogo != null) && (activityPackage != null)) {
//Our activity, logo specified, override with our value
logo = activityLogo.intValue();
}
}
if (isOurActivity) {
//do any more processing of the manifest
break;
}
}
}
eventType = xml.nextToken();
}
} catch (Exception e) {
e.printStackTrace();
}
if (ActionBarSherlock.DEBUG)
Log.i(TAG, "Returning " + Integer.toHexString(logo));
return logo;
}
use of android.content.res.XmlResourceParser in project ActionBarSherlock by JakeWharton.
the class ActionBarSherlockCompat method loadUiOptionsFromManifest.
private static int loadUiOptionsFromManifest(Activity activity) {
int uiOptions = 0;
try {
final String thisPackage = activity.getClass().getName();
if (ActionBarSherlock.DEBUG)
Log.i(TAG, "Parsing AndroidManifest.xml for " + thisPackage);
final String packageName = activity.getApplicationInfo().packageName;
final AssetManager am = activity.createPackageContext(packageName, 0).getAssets();
final XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml");
int eventType = xml.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
String name = xml.getName();
if ("application".equals(name)) {
//Check if the <application> has the attribute
if (ActionBarSherlock.DEBUG)
Log.d(TAG, "Got <application>");
for (int i = xml.getAttributeCount() - 1; i >= 0; i--) {
if (ActionBarSherlock.DEBUG)
Log.d(TAG, xml.getAttributeName(i) + ": " + xml.getAttributeValue(i));
if ("uiOptions".equals(xml.getAttributeName(i))) {
uiOptions = xml.getAttributeIntValue(i, 0);
//out of for loop
break;
}
}
} else if ("activity".equals(name)) {
//Check if the <activity> is us and has the attribute
if (ActionBarSherlock.DEBUG)
Log.d(TAG, "Got <activity>");
Integer activityUiOptions = null;
String activityPackage = null;
boolean isOurActivity = false;
for (int i = xml.getAttributeCount() - 1; i >= 0; i--) {
if (ActionBarSherlock.DEBUG)
Log.d(TAG, xml.getAttributeName(i) + ": " + xml.getAttributeValue(i));
//We need both uiOptions and name attributes
String attrName = xml.getAttributeName(i);
if ("uiOptions".equals(attrName)) {
activityUiOptions = xml.getAttributeIntValue(i, 0);
} else if ("name".equals(attrName)) {
activityPackage = cleanActivityName(packageName, xml.getAttributeValue(i));
if (!thisPackage.equals(activityPackage)) {
//out of for loop
break;
}
isOurActivity = true;
}
//Make sure we have both attributes before processing
if ((activityUiOptions != null) && (activityPackage != null)) {
//Our activity, uiOptions specified, override with our value
uiOptions = activityUiOptions.intValue();
}
}
if (isOurActivity) {
//do any more processing of the manifest
break;
}
}
}
eventType = xml.nextToken();
}
} catch (Exception e) {
e.printStackTrace();
}
if (ActionBarSherlock.DEBUG)
Log.i(TAG, "Returning " + Integer.toHexString(uiOptions));
return uiOptions;
}
use of android.content.res.XmlResourceParser in project ActionBarSherlock by JakeWharton.
the class MenuInflater method inflate.
/**
* Inflate a menu hierarchy from the specified XML resource. Throws
* {@link InflateException} if there is an error.
*
* @param menuRes Resource ID for an XML layout resource to load (e.g.,
* <code>R.menu.main_activity</code>)
* @param menu The Menu to inflate into. The items and submenus will be
* added to this Menu.
*/
public void inflate(int menuRes, Menu menu) {
XmlResourceParser parser = null;
try {
parser = mContext.getResources().getLayout(menuRes);
AttributeSet attrs = Xml.asAttributeSet(parser);
parseMenu(parser, attrs, menu);
} catch (XmlPullParserException e) {
throw new InflateException("Error inflating menu XML", e);
} catch (IOException e) {
throw new InflateException("Error inflating menu XML", e);
} finally {
if (parser != null)
parser.close();
}
}
use of android.content.res.XmlResourceParser in project android_frameworks_base by DirtyUnicorns.
the class PackageParser method parseBaseApk.
private Package parseBaseApk(File apkFile, AssetManager assets, int flags) throws PackageParserException {
final String apkPath = apkFile.getAbsolutePath();
String volumeUuid = null;
if (apkPath.startsWith(MNT_EXPAND)) {
final int end = apkPath.indexOf('/', MNT_EXPAND.length());
volumeUuid = apkPath.substring(MNT_EXPAND.length(), end);
}
mParseError = PackageManager.INSTALL_SUCCEEDED;
mArchiveSourcePath = apkFile.getAbsolutePath();
if (DEBUG_JAR)
Slog.d(TAG, "Scanning base APK: " + apkPath);
final int cookie = loadApkIntoAssetManager(assets, apkPath, flags);
Resources res = null;
XmlResourceParser parser = null;
try {
res = new Resources(assets, mMetrics, null);
assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Build.VERSION.RESOURCES_SDK_INT);
parser = assets.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);
final String[] outError = new String[1];
final Package pkg = parseBaseApk(res, parser, flags, outError);
if (pkg == null) {
throw new PackageParserException(mParseError, apkPath + " (at " + parser.getPositionDescription() + "): " + outError[0]);
}
pkg.setVolumeUuid(volumeUuid);
pkg.setApplicationVolumeUuid(volumeUuid);
pkg.setBaseCodePath(apkPath);
pkg.setSignatures(null);
return pkg;
} catch (PackageParserException e) {
throw e;
} catch (Exception e) {
throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION, "Failed to read manifest from " + apkPath, e);
} finally {
IoUtils.closeQuietly(parser);
}
}
Aggregations