-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ShadowPopupWindow.java
181 lines (150 loc) · 4.18 KB
/
ShadowPopupWindow.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package org.robolectric.shadows;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import static org.robolectric.Shadows.shadowOf;
@SuppressWarnings({"UnusedDeclaration"})
@Implements(PopupWindow.class)
public class ShadowPopupWindow {
@RealObject
protected PopupWindow realPopupWindow;
private View contentView;
private int width;
private int height;
private boolean focusable;
private boolean touchable;
private boolean outSideTouchable;
private boolean showing;
private Drawable background;
private View.OnTouchListener onTouchInterceptor;
private Context context;
private LinearLayout containerView;
private int xOffset;
private int yOffset;
public void __constructor__(View contentView) {
setContentView(contentView);
// getWindowManager();
}
public void __constructor__(View contentView, int width, int height, boolean focusable) {
__constructor__(contentView);
this.width = width;
this.height = height;
this.focusable = focusable;
}
@Implementation
public void setContentView(View contentView) {
this.contentView = contentView;
if (contentView != null) {
context = contentView.getContext();
}
}
@Implementation
public View getContentView() {
return contentView;
}
@Implementation
public void setWidth(int width) {
this.width = width;
}
@Implementation
public int getWidth() {
return width;
}
@Implementation
public void setHeight(int height) {
this.height = height;
}
@Implementation
public int getHeight() {
return height;
}
@Implementation
public void setFocusable(boolean focusable) {
this.focusable = focusable;
}
@Implementation
public boolean isFocusable() {
return focusable;
}
@Implementation
public void setTouchable(boolean touchable) {
this.touchable = touchable;
}
@Implementation
public boolean isTouchable() {
return touchable;
}
@Implementation
public void setOutsideTouchable(boolean touchable) {
outSideTouchable = touchable;
}
@Implementation
public boolean isOutsideTouchable() {
return outSideTouchable;
}
@Implementation
public boolean isShowing() {
return showing;
}
@Implementation
public void dismiss() {
if (context != null) {
getWindowManager().removeView(containerView);
}
showing = false;
}
@Implementation
public void setBackgroundDrawable(Drawable background) {
this.background = background;
}
@Implementation
public Drawable getBackground() {
return background;
}
@Implementation
public void setTouchInterceptor(android.view.View.OnTouchListener l) {
onTouchInterceptor = l;
}
@Implementation
public void showAsDropDown(View anchor) {
containerView = new LinearLayout(context);
containerView.addView(contentView);
containerView.setBackgroundDrawable(background);
getWindowManager().addView(containerView, new WindowManager.LayoutParams());
showing = true;
shadowOf(RuntimeEnvironment.application).setLatestPopupWindow(realPopupWindow);
}
@Implementation
public void showAsDropDown(View anchor, int xoff, int yoff) {
xOffset = xoff;
yOffset = yoff;
showAsDropDown(anchor);
}
@Implementation
public void showAtLocation(View parent, int gravity, int x, int y) {
showAsDropDown(parent);
}
public boolean dispatchTouchEvent(MotionEvent e) {
return onTouchInterceptor != null && onTouchInterceptor.onTouch(realPopupWindow.getContentView(), e);
}
private WindowManager getWindowManager() {
return (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
}
public int getXOffset() {
return xOffset;
}
public int getYOffset() {
return yOffset;
}
public static PopupWindow getLatestPopupWindow() {
return shadowOf(RuntimeEnvironment.application).getLatestPopupWindow();
}
}