2  *   ownCloud Android client application 
   4  *   @author Lorensius. W. T 
   5  *   Copyright (C) 2011  Bartek Przybylski 
   6  *   Copyright (C) 2015 ownCloud Inc. 
   8  *   This program is free software: you can redistribute it and/or modify 
   9  *   it under the terms of the GNU General Public License version 2, 
  10  *   as published by the Free Software Foundation. 
  12  *   This program is distributed in the hope that it will be useful, 
  13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  15  *   GNU General Public License for more details. 
  17  *   You should have received a copy of the GNU General Public License 
  18  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  21 package com
.owncloud
.android
.ui
; 
  23 import android
.content
.Context
; 
  24 import android
.graphics
.Rect
; 
  25 import android
.graphics
.drawable
.BitmapDrawable
; 
  26 import android
.graphics
.drawable
.Drawable
; 
  27 import android
.view
.Gravity
; 
  28 import android
.view
.LayoutInflater
; 
  29 import android
.view
.MotionEvent
; 
  30 import android
.view
.View
; 
  31 import android
.view
.WindowManager
; 
  32 import android
.view
.View
.OnTouchListener
; 
  33 import android
.view
.ViewGroup
.LayoutParams
; 
  34 import android
.widget
.PopupWindow
; 
  37  * Represents a custom PopupWindows 
  39 public class CustomPopup 
{ 
  40     protected final View mAnchor
; 
  41     protected final PopupWindow mWindow
; 
  43     private Drawable background 
= null
; 
  44     protected final WindowManager mWManager
; 
  46     public CustomPopup(View anchor
) { 
  48         mWindow 
= new PopupWindow(anchor
.getContext()); 
  50         mWindow
.setTouchInterceptor(new OnTouchListener() { 
  52             public boolean onTouch(View v
, MotionEvent event
) { 
  53                 if (event
.getAction() == MotionEvent
.ACTION_OUTSIDE
) { 
  54                     CustomPopup
.this.dismiss(); 
  61         mWManager 
= (WindowManager
) anchor
.getContext().getSystemService( 
  62                 Context
.WINDOW_SERVICE
); 
  66     public void onCreate() { 
  69     public void onShow() { 
  72     public void preShow() { 
  74             throw new IllegalStateException( 
  75                     "setContentView called with a view to display"); 
  80         if (background 
== null
) { 
  81             mWindow
.setBackgroundDrawable(new BitmapDrawable()); 
  83             mWindow
.setBackgroundDrawable(background
); 
  86         mWindow
.setWidth(WindowManager
.LayoutParams
.WRAP_CONTENT
); 
  87         mWindow
.setHeight(WindowManager
.LayoutParams
.WRAP_CONTENT
); 
  88         mWindow
.setTouchable(true
); 
  89         mWindow
.setFocusable(true
); 
  90         mWindow
.setOutsideTouchable(true
); 
  92         mWindow
.setContentView(root
); 
  95     public void setBackgroundDrawable(Drawable background
) { 
  96         this.background 
= background
; 
  99     public void setContentView(View root
) { 
 101         mWindow
.setContentView(root
); 
 104     public void setContentView(int layoutResId
) { 
 105         LayoutInflater inflater 
= (LayoutInflater
) mAnchor
.getContext() 
 106                 .getSystemService(Context
.LAYOUT_INFLATER_SERVICE
); 
 107         setContentView(inflater
.inflate(layoutResId
, null
)); 
 110     public void showDropDown() { 
 114     public void showDropDown(int x
, int y
) { 
 116         mWindow
.setAnimationStyle(android
.R
.style
.Animation_Dialog
); 
 117         mWindow
.showAsDropDown(mAnchor
, x
, y
); 
 120     public void showLikeQuickAction() { 
 121         showLikeQuickAction(0, 0); 
 124     public void showLikeQuickAction(int x
, int y
) { 
 127         mWindow
.setAnimationStyle(android
.R
.style
.Animation_Dialog
); 
 128         int[] location 
= new int[2]; 
 129         mAnchor
.getLocationOnScreen(location
); 
 131         Rect anchorRect 
= new Rect(location
[0], location
[1], location
[0] 
 132                 + mAnchor
.getWidth(), location
[1] + mAnchor
.getHeight()); 
 134         root
.setLayoutParams(new LayoutParams(LayoutParams
.WRAP_CONTENT
, 
 135                 LayoutParams
.WRAP_CONTENT
)); 
 136         root
.measure(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
); 
 138         int rootW 
= root
.getWidth(), rootH 
= root
.getHeight(); 
 139         int screenW 
= mWManager
.getDefaultDisplay().getWidth(); 
 141         int xpos 
= ((screenW 
- rootW
) / 2) + x
; 
 142         int ypos 
= anchorRect
.top 
- rootH 
+ y
; 
 144         if (rootH 
> anchorRect
.top
) { 
 145             ypos 
= anchorRect
.bottom 
+ y
; 
 147         mWindow
.showAtLocation(mAnchor
, Gravity
.NO_GRAVITY
, xpos
, ypos
); 
 150     public void dismiss() {