1 /* ownCloud Android client application
3 * @author Lorensius. W. T
4 * Copyright (C) 2011 Bartek Przybylski
5 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
20 package com
.owncloud
.android
.ui
;
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
;
36 * Represents a custom PopupWindows
38 public class CustomPopup
{
39 protected final View mAnchor
;
40 protected final PopupWindow mWindow
;
42 private Drawable background
= null
;
43 protected final WindowManager mWManager
;
45 public CustomPopup(View anchor
) {
47 mWindow
= new PopupWindow(anchor
.getContext());
49 mWindow
.setTouchInterceptor(new OnTouchListener() {
51 public boolean onTouch(View v
, MotionEvent event
) {
52 if (event
.getAction() == MotionEvent
.ACTION_OUTSIDE
) {
53 CustomPopup
.this.dismiss();
60 mWManager
= (WindowManager
) anchor
.getContext().getSystemService(
61 Context
.WINDOW_SERVICE
);
65 public void onCreate() {
68 public void onShow() {
71 public void preShow() {
73 throw new IllegalStateException(
74 "setContentView called with a view to display");
79 if (background
== null
) {
80 mWindow
.setBackgroundDrawable(new BitmapDrawable());
82 mWindow
.setBackgroundDrawable(background
);
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
);
91 mWindow
.setContentView(root
);
94 public void setBackgroundDrawable(Drawable background
) {
95 this.background
= background
;
98 public void setContentView(View root
) {
100 mWindow
.setContentView(root
);
103 public void setContentView(int layoutResId
) {
104 LayoutInflater inflater
= (LayoutInflater
) mAnchor
.getContext()
105 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
106 setContentView(inflater
.inflate(layoutResId
, null
));
109 public void showDropDown() {
113 public void showDropDown(int x
, int y
) {
115 mWindow
.setAnimationStyle(android
.R
.style
.Animation_Dialog
);
116 mWindow
.showAsDropDown(mAnchor
, x
, y
);
119 public void showLikeQuickAction() {
120 showLikeQuickAction(0, 0);
123 public void showLikeQuickAction(int x
, int y
) {
126 mWindow
.setAnimationStyle(android
.R
.style
.Animation_Dialog
);
127 int[] location
= new int[2];
128 mAnchor
.getLocationOnScreen(location
);
130 Rect anchorRect
= new Rect(location
[0], location
[1], location
[0]
131 + mAnchor
.getWidth(), location
[1] + mAnchor
.getHeight());
133 root
.setLayoutParams(new LayoutParams(LayoutParams
.WRAP_CONTENT
,
134 LayoutParams
.WRAP_CONTENT
));
135 root
.measure(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
);
137 int rootW
= root
.getWidth(), rootH
= root
.getHeight();
138 int screenW
= mWManager
.getDefaultDisplay().getWidth();
140 int xpos
= ((screenW
- rootW
) / 2) + x
;
141 int ypos
= anchorRect
.top
- rootH
+ y
;
143 if (rootH
> anchorRect
.top
) {
144 ypos
= anchorRect
.bottom
+ y
;
146 mWindow
.showAtLocation(mAnchor
, Gravity
.NO_GRAVITY
, xpos
, ypos
);
149 public void dismiss() {