下面列出了android.content.res.XmlResourceParser#getName() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void parseKeyboardLayoutSet(final Resources res, final int resId)
throws XmlPullParserException, IOException {
final XmlResourceParser parser = res.getXml(resId);
try {
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
final int event = parser.next();
if (event == XmlPullParser.START_TAG) {
final String tag = parser.getName();
if (TAG_KEYBOARD_SET.equals(tag)) {
parseKeyboardLayoutSetContent(parser);
} else {
throw new XmlParseUtils.IllegalStartTag(parser, tag, TAG_KEYBOARD_SET);
}
}
}
} finally {
parser.close();
}
}
@Override
public long parseAndAdd(XmlResourceParser parser) throws XmlPullParserException,
IOException {
final int groupDepth = parser.getDepth();
int type;
long addedId = -1;
while ((type = parser.next()) != XmlPullParser.END_TAG ||
parser.getDepth() > groupDepth) {
if (type != XmlPullParser.START_TAG || addedId > -1) {
continue;
}
final String fallback_item_name = parser.getName();
if (TAG_FAVORITE.equals(fallback_item_name)) {
addedId = mChildParser.parseAndAdd(parser);
}
}
return addedId;
}
/**
* ファイル保存場所が記載されているxmlを解析する.
* @param xpp xmlパーサ
* @return 解析結果
* @throws XmlPullParserException xmlの解析に失敗した場合
* @throws IOException xmlファイルの読み込みに失敗した場合
*/
private static FileLocation parseFilePath(final XmlResourceParser xpp)
throws XmlPullParserException, IOException {
FileLocation location = new FileLocation();
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
final String name = xpp.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (TAG_EXTERNAL_PATH.equals(name)) {
location.mType = TYPE_EXTERNAL_PATH;
location.mPath = xpp.getAttributeValue(null, ATTR_PATH);
} else if (TAG_INTERNAL_PATH.equals(name)) {
location.mType = TYPE_INTERNAL_PATH;
location.mPath = xpp.getAttributeValue(null, ATTR_PATH);
}
break;
case XmlPullParser.END_TAG:
break;
default:
break;
}
eventType = xpp.next();
}
return location;
}
public static String getSettingsActivity(Context context, ServiceInfo si)
throws XmlPullParserException, IOException {
PackageManager pm = context.getPackageManager();
XmlResourceParser parser = null;
try {
parser = si.loadXmlMetaData(pm, RecognitionService.SERVICE_META_DATA);
if (parser == null) {
throw new XmlPullParserException("No " + RecognitionService.SERVICE_META_DATA + " meta-data");
}
int type;
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
&& type != XmlPullParser.START_TAG) {
}
String nodeName = parser.getName();
if (!"recognition-service".equals(nodeName)) {
throw new XmlPullParserException(
"Meta-data does not start with recognition-service tag");
}
return parser.getAttributeValue("http://schemas.android.com/apk/res/android",
"settingsActivity");
} finally {
if (parser != null) parser.close();
}
}
private void readUuidData(XmlResourceParser xpp) throws XmlPullParserException, IOException {
xpp.next();
String tagName = null;
String uuid = null;
String descr = null;
String icon = null;
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
// do nothing
} else if (eventType == XmlPullParser.START_TAG) {
tagName = xpp.getName();
uuid = xpp.getAttributeValue(null, "uuid");
descr = xpp.getAttributeValue(null, "descr");
icon = xpp.getAttributeValue(null,"icon");
} else if (eventType == XmlPullParser.END_TAG) {
// do nothing
} else if (eventType == XmlPullParser.TEXT) {
if (tagName.equalsIgnoreCase("item")) {
if (!uuid.isEmpty()) {
uuid = uuid.replace("0x", "");
mNameMap.put(uuid, xpp.getText());
mDescrMap.put(uuid, descr);
mIconMap.put(uuid, icon);
}
}
}
eventType = xpp.next();
}
}
/**
* 解析Application节点
*/
private void parseApplication(XmlResourceParser parser) throws IOException, XmlPullParserException {
String name = parser.getAttributeValue(ANDROID_RESOURCES, "name");
applicationClass = buildClassName(pkg, name);
final int innerDepth = parser.getDepth();
int type;
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
&& (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
String tagName = parser.getName();
if (tagName.equals("activity")) {
// Activity Info
ComponentBean activity = parseComponent(parser);
activities.add(activity);
} else if (tagName.equals("receiver")) {
// Recevier Info
ComponentBean receiver = parseComponent(parser);
receivers.add(receiver);
} else if (tagName.equals("service")) {
// Service Info
ComponentBean service = parseComponent(parser);
services.add(service);
} else if (tagName.equals("provider")) {
// Providers
ComponentBean provider = parseComponent(parser);
providers.add(provider);
}
}
}
private static String addIntentFilter(HashMap<String, ArrayList<PluginIntentFilter>> map, String packageName, String namespace,
XmlResourceParser parser, String endTagName) throws XmlPullParserException, IOException {
int eventType = parser.getEventType();
String activityName = parser.getAttributeValue(namespace, "name");
activityName = getName(activityName, packageName);
ArrayList<PluginIntentFilter> filters = map.get(activityName);
if (filters == null) {
filters = new ArrayList<PluginIntentFilter>();
map.put(activityName, filters);
}
PluginIntentFilter intentFilter = new PluginIntentFilter();
do {
switch (eventType) {
case XmlPullParser.START_TAG: {
String tag = parser.getName();
if ("intent-filter".equals(tag)) {
intentFilter = new PluginIntentFilter();
filters.add(intentFilter);
} else {
intentFilter.readFromXml(tag, namespace, parser);
}
}
}
eventType = parser.next();
} while (!endTagName.equals(parser.getName()));//再次到达,表示一个标签结束了
return activityName;
}
/**
* Parse and return {@link PathStrategy} for given authority as defined in
* {@link #META_DATA_FILE_PROVIDER_PATHS} {@code <meta-data>}.
*
* @see #getPathStrategy(Context, String)
*/
private static PathStrategy parsePathStrategy(Context context, String authority)
throws IOException, XmlPullParserException {
final SimplePathStrategy strat = new SimplePathStrategy(authority);
final ProviderInfo info = context.getPackageManager()
.resolveContentProvider(authority, PackageManager.GET_META_DATA);
final XmlResourceParser in = info.loadXmlMetaData(
context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
if (in == null) {
throw new IllegalArgumentException(
"Missing " + META_DATA_FILE_PROVIDER_PATHS + " meta-data");
}
int type;
while ((type = in.next()) != END_DOCUMENT) {
if (type == START_TAG) {
final String tag = in.getName();
final String name = in.getAttributeValue(null, ATTR_NAME);
String path = in.getAttributeValue(null, ATTR_PATH);
File target = null;
if (TAG_ROOT_PATH.equals(tag)) {
target = buildPath(DEVICE_ROOT, path);
} else if (TAG_FILES_PATH.equals(tag)) {
target = buildPath(context.getFilesDir(), path);
} else if (TAG_CACHE_PATH.equals(tag)) {
target = buildPath(context.getCacheDir(), path);
} else if (TAG_EXTERNAL.equals(tag)) {
target = buildPath(Environment.getExternalStorageDirectory(), path);
}
if (target != null) {
strat.addRoot(name, target);
}
}
}
return strat;
}
@Override
public long parseAndAdd(XmlResourceParser parser)
throws XmlPullParserException, IOException {
final String title;
final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
if (titleResId != 0) {
title = mSourceRes.getString(titleResId);
} else {
title = mContext.getResources().getString(R.string.folder_name);
}
mValues.put(Favorites.TITLE, title);
mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
mValues.put(Favorites.SPANX, 1);
mValues.put(Favorites.SPANY, 1);
mValues.put(Favorites._ID, mCallback.generateNewItemId());
long folderId = mCallback.insertAndCheck(mDb, mValues);
if (folderId < 0) {
if (LOGD) Log.e(TAG, "Unable to add folder");
return -1;
}
final ContentValues myValues = new ContentValues(mValues);
ArrayList<Long> folderItems = new ArrayList<Long>();
int type;
int folderDepth = parser.getDepth();
int rank = 0;
while ((type = parser.next()) != XmlPullParser.END_TAG ||
parser.getDepth() > folderDepth) {
if (type != XmlPullParser.START_TAG) {
continue;
}
mValues.clear();
mValues.put(Favorites.CONTAINER, folderId);
mValues.put(Favorites.RANK, rank);
TagParser tagParser = mFolderElements.get(parser.getName());
if (tagParser != null) {
final long id = tagParser.parseAndAdd(parser);
if (id >= 0) {
folderItems.add(id);
rank++;
}
} else {
throw new RuntimeException("Invalid folder item " + parser.getName());
}
}
long addedId = folderId;
// We can only have folders with >= 2 items, so we need to remove the
// folder and clean up if less than 2 items were included, or some
// failed to add, and less than 2 were actually added
if (folderItems.size() < 2) {
// Delete the folder
Uri uri = Favorites.getContentUri(folderId);
SqlArguments args = new SqlArguments(uri, null, null);
mDb.delete(args.table, args.where, args.args);
addedId = -1;
// If we have a single item, promote it to where the folder
// would have been.
if (folderItems.size() == 1) {
final ContentValues childValues = new ContentValues();
copyInteger(myValues, childValues, Favorites.CONTAINER);
copyInteger(myValues, childValues, Favorites.SCREEN);
copyInteger(myValues, childValues, Favorites.CELLX);
copyInteger(myValues, childValues, Favorites.CELLY);
addedId = folderItems.get(0);
mDb.update(LauncherProvider.TABLE_FAVORITES, childValues,
Favorites._ID + "=" + addedId, null);
}
}
return addedId;
}
private List<Pair<NetworkSecurityConfig.Builder, Set<Domain>>> parseConfigEntry(
XmlResourceParser parser, Set<String> seenDomains,
NetworkSecurityConfig.Builder parentBuilder, int configType)
throws IOException, XmlPullParserException, ParserException {
List<Pair<NetworkSecurityConfig.Builder, Set<Domain>>> builders = new ArrayList<>();
NetworkSecurityConfig.Builder builder = new NetworkSecurityConfig.Builder();
builder.setParent(parentBuilder);
Set<Domain> domains = new ArraySet<>();
boolean seenPinSet = false;
boolean seenTrustAnchors = false;
boolean defaultOverridePins = configType == CONFIG_DEBUG;
String configName = parser.getName();
int outerDepth = parser.getDepth();
// Add this builder now so that this builder occurs before any of its children. This
// makes the final build pass easier.
builders.add(new Pair<>(builder, domains));
// Parse config attributes. Only set values that are present, config inheritence will
// handle the rest.
for (int i = 0; i < parser.getAttributeCount(); i++) {
String name = parser.getAttributeName(i);
if ("hstsEnforced".equals(name)) {
builder.setHstsEnforced(
parser.getAttributeBooleanValue(i,
NetworkSecurityConfig.DEFAULT_HSTS_ENFORCED));
} else if ("cleartextTrafficPermitted".equals(name)) {
builder.setCleartextTrafficPermitted(
parser.getAttributeBooleanValue(i,
NetworkSecurityConfig.DEFAULT_CLEARTEXT_TRAFFIC_PERMITTED));
}
}
// Parse the config elements.
while (XmlUtils.nextElementWithin(parser, outerDepth)) {
String tagName = parser.getName();
if ("domain".equals(tagName)) {
if (configType != CONFIG_DOMAIN) {
throw new ParserException(parser,
"domain element not allowed in " + getConfigString(configType));
}
Domain domain = parseDomain(parser, seenDomains);
domains.add(domain);
} else if ("trust-anchors".equals(tagName)) {
if (seenTrustAnchors) {
throw new ParserException(parser,
"Multiple trust-anchor elements not allowed");
}
builder.addCertificatesEntryRefs(
parseTrustAnchors(parser, defaultOverridePins));
seenTrustAnchors = true;
} else if ("pin-set".equals(tagName)) {
if (configType != CONFIG_DOMAIN) {
throw new ParserException(parser,
"pin-set element not allowed in " + getConfigString(configType));
}
if (seenPinSet) {
throw new ParserException(parser, "Multiple pin-set elements not allowed");
}
builder.setPinSet(parsePinSet(parser));
seenPinSet = true;
} else if ("domain-config".equals(tagName)) {
if (configType != CONFIG_DOMAIN) {
throw new ParserException(parser,
"Nested domain-config not allowed in " + getConfigString(configType));
}
builders.addAll(parseConfigEntry(parser, seenDomains, builder, configType));
} else {
XmlUtils.skipCurrentTag(parser);
}
}
if (configType == CONFIG_DOMAIN && domains.isEmpty()) {
throw new ParserException(parser, "No domain elements in domain-config");
}
return builders;
}
/**
* Parse and return {@link PathStrategy} for given authority as defined in
* {@link #META_DATA_FILE_PROVIDER_PATHS} {@code <meta-data>}.
*
* @see #getPathStrategy(Context, String)
*/
private static PathStrategy parsePathStrategy(Context context, String authority)
throws IOException, XmlPullParserException {
final SimplePathStrategy strat = new SimplePathStrategy(authority);
final ProviderInfo info = context.getPackageManager()
.resolveContentProvider(authority, PackageManager.GET_META_DATA);
final XmlResourceParser in = info.loadXmlMetaData(
context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
if (in == null) {
throw new IllegalArgumentException(
"Missing " + META_DATA_FILE_PROVIDER_PATHS + " meta-data");
}
int type;
while ((type = in.next()) != END_DOCUMENT) {
if (type == START_TAG) {
final String tag = in.getName();
final String name = in.getAttributeValue(null, ATTR_NAME);
String path = in.getAttributeValue(null, ATTR_PATH);
File target = null;
if (TAG_ROOT_PATH.equals(tag)) {
target = buildPath(DEVICE_ROOT, path);
} else if (TAG_FILES_PATH.equals(tag)) {
target = buildPath(context.getFilesDir(), path);
} else if (TAG_CACHE_PATH.equals(tag)) {
target = buildPath(context.getCacheDir(), path);
} else if (TAG_EXTERNAL.equals(tag)) {
target = buildPath(Environment.getExternalStorageDirectory(), path);
}
if (target != null) {
strat.addRoot(name, target);
}
}
}
return strat;
}
/**
* Constructor.
*
* @param context The Context in which we are parsing the device admin.
* @param activityInfo The ActivityInfo returned from the package manager about
* this device admin's component.
*
* @hide
*/
public DeviceAdminInfo(Context context, ActivityInfo activityInfo)
throws XmlPullParserException, IOException {
mActivityInfo = activityInfo;
PackageManager pm = context.getPackageManager();
XmlResourceParser parser = null;
try {
parser = mActivityInfo.loadXmlMetaData(pm, DeviceAdminReceiver.DEVICE_ADMIN_META_DATA);
if (parser == null) {
throw new XmlPullParserException("No "
+ DeviceAdminReceiver.DEVICE_ADMIN_META_DATA + " meta-data");
}
Resources res = pm.getResourcesForApplication(mActivityInfo.applicationInfo);
AttributeSet attrs = Xml.asAttributeSet(parser);
int type;
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
&& type != XmlPullParser.START_TAG) {
}
String nodeName = parser.getName();
if (!"device-admin".equals(nodeName)) {
throw new XmlPullParserException(
"Meta-data does not start with device-admin tag");
}
TypedArray sa = res.obtainAttributes(attrs,
com.android.internal.R.styleable.DeviceAdmin);
mVisible = sa.getBoolean(
com.android.internal.R.styleable.DeviceAdmin_visible, true);
sa.recycle();
int outerDepth = parser.getDepth();
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
&& (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
String tagName = parser.getName();
if (tagName.equals("uses-policies")) {
int innerDepth = parser.getDepth();
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
&& (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
String policyName = parser.getName();
Integer val = sKnownPolicies.get(policyName);
if (val != null) {
mUsesPolicies |= 1 << val.intValue();
} else {
Log.w(TAG, "Unknown tag under uses-policies of "
+ getComponent() + ": " + policyName);
}
}
} else if (tagName.equals("support-transfer-ownership")) {
if (parser.next() != XmlPullParser.END_TAG) {
throw new XmlPullParserException(
"support-transfer-ownership tag must be empty.");
}
mSupportsTransferOwnership = true;
}
}
} catch (NameNotFoundException e) {
throw new XmlPullParserException(
"Unable to create context for: " + mActivityInfo.packageName);
} finally {
if (parser != null) parser.close();
}
}
/**
* Constructor.
*
* @param context The Context in which we are parsing the wallpaper.
* @param service The ResolveInfo returned from the package manager about
* this wallpaper's component.
*/
public WallpaperInfo(Context context, ResolveInfo service)
throws XmlPullParserException, IOException {
mService = service;
ServiceInfo si = service.serviceInfo;
final PackageManager pm = context.getPackageManager();
XmlResourceParser parser = null;
try {
parser = si.loadXmlMetaData(pm, WallpaperService.SERVICE_META_DATA);
if (parser == null) {
throw new XmlPullParserException("No "
+ WallpaperService.SERVICE_META_DATA + " meta-data");
}
Resources res = pm.getResourcesForApplication(si.applicationInfo);
AttributeSet attrs = Xml.asAttributeSet(parser);
int type;
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
&& type != XmlPullParser.START_TAG) {
}
String nodeName = parser.getName();
if (!"wallpaper".equals(nodeName)) {
throw new XmlPullParserException(
"Meta-data does not start with wallpaper tag");
}
TypedArray sa = res.obtainAttributes(attrs,
com.android.internal.R.styleable.Wallpaper);
mSettingsActivityName = sa.getString(
com.android.internal.R.styleable.Wallpaper_settingsActivity);
mThumbnailResource = sa.getResourceId(
com.android.internal.R.styleable.Wallpaper_thumbnail,
-1);
mAuthorResource = sa.getResourceId(
com.android.internal.R.styleable.Wallpaper_author,
-1);
mDescriptionResource = sa.getResourceId(
com.android.internal.R.styleable.Wallpaper_description,
-1);
mContextUriResource = sa.getResourceId(
com.android.internal.R.styleable.Wallpaper_contextUri,
-1);
mContextDescriptionResource = sa.getResourceId(
com.android.internal.R.styleable.Wallpaper_contextDescription,
-1);
mShowMetadataInPreview = sa.getBoolean(
com.android.internal.R.styleable.Wallpaper_showMetadataInPreview,
false);
mSupportsAmbientMode = sa.getBoolean(
com.android.internal.R.styleable.Wallpaper_supportsAmbientMode,
false);
sa.recycle();
} catch (NameNotFoundException e) {
throw new XmlPullParserException(
"Unable to create context for: " + si.packageName);
} finally {
if (parser != null) parser.close();
}
}
/**
* 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.
*/
private static int loadLogoFromManifest(Activity activity) {
int logo = 0;
try {
final String thisPackage = activity.getClass().getName();
if (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 (DEBUG) Log.d(TAG, "Got <application>");
for (int i = xml.getAttributeCount() - 1; i >= 0; i--) {
if (DEBUG) Log.d(TAG, xml.getAttributeName(i) + ": " + xml.getAttributeValue(i));
if ("logo".equals(xml.getAttributeName(i))) {
logo = xml.getAttributeResourceValue(i, 0);
break; //out of for loop
}
}
} else if ("activity".equals(name)) {
//Check if the <activity> is us and has the attribute
if (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 (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)) {
break; //on to the next
}
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) {
//If we matched our activity but it had no logo don't
//do any more processing of the manifest
break;
}
}
}
eventType = xml.nextToken();
}
} catch (Exception e) {
e.printStackTrace();
}
if (DEBUG) Log.i(TAG, "Returning " + Integer.toHexString(logo));
return logo;
}
private CompositeStreamStrategy parseStreamStrategy(final CompositeStreamStrategy result,
Context context,
String authority)
throws IOException, XmlPullParserException {
final ProviderInfo info=
context.getPackageManager()
.resolveContentProvider(authority,
PackageManager.GET_META_DATA);
useLegacyCursorWrapper=info.metaData.getBoolean(META_DATA_USE_LEGACY_CURSOR_WRAPPER, true);
useUriForDataColumn=info.metaData.getBoolean(META_DATA_USE_URI_FOR_DATA_COLUMN, false);
final XmlResourceParser in=
info.loadXmlMetaData(context.getPackageManager(),
META_DATA_FILE_PROVIDER_PATHS);
if (in == null) {
throw new IllegalArgumentException("Missing "
+ META_DATA_FILE_PROVIDER_PATHS + " meta-data");
}
int type;
while ((type=in.next()) != org.xmlpull.v1.XmlPullParser.END_DOCUMENT) {
if (type == org.xmlpull.v1.XmlPullParser.START_TAG) {
final String tag=in.getName();
if (!"paths".equals(tag)) {
final String name=in.getAttributeValue(null, ATTR_NAME);
if (TextUtils.isEmpty(name)) {
throw new IllegalArgumentException("Name must not be empty");
}
String path=in.getAttributeValue(null, ATTR_PATH);
boolean readOnly=allReadOnly ||
Boolean.parseBoolean(in.getAttributeValue(null, ATTR_READ_ONLY));
HashMap<String, String> attrs=new HashMap<String, String>();
for (int i=0;i<in.getAttributeCount();i++) {
attrs.put(in.getAttributeName(i), in.getAttributeValue(i));
}
StreamStrategy strategy=
buildStrategy(context, tag, name, path, readOnly, attrs);
if (strategy != null) {
result.add(name, strategy);
}
else {
throw new IllegalArgumentException("Could not build strategy for "
+ tag);
}
}
}
}
return(result);
}
private static d b(Context context, String s)
{
e e1 = new e(s);
XmlResourceParser xmlresourceparser = context.getPackageManager().resolveContentProvider(s, 128).loadXmlMetaData(context.getPackageManager(), "android.support.FILE_PROVIDER_PATHS");
if (xmlresourceparser == null)
{
throw new IllegalArgumentException("Missing android.support.FILE_PROVIDER_PATHS meta-data");
}
do
{
int l = xmlresourceparser.next();
if (l != 1)
{
if (l == 2)
{
String s1 = xmlresourceparser.getName();
String s2 = xmlresourceparser.getAttributeValue(null, "name");
String s3 = xmlresourceparser.getAttributeValue(null, "path");
File file;
if ("root-path".equals(s1))
{
file = a(i, new String[] {
s3
});
} else
if ("files-path".equals(s1))
{
file = a(context.getFilesDir(), new String[] {
s3
});
} else
if ("cache-path".equals(s1))
{
file = a(context.getCacheDir(), new String[] {
s3
});
} else
if ("external-path".equals(s1))
{
file = a(Environment.getExternalStorageDirectory(), new String[] {
s3
});
} else
{
file = null;
}
if (file != null)
{
e1.a(s2, file);
}
}
} else
{
return e1;
}
} while (true);
}
static HashMap<String, String> getDefaultsFromXml(Context context, int resourceId) {
HashMap<String,String> defaultsMap = new HashMap<>();
try {
Resources resources = context.getResources();
if (resources == null) {
Log.e("ProductConfig", "Could not find the resources of the current context while trying to set defaults from an XML.");
return defaultsMap;
}
XmlResourceParser xmlParser = resources.getXml(resourceId);
String curTag = null;
String key = null;
String value = null;
for (int eventType = xmlParser.getEventType(); eventType != XmlPullParser.END_DOCUMENT; eventType = xmlParser.next()) {
if (eventType == XmlPullParser.START_TAG) {
curTag = xmlParser.getName();
} else if (eventType != XmlPullParser.END_TAG) {
if (eventType == XmlPullParser.TEXT && curTag != null) {
byte tagType = -1;
switch (curTag) {
case XML_TAG_KEY:
tagType = XML_TAG_TYPE_KEY;
break;
case XML_TAG_VALUE:
tagType = XML_TAG_TYPE_VALUE;
}
switch (tagType) {
case XML_TAG_TYPE_KEY:
key = xmlParser.getText();
break;
case XML_TAG_TYPE_VALUE:
value = xmlParser.getText();
break;
default:
Log.w(LOG_TAG_PRODUCT_CONFIG, "Encountered an unexpected tag while parsing the defaults XML.");
}
}
} else {
if (xmlParser.getName().equals(XML_TAG_ENTRY)) {
if (key != null && value != null) {
defaultsMap.put(key, value);
} else {
Log.w(LOG_TAG_PRODUCT_CONFIG, "An entry in the defaults XML has an invalid key and/or value tag.");
}
key = null;
value = null;
}
curTag = null;
}
}
} catch (IOException | XmlPullParserException var11) {
Log.e("ProductConfig", "Encountered an error while parsing the defaults XML file.", var11);
}
return defaultsMap;
}
/**
* Parse and return {@link PathStrategy} for given authority as defined in
* {@link #META_DATA_FILE_PROVIDER_PATHS} {@code <meta-data>}.
*
* @see #getPathStrategy(Context, String)
*/
private static PathStrategy parsePathStrategy(Context context, String authority)
throws IOException, XmlPullParserException {
final SimplePathStrategy strat = new SimplePathStrategy(authority);
final ProviderInfo info = context.getPackageManager()
.resolveContentProvider(authority, PackageManager.GET_META_DATA);
final XmlResourceParser in = info.loadXmlMetaData(
context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
if (in == null) {
throw new IllegalArgumentException(
"Missing " + META_DATA_FILE_PROVIDER_PATHS + " meta-data");
}
int type;
while ((type = in.next()) != END_DOCUMENT) {
if (type == START_TAG) {
final String tag = in.getName();
final String name = in.getAttributeValue(null, ATTR_NAME);
String path = in.getAttributeValue(null, ATTR_PATH);
File target = null;
if (TAG_ROOT_PATH.equals(tag)) {
target = DEVICE_ROOT;
} else if (TAG_FILES_PATH.equals(tag)) {
target = context.getFilesDir();
} else if (TAG_CACHE_PATH.equals(tag)) {
target = context.getCacheDir();
} else if (TAG_EXTERNAL.equals(tag)) {
target = Environment.getExternalStorageDirectory();
} else if (TAG_EXTERNAL_FILES.equals(tag)) {
target = context.getExternalFilesDir(null);
} else if (TAG_EXTERNAL_CACHE.equals(tag)) {
target = context.getExternalCacheDir();
}
if (target != null) {
strat.addRoot(name, buildPath(target, path));
}
}
}
return strat;
}
/**
* 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);
break; //out of for loop
}
}
} 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)) {
break; //on to the next
}
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) {
//If we matched our activity but it had no logo don't
//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;
}
/**
* Parse and return {@link PathStrategy} for given authority as defined in
* {@link #META_DATA_FILE_PROVIDER_PATHS} {@code <meta-data>}.
*
* @see #getPathStrategy(Context, String)
*/
private static PathStrategy parsePathStrategy(Context context, String authority)
throws IOException, XmlPullParserException {
final SimplePathStrategy strat = new SimplePathStrategy(authority);
final ProviderInfo info =
context.getPackageManager().resolveContentProvider(authority, PackageManager.GET_META_DATA);
final XmlResourceParser in =
info.loadXmlMetaData(context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
if (in == null) {
throw new IllegalArgumentException("Missing " + META_DATA_FILE_PROVIDER_PATHS + " meta-data");
}
int type;
while ((type = in.next()) != END_DOCUMENT) {
if (type == START_TAG) {
final String tag = in.getName();
final String name = in.getAttributeValue(null, ATTR_NAME);
String path = in.getAttributeValue(null, ATTR_PATH);
File target = null;
if (TAG_ROOT_PATH.equals(tag)) {
target = buildPath(DEVICE_ROOT, path);
} else if (TAG_FILES_PATH.equals(tag)) {
target = buildPath(context.getFilesDir(), path);
} else if (TAG_CACHE_PATH.equals(tag)) {
target = buildPath(context.getCacheDir(), path);
} else if (TAG_EXTERNAL.equals(tag)) {
target = buildPath(Environment.getExternalStorageDirectory(), path);
} else if (TAG_EXTERNAL_APP.equals(tag)) {
target = buildPath(context.getExternalFilesDir(null), path);
}
if (target != null) {
strat.addRoot(name, target);
}
}
}
return strat;
}