TimeOutUtils.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.emato.ich.utils;
  2. import android.os.CountDownTimer;
  3. import android.widget.TextView;
  4. import androidx.fragment.app.Fragment;
  5. import androidx.navigation.fragment.NavHostFragment;
  6. import com.emato.ich.MainActivity;
  7. import com.emato.ich.R;
  8. import com.emato.ich.fragment.ChooseCabinetFragment;
  9. import com.emato.ich.fragment.ExceptionFragment;
  10. import com.emato.ich.fragment.InputInfoFragment;
  11. import com.emato.ich.fragment.InputInfoKeyBoardFragment;
  12. import com.emato.ich.fragment.SendFragment;
  13. import com.emato.ich.fragment.SendInfoConfirmFragment;
  14. import com.emato.ich.fragment.SendKeyBoardFragment;
  15. import com.emato.ich.fragment.SendMainFragment;
  16. import com.emato.ich.fragment.SendSuccessFragment;
  17. import com.emato.ich.fragment.TakeCodeFragment;
  18. import com.emato.ich.fragment.TakeFragment;
  19. import com.emato.ich.fragment.TakeSuccessFragment;
  20. import com.emato.ich.local.LocalStorage;
  21. /**
  22. * 页面倒计时工具类
  23. */
  24. public class TimeOutUtils {
  25. /**
  26. * 倒计时
  27. * @param mainActivity activity
  28. * @param fragment fragment
  29. * @param textView 倒计时显示的控件
  30. * @param time 时间
  31. * @return 计时器
  32. */
  33. public static CountDownTimer timeout(MainActivity mainActivity, Fragment fragment, TextView textView, int time){
  34. // TODO 需要action 每一个Fragment都需要一个跳转Main的action 需要判断是哪个Fragment
  35. return new CountDownTimer(time * 1000, 1000) {
  36. @Override
  37. public void onTick(long millisUntilFinished) {
  38. textView.setText(String.valueOf((millisUntilFinished / 1000)));
  39. if (millisUntilFinished <= 1999) {
  40. int fragmentAction = getFragmentAction(fragment);
  41. if (fragmentAction != 0) {
  42. NavUtils.navigate(fragment, fragmentAction);
  43. fragment.onDestroy();
  44. }
  45. }
  46. }
  47. @Override
  48. public void onFinish() {
  49. int fragmentAction = getFragmentAction(fragment);
  50. if (fragmentAction != 0) {
  51. // 清空session和页面传值, 异常处理有不同处理方式
  52. boolean isException = false;
  53. if ( LocalStorage.getInstance().getSession().getException() &&
  54. (fragment instanceof SendInfoConfirmFragment || fragment instanceof SendSuccessFragment)) {
  55. isException = true;
  56. }
  57. LocalStorage.getInstance().cleanSession(mainActivity, isException);
  58. fragment.onDestroy();
  59. }
  60. }
  61. };
  62. }
  63. /**
  64. * 根据Fragment来跳转
  65. * @param fragment fragment
  66. * @return 跳转所对应的action id
  67. */
  68. private static int getFragmentAction(Fragment fragment) {
  69. if (fragment instanceof ChooseCabinetFragment) {
  70. return R.id.action_chooseCabinetFragment_to_mainFragment;
  71. } else if (fragment instanceof ExceptionFragment) {
  72. return R.id.actionExceptionFragment_to_mainFragment;
  73. } else if (fragment instanceof InputInfoFragment) {
  74. return R.id.action_InputInfoKeyBoardFragment_to_mainFragment;
  75. } else if (fragment instanceof InputInfoKeyBoardFragment) {
  76. return R.id.action_InputInfoKeyBoardFragment_to_mainFragment;
  77. }else if (fragment instanceof SendFragment) {
  78. return R.id.action_sendFragment_to_mainFragment;
  79. } else if (fragment instanceof SendKeyBoardFragment) {
  80. return R.id.action_sendFragment_to_mainFragment;
  81. }else if (fragment instanceof SendInfoConfirmFragment) {
  82. return R.id.action_sendInfoConfirmFragment_to_mainFragment;
  83. } else if (fragment instanceof SendMainFragment) {
  84. return R.id.action_sendMainFragment_to_mainFragment;
  85. } else if (fragment instanceof SendSuccessFragment) {
  86. return R.id.action_sendSuccessFragment_to_mainFragment;
  87. } else if (fragment instanceof TakeCodeFragment) {
  88. return R.id.action_takeCodeFragment_to_mainFragment;
  89. } else if (fragment instanceof TakeFragment) {
  90. return R.id.action_takeFragment_to_mainFragment;
  91. } else if (fragment instanceof TakeSuccessFragment) {
  92. return R.id.action_takeSuccessFragment_to_mainFragment;
  93. }
  94. return 0;
  95. }
  96. }