1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.ui
;
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
;
35 * Represents a custom PopupWindows
37 * @author Lorensius. W. T
40 public class CustomPopup
{
41 protected final View mAnchor
;
42 protected final PopupWindow mWindow
;
44 private Drawable background
= null
;
45 protected final WindowManager mWManager
;
47 public CustomPopup(View anchor
) {
49 mWindow
= new PopupWindow(anchor
.getContext());
51 mWindow
.setTouchInterceptor(new OnTouchListener() {
53 public boolean onTouch(View v
, MotionEvent event
) {
54 if (event
.getAction() == MotionEvent
.ACTION_OUTSIDE
) {
55 CustomPopup
.this.dismiss();
62 mWManager
= (WindowManager
) anchor
.getContext().getSystemService(
63 Context
.WINDOW_SERVICE
);
67 public void onCreate() {
70 public void onShow() {
73 public void preShow() {
75 throw new IllegalStateException(
76 "setContentView called with a view to display");
81 if (background
== null
) {
82 mWindow
.setBackgroundDrawable(new BitmapDrawable());
84 mWindow
.setBackgroundDrawable(background
);
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
);
93 mWindow
.setContentView(root
);
96 public void setBackgroundDrawable(Drawable background
) {
97 this.background
= background
;
100 public void setContentView(View root
) {
102 mWindow
.setContentView(root
);
105 public void setContentView(int layoutResId
) {
106 LayoutInflater inflater
= (LayoutInflater
) mAnchor
.getContext()
107 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
);
108 setContentView(inflater
.inflate(layoutResId
, null
));
111 public void showDropDown() {
115 public void showDropDown(int x
, int y
) {
117 mWindow
.setAnimationStyle(android
.R
.style
.Animation_Dialog
);
118 mWindow
.showAsDropDown(mAnchor
, x
, y
);
121 public void showLikeQuickAction() {
122 showLikeQuickAction(0, 0);
125 public void showLikeQuickAction(int x
, int y
) {
128 mWindow
.setAnimationStyle(android
.R
.style
.Animation_Dialog
);
129 int[] location
= new int[2];
130 mAnchor
.getLocationOnScreen(location
);
132 Rect anchorRect
= new Rect(location
[0], location
[1], location
[0]
133 + mAnchor
.getWidth(), location
[1] + mAnchor
.getHeight());
135 root
.setLayoutParams(new LayoutParams(LayoutParams
.WRAP_CONTENT
,
136 LayoutParams
.WRAP_CONTENT
));
137 root
.measure(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
);
139 int rootW
= root
.getWidth(), rootH
= root
.getHeight();
140 int screenW
= mWManager
.getDefaultDisplay().getWidth();
142 int xpos
= ((screenW
- rootW
) / 2) + x
;
143 int ypos
= anchorRect
.top
- rootH
+ y
;
145 if (rootH
> anchorRect
.top
) {
146 ypos
= anchorRect
.bottom
+ y
;
148 mWindow
.showAtLocation(mAnchor
, Gravity
.NO_GRAVITY
, xpos
, ypos
);
151 public void dismiss() {