public static int parseColor(String color) { try { return Color.parseColor(color); } catch (IllegalArgumentException ex) { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "Invalid color : " + color + ". Using #000000"); } return Color.BLACK; } }
public static void addRelativeLayoutRule(View view, int verb, int anchor) { ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); if (layoutParams instanceof RelativeLayout.LayoutParams) { RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layoutParams; params.addRule(verb, anchor); view.setLayoutParams(params); } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "cannot add relative layout rules when container is not relative"); } } }
public static int parseInt(String attributeValue) { int number; if (ProteusConstants.DATA_NULL.equals(attributeValue)) { return 0; } try { number = Integer.parseInt(attributeValue); } catch (NumberFormatException e) { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, attributeValue + " is NAN. Error: " + e.getMessage()); } number = 0; } return number; }
public static double parseDouble(String attributeValue) { double number; if (ProteusConstants.DATA_NULL.equals(attributeValue)) { return 0; } try { number = Double.parseDouble(attributeValue); } catch (NumberFormatException e) { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, attributeValue + " is NAN. Error: " + e.getMessage()); } number = 0; } return number; }
@Override public void setGravity(V view, @Gravity int gravity) { ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); if (layoutParams instanceof LinearLayout.LayoutParams) { LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) layoutParams; linearLayoutParams.gravity = gravity; view.setLayoutParams(layoutParams); } else if (layoutParams instanceof FrameLayout.LayoutParams) { FrameLayout.LayoutParams linearLayoutParams = (FrameLayout.LayoutParams) layoutParams; linearLayoutParams.gravity = gravity; view.setLayoutParams(layoutParams); } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "'layout_gravity' is only supported for LinearLayout and FrameLayout"); } } } });
@Override public void setDimension(V view, float dimension) { if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) { ViewGroup.MarginLayoutParams layoutParams; layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams(); layoutParams.setMargins(layoutParams.leftMargin, (int) dimension, layoutParams.rightMargin, layoutParams.bottomMargin); view.setLayoutParams(layoutParams); } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "margins can only be applied to views with parent ViewGroup"); } } } });
@Override public void setDimension(V view, float dimension) { if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) { ViewGroup.MarginLayoutParams layoutParams; layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams(); layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin, layoutParams.rightMargin, (int) dimension); view.setLayoutParams(layoutParams); } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "margins can only be applied to views with parent ViewGroup"); } } } });
@Override public void setString(V view, String value) { LinearLayout.LayoutParams layoutParams; if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) { layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams(); layoutParams.weight = ParseHelper.parseFloat(value); view.setLayoutParams(layoutParams); } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "'weight' is only supported for LinearLayouts"); } } } });
@Override public void setDimension(V view, float dimension) { if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) { ViewGroup.MarginLayoutParams layoutParams; layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams(); layoutParams.setMargins((int) dimension, layoutParams.topMargin, layoutParams.rightMargin, layoutParams.bottomMargin); view.setLayoutParams(layoutParams); } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "margins can only be applied to views with parent ViewGroup"); } } } });
@Override public void setDimension(V view, float dimension) { if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) { ViewGroup.MarginLayoutParams layoutParams; layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams(); layoutParams.setMargins((int) dimension, (int) dimension, (int) dimension, (int) dimension); view.setLayoutParams(layoutParams); } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "margins can only be applied to views with parent ViewGroup"); } } } });
@Override public void setDimension(V view, float dimension) { if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) { ViewGroup.MarginLayoutParams layoutParams; layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams(); layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin, (int) dimension, layoutParams.bottomMargin); view.setLayoutParams(layoutParams); } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "margins can only be applied to views with parent ViewGroup"); } } } });
protected boolean handleAttribute(@NonNull ViewTypeParser parser, @NonNull ProteusView view, int attribute, @NonNull Value value) { if (ProteusConstants.isLoggingEnabled()) { Log.d(TAG, "Handle '" + attribute + "' : " + value); } //noinspection unchecked return parser.handleAttribute(view.getAsView(), attribute, value); } }
@NonNull @Override public Value evaluate(Context context, Value data, int index) { Value[] arguments = resolve(context, this.arguments, data, index); try { return this.function.call(context, data, index, arguments); } catch (Exception e) { if (ProteusConstants.isLoggingEnabled()) { Log.e(Utils.LIB_NAME, e.getMessage(), e); } return Null.INSTANCE; } }
@Override public void handleValue(V view, Value value) { Animation animation = AnimationUtils.loadAnimation(view.getContext(), value); if (null != animation) { setAnimation(view, animation); } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "Animation Resource must be a primitive or an object. value -> " + value.toString()); } } }
static void setLayoutBehavior(View v, String behavior) { CoordinatorLayout.LayoutParams layoutParams = getLayoutParams(v); if (null != layoutParams && !TextUtils.isEmpty(behavior)) { //noinspection TryWithIdenticalCatches since there are min API requirements try { Class clazz = sBehaviorMap.get(behavior); if (null == clazz) { clazz = Class.forName(behavior); sBehaviorMap.put(behavior, clazz); } Object behaviorObj = clazz.newInstance(); if (null != behaviorObj && behaviorObj instanceof CoordinatorLayout.Behavior) { layoutParams.setBehavior((CoordinatorLayout.Behavior) behaviorObj); } } catch (ClassNotFoundException e) { if (ProteusConstants.isLoggingEnabled()) { e.printStackTrace(); } } catch (InstantiationException e) { if (ProteusConstants.isLoggingEnabled()) { e.printStackTrace(); } } catch (IllegalAccessException e) { if (ProteusConstants.isLoggingEnabled()) { e.printStackTrace(); } } } } }
@Nullable public static AttributeResource valueOf(String value, Context context) { AttributeResource attribute = AttributeCache.cache.get(value); if (null == attribute) { try { attribute = new AttributeResource(value, context); } catch (Exception e) { if (ProteusConstants.isLoggingEnabled()) { e.printStackTrace(); } attribute = NULL; } AttributeCache.cache.put(value, attribute); } return NULL == attribute ? null : attribute; }
@Nullable public static StyleResource valueOf(String value, Context context) { StyleResource style = StyleCache.cache.get(value); if (null == style) { try { style = new StyleResource(value, context); } catch (Exception e) { if (ProteusConstants.isLoggingEnabled()) { e.printStackTrace(); } style = NULL; } StyleCache.cache.put(value, style); } return NULL == style ? null : style; }
@NonNull protected ProteusView onUnknownViewEncountered(String type, Layout layout, ObjectValue data, int dataIndex) { if (ProteusConstants.isLoggingEnabled()) { Log.d(TAG, "No ViewTypeParser for: " + type); } if (context.getCallback() != null) { ProteusView view = context.getCallback().onUnknownViewType(context, type, layout, data, dataIndex); //noinspection ConstantConditions because we need to throw a ProteusInflateException specifically if (view == null) { throw new ProteusInflateException("inflater Callback#onUnknownViewType() must not return null"); } } throw new ProteusInflateException("Layout contains type: 'include' but inflater callback is null"); }
/** * Loads an {@link Interpolator} object from a resource * * @param context Application context used to access resources * @param value Json representation of the Interpolator * @return The animation object reference by the specified id * @throws android.content.res.Resources.NotFoundException */ public static Interpolator loadInterpolator(Context context, Value value) throws Resources.NotFoundException { Interpolator interpolator = null; if (value.isPrimitive()) { interpolator = handleStringInterpolator(context, value.getAsString()); } else if (value.isObject()) { interpolator = handleElementInterpolator(context, value.getAsObject()); } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "Could not load interpolator for : " + value.toString()); } } return interpolator; }
/** * Loads an {@link Animation} object from a resource * * @param context Application context used to access resources * @param value JSON representation of the Animation * @return The animation object reference by the specified id * @throws android.content.res.Resources.NotFoundException when the animation cannot be loaded */ public static Animation loadAnimation(Context context, Value value) throws Resources.NotFoundException { Animation anim = null; if (value.isPrimitive()) { anim = handleString(context, value.getAsPrimitive().getAsString()); } else if (value.isObject()) { anim = handleElement(context, value.getAsObject()); } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "Could not load animation for : " + value.toString()); } } return anim; }