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