b7ce4265faf84d6da3504be5d6d2171feaed475e
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / CustomPopup.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18 package com.owncloud.android.ui;
19
20 import android.content.Context;
21 import android.graphics.Rect;
22 import android.graphics.drawable.BitmapDrawable;
23 import android.graphics.drawable.Drawable;
24 import android.view.Gravity;
25 import android.view.LayoutInflater;
26 import android.view.MotionEvent;
27 import android.view.View;
28 import android.view.WindowManager;
29 import android.view.View.OnTouchListener;
30 import android.view.ViewGroup.LayoutParams;
31 import android.widget.PopupWindow;
32
33 /**
34 * Represents a custom PopupWindows
35 *
36 * @author Lorensius. W. T
37 *
38 */
39 public class CustomPopup {
40 protected final View mAnchor;
41 protected final PopupWindow mWindow;
42 private View root;
43 private Drawable background = null;
44 protected final WindowManager mWManager;
45
46 public CustomPopup(View anchor) {
47 mAnchor = anchor;
48 mWindow = new PopupWindow(anchor.getContext());
49
50 mWindow.setTouchInterceptor(new OnTouchListener() {
51
52 public boolean onTouch(View v, MotionEvent event) {
53 if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
54 CustomPopup.this.dismiss();
55 return true;
56 }
57 return false;
58 }
59 });
60
61 mWManager = (WindowManager) anchor.getContext().getSystemService(
62 Context.WINDOW_SERVICE);
63 onCreate();
64 }
65
66 public void onCreate() {
67 }
68
69 public void onShow() {
70 }
71
72 public void preShow() {
73 if (root == null) {
74 throw new IllegalStateException(
75 "setContentView called with a view to display");
76 }
77
78 onShow();
79
80 if (background == null) {
81 mWindow.setBackgroundDrawable(new BitmapDrawable());
82 } else {
83 mWindow.setBackgroundDrawable(background);
84 }
85
86 mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
87 mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
88 mWindow.setTouchable(true);
89 mWindow.setFocusable(true);
90 mWindow.setOutsideTouchable(true);
91
92 mWindow.setContentView(root);
93 }
94
95 public void setBackgroundDrawable(Drawable background) {
96 this.background = background;
97 }
98
99 public void setContentView(View root) {
100 this.root = root;
101 mWindow.setContentView(root);
102 }
103
104 public void setContentView(int layoutResId) {
105 LayoutInflater inflater = (LayoutInflater) mAnchor.getContext()
106 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
107 setContentView(inflater.inflate(layoutResId, null));
108 }
109
110 public void showDropDown() {
111 showDropDown(0, 0);
112 }
113
114 public void showDropDown(int x, int y) {
115 preShow();
116 mWindow.setAnimationStyle(android.R.style.Animation_Dialog);
117 mWindow.showAsDropDown(mAnchor, x, y);
118 }
119
120 public void showLikeQuickAction() {
121 showLikeQuickAction(0, 0);
122 }
123
124 public void showLikeQuickAction(int x, int y) {
125 preShow();
126
127 mWindow.setAnimationStyle(android.R.style.Animation_Dialog);
128 int[] location = new int[2];
129 mAnchor.getLocationOnScreen(location);
130
131 Rect anchorRect = new Rect(location[0], location[1], location[0]
132 + mAnchor.getWidth(), location[1] + mAnchor.getHeight());
133
134 root.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
135 LayoutParams.WRAP_CONTENT));
136 root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
137
138 int rootW = root.getWidth(), rootH = root.getHeight();
139 int screenW = mWManager.getDefaultDisplay().getWidth();
140
141 int xpos = ((screenW - rootW) / 2) + x;
142 int ypos = anchorRect.top - rootH + y;
143
144 if (rootH > anchorRect.top) {
145 ypos = anchorRect.bottom + y;
146 }
147 mWindow.showAtLocation(mAnchor, Gravity.NO_GRAVITY, xpos, ypos);
148 }
149
150 public void dismiss() {
151 mWindow.dismiss();
152 }
153
154 }