Java Code Examples for android.content.res.XmlResourceParser#getAttributeResourceValue()

The following examples show how to use android.content.res.XmlResourceParser#getAttributeResourceValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlConfigSource.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private CertificatesEntryRef parseCertificatesEntry(XmlResourceParser parser,
        boolean defaultOverridePins)
        throws IOException, XmlPullParserException, ParserException {
    boolean overridePins =
            parser.getAttributeBooleanValue(null, "overridePins", defaultOverridePins);
    int sourceId = parser.getAttributeResourceValue(null, "src", -1);
    String sourceString = parser.getAttributeValue(null, "src");
    CertificateSource source = null;
    if (sourceString == null) {
        throw new ParserException(parser, "certificates element missing src attribute");
    }
    if (sourceId != -1) {
        // TODO: Cache ResourceCertificateSources by sourceId
        source = new ResourceCertificateSource(sourceId, mContext);
    } else if ("system".equals(sourceString)) {
        source = SystemCertificateSource.getInstance();
    } else if ("user".equals(sourceString)) {
        source = UserCertificateSource.getInstance();
    } else {
        throw new ParserException(parser, "Unknown certificates src. "
                + "Should be one of system|user|@resourceVal");
    }
    XmlUtils.skipCurrentTag(parser);
    return new CertificatesEntryRef(source, overridePins);
}
 
Example 2
Source File: XmlConfigSource.java    From cwac-netsecurity with Apache License 2.0 6 votes vote down vote up
private CertificatesEntryRef parseCertificatesEntry(XmlResourceParser parser,
            boolean defaultOverridePins)
            throws IOException, XmlPullParserException, ParserException {
        boolean overridePins =
                parser.getAttributeBooleanValue(null, "overridePins", defaultOverridePins);
        int sourceId = parser.getAttributeResourceValue(null, "src", -1);
        String sourceString = parser.getAttributeValue(null, "src");
        CertificateSource source = null;
        if (sourceString == null) {
            throw new ParserException(parser, "certificates element missing src attribute");
        }
        if (sourceId != -1) {
            // TODO: Cache ResourceCertificateSources by sourceId
            source = new ResourceCertificateSource(sourceId, mContext);
        } else if ("system".equals(sourceString) || "user".equals(sourceString)) {
            // MLM treat user as system
            source = SystemCertificateSource.getInstance();
//        } else if ("user".equals(sourceString)) {
//            source = UserCertificateSource.getInstance();
        } else {
            throw new ParserException(parser, "Unknown certificates src. "
                    + "Should be one of system|user|@resourceVal");
        }
        XmlUtils.skipCurrentTag(parser);
        return new CertificatesEntryRef(source, overridePins);
    }
 
Example 3
Source File: XmlKeyboardLoader.java    From AndroidTVWidget with Apache License 2.0 6 votes vote down vote up
private float getFloat(XmlResourceParser xrp, String name, float defValue) {
	int resId = xrp.getAttributeResourceValue(null, name, 0);
	if (resId == 0) {
		String s = xrp.getAttributeValue(null, name);
		if (null == s)
			return defValue;
		try {
			float ret;
			if (s.endsWith("%p")) {
				ret = Float.parseFloat(s.substring(0, s.length() - 2)) / 100;
			} else {
				ret = Float.parseFloat(s);
			}
			return ret;
		} catch (NumberFormatException e) {
			return defValue;
		}
	} else {
		return mContext.getResources().getDimension(resId);
	}
}
 
Example 4
Source File: AutoInstallsLayout.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return attribute resource value, attempting launcher-specific namespace
 * first before falling back to anonymous attribute.
 */
static int getAttributeResourceValue(XmlResourceParser parser, String attribute,
        int defaultValue) {
    int value = parser.getAttributeResourceValue(
            "http://schemas.android.com/apk/res-auto/com.enrico.launcher3", attribute,
            defaultValue);
    if (value == defaultValue) {
        value = parser.getAttributeResourceValue(null, attribute, defaultValue);
    }
    return value;
}
 
Example 5
Source File: AutoInstallsLayout.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
/**
 * Return attribute resource value, attempting launcher-specific namespace
 * first before falling back to anonymous attribute.
 */
protected static int getAttributeResourceValue(XmlResourceParser parser, String attribute,
        int defaultValue) {
    int value = parser.getAttributeResourceValue(
            "http://schemas.android.com/apk/res-auto/com.lb.launcher", attribute,
            defaultValue);
    if (value == defaultValue) {
        value = parser.getAttributeResourceValue(null, attribute, defaultValue);
    }
    return value;
}
 
