7c919700bc42cc80b5c0720f7b78373592105469
1 package eu
.alefzero
.owncloud
;
3 import android
.content
.Context
;
4 import android
.graphics
.Rect
;
5 import android
.graphics
.drawable
.BitmapDrawable
;
6 import android
.graphics
.drawable
.Drawable
;
7 import android
.view
.Gravity
;
8 import android
.view
.LayoutInflater
;
9 import android
.view
.MotionEvent
;
10 import android
.view
.View
;
11 import android
.view
.WindowManager
;
12 import android
.view
.View
.OnTouchListener
;
13 import android
.view
.ViewGroup
.LayoutParams
;
14 import android
.widget
.PopupWindow
;
16 public class CustomPopup
{
17 protected final View mAnchor
;
18 protected final PopupWindow mWindow
;
20 private Drawable background
= null
;
21 protected final WindowManager mWManager
;
23 public CustomPopup(View anchor
) {
25 mWindow
= new PopupWindow(anchor
.getContext());
27 mWindow
.setTouchInterceptor(new OnTouchListener() {
29 public boolean onTouch(View v
, MotionEvent event
) {
30 if (event
.getAction() == MotionEvent
.ACTION_OUTSIDE
) {
31 CustomPopup
.this.dismiss();
38 mWManager
= (WindowManager
) anchor
.getContext().getSystemService(Context
.WINDOW_SERVICE
);
43 public void onCreate() {}
44 public void onShow() {}
46 public void preShow() {
48 throw new IllegalStateException("setContentView called with a view to display");
53 if (background
== null
) {
54 mWindow
.setBackgroundDrawable(new BitmapDrawable());
56 mWindow
.setBackgroundDrawable(background
);
59 mWindow
.setWidth(WindowManager
.LayoutParams
.WRAP_CONTENT
);
60 mWindow
.setHeight(WindowManager
.LayoutParams
.WRAP_CONTENT
);
61 mWindow
.setTouchable(true
);
62 mWindow
.setFocusable(true
);
63 mWindow
.setOutsideTouchable(true
);
65 mWindow
.setContentView(root
);
68 public void setBackgroundDrawable(Drawable background
) {
69 this.background
= background
;
72 public void setContentView(View root
) {
74 mWindow
.setContentView(root
);
77 public void setContentView(int layoutResId
) {
78 LayoutInflater inflater
=
79 (LayoutInflater
) mAnchor
.getContext().getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
80 setContentView(inflater
.inflate(layoutResId
, null
));
83 public void showDropDown() {
87 public void showDropDown(int x
, int y
) {
89 mWindow
.setAnimationStyle(android
.R
.style
.Animation_Dialog
);
90 mWindow
.showAsDropDown(mAnchor
, x
, y
);
93 public void showLikeQuickAction() {
94 showLikeQuickAction(0, 0);
97 public void showLikeQuickAction(int x
, int y
) {
100 mWindow
.setAnimationStyle(android
.R
.style
.Animation_Dialog
);
101 int[] location
= new int[2];
102 mAnchor
.getLocationOnScreen(location
);
105 new Rect(location
[0], location
[1], location
[0] + mAnchor
.getWidth(), location
[1] + mAnchor
.getHeight());
107 root
.setLayoutParams(new LayoutParams(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
));
108 root
.measure(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
);
110 int rootW
= root
.getWidth(), rootH
= root
.getHeight();
111 int screenW
= mWManager
.getDefaultDisplay().getWidth();
113 int xpos
= ((screenW
-rootW
)/2) + x
;
114 int ypos
= anchorRect
.top
- rootH
+ y
;
116 if (rootH
> anchorRect
.top
) {
117 ypos
= anchorRect
.bottom
+ y
;
119 mWindow
.showAtLocation(mAnchor
, Gravity
.NO_GRAVITY
, xpos
, ypos
);
122 public void dismiss() {