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 Context context;
295fb103e54becb5df0ce635a7884fff620196ae39Rick Kawala & Ryan Richard    private LinearLayout containerView;
30cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry    private int xOffset;
31cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry    private int yOffset;
324f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard
33bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard    public void __constructor__(View contentView) {
34d51b9b2a9ff6a6117340deec189fac6a6e3fd09cRick Kawala & Ryan Richard        setContentView(contentView);
35d51b9b2a9ff6a6117340deec189fac6a6e3fd09cRick Kawala & Ryan Richard        getWindowManager();
364f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard    }
37ab604478ab38300c47bd13b76613b768c227abdfChris Perry
38bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard    public void __constructor__(View contentView, int width, int height, boolean focusable) {
39bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard        __constructor__(contentView);
40bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard        this.width = width;
41bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard        this.height = height;
42bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard        this.focusable = focusable;
43bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard    }
44bca7d93d5d5ab81bded83b76acaeadc868d0d613Rick Kawala & Ryan Richard
45ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
46ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public void setContentView(View contentView) {
47ab604478ab38300c47bd13b76613b768c227abdfChris Perry        this.contentView = contentView;
48d51b9b2a9ff6a6117340deec189fac6a6e3fd09cRick Kawala & Ryan Richard        context = contentView.getContext();
49ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
50ab604478ab38300c47bd13b76613b768c227abdfChris Perry
51ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
52ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public View getContentView() {
53ab604478ab38300c47bd13b76613b768c227abdfChris Perry        return contentView;
54ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
55ab604478ab38300c47bd13b76613b768c227abdfChris Perry
56ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
57ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public void setWidth(int width) {
58ab604478ab38300c47bd13b76613b768c227abdfChris Perry        this.width = width;
59ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
60ab604478ab38300c47bd13b76613b768c227abdfChris Perry
61ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
62ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public int getWidth() {
63ab604478ab38300c47bd13b76613b768c227abdfChris Perry        return width;
64ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
65ab604478ab38300c47bd13b76613b768c227abdfChris Perry
66ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
67ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public void setHeight(int height) {
68ab604478ab38300c47bd13b76613b768c227abdfChris Perry        this.height = height;
69ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
70ab604478ab38300c47bd13b76613b768c227abdfChris Perry
71ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
72ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public int getHeight() {
73ab604478ab38300c47bd13b76613b768c227abdfChris Perry        return height;
74ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
75ab604478ab38300c47bd13b76613b768c227abdfChris Perry
76ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
77ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public void setFocusable(boolean focusable) {
78ab604478ab38300c47bd13b76613b768c227abdfChris Perry        this.focusable = focusable;
79ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
80ab604478ab38300c47bd13b76613b768c227abdfChris Perry
81ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
82ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public boolean isFocusable() {
83ab604478ab38300c47bd13b76613b768c227abdfChris Perry        return focusable;
84ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
85ab604478ab38300c47bd13b76613b768c227abdfChris Perry
86ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
87ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public void setTouchable(boolean touchable) {
88ab604478ab38300c47bd13b76613b768c227abdfChris Perry        this.touchable = touchable;
89ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
90ab604478ab38300c47bd13b76613b768c227abdfChris Perry
91ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
92ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public boolean isTouchable() {
93ab604478ab38300c47bd13b76613b768c227abdfChris Perry        return touchable;
94ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
95ab604478ab38300c47bd13b76613b768c227abdfChris Perry
96ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
97ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public void setOutsideTouchable(boolean touchable) {
98ab604478ab38300c47bd13b76613b768c227abdfChris Perry        outSideTouchable = touchable;
99ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
100ab604478ab38300c47bd13b76613b768c227abdfChris Perry
101ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
102ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public boolean isOutsideTouchable() {
103ab604478ab38300c47bd13b76613b768c227abdfChris Perry        return outSideTouchable;
104ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
105ab604478ab38300c47bd13b76613b768c227abdfChris Perry
106ab604478ab38300c47bd13b76613b768c227abdfChris Perry    /**
107ab604478ab38300c47bd13b76613b768c227abdfChris Perry     * non-android setter for testing
1087f5c8507976947879b349971412143074901c048Chris Perry     *
109ab604478ab38300c47bd13b76613b768c227abdfChris Perry     * @param showing true if popup window is showing
110ab604478ab38300c47bd13b76613b768c227abdfChris Perry     */
111ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public void setShowing(boolean showing) {
112ab604478ab38300c47bd13b76613b768c227abdfChris Perry        this.showing = showing;
113ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
114ab604478ab38300c47bd13b76613b768c227abdfChris Perry
115ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
116ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public boolean isShowing() {
117ab604478ab38300c47bd13b76613b768c227abdfChris Perry        return showing;
118ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
119ab604478ab38300c47bd13b76613b768c227abdfChris Perry
120ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
121ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public void dismiss() {
122d51b9b2a9ff6a6117340deec189fac6a6e3fd09cRick Kawala & Ryan Richard        if (context != null) {
123d51b9b2a9ff6a6117340deec189fac6a6e3fd09cRick Kawala & Ryan Richard            getWindowManager().removeView(containerView);
124d51b9b2a9ff6a6117340deec189fac6a6e3fd09cRick Kawala & Ryan Richard        }
125ab604478ab38300c47bd13b76613b768c227abdfChris Perry        showing = false;
126ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
127ab604478ab38300c47bd13b76613b768c227abdfChris Perry
128ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
129ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public void setBackgroundDrawable(Drawable background) {
130ab604478ab38300c47bd13b76613b768c227abdfChris Perry        this.background = background;
131ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
132ab604478ab38300c47bd13b76613b768c227abdfChris Perry
133ab604478ab38300c47bd13b76613b768c227abdfChris Perry    @Implementation
134ab604478ab38300c47bd13b76613b768c227abdfChris Perry    public Drawable getBackground() {
135ab604478ab38300c47bd13b76613b768c227abdfChris Perry        return background;
136ab604478ab38300c47bd13b76613b768c227abdfChris Perry    }
1377f5c8507976947879b349971412143074901c048Chris Perry
1387f5c8507976947879b349971412143074901c048Chris Perry    @Implementation
1397f5c8507976947879b349971412143074901c048Chris Perry    public void setTouchInterceptor(android.view.View.OnTouchListener l) {
1407f5c8507976947879b349971412143074901c048Chris Perry        onTouchInterceptor = l;
1417f5c8507976947879b349971412143074901c048Chris Perry    }
1427f5c8507976947879b349971412143074901c048Chris Perry
1434f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard    @Implementation
1444f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard    public void showAsDropDown(View anchor) {
1455fb103e54becb5df0ce635a7884fff620196ae39Rick Kawala & Ryan Richard        containerView = new LinearLayout(context);
1465fb103e54becb5df0ce635a7884fff620196ae39Rick Kawala & Ryan Richard        containerView.addView(contentView);
1475fb103e54becb5df0ce635a7884fff620196ae39Rick Kawala & Ryan Richard        containerView.setBackgroundDrawable(background);
148d51b9b2a9ff6a6117340deec189fac6a6e3fd09cRick Kawala & Ryan Richard        getWindowManager().addView(containerView, null);
1494f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard    }
1504f271ae722a6f52f481267273ae31ebfc672ed86Rick Kawala & Ryan Richard
151cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry    @Implementation
152cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry    public void showAsDropDown(View anchor, int xoff, int yoff) {
153cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry        xOffset = xoff;
154cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry        yOffset = yoff;
155cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry        showAsDropDown(anchor);
156cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry    }
157cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry
1587f5c8507976947879b349971412143074901c048Chris Perry    public boolean dispatchTouchEvent(MotionEvent e) {
1597f5c8507976947879b349971412143074901c048Chris Perry        return onTouchInterceptor != null && onTouchInterceptor.onTouch(realPopupWindow.getContentView(), e);
1607f5c8507976947879b349971412143074901c048Chris Perry    }
161d51b9b2a9ff6a6117340deec189fac6a6e3fd09cRick Kawala & Ryan Richard
162d51b9b2a9ff6a6117340deec189fac6a6e3fd09cRick Kawala & Ryan Richard    private WindowManager getWindowManager() {
163d51b9b2a9ff6a6117340deec189fac6a6e3fd09cRick Kawala & Ryan Richard        return (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
164d51b9b2a9ff6a6117340deec189fac6a6e3fd09cRick Kawala & Ryan Richard    }
165cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry
166cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry    public int getXOffset() {
167cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry        return xOffset;
168cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry    }
169cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry
170cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry    public int getYOffset() {
171cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry        return yOffset;
172cd6b5772372178977db2389dbf70698039ddbff9Amrit Thakur & Chris Perry    }
173ab604478ab38300c47bd13b76613b768c227abdfChris Perry}
174