Example 6
Source File: XmlKeyboardLoader.java    From Android-tv-widget with Apache License 2.0 5 votes vote down vote up
private String getString(XmlResourceParser xrp, String name, String defValue) {
	int resId = xrp.getAttributeResourceValue(null, name, 0);
	if (resId == 0) {
		return xrp.getAttributeValue(null, name);
	} else {
		return mContext.getResources().getString(resId);
	}
}
 
Example 7
Source File: XmlKeyboardLoader.java    From AndroidTVWidget with Apache License 2.0 5 votes vote down vote up
private String getString(XmlResourceParser xrp, String name, String defValue) {
	int resId = xrp.getAttributeResourceValue(null, name, 0);
	if (resId == 0) {
		return xrp.getAttributeValue(null, name);
	} else {
		return mContext.getResources().getString(resId);
	}
}
 
Example 8
Source File: AutoInstallsLayout.java    From Trebuchet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return attribute resource value, attempting launcher-specific namespace
 * first before falling back to anonymous attribute.
 */
protected static int getAttributeResourceValue(XmlResourceParser parser, String attribute,
        int defaultValue) {
    int value = parser.getAttributeResourceValue(
            "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
            defaultValue);
    if (value == defaultValue) {
        value = parser.getAttributeResourceValue(null, attribute, defaultValue);
    }
    return value;
}
 
Example 9
Source File: TabParser.java    From BottomBar with Apache License 2.0 5 votes vote down vote up
@ColorInt
private int getColorValue(@NonNull XmlResourceParser parser, @IntRange(from = 0) int attrIndex) {
    int colorResource = parser.getAttributeResourceValue(attrIndex, 0);

    if (colorResource == RESOURCE_NOT_FOUND) {
        try {
            String colorValue = parser.getAttributeValue(attrIndex);
            return Color.parseColor(colorValue);
        } catch (Exception ignored) {
            return COLOR_NOT_SET;
        }
    }

    return ContextCompat.getColor(context, colorResource);
}
 
Example 10
Source File: ResourcesCompat.java    From zhangshangwuda with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: TabParser.java    From BottomBar with Apache License 2.0 4 votes vote down vote up
@NonNull
private String getTitleValue(@NonNull XmlResourceParser parser, @IntRange(from = 0) int attrIndex) {
    int titleResource = parser.getAttributeResourceValue(attrIndex, 0);
    return titleResource == RESOURCE_NOT_FOUND
            ? parser.getAttributeValue(attrIndex) : context.getString(titleResource);
}
 
Example 12
Source File: XmlKeyboardLoader.java    From AndroidTVWidget with Apache License 2.0 4 votes vote down vote up
private Drawable getDrawable(XmlResourceParser xrp, String name, Drawable defValue) {
	int resId = xrp.getAttributeResourceValue(null, name, 0);
	if (0 == resId)
		return defValue;
	return mResources.getDrawable(resId);
}
 
