package com.xtremelabs.droidsugar.fakes; import android.content.Context; import android.content.res.Resources; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import com.xtremelabs.droidsugar.ProxyDelegatingHandler; import com.xtremelabs.droidsugar.util.Implements; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @SuppressWarnings({"UnusedDeclaration"}) @Implements(View.class) public class FakeView { @Deprecated public static final int UNINITIALIZED_ATTRIBUTE = -1000; protected View realView; private int id; private List children = new ArrayList(); private FakeView parent; private Context context; public boolean selected; private View.OnClickListener onClickListener; private Object tag; private boolean enabled = true; public int visibility = View.VISIBLE; public int left; public int top; public int right; public int bottom; public int paddingLeft; public int paddingTop; public int paddingRight; public int paddingBottom; public ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(0, 0); private Map tags = new HashMap(); public boolean clickable; public boolean focusable; public int backgroundResourceId = -1; protected View.OnKeyListener onKeyListener; public boolean hasFocus; private View.OnFocusChangeListener onFocusChangeListener; public boolean wasInvalidated; private View.OnTouchListener onTouchListener; public FakeView(View view) { this.realView = view; } public void __constructor__(Context context) { this.context = context; } public void __constructor__(Context context, AttributeSet attrs) { __constructor__(context); } public void setId(int id) { this.id = id; } public void setClickable(boolean clickable) { this.clickable = clickable; } public void setFocusable(boolean focusable) { this.focusable = focusable; } public int getId() { return id; } public static View inflate(Context context, int resource, ViewGroup root) { View view = FakeContextWrapper.resourceLoader.viewLoader.inflateView(context, resource); if (root != null) { root.addView(view); } return view; } public View findViewById(int id) { if (id == this.id) { return realView; } for (View child : children) { View found = child.findViewById(id); if (found != null) { return found; } } return null; } public View getRootView() { FakeView root = this; while(root.parent != null) { root = root.parent; } return root.realView; } public void addView(View child) { children.add(child); childProxy(child).parent = this; } private FakeView childProxy(View child) { return (FakeView) ProxyDelegatingHandler.getInstance().proxyFor(child); } public int getChildCount() { return children.size(); } public ViewGroup.LayoutParams getLayoutParams() { return layoutParams; } public void setLayoutParams(ViewGroup.LayoutParams params) { layoutParams = params; } public View getChildAt(int index) { return children.get(index); } public final ViewParent getParent() { return parent == null ? null : (ViewParent) parent.realView; } public void removeAllViews() { for (View child : children) { childProxy(child).parent = null; } children.clear(); } public void removeViewAt(int position) { childProxy(children.remove(position)).parent = null; } public final Context getContext() { return context; } public Resources getResources() { return context.getResources(); } public void setBackgroundResource(int backgroundResourceId) { this.backgroundResourceId = backgroundResourceId; } public int getVisibility() { return visibility; } public void setVisibility(int visibility) { this.visibility = visibility; } public void setSelected(boolean selected) { this.selected = selected; } public boolean isSelected() { return this.selected; } public boolean isEnabled() { return this.enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public void setOnClickListener(View.OnClickListener onClickListener) { this.onClickListener = onClickListener; } public boolean performClick() { if (onClickListener != null) { onClickListener.onClick(realView); return true; } else { return false; } } public void setOnKeyListener(View.OnKeyListener onKeyListener) { this.onKeyListener = onKeyListener; } public Object getTag() { return this.tag; } public void setTag(Object tag) { this.tag = tag; } public final int getHeight() { return bottom - top; } public final int getWidth() { return right - left; } public final int getMeasuredWidth() { return getWidth(); } public final void layout(int l, int t, int r, int b) { left = l; top = t; right = r; bottom = b; // todo: realView.onLayout(); } public void setPadding(int left, int top, int right, int bottom) { paddingLeft = left; paddingTop = top; paddingRight = right; paddingBottom = bottom; } public int getPaddingTop() { return paddingTop; } public int getPaddingLeft() { return paddingLeft; } public int getPaddingRight() { return paddingRight; } public int getPaddingBottom() { return paddingBottom; } public Object getTag(int key) { return tags.get(key); } public void setTag(int key, Object value) { tags.put(key, value); } public void setViewFocus(boolean hasFocus) { this.hasFocus = hasFocus; if (onFocusChangeListener != null) { onFocusChangeListener.onFocusChange(realView, hasFocus); } } public boolean hasFocus() { return hasFocus; } public void setOnFocusChangeListener(View.OnFocusChangeListener listener) { onFocusChangeListener = listener; } public void invalidate() { wasInvalidated = true; } public void setOnTouchListener(View.OnTouchListener onTouchListener) { this.onTouchListener = onTouchListener; } public boolean dispatchTouchEvent(MotionEvent event) { if (onTouchListener != null) { return onTouchListener.onTouch(realView, event); } return false; } public String innerText() { return ""; } }