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