7c919700bc42cc80b5c0720f7b78373592105469
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / CustomPopup.java
1 package eu.alefzero.owncloud;
2
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;
15
16 public class CustomPopup {
17 protected final View mAnchor;
18 protected final PopupWindow mWindow;
19 private View root;
20 private Drawable background = null;
21 protected final WindowManager mWManager;
22
23 public CustomPopup(View anchor) {
24 mAnchor = anchor;
25 mWindow = new PopupWindow(anchor.getContext());
26
27 mWindow.setTouchInterceptor(new OnTouchListener() {
28
29 public boolean onTouch(View v, MotionEvent event) {
30 if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
31 CustomPopup.this.dismiss();
32 return true;
33 }
34 return false;
35 }
36 });
37
38 mWManager = (WindowManager) anchor.getContext().getSystemService(Context.WINDOW_SERVICE);
39 onCreate();
40 }
41
42
43 public void onCreate() {}
44 public void onShow() {}
45
46 public void preShow() {
47 if (root == null) {
48 throw new IllegalStateException("setContentView called with a view to display");
49 }
50
51 onShow();
52
53 if (background == null) {
54 mWindow.setBackgroundDrawable(new BitmapDrawable());
55 } else {
56 mWindow.setBackgroundDrawable(background);
57 }
58
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);
64
65 mWindow.setContentView(root);
66 }
67
68 public void setBackgroundDrawable(Drawable background) {
69 this.background = background;
70 }
71
72 public void setContentView(View root) {
73 this.root = root;
74 mWindow.setContentView(root);
75 }
76
77 public void setContentView(int layoutResId) {
78 LayoutInflater inflater =
79 (LayoutInflater) mAnchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
80 setContentView(inflater.inflate(layoutResId, null));
81 }
82
83 public void showDropDown() {
84 showDropDown(0, 0);
85 }
86
87 public void showDropDown(int x, int y) {
88 preShow();
89 mWindow.setAnimationStyle(android.R.style.Animation_Dialog);
90 mWindow.showAsDropDown(mAnchor, x, y);
91 }
92
93 public void showLikeQuickAction() {
94 showLikeQuickAction(0, 0);
95 }
96
97 public void showLikeQuickAction(int x, int y) {
98 preShow();
99
100 mWindow.setAnimationStyle(android.R.style.Animation_Dialog);
101 int[] location = new int[2];
102 mAnchor.getLocationOnScreen(location);
103
104 Rect anchorRect =
105 new Rect(location[0], location[1], location[0] + mAnchor.getWidth(), location[1] + mAnchor.getHeight());
106
107 root.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
108 root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
109
110 int rootW = root.getWidth(), rootH = root.getHeight();
111 int screenW = mWManager.getDefaultDisplay().getWidth();
112
113 int xpos = ((screenW-rootW)/2) + x;
114 int ypos = anchorRect.top - rootH + y;
115
116 if (rootH > anchorRect.top) {
117 ypos = anchorRect.bottom + y;
118 }
119 mWindow.showAtLocation(mAnchor, Gravity.NO_GRAVITY, xpos, ypos);
120 }
121
122 public void dismiss() {
123 mWindow.dismiss();
124 }
125
126 }