CustomPopup.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author Lorensius. W. T
  5. * Copyright (C) 2011 Bartek Przybylski
  6. * Copyright (C) 2015 ownCloud Inc.
  7. *
  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.
  11. *
  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.
  16. *
  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/>.
  19. */
  20. 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.View.OnTouchListener;
  30. import android.view.ViewGroup.LayoutParams;
  31. import android.view.WindowManager;
  32. import android.widget.PopupWindow;
  33. /**
  34. * Represents a custom PopupWindows
  35. */
  36. public class CustomPopup {
  37. protected final View mAnchor;
  38. protected final PopupWindow mWindow;
  39. private View root;
  40. private Drawable background = null;
  41. protected final WindowManager mWManager;
  42. public CustomPopup(View anchor) {
  43. mAnchor = anchor;
  44. mWindow = new PopupWindow(anchor.getContext());
  45. mWindow.setTouchInterceptor(new OnTouchListener() {
  46. public boolean onTouch(View v, MotionEvent event) {
  47. if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
  48. CustomPopup.this.dismiss();
  49. return true;
  50. }
  51. return false;
  52. }
  53. });
  54. mWManager = (WindowManager) anchor.getContext().getSystemService(
  55. Context.WINDOW_SERVICE);
  56. onCreate();
  57. }
  58. public void onCreate() {
  59. // not used at the moment
  60. }
  61. public void onShow() {
  62. // not used at the moment
  63. }
  64. public void preShow() {
  65. if (root == null) {
  66. throw new IllegalStateException(
  67. "setContentView called with a view to display");
  68. }
  69. onShow();
  70. if (background == null) {
  71. mWindow.setBackgroundDrawable(new BitmapDrawable());
  72. } else {
  73. mWindow.setBackgroundDrawable(background);
  74. }
  75. mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
  76. mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
  77. mWindow.setTouchable(true);
  78. mWindow.setFocusable(true);
  79. mWindow.setOutsideTouchable(true);
  80. mWindow.setContentView(root);
  81. }
  82. public void setBackgroundDrawable(Drawable background) {
  83. this.background = background;
  84. }
  85. public void setContentView(View root) {
  86. this.root = root;
  87. mWindow.setContentView(root);
  88. }
  89. public void setContentView(int layoutResId) {
  90. LayoutInflater inflater = (LayoutInflater) mAnchor.getContext()
  91. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  92. setContentView(inflater.inflate(layoutResId, null));
  93. }
  94. public void showDropDown() {
  95. showDropDown(0, 0);
  96. }
  97. public void showDropDown(int x, int y) {
  98. preShow();
  99. mWindow.setAnimationStyle(android.R.style.Animation_Dialog);
  100. mWindow.showAsDropDown(mAnchor, x, y);
  101. }
  102. public void showLikeQuickAction() {
  103. showLikeQuickAction(0, 0);
  104. }
  105. public void showLikeQuickAction(int x, int y) {
  106. preShow();
  107. mWindow.setAnimationStyle(android.R.style.Animation_Dialog);
  108. int[] location = new int[2];
  109. mAnchor.getLocationOnScreen(location);
  110. Rect anchorRect = new Rect(location[0], location[1], location[0]
  111. + mAnchor.getWidth(), location[1] + mAnchor.getHeight());
  112. root.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
  113. LayoutParams.WRAP_CONTENT));
  114. root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  115. int rootW = root.getWidth();
  116. int rootH = root.getHeight();
  117. int screenW = mWManager.getDefaultDisplay().getWidth();
  118. int xpos = ((screenW - rootW) / 2) + x;
  119. int ypos = anchorRect.top - rootH + y;
  120. if (rootH > anchorRect.top) {
  121. ypos = anchorRect.bottom + y;
  122. }
  123. mWindow.showAtLocation(mAnchor, Gravity.NO_GRAVITY, xpos, ypos);
  124. }
  125. public void dismiss() {
  126. mWindow.dismiss();
  127. }
  128. }