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