ShadowPopupWindow.java revision bca7d93d5d5ab81bded83b76acaeadc868d0d613
1ab604478ab38300c47bd13b76613b768c227abdfChris Perrypackage com.xtremelabs.robolectric.shadows; 2ab604478ab38300c47bd13b76613b768c227abdfChris Perry 34f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richardimport android.content.Context; 4ab604478ab38300c47bd13b76613b768c227abdfChris Perryimport android.graphics.drawable.Drawable; 57f5c8507976947879b349971412143074901c048Chris Perryimport android.view.MotionEvent; 6ab604478ab38300c47bd13b76613b768c227abdfChris Perryimport android.view.View; 74f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richardimport android.view.WindowManager; 84f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richardimport android.widget.LinearLayout; 9ab604478ab38300c47bd13b76613b768c227abdfChris Perryimport android.widget.PopupWindow; 10ab604478ab38300c47bd13b76613b768c227abdfChris Perryimport com.xtremelabs.robolectric.internal.Implementation; 11ab604478ab38300c47bd13b76613b768c227abdfChris Perryimport com.xtremelabs.robolectric.internal.Implements; 12ab604478ab38300c47bd13b76613b768c227abdfChris Perryimport com.xtremelabs.robolectric.internal.RealObject; 13ab604478ab38300c47bd13b76613b768c227abdfChris Perry 14ab604478ab38300c47bd13b76613b768c227abdfChris Perry@SuppressWarnings({"UnusedDeclaration"}) 15ab604478ab38300c47bd13b76613b768c227abdfChris Perry@Implements(PopupWindow.class) 16ab604478ab38300c47bd13b76613b768c227abdfChris Perrypublic class ShadowPopupWindow { 17ab604478ab38300c47bd13b76613b768c227abdfChris Perry @RealObject 18ab604478ab38300c47bd13b76613b768c227abdfChris Perry protected PopupWindow realPopupWindow; 19ab604478ab38300c47bd13b76613b768c227abdfChris Perry private View contentView; 20ab604478ab38300c47bd13b76613b768c227abdfChris Perry private int width; 21ab604478ab38300c47bd13b76613b768c227abdfChris Perry private int height; 22ab604478ab38300c47bd13b76613b768c227abdfChris Perry private boolean focusable; 23ab604478ab38300c47bd13b76613b768c227abdfChris Perry private boolean touchable; 24ab604478ab38300c47bd13b76613b768c227abdfChris Perry private boolean outSideTouchable; 25ab604478ab38300c47bd13b76613b768c227abdfChris Perry private boolean showing; 26ab604478ab38300c47bd13b76613b768c227abdfChris Perry private Drawable background; 277f5c8507976947879b349971412143074901c048Chris Perry private View.OnTouchListener onTouchInterceptor; 284f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard private WindowManager windowManager; 294f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard private Context context; 304f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard 31bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard public void __constructor__(View contentView) { 324f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard this.contentView = contentView; 334f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard context = contentView.getContext(); 344f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 354f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard } 36ab604478ab38300c47bd13b76613b768c227abdfChris Perry 37bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard public void __constructor__(View contentView, int width, int height, boolean focusable) { 38bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard __constructor__(contentView); 39bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard this.width = width; 40bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard this.height = height; 41bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard this.focusable = focusable; 42bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard } 43bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard 44ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 45ab604478ab38300c47bd13b76613b768c227abdfChris Perry public void setContentView(View contentView) { 46ab604478ab38300c47bd13b76613b768c227abdfChris Perry this.contentView = contentView; 47ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 48ab604478ab38300c47bd13b76613b768c227abdfChris Perry 49ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 50ab604478ab38300c47bd13b76613b768c227abdfChris Perry public View getContentView() { 51ab604478ab38300c47bd13b76613b768c227abdfChris Perry return contentView; 52ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 53ab604478ab38300c47bd13b76613b768c227abdfChris Perry 54ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 55ab604478ab38300c47bd13b76613b768c227abdfChris Perry public void setWidth(int width) { 56ab604478ab38300c47bd13b76613b768c227abdfChris Perry this.width = width; 57ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 58ab604478ab38300c47bd13b76613b768c227abdfChris Perry 59ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 60ab604478ab38300c47bd13b76613b768c227abdfChris Perry public int getWidth() { 61ab604478ab38300c47bd13b76613b768c227abdfChris Perry return width; 62ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 63ab604478ab38300c47bd13b76613b768c227abdfChris Perry 64ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 65ab604478ab38300c47bd13b76613b768c227abdfChris Perry public void setHeight(int height) { 66ab604478ab38300c47bd13b76613b768c227abdfChris Perry this.height = height; 67ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 68ab604478ab38300c47bd13b76613b768c227abdfChris Perry 69ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 70ab604478ab38300c47bd13b76613b768c227abdfChris Perry public int getHeight() { 71ab604478ab38300c47bd13b76613b768c227abdfChris Perry return height; 72ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 73ab604478ab38300c47bd13b76613b768c227abdfChris Perry 74ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 75ab604478ab38300c47bd13b76613b768c227abdfChris Perry public void setFocusable(boolean focusable) { 76ab604478ab38300c47bd13b76613b768c227abdfChris Perry this.focusable = focusable; 77ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 78ab604478ab38300c47bd13b76613b768c227abdfChris Perry 79ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 80ab604478ab38300c47bd13b76613b768c227abdfChris Perry public boolean isFocusable() { 81ab604478ab38300c47bd13b76613b768c227abdfChris Perry return focusable; 82ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 83ab604478ab38300c47bd13b76613b768c227abdfChris Perry 84ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 85ab604478ab38300c47bd13b76613b768c227abdfChris Perry public void setTouchable(boolean touchable) { 86ab604478ab38300c47bd13b76613b768c227abdfChris Perry this.touchable = touchable; 87ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 88ab604478ab38300c47bd13b76613b768c227abdfChris Perry 89ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 90ab604478ab38300c47bd13b76613b768c227abdfChris Perry public boolean isTouchable() { 91ab604478ab38300c47bd13b76613b768c227abdfChris Perry return touchable; 92ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 93ab604478ab38300c47bd13b76613b768c227abdfChris Perry 94ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 95ab604478ab38300c47bd13b76613b768c227abdfChris Perry public void setOutsideTouchable(boolean touchable) { 96ab604478ab38300c47bd13b76613b768c227abdfChris Perry outSideTouchable = touchable; 97ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 98ab604478ab38300c47bd13b76613b768c227abdfChris Perry 99ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 100ab604478ab38300c47bd13b76613b768c227abdfChris Perry public boolean isOutsideTouchable() { 101ab604478ab38300c47bd13b76613b768c227abdfChris Perry return outSideTouchable; 102ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 103ab604478ab38300c47bd13b76613b768c227abdfChris Perry 104ab604478ab38300c47bd13b76613b768c227abdfChris Perry /** 105ab604478ab38300c47bd13b76613b768c227abdfChris Perry * non-android setter for testing 1067f5c8507976947879b349971412143074901c048Chris Perry * 107ab604478ab38300c47bd13b76613b768c227abdfChris Perry * @param showing true if popup window is showing 108ab604478ab38300c47bd13b76613b768c227abdfChris Perry */ 109ab604478ab38300c47bd13b76613b768c227abdfChris Perry public void setShowing(boolean showing) { 110ab604478ab38300c47bd13b76613b768c227abdfChris Perry this.showing = showing; 111ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 112ab604478ab38300c47bd13b76613b768c227abdfChris Perry 113ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 114ab604478ab38300c47bd13b76613b768c227abdfChris Perry public boolean isShowing() { 115ab604478ab38300c47bd13b76613b768c227abdfChris Perry return showing; 116ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 117ab604478ab38300c47bd13b76613b768c227abdfChris Perry 118ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 119ab604478ab38300c47bd13b76613b768c227abdfChris Perry public void dismiss() { 120ab604478ab38300c47bd13b76613b768c227abdfChris Perry showing = false; 121ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 122ab604478ab38300c47bd13b76613b768c227abdfChris Perry 123ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 124ab604478ab38300c47bd13b76613b768c227abdfChris Perry public void setBackgroundDrawable(Drawable background) { 125ab604478ab38300c47bd13b76613b768c227abdfChris Perry this.background = background; 126ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 127ab604478ab38300c47bd13b76613b768c227abdfChris Perry 128ab604478ab38300c47bd13b76613b768c227abdfChris Perry @Implementation 129ab604478ab38300c47bd13b76613b768c227abdfChris Perry public Drawable getBackground() { 130ab604478ab38300c47bd13b76613b768c227abdfChris Perry return background; 131ab604478ab38300c47bd13b76613b768c227abdfChris Perry } 1327f5c8507976947879b349971412143074901c048Chris Perry 1337f5c8507976947879b349971412143074901c048Chris Perry @Implementation 1347f5c8507976947879b349971412143074901c048Chris Perry public void setTouchInterceptor(android.view.View.OnTouchListener l) { 1357f5c8507976947879b349971412143074901c048Chris Perry onTouchInterceptor = l; 1367f5c8507976947879b349971412143074901c048Chris Perry } 1377f5c8507976947879b349971412143074901c048Chris Perry 1384f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard @Implementation 1394f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard public void showAsDropDown(View anchor) { 1404f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard LinearLayout container = new LinearLayout(context); 1414f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard container.addView(contentView); 1424f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard windowManager.addView(container, null); 1434f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard } 1444f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard 1457f5c8507976947879b349971412143074901c048Chris Perry public boolean dispatchTouchEvent(MotionEvent e) { 1467f5c8507976947879b349971412143074901c048Chris Perry return onTouchInterceptor != null && onTouchInterceptor.onTouch(realPopupWindow.getContentView(), e); 1477f5c8507976947879b349971412143074901c048Chris Perry } 148ab604478ab38300c47bd13b76613b768c227abdfChris Perry} 149