forked from owncloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomPopup.java
More file actions
154 lines (126 loc) · 4.74 KB
/
Copy pathCustomPopup.java
File metadata and controls
154 lines (126 loc) · 4.74 KB
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
/* ownCloud Android client application
* Copyright (C) 2011 Bartek Przybylski
* Copyright (C) 2012-2013 ownCloud Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.owncloud.android.ui;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
/**
* Represents a custom PopupWindows
*
* @author Lorensius. W. T
*
*/
public class CustomPopup {
protected final View mAnchor;
protected final PopupWindow mWindow;
private View root;
private Drawable background = null;
protected final WindowManager mWManager;
public CustomPopup(View anchor) {
mAnchor = anchor;
mWindow = new PopupWindow(anchor.getContext());
mWindow.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
CustomPopup.this.dismiss();
return true;
}
return false;
}
});
mWManager = (WindowManager) anchor.getContext().getSystemService(
Context.WINDOW_SERVICE);
onCreate();
}
public void onCreate() {
}
public void onShow() {
}
public void preShow() {
if (root == null) {
throw new IllegalStateException(
"setContentView called with a view to display");
}
onShow();
if (background == null) {
mWindow.setBackgroundDrawable(new BitmapDrawable());
} else {
mWindow.setBackgroundDrawable(background);
}
mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
mWindow.setTouchable(true);
mWindow.setFocusable(true);
mWindow.setOutsideTouchable(true);
mWindow.setContentView(root);
}
public void setBackgroundDrawable(Drawable background) {
this.background = background;
}
public void setContentView(View root) {
this.root = root;
mWindow.setContentView(root);
}
public void setContentView(int layoutResId) {
LayoutInflater inflater = (LayoutInflater) mAnchor.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setContentView(inflater.inflate(layoutResId, null));
}
public void showDropDown() {
showDropDown(0, 0);
}
public void showDropDown(int x, int y) {
preShow();
mWindow.setAnimationStyle(android.R.style.Animation_Dialog);
mWindow.showAsDropDown(mAnchor, x, y);
}
public void showLikeQuickAction() {
showLikeQuickAction(0, 0);
}
public void showLikeQuickAction(int x, int y) {
preShow();
mWindow.setAnimationStyle(android.R.style.Animation_Dialog);
int[] location = new int[2];
mAnchor.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0]
+ mAnchor.getWidth(), location[1] + mAnchor.getHeight());
root.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootW = root.getWidth(), rootH = root.getHeight();
int screenW = mWManager.getDefaultDisplay().getWidth();
int xpos = ((screenW - rootW) / 2) + x;
int ypos = anchorRect.top - rootH + y;
if (rootH > anchorRect.top) {
ypos = anchorRect.bottom + y;
}
mWindow.showAtLocation(mAnchor, Gravity.NO_GRAVITY, xpos, ypos);
}
public void dismiss() {
mWindow.dismiss();
}
}