1
0

LocalStorage.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.emato.ich.local;
  2. import android.os.Bundle;
  3. import com.emato.ich.MainActivity;
  4. import com.emato.ich.utils.StringUtils;
  5. import java.util.Map;
  6. import java.util.concurrent.TimeUnit;
  7. public class LocalStorage {
  8. private final Session SESSION = new Session();
  9. private LocalStorage(){}
  10. public static LocalStorage getInstance(){
  11. return LocalStorageInnerClass.INSTANCE;
  12. }
  13. public Session getSession(){
  14. return SESSION;
  15. }
  16. public Session setSession(Session session) {
  17. if (StringUtils.isNullOrEmpty(SESSION.phone) || StringUtils.isNullOrEmpty(SESSION.token)) {
  18. SESSION.token = session.getToken();
  19. SESSION.phone = session.getPhone();
  20. if (session.getExpiryTime() > 0) {
  21. SESSION.expiryTime = session.getExpiryTime();
  22. }
  23. if (session.getTime() > 0) {
  24. SESSION.time = session.getTime();
  25. }
  26. }
  27. return SESSION;
  28. }
  29. public void cleanSession(MainActivity activity){
  30. SESSION.phone = "";
  31. SESSION.token = "";
  32. SESSION.exception = true;
  33. Map<String, Bundle> bundleMap = activity.getBundleMap();
  34. if (activity != null && bundleMap != null && bundleMap.size() > 0) {
  35. bundleMap.clear();
  36. }
  37. }
  38. /**
  39. * 过期返回true, 否则返回false
  40. * @param session session
  41. * @return 是否
  42. */
  43. public boolean isExpired(Session session) {
  44. long sessionTime = session.getTime();
  45. long currentTime = System.currentTimeMillis();
  46. long diff = currentTime - sessionTime;
  47. return diff > session.getExpiryTime() * 60 * 1000;
  48. }
  49. public static final class LocalStorageInnerClass {
  50. private final static LocalStorage INSTANCE = new LocalStorage();
  51. }
  52. public static class Session {
  53. private String phone;
  54. private String token;
  55. private long time;
  56. private int expiryTime = 15;
  57. /**
  58. * 新增字段 只能做一次异常处理, 点击异常处理的 异常事件按钮 后该值为false
  59. */
  60. private boolean exception = true;
  61. private TimeUnit timeUnit = TimeUnit.MINUTES;
  62. public String getPhone() {
  63. return phone;
  64. }
  65. public void setPhone(String phone) {
  66. this.phone = phone;
  67. }
  68. public String getToken() {
  69. return token;
  70. }
  71. public void setToken(String token) {
  72. this.token = token.replace("\n", "").trim();
  73. }
  74. public long getTime() {
  75. return time;
  76. }
  77. public void setTime(long time) {
  78. this.time = time;
  79. }
  80. public int getExpiryTime() {
  81. return expiryTime;
  82. }
  83. public void setExpiryTime(int expiryTime) {
  84. this.expiryTime = expiryTime;
  85. }
  86. public TimeUnit getTimeUnit() {
  87. return timeUnit;
  88. }
  89. public void setTimeUnit(TimeUnit timeUnit) {
  90. this.timeUnit = timeUnit;
  91. }
  92. public boolean getException() {
  93. return exception;
  94. }
  95. public void setException(boolean exception) {
  96. this.exception = exception;
  97. }
  98. }
  99. }