UserPreferences.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package com.sheishuo.app.config.preference;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.sheishuo.app.SheishuoCache;
  7. import com.netease.nimlib.sdk.StatusBarNotificationConfig;
  8. /**
  9. * Created by hzxuwen on 2015/4/13.
  10. */
  11. public class UserPreferences {
  12. private final static String KEY_DOWNTIME_TOGGLE = "down_time_toggle";
  13. private final static String KEY_SB_NOTIFY_TOGGLE = "sb_notify_toggle";
  14. private final static String KEY_TEAM_ANNOUNCE_CLOSED = "team_announce_closed";
  15. private final static String KEY_STATUS_BAR_NOTIFICATION_CONFIG = "KEY_STATUS_BAR_NOTIFICATION_CONFIG";
  16. // 测试过滤通知
  17. private final static String KEY_MSG_IGNORE = "KEY_MSG_IGNORE";
  18. // 响铃配置
  19. private final static String KEY_RING_TOGGLE = "KEY_RING_TOGGLE";
  20. // 呼吸灯配置
  21. private final static String KEY_LED_TOGGLE = "KEY_LED_TOGGLE";
  22. // 通知栏标题配置
  23. private final static String KEY_NOTICE_CONTENT_TOGGLE = "KEY_NOTICE_CONTENT_TOGGLE";
  24. // 通知栏样式(展开、折叠)配置
  25. private final static String KEY_NOTIFICATION_FOLDED_TOGGLE = "KEY_NOTIFICATION_FOLDED";
  26. // 保存在线状态订阅时间
  27. private final static String KEY_SUBSCRIBE_TIME = "KEY_SUBSCRIBE_TIME";
  28. public static void setMsgIgnore(boolean enable) {
  29. saveBoolean(KEY_MSG_IGNORE, enable);
  30. }
  31. public static boolean getMsgIgnore() {
  32. return getBoolean(KEY_MSG_IGNORE, false);
  33. }
  34. public static void setNotificationToggle(boolean on) {
  35. saveBoolean(KEY_SB_NOTIFY_TOGGLE, on);
  36. }
  37. public static boolean getNotificationToggle() {
  38. return getBoolean(KEY_SB_NOTIFY_TOGGLE, true);
  39. }
  40. public static void setRingToggle(boolean on) {
  41. saveBoolean(KEY_RING_TOGGLE, on);
  42. }
  43. public static boolean getRingToggle() {
  44. return getBoolean(KEY_RING_TOGGLE, true);
  45. }
  46. public static void setLedToggle(boolean on) {
  47. saveBoolean(KEY_LED_TOGGLE, on);
  48. }
  49. public static boolean getLedToggle() {
  50. return getBoolean(KEY_LED_TOGGLE, true);
  51. }
  52. public static boolean getNoticeContentToggle() {
  53. return getBoolean(KEY_NOTICE_CONTENT_TOGGLE, false);
  54. }
  55. public static void setNoticeContentToggle(boolean on) {
  56. saveBoolean(KEY_NOTICE_CONTENT_TOGGLE, on);
  57. }
  58. public static void setDownTimeToggle(boolean on) {
  59. saveBoolean(KEY_DOWNTIME_TOGGLE, on);
  60. }
  61. public static boolean getDownTimeToggle() {
  62. return getBoolean(KEY_DOWNTIME_TOGGLE, false);
  63. }
  64. public static void setNotificationFoldedToggle(boolean folded) {
  65. saveBoolean(KEY_NOTIFICATION_FOLDED_TOGGLE, folded);
  66. }
  67. public static boolean getNotificationFoldedToggle() {
  68. return getBoolean(KEY_NOTIFICATION_FOLDED_TOGGLE, true);
  69. }
  70. public static void setStatusConfig(StatusBarNotificationConfig config) {
  71. saveStatusBarNotificationConfig(KEY_STATUS_BAR_NOTIFICATION_CONFIG, config);
  72. }
  73. public static StatusBarNotificationConfig getStatusConfig() {
  74. return getConfig(KEY_STATUS_BAR_NOTIFICATION_CONFIG);
  75. }
  76. public static void setTeamAnnounceClosed(String teamId, boolean closed) {
  77. saveBoolean(KEY_TEAM_ANNOUNCE_CLOSED + teamId, closed);
  78. }
  79. public static boolean getTeamAnnounceClosed(String teamId) {
  80. return getBoolean(KEY_TEAM_ANNOUNCE_CLOSED + teamId, false);
  81. }
  82. public static void setOnlineStateSubsTime(long time) {
  83. saveLong(KEY_SUBSCRIBE_TIME, time);
  84. }
  85. public static long getOnlineStateSubsTime() {
  86. return getLong(KEY_SUBSCRIBE_TIME, 0);
  87. }
  88. private static StatusBarNotificationConfig getConfig(String key) {
  89. StatusBarNotificationConfig config = new StatusBarNotificationConfig();
  90. String jsonString = getSharedPreferences().getString(key, "");
  91. try {
  92. JSONObject jsonObject = JSONObject.parseObject(jsonString);
  93. if (jsonObject == null) {
  94. return null;
  95. }
  96. config.downTimeBegin = jsonObject.getString("downTimeBegin");
  97. config.downTimeEnd = jsonObject.getString("downTimeEnd");
  98. config.downTimeToggle = jsonObject.getBoolean("downTimeToggle");
  99. config.ring = jsonObject.getBoolean("ring");
  100. config.vibrate = jsonObject.getBoolean("vibrate");
  101. config.notificationSmallIconId = jsonObject.getIntValue("notificationSmallIconId");
  102. config.notificationSound = jsonObject.getString("notificationSound");
  103. config.hideContent = jsonObject.getBoolean("hideContent");
  104. config.ledARGB = jsonObject.getIntValue("ledargb");
  105. config.ledOnMs = jsonObject.getIntValue("ledonms");
  106. config.ledOffMs = jsonObject.getIntValue("ledoffms");
  107. config.titleOnlyShowAppName = jsonObject.getBoolean("titleOnlyShowAppName");
  108. config.notificationFolded = jsonObject.getBoolean("notificationFolded");
  109. config.notificationEntrance = (Class<? extends Activity>) Class.forName(jsonObject.getString("notificationEntrance"));
  110. config.notificationColor = jsonObject.getInteger("notificationColor");
  111. } catch (Exception e) {
  112. e.printStackTrace();
  113. }
  114. return config;
  115. }
  116. private static void saveStatusBarNotificationConfig(String key, StatusBarNotificationConfig config) {
  117. SharedPreferences.Editor editor = getSharedPreferences().edit();
  118. JSONObject jsonObject = new JSONObject();
  119. try {
  120. jsonObject.put("downTimeBegin", config.downTimeBegin);
  121. jsonObject.put("downTimeEnd", config.downTimeEnd);
  122. jsonObject.put("downTimeToggle", config.downTimeToggle);
  123. jsonObject.put("ring", config.ring);
  124. jsonObject.put("vibrate", config.vibrate);
  125. jsonObject.put("notificationSmallIconId", config.notificationSmallIconId);
  126. jsonObject.put("notificationSound", config.notificationSound);
  127. jsonObject.put("hideContent", config.hideContent);
  128. jsonObject.put("ledargb", config.ledARGB);
  129. jsonObject.put("ledonms", config.ledOnMs);
  130. jsonObject.put("ledoffms", config.ledOffMs);
  131. jsonObject.put("titleOnlyShowAppName", config.titleOnlyShowAppName);
  132. jsonObject.put("notificationFolded", config.notificationFolded);
  133. jsonObject.put("notificationEntrance", config.notificationEntrance.getName());
  134. jsonObject.put("notificationColor", config.notificationColor);
  135. } catch (Exception e) {
  136. e.printStackTrace();
  137. }
  138. editor.putString(key, jsonObject.toString());
  139. editor.commit();
  140. }
  141. private static boolean getBoolean(String key, boolean value) {
  142. return getSharedPreferences().getBoolean(key, value);
  143. }
  144. private static void saveBoolean(String key, boolean value) {
  145. SharedPreferences.Editor editor = getSharedPreferences().edit();
  146. editor.putBoolean(key, value);
  147. editor.commit();
  148. }
  149. private static void saveLong(String key, long value) {
  150. SharedPreferences.Editor editor = getSharedPreferences().edit();
  151. editor.putLong(key, value);
  152. editor.commit();
  153. }
  154. private static long getLong(String key, long value) {
  155. return getSharedPreferences().getLong(key, value);
  156. }
  157. static SharedPreferences getSharedPreferences() {
  158. return SheishuoCache.getContext().getSharedPreferences("Demo." + SheishuoCache.getAccount(), Context.MODE_PRIVATE);
  159. }
  160. }