CustomPopup.java 4.7 KB

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