ShadowView.java revision a1ef7deaed03310ed6ec51621125f69bb16ac8fc
1d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)package com.xtremelabs.droidsugar.fakes;
2d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
3d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)import android.content.Context;
4d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)import android.content.res.Resources;
5d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)import android.util.AttributeSet;
6d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)import android.view.MotionEvent;
7d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)import android.view.View;
8d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)import android.view.ViewGroup;
9d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)import android.view.ViewParent;
10d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)import com.xtremelabs.droidsugar.ProxyDelegatingHandler;
11d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)import com.xtremelabs.droidsugar.util.Implements;
12d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
13d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)import java.util.ArrayList;
14d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)import java.util.HashMap;
15d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)import java.util.List;
16d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)import java.util.Map;
17d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
18d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)@SuppressWarnings({"UnusedDeclaration"})
19d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)@Implements(View.class)
20d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)public class FakeView {
21d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    @Deprecated
22d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    public static final int UNINITIALIZED_ATTRIBUTE = -1000;
23d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
24d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    protected View realView;
25d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
26d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    private int id;
27d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    private List<View> children = new ArrayList<View>();
28d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    private FakeView parent;
29d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    private Context context;
30d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    public boolean selected;
31d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    private View.OnClickListener onClickListener;
32d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    private Object tag;
33    private boolean enabled = true;
34    public int visibility = View.VISIBLE;
35    public int left;
36    public int top;
37    public int right;
38    public int bottom;
39    public int paddingLeft;
40    public int paddingTop;
41    public int paddingRight;
42    public int paddingBottom;
43    public ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(0, 0);
44    private Map<Integer, Object> tags = new HashMap<Integer, Object>();
45    public boolean clickable;
46    public boolean focusable;
47    public int backgroundResourceId = -1;
48    protected View.OnKeyListener onKeyListener;
49    public boolean hasFocus;
50    private View.OnFocusChangeListener onFocusChangeListener;
51    public boolean wasInvalidated;
52    private View.OnTouchListener onTouchListener;
53
54    public FakeView(View view) {
55        this.realView = view;
56    }
57
58    public void __constructor__(Context context) {
59        this.context = context;
60    }
61
62    public void __constructor__(Context context, AttributeSet attrs) {
63        __constructor__(context);
64    }
65
66    public void setId(int id) {
67        this.id = id;
68    }
69
70    public void setClickable(boolean clickable) {
71        this.clickable = clickable;
72    }
73
74    public void setFocusable(boolean focusable) {
75        this.focusable = focusable;
76    }
77
78    public int getId() {
79        return id;
80    }
81
82    public static View inflate(Context context, int resource, ViewGroup root) {
83        View view = FakeContextWrapper.resourceLoader.viewLoader.inflateView(context, resource);
84        if (root != null) {
85            root.addView(view);
86        }
87        return view;
88    }
89
90    public View findViewById(int id) {
91        if (id == this.id) {
92            return realView;
93        }
94
95        for (View child : children) {
96            View found = child.findViewById(id);
97            if (found != null) {
98                return found;
99            }
100        }
101        return null;
102    }
103
104    public View getRootView() {
105        FakeView root = this;
106        while(root.parent != null) {
107            root = root.parent;
108        }
109        return root.realView;
110    }
111
112    public void addView(View child) {
113        children.add(child);
114        childProxy(child).parent = this;
115    }
116
117    private FakeView childProxy(View child) {
118        return (FakeView) ProxyDelegatingHandler.getInstance().proxyFor(child);
119    }
120
121    public int getChildCount() {
122        return children.size();
123    }
124
125    public ViewGroup.LayoutParams getLayoutParams() {
126        return layoutParams;
127    }
128
129    public void setLayoutParams(ViewGroup.LayoutParams params) {
130        layoutParams = params;
131    }
132
133    public View getChildAt(int index) {
134        return children.get(index);
135    }
136
137    public final ViewParent getParent() {
138        return parent == null ? null : (ViewParent) parent.realView;
139    }
140
141    public void removeAllViews() {
142        for (View child : children) {
143            childProxy(child).parent = null;
144        }
145        children.clear();
146    }
147
148    public void removeViewAt(int position) {
149        childProxy(children.remove(position)).parent = null;
150    }
151
152    public final Context getContext() {
153        return context;
154    }
155
156    public Resources getResources() {
157        return context.getResources();
158    }
159
160    public void setBackgroundResource(int backgroundResourceId) {
161        this.backgroundResourceId = backgroundResourceId;
162    }
163
164    public int getVisibility() {
165        return visibility;
166    }
167
168    public void setVisibility(int visibility) {
169        this.visibility = visibility;
170    }
171
172    public void setSelected(boolean selected) {
173        this.selected = selected;
174    }
175
176    public boolean isSelected() {
177        return this.selected;
178    }
179
180    public boolean isEnabled() {
181        return this.enabled;
182    }
183
184    public void setEnabled(boolean enabled) {
185        this.enabled = enabled;
186    }
187
188    public void setOnClickListener(View.OnClickListener onClickListener) {
189        this.onClickListener = onClickListener;
190    }
191
192    public boolean performClick() {
193        if (onClickListener != null) {
194            onClickListener.onClick(realView);
195            return true;
196        } else {
197            return false;
198        }
199    }
200
201    public void setOnKeyListener(View.OnKeyListener onKeyListener) {
202        this.onKeyListener = onKeyListener;
203    }
204
205    public Object getTag() {
206        return this.tag;
207    }
208
209    public void setTag(Object tag) {
210        this.tag = tag;
211    }
212
213    public final int getHeight() {
214        return bottom - top;
215    }
216
217    public final int getWidth() {
218        return right - left;
219    }
220
221    public final int getMeasuredWidth() {
222        return getWidth();
223    }
224
225    public final void layout(int l, int t, int r, int b) {
226        left = l;
227        top = t;
228        right = r;
229        bottom = b;
230
231// todo:       realView.onLayout();
232    }
233
234    public void setPadding(int left, int top, int right, int bottom) {
235        paddingLeft = left;
236        paddingTop = top;
237        paddingRight = right;
238        paddingBottom = bottom;
239    }
240
241    public int getPaddingTop() {
242        return paddingTop;
243    }
244
245    public int getPaddingLeft() {
246        return paddingLeft;
247    }
248
249    public int getPaddingRight() {
250        return paddingRight;
251    }
252
253    public int getPaddingBottom() {
254        return paddingBottom;
255    }
256
257    public Object getTag(int key) {
258        return tags.get(key);
259    }
260
261    public void setTag(int key, Object value) {
262        tags.put(key, value);
263    }
264
265    public void setViewFocus(boolean hasFocus) {
266        this.hasFocus = hasFocus;
267        if (onFocusChangeListener != null) {
268            onFocusChangeListener.onFocusChange(realView, hasFocus);
269        }
270    }
271
272    public boolean hasFocus() {
273        return hasFocus;
274    }
275
276    public void setOnFocusChangeListener(View.OnFocusChangeListener listener) {
277        onFocusChangeListener = listener;
278    }
279
280    public void invalidate() {
281        wasInvalidated = true;
282    }
283
284    public void setOnTouchListener(View.OnTouchListener onTouchListener) {
285        this.onTouchListener = onTouchListener;
286    }
287
288    public boolean dispatchTouchEvent(MotionEvent event) {
289        if (onTouchListener != null) {
290            return onTouchListener.onTouch(realView, event);
291        }
292        return false;
293    }
294
295    public String innerText() {
296        return "";
297    }
298}
299