Refactoring: Added comments to every class, as well as a copyright
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / 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 eu.alefzero.owncloud.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 * @author Lorensius. W. T
36 *
37 */
38 public class CustomPopup {
39 protected final View mAnchor;
40 protected final PopupWindow mWindow;
41 private View root;
42 private Drawable background = null;
43 protected final WindowManager mWManager;
44
45 public CustomPopup(View anchor) {
46 mAnchor = anchor;
47 mWindow = new PopupWindow(anchor.getContext());
48
49 mWindow.setTouchInterceptor(new OnTouchListener() {
50
51 public boolean onTouch(View v, MotionEvent event) {
52 if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
53 CustomPopup.this.dismiss();
54 return true;
55 }
56 return false;
57 }
58 });
59
60 mWManager = (WindowManager) anchor.getContext().getSystemService(Context.WINDOW_SERVICE);
61 onCreate();
62 }
63
64
65 public void onCreate() {}
66 public void onShow() {}
67
68 public void preShow() {
69 if (root == null) {
70 throw new IllegalStateException("setContentView called with a view to display");
71 }
72
73 onShow();
74
75 if (background == null) {
76 mWindow.setBackgroundDrawable(new BitmapDrawable());
77 } else {
78 mWindow.setBackgroundDrawable(background);
79 }
80
81 mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
82 mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
83 mWindow.setTouchable(true);
84 mWindow.setFocusable(true);
85 mWindow.setOutsideTouchable(true);
86
87 mWindow.setContentView(root);
88 }
89
90 public void setBackgroundDrawable(Drawable background) {
91 this.background = background;
92 }
93
94 public void setContentView(View root) {
95 this.root = root;
96 mWindow.setContentView(root);
97 }
98
99 public void setContentView(int layoutResId) {
100 LayoutInflater inflater =
101 (LayoutInflater) mAnchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
102 setContentView(inflater.inflate(layoutResId, null));
103 }
104
105 public void showDropDown() {
106 showDropDown(0, 0);
107 }
108
109 public void showDropDown(int x, int y) {
110 preShow();
111 mWindow.setAnimationStyle(android.R.style.Animation_Dialog);
112 mWindow.showAsDropDown(mAnchor, x, y);
113 }
114
115 public void showLikeQuickAction() {
116 showLikeQuickAction(0, 0);
117 }
118
119 public void showLikeQuickAction(int x, int y) {
120 preShow();
121
122 mWindow.setAnimationStyle(android.R.style.Animation_Dialog);
123 int[] location = new int[2];
124 mAnchor.getLocationOnScreen(location);
125
126 Rect anchorRect =
127 new Rect(location[0], location[1], location[0] + mAnchor.getWidth(), location[1] + mAnchor.getHeight());
128
129 root.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
130 root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
131
132 int rootW = root.getWidth(), rootH = root.getHeight();
133 int screenW = mWManager.getDefaultDisplay().getWidth();
134
135 int xpos = ((screenW-rootW)/2) + x;
136 int ypos = anchorRect.top - rootH + y;
137
138 if (rootH > anchorRect.top) {
139 ypos = anchorRect.bottom + y;
140 }
141 mWindow.showAtLocation(mAnchor, Gravity.NO_GRAVITY, xpos, ypos);
142 }
143
144 public void dismiss() {
145 mWindow.dismiss();
146 }
147
148 }