1
0

BaseDialog.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. package com.kongzue.dialogx.interfaces;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.res.ColorStateList;
  6. import android.content.res.Configuration;
  7. import android.content.res.Resources;
  8. import android.graphics.Color;
  9. import android.os.Build;
  10. import android.os.Handler;
  11. import android.os.Looper;
  12. import android.text.TextUtils;
  13. import android.util.Log;
  14. import android.util.TypedValue;
  15. import android.view.LayoutInflater;
  16. import android.view.View;
  17. import android.view.ViewGroup;
  18. import android.view.WindowInsets;
  19. import android.view.inputmethod.InputMethodManager;
  20. import android.widget.EditText;
  21. import android.widget.FrameLayout;
  22. import android.widget.LinearLayout;
  23. import android.widget.TextView;
  24. import androidx.appcompat.app.AppCompatActivity;
  25. import androidx.fragment.app.FragmentManager;
  26. import com.kongzue.dialogx.DialogX;
  27. import com.kongzue.dialogx.R;
  28. import com.kongzue.dialogx.dialogs.PopTip;
  29. import com.kongzue.dialogx.impl.ActivityLifecycleImpl;
  30. import com.kongzue.dialogx.impl.DialogFragmentImpl;
  31. import com.kongzue.dialogx.util.ActivityRunnable;
  32. import com.kongzue.dialogx.util.DialogXFloatingWindowActivity;
  33. import com.kongzue.dialogx.util.TextInfo;
  34. import com.kongzue.dialogx.util.WindowUtil;
  35. import com.kongzue.dialogx.util.views.DialogXBaseRelativeLayout;
  36. import java.lang.ref.WeakReference;
  37. import java.util.ArrayList;
  38. import java.util.HashMap;
  39. import java.util.List;
  40. import java.util.Map;
  41. import java.util.concurrent.CopyOnWriteArrayList;
  42. import static com.kongzue.dialogx.DialogX.DEBUGMODE;
  43. /**
  44. * @author: Kongzue
  45. * @github: https://github.com/kongzue/
  46. * @homepage: http://kongzue.com/
  47. * @mail: myzcxhh@live.cn
  48. * @createTime: 2020/9/22 14:10
  49. */
  50. public abstract class BaseDialog {
  51. protected static WeakReference<Thread> uiThread;
  52. private static WeakReference<FrameLayout> rootFrameLayout;
  53. private static WeakReference<Activity> contextWeakReference;
  54. protected WeakReference<Activity> ownActivity;
  55. private static List<BaseDialog> runningDialogList;
  56. private WeakReference<View> dialogView;
  57. protected WeakReference<DialogFragmentImpl> ownDialogFragmentImpl;
  58. protected DialogX.IMPL_MODE dialogImplMode = DialogX.implIMPLMode;
  59. protected WeakReference<DialogXFloatingWindowActivity> floatingWindowActivity;
  60. public static void init(Context context) {
  61. if (context == null) context = ActivityLifecycleImpl.getTopActivity();
  62. if (context instanceof Activity) {
  63. initActivityContext((Activity) context);
  64. }
  65. ActivityLifecycleImpl.init(context, new ActivityLifecycleImpl.onActivityResumeCallBack() {
  66. @Override
  67. public void getActivity(Activity activity) {
  68. initActivityContext(activity);
  69. }
  70. });
  71. }
  72. private static void initActivityContext(Activity activity) {
  73. try {
  74. uiThread = new WeakReference<>(Thread.currentThread());
  75. contextWeakReference = new WeakReference<>(activity);
  76. rootFrameLayout = new WeakReference<>((FrameLayout) activity.getWindow().getDecorView());
  77. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  78. publicWindowInsets(rootFrameLayout.get().getRootWindowInsets());
  79. }
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. error("DialogX.init: 初始化异常,找不到Activity的根布局");
  83. }
  84. }
  85. protected static void log(Object o) {
  86. if (DEBUGMODE) Log.i(">>>", o.toString());
  87. }
  88. protected static void error(Object o) {
  89. if (DEBUGMODE) Log.e(">>>", o.toString());
  90. }
  91. public static void onActivityResume(Activity activity) {
  92. if (runningDialogList != null) {
  93. CopyOnWriteArrayList<BaseDialog> copyOnWriteList = new CopyOnWriteArrayList<>(runningDialogList);
  94. for (int i = copyOnWriteList.size() - 1; i >= 0; i--) {
  95. BaseDialog baseDialog = copyOnWriteList.get(i);
  96. if (baseDialog.getActivity() == activity && baseDialog.isShow && baseDialog.getDialogView() != null) {
  97. View boxRoot = baseDialog.getDialogView().findViewById(R.id.box_root);
  98. if (boxRoot instanceof DialogXBaseRelativeLayout) {
  99. if (((DialogXBaseRelativeLayout) boxRoot).isBaseFocusable()) {
  100. boxRoot.requestFocus();
  101. return;
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. private static void requestDialogFocus() {
  109. if (getContext() instanceof Activity) {
  110. onActivityResume((Activity) getContext());
  111. }
  112. }
  113. public abstract void restartDialog();
  114. protected static void show(final View view) {
  115. if (view == null) return;
  116. final BaseDialog baseDialog = (BaseDialog) view.getTag();
  117. if (baseDialog != null) {
  118. if (baseDialog.isShow) {
  119. if (baseDialog.getDialogView() != null) {
  120. baseDialog.getDialogView().setVisibility(View.VISIBLE);
  121. return;
  122. }
  123. error(((BaseDialog) view.getTag()).dialogKey() + "已处于显示状态,请勿重复执行 show() 指令。");
  124. return;
  125. }
  126. baseDialog.ownActivity = new WeakReference<>(contextWeakReference.get());
  127. baseDialog.dialogView = new WeakReference<>(view);
  128. log(baseDialog.dialogKey() + ".show");
  129. addDialogToRunningList(baseDialog);
  130. switch (baseDialog.dialogImplMode) {
  131. case WINDOW:
  132. runOnMain(new Runnable() {
  133. @Override
  134. public void run() {
  135. WindowUtil.show(contextWeakReference.get(), view, !(baseDialog instanceof PopTip));
  136. }
  137. });
  138. break;
  139. case DIALOG_FRAGMENT:
  140. DialogFragmentImpl dialogFragment = new DialogFragmentImpl(baseDialog, view);
  141. dialogFragment.show(getSupportFragmentManager(contextWeakReference.get()), "DialogX");
  142. baseDialog.ownDialogFragmentImpl = new WeakReference<>(dialogFragment);
  143. break;
  144. case FLOATING_ACTIVITY:
  145. if (waitRunDialogX == null) {
  146. waitRunDialogX = new HashMap<>();
  147. }
  148. waitRunDialogX.put(baseDialog.dialogKey(), new ActivityRunnable() {
  149. @Override
  150. public void run(Activity activity) {
  151. baseDialog.floatingWindowActivity = new WeakReference<>((DialogXFloatingWindowActivity) activity);
  152. final FrameLayout activityRootView = (FrameLayout) activity.getWindow().getDecorView();
  153. if (activityRootView == null) {
  154. return;
  155. }
  156. runOnMain(new Runnable() {
  157. @Override
  158. public void run() {
  159. if (view.getParent() == rootFrameLayout.get()) {
  160. error(((BaseDialog) view.getTag()).dialogKey() + "已处于显示状态,请勿重复执行 show() 指令。");
  161. return;
  162. }
  163. if (view.getParent() != null) {
  164. ((ViewGroup) view.getParent()).removeView(view);
  165. }
  166. activityRootView.addView(view);
  167. }
  168. });
  169. }
  170. });
  171. DialogXFloatingWindowActivity dialogXFloatingWindowActivity = DialogXFloatingWindowActivity.getDialogXFloatingWindowActivity();
  172. if (dialogXFloatingWindowActivity != null && dialogXFloatingWindowActivity.isSameFrom(contextWeakReference.get().hashCode())) {
  173. dialogXFloatingWindowActivity.showDialogX(baseDialog.dialogKey());
  174. return;
  175. }
  176. Intent intent = new Intent(contextWeakReference.get(), DialogXFloatingWindowActivity.class);
  177. intent.putExtra("dialogXKey", baseDialog.dialogKey());
  178. intent.putExtra("fromActivityUiStatus", contextWeakReference.get().getWindow().getDecorView().getSystemUiVisibility());
  179. intent.putExtra("from", contextWeakReference.get().hashCode());
  180. contextWeakReference.get().startActivity(intent);
  181. int version = Integer.valueOf(Build.VERSION.SDK_INT);
  182. if (version > 5) {
  183. contextWeakReference.get().overridePendingTransition(0, 0);
  184. }
  185. break;
  186. default:
  187. if (rootFrameLayout == null || rootFrameLayout.get() == null) return;
  188. runOnMain(new Runnable() {
  189. @Override
  190. public void run() {
  191. if (view.getParent() == rootFrameLayout.get()) {
  192. error(((BaseDialog) view.getTag()).dialogKey() + "已处于显示状态,请勿重复执行 show() 指令。");
  193. return;
  194. }
  195. if (view.getParent() != null) {
  196. ((ViewGroup) view.getParent()).removeView(view);
  197. }
  198. rootFrameLayout.get().addView(view);
  199. }
  200. });
  201. break;
  202. }
  203. }
  204. }
  205. private static FragmentManager getSupportFragmentManager(Activity activity) {
  206. return (activity instanceof AppCompatActivity) ? ((AppCompatActivity) activity).getSupportFragmentManager() : null;
  207. }
  208. private static Map<String, ActivityRunnable> waitRunDialogX;
  209. public static ActivityRunnable getActivityRunnable(String dialogXKey) {
  210. if (dialogXKey == null) return null;
  211. return waitRunDialogX.get(dialogXKey);
  212. }
  213. protected static void show(final Activity activity, final View view) {
  214. if (activity == null || view == null) return;
  215. if (contextWeakReference == null || contextWeakReference.get() == null) {
  216. initActivityContext(activity);
  217. }
  218. final BaseDialog baseDialog = (BaseDialog) view.getTag();
  219. if (baseDialog != null) {
  220. if (baseDialog.getDialogView() != null) {
  221. baseDialog.getDialogView().setVisibility(View.VISIBLE);
  222. }
  223. if (baseDialog.isShow) {
  224. error(((BaseDialog) view.getTag()).dialogKey() + "已处于显示状态,请勿重复执行 show() 指令。");
  225. return;
  226. }
  227. if (activity.isDestroyed()) {
  228. error(((BaseDialog) view.getTag()).dialogKey() + ".show ERROR: activity is Destroyed.");
  229. return;
  230. }
  231. baseDialog.ownActivity = new WeakReference<>(activity);
  232. baseDialog.dialogView = new WeakReference<>(view);
  233. log(baseDialog + ".show");
  234. addDialogToRunningList(baseDialog);
  235. switch (baseDialog.dialogImplMode) {
  236. case WINDOW:
  237. runOnMain(new Runnable() {
  238. @Override
  239. public void run() {
  240. WindowUtil.show(activity, view, !(baseDialog instanceof PopTip));
  241. }
  242. });
  243. break;
  244. case DIALOG_FRAGMENT:
  245. DialogFragmentImpl dialogFragment = new DialogFragmentImpl(baseDialog, view);
  246. dialogFragment.show(getSupportFragmentManager(activity), "DialogX");
  247. baseDialog.ownDialogFragmentImpl = new WeakReference<>(dialogFragment);
  248. break;
  249. case FLOATING_ACTIVITY:
  250. if (waitRunDialogX == null) {
  251. waitRunDialogX = new HashMap<>();
  252. }
  253. waitRunDialogX.put(baseDialog.dialogKey(), new ActivityRunnable() {
  254. @Override
  255. public void run(Activity activity) {
  256. baseDialog.floatingWindowActivity = new WeakReference<>((DialogXFloatingWindowActivity) activity);
  257. final FrameLayout activityRootView = (FrameLayout) activity.getWindow().getDecorView();
  258. if (activityRootView == null) {
  259. return;
  260. }
  261. runOnMain(new Runnable() {
  262. @Override
  263. public void run() {
  264. if (view.getParent() == rootFrameLayout.get()) {
  265. error(((BaseDialog) view.getTag()).dialogKey() + "已处于显示状态,请勿重复执行 show() 指令。");
  266. return;
  267. }
  268. if (view.getParent() != null) {
  269. ((ViewGroup) view.getParent()).removeView(view);
  270. }
  271. activityRootView.addView(view);
  272. }
  273. });
  274. }
  275. });
  276. DialogXFloatingWindowActivity dialogXFloatingWindowActivity = DialogXFloatingWindowActivity.getDialogXFloatingWindowActivity();
  277. if (dialogXFloatingWindowActivity != null && dialogXFloatingWindowActivity.isSameFrom(activity.hashCode())) {
  278. dialogXFloatingWindowActivity.showDialogX(baseDialog.dialogKey());
  279. return;
  280. }
  281. Intent intent = new Intent(activity, DialogXFloatingWindowActivity.class);
  282. intent.putExtra("dialogXKey", baseDialog.dialogKey());
  283. intent.putExtra("from", activity.hashCode());
  284. intent.putExtra("fromActivityUiStatus", activity.getWindow().getDecorView().getSystemUiVisibility());
  285. activity.startActivity(intent);
  286. int version = Integer.valueOf(Build.VERSION.SDK_INT);
  287. if (version > 5) {
  288. activity.overridePendingTransition(0, 0);
  289. }
  290. break;
  291. default:
  292. final FrameLayout activityRootView = (FrameLayout) activity.getWindow().getDecorView();
  293. if (activityRootView == null) {
  294. return;
  295. }
  296. runOnMain(new Runnable() {
  297. @Override
  298. public void run() {
  299. if (view.getParent() == rootFrameLayout.get()) {
  300. error(((BaseDialog) view.getTag()).dialogKey() + "已处于显示状态,请勿重复执行 show() 指令。");
  301. return;
  302. }
  303. if (view.getParent() != null) {
  304. ((ViewGroup) view.getParent()).removeView(view);
  305. }
  306. activityRootView.addView(view);
  307. }
  308. });
  309. break;
  310. }
  311. }
  312. }
  313. protected static void dismiss(final View dialogView) {
  314. if (dialogView == null) return;
  315. final BaseDialog baseDialog = (BaseDialog) dialogView.getTag();
  316. log(baseDialog.dialogKey() + ".dismiss");
  317. removeDialogToRunningList(baseDialog);
  318. if (baseDialog.dialogView != null) baseDialog.dialogView.clear();
  319. switch (baseDialog.dialogImplMode) {
  320. case WINDOW:
  321. runOnMain(new Runnable() {
  322. @Override
  323. public void run() {
  324. WindowUtil.dismiss(dialogView);
  325. }
  326. });
  327. break;
  328. case DIALOG_FRAGMENT:
  329. if (baseDialog.ownDialogFragmentImpl != null && baseDialog.ownDialogFragmentImpl.get() != null) {
  330. baseDialog.ownDialogFragmentImpl.get().dismiss();
  331. }
  332. break;
  333. case FLOATING_ACTIVITY:
  334. if (baseDialog.floatingWindowActivity != null && baseDialog.floatingWindowActivity.get() != null) {
  335. FrameLayout rootView = ((FrameLayout) baseDialog.floatingWindowActivity.get().getWindow().getDecorView());
  336. if (rootView != null) rootView.removeView(dialogView);
  337. baseDialog.floatingWindowActivity.get().finish(baseDialog.dialogKey());
  338. requestDialogFocus();
  339. }
  340. break;
  341. default:
  342. runOnMain(new Runnable() {
  343. @Override
  344. public void run() {
  345. if (dialogView.getParent() == null || !(dialogView.getParent() instanceof ViewGroup)) {
  346. if (rootFrameLayout == null) return;
  347. rootFrameLayout.get().removeView(dialogView);
  348. } else {
  349. ((ViewGroup) dialogView.getParent()).removeView(dialogView);
  350. }
  351. requestDialogFocus();
  352. }
  353. });
  354. break;
  355. }
  356. }
  357. private static void addDialogToRunningList(BaseDialog baseDialog) {
  358. if (runningDialogList == null) runningDialogList = new CopyOnWriteArrayList<>();
  359. runningDialogList.add(baseDialog);
  360. }
  361. private static void removeDialogToRunningList(BaseDialog baseDialog) {
  362. if (runningDialogList != null) runningDialogList.remove(baseDialog);
  363. }
  364. public static Context getContext() {
  365. if (contextWeakReference == null) {
  366. init(null);
  367. if (contextWeakReference == null) {
  368. return ActivityLifecycleImpl.getTopActivity();
  369. }
  370. return contextWeakReference.get();
  371. }
  372. return contextWeakReference.get();
  373. }
  374. /**
  375. * 自动执行,不建议自行调用此方法
  376. *
  377. * @hide
  378. */
  379. public static void cleanContext() {
  380. if (contextWeakReference != null) contextWeakReference.clear();
  381. contextWeakReference = null;
  382. System.gc();
  383. }
  384. protected abstract void shutdown();
  385. protected boolean cancelable = true;
  386. protected OnBackPressedListener onBackPressedListener;
  387. protected boolean isShow;
  388. protected DialogXStyle style;
  389. protected DialogX.THEME theme;
  390. protected boolean autoShowInputKeyboard;
  391. protected int backgroundColor = -1;
  392. protected long enterAnimDuration = -1;
  393. protected long exitAnimDuration = -1;
  394. protected int maxWidth;
  395. public BaseDialog() {
  396. cancelable = DialogX.cancelable;
  397. style = DialogX.globalStyle;
  398. theme = DialogX.globalTheme;
  399. enterAnimDuration = DialogX.enterAnimDuration;
  400. exitAnimDuration = DialogX.exitAnimDuration;
  401. autoShowInputKeyboard = DialogX.autoShowInputKeyboard;
  402. }
  403. public abstract boolean isCancelable();
  404. public View createView(int layoutId) {
  405. if (getContext() == null) {
  406. error("DialogX 未初始化。\n请检查是否在启动对话框前进行初始化操作,使用以下代码进行初始化:\nDialogX.init(context);\n\n另外建议您前往查看 DialogX 的文档进行使用:https://github.com/kongzue/DialogX");
  407. return null;
  408. }
  409. return LayoutInflater.from(getContext()).inflate(layoutId, null);
  410. }
  411. public boolean isShow() {
  412. return isShow;
  413. }
  414. public DialogXStyle getStyle() {
  415. return style;
  416. }
  417. public DialogX.THEME getTheme() {
  418. return theme;
  419. }
  420. public static void useTextInfo(TextView textView, TextInfo textInfo) {
  421. if (textInfo == null) return;
  422. if (textView == null) return;
  423. if (textInfo.getFontSize() > 0) {
  424. textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textInfo.getFontSize());
  425. }
  426. if (textInfo.getFontColor() != 1) {
  427. textView.setTextColor(textInfo.getFontColor());
  428. }
  429. if (textInfo.getGravity() != -1) {
  430. textView.setGravity(textInfo.getGravity());
  431. }
  432. if (textInfo.isShowEllipsis()) {
  433. textView.setEllipsize(TextUtils.TruncateAt.END);
  434. } else {
  435. textView.setEllipsize(null);
  436. }
  437. if (textInfo.getMaxLines() != -1) {
  438. textView.setMaxLines(textInfo.getMaxLines());
  439. } else {
  440. textView.setMaxLines(Integer.MAX_VALUE);
  441. }
  442. textView.getPaint().setFakeBoldText(textInfo.isBold());
  443. }
  444. protected void showText(TextView textView, CharSequence text) {
  445. if (textView == null) return;
  446. if (isNull(text)) {
  447. textView.setVisibility(View.GONE);
  448. textView.setText("");
  449. } else {
  450. textView.setVisibility(View.VISIBLE);
  451. textView.setText(text);
  452. }
  453. }
  454. protected View createHorizontalSplitView(int color) {
  455. View splitView = new View(getContext());
  456. splitView.setBackgroundColor(color);
  457. LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
  458. splitView.setLayoutParams(lp);
  459. return splitView;
  460. }
  461. protected View createVerticalSplitView(int color, int height) {
  462. View splitView = new View(getContext());
  463. splitView.setBackgroundColor(color);
  464. LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(1, dip2px(height));
  465. splitView.setLayoutParams(lp);
  466. return splitView;
  467. }
  468. public static boolean isNull(String s) {
  469. if (s == null || s.trim().isEmpty() || "null".equals(s) || "(null)".equals(s)) {
  470. return true;
  471. }
  472. return false;
  473. }
  474. public static boolean isNull(CharSequence c) {
  475. String s = String.valueOf(c);
  476. if (c == null || s.trim().isEmpty() || "null".equals(s) || "(null)".equals(s)) {
  477. return true;
  478. }
  479. return false;
  480. }
  481. public Resources getResources() {
  482. if (getContext() == null) return Resources.getSystem();
  483. return getContext().getResources();
  484. }
  485. public int dip2px(float dpValue) {
  486. final float scale = getContext().getResources().getDisplayMetrics().density;
  487. return (int) (dpValue * scale + 0.5f);
  488. }
  489. public boolean isLightTheme() {
  490. if (theme == DialogX.THEME.AUTO) {
  491. if (getContext() == null) return theme == DialogX.THEME.LIGHT;
  492. return (getContext().getApplicationContext().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_NO;
  493. }
  494. return theme == DialogX.THEME.LIGHT;
  495. }
  496. public static FrameLayout getRootFrameLayout() {
  497. if (rootFrameLayout == null) {
  498. error("DialogX 未初始化。\n请检查是否在启动对话框前进行初始化操作,使用以下代码进行初始化:\nDialogX.init(context);\n\n另外建议您前往查看 DialogX 的文档进行使用:https://github.com/kongzue/DialogX");
  499. return null;
  500. }
  501. return rootFrameLayout.get();
  502. }
  503. public void tintColor(View view, int color) {
  504. if (view == null) {
  505. return;
  506. }
  507. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  508. view.setBackgroundTintList(ColorStateList.valueOf(color));
  509. }
  510. }
  511. /**
  512. * 此标记用于拦截重复 dismiss 指令导致的关闭动画抖动异常
  513. */
  514. protected boolean dismissAnimFlag;
  515. protected void beforeShow() {
  516. dismissAnimFlag = false;
  517. if (getContext() == null) {
  518. init(null);
  519. if (getContext() == null) {
  520. error("DialogX 未初始化。\n请检查是否在启动对话框前进行初始化操作,使用以下代码进行初始化:\nDialogX.init(context);\n\n另外建议您前往查看 DialogX 的文档进行使用:https://github.com/kongzue/DialogX");
  521. return;
  522. }
  523. }
  524. if (style.styleVer != DialogXStyle.styleVer) {
  525. error("DialogX 所引用的 Style 不符合当前适用版本:" + DialogXStyle.styleVer + " 引入的 Style(" + style.getClass().getSimpleName() + ") 版本" + style.styleVer);
  526. }
  527. //Hide IME
  528. View view = ((Activity) BaseDialog.getContext()).getCurrentFocus();
  529. if (view != null) {
  530. InputMethodManager imm = (InputMethodManager) BaseDialog.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  531. imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
  532. }
  533. }
  534. protected String getString(int titleResId) {
  535. if (getContext() == null) {
  536. error("DialogX 未初始化。\n请检查是否在启动对话框前进行初始化操作,使用以下代码进行初始化:\nDialogX.init(context);\n\n另外建议您前往查看 DialogX 的文档进行使用:https://github.com/kongzue/DialogX");
  537. return null;
  538. }
  539. return getContext().getString(titleResId);
  540. }
  541. protected int getColor(int backgroundRes) {
  542. if (getContext() == null) {
  543. error("DialogX 未初始化。\n请检查是否在启动对话框前进行初始化操作,使用以下代码进行初始化:\nDialogX.init(context);\n\n另外建议您前往查看 DialogX 的文档进行使用:https://github.com/kongzue/DialogX");
  544. return Color.BLACK;
  545. }
  546. return getResources().getColor(backgroundRes);
  547. }
  548. public enum BOOLEAN {
  549. TRUE, FALSE
  550. }
  551. public abstract String dialogKey();
  552. protected static void runOnMain(Runnable runnable) {
  553. if (!DialogX.autoRunOnUIThread || (uiThread != null && Thread.currentThread() == uiThread.get())) {
  554. runnable.run();
  555. return;
  556. }
  557. new Handler(Looper.getMainLooper()).post(runnable);
  558. }
  559. protected static void runOnMainDelay(Runnable runnable, long delay) {
  560. if (!DialogX.autoRunOnUIThread) runnable.run();
  561. new Handler(Looper.getMainLooper()).postDelayed(runnable, delay);
  562. }
  563. public View getDialogView() {
  564. if (dialogView == null) return null;
  565. return dialogView.get();
  566. }
  567. public Activity getActivity() {
  568. return ownActivity == null ? null : ownActivity.get();
  569. }
  570. protected void cleanActivityContext() {
  571. if (ownActivity != null) ownActivity.clear();
  572. ownActivity = null;
  573. }
  574. public static void cleanAll() {
  575. if (runningDialogList != null) {
  576. CopyOnWriteArrayList<BaseDialog> copyOnWriteList = new CopyOnWriteArrayList<>(runningDialogList);
  577. for (BaseDialog baseDialog : copyOnWriteList) {
  578. if (baseDialog.isShow()) baseDialog.shutdown();
  579. baseDialog.cleanActivityContext();
  580. runningDialogList.remove(baseDialog);
  581. }
  582. }
  583. }
  584. public static void recycleDialog(Activity activity) {
  585. switch (DialogX.implIMPLMode) {
  586. case WINDOW:
  587. if (runningDialogList != null) {
  588. CopyOnWriteArrayList<BaseDialog> copyOnWriteList = new CopyOnWriteArrayList<>(runningDialogList);
  589. for (BaseDialog baseDialog : copyOnWriteList) {
  590. if (baseDialog.getActivity() == activity && baseDialog.dialogView != null) {
  591. WindowUtil.dismiss(baseDialog.dialogView.get());
  592. }
  593. }
  594. }
  595. break;
  596. case DIALOG_FRAGMENT:
  597. if (runningDialogList != null) {
  598. CopyOnWriteArrayList<BaseDialog> copyOnWriteList = new CopyOnWriteArrayList<>(runningDialogList);
  599. for (BaseDialog baseDialog : copyOnWriteList) {
  600. if (baseDialog.getActivity() == activity && baseDialog.ownDialogFragmentImpl != null && baseDialog.ownDialogFragmentImpl.get() != null) {
  601. baseDialog.ownDialogFragmentImpl.get().dismiss();
  602. }
  603. }
  604. }
  605. break;
  606. case FLOATING_ACTIVITY:
  607. break;
  608. default:
  609. if (runningDialogList != null) {
  610. CopyOnWriteArrayList<BaseDialog> copyOnWriteList = new CopyOnWriteArrayList<>(runningDialogList);
  611. for (BaseDialog baseDialog : copyOnWriteList) {
  612. if (baseDialog.getActivity() == activity) {
  613. baseDialog.cleanActivityContext();
  614. runningDialogList.remove(baseDialog);
  615. }
  616. }
  617. }
  618. break;
  619. }
  620. if (activity == getContext()) {
  621. cleanContext();
  622. }
  623. }
  624. public static List<BaseDialog> getRunningDialogList() {
  625. if (runningDialogList == null) return new ArrayList<>();
  626. return new CopyOnWriteArrayList<>(runningDialogList);
  627. }
  628. protected void imeShow(EditText editText, boolean show) {
  629. if (getContext() == null) return;
  630. InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  631. if (show) {
  632. imm.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN);
  633. } else {
  634. imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
  635. }
  636. }
  637. public int getMaxWidth() {
  638. if (maxWidth == 0) return DialogX.dialogMaxWidth;
  639. return maxWidth;
  640. }
  641. public DialogX.IMPL_MODE getDialogImplMode() {
  642. return dialogImplMode;
  643. }
  644. protected static WindowInsets windowInsets;
  645. public static WindowInsets publicWindowInsets() {
  646. return windowInsets;
  647. }
  648. public static void publicWindowInsets(WindowInsets windowInsets) {
  649. if (windowInsets != null) BaseDialog.windowInsets = windowInsets;
  650. if (runningDialogList != null) {
  651. CopyOnWriteArrayList<BaseDialog> copyOnWriteList = new CopyOnWriteArrayList<>(runningDialogList);
  652. for (int i = copyOnWriteList.size() - 1; i >= 0; i--) {
  653. BaseDialog baseDialog = copyOnWriteList.get(i);
  654. if (baseDialog.isShow && baseDialog.getDialogView() != null) {
  655. View boxRoot = baseDialog.getDialogView().findViewById(R.id.box_root);
  656. if (boxRoot instanceof DialogXBaseRelativeLayout) {
  657. ((DialogXBaseRelativeLayout) boxRoot).paddingView(windowInsets);
  658. }
  659. }
  660. }
  661. }
  662. }
  663. protected void bindFloatingActivity(DialogXFloatingWindowActivity activity) {
  664. floatingWindowActivity = new WeakReference<>(activity);
  665. }
  666. }