PopWindowUtil.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. package com.edufound.reader.popwindow;
  2. import android.app.Activity;
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.graphics.Color;
  6. import android.view.Display;
  7. import android.view.Gravity;
  8. import android.view.KeyEvent;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.Window;
  12. import android.view.WindowManager;
  13. import android.widget.FrameLayout;
  14. import android.widget.ImageView;
  15. import android.widget.PopupWindow;
  16. import android.widget.TextView;
  17. import com.edufound.reader.R;
  18. import com.edufound.reader.application.EApplication;
  19. import com.edufound.reader.bean.ChannelCodeEnum;
  20. import com.edufound.reader.bean.RecordResultBean;
  21. import com.edufound.reader.bean.UserRecordBean;
  22. import com.edufound.reader.cusview.GridRadioGroup;
  23. import com.edufound.reader.listener.PopUtilClickListener;
  24. import com.edufound.reader.listener.PopupRecordStatusListener;
  25. import com.edufound.reader.presenter.PopWindowPresneter;
  26. import com.edufound.reader.util.Consts;
  27. import com.edufound.reader.util.DeviceUtil;
  28. import com.edufound.reader.util.DeviceUuidFactory;
  29. import com.edufound.reader.util.GlideUtils;
  30. import com.edufound.reader.util.SizeUtils;
  31. import com.jakewharton.rxbinding4.view.RxView;
  32. import com.orhanobut.logger.Logger;
  33. import com.trello.rxlifecycle4.android.ActivityEvent;
  34. import java.util.List;
  35. import java.util.concurrent.TimeUnit;
  36. import androidx.annotation.Nullable;
  37. import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
  38. import io.reactivex.rxjava3.annotations.NonNull;
  39. import io.reactivex.rxjava3.core.Observable;
  40. import io.reactivex.rxjava3.core.Observer;
  41. import io.reactivex.rxjava3.functions.Consumer;
  42. import io.reactivex.rxjava3.functions.Function;
  43. import kotlin.Unit;
  44. import static com.trello.rxlifecycle4.RxLifecycle.bindUntilEvent;
  45. public class PopWindowUtil {
  46. private static PopWindowPresneter mPresenter;
  47. private static PopupWindow mPopupWindow;
  48. private static FrameLayout dialog_view;
  49. private static PopupWindow recordPopupWindow;
  50. private static FrameLayout record_view;
  51. private static PopupWindow eventPopupWindow;
  52. private static FrameLayout event_view;
  53. private static void initPresenter() {
  54. if (mPresenter == null) {
  55. mPresenter = new PopWindowPresneter();
  56. }
  57. }
  58. private static boolean checkWindowShoing() {
  59. if (mPopupWindow == null) {
  60. return false;
  61. }
  62. return mPopupWindow.isShowing();
  63. }
  64. public static void hidePopupWindow() {
  65. if (mPopupWindow.isShowing()) {
  66. mPopupWindow.dismiss();
  67. }
  68. }
  69. public static void showExitAppWindow(Activity context, View parent) {
  70. initPresenter();
  71. if (checkWindowShoing()) {
  72. return;
  73. }
  74. dialog_view = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.popupwindow_exit_app, null);
  75. dialog_view.setFocusable(true);
  76. ImageView image = dialog_view.findViewById(R.id.popupwindow_exit_app_image);
  77. GlideUtils.loadImageSizeKipMemoryCache(context, "http://reader-apk.ai160.com/reader-apk/res/exit_app.png", image);
  78. FrameLayout exit = dialog_view.findViewById(R.id.popupwindow_exit_app_ok);
  79. FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_exit_app_cancel);
  80. setClickListener(exit, o -> {
  81. EApplication.killAppProcess(context);
  82. });
  83. setClickListener(cancel, o -> {
  84. mPopupWindow.dismiss();
  85. });
  86. RxView.longClicks(image).subscribe(o -> {
  87. //显示deviceInfo
  88. showDeviceInfo(context);
  89. });
  90. newPopupWindow(parent, true);
  91. }
  92. public static void showSelectGradeWindow(Context context, View parent, int checked, PopUtilClickListener listener) {
  93. initPresenter();
  94. if (checkWindowShoing()) {
  95. return;
  96. }
  97. dialog_view = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.popupwindow_select_grade, null);
  98. dialog_view.setFocusable(true);
  99. FrameLayout btn_ok = dialog_view.findViewById(R.id.popupwindow_select_ok);
  100. GridRadioGroup gridRadioGroup = dialog_view.findViewById(R.id.popupwindow_select_grade_gridgroup);
  101. int id = 0;
  102. final String[] checkText = {String.valueOf(id)};
  103. switch (checked) {
  104. case 0:
  105. id = R.id.popupwindow_select_grade_one;
  106. checkText[0] = "一年级";
  107. break;
  108. case 1:
  109. id = R.id.popupwindow_select_grade_two;
  110. checkText[0] = "二年级";
  111. break;
  112. case 2:
  113. id = R.id.popupwindow_select_grade_three;
  114. checkText[0] = "三年级";
  115. break;
  116. case 3:
  117. id = R.id.popupwindow_select_grade_four;
  118. checkText[0] = "四年级";
  119. break;
  120. case 4:
  121. id = R.id.popupwindow_select_grade_pre;
  122. checkText[0] = "学前";
  123. break;
  124. }
  125. gridRadioGroup.setOnCheckedChangeListener(new GridRadioGroup.OnCheckedChangeListener() {
  126. @Override
  127. public void onCheckedChanged(GridRadioGroup group, int checkedId) {
  128. group.check(checkedId);
  129. switch (checkedId) {
  130. case R.id.popupwindow_select_grade_one:
  131. //一年级
  132. checkText[0] = "一年级";
  133. break;
  134. case R.id.popupwindow_select_grade_two:
  135. checkText[0] = "二年级";
  136. //二年级
  137. break;
  138. case R.id.popupwindow_select_grade_three:
  139. checkText[0] = "三年级";
  140. //三年级
  141. break;
  142. case R.id.popupwindow_select_grade_four:
  143. checkText[0] = "四年级";
  144. //四年级
  145. break;
  146. case R.id.popupwindow_select_grade_pre:
  147. //学前
  148. checkText[0] = "学前";
  149. break;
  150. }
  151. }
  152. });
  153. gridRadioGroup.check(id);
  154. setClickListener(btn_ok, o -> {
  155. mPopupWindow.dismiss();
  156. listener.clickSubmit(checkText[0]);
  157. });
  158. newPopupWindow(parent, true);
  159. }
  160. public static void showExitLoginWindow(Context context, View parent, PopUtilClickListener listener) {
  161. initPresenter();
  162. if (checkWindowShoing()) {
  163. return;
  164. }
  165. dialog_view = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.popupwindow_exit_login, null);
  166. dialog_view.setFocusable(true);
  167. ImageView image = dialog_view.findViewById(R.id.popupwindow_exit_login_image);
  168. GlideUtils.loadImageSizeKipMemoryCache(context, "http://reader-apk.ai160.com/reader-apk/res/exit_login.png", image);
  169. FrameLayout exit = dialog_view.findViewById(R.id.popupwindow_exit_login_ok);
  170. FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_exit_login_cancel);
  171. setClickListener(exit, o -> {
  172. mPopupWindow.dismiss();
  173. listener.clickSubmit(null);
  174. });
  175. setClickListener(cancel, o -> {
  176. mPopupWindow.dismiss();
  177. listener.clickCancel();
  178. });
  179. newPopupWindow(parent, true);
  180. }
  181. public static void showCancellationAccountWindow(Context context, View parent, PopUtilClickListener listener) {
  182. initPresenter();
  183. if (checkWindowShoing()) {
  184. return;
  185. }
  186. dialog_view = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.popupwindow_cancellation_account, null);
  187. dialog_view.setFocusable(true);
  188. FrameLayout exit = dialog_view.findViewById(R.id.popupwindow_cancellation_account_ok);
  189. FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_cancellation_account_cancel);
  190. setClickListener(exit, o -> {
  191. mPopupWindow.dismiss();
  192. listener.clickSubmit(null);
  193. });
  194. setClickListener(cancel, o -> {
  195. mPopupWindow.dismiss();
  196. listener.clickCancel();
  197. });
  198. newPopupWindow(parent, true);
  199. }
  200. public static void showBindWeChatWindow(Activity context, View parent, @Nullable PopUtilClickListener listener) {
  201. initPresenter();
  202. if (checkWindowShoing()) {
  203. return;
  204. }
  205. dialog_view = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.popupwindow_bind_wechat, null);
  206. dialog_view.setFocusable(true);
  207. ImageView back = dialog_view.findViewById(R.id.popupwindow_bindwechat_back);
  208. ImageView image = dialog_view.findViewById(R.id.popupwindow_bind_wechat_image);
  209. TextView text = dialog_view.findViewById(R.id.popupwindow_bind_wechat_name);
  210. setClickListener(back, o -> {
  211. mPopupWindow.dismiss();
  212. if (listener != null) {
  213. listener.clickCancel();
  214. }
  215. mPresenter.getPopHandler().removeMessages(0x1312);
  216. });
  217. mPresenter.getBindWeChatPath(context, image);
  218. mPresenter.getMyInfo(context, text);
  219. newPopupWindow(parent, false);
  220. }
  221. public static void showRecordStatusWindow(Activity context, View parent, RecordResultBean bean, UserRecordBean.UserRead userread, PopupRecordStatusListener listener) {
  222. initPresenter();
  223. if (recordPopupWindow != null) {
  224. recordPopupWindow = null;
  225. }
  226. if (record_view != null) {
  227. record_view = null;
  228. }
  229. record_view = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.popupwindow_record_status, null);
  230. record_view.setFocusable(true);
  231. record_view.setOnKeyListener(new View.OnKeyListener() {
  232. @Override
  233. public boolean onKey(View v, int keyCode, KeyEvent event) {
  234. if (keyCode == KeyEvent.KEYCODE_BACK) {
  235. return true;
  236. }
  237. return false;
  238. }
  239. });
  240. ImageView back = record_view.findViewById(R.id.popupwindow_record_status_close);
  241. setClickListener(back, o -> {
  242. recordPopupWindow.dismiss();
  243. });
  244. mPresenter.initRecordStatusWindow(context, record_view, bean, userread, listener);
  245. recordPopupWindow = new PopupWindow(record_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  246. recordPopupWindow.setFocusable(false);
  247. recordPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  248. recordPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
  249. @Override
  250. public void onDismiss() {
  251. record_view.removeAllViews();
  252. record_view = null;
  253. recordPopupWindow = null;
  254. }
  255. });
  256. }
  257. public static void showMyOrderWindow(Activity context, View parent) {
  258. initPresenter();
  259. if (checkWindowShoing()) {
  260. mPopupWindow.dismiss();
  261. return;
  262. }
  263. dialog_view = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.popupwindow_myorder, null);
  264. dialog_view.setFocusable(true);
  265. dialog_view.setOnKeyListener(new View.OnKeyListener() {
  266. @Override
  267. public boolean onKey(View v, int keyCode, KeyEvent event) {
  268. if (keyCode == KeyEvent.KEYCODE_BACK) {
  269. return true;
  270. }
  271. return false;
  272. }
  273. });
  274. ImageView back = dialog_view.findViewById(R.id.popupwindow_myorder_back);
  275. setClickListener(back, o -> {
  276. mPopupWindow.dismiss();
  277. });
  278. mPresenter.myOrderWindowInit(context, dialog_view);
  279. newPopupWindow(parent, false);
  280. }
  281. public static void showRecordRemovedWindow(Context context, View parent, PopUtilClickListener listener) {
  282. initPresenter();
  283. if (checkWindowShoing()) {
  284. return;
  285. }
  286. dialog_view = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.popupwindow_record_remove, null);
  287. dialog_view.setFocusable(true);
  288. FrameLayout ok = dialog_view.findViewById(R.id.popupwindow_record_remove_ok);
  289. FrameLayout cancel = dialog_view.findViewById(R.id.popupwindow_record_remove_cancel);
  290. setClickListener(ok, o -> {
  291. mPopupWindow.dismiss();
  292. listener.clickSubmit(null);
  293. });
  294. setClickListener(cancel, o -> {
  295. mPopupWindow.dismiss();
  296. listener.clickCancel();
  297. });
  298. newPopupWindow(parent, true);
  299. }
  300. /**
  301. * 注冊送7天活动的popupwindow
  302. */
  303. public static void RegistrationEventsWindow(Context context, View parent, PopUtilClickListener listener) {
  304. initPresenter();
  305. if (checkWindowShoing()) {
  306. return;
  307. }
  308. dialog_view = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.popupwindow_registration_events, null);
  309. dialog_view.setFocusable(true);
  310. ImageView img = dialog_view.findViewById(R.id.popupwindow_registration_events_img);
  311. GlideUtils.loadImageSizeKipMemoryCache(context, "http://reader-apk.ai160.com/reader-apk/res/login_event.png", img);
  312. ImageView close = dialog_view.findViewById(R.id.popupwindow_registration_events_close);
  313. setClickListener(img, o -> {
  314. mPopupWindow.dismiss();
  315. listener.clickSubmit(null);
  316. });
  317. setClickListener(close, o -> {
  318. mPopupWindow.dismiss();
  319. listener.clickCancel();
  320. });
  321. newPopupWindow(parent, true);
  322. }
  323. /**
  324. * 跳转webview的活动
  325. */
  326. public static void webviewEventsWindow(Context context, View parent, PopUtilClickListener listener, boolean needwindow) {
  327. initPresenter();
  328. if (!needwindow) {
  329. listener.clickSubmit(null);
  330. return;
  331. }
  332. event_view = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.popupwindow_webs_events, null);
  333. event_view.setFocusable(true);
  334. ImageView img = event_view.findViewById(R.id.popupwindow_webs_events_img);
  335. GlideUtils.loadImageSizeKipMemoryCache(context, "http://reader-apk.ai160.com/reader-apk/res/event_img.png", img);
  336. ImageView close = event_view.findViewById(R.id.popupwindow_webs_events_close);
  337. setClickListener(img, o -> {
  338. eventPopupWindow.dismiss();
  339. listener.clickSubmit(null);
  340. });
  341. setClickListener(close, o -> {
  342. eventPopupWindow.dismiss();
  343. listener.clickCancel();
  344. });
  345. eventPopupWindow = new PopupWindow(event_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  346. eventPopupWindow.setFocusable(true);
  347. eventPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  348. eventPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
  349. @Override
  350. public void onDismiss() {
  351. event_view.removeAllViews();
  352. event_view = null;
  353. eventPopupWindow = null;
  354. }
  355. });
  356. }
  357. private static void showDeviceInfo(Activity context) {
  358. StringBuffer buffer = new StringBuffer();
  359. buffer.append("\tchannel:\t" + "\t" + Consts.getUmengChannel() + "\n");
  360. buffer.append("\tUUID:\t" + "\t" + DeviceUuidFactory.getUuid() + "\n");
  361. buffer.append("\t厂商:\t" + "\t" + DeviceUtil.getDeviceBrand() + "\n");
  362. buffer.append("\t型号:\t" + "\t" + DeviceUtil.getSystemModel() + "\n");
  363. buffer.append("\t系统版本号:\t" + "\t" + DeviceUtil.getSystemVersion() + "\n");
  364. buffer.append("\t应用版本号:\t" + "\t" + DeviceUtil.getVersionCode(context) + "\n");
  365. buffer.append("\t应用版本号名称:\t" + "\t" + DeviceUtil.getVersionName(context) + "\n");
  366. buffer.append("\t屏幕宽度:\t\t" + DeviceUtil.getWidth(context) + "\n");
  367. buffer.append("\t屏幕高度:\t\t" + DeviceUtil.getHeight(context) + "\n");
  368. buffer.append("\t屏幕密度:\t\t" + DeviceUtil.getDensity(context) + "\n");
  369. buffer.append("\t屏幕DPI:\t\t" + DeviceUtil.getDensityDpi(context) + "\n");
  370. if (Consts.getUmengChannel().equals(ChannelCodeEnum.BAIDU.getChannelCode()) && Consts.getmConstsBaiDuUserBean() != null) {
  371. buffer.append("\t百度UserId:\t\t" + Consts.getmConstsBaiDuUserBean().getUserInfo().getUserId() + "\n");
  372. }
  373. View infoView = LayoutInflater.from(context).inflate(R.layout.window_deviceinfo, null);
  374. TextView text = (TextView) infoView.findViewById(R.id.deviceinfo);
  375. text.setText(buffer.toString());
  376. text.setTextSize(SizeUtils.dp2px(context, 20));
  377. text.setTextColor(Color.WHITE);
  378. Dialog dialog = new Dialog(context);
  379. dialog.setContentView(infoView);
  380. Window dialogWindow = dialog.getWindow();
  381. dialogWindow.setGravity(Gravity.CENTER);
  382. WindowManager m = context.getWindowManager();
  383. Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
  384. WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
  385. p.height = (int) (d.getHeight() * 0.8);
  386. p.width = (int) (d.getWidth() * 0.7);
  387. dialogWindow.setAttributes(p);
  388. dialog.setTitle("Device信息");
  389. dialog.show();
  390. }
  391. private static void setClickListener(View view, Consumer onNext) {
  392. RxView.clicks(view).throttleFirst(2, TimeUnit.SECONDS).subscribe(onNext);
  393. }
  394. private static void newPopupWindow(View parent, boolean nedFocus) {
  395. mPopupWindow = new PopupWindow(dialog_view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  396. mPopupWindow.setFocusable(nedFocus);
  397. mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
  398. mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
  399. @Override
  400. public void onDismiss() {
  401. dialog_view.removeAllViews();
  402. dialog_view = null;
  403. mPopupWindow = null;
  404. }
  405. });
  406. }
  407. }