PopWindowUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package com.edufound.reader.popwindow;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.view.Gravity;
  5. import android.view.KeyEvent;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.widget.FrameLayout;
  9. import android.widget.ImageView;
  10. import android.widget.PopupWindow;
  11. import com.edufound.reader.R;
  12. import com.edufound.reader.application.EApplication;
  13. import com.edufound.reader.bean.RecordResultBean;
  14. import com.edufound.reader.bean.UserRecordBean;
  15. import com.edufound.reader.cusview.GridRadioGroup;
  16. import com.edufound.reader.listener.PopUtilClickListener;
  17. import com.edufound.reader.listener.PopupRecordStatusListener;
  18. import com.edufound.reader.presenter.PopWindowPresneter;
  19. import com.edufound.reader.util.GlideUtils;
  20. import com.jakewharton.rxbinding4.view.RxView;
  21. import com.orhanobut.logger.Logger;
  22. import java.util.concurrent.TimeUnit;
  23. import io.reactivex.rxjava3.functions.Consumer;
  24. public class PopWindowUtil {
  25. private static PopWindowPresneter mPresenter;
  26. private static PopupWindow mPopupWindow;
  27. private static void initPresenter() {
  28. if (mPresenter == null) {
  29. mPresenter = new PopWindowPresneter();
  30. }
  31. }
  32. private static boolean checkWindowShoing() {
  33. if (mPopupWindow == null) {
  34. return false;
  35. }
  36. return mPopupWindow.isShowing();
  37. }
  38. public static void hidePopupWindow() {
  39. if (mPopupWindow.isShowing()) {
  40. mPopupWindow.dismiss();
  41. }
  42. }
  43. public static void showExitAppWindow(Context context, View parent) {
  44. initPresenter();
  45. if (checkWindowShoing()) {
  46. return;
  47. }
  48. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_exit_app, null);
  49. dialog_view.setFocusable(true);
  50. ImageView image = dialog_view.findViewById(R.id.popupwindow_exit_app_image);
  51. GlideUtils.loadImageSizeKipMemoryCache(context, "http://reader-apk.ai160.com/reader-apk/res/exit_app.png", image);
  52. FrameLayout exit = dialog_view.findViewById(R.id.popupwindow_exit_app_ok);
  53. FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_exit_app_cancel);
  54. setClickListener(exit, o -> {
  55. EApplication.killAppProcess(context);
  56. });
  57. setClickListener(cancel, o -> {
  58. mPopupWindow.dismiss();
  59. });
  60. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  61. mPopupWindow.setFocusable(true);
  62. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  63. }
  64. public static void showSelectGradeWindow(Context context, View parent, int checked, PopUtilClickListener listener) {
  65. initPresenter();
  66. if (checkWindowShoing()) {
  67. return;
  68. }
  69. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_select_grade, null);
  70. dialog_view.setFocusable(true);
  71. FrameLayout btn_ok = dialog_view.findViewById(R.id.popupwindow_select_ok);
  72. GridRadioGroup gridRadioGroup = dialog_view.findViewById(R.id.popupwindow_select_grade_gridgroup);
  73. int id = 0;
  74. final String[] checkText = {String.valueOf(id)};
  75. switch (checked) {
  76. case 0:
  77. id = R.id.popupwindow_select_grade_one;
  78. checkText[0] = "一年级";
  79. break;
  80. case 1:
  81. id = R.id.popupwindow_select_grade_two;
  82. checkText[0] = "二年级";
  83. break;
  84. case 2:
  85. id = R.id.popupwindow_select_grade_three;
  86. checkText[0] = "三年级";
  87. break;
  88. case 3:
  89. id = R.id.popupwindow_select_grade_four;
  90. checkText[0] = "四年级";
  91. break;
  92. case 4:
  93. id = R.id.popupwindow_select_grade_pre;
  94. checkText[0] = "学前";
  95. break;
  96. }
  97. gridRadioGroup.setOnCheckedChangeListener(new GridRadioGroup.OnCheckedChangeListener() {
  98. @Override
  99. public void onCheckedChanged(GridRadioGroup group, int checkedId) {
  100. Logger.e("checkedId:" + checkedId);
  101. group.check(checkedId);
  102. switch (checkedId) {
  103. case R.id.popupwindow_select_grade_one:
  104. //一年级
  105. checkText[0] = "一年级";
  106. break;
  107. case R.id.popupwindow_select_grade_two:
  108. checkText[0] = "二年级";
  109. //二年级
  110. break;
  111. case R.id.popupwindow_select_grade_three:
  112. checkText[0] = "三年级";
  113. //三年级
  114. break;
  115. case R.id.popupwindow_select_grade_four:
  116. checkText[0] = "四年级";
  117. //四年级
  118. break;
  119. case R.id.popupwindow_select_grade_pre:
  120. //学前
  121. checkText[0] = "学前";
  122. break;
  123. }
  124. }
  125. });
  126. gridRadioGroup.check(id);
  127. setClickListener(btn_ok, o -> {
  128. mPopupWindow.dismiss();
  129. listener.clickSubmit(checkText[0]);
  130. });
  131. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  132. mPopupWindow.setFocusable(true);
  133. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  134. }
  135. public static void showExitLoginWindow(Context context, View parent, PopUtilClickListener listener) {
  136. initPresenter();
  137. if (checkWindowShoing()) {
  138. return;
  139. }
  140. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_exit_login, null);
  141. dialog_view.setFocusable(true);
  142. ImageView image = dialog_view.findViewById(R.id.popupwindow_exit_login_image);
  143. GlideUtils.loadImageSizeKipMemoryCache(context, "http://reader-apk.ai160.com/reader-apk/res/exit_login.png", image);
  144. FrameLayout exit = dialog_view.findViewById(R.id.popupwindow_exit_login_ok);
  145. FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_exit_login_cancel);
  146. setClickListener(exit, o -> {
  147. listener.clickSubmit(null);
  148. });
  149. setClickListener(cancel, o -> {
  150. mPopupWindow.dismiss();
  151. listener.clickCancel();
  152. });
  153. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  154. mPopupWindow.setFocusable(true);
  155. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  156. }
  157. public static void showCancellationAccountWindow(Context context, View parent, PopUtilClickListener listener) {
  158. initPresenter();
  159. if (checkWindowShoing()) {
  160. return;
  161. }
  162. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_cancellation_account, null);
  163. dialog_view.setFocusable(true);
  164. FrameLayout exit = dialog_view.findViewById(R.id.popupwindow_cancellation_account_ok);
  165. FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_cancellation_account_cancel);
  166. setClickListener(exit, o -> {
  167. listener.clickSubmit(null);
  168. });
  169. setClickListener(cancel, o -> {
  170. mPopupWindow.dismiss();
  171. listener.clickCancel();
  172. });
  173. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  174. mPopupWindow.setFocusable(true);
  175. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  176. }
  177. public static void showBindWeChatWindow(Activity context, View parent, PopUtilClickListener listener) {
  178. initPresenter();
  179. if (checkWindowShoing()) {
  180. return;
  181. }
  182. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_bind_wechat, null);
  183. dialog_view.setFocusable(true);
  184. ImageView back = dialog_view.findViewById(R.id.popupwindow_bindwechat_back);
  185. ImageView image = dialog_view.findViewById(R.id.popupwindow_bind_wechat_image);
  186. setClickListener(back, o -> {
  187. mPopupWindow.dismiss();
  188. listener.clickCancel();
  189. });
  190. mPresenter.getBindWeChatPath(context, image);
  191. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  192. mPopupWindow.setFocusable(true);
  193. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  194. }
  195. public static void showRecordStatusWindow(Activity context, View parent, RecordResultBean bean, UserRecordBean.UserRead userread, PopupRecordStatusListener listener) {
  196. initPresenter();
  197. if (checkWindowShoing()) {
  198. return;
  199. }
  200. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_record_status, null);
  201. dialog_view.setFocusable(true);
  202. dialog_view.setOnKeyListener(new View.OnKeyListener() {
  203. @Override
  204. public boolean onKey(View v, int keyCode, KeyEvent event) {
  205. if (keyCode == KeyEvent.KEYCODE_BACK) {
  206. return true;
  207. }
  208. return false;
  209. }
  210. });
  211. ImageView back = dialog_view.findViewById(R.id.popupwindow_record_status_close);
  212. setClickListener(back, o -> {
  213. mPopupWindow.dismiss();
  214. });
  215. mPresenter.initRecordStatusWindow(context, dialog_view, bean, userread, listener);
  216. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  217. mPopupWindow.setFocusable(false);
  218. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  219. }
  220. public static void showMyOrderWindow(Context context, View parent) {
  221. initPresenter();
  222. if (checkWindowShoing()) {
  223. return;
  224. }
  225. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_myorder, null);
  226. dialog_view.setFocusable(true);
  227. dialog_view.setOnKeyListener(new View.OnKeyListener() {
  228. @Override
  229. public boolean onKey(View v, int keyCode, KeyEvent event) {
  230. if (keyCode == KeyEvent.KEYCODE_BACK) {
  231. return true;
  232. }
  233. return false;
  234. }
  235. });
  236. ImageView back = dialog_view.findViewById(R.id.popupwindow_myorder_back);
  237. setClickListener(back, o -> {
  238. mPopupWindow.dismiss();
  239. });
  240. mPresenter.myOrderWindowInit(context, dialog_view);
  241. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  242. mPopupWindow.setFocusable(false);
  243. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  244. }
  245. public static void showRecordRemovedWindow(Context context, View parent, PopUtilClickListener listener) {
  246. initPresenter();
  247. if (checkWindowShoing()) {
  248. return;
  249. }
  250. View dialog_view = LayoutInflater.from(context).inflate(R.layout.popupwindow_record_remove, null);
  251. dialog_view.setFocusable(true);
  252. FrameLayout ok = dialog_view.findViewById(R.id.popupwindow_record_remove_ok);
  253. FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_record_remove_cancel);
  254. setClickListener(ok, o -> {
  255. mPopupWindow.dismiss();
  256. listener.clickSubmit(null);
  257. });
  258. setClickListener(cancel, o -> {
  259. mPopupWindow.dismiss();
  260. listener.clickCancel();
  261. });
  262. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  263. mPopupWindow.setFocusable(true);
  264. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  265. }
  266. private static void setClickListener(View view, Consumer onNext) {
  267. RxView.clicks(view).throttleFirst(2, TimeUnit.SECONDS).subscribe(onNext);
  268. }
  269. }