f48670c6d84dc541e22223cdae90c2ab59f6643e.svn-base 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package eVVM.apk.other;
  2. import android.content.Context;
  3. import android.graphics.drawable.Drawable;
  4. import android.net.ConnectivityManager;
  5. import android.net.NetworkInfo;
  6. import android.support.annotation.DrawableRes;
  7. import android.support.annotation.RequiresPermission;
  8. import android.support.annotation.StringRes;
  9. import android.support.v4.app.FragmentActivity;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import com.hjq.base.BaseDialog;
  13. import com.hjq.dialog.WaitDialog;
  14. import com.hjq.widget.HintLayout;
  15. import eVVM.apk.R;
  16. import static android.Manifest.permission.ACCESS_NETWORK_STATE;
  17. /**
  18. * desc : 界面状态管理类
  19. */
  20. public final class StatusManager {
  21. // 加载对话框
  22. private BaseDialog mDialog;
  23. // 提示布局
  24. private HintLayout mHintLayout;
  25. /**
  26. * 显示加载中
  27. */
  28. public void showLoading(FragmentActivity activity) {
  29. if (mDialog == null) {
  30. mDialog = new WaitDialog.Builder(activity)
  31. .setMessage("加载中...") // 消息文本可以不用填写
  32. .create();
  33. }
  34. if (!mDialog.isShowing()) {
  35. mDialog.show();
  36. }
  37. }
  38. /**
  39. * 显示加载完成
  40. */
  41. public void showComplete() {
  42. if (mDialog != null && mDialog.isShowing()) {
  43. mDialog.dismiss();
  44. }
  45. if (mHintLayout != null && mHintLayout.isShow()) {
  46. mHintLayout.hide();
  47. }
  48. }
  49. /**
  50. * 显示空提示
  51. */
  52. public void showEmpty(View view) {
  53. showLayout(view, R.mipmap.icon_hint_empty, R.string.hint_layout_no_data);
  54. }
  55. /**
  56. * 显示错误提示
  57. */
  58. public void showError(View view) {
  59. // 判断当前网络是否可用
  60. if (isNetworkAvailable(view.getContext())) {
  61. showLayout(view, R.mipmap.icon_hint_request, R.string.hint_layout_error_request);
  62. } else {
  63. showLayout(view, R.mipmap.icon_hint_nerwork, R.string.hint_layout_error_network);
  64. }
  65. }
  66. /**
  67. * 显示自定义提示
  68. */
  69. public void showLayout(View view, @DrawableRes int iconId, @StringRes int textId) {
  70. showLayout(view, view.getResources().getDrawable(iconId), view.getResources().getString(textId));
  71. }
  72. public void showLayout(View view, Drawable drawable, CharSequence hint) {
  73. if (mDialog != null && mDialog.isShowing()) {
  74. mDialog.dismiss();
  75. }
  76. if (mHintLayout == null) {
  77. if (view instanceof HintLayout) {
  78. mHintLayout = (HintLayout) view;
  79. }else if (view instanceof ViewGroup) {
  80. mHintLayout = findHintLayout((ViewGroup) view);
  81. }
  82. if (mHintLayout == null) {
  83. throw new IllegalStateException("You didn't add this HintLayout to your Activity layout");
  84. }
  85. }
  86. mHintLayout.show();
  87. mHintLayout.setIcon(drawable);
  88. mHintLayout.setHint(hint);
  89. }
  90. /**
  91. * 判断网络功能是否可用
  92. */
  93. @RequiresPermission(ACCESS_NETWORK_STATE)
  94. private static boolean isNetworkAvailable(Context context){
  95. NetworkInfo info = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
  96. return (info != null && info.isConnected());
  97. }
  98. /**
  99. * 智能获取布局中的 HintLayout 对象
  100. */
  101. private static HintLayout findHintLayout(ViewGroup group) {
  102. for (int i = 0; i < group.getChildCount(); i++) {
  103. View view = group.getChildAt(i);
  104. if ((view instanceof HintLayout)) {
  105. return (HintLayout) view;
  106. } else if (view instanceof ViewGroup) {
  107. HintLayout layout = findHintLayout((ViewGroup) view);
  108. if (layout != null) return layout;
  109. }
  110. }
  111. return null;
  112. }
  113. }