Example 13
Source File: ResourcesCompat.java    From zen4android with MIT License 4 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: ResourcesCompat.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 15
Source File: ResourcesCompat.java    From Libraries-for-Android-Developers with MIT License 4 votes vote down vote up
/**
 * 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;
}
 
Example 16
Source File: XmlKeyboardLoader.java    From Android-tv-widget with Apache License 2.0 4 votes vote down vote up
private Drawable getDrawable(XmlResourceParser xrp, String name, Drawable defValue) {
	int resId = xrp.getAttributeResourceValue(null, name, 0);
	if (0 == resId)
		return defValue;
	return mResources.getDrawable(resId);
}
 
Example 17
Source File: Utility4.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 18
Source File: TimeSpinner.java    From ReminderDatePicker with Apache License 2.0 4 votes vote down vote up
@Override
protected @Nullable TwinTextItem parseItemFromXmlTag(@NonNull XmlResourceParser parser) {
    if(!parser.getName().equals(XML_TAG_TIMEITEM)) {
        Log.d("TimeSpinner", "Unknown xml tag name: " + parser.getName());
        return null;
    }

    // parse the TimeItem, possible values are
    String text = null;
    @StringRes int textResource = NO_ID, id = NO_ID;
    int hour = 0, minute = 0;
    for(int i=parser.getAttributeCount()-1; i>=0; i--) {
        String attrName = parser.getAttributeName(i);
        switch (attrName) {
            case XML_ATTR_ID:
                id = parser.getIdAttributeResourceValue(NO_ID);
                break;
            case XML_ATTR_TEXT:
                text = parser.getAttributeValue(i);
                // try to get a resource value, the string is retrieved below
                if(text != null && text.startsWith("@"))
                    textResource = parser.getAttributeResourceValue(i, NO_ID);
                break;

            case XML_ATTR_ABSHOUR:
                hour = parser.getAttributeIntValue(i, -1);
                break;
            case XML_ATTR_ABSMINUTE:
                minute = parser.getAttributeIntValue(i, -1);
                break;

            case XML_ATTR_RELHOUR:
                hour += parser.getAttributeIntValue(i, 0);
                break;
            case XML_ATTR_RELMINUTE:
                minute += parser.getAttributeIntValue(i, 0);
                break;
            default:
                Log.d("TimeSpinner", "Skipping unknown attribute tag parsing xml resource: "
                        + attrName + ", maybe a typo?");
        }
    }// end for attr

    // now construct the time item from the attributes
    if(textResource != NO_ID)
        text = getResources().getString(textResource);

    // when no text is given, format the date to have at least something to show
    if(text == null || text.equals(""))
        text = formatTime(hour, minute);

    return new TimeItem(text, formatTime(hour, minute), hour, minute, id);
}
 
Example 19
Source File: DateSpinner.java    From ReminderDatePicker with Apache License 2.0 4 votes vote down vote up
@Override
protected @Nullable TwinTextItem parseItemFromXmlTag(@NonNull XmlResourceParser parser) {
    if(!parser.getName().equals(XML_TAG_DATEITEM)) {
        Log.d("DateSpinner", "Unknown xml tag name: " + parser.getName());
        return null;
    }

    // parse the DateItem, possible values are
    String text = null;
    @StringRes int textResource = NO_ID, id = NO_ID;
    Calendar date = Calendar.getInstance();
    for(int i=parser.getAttributeCount()-1; i>=0; i--) {
        String attrName = parser.getAttributeName(i);
        switch (attrName) {
            case XML_ATTR_ID:
                id = parser.getIdAttributeResourceValue(NO_ID);
                break;
            case XML_ATTR_TEXT:
                text = parser.getAttributeValue(i);
                // try to get a resource value, the string is retrieved below
                if(text != null && text.startsWith("@"))
                    textResource = parser.getAttributeResourceValue(i, NO_ID);
                break;

            case XML_ATTR_ABSDAYOFYEAR:
                final int absDayOfYear = parser.getAttributeIntValue(i, -1);
                if(absDayOfYear > 0)
                    date.set(Calendar.DAY_OF_YEAR, absDayOfYear);
                break;
            case XML_ATTR_ABSDAYOFMONTH:
                final int absDayOfMonth = parser.getAttributeIntValue(i, -1);
                if(absDayOfMonth > 0)
                    date.set(Calendar.DAY_OF_MONTH, absDayOfMonth);
                break;
            case XML_ATTR_ABSMONTH:
                final int absMonth = parser.getAttributeIntValue(i, -1);
                if(absMonth >= 0)
                    date.set(Calendar.MONTH, absMonth);
                break;
            case XML_ATTR_ABSYEAR:
                final int absYear = parser.getAttributeIntValue(i, -1);
                if(absYear >= 0)
                    date.set(Calendar.YEAR, absYear);
                break;

            case XML_ATTR_RELDAY:
                final int relDay = parser.getAttributeIntValue(i, 0);
                date.add(Calendar.DAY_OF_YEAR, relDay);
                break;
            case XML_ATTR_RELMONTH:
                final int relMonth = parser.getAttributeIntValue(i, 0);
                date.add(Calendar.MONTH, relMonth);
                break;
            case XML_ATTR_RELYEAR:
                final int relYear = parser.getAttributeIntValue(i, 0);
                date.add(Calendar.YEAR, relYear);
                break;
            default:
                Log.d("DateSpinner", "Skipping unknown attribute tag parsing xml resource: "
                        + attrName + ", maybe a typo?");
        }
    }// end for attr

    // now construct the date item from the attributes

    // check if we got a textResource earlier and parse that string together with the weekday
    if(textResource != NO_ID)
        text = getWeekDay(date.get(Calendar.DAY_OF_WEEK), textResource);

    // when no text is given, format the date to have at least something to show
    if(text == null || text.equals(""))
        text = formatDate(date);

    return new DateItem(text, date, id);
}
 
Example 20
Source File: ActionBarView.java    From android-apps with MIT License 4 votes vote down vote up
/**
 * 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;
}