TimeOutUtils.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.SendFragment;
  12. import com.emato.ich.fragment.SendInfoConfirmFragment;
  13. import com.emato.ich.fragment.SendMainFragment;
  14. import com.emato.ich.fragment.SendSuccessFragment;
  15. import com.emato.ich.fragment.TakeCodeFragment;
  16. import com.emato.ich.fragment.TakeFragment;
  17. import com.emato.ich.fragment.TakeSuccessFragment;
  18. import com.emato.ich.local.LocalStorage;
  19. public class TimeOutUtils {
  20. public static CountDownTimer timeout(MainActivity mainActivity, Fragment fragment, TextView textView, int time){
  21. // TODO 需要action 每一个Fragment都需要一个跳转Main的action 需要判断是哪个Fragment
  22. return new CountDownTimer(time * 1000, 1000) {
  23. @Override
  24. public void onTick(long millisUntilFinished) {
  25. textView.setText(String.valueOf((millisUntilFinished / 1000)));
  26. }
  27. @Override
  28. public void onFinish() {
  29. int fragmentAction = getFragmentAction(fragment);
  30. if (fragmentAction != 0) {
  31. // 清空session和页面传值
  32. LocalStorage.getInstance().cleanSession(mainActivity);
  33. NavHostFragment.findNavController(fragment).navigate(fragmentAction);
  34. }
  35. }
  36. };
  37. }
  38. private static int getFragmentAction(Fragment fragment) {
  39. if (fragment instanceof ChooseCabinetFragment) {
  40. return R.id.action_chooseCabinetFragment_to_mainFragment;
  41. } else if (fragment instanceof ExceptionFragment) {
  42. return R.id.actionExceptionFragment_to_mainFragment;
  43. } else if (fragment instanceof InputInfoFragment) {
  44. return R.id.action_inputInfoFragment_to_mainFragment;
  45. } else if (fragment instanceof SendFragment) {
  46. return R.id.action_sendFragment_to_mainFragment;
  47. } else if (fragment instanceof SendInfoConfirmFragment) {
  48. return R.id.action_sendInfoConfirmFragment_to_mainFragment;
  49. } else if (fragment instanceof SendMainFragment) {
  50. return R.id.action_sendMainFragment_to_mainFragment;
  51. } else if (fragment instanceof SendSuccessFragment) {
  52. return R.id.action_sendSuccessFragment_to_mainFragment;
  53. } else if (fragment instanceof TakeCodeFragment) {
  54. return R.id.action_takeCodeFragment_to_mainFragment;
  55. } else if (fragment instanceof TakeFragment) {
  56. return R.id.action_takeFragment_to_mainFragment;
  57. } else if (fragment instanceof TakeSuccessFragment) {
  58. return R.id.action_takeSuccessFragment_to_mainFragment;
  59. }
  60. return 0;
  61. }
  62